uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
handlers.c
Go to the documentation of this file.
1 #include <stdint.h>
2 #include <FreeRTOS.h>
3 #include <task.h>
4 #include <queue.h>
5 #include <osdebug.h>
6 #include <core_cm3.h>
7 
8 void BoardExceptionHandler(int code);
9 
10 enum FaultType {
12 };
13 
15  uint32_t r0, r1, r2, r3, r12, lr, pc, xPSR;
16 };
17 
19  uint32_t r4, r5, r6, r7, r8, r9, r10, r11;
20 };
21 
22 static void print_fault_data(struct fault_data_cpu_t * fault_data_cpu, struct fault_data_extra_t * fault_data_extra) {
23  // These ones are saved naturally by the CPU
24  DBGOUT("fd_cpu = %p - fd_extra = %p\r\n", fault_data_cpu, fault_data_extra);
25  DBGOUT("pc: %p\r\n", fault_data_cpu->pc);
26  DBGOUT("sp = %p - lr = %p - r12 = %p -xPSR = %p\r\n", fault_data_cpu + 1, fault_data_cpu->lr, fault_data_cpu->r12, fault_data_cpu->xPSR);
27  DBGOUT("r0 = %p - r1 = %p - r2 = %p - r3 = %p\r\n", fault_data_cpu->r0, fault_data_cpu->r1, fault_data_cpu->r2, fault_data_cpu->r3);
28  // These ones are saved by the ASM handler; hence the reason they are before in memory.
29  DBGOUT("r4 = %p - r5 = %p - r6 = %p - r7 = %p\r\n", fault_data_extra->r4, fault_data_extra->r5, fault_data_extra->r6, fault_data_extra->r7);
30  DBGOUT("r8 = %p - r9 = %p - r10 = %p - r11 = %p\r\n", fault_data_extra->r8, fault_data_extra->r9, fault_data_extra->r10, fault_data_extra->r11);
31 }
32 
33 void general_C_handler(enum FaultType fault, struct fault_data_extra_t * fault_data_extra, uintptr_t lr) {
34  uintptr_t eflags = lr & 15;
35  uint8_t MMFSR = SCB->CFSR & 0xff;
36  uint8_t BFSR = (SCB->CFSR >> 8) & 0xff;
37  uint16_t UFSR = (SCB->CFSR >> 16) & 0xffff;
38  (void) MMFSR;
39  (void) BFSR;
40  (void) UFSR;
41  struct fault_data_cpu_t * fault_data_cpu = (struct fault_data_cpu_t *) ((eflags & 4) ? (void *) __get_PSP() : (void *) (fault_data_extra + 1));
42  DBGOUT("***FAULT***\r\neflags = 0x0%x\r\nPSP = %p\r\nType: ", eflags, __get_PSP());
43  switch (fault) {
44  case NMI:
45  DBGOUT("NMI\r\n");
46  break;
47  case HardFault:
48  DBGOUT("HardFault\r\n");
49  break;
50  case MemManage:
51  DBGOUT("MemManage\r\n");
52  break;
53  case BusFault:
54  DBGOUT("BusFault\r\n");
55  break;
56  case UsageFault:
57  DBGOUT("UsageFault\r\n");
58  break;
59  default:
60  DBGOUT("Unknown\r\n");
61  }
62 
63  DBGOUT("*MMFSR* MMARVALID: %d - MSTKERR: %d - MUNSTKERR: %d - DACCVIOL: %d - IACCVIOL: %d\r\n", MMFSR & 128 ? 1 : 0, MMFSR & 16 ? 1 : 0, MMFSR & 8 ? 1 : 0, MMFSR & 2 ? 1 : 0, MMFSR & 1);
64  DBGOUT("*BFSR* BFARVALID: %d - STKERR: %d - UNSTKERR: %d - IMPREISERR: %d - PRECISERR: %d - IBUSERR: %d\r\n", BFSR & 128 ? 1 : 0, BFSR & 16 ? 1 : 0, BFSR & 8 ? 1 : 0, BFSR & 4 ? 1 : 0, BFSR & 2 ? 1 : 0, BFSR & 1);
65  DBGOUT("*UFSR* DIVBYZERO: %d - UNALIGNED: %d - NOCP: %d - INVPC: %d - INVSTATE: %d - UNDEFINSTR: %d\r\n", UFSR & 512 ? 1 : 0, UFSR & 256 ? 1 : 0, UFSR & 8 ? 1 : 0, UFSR & 4 ? 1 : 0, UFSR & 2 ? 1 : 0, UFSR & 1);
66  DBGOUT("*HFSR* DEBUGEVT: %d - FORCED: %d - VECTBL: %d\r\n", SCB->HFSR & 0x80000000 ? 1 : 0, SCB->HFSR & 0x40000000 ? 1 : 0, SCB->HFSR & 2 ? 1 : 0);
67  DBGOUT("*DFSR* EXTERNAL: %d - VCATCH: %d - DWTTRAP: %d - BKPT: %d - HALTED: %d\r\n", SCB->DFSR & 16 ? 1 : 0, SCB->DFSR & 8 ? 1 : 0, SCB->DFSR & 4 ? 1 : 0, SCB->DFSR & 2 ? 1 : 0, SCB->DFSR & 1);
68  DBGOUT("MMAR = %p - BFAR = %p - AFSR: %d\r\n", SCB->MMFAR, SCB->BFAR, SCB->AFSR);
69 
70  print_fault_data(fault_data_cpu, fault_data_extra);
71 
72  DBGOUT("HALTING\r\n");
74  while(1);
75 }