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