|
2 | 2 |
|
3 | 3 | package runtime |
4 | 4 |
|
5 | | -import ( |
6 | | - "unsafe" |
7 | | -) |
8 | | - |
9 | 5 | // This function is called at HardFault. |
10 | | -// Before this function is called, the stack pointer is reset to the initial |
11 | | -// stack pointer (loaded from address 0x0) and the previous stack pointer is |
12 | | -// passed as an argument to this function. This allows for easy inspection of |
13 | | -// the stack the moment a HardFault occurs, but it means that the stack will be |
14 | | -// corrupted by this function and thus this handler must not attempt to recover. |
15 | 6 | // |
16 | 7 | // For details, see: |
17 | 8 | // https://community.arm.com/developer/ip-products/system/f/embedded-forum/3257/debugging-a-cortex-m0-hard-fault |
18 | 9 | // https://blog.feabhas.com/2013/02/developing-a-generic-hard-fault-handler-for-arm-cortex-m3cortex-m4/ |
19 | 10 | // |
20 | | -//export handleHardFault |
21 | | -func handleHardFault(sp *interruptStack) { |
22 | | - print("fatal error: ") |
23 | | - if uintptr(unsafe.Pointer(sp)) < 0x20000000 { |
24 | | - print("stack overflow") |
25 | | - } else { |
26 | | - // TODO: try to find the cause of the hard fault. Especially on |
27 | | - // Cortex-M3 and higher it is possible to find more detailed information |
28 | | - // in special status registers. |
29 | | - print("HardFault") |
30 | | - } |
31 | | - print(" with sp=", sp) |
32 | | - if uintptr(unsafe.Pointer(&sp.PC)) >= 0x20000000 { |
33 | | - // Only print the PC if it points into memory. |
34 | | - // It may not point into memory during a stack overflow, so check that |
35 | | - // first before accessing the stack. |
36 | | - print(" pc=", sp.PC) |
37 | | - } |
| 11 | +//export HardFault_Handler |
| 12 | +func HardFault_Handler() { |
| 13 | + // Obtain the stack pointer as it was on entry to the HardFault. It contains |
| 14 | + // the registers that were pushed by the NVIC and that we can now read back |
| 15 | + // to print the PC value at the time of the hard fault, for example. |
| 16 | + sp := (*interruptStack)(llvm_sponentry()) |
| 17 | + |
| 18 | + // Note: by reusing the string "panic: runtime error at " we save a little |
| 19 | + // bit in terms of code size as the string can be deduplicated. |
| 20 | + print("panic: runtime error at ", sp.PC, ": HardFault with sp=", sp) |
| 21 | + // TODO: try to find the cause of the hard fault. Especially on Cortex-M3 |
| 22 | + // and higher it is possible to find more detailed information in special |
| 23 | + // status registers. |
38 | 24 | println() |
39 | 25 | abort() |
40 | 26 | } |
0 commit comments