diff --git a/boards/nrf52840_s140_v7_extrafs.ld b/boards/nrf52840_s140_v7_extrafs.ld index 217f95ab..143063d5 100644 --- a/boards/nrf52840_s140_v7_extrafs.ld +++ b/boards/nrf52840_s140_v7_extrafs.ld @@ -36,7 +36,10 @@ MEMORY * 18. 10. 2026: what DOES survive is the middle of RAM. The clock record now * lives in .noinit (src/helpers/NRF52Board.cpp), which the linker places right * after .bss -- measured on x1 (XIAO, companion): set +30 days, soft `reboot`, - * the clock came back +30 days. The hardfault record should move there too. + * the clock came back +30 days. The hardfault record moved there too + * (g_hardfault_record, handler in NRF52Board.cpp) and was proven on x2: a + * deliberate fault printed its PC on the next boot. These 16 B are no longer + * used by anything; kept only so the RAM layout of existing envs is unchanged. */ RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 0x2003FFF0 - 0x20006000 } diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 5ad36379..9c307cf5 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -1697,6 +1697,13 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply } while (*command == ' ') command++; // skip leading spaces +#ifdef BENCH_FAULT_CMD + // Bench builds only (PLATFORMIO_BUILD_FLAGS=-D BENCH_FAULT_CMD): crash on + // purpose, to prove the hardfault record survives the reset and gets printed. + if (strcmp(command, "hardfault") == 0) { + __builtin_trap(); // UDF -> UsageFault, escalates to HardFault + } +#endif if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI) memcpy(reply, command, 3); // reflect the prefix back diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 579aa442..460cf432 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -739,7 +739,8 @@ void setup() { #if defined(NRF52_PLATFORM) // CUSTOM (TeTeHacko diagnostics): print the hardfault record saved by the // patched HardFault_Handler (framework debug.cpp) before the last reset. - // 16 B at the end of RAM, reserved in nrf52840_s140_v7_extrafs.ld. + // g_hardfault_record in .noinit (NRF52Board.cpp) -- the old fixed 0x2003FFF0 at + // the end of RAM was where the bootloader's stack lands on the following reset. // NOTE: needs MESH_DEBUG or BOOT_DIAG_DELAY_MS to be readable at all (above). // CUSTOM (TeTeHacko): why did this board last restart? The core latches // NRF_POWER->RESETREAS in init() before clearing it, and it is the only thing @@ -764,7 +765,8 @@ void setup() { } { - volatile uint32_t* hf = (volatile uint32_t*)0x2003FFF0; + extern uint32_t g_hardfault_record[4]; // NRF52Board.cpp, .noinit + volatile uint32_t* hf = g_hardfault_record; if (hf[0] == 0xFA010DEB) { Serial.printf("!!! HARDFAULT before last reboot: PC=%08lX LR=%08lX PSR=%08lX\n", (unsigned long)hf[1], (unsigned long)hf[2], (unsigned long)hf[3]); diff --git a/src/helpers/NRF52Board.cpp b/src/helpers/NRF52Board.cpp index 652c7e45..618364c3 100644 --- a/src/helpers/NRF52Board.cpp +++ b/src/helpers/NRF52Board.cpp @@ -12,6 +12,41 @@ extern "C" { void retainedClockInvalidate() { g_retained_clock[0] = 0; } } #endif + +// CUSTOM (TeTeHacko): hardfault recorder. On a fault, save the stacked PC/LR/PSR +// to g_hardfault_record and reset; simple_repeater/main.cpp prints it on the next +// boot. Both halves had to move here (18. 10. 2026): +// - The record lived at a fixed 0x2003FFF0, the top of RAM, which is where the +// Adafruit bootloader's stack lands on the reset after the fault. .noinit +// (after .bss) survives, same as the retained clock. +// - The handler was a patch in the framework's utility/debug.cpp and it NEVER +// linked: the core is built as an archive, the startup's weak +// HardFault_Handler already satisfies the vector table, so the linker never +// pulled debug.o in (nm on any build showed only the weak symbol). An object +// of the application is always linked, so a strong definition here wins. +// debug.o would only come in with CFG_DEBUG (dbgPrintVersion() in the core's +// main.cpp), which would then be a duplicate symbol -- no env sets it. +#define HARDFAULT_MAGIC 0xFA010DEB +extern "C" { + uint32_t g_hardfault_record[4] __attribute__((section(".noinit"))); // magic, pc, lr, psr + + void HardFault_c_handler(uint32_t* sp) { + g_hardfault_record[1] = sp[6]; // stacked PC + g_hardfault_record[2] = sp[5]; // stacked LR + g_hardfault_record[3] = sp[7]; // stacked xPSR + g_hardfault_record[0] = HARDFAULT_MAGIC; + NVIC_SystemReset(); + } + + __attribute__((naked)) void HardFault_Handler(void) { + __asm volatile( + "tst lr, #4 \n" + "ite eq \n" + "mrseq r0, msp \n" + "mrsne r0, psp \n" + "b HardFault_c_handler \n"); + } +} #include #include