simple_sensor: dfu, radio silence watchdog and get boot via shared NodeMaintenance
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
278c1b8c8a
commit
7159a00f05
4 files changed
+305
-1
No files matched your search
@@ -0,0 +1,211 @@
|
||||
#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.
|
||||
|
||||
#define RADIO_SILENCE_FILE "/rsilence"
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
extern "C" {
|
||||
void enterUf2Dfu(void);
|
||||
void enterSerialDfu(void);
|
||||
void enterOTADfu(void);
|
||||
extern uint32_t g_hardfault_record[4]; // NRF52Board.cpp, .noinit: magic, pc, lr, psr
|
||||
}
|
||||
#endif
|
||||
|
||||
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.
|
||||
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(!)" : "");
|
||||
|
||||
volatile uint32_t* hf = g_hardfault_record;
|
||||
if (hf[0] == 0xFA010DEB) {
|
||||
_hardfault[0] = hf[1]; _hardfault[1] = hf[2]; _hardfault[2] = hf[3];
|
||||
_hardfault_seen = true;
|
||||
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; // printed once; `get boot` keeps it until the next reset
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// --- radio silence watchdog (#6), see RadioSilenceWatchdog.h ----------------
|
||||
// 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.
|
||||
|
||||
void NodeMaintenance::silenceLoad(uint8_t* backoff) {
|
||||
*backoff = 0;
|
||||
if (!_fs || !_fs->exists(RADIO_SILENCE_FILE)) return;
|
||||
#if defined(RP2040_PLATFORM)
|
||||
File f = _fs->open(RADIO_SILENCE_FILE, "r");
|
||||
#else
|
||||
File f = _fs->open(RADIO_SILENCE_FILE);
|
||||
#endif
|
||||
if (!f) return;
|
||||
if (f.read((uint8_t*)&_silence_reboots, sizeof(_silence_reboots)) != sizeof(_silence_reboots)) {
|
||||
_silence_reboots = 0;
|
||||
}
|
||||
if (f.read(backoff, 1) != 1) *backoff = 0;
|
||||
f.close();
|
||||
}
|
||||
|
||||
void NodeMaintenance::silenceSave(uint8_t backoff) {
|
||||
if (!_fs) return;
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
_fs->remove(RADIO_SILENCE_FILE);
|
||||
File f = _fs->open(RADIO_SILENCE_FILE, FILE_O_WRITE);
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
File f = _fs->open(RADIO_SILENCE_FILE, "w");
|
||||
#else
|
||||
File f = _fs->open(RADIO_SILENCE_FILE, "w", true);
|
||||
#endif
|
||||
if (!f) return;
|
||||
f.write((const uint8_t*)&_silence_reboots, sizeof(_silence_reboots));
|
||||
f.write(&backoff, 1);
|
||||
f.close();
|
||||
}
|
||||
|
||||
void NodeMaintenance::begin(FILESYSTEM* fs, NodePrefs* prefs, CommonCLICallbacks* callbacks) {
|
||||
_fs = fs;
|
||||
_prefs = prefs;
|
||||
_callbacks = callbacks;
|
||||
uint8_t backoff;
|
||||
silenceLoad(&backoff);
|
||||
_silence.begin(millis(), radioActivity(), backoff);
|
||||
_silence.setMinutes(_prefs->radio_silence_min);
|
||||
Serial.printf("boot: radio silence watchdog %u min, reboots %lu, backoff x%u\n",
|
||||
(unsigned) _silence.minutes(), (unsigned long) _silence_reboots,
|
||||
(unsigned) (1u << _silence.backoff()));
|
||||
}
|
||||
|
||||
void NodeMaintenance::loop() {
|
||||
dfuTick();
|
||||
switch (_silence.poll(millis(), radioActivity())) {
|
||||
case RadioSilenceWatchdog::KICK_RX:
|
||||
Serial.printf("radio silence: nothing received for %lu min, re-arming RX\n",
|
||||
(unsigned long) (_silence.quietMs(millis()) / 60000));
|
||||
_radio.kickRx();
|
||||
break;
|
||||
case RadioSilenceWatchdog::REBOOT:
|
||||
Serial.printf("radio silence: still nothing after %lu min, rebooting\n",
|
||||
(unsigned long) (_silence.quietMs(millis()) / 60000));
|
||||
_silence_reboots++;
|
||||
silenceSave(_silence.nextBackoff());
|
||||
delay(100); // let the line out
|
||||
_board.reboot();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (_silence.takeBackoffCleared()) silenceSave(0);
|
||||
}
|
||||
|
||||
bool NodeMaintenance::silenceCommand(const char* command, char* reply) {
|
||||
if (strcmp(command, "get radio.reboots") == 0) {
|
||||
sprintf(reply, "> %lu", (unsigned long) _silence_reboots);
|
||||
return true;
|
||||
}
|
||||
if (strcmp(command, "get radio.silence") == 0) {
|
||||
if (_silence.minutes() == 0) {
|
||||
strcpy(reply, "> off");
|
||||
} else {
|
||||
sprintf(reply, "> %u min (now x%u), quiet %lu min, stage %u",
|
||||
(unsigned) _silence.minutes(), (unsigned) (1u << _silence.backoff()),
|
||||
(unsigned long) (_silence.quietMs(millis()) / 60000), (unsigned) _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;
|
||||
}
|
||||
_prefs->radio_silence_min = (uint16_t) m;
|
||||
_callbacks->savePrefs();
|
||||
_silence.setMinutes((uint16_t) m);
|
||||
strcpy(reply, "OK");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// --- 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.
|
||||
|
||||
// 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).
|
||||
NRF_USBD->USBPULLUP = 0;
|
||||
delay(150);
|
||||
if (_dfu_mode == 1) enterSerialDfu();
|
||||
else if (_dfu_mode == 2) enterOTADfu();
|
||||
else enterUf2Dfu();
|
||||
#endif
|
||||
}
|
||||
|
||||
bool NodeMaintenance::dfuCommand(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++;
|
||||
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_at = millis() + 700;
|
||||
_dfu_mode = (strcmp(arg, "serial") == 0) ? 1 : (strcmp(arg, "ota") == 0 ? 2 : 0);
|
||||
#else
|
||||
strcpy(reply, "ERR: not an nRF52 board");
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
bool NodeMaintenance::handleCommand(uint32_t sender_timestamp, const char* command, char* reply) {
|
||||
if (dfuCommand(sender_timestamp, command, reply)) return true;
|
||||
if (silenceCommand(command, reply)) return true;
|
||||
if (strcmp(command, "get boot") == 0) {
|
||||
#if defined(NRF52_PLATFORM)
|
||||
if (_hardfault_seen) {
|
||||
sprintf(reply, "> RESETREAS=0x%08lX, HARDFAULT before it: PC=%08lX LR=%08lX PSR=%08lX",
|
||||
(unsigned long) _reset_reason, (unsigned long) _hardfault[0],
|
||||
(unsigned long) _hardfault[1], (unsigned long) _hardfault[2]);
|
||||
} else {
|
||||
sprintf(reply, "> RESETREAS=0x%08lX, no hardfault", (unsigned long) _reset_reason);
|
||||
}
|
||||
#else
|
||||
strcpy(reply, "> not recorded on this platform");
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in new issue
Block a user