From 2ced8502a0697e81650f805cb1cf6d36a5abf17e Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Wed, 4 Nov 2026 00:38:43 +0100 Subject: [PATCH] simple_repeater: dfu, radio silence watchdog and boot diagnostics from NodeMaintenance Co-Authored-By: Claude Opus 5.5 --- examples/simple_repeater/MyMesh.cpp | 13 +- examples/simple_repeater/main.cpp | 270 ++-------------------------- src/helpers/NodeMaintenance.cpp | 74 +++++++- 3 files changed, 82 insertions(+), 275 deletions(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index ce1dca7b..f15dac1b 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -1866,18 +1866,15 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply // since a board with no data cable and no BLE cannot be identified at all. extern bool identHandleCommand(const char* command, char* reply); if (identHandleCommand(command, reply)) return; - // CUSTOM (TeTeHacko): `dfu [uf2|serial|ota]` — deterministic reboot into the - // bootloader (GPREGRET magic, same as the core's enterUf2Dfu). Local only. - extern bool dfuHandleCommand(uint32_t sender_timestamp, const char* command, char* reply); - if (dfuHandleCommand(sender_timestamp, command, reply)) return; + // CUSTOM (TeTeHacko): `dfu [uf2|serial|ota]` (local only), `get/set + // radio.silence`, `get radio.reboots`, `get boot` -- helpers/NodeMaintenance. + // Ahead of CommonCLI's get/set. + extern bool maintHandleCommand(uint32_t sender_timestamp, const char* command, char* reply); + if (maintHandleCommand(sender_timestamp, command, reply)) return; // CUSTOM (TeTeHacko): `txpwr [dBm]` — set output power and report RadioLib's // verdict, which `set tx` swallows. See main.cpp. extern bool txPwrHandleCommand(const char* command, char* reply); if (txPwrHandleCommand(command, reply)) return; - // CUSTOM (TeTeHacko): `get/set radio.silence`, `get radio.reboots` -- the - // deaf-receiver watchdog, see main.cpp. Ahead of CommonCLI's get/set. - extern bool radioSilenceHandleCommand(const char* command, char* reply); - if (radioSilenceHandleCommand(command, reply)) return; #ifdef RADIO_DIAG // CUSTOM (TeTeHacko): `radiostat` / `radiokick` -- see main.cpp. extern bool radioDiagHandleCommand(const char* command, char* reply); diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 9b40f752..e551de02 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -2,7 +2,7 @@ #include #include "MyMesh.h" -#include +#include // =========================================================================== // CUSTOM (TeTeHacko): `blink [n]` -- make THIS board flash its LED on demand, @@ -120,113 +120,6 @@ static void identTick() { } #endif // RPT_IDENT_LED_PIN -// CUSTOM (TeTeHacko): `dfu [uf2|serial|ota]` -- reboot straight into the -// bootloader, deterministically. -// -// This is what the 1200-baud touch was a bad approximation of. The Adafruit -// bootloader picks its mode from GPREGRET at boot, and the core exposes exactly -// that (cores/nRF5/wiring.c): reset_mcu() writes the magic and calls -// NVIC_SystemReset(). -// uf2 0x57 mass-storage drive, drop a .uf2 on it <- the usual one -// serial 0x4e CDC only, for adafruit-nrfutil; no drive appears -// ota 0xA8 BLE DFU -// The touch only ever requested the *serial* mode, and on this build it worked -// about half the time: twice it wedged a board out of USB and BLE entirely, with -// no self-recovery (400 s) and no way back but a physical replug. This has no -// such failure mode -- it is the same call the core makes, from a command. -// -// LOCAL ONLY. Over the mesh this would strand a node in a bootloader that waits -// forever, on a mast, with no way back but a visit. sender_timestamp == 0 is the -// same "physically present" test CommonCLI uses for `erase` and `set freq`. -#if defined(NRF52_PLATFORM) -extern "C" { - void enterUf2Dfu(void); - void enterSerialDfu(void); - void enterOTADfu(void); -} -static unsigned long dfu_pending_at = 0; // 0 = nothing armed -static uint8_t dfu_pending_mode = 0; // 0=uf2 1=serial 2=ota - -// Deferred so the reply reaches the console first -- reset_mcu() never returns, -// and over BLE the notification still has to be pumped out of the TX ring. -static void dfuTick() { - if (dfu_pending_at == 0 || (long)(millis() - dfu_pending_at) < 0) return; - dfu_pending_at = 0; - - // CUSTOM (TeTeHacko): detach USB before the reset instead of letting the reset - // yank it out from under the host. - // - // Measured 4. 8. 2026: 3 of 8 `dfu` commands left the board dead on USB *and* - // BLE, recoverable only by a physical replug. The kernel log is unambiguous - // about what that is and is not: - // - // good: usb 7-9: USB disconnect -> 3 s later idProduct=0045 (bootloader) - // bad: usb 7-6: USB disconnect -> nothing, ever - // - // CORRECTION, same evening: "no enumeration errors, therefore the board is - // wedged" was WRONG, and worth leaving here as a warning. The identical - // signature -- disconnect, then nothing, no errors, port reporting - // `not attached` -- was later produced on a KNOWN GOOD board purely by - // toggling the root-hub port's `disable` from the host. So the log tells you - // the host stopped seeing a device; it does NOT tell you why, and a missing - // error message is not evidence of a dead board. - // - // What is still true: the failure follows resets that happen with USB active, - // in BOTH directions (app->bootloader and bootloader->app), and a reset via - // the RESET pin -- the double tap -- has never failed. That is what this - // change is aimed at, and it is a hypothesis about the transition, not a - // diagnosis of the chip. - // - // reset_mcu() (core wiring.c) disables the SoftDevice and resets with USBD - // still enabled and possibly mid-transfer. Detaching first drops D+ while the - // application is still alive and in control, which gives the host an - // unambiguous disconnect and leaves USBD in a defined state at reset. - // - // Honest scope: this removes a class of race, it is NOT yet proven to be the - // cause -- the failure is intermittent, so only a run of trials can say. The - // RESETREAS line printed at boot is the other half of the instrument. - // Dropping the D+ pullup directly rather than through TinyUSBDevice.detach(): - // one register write, no dependency on the USB stack's headers being reachable - // from here, and it is exactly what detach() does on this chip. - NRF_USBD->USBPULLUP = 0; - delay(150); // long enough for the host to see and log the disconnect - - if (dfu_pending_mode == 1) enterSerialDfu(); - else if (dfu_pending_mode == 2) enterOTADfu(); - else enterUf2Dfu(); -} -#endif - -bool dfuHandleCommand(uint32_t sender_timestamp, const char* command, char* reply) { - if (memcmp(command, "dfu", 3) != 0 || (command[3] != 0 && command[3] != ' ')) return false; -#if defined(NRF52_PLATFORM) - if (sender_timestamp != 0) { - strcpy(reply, "ERR: dfu is local-only (would strand the node in the bootloader)"); - return true; - } - const char* arg = (command[3] == ' ') ? &command[4] : ""; - while (*arg == ' ') arg++; - // Reply first: the reset below never returns, so anything printed after it is - // lost -- and over BLE even this one needs the pump to have run, hence the - // small delay. - if (strcmp(arg, "serial") == 0) { - strcpy(reply, "OK - rebooting into serial DFU (CDC only, no drive)"); - } else if (strcmp(arg, "ota") == 0) { - strcpy(reply, "OK - rebooting into BLE OTA DFU"); - } else if (arg[0] == 0 || strcmp(arg, "uf2") == 0) { - strcpy(reply, "OK - rebooting into UF2 (drive appears in a few seconds)"); - } else { - strcpy(reply, "ERR: dfu [uf2|serial|ota]"); - return true; - } - dfu_pending_at = millis() + 700; - dfu_pending_mode = (strcmp(arg, "serial") == 0) ? 1 : (strcmp(arg, "ota") == 0 ? 2 : 0); -#else - strcpy(reply, "ERR: not an nRF52 board"); -#endif - return true; -} - // Reachable from serial, the BLE console AND the mesh admin CLI, so a node that // is already mounted can still be asked to identify itself. bool identHandleCommand(const char* command, char* reply) { @@ -751,114 +644,12 @@ bool txPwrHandleCommand(const char* command, char* reply) { return true; } -// CUSTOM (TeTeHacko, tth/meshcore-tth#6): radio silence watchdog -- see -// RadioSilenceWatchdog.h for why it is staged and how it avoids a reboot loop. -// `set radio.silence ` (0 = off), `get radio.silence`, `get radio.reboots`. -// Reboots it caused and the threshold backoff survive power loss in a small file, -// written only right before such a reboot and once when the radio proves itself. -static RadioSilenceWatchdog radio_silence; -static FILESYSTEM* radio_silence_fs = NULL; -static uint32_t radio_silence_reboots = 0; -#define RADIO_SILENCE_FILE "/rsilence" +// CUSTOM (TeTeHacko): `dfu`, the radio silence watchdog (#6) and `get boot` -- +// board upkeep shared with the sensor, see helpers/NodeMaintenance.h. +static NodeMaintenance maint(board, radio_driver); -static uint32_t radioActivity() { - return radio_driver.getPacketsRecv() + radio_driver.getPacketsRecvErrors(); -} - -static void radioSilenceLoad(uint8_t* backoff) { - *backoff = 0; - if (!radio_silence_fs || !radio_silence_fs->exists(RADIO_SILENCE_FILE)) return; -#if defined(RP2040_PLATFORM) - File f = radio_silence_fs->open(RADIO_SILENCE_FILE, "r"); -#else - File f = radio_silence_fs->open(RADIO_SILENCE_FILE); -#endif - if (!f) return; - if (f.read((uint8_t*)&radio_silence_reboots, sizeof(radio_silence_reboots)) != sizeof(radio_silence_reboots)) { - radio_silence_reboots = 0; - } - if (f.read(backoff, 1) != 1) *backoff = 0; - f.close(); -} - -static void radioSilenceSave(uint8_t backoff) { - if (!radio_silence_fs) return; -#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) - radio_silence_fs->remove(RADIO_SILENCE_FILE); - File f = radio_silence_fs->open(RADIO_SILENCE_FILE, FILE_O_WRITE); -#elif defined(RP2040_PLATFORM) - File f = radio_silence_fs->open(RADIO_SILENCE_FILE, "w"); -#else - File f = radio_silence_fs->open(RADIO_SILENCE_FILE, "w", true); -#endif - if (!f) return; - f.write((const uint8_t*)&radio_silence_reboots, sizeof(radio_silence_reboots)); - f.write(&backoff, 1); - f.close(); -} - -static void radioSilenceBegin(FILESYSTEM* fs) { - radio_silence_fs = fs; - uint8_t backoff; - radioSilenceLoad(&backoff); - radio_silence.begin(millis(), radioActivity(), backoff); - radio_silence.setMinutes(the_mesh.getNodePrefs()->radio_silence_min); - Serial.printf("boot: radio silence watchdog %u min, reboots %lu, backoff x%u\n", - (unsigned) radio_silence.minutes(), (unsigned long) radio_silence_reboots, - (unsigned) (1u << radio_silence.backoff())); -} - -static void radioSilenceLoop() { - switch (radio_silence.poll(millis(), radioActivity())) { - case RadioSilenceWatchdog::KICK_RX: - Serial.printf("radio silence: nothing received for %lu min, re-arming RX\n", - (unsigned long) (radio_silence.quietMs(millis()) / 60000)); - radio_driver.kickRx(); - break; - case RadioSilenceWatchdog::REBOOT: - Serial.printf("radio silence: still nothing after %lu min, rebooting\n", - (unsigned long) (radio_silence.quietMs(millis()) / 60000)); - radio_silence_reboots++; - radioSilenceSave(radio_silence.nextBackoff()); - delay(100); // let the line out - board.reboot(); - break; - default: - break; - } - if (radio_silence.takeBackoffCleared()) radioSilenceSave(0); -} - -bool radioSilenceHandleCommand(const char* command, char* reply) { - if (strcmp(command, "get radio.reboots") == 0) { - sprintf(reply, "> %lu", (unsigned long) radio_silence_reboots); - return true; - } - if (strcmp(command, "get radio.silence") == 0) { - if (radio_silence.minutes() == 0) { - strcpy(reply, "> off"); - } else { - sprintf(reply, "> %u min (now x%u), quiet %lu min, stage %u", - (unsigned) radio_silence.minutes(), (unsigned) (1u << radio_silence.backoff()), - (unsigned long) (radio_silence.quietMs(millis()) / 60000), (unsigned) radio_silence.stage()); - } - return true; - } - if (memcmp(command, "set radio.silence ", 18) == 0) { - const char* arg = &command[18]; - bool numeric = *arg != 0 && strspn(arg, "0123456789") == strlen(arg); - int m = strcmp(arg, "off") == 0 ? 0 : (numeric ? atoi(arg) : -1); - if (m < 0 || m > RadioSilenceWatchdog::MAX_MINUTES) { - sprintf(reply, "ERR - minutes 0..%u (0/off = disabled)", (unsigned) RadioSilenceWatchdog::MAX_MINUTES); - return true; - } - the_mesh.getNodePrefs()->radio_silence_min = (uint16_t) m; - the_mesh.savePrefs(); - radio_silence.setMinutes((uint16_t) m); - strcpy(reply, "OK"); - return true; - } - return false; +bool maintHandleCommand(uint32_t sender_timestamp, const char* command, char* reply) { + return maint.handleCommand(sender_timestamp, command, reply); } #ifdef RADIO_DIAG @@ -950,44 +741,10 @@ void setup() { delay(BOOT_DIAG_DELAY_MS); #endif -#if defined(NRF52_PLATFORM) - // CUSTOM (TeTeHacko diagnostics): print the hardfault record saved by the - // patched HardFault_Handler (framework debug.cpp) before the last reset. - // 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 - // that distinguishes "somebody typed `dfu`" from "the CPU locked up" or "the - // power went away" after the fact. Without it a board found sitting in the - // application is mute about how it got there -- which is exactly the hole hit - // while chasing boards that vanished from USB after `dfu`. - { - uint32_t rr = readResetReason(); - // The build epoch belongs next to it: it is what the clock comes up at when - // nothing else sets it, so without it printed you cannot tell "fell back to - // the build epoch" from "restored something" -- and comparing against the - // epoch of a DIFFERENT build of the same tree invents discrepancies that are - // not there. Cost one wrong conclusion here. - Serial.printf("boot: FIRMWARE_BUILD_EPOCH=%lu\n", (unsigned long) FIRMWARE_BUILD_EPOCH); - Serial.printf("boot: RESETREAS=0x%08lX%s%s%s%s%s\n", (unsigned long)rr, - rr == 0 ? " (none set = power-on/brownout: power was removed)" : "", - (rr & 0x01) ? " RESETPIN" : "", - (rr & 0x02) ? " WATCHDOG" : "", - (rr & 0x04) ? " SREQ(sw reset, e.g. `dfu`/`reboot`)" : "", - (rr & 0x08) ? " LOCKUP(!)" : ""); - } - - { - 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]); - hf[0] = 0; - } - } -#endif + // CUSTOM (TeTeHacko): reset reason, build epoch and the hardfault record of the + // last crash (NodeMaintenance.cpp). Needs MESH_DEBUG or BOOT_DIAG_DELAY_MS to be + // seen at boot (above); `get boot` keeps it readable afterwards. + maint.printBootDiag(); #if defined(PIN_DIAG) && defined(XIAO_NRF52) // CUSTOM (TeTeHacko): header GPIO integrity sweep, for salvaging a board whose @@ -1129,7 +886,7 @@ void setup() { sensors.begin(); the_mesh.begin(fs); - radioSilenceBegin(fs); // CUSTOM (TeTeHacko): needs the prefs loaded by begin() + maint.begin(fs, the_mesh.getNodePrefs(), &the_mesh); // CUSTOM (TeTeHacko): needs the prefs loaded by begin() #ifdef DISPLAY_CLASS ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION); @@ -1229,9 +986,6 @@ void loop() { #ifdef RPT_IDENT_LED_PIN identTick(); // CUSTOM (TeTeHacko): drive `blink` without blocking the mesh #endif -#if defined(NRF52_PLATFORM) - dfuTick(); // CUSTOM (TeTeHacko): deferred `dfu` reboot, after the reply went out -#endif #if defined(BLE_PIN_CODE) && defined(NRF52_PLATFORM) // CUSTOM (TeTeHacko): deferred apply of `ble on|off` — the reply still goes // out over the old path, only then advertising/connections get switched. @@ -1370,7 +1124,7 @@ void loop() { #endif the_mesh.loop(); - radioSilenceLoop(); // CUSTOM (TeTeHacko): deaf-receiver watchdog + maint.loop(); // CUSTOM (TeTeHacko): deferred `dfu`, deaf-receiver watchdog sensors.loop(); #ifdef DISPLAY_CLASS ui_task.loop(); diff --git a/src/helpers/NodeMaintenance.cpp b/src/helpers/NodeMaintenance.cpp index 8ed89ad7..6ab7aeba 100644 --- a/src/helpers/NodeMaintenance.cpp +++ b/src/helpers/NodeMaintenance.cpp @@ -1,8 +1,7 @@ #include "NodeMaintenance.h" -// CUSTOM (TeTeHacko): moved out of examples/simple_repeater/main.cpp, where the -// long history of each piece still sits next to the repeater's own setup -- the -// why of the dfu USB detach, of RESETREAS and of the hardfault record is there. +// CUSTOM (TeTeHacko): moved out of examples/simple_repeater/main.cpp (4. 11. 2026), +// history included. #define RADIO_SILENCE_FILE "/rsilence" @@ -15,12 +14,22 @@ extern "C" { } #endif +// Why did this board last restart? The core latches NRF_POWER->RESETREAS in +// init() before clearing it, and it is the only thing that distinguishes +// "somebody typed `dfu`" from "the CPU locked up" or "the power went away" after +// the fact -- the hole hit while chasing boards that vanished from USB after `dfu`. +// The hardfault record is saved by the patched HardFault_Handler (framework +// debug.cpp) in g_hardfault_record, .noinit (NRF52Board.cpp) -- the old fixed +// 0x2003FFF0 at the end of RAM was where the bootloader's stack lands on reset. void NodeMaintenance::printBootDiag() { #if defined(NRF52_PLATFORM) uint32_t rr = readResetReason(); _reset_reason = rr; // The build epoch belongs next to it: it is what the clock comes up at when - // nothing else sets it. + // nothing else sets it, so without it printed you cannot tell "fell back to + // the build epoch" from "restored something" -- and comparing against the + // epoch of a DIFFERENT build of the same tree invents discrepancies that are + // not there. Cost one wrong conclusion here. Serial.printf("boot: FIRMWARE_BUILD_EPOCH=%lu\n", (unsigned long) FIRMWARE_BUILD_EPOCH); Serial.printf("boot: RESETREAS=0x%08lX%s%s%s%s%s\n", (unsigned long)rr, rr == 0 ? " (none set = power-on/brownout: power was removed)" : "", @@ -144,17 +153,64 @@ bool NodeMaintenance::silenceCommand(const char* command, char* reply) { } // --- dfu [uf2|serial|ota] ------------------------------------------------------ -// The Adafruit bootloader picks its mode from GPREGRET at boot; the core's -// enter*Dfu() write the magic and reset. LOCAL ONLY: over the mesh this would -// strand a node in a bootloader that waits forever. +// Reboot straight into the bootloader, deterministically. +// +// This is what the 1200-baud touch was a bad approximation of. The Adafruit +// bootloader picks its mode from GPREGRET at boot, and the core exposes exactly +// that (cores/nRF5/wiring.c): reset_mcu() writes the magic and calls +// NVIC_SystemReset(). +// uf2 0x57 mass-storage drive, drop a .uf2 on it <- the usual one +// serial 0x4e CDC only, for adafruit-nrfutil; no drive appears +// ota 0xA8 BLE DFU +// The touch only ever requested the *serial* mode, and on this build it worked +// about half the time: twice it wedged a board out of USB and BLE entirely, with +// no self-recovery (400 s) and no way back but a physical replug. This has no +// such failure mode -- it is the same call the core makes, from a command. +// +// LOCAL ONLY. Over the mesh this would strand a node in a bootloader that waits +// forever, on a mast, with no way back but a visit. sender_timestamp == 0 is the +// same "physically present" test CommonCLI uses for `erase` and `set freq`. // Deferred so the reply reaches the console first -- the reset never returns. void NodeMaintenance::dfuTick() { #if defined(NRF52_PLATFORM) if (_dfu_at == 0 || (long)(millis() - _dfu_at) < 0) return; _dfu_at = 0; - // Detach USB before the reset instead of letting the reset yank it out from - // under the host (see simple_repeater/main.cpp for the measurements). + // Detach USB before the reset instead of letting the reset + // yank it out from under the host. + // + // Measured 4. 8. 2026: 3 of 8 `dfu` commands left the board dead on USB *and* + // BLE, recoverable only by a physical replug. The kernel log is unambiguous + // about what that is and is not: + // + // good: usb 7-9: USB disconnect -> 3 s later idProduct=0045 (bootloader) + // bad: usb 7-6: USB disconnect -> nothing, ever + // + // CORRECTION, same evening: "no enumeration errors, therefore the board is + // wedged" was WRONG, and worth leaving here as a warning. The identical + // signature -- disconnect, then nothing, no errors, port reporting + // `not attached` -- was later produced on a KNOWN GOOD board purely by + // toggling the root-hub port's `disable` from the host. So the log tells you + // the host stopped seeing a device; it does NOT tell you why, and a missing + // error message is not evidence of a dead board. + // + // What is still true: the failure follows resets that happen with USB active, + // in BOTH directions (app->bootloader and bootloader->app), and a reset via + // the RESET pin -- the double tap -- has never failed. That is what this + // change is aimed at, and it is a hypothesis about the transition, not a + // diagnosis of the chip. + // + // reset_mcu() (core wiring.c) disables the SoftDevice and resets with USBD + // still enabled and possibly mid-transfer. Detaching first drops D+ while the + // application is still alive and in control, which gives the host an + // unambiguous disconnect and leaves USBD in a defined state at reset. + // + // Honest scope: this removes a class of race, it is NOT yet proven to be the + // cause -- the failure is intermittent, so only a run of trials can say. The + // RESETREAS line printed at boot is the other half of the instrument. + // Dropping the D+ pullup directly rather than through TinyUSBDevice.detach(): + // one register write, no dependency on the USB stack's headers being reachable + // from here, and it is exactly what detach() does on this chip. NRF_USBD->USBPULLUP = 0; delay(150); if (_dfu_mode == 1) enterSerialDfu();