From 3c27e7745f37bbfc1ee0e5f86c479b59aeaf7bf7 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Tue, 4 Aug 2026 12:53:20 +0200 Subject: [PATCH] =?UTF-8?q?feat(repeater):=20`dfu=20[uf2|serial|ota]`=20?= =?UTF-8?q?=E2=80=94=20deterministic=20reboot=20into=20the=20bootloader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 1200-baud touch was the wrong mechanism, not an unreliable one. The Adafruit bootloader chooses its mode from GPREGRET at boot, and the core already exposes exactly that (cores/nRF5/wiring.c): enterUf2Dfu() 0x57 DFU_MAGIC_UF2_RESET -> mass-storage drive enterSerialDfu() 0x4e DFU_MAGIC_SERIAL_ONLY_RESET -> CDC only, no drive enterOTADfu() 0xA8 DFU_MAGIC_OTA_RESET -> BLE DFU The touch only ever requests the *serial* one. That is why no UF2 drive ever appeared however long we waited for it, and it is the real reason today's flashes kept going sideways -- I read "coin flip" into what was simply a request for a different mode, and then wedged two boards proving it. `dfu` writes the magic and resets, so the board lands in the mode you asked for, every time. The reply goes out first and the reset happens ~700 ms later from loop(), since reset_mcu() never returns and over BLE the notification still has to be pumped out of the TX ring. LOCAL ONLY (sender_timestamp == 0, the same physical-presence test CommonCLI uses for `erase` and `set freq`). Over the mesh this would park a node in a bootloader that waits forever, on a mast, with no way back but a visit. Builds clean; NOT yet exercised on hardware -- the boards that could test it are the ones still running firmware without the command. Co-Authored-By: Claude Opus 5 --- examples/simple_repeater/MyMesh.cpp | 4 ++ examples/simple_repeater/main.cpp | 71 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index c7858391..c8310079 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -1629,6 +1629,10 @@ 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): `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); diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 22388030..32e8a4af 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -119,6 +119,74 @@ 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; + 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) { @@ -643,6 +711,9 @@ 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.