Repeater (SenseCAP Solar): battery thresholds settable at runtime, and a cutoff that switches a running node off before the cell runs flat
pub-release / release (push) Successful in 34m55s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-23 13:22:24 +01:00
1 parent 0066d073db
commit be46cfa5fb
10 files changed
+392 -19

No files matched your search

+124
View File
@@ -93,6 +93,9 @@ void NodeMaintenance::begin(FILESYSTEM* fs, NodePrefs* prefs, CommonCLICallbacks
_fs = fs;
_prefs = prefs;
_callbacks = callbacks;
#ifdef NODE_PWR_GUARD
pwrBegin(); // first: below the bootlock nothing else should run
#endif
uint8_t backoff;
silenceLoad(&backoff);
_silence.begin(millis(), radioActivity(), backoff);
@@ -106,6 +109,9 @@ void NodeMaintenance::begin(FILESYSTEM* fs, NodePrefs* prefs, CommonCLICallbacks
void NodeMaintenance::loop(int tx_queued, int tx_due) {
dfuTick();
#ifdef NODE_PWR_GUARD
pwrTick();
#endif
txStallTick(tx_queued, tx_due);
#ifdef FW_OTA
_fw.loop();
@@ -331,6 +337,9 @@ bool NodeMaintenance::handleCommand(uint32_t sender_timestamp, const char* comma
#endif
if (silenceCommand(command, reply)) return true;
if (txStallCommand(command, reply)) return true;
#ifdef NODE_PWR_GUARD
if (pwrCommand(command, reply)) return true;
#endif
if (strcmp(command, "get boot") == 0) {
#if defined(NRF52_PLATFORM)
if (_hardfault_seen) {
@@ -347,3 +356,118 @@ bool NodeMaintenance::handleCommand(uint32_t sender_timestamp, const char* comma
}
return false;
}
// --- battery thresholds (PWRMGT_RUNTIME), see helpers/LowVoltageGuard.h ---------
// Saved in their own small file, not in the prefs: no prefs-layout change, and a
// node without the file runs on the build defaults.
#ifdef NODE_PWR_GUARD
#define PWR_GUARD_FILE "/pwr_guard"
#ifndef PWRMGT_BOOTLOCK_DEFAULT
#ifdef PWRMGT_VOLTAGE_BOOTLOCK
#define PWRMGT_BOOTLOCK_DEFAULT PWRMGT_VOLTAGE_BOOTLOCK
#else
#define PWRMGT_BOOTLOCK_DEFAULT 0
#endif
#endif
#ifndef PWRMGT_CUTOFF_DEFAULT
#define PWRMGT_CUTOFF_DEFAULT 0
#endif
static void fmtMv(char* out, uint16_t mv) {
if (mv == 0) strcpy(out, "off"); else sprintf(out, "%u mV", (unsigned) mv);
}
void NodeMaintenance::pwrBegin() {
uint16_t v[2] = { PWRMGT_BOOTLOCK_DEFAULT, PWRMGT_CUTOFF_DEFAULT };
if (_fs && _fs->exists(PWR_GUARD_FILE)) {
#if defined(RP2040_PLATFORM)
File f = _fs->open(PWR_GUARD_FILE, "r");
#else
File f = _fs->open(PWR_GUARD_FILE);
#endif
if (f) {
uint16_t r[2];
if (f.read((uint8_t*) r, sizeof(r)) == sizeof(r) && !LowVoltageGuard::validate(r[0], r[1])) {
v[0] = r[0]; v[1] = r[1];
}
f.close();
}
}
_pwr.begin(millis(), v[0], v[1]);
Serial.printf("boot: pwr bootlock %u mV, cutoff %u mV (0 = off), battery %u mV\n",
(unsigned) v[0], (unsigned) v[1], (unsigned) _board.getBattMilliVolts());
// Below the bootlock this does not return: SYSTEMOFF until the LPCOMP wakes it.
// Also latches the reset/shutdown reasons, so it runs with the bootlock off too.
nrf().bootVoltageCheck(v[0]);
}
void NodeMaintenance::pwrSave() {
if (!_fs) return;
uint16_t v[2] = { _pwr.bootlock(), _pwr.cutoff() };
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
_fs->remove(PWR_GUARD_FILE);
File f = _fs->open(PWR_GUARD_FILE, FILE_O_WRITE);
#elif defined(RP2040_PLATFORM)
File f = _fs->open(PWR_GUARD_FILE, "w");
#else
File f = _fs->open(PWR_GUARD_FILE, "w", true);
#endif
if (!f) return;
f.write((const uint8_t*) v, sizeof(v));
f.close();
}
void NodeMaintenance::pwrTick() {
uint32_t now = millis();
if (!_pwr.due(now)) return;
uint16_t mv = _board.getBattMilliVolts();
if (_pwr.poll(now, mv, _board.isExternalPowered())) {
Serial.printf("pwr: battery %u mV, below the cutoff %u mV %u times in a row, switching off\n",
(unsigned) mv, (unsigned) _pwr.cutoff(), (unsigned) LowVoltageGuard::CUTOFF_SAMPLES);
delay(100); // let the line out
nrf().lowVoltageShutdown(); // does not return; the LPCOMP wakes it
}
}
bool NodeMaintenance::pwrCommand(const char* command, char* reply) {
char a[12], b[12];
if (strcmp(command, "get pwr") == 0) {
fmtMv(a, _pwr.bootlock());
fmtMv(b, _pwr.cutoff());
uint8_t why = nrf().getShutdownReason();
sprintf(reply, "> bootlock %s, cutoff %s, battery %u mV (at boot %u)%s, last off: %s",
a, b, (unsigned) _board.getBattMilliVolts(), (unsigned) nrf().getBootVoltage(),
_board.isExternalPowered() ? ", USB" : "", nrf().getShutdownReasonString(why));
return true;
}
bool is_boot = strncmp(command, "set pwr.bootlock ", 17) == 0;
bool is_cut = strncmp(command, "set pwr.cutoff ", 15) == 0;
if (!is_boot && !is_cut) return false;
const char* arg = command + (is_boot ? 17 : 15);
uint16_t mv = (strcmp(arg, "off") == 0) ? 0 : (uint16_t) atoi(arg);
uint16_t boot = is_boot ? mv : _pwr.bootlock();
uint16_t cut = is_cut ? mv : _pwr.cutoff();
const char* err = LowVoltageGuard::validate(boot, cut);
if (err) {
sprintf(reply, "Error: %s (%u-%u mV or off)", err,
(unsigned) LowVoltageGuard::MIN_MV, (unsigned) LowVoltageGuard::MAX_MV);
return true;
}
// No threshold above the battery right now: a mast admin would lock the node
// out -- it would not start after a reboot, or switch off within minutes.
uint16_t batt = _board.getBattMilliVolts();
if (mv != 0 && !_board.isExternalPowered() && batt >= LowVoltageGuard::GLITCH_MV && mv >= batt) {
sprintf(reply, "Error: battery is %u mV now, a threshold of %u mV would lock the node out",
(unsigned) batt, (unsigned) mv);
return true;
}
_pwr.set(boot, cut);
pwrSave();
fmtMv(a, _pwr.bootlock());
fmtMv(b, _pwr.cutoff());
sprintf(reply, "OK - bootlock %s, cutoff %s", a, b);
return true;
}
#endif // NODE_PWR_GUARD