OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [hal/] [openrisc/] [arch/] [current/] [src/] [hal_misc.c] - Blame information for rev 27

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      hal_misc.c
4
//
5
//      HAL miscellaneous functions
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under
14
// the terms of the GNU General Public License as published by the Free
15
// Software Foundation; either version 2 or (at your option) any later version.
16
//
17
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20
// for more details.
21
//
22
// You should have received a copy of the GNU General Public License along
23
// with eCos; if not, write to the Free Software Foundation, Inc.,
24
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25
//
26
// As a special exception, if other files instantiate templates or use macros
27
// or inline functions from this file, or you compile this file and link it
28
// with other works to produce a work based on this file, this file does not
29
// by itself cause the resulting work to be covered by the GNU General Public
30
// License. However the source code for this file must still be made available
31
// in accordance with section (3) of the GNU General Public License.
32
//
33
// This exception does not invalidate any other reasons why a work based on
34
// this file might be covered by the GNU General Public License.
35
//
36
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37
// at http://sources.redhat.com/ecos/ecos-license/
38
// -------------------------------------------
39
//####ECOSGPLCOPYRIGHTEND####
40
//==========================================================================
41
//#####DESCRIPTIONBEGIN####
42
//
43
// Author(s):    nickg
44
// Contributors: nickg, jlarmour
45
// Date:         1999-01-21
46
// Purpose:      HAL miscellaneous functions
47
// Description:  This file contains miscellaneous functions provided by the
48
//               HAL.
49
//
50
//####DESCRIPTIONEND####
51
//
52
//========================================================================*/
53
 
54
#include <pkgconf/hal.h>
55
 
56
#include <cyg/infra/cyg_type.h>         // Base types
57
#include <cyg/infra/cyg_trac.h>         // tracing macros
58
#include <cyg/infra/cyg_ass.h>          // assertion macros
59
 
60
#define CYGARC_HAL_COMMON_EXPORT_CPU_MACROS
61
#include <cyg/hal/hal_arch.h>           // architectural definitions
62
#include <cyg/hal/hal_intr.h>           // Interrupt handling
63
#include <cyg/hal/hal_if.h>             // hal_ctrlc_isr()
64
 
65
#include CYGHWR_MEMORY_LAYOUT_H
66
 
67
/*------------------------------------------------------------------------*/
68
/* If required, define a variable to store the clock period.              */
69
 
70
#ifdef CYGHWR_HAL_CLOCK_PERIOD_DEFINED
71
 
72
CYG_WORD32 cyg_hal_clock_period;
73
 
74
#endif
75
 
76
/*------------------------------------------------------------------------*/
77
/* First level C exception handler.                                       */
78
 
79
externC void __handle_exception (void);
80
 
81
externC HAL_SavedRegisters *_hal_registers;
82
 
83
externC void* volatile __mem_fault_handler;
84
 
85
externC cyg_uint8 cyg_hal_mips_process_fpe( HAL_SavedRegisters *regs );
86
 
87
externC cyg_uint32 cyg_hal_exception_handler(HAL_SavedRegisters *regs)
88
{
89
#if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
90
 
91
    // If we caught an exception inside the stubs, see if we were expecting it
92
    // and if so jump to the saved address
93
    if (__mem_fault_handler) {
94
        regs->pc = (CYG_HAL_OPENRISC_REG)(signed long)__mem_fault_handler;
95
        return 0; // Caught an exception inside stubs        
96
    }
97
 
98
    // Set the pointer to the registers of the current exception
99
    // context. At entry the GDB stub will expand the
100
    // HAL_SavedRegisters structure into a (bigger) register array.
101
    _hal_registers = regs;
102
    __handle_exception();
103
 
104
#elif defined(CYGFUN_HAL_COMMON_KERNEL_SUPPORT) && defined(CYGPKG_HAL_EXCEPTIONS)
105
 
106
    // We should decode the vector and pass a more appropriate
107
    // value as the second argument. For now we simply pass a
108
    // pointer to the saved registers. We should also divert
109
    // breakpoint and other debug vectors into the debug stubs.
110
 
111
    cyg_hal_deliver_exception( regs->vector, (CYG_ADDRWORD)regs );
112
 
113
#else
114
 
115
    CYG_FAIL("Exception!!!");
116
 
117
#endif    
118
    return 0;
119
}
120
 
121
/*------------------------------------------------------------------------*/
122
/* default ISR                                                            */
123
 
124
externC cyg_uint32
125
hal_arch_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
126
{
127
    return 0;
128
}
129
 
130
/*------------------------------------------------------------------------*/
131
// Come here if interrupt triggered, but no apparent cause
132
void hal_spurious_IRQ(HAL_SavedRegisters *regs) CYGBLD_ATTRIB_WEAK;
133
void
134
hal_spurious_IRQ(HAL_SavedRegisters *regs)
135
{
136
#if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
137
    cyg_hal_exception_handler(regs);
138
#else
139
    CYG_FAIL("Spurious interrupt!!");
140
#endif    
141
}
142
/*------------------------------------------------------------------------*/
143
 
144
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
145
cyg_bool cyg_hal_stop_constructors;
146
#endif
147
 
148
typedef void (*pfunc) (void);
149
extern pfunc __CTOR_LIST__[];
150
extern pfunc __CTOR_END__[];
151
 
152
void
153
cyg_hal_invoke_constructors(void)
154
{
155
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
156
    static pfunc *p = &__CTOR_END__[-1];
157
 
158
    cyg_hal_stop_constructors = 0;
159
    for (; p >= __CTOR_LIST__; p--) {
160
        (*p) ();
161
        if (cyg_hal_stop_constructors) {
162
            p--;
163
            break;
164
        }
165
    }
166
#else
167
    pfunc *p;
168
 
169
    for (p = &__CTOR_END__[-1]; p >= __CTOR_LIST__; p--)
170
        (*p) ();
171
#endif
172
 
173
} // cyg_hal_invoke_constructors()
174
 
175
/*------------------------------------------------------------------------*/
176
/* Determine the index of the ls bit of the supplied mask.                */
177
 
178
cyg_uint32 hal_lsbit_index(cyg_uint32 mask)
179
{
180
    cyg_uint32 n = mask;
181
 
182
    static const signed char tab[64] =
183
    { -1, 0, 1, 12, 2, 6, 0, 13, 3, 0, 7, 0, 0, 0, 0, 14, 10,
184
      4, 0, 0, 8, 0, 0, 25, 0, 0, 0, 0, 0, 21, 27 , 15, 31, 11,
185
      5, 0, 0, 0, 0, 0, 9, 0, 0, 24, 0, 0 , 20, 26, 30, 0, 0, 0,
186
      0, 23, 0, 19, 29, 0, 22, 18, 28, 17, 16, 0
187
    };
188
 
189
    n &= ~(n-1UL);
190
    n = (n<<16)-n;
191
    n = (n<<6)+n;
192
    n = (n<<4)+n;
193
 
194
    return tab[n>>26];
195
}
196
 
197
/*------------------------------------------------------------------------*/
198
/* Determine the index of the ms bit of the supplied mask.                */
199
 
200
cyg_uint32 hal_msbit_index(cyg_uint32 mask)
201
{
202
    cyg_uint32 x = mask;
203
    cyg_uint32 w;
204
 
205
    /* Phase 1: make word with all ones from that one to the right */
206
    x |= x >> 16;
207
    x |= x >> 8;
208
    x |= x >> 4;
209
    x |= x >> 2;
210
    x |= x >> 1;
211
 
212
    /* Phase 2: calculate number of "1" bits in the word        */
213
    w = (x & 0x55555555) + ((x >> 1) & 0x55555555);
214
    w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
215
    w = w + (w >> 4);
216
    w = (w & 0x000F000F) + ((w >> 8) & 0x000F000F);
217
    return (cyg_uint32)((w + (w >> 16)) & 0xFF) - 1;
218
 
219
}
220
 
221
/*------------------------------------------------------------------------*/
222
/* Delay for some number of useconds.                                     */
223
void
224
hal_delay_us(int us)
225
{
226
    cyg_uint32 val1, val2;
227
    int diff;
228
    long usticks;
229
    long ticks;
230
 
231
    // Calculate the number of counter register ticks per microsecond.
232
 
233
    usticks = (CYGNUM_HAL_RTC_PERIOD * CYGNUM_HAL_RTC_DENOMINATOR) / 1000000;
234
 
235
    // Make sure that the value is not zero.
236
    if( usticks == 0 ) usticks = 1;
237
 
238
    while( us > 0 )
239
    {
240
        int us1 = us;
241
 
242
        // Wait in bursts of less than 10000us to avoid any overflow
243
        // problems in the multiply.
244
        if( us1 > 10000 )
245
            us1 = 10000;
246
 
247
        us -= us1;
248
 
249
        ticks = us1 * usticks;
250
 
251
        HAL_CLOCK_READ(&val1);
252
        while (ticks > 0) {
253
            do {
254
                HAL_CLOCK_READ(&val2);
255
            } while (val1 == val2);
256
            diff = val2 - val1;
257
            if (diff < 0) diff += CYGNUM_HAL_RTC_PERIOD;
258
            ticks -= diff;
259
            val1 = val2;
260
        }
261
    }
262
}
263
 
264
/*------------------------------------------------------------------------*/
265
 
266
void hal_arch_program_new_stack(void *_func)
267
{
268
    externC void hal_program_new_stack( void *func, CYG_ADDRESS addr);
269
    hal_program_new_stack( (void *)_func,
270
                   (CYGMEM_REGION_ram + CYGMEM_REGION_ram_SIZE - sizeof(CYG_ADDRESS)) & ~7 );
271
}
272
 
273
/*------------------------------------------------------------------------*/
274
/* Idle thread action                                                     */
275
 
276
#include <cyg/infra/diag.h>
277
 
278
void hal_idle_thread_action( cyg_uint32 count )
279
{
280
}
281
 
282
/*------------------------------------------------------------------------*/
283
/* End of hal_misc.c                                                      */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.