Fix peripheral power shutdown and auto-detect Station G3 radio slots

This commit is contained in:
mikecarper committed 2026-10-15 22:30:37 -07:00
1 parent c6e43c5530
commit 8eced48d34
103 files changed
+4900 -194

No files matched your search

+53 -3
View File
@@ -9,6 +9,8 @@ static std::vector<std::string> calls;
static bool usb_logging = true, usb_debug = true;
static bool serial_live = false;
static bool reset_event = false, cleanup_ready = false;
static bool simulate_no_radio = false;
static unsigned identity_loads = 0;
struct TestSerial : Stream {
int rx = -1, tx = -1;
unsigned baud = 0;
@@ -76,8 +78,20 @@ struct Sensors {
void begin() {}
void loop() { calls.push_back("sensors.loop"); }
} sensors;
static bool radio_init() { return true; }
static void loadOrCreateIdentity() {}
struct RecoveryReached {};
static bool radio_init() {
#if defined(STATION_G3_ESP32)
assert(serial_live && "empty-slot recovery must have USB before radio init");
if (simulate_no_radio) {
calls.push_back("radio.recovery");
throw RecoveryReached();
}
#endif
return true;
}
static void loadOrCreateIdentity() { ++identity_loads; }
struct Filesystem { bool begin(bool format_if_empty) { assert(format_if_empty);return true; } } SPIFFS;
static void halt() { assert(false); }
static void onSetRadio() {}
static void onSetTxPower() {}
static void onGetCurrentRssi() {}
@@ -102,6 +116,7 @@ public:
};
static KissModem* modem = nullptr;
#define MESH_DEBUG_PRINTLN(...) ((void)0)
#define KISS_BOOT_STAGE(stage) ((void)0)
@TRANSPORT_SELECTION@
@LOOP_STATE@
@SETUP@
@@ -112,16 +127,30 @@ int main() {
assert(modem != nullptr);
#if EXPECT_HWCDC
assert(modem->port == &guarded_port);
#if defined(STATION_G3_ESP32)
const std::vector<std::string> expected = {
"logging.gate", "debug.gate", "prepare", "serial.begin", "begin.logging",
"board.begin", "radio.begin", "modem.construct", "modem.begin", "boot.complete"};
#else
const std::vector<std::string> expected = {
"logging.gate", "debug.gate", "board.begin", "radio.begin", "prepare",
"serial.begin", "begin.logging", "modem.construct", "modem.begin", "boot.complete"};
#endif
assert(calls == expected);
#elif EXPECT_UART
assert(modem->port == &Serial1);
assert(Serial1.rx == KISS_UART_RX && Serial1.tx == KISS_UART_TX);
assert(Serial1.baud == 115200 && !serial_live);
assert(Serial1.baud == 115200);
#if defined(STATION_G3_ESP32)
assert(serial_live);
const std::vector<std::string> expected = {
"prepare", "serial.begin", "begin.logging", "board.begin", "radio.begin",
"uart.begin", "modem.construct", "modem.begin", "boot.complete"};
#else
assert(!serial_live);
const std::vector<std::string> expected = {
"board.begin", "radio.begin", "uart.begin", "modem.construct", "modem.begin", "boot.complete"};
#endif
assert(calls == expected);
#else
assert(modem->port == &Serial);
@@ -166,4 +195,25 @@ int main() {
assert(calls == normal);
#endif
delete modem;
#if defined(STATION_G3_ESP32)
// Execute the same production setup with radio discovery suspended at its
// recovery loop. Neither a working modem nor identity creation is required
// to make the primary USB console reachable, even for UART-hosted KISS.
modem = nullptr;
const unsigned old_identity_loads = identity_loads;
serial_live = false;
calls.clear();
simulate_no_radio = true;
try { setup(); assert(false); } catch (const RecoveryReached&) {}
assert(serial_live && modem == nullptr && identity_loads == old_identity_loads);
#if EXPECT_HWCDC
const std::vector<std::string> recovery = {
"logging.gate", "debug.gate", "prepare", "serial.begin", "begin.logging",
"board.begin", "radio.recovery"};
#else
const std::vector<std::string> recovery = {
"prepare", "serial.begin", "begin.logging", "board.begin", "radio.recovery"};
#endif
assert(calls == recovery);
#endif
}
@@ -52,6 +52,7 @@ extern MockFicr mock_ficr;
#define NRF_SPIM1 1
#define WB_IO2 34
#define OUTPUT 1
#define INPUT 0
#define HIGH 1
#define LOW 0
void pinMode(int pin, int mode);
+83 -1
View File
@@ -33,6 +33,7 @@ static std::atomic<int> hardware_operations{0};
static std::atomic<int> main_hardware_operations{0};
static std::atomic<int> live_task_allocations{0};
static std::atomic<int> task_deletions{0};
static unsigned terminal_pad_releases = 0;
static bool fail_task_creation = false;
static std::atomic<bool> defer_task_start{false};
static std::mutex library_mutex;
@@ -80,7 +81,13 @@ void mock_spi_end() {
assert(spi_active.exchange(false));
++hardware_operations;
}
void pinMode(int pin, int mode) { assert(pin == 21 && mode == OUTPUT); }
void pinMode(int pin, int mode) {
if (mode == INPUT) {
assert((pin==3||pin==29||pin==30||pin==26||pin==21)&&!spi_active.load());
assert(!ethernet_worker_active.load()||ethernet_worker_stopped.load());
++terminal_pad_releases;
} else assert(pin == 21 && mode == OUTPUT);
}
void digitalWrite(int pin, int value) {
assert((pin == 21 || pin == 26) && value == HIGH);
}
@@ -129,6 +136,11 @@ uint32_t ulTaskNotifyTake(int clear_on_exit, uint32_t ticks) {
return notifications;
}
void vTaskDelay(uint32_t ticks) {
if (current_task == nullptr) {
mock_millis.fetch_add(ticks);
std::this_thread::sleep_for(std::chrono::milliseconds(ticks));
return;
}
assert(current_task != nullptr);
if (ticks == portMAX_DELAY) {
std::unique_lock<std::mutex> lock(current_task->mutex);
@@ -735,6 +747,76 @@ int main(int argc, char** argv) {
assert(output().find("old-record-result") == std::string::npos);
break;
}
case 29: { // Registered but never-started Ethernet must not touch storage's pads.
assert(mesh::nrf52::terminalEthernetShutdown(0));
assert(hardware_operations.load()==0&&terminal_pad_releases==0&&context.saves==0);
break;
}
case 30: { // A blocked DHCP owner times out without task deletion or pad remapping.
configure(context,true);
Ethernet.begin_blocked.store(true);
assert(command("eth on").find("starting")!=std::string::npos);
wait_until([] { return Ethernet.begin_count.load()==1; });
const unsigned saves=context.saves;
const uint32_t started=millis();
assert(!mesh::nrf52::terminalEthernetShutdown(20));
assert(static_cast<uint32_t>(millis()-started)>=20);
assert(spi_active.load()&&bus_owner.load()&&context.releases==0);
assert(task_deletions.load()==0&&terminal_pad_releases==0&&context.saves==saves);
unblock(Ethernet.begin_blocked);
wait_until([] { return ethernet_worker_stopped.load(); });
assert(mesh::nrf52::terminalEthernetShutdown(500));
assert(task_deletions.load()==1&&terminal_pad_releases==5&&context.saves==saves);
break;
}
case 31: { // Online terminal stop completes all owner hooks without changing preferences.
configure(context,true);
assert(command("eth on").find("starting")!=std::string::npos);
wait_until([] { return ethernet_is_running(); });
const unsigned saves=context.saves;
assert(mesh::nrf52::terminalEthernetShutdown(500));
assert(!ethernet_running.load()&&!ethernet_enabled.load());
assert(context.releases==1&&context.saves==saves&&terminal_pad_releases==5);
assert(task_deletions.load()==1&&!spi_active.load()&&!bus_owner.load());
// Repeated shutdown is harmless and does not end an unstarted SPI object.
const int operations=hardware_operations.load();
assert(mesh::nrf52::terminalEthernetShutdown(0));
assert(hardware_operations.load()==operations&&context.releases==1);
break;
}
case 32: { // An OTA release refusal cannot be mistaken for permission to cut the rail.
start_online();context.release_allowed=false;
assert(!mesh::nrf52::terminalEthernetShutdown(50));
assert(!spi_active.load()&&live_task_allocations.load()==0&&bus_owner.load());
assert(context.releases>0&&terminal_pad_releases==5);
context.release_allowed=true;
assert(mesh::nrf52::terminalEthernetShutdown(500));
assert(!bus_owner.load()&&context.saves==0&&terminal_pad_releases==5);
break;
}
case 33: { // Slow renewal cannot be force-deleted or lose its supply.
start_online();Ethernet.maintain_blocked.store(true);
mock_millis.fetch_add(1000);ethernet_loop_maintain();
wait_until([] { return Ethernet.maintain_count.load()==1; });
assert(!mesh::nrf52::terminalEthernetShutdown(20));
assert(spi_active.load()&&task_deletions.load()==0&&terminal_pad_releases==0);
unblock(Ethernet.maintain_blocked);
wait_until([] { return ethernet_worker_stopped.load(); });
assert(mesh::nrf52::terminalEthernetShutdown(500));
break;
}
case 34: { // Stop deadline remains bounded across millis() rollover.
Ethernet.begin_blocked.store(true);
assert(command("eth on").find("starting")!=std::string::npos);
wait_until([] { return Ethernet.begin_count.load()==1; });
mock_millis.store(UINT32_MAX-9);
assert(!mesh::nrf52::terminalEthernetShutdown(20));
assert(millis()<100&&spi_active.load()&&terminal_pad_releases==0);
unblock(Ethernet.begin_blocked);
wait_until([] { return ethernet_worker_stopped.load(); });
assert(mesh::nrf52::terminalEthernetShutdown(500));
break;
}
default: assert(false);
}
assert(live_task_allocations.load() == 0 && !spi_active.load() && !bus_owner.load());
+30
View File
@@ -291,6 +291,7 @@ static void storage() {
assert(sensors.begin()); assert(!sensors.isGPSDetected()&&Serial1.begins==0);
sensors.configureSharedPeripheralMode(false);
assert(sensors._wisblock_power.holds()&mesh::gps::ExternalStorage);
assert(!sensors.canRemoveSharedStoragePowerForDeepSleep());
assert(digitalRead(WB_IO2)==HIGH); requireNoLow();
sensors.configureSharedPeripheralMode(true);
assert(digitalRead(WB_IO2)==HIGH); requireNoLow();
@@ -298,6 +299,7 @@ static void storage() {
static void fixedStorage() {
fresh(); TestLocation location; TestEnvironment sensors(location); bootUart(sensors,location);
assert(sensors._wisblock_power.holds()&mesh::gps::ExternalStorage);
assert(!sensors.canRemoveSharedStoragePowerForDeepSleep());
assert(sensors.isGpsPowered()&&digitalRead(WB_IO2)==HIGH);
assert(!sensors.active()&&!Serial1.installed); requireNoLow();
assert(mesh::ota::OtaStoreQspiNrf52::probes==0);
@@ -319,6 +321,7 @@ static void storageIds() {
sensors.configureSharedPeripheralMode(false);
const bool storage_held=selected!=0||(id!=0&&id!=0xffffffU);
assert(bool(sensors._wisblock_power.holds()&mesh::gps::ExternalStorage)==storage_held);
assert(sensors.canRemoveSharedStoragePowerForDeepSleep()==!storage_held);
assert(digitalRead(WB_IO2)==int(storage_held||compile_held));
assert(mesh::ota::OtaStoreQspiNrf52::probes>0);
}
@@ -418,6 +421,32 @@ static void dedicated() {
#endif
}
static void terminalStorage() {
SensorManager unknown;
assert(!unknown.canRemoveSharedStoragePowerForDeepSleep());
for(uint8_t holds:{uint8_t(0),uint8_t(mesh::gps::RadioPower),uint8_t(mesh::gps::EthernetPower),
uint8_t(mesh::gps::ExternalStorage),uint8_t(0x80)}) {
for(bool shared:{false,true}) {
fresh();TestLocation location;TestEnvironment sensors(location);
sensors._wisblock_power=mesh::gps::WisBlockSharedPowerPolicy(holds);
assert(!sensors.canRemoveSharedStoragePowerForDeepSleep());
assert(sensors._wisblock_power.configure(shared?mesh::gps::WisBlockPeripheralMode::Shared
:mesh::gps::WisBlockPeripheralMode::GpsOnly));
assert(!sensors.canRemoveSharedStoragePowerForDeepSleep());
sensors._wisblock_power.setDiscoveryActive(false);
const size_t events=hardware_events.size();
const unsigned probes=mesh::ota::OtaStoreQspiNrf52::probes;
const bool allowed=(holds&(mesh::gps::ExternalStorage|0x80))==0;
assert(sensors.canRemoveSharedStoragePowerForDeepSleep()==allowed);
sensors._wisblock_power.setGpsActive(true);
assert(sensors.canRemoveSharedStoragePowerForDeepSleep()==allowed);
assert(hardware_events.size()==events&&mesh::ota::OtaStoreQspiNrf52::probes==probes);
sensors._wisblock_power.setDiscoveryActive(true);
assert(!sensors.canRemoveSharedStoragePowerForDeepSleep());
}
}
}
int main(int argc,char** argv) {
assert(argc==2);
if(!std::strcmp(argv[1],"uart")) uart(false);
@@ -440,5 +469,6 @@ int main(int argc,char** argv) {
else if(!std::strcmp(argv[1],"wake_bus_failure")) i2c(false,4);
else if(!std::strcmp(argv[1],"initial_config_failure")) initialConfigFailure();
else if(!std::strcmp(argv[1],"dedicated")) dedicated();
else if(!std::strcmp(argv[1],"terminal_storage")) terminalStorage();
else assert(false&&"unknown scenario");
}
+8
View File
@@ -46,15 +46,18 @@ int main(int argc,char** argv) {
mesh::ota::OtaStoreQspiNrf52::raw_id=id;
mesh::ota::OtaStoreQspiNrf52::probes=0;
TestEnvironment sensors;
assert(!sensors.canRemoveSharedStoragePowerForDeepSleep());
if(prefs_first) {
sensors.configureSharedPeripheralMode(shared);
assert(sensors._wisblock_power.discoveryActive());
assert(!sensors.canRemoveSharedStoragePowerForDeepSleep());
assert(digitalRead(WB_IO2)==HIGH);
}
assert(sensors.begin());
if(!prefs_first) {
// Even a no-GPS image must wait for validated mode after discovery.
assert(digitalRead(WB_IO2)==HIGH);
assert(!sensors.canRemoveSharedStoragePowerForDeepSleep());
sensors.configureSharedPeripheralMode(shared);
}
assert(sensors._wisblock_power.isConfigured()&&!sensors._wisblock_power.discoveryActive());
@@ -62,6 +65,11 @@ int main(int argc,char** argv) {
const bool held=shared||storage||mesh::gps::wisBlockCompileRailHolds()!=0;
assert(digitalRead(WB_IO2)==int(held));
assert(bool(sensors._wisblock_power.holds()&mesh::gps::ExternalStorage)==storage);
const unsigned probes=mesh::ota::OtaStoreQspiNrf52::probes;
const size_t events=hardware_events.size();
assert(sensors.canRemoveSharedStoragePowerForDeepSleep()==!storage);
assert(sensors.canRemoveSharedStoragePowerForDeepSleep()==!storage);
assert(mesh::ota::OtaStoreQspiNrf52::probes==probes&&hardware_events.size()==events);
assert(mesh::ota::OtaStoreQspiNrf52::probes>0);
assert(!sensors.isGPSDetected()&&!sensors.isGpsPowered());
assert(!sensors.setSettingValue("gps","1"));
+9 -1
View File
@@ -47,6 +47,7 @@ extern MockFicr mock_ficr;
#define NRF_SPIM1 1
#define WB_IO2 2
#define OUTPUT 1
#define INPUT 0
#define HIGH 1
#define INPUT_PULLUP 2
inline void pinMode(int, int) {}
@@ -55,4 +56,11 @@ inline int digitalRead(int) { return HIGH; }
inline void vTaskDelete(void*) {}
inline void vTaskDelay(uint32_t value) { delay(value); }
inline uint32_t pdMS_TO_TICKS(uint32_t value) { return value; }
inline void xTaskCreate(void (*)(void*), const char*, int, void*, int, void*) {}
using TaskHandle_t = void*;
#define pdPASS 1
#define pdTRUE 1
#define taskENTER_CRITICAL() do {} while(0)
#define taskEXIT_CRITICAL() do {} while(0)
inline void xTaskNotifyGive(TaskHandle_t) {}
inline uint32_t ulTaskNotifyTake(int, uint32_t value) { delay(value);return 0; }
inline int xTaskCreate(void (*)(void*), const char*, int, void*, int, TaskHandle_t*) { return pdPASS; }
+1
View File
@@ -4,4 +4,5 @@ class SPIClass {
public:
SPIClass(int, int, int, int) {}
void begin() {}
void end() {}
};