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, 2006 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): Enrico Piria
|
43 |
|
|
// Contributors:
|
44 |
|
|
// Date: 2005-25-06
|
45 |
|
|
// Purpose: Miscellaneous routine and variable definitions.
|
46 |
|
|
//
|
47 |
|
|
//####DESCRIPTIONEND####
|
48 |
|
|
//========================================================================
|
49 |
|
|
|
50 |
|
|
#include <pkgconf/hal.h>
|
51 |
|
|
|
52 |
|
|
#include <cyg/infra/cyg_type.h>
|
53 |
|
|
#include <cyg/infra/cyg_trac.h> // tracing macros
|
54 |
|
|
#include <cyg/infra/cyg_ass.h> // assertion macros
|
55 |
|
|
#include <cyg/infra/diag.h> // diag_printf
|
56 |
|
|
|
57 |
|
|
#include <cyg/hal/hal_arch.h> // HAL header
|
58 |
|
|
|
59 |
|
|
#include <cyg/hal/hal_intr.h> // VSR/ISR defines
|
60 |
|
|
|
61 |
|
|
// -------------------------------------------------------------------------
|
62 |
|
|
// ISR table
|
63 |
|
|
|
64 |
|
|
volatile CYG_ADDRESS cyg_hal_interrupt_handlers[CYGNUM_HAL_ISR_COUNT];
|
65 |
|
|
volatile CYG_ADDRWORD cyg_hal_interrupt_data[CYGNUM_HAL_ISR_COUNT];
|
66 |
|
|
volatile CYG_ADDRESS cyg_hal_interrupt_objects[CYGNUM_HAL_ISR_COUNT];
|
67 |
|
|
|
68 |
|
|
// -------------------------------------------------------------------------
|
69 |
|
|
// VSR table
|
70 |
|
|
|
71 |
|
|
externC void __handle_exception(void);
|
72 |
|
|
|
73 |
|
|
externC HAL_SavedRegisters * _hal_registers;
|
74 |
|
|
|
75 |
|
|
#ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
|
76 |
|
|
externC void* volatile __mem_fault_handler;
|
77 |
|
|
#endif
|
78 |
|
|
|
79 |
|
|
// Defined in variant HAL
|
80 |
|
|
externC void hal_interrupt_update_level(void);
|
81 |
|
|
|
82 |
|
|
// --------------------------------------------------------------------------
|
83 |
|
|
// Default exception handler.
|
84 |
|
|
|
85 |
|
|
void hal_exception_handler(CYG_WORD vector, HAL_SavedRegisters *regs)
|
86 |
|
|
{
|
87 |
|
|
|
88 |
|
|
#ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
|
89 |
|
|
|
90 |
|
|
// If we caught an exception inside the stubs, see if we were expecting it
|
91 |
|
|
// and if so jump to the saved address.
|
92 |
|
|
if (__mem_fault_handler) {
|
93 |
|
|
regs->pc = (CYG_ADDRWORD)__mem_fault_handler;
|
94 |
|
|
// Caught an exception inside stubs
|
95 |
|
|
return;
|
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 |
|
|
|
103 |
|
|
__handle_exception();
|
104 |
|
|
|
105 |
|
|
#elif defined(CYGFUN_HAL_COMMON_KERNEL_SUPPORT) && \
|
106 |
|
|
defined(CYGPKG_HAL_EXCEPTIONS)
|
107 |
|
|
|
108 |
|
|
// We should decode the vector and pass a more appropriate
|
109 |
|
|
// value as the second argument. For now we simply pass a
|
110 |
|
|
// pointer to the saved registers. We should also divert
|
111 |
|
|
// breakpoint and other debug vectors into the debug stubs.
|
112 |
|
|
|
113 |
|
|
cyg_hal_deliver_exception(vector, (CYG_ADDRWORD)regs);
|
114 |
|
|
|
115 |
|
|
#else
|
116 |
|
|
|
117 |
|
|
CYG_FAIL("Exception!!!");
|
118 |
|
|
|
119 |
|
|
#endif
|
120 |
|
|
|
121 |
|
|
return;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
// --------------------------------------------------------------------------
|
125 |
|
|
// Default ISR handler.
|
126 |
|
|
|
127 |
|
|
cyg_uint32 hal_arch_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
|
128 |
|
|
{
|
129 |
|
|
CYG_FAIL("Unexpected ISR");
|
130 |
|
|
return 0;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
// --------------------------------------------------------------------------
|
134 |
|
|
// Default spurious interrupt handler. This routine is called with all
|
135 |
|
|
// interrupts disabled.
|
136 |
|
|
|
137 |
|
|
#ifndef CYGIMP_HAL_COMMON_INTERRUPTS_IGNORE_SPURIOUS
|
138 |
|
|
|
139 |
|
|
void hal_spurious_interrupt(HAL_SavedRegisters *regs) CYGBLD_ATTRIB_WEAK;
|
140 |
|
|
|
141 |
|
|
void hal_spurious_interrupt(HAL_SavedRegisters *regs)
|
142 |
|
|
{
|
143 |
|
|
CYG_FAIL("Spurious interrupt!!");
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
#endif
|
147 |
|
|
|
148 |
|
|
// --------------------------------------------------------------------------
|
149 |
|
|
// Idle thread action.
|
150 |
|
|
|
151 |
|
|
void hal_idle_thread_action(cyg_uint32 count)
|
152 |
|
|
{
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
// -----------------------------------------------------------------------
|
156 |
|
|
// Determine the index of the ls bit of the supplied mask.
|
157 |
|
|
|
158 |
|
|
cyg_uint32 hal_lsbit_index(cyg_uint32 mask)
|
159 |
|
|
{
|
160 |
|
|
cyg_uint32 n = mask;
|
161 |
|
|
|
162 |
|
|
static const signed char tab[64] =
|
163 |
|
|
{ -1, 0, 1, 12, 2, 6, 0, 13, 3, 0, 7, 0, 0, 0, 0, 14, 10,
|
164 |
|
|
4, 0, 0, 8, 0, 0, 25, 0, 0, 0, 0, 0, 21, 27 , 15, 31, 11,
|
165 |
|
|
5, 0, 0, 0, 0, 0, 9, 0, 0, 24, 0, 0 , 20, 26, 30, 0, 0, 0,
|
166 |
|
|
0, 23, 0, 19, 29, 0, 22, 18, 28, 17, 16, 0
|
167 |
|
|
};
|
168 |
|
|
|
169 |
|
|
n &= ~(n-1UL);
|
170 |
|
|
n = (n<<16)-n;
|
171 |
|
|
n = (n<<6)+n;
|
172 |
|
|
n = (n<<4)+n;
|
173 |
|
|
|
174 |
|
|
return tab[n>>26];
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
// -----------------------------------------------------------------------
|
178 |
|
|
// Determine the index of the ms bit of the supplied mask.
|
179 |
|
|
|
180 |
|
|
cyg_uint32 hal_msbit_index(cyg_uint32 mask)
|
181 |
|
|
{
|
182 |
|
|
cyg_uint32 x = mask;
|
183 |
|
|
cyg_uint32 w;
|
184 |
|
|
|
185 |
|
|
// Phase 1: make word with all ones from that one to the right
|
186 |
|
|
x |= x >> 16;
|
187 |
|
|
x |= x >> 8;
|
188 |
|
|
x |= x >> 4;
|
189 |
|
|
x |= x >> 2;
|
190 |
|
|
x |= x >> 1;
|
191 |
|
|
|
192 |
|
|
// Phase 2: calculate number of "1" bits in the word
|
193 |
|
|
w = (x & 0x55555555) + ((x >> 1) & 0x55555555);
|
194 |
|
|
w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
|
195 |
|
|
w = w + (w >> 4);
|
196 |
|
|
w = (w & 0x000F000F) + ((w >> 8) & 0x000F000F);
|
197 |
|
|
return (cyg_uint32)((w + (w >> 16)) & 0xFF) - 1;
|
198 |
|
|
|
199 |
|
|
}
|