37 #include <AppHardwareApi.h>
39 #include "exceptions.h"
41 #ifndef EXCEPTION_STALLS_SYSTEM
42 #define EXCEPTION_STALLS_SYSTEM 0
45 #ifndef PRINT_STACK_ON_REBOOT
46 #define PRINT_STACK_ON_REBOOT 1
50 #ifndef EXC_DUMP_STACK
51 #define EXC_DUMP_STACK
60 #if (defined JENNIC_CHIP_FAMILY_JN514x)
61 #define EXCEPTION_VECTORS_LOCATION_RAM
62 #elif (defined JENNIC_CHIP_FAMILY_JN516x)
63 #define EXCEPTION_VECTORS_LOCATION_FLASH
65 #error Unsupported chip family selected
68 #if (defined EXCEPTION_VECTORS_LOCATION_RAM)
71 #define BUS_ERROR *((volatile uint32 *)(0x4000000))
72 #define TICK_TIMER *((volatile uint32 *)(0x4000004))
73 #define UNALIGNED_ACCESS *((volatile uint32 *)(0x4000008))
74 #define ILLEGAL_INSTRUCTION *((volatile uint32 *)(0x400000c))
75 #define EXTERNAL_INTERRUPT *((volatile uint32 *)(0x4000010))
76 #define SYSCALL *((volatile uint32 *)(0x4000014))
77 #define TRAP *((volatile uint32 *)(0x4000018))
78 #define GENERIC *((volatile uint32 *)(0x400001c))
79 #define STACK_OVERFLOW *((volatile uint32 *)(0x4000020))
80 #elif (defined EXCEPTION_VECTORS_LOCATION_FLASH)
83 #error Unknown exception vector location
88 #define PROGRAM_COUNTER 18
89 #define EFFECTIVE_ADDR 19
95 #if defined(JENNIC_CHIP_JN5148) || defined(JENNIC_CHIP_JN5148J01)
96 #define EXCEPTION_RAM_TOP 0x04020000
98 #define EXCEPTION_RAM_TOP 0x04008000
101 static void exception_handler(uint32 *pu32Stack, eExceptionType eType);
102 static void *heap_alloc_overflow_protect(
void *pvPointer, uint32 u32Size, bool_t bClear);
104 #if PRINT_STACK_ON_REBOOT
105 static void hexprint(uint8 v);
106 static void hexprint32(uint32 v);
107 static void printstring(
const char *s);
111 static const char *debug_filename =
"nothing";
112 static int debug_line = -1;
115 debug_file_line(
const char *file,
int line)
117 debug_filename = file;
120 extern uint32 heap_location;
121 extern void *(*prHeap_AllocFunc)(
void *, uint32, bool_t);
122 PRIVATE
void *(*prHeap_AllocOrig)(
void *, uint32, bool_t);
126 extern void *stack_low_water_mark;
132 #if PRINT_STACK_ON_REBOOT
133 #include "dev/uart0.h"
134 #define printchar(X) uart0_write_direct(X)
139 const char hexconv[] =
"0123456789abcdef";
140 printchar(hexconv[v >> 4]);
141 printchar(hexconv[v & 0x0f]);
147 hexprint(((uint32)v) >> (uint32)24);
148 hexprint(((uint32)v) >> (uint32)16);
149 hexprint(((uint32)v) >> (uint32)8);
150 hexprint((v) & 0xff);
154 printstring(
const char *s)
180 #ifdef EXCEPTION_VECTORS_LOCATION_RAM
182 BUS_ERROR = (uint32)exception_handler;
183 UNALIGNED_ACCESS = (uint32)exception_handler;
184 ILLEGAL_INSTRUCTION = (uint32)exception_handler;
185 SYSCALL = (uint32)exception_handler;
186 TRAP = (uint32)exception_handler;
187 GENERIC = (uint32)exception_handler;
188 STACK_OVERFLOW = (uint32)exception_handler;
191 prHeap_AllocOrig = prHeap_AllocFunc;
192 prHeap_AllocFunc = heap_alloc_overflow_protect;
194 #ifdef EXCEPTION_VECTORS_LOCATION_FLASH
199 vException_BusError(uint32 *pu32Stack, eExceptionType eType)
201 exception_handler(pu32Stack, eType);
204 vException_UnalignedAccess(uint32 *pu32Stack, eExceptionType eType)
206 exception_handler(pu32Stack, eType);
209 vException_IllegalInstruction(uint32 *pu32Stack, eExceptionType eType)
211 exception_handler(pu32Stack, eType);
214 vException_SysCall(uint32 *pu32Stack, eExceptionType eType)
216 exception_handler(pu32Stack, eType);
219 vException_Trap(uint32 *pu32Stack, eExceptionType eType)
221 exception_handler(pu32Stack, eType);
224 vException_StackOverflow(uint32 *pu32Stack, eExceptionType eType)
226 exception_handler(pu32Stack, eType);
244 exception_handler(uint32 *pu32Stack, eExceptionType eType)
246 #if (defined EXC_DUMP_STACK) || (defined EXC_DUMP_REGS)
249 uint32 u32EPCR, u32EEAR, u32Stack;
252 MICRO_DISABLE_INTERRUPTS();
255 case E_EXC_BUS_ERROR:
259 case E_EXC_UNALIGNED_ACCESS:
263 case E_EXC_ILLEGAL_INSTRUCTION:
264 pcString =
"ILLEGAL";
268 pcString =
"SYSCALL";
276 pcString =
"GENERIC";
279 case E_EXC_STACK_OVERFLOW:
284 pcString =
"UNKNOWN";
288 if(bAHI_WatchdogResetEvent()) {
289 pcString =
"WATCHDOG";
294 u32EPCR = pu32Stack[PROGRAM_COUNTER];
295 u32EEAR = pu32Stack[EFFECTIVE_ADDR];
296 u32Stack = pu32Stack[STACK_REG];
299 printstring(
"\n\n\n");
300 printstring(pcString);
301 printstring(
" EXCEPTION @ $");
303 printstring(
" EA: ");
305 printstring(
" SK: ");
306 hexprint32(u32Stack);
307 printstring(
" HP: ");
308 hexprint32(((uint32 *)&heap_location)[0]);
310 printstring(
" File: ");
311 printstring(debug_filename);
312 printstring(
" Line: ");
313 hexprint32(debug_line);
317 printstring(
"\nREGS: ");
319 for(i = 0; i < REG_COUNT; i += 4) {
325 hexprint(pu32Stack[i]);
327 hexprint32(pu32Stack[i + 1]);
329 hexprint32(pu32Stack[i + 2]);
331 hexprint32(pu32Stack[i + 3]);
336 #ifdef EXC_DUMP_STACK
338 printstring(
"\nRAM top: ");
339 hexprint32(EXCEPTION_RAM_TOP);
340 printstring(
"\nSTACK: \n");
341 pu32Stack = (uint32 *)(u32Stack & 0xFFFFFFF0);
342 for(i = 0; (pu32Stack + i) < (uint32 *)(EXCEPTION_RAM_TOP); i += 4) {
344 hexprint32((uint32)(pu32Stack + i));
346 hexprint32(pu32Stack[i]);
348 hexprint32(pu32Stack[i + 1]);
350 hexprint32(pu32Stack[i + 2]);
352 hexprint32(pu32Stack[i + 3]);
357 #if EXCEPTION_STALLS_SYSTEM
362 vAHI_WatchdogException(0);
384 heap_alloc_overflow_protect(
void *pvPointer, uint32 u32Size, bool_t bClear)
388 pvAlloc = prHeap_AllocOrig(pvPointer, u32Size, bClear);
395 vAHI_SetStackOverflow(
TRUE, ((uint32 *)&heap_location)[0]);
#define TRUE
An alias for one, used for clarity.