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

+81
View File
@@ -0,0 +1,81 @@
#pragma once
#include <stdint.h>
// CUSTOM (TeTeHacko): battery thresholds of a solar node, settable at runtime.
//
// remote-mast (11/2026) faces east and gets no direct sun in winter: -50 mV a day
// with consumption unchanged. Upstream NRF52_POWER_MANAGEMENT only refuses to
// BOOT below a compiled threshold; nothing switches a RUNNING node off, so the
// cell runs down to the brown-out and the panel then restarts the node on a
// cell that cannot hold it. Two thresholds, both in mV, 0 = off:
//
// bootlock checked once at boot: below it the node goes to SYSTEMOFF and the
// board's LPCOMP wakes it when the battery recovers
// cutoff checked while running: CUTOFF_SAMPLES readings in a row below it
// (one every CHECK_MS, so a TX dip cannot trigger it) and the node
// goes to the same SYSTEMOFF
//
// cutoff must stay below bootlock: a node switched off at the cutoff then only
// starts again once the battery is past bootlock, which is the hysteresis.
// External power (USB) disarms the cutoff, a reading under 1 V is an ADC glitch
// and is ignored. The LPCOMP wake level is hardware (a fraction of VDD), not
// one of these.
//
// Pure logic, no Arduino: driven from NodeMaintenance, tested on the host.
class LowVoltageGuard {
public:
static constexpr uint16_t MIN_MV = 3000;
static constexpr uint16_t MAX_MV = 4200;
static constexpr uint32_t CHECK_MS = 60000;
static constexpr uint8_t CUTOFF_SAMPLES = 3;
static constexpr uint16_t GLITCH_MV = 1000;
void begin(uint32_t now, uint16_t bootlock_mv, uint16_t cutoff_mv) {
_bootlock = bootlock_mv;
_cutoff = cutoff_mv;
_last_check = now;
_low = 0;
}
uint16_t bootlock() const { return _bootlock; }
uint16_t cutoff() const { return _cutoff; }
uint8_t lowSamples() const { return _low; }
// NULL if the pair is acceptable, else the reason (for the CLI reply).
static const char* validate(uint16_t bootlock_mv, uint16_t cutoff_mv) {
if (bootlock_mv != 0 && (bootlock_mv < MIN_MV || bootlock_mv > MAX_MV)) return "bootlock out of range";
if (cutoff_mv != 0 && (cutoff_mv < MIN_MV || cutoff_mv > MAX_MV)) return "cutoff out of range";
if (cutoff_mv != 0 && bootlock_mv != 0 && cutoff_mv >= bootlock_mv) return "cutoff must be below bootlock";
return 0;
}
bool set(uint16_t bootlock_mv, uint16_t cutoff_mv) {
if (validate(bootlock_mv, cutoff_mv)) return false;
_bootlock = bootlock_mv;
_cutoff = cutoff_mv;
_low = 0;
return true;
}
// True when poll() would take a reading: read the battery only then (the ADC
// read is not free on every board -- SenseCAP waits 10 ms for the divider).
bool due(uint32_t now) const { return (uint32_t)(now - _last_check) >= CHECK_MS; }
// Returns true when the node should switch off now.
bool poll(uint32_t now, uint16_t batt_mv, bool external_power) {
if (!due(now)) return false;
_last_check = now;
if (_cutoff == 0 || external_power) { _low = 0; return false; }
if (batt_mv < GLITCH_MV) return false; // not a reading, keep the count
if (batt_mv >= _cutoff) { _low = 0; return false; }
if (_low < CUTOFF_SAMPLES) _low++;
return _low >= CUTOFF_SAMPLES;
}
private:
uint16_t _bootlock = 0;
uint16_t _cutoff = 0;
uint32_t _last_check = 0;
uint8_t _low = 0;
};
+8
View File
@@ -71,6 +71,14 @@ public:
#ifdef PWRMGT_LPCOMP_AIN
bool getWakeLpcompSupported() const override { return true; }
#endif
// CUSTOM (TeTeHacko): entry points for runtime battery thresholds
// (PWRMGT_RUNTIME, helpers/LowVoltageGuard.h via NodeMaintenance). The boot
// check reads only voltage_bootlock; the wake is armed by initiateShutdown().
bool bootVoltageCheck(uint16_t bootlock_mv) {
PowerMgtConfig c = { 0, 0, bootlock_mv };
return checkBootVoltage(&c);
}
void lowVoltageShutdown() { initiateShutdown(SHUTDOWN_REASON_LOW_VOLTAGE); }
#endif
};
+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
+20
View File
@@ -19,6 +19,11 @@
// get radio.txstall TX stall watchdog: threshold, queue, time without TX
// set radio.txstall <min> 0/off = disabled; RAM only, RADIO_TXSTALL_MIN at boot
// get boot why the board last reset, and the hardfault record
// get pwr battery thresholds, battery now and at boot, last shutdown
// set pwr.bootlock <mV> don't start below it, sleep until the battery recovers
// set pwr.cutoff <mV> switch a running node off below it; 0/off = disabled
// (both only with -D PWRMGT_RUNTIME, nRF52 power management,
// see helpers/LowVoltageGuard.h)
//
// Wiring (see simple_sensor/main.cpp):
// setup(): maint.printBootDiag() early, maint.begin(fs, prefs, cb) after the
@@ -34,6 +39,11 @@
#include <helpers/FwUpdate.h>
#endif
#include <helpers/RadioSilenceWatchdog.h>
#include <helpers/LowVoltageGuard.h>
#if defined(NRF52_PLATFORM) && defined(NRF52_POWER_MANAGEMENT) && defined(PWRMGT_RUNTIME)
#include <helpers/NRF52Board.h>
#define NODE_PWR_GUARD 1
#endif
#include <helpers/radiolib/RadioLibWrappers.h>
class NodeMaintenance {
@@ -77,6 +87,13 @@ private:
void txStallTick(int tx_queued, int tx_due);
bool txStallCommand(const char* command, char* reply);
void dfuTick();
#ifdef NODE_PWR_GUARD
void pwrBegin();
void pwrTick();
void pwrSave();
bool pwrCommand(const char* command, char* reply);
NRF52Board& nrf() { return static_cast<NRF52Board&>(_board); }
#endif
mesh::MainBoard& _board;
RadioLibWrapper& _radio;
@@ -90,6 +107,9 @@ private:
RadioSilenceWatchdog _silence;
uint32_t _silence_reboots = 0;
RadioSilenceWatchdog _txstall; // same staging, `activity` = packets sent
#ifdef NODE_PWR_GUARD
LowVoltageGuard _pwr;
#endif
int _tx_queued = -1, _tx_due = -1;
unsigned long _dfu_at = 0; // 0 = nothing armed