37#include <AppHardwareApi.h>
40#include "exceptions.h"
42#ifndef EXCEPTION_STALLS_SYSTEM
43#define EXCEPTION_STALLS_SYSTEM 0
46#ifndef PRINT_STACK_ON_REBOOT
47#define PRINT_STACK_ON_REBOOT 1
61#if (defined JENNIC_CHIP_FAMILY_JN514x)
62#define EXCEPTION_VECTORS_LOCATION_RAM
63#elif (defined JENNIC_CHIP_FAMILY_JN516x)
64#define EXCEPTION_VECTORS_LOCATION_FLASH
66#error Unsupported chip family selected
69#if (defined EXCEPTION_VECTORS_LOCATION_RAM)
72#define BUS_ERROR *((volatile uint32 *)(0x4000000))
73#define TICK_TIMER *((volatile uint32 *)(0x4000004))
74#define UNALIGNED_ACCESS *((volatile uint32 *)(0x4000008))
75#define ILLEGAL_INSTRUCTION *((volatile uint32 *)(0x400000c))
76#define EXTERNAL_INTERRUPT *((volatile uint32 *)(0x4000010))
77#define SYSCALL *((volatile uint32 *)(0x4000014))
78#define TRAP *((volatile uint32 *)(0x4000018))
79#define GENERIC *((volatile uint32 *)(0x400001c))
80#define STACK_OVERFLOW *((volatile uint32 *)(0x4000020))
81#elif (defined EXCEPTION_VECTORS_LOCATION_FLASH)
84#error Unknown exception vector location
89#define PROGRAM_COUNTER 18
90#define EFFECTIVE_ADDR 19
96#if defined(JENNIC_CHIP_JN5148) || defined(JENNIC_CHIP_JN5148J01)
97#define EXCEPTION_RAM_TOP 0x04020000
99#define EXCEPTION_RAM_TOP 0x04008000
102static void exception_handler(uint32 *pu32Stack, eExceptionType eType);
103static void *heap_alloc_overflow_protect(
void *pvPointer, uint32 u32Size, bool_t bClear);
105#if PRINT_STACK_ON_REBOOT
106static void hexprint(uint8 v);
107static void hexprint32(uint32 v);
108static void printstring(
const char *s);
112static const char *debug_filename =
"nothing";
113static int debug_line = -1;
116debug_file_line(
const char *file,
int line)
118 debug_filename = file;
121extern uint32 heap_location;
122extern void *(*prHeap_AllocFunc)(
void *, uint32, bool_t);
123PRIVATE
void *(*prHeap_AllocOrig)(
void *, uint32, bool_t);
127extern void *stack_low_water_mark;
133#if PRINT_STACK_ON_REBOOT
134#include "dev/uart0.h"
135#define printchar(X) uart0_write_direct(X)
140 const char hexconv[] =
"0123456789abcdef";
141 printchar(hexconv[v >> 4]);
142 printchar(hexconv[v & 0x0f]);
148 hexprint(((uint32)v) >> (uint32)24);
149 hexprint(((uint32)v) >> (uint32)16);
150 hexprint(((uint32)v) >> (uint32)8);
151 hexprint((v) & 0xff);
155printstring(
const char *s)
181#ifdef EXCEPTION_VECTORS_LOCATION_RAM
183 BUS_ERROR = (uint32)exception_handler;
184 UNALIGNED_ACCESS = (uint32)exception_handler;
185 ILLEGAL_INSTRUCTION = (uint32)exception_handler;
186 SYSCALL = (uint32)exception_handler;
187 TRAP = (uint32)exception_handler;
188 GENERIC = (uint32)exception_handler;
189 STACK_OVERFLOW = (uint32)exception_handler;
192 prHeap_AllocOrig = prHeap_AllocFunc;
193 prHeap_AllocFunc = heap_alloc_overflow_protect;
195#ifdef EXCEPTION_VECTORS_LOCATION_FLASH
200vException_BusError(uint32 *pu32Stack, eExceptionType eType)
202 exception_handler(pu32Stack, eType);
205vException_UnalignedAccess(uint32 *pu32Stack, eExceptionType eType)
207 exception_handler(pu32Stack, eType);
210vException_IllegalInstruction(uint32 *pu32Stack, eExceptionType eType)
212 exception_handler(pu32Stack, eType);
215vException_SysCall(uint32 *pu32Stack, eExceptionType eType)
217 exception_handler(pu32Stack, eType);
220vException_Trap(uint32 *pu32Stack, eExceptionType eType)
222 exception_handler(pu32Stack, eType);
225vException_StackOverflow(uint32 *pu32Stack, eExceptionType eType)
227 exception_handler(pu32Stack, eType);
245exception_handler(uint32 *pu32Stack, eExceptionType eType)
247#if (defined EXC_DUMP_STACK) || (defined EXC_DUMP_REGS)
250 uint32 u32EPCR, u32EEAR, u32Stack;
253 MICRO_DISABLE_INTERRUPTS();
256 case E_EXC_BUS_ERROR:
260 case E_EXC_UNALIGNED_ACCESS:
264 case E_EXC_ILLEGAL_INSTRUCTION:
265 pcString =
"ILLEGAL";
269 pcString =
"SYSCALL";
277 pcString =
"GENERIC";
280 case E_EXC_STACK_OVERFLOW:
285 pcString =
"UNKNOWN";
289 if(bAHI_WatchdogResetEvent()) {
290 pcString =
"WATCHDOG";
295 u32EPCR = pu32Stack[PROGRAM_COUNTER];
296 u32EEAR = pu32Stack[EFFECTIVE_ADDR];
297 u32Stack = pu32Stack[STACK_REG];
300 printstring(
"\n\n\n");
301 printstring(pcString);
302 printstring(
" EXCEPTION @ $");
304 printstring(
" EA: ");
306 printstring(
" SK: ");
307 hexprint32(u32Stack);
308 printstring(
" HP: ");
309 hexprint32(((uint32 *)&heap_location)[0]);
311 printstring(
" File: ");
312 printstring(debug_filename);
313 printstring(
" Line: ");
314 hexprint32(debug_line);
318 printstring(
"\nREGS: ");
320 for(i = 0; i < REG_COUNT; i += 4) {
326 hexprint(pu32Stack[i]);
328 hexprint32(pu32Stack[i + 1]);
330 hexprint32(pu32Stack[i + 2]);
332 hexprint32(pu32Stack[i + 3]);
339 printstring(
"\nRAM top: ");
340 hexprint32(EXCEPTION_RAM_TOP);
341 printstring(
"\nSTACK: \n");
342 pu32Stack = (uint32 *)(u32Stack & 0xFFFFFFF0);
343 for(i = 0; (pu32Stack + i) < (uint32 *)(EXCEPTION_RAM_TOP); i += 4) {
345 hexprint32((uint32)(pu32Stack + i));
347 hexprint32(pu32Stack[i]);
349 hexprint32(pu32Stack[i + 1]);
351 hexprint32(pu32Stack[i + 2]);
353 hexprint32(pu32Stack[i + 3]);
358 if(eType == E_EXC_SYSCALL) {
362#if EXCEPTION_STALLS_SYSTEM
367 vAHI_WatchdogException(0);
389heap_alloc_overflow_protect(
void *pvPointer, uint32 u32Size, bool_t bClear)
393 pvAlloc = prHeap_AllocOrig(pvPointer, u32Size, bClear);
400 vAHI_SetStackOverflow(TRUE, ((uint32 *)&heap_location)[0]);