REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
crash_report.cpp
Go to the documentation of this file.
1
2#include "utils/logging.h"
3#include <CrashReport.h> // core/teensy4
4
5namespace utils {
6
24 if (CrashReport) {
25 // StringPrint buf;
26 // crash.printTo(buf);
27 // buf.str();
28 msg::Log::get().println("ALERT: Previous System crash detected. Post Mortem Report:");
29 msg::Log::get().println(CrashReport);
30 }
31}
32
33// Teensy 4.x (Cortex-M7) hard fault handler with full register dump
34
35extern "C" {
36
37 // This runs in assembly to capture SP before it changes,
38 // then calls our C function with a pointer to the saved frame.
39 __attribute__((naked)) void hard_fault_isr(void) {
40 __asm volatile (
41 // On fault entry, ARM has already pushed {r0-r3, r12, lr, pc, xpsr}
42 // onto either MSP or PSP. Figure out which stack was active:
43 "tst lr, #4 \n" // test EXC_RETURN bit 2
44 "ite eq \n"
45 "mrseq r0, msp \n" // if 0: came from MSP (main stack)
46 "mrsne r0, psp \n" // if 1: came from PSP (thread stack)
47 "b hard_fault_handler_c \n"
48 );
49 }
50
51 // r0 now points to the saved stack frame
53 Serial.println("\n=== HARD FAULT ===");
54 Serial.printf("R0 = 0x%08X\n", frame[0]);
55 Serial.printf("R1 = 0x%08X\n", frame[1]);
56 Serial.printf("R2 = 0x%08X\n", frame[2]);
57 Serial.printf("R3 = 0x%08X\n", frame[3]);
58 Serial.printf("R12 = 0x%08X\n", frame[4]);
59 Serial.printf("LR = 0x%08X\n", frame[5]); // ← return address = caller
60 Serial.printf("PC = 0x%08X\n", frame[6]); // ← where crash happened
61 Serial.printf("PSR = 0x%08X\n", frame[7]);
62
63 // Also dump fault status registers
64 Serial.printf("CFSR = 0x%08X\n", *(volatile uint32_t*)0xE000ED28);
65 Serial.printf("HFSR = 0x%08X\n", *(volatile uint32_t*)0xE000ED2C);
66 Serial.printf("MMFAR = 0x%08X\n", *(volatile uint32_t*)0xE000ED34); // mem fault addr
67 Serial.printf("BFAR = 0x%08X\n", *(volatile uint32_t*)0xE000ED38); // bus fault addr
68
69 Serial.flush();
70 delay(1000);
71 SCB_AIRCR = 0x05FA0004;
72 }
73
74} // extern "C"
75
76} // namespace utils
uint32_t
Definition flasher.cpp:195
void hard_fault_handler_c(uint32_t *frame)
void check_and_log_crash()
Logs out what has been captured by the Teensy CrashReport tooling, cf.
__attribute__((naked)) void hard_fault_isr(void)