1 //==========================================================================
5 // HAL miscellaneous functions
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 // Copyright (C) 2006 eCosCentric Ltd.
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
36 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //==========================================================================
39 //#####DESCRIPTIONBEGIN####
41 // Author(s): Enrico Piria
44 // Purpose: Miscellaneous routine and variable definitions.
46 //####DESCRIPTIONEND####
47 //========================================================================
49 #include <pkgconf/hal.h>
51 #include <cyg/infra/cyg_type.h>
52 #include <cyg/infra/cyg_trac.h> // tracing macros
53 #include <cyg/infra/cyg_ass.h> // assertion macros
54 #include <cyg/infra/diag.h> // diag_printf
56 #include <cyg/hal/hal_arch.h> // HAL header
58 #include <cyg/hal/hal_intr.h> // VSR/ISR defines
60 // -------------------------------------------------------------------------
63 volatile CYG_ADDRESS cyg_hal_interrupt_handlers[CYGNUM_HAL_ISR_COUNT];
64 volatile CYG_ADDRWORD cyg_hal_interrupt_data[CYGNUM_HAL_ISR_COUNT];
65 volatile CYG_ADDRESS cyg_hal_interrupt_objects[CYGNUM_HAL_ISR_COUNT];
67 // -------------------------------------------------------------------------
70 externC void __handle_exception(void);
72 externC HAL_SavedRegisters * _hal_registers;
74 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
75 externC void* volatile __mem_fault_handler;
78 // Defined in variant HAL
79 externC void hal_interrupt_update_level(void);
81 // --------------------------------------------------------------------------
82 // Default exception handler.
84 void hal_exception_handler(CYG_WORD vector, HAL_SavedRegisters *regs)
87 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
89 // If we caught an exception inside the stubs, see if we were expecting it
90 // and if so jump to the saved address.
91 if (__mem_fault_handler) {
92 regs->pc = (CYG_ADDRWORD)__mem_fault_handler;
93 // Caught an exception inside stubs
97 // Set the pointer to the registers of the current exception
98 // context. At entry the GDB stub will expand the
99 // HAL_SavedRegisters structure into a (bigger) register array.
100 _hal_registers = regs;
102 __handle_exception();
104 #elif defined(CYGFUN_HAL_COMMON_KERNEL_SUPPORT) && \
105 defined(CYGPKG_HAL_EXCEPTIONS)
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.
112 cyg_hal_deliver_exception(vector, (CYG_ADDRWORD)regs);
116 CYG_FAIL("Exception!!!");
123 // --------------------------------------------------------------------------
124 // Default ISR handler.
126 cyg_uint32 hal_arch_default_isr(CYG_ADDRWORD vector, CYG_ADDRWORD data)
128 CYG_FAIL("Unexpected ISR");
132 // --------------------------------------------------------------------------
133 // Default spurious interrupt handler. This routine is called with all
134 // interrupts disabled.
136 #ifndef CYGIMP_HAL_COMMON_INTERRUPTS_IGNORE_SPURIOUS
138 void hal_spurious_interrupt(HAL_SavedRegisters *regs) CYGBLD_ATTRIB_WEAK;
140 void hal_spurious_interrupt(HAL_SavedRegisters *regs)
142 CYG_FAIL("Spurious interrupt!!");
147 // --------------------------------------------------------------------------
148 // Idle thread action.
150 void hal_idle_thread_action(cyg_uint32 count)
154 // -----------------------------------------------------------------------
155 // Determine the index of the ls bit of the supplied mask.
157 cyg_uint32 hal_lsbit_index(cyg_uint32 mask)
161 static const signed char tab[64] =
162 { -1, 0, 1, 12, 2, 6, 0, 13, 3, 0, 7, 0, 0, 0, 0, 14, 10,
163 4, 0, 0, 8, 0, 0, 25, 0, 0, 0, 0, 0, 21, 27 , 15, 31, 11,
164 5, 0, 0, 0, 0, 0, 9, 0, 0, 24, 0, 0 , 20, 26, 30, 0, 0, 0,
165 0, 23, 0, 19, 29, 0, 22, 18, 28, 17, 16, 0
176 // -----------------------------------------------------------------------
177 // Determine the index of the ms bit of the supplied mask.
179 cyg_uint32 hal_msbit_index(cyg_uint32 mask)
184 // Phase 1: make word with all ones from that one to the right
191 // Phase 2: calculate number of "1" bits in the word
192 w = (x & 0x55555555) + ((x >> 1) & 0x55555555);
193 w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
195 w = (w & 0x000F000F) + ((w >> 8) & 0x000F000F);
196 return (cyg_uint32)((w + (w >> 16)) & 0xFF) - 1;