Restore G3 startup tolerance and G2-compatible flash mode

This commit is contained in:
mikecarper committed 2026-10-18 13:58:02 -07:00
1 parent 5d741318f5
commit 015eff4c22
7 files changed
+334 -16

No files matched your search

+62 -1
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
"""Host contracts for the Station G3 Sensor role on each RF daughterboard slot.
"""Host contracts for Station G3 boot recipes and each Sensor RF slot.
Resolve tracked source recipes without starting PlatformIO, building firmware,
flashing hardware, or changing a release catalog. These fixtures check routing
@@ -15,6 +15,7 @@ import tempfile
import unittest
from test_upstream_observer_recipes import Recipes
from test_esp32_full_partition import apply_policy
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "scripts"))
@@ -63,6 +64,66 @@ class StationG3SensorRecipesTests(unittest.TestCase):
def setUpClass(cls):
cls.recipes = Recipes()
def assert_dio_boot_recipes(self, recipes):
board = json.loads((ROOT / "boards/station-g3-esp32.json").read_text())
self.assertEqual(board["build"]["flash_mode"], "dio")
self.assertEqual(board["build"]["arduino"]["memory_type"], "qio_opi")
targets = [section for section in recipes.config.sections()
if section.startswith("env:Station_G3_ESP32_")]
self.assertGreaterEqual(len(targets), 16)
for slot in SLOTS:
for role in ("repeater", "room_server", "sensor",
"repeater_observer_mqtt", "room_server_observer_mqtt"):
self.assertIn("env:" + slot + "_" + role, targets)
for role in ("companion_radio_usb", "companion_radio_ble",
"companion_radio_wifi", "companion_radio_full", "kiss_modem"):
self.assertIn("env:Station_G3_ESP32_" + role, targets)
for section in targets:
with self.subTest(target=section):
self.assertEqual(recipes.value(section, "board"), "station-g3-esp32")
flash_mode = recipes.value(section, "board_build.flash_mode")
self.assertEqual(flash_mode or board["build"]["flash_mode"], "dio")
memory_type = recipes.value(section, "board_build.arduino.memory_type")
self.assertEqual(memory_type or board["build"]["arduino"]["memory_type"], "qio_opi")
psram_type = recipes.value(section, "board_build.psram_type")
self.assertIn(psram_type, ("", "opi"))
for target in ("Station_G3_ESP32_repeater", "Station_G3_ESP32_r2_repeater",
"Station_G3_ESP32_companion_radio_full"):
section = "env:" + target
values = {
"build.mcu": board["build"]["mcu"],
"build.flash_mode": recipes.value(section, "board_build.flash_mode")
or board["build"]["flash_mode"],
"build.arduino.memory_type": board["build"]["arduino"]["memory_type"],
"build.partitions": recipes.value(section, "board_build.partitions"),
"upload.flash_size": board["upload"]["flash_size"],
}
with self.subTest(full_target=target):
# build.sh injects this existing policy for Full promotion;
# ordinary tracked recipes do not list it in extra_scripts.
full, _ = apply_policy(values, companion="companion_radio" in target)
self.assertEqual(full.values["build.flash_mode"], "dio")
self.assertEqual(full.values["build.arduino.memory_type"], "qio_opi")
def test_all_g3_roles_inherit_dio_without_changing_psram_or_full_policy(self):
self.assert_dio_boot_recipes(self.recipes)
def test_nested_role_override_cannot_silently_restore_qio(self):
recipes = Recipes()
recipes.config.set("env:Station_G3_ESP32_r2_sensor", "board_build.flash_mode", "qio")
# Exercise resolved inheritance, not a textual manifest-only assertion.
# subTest records failures instead of raising, so collect this control
# with an independent TestResult whose failure must be observable.
control = StationG3SensorRecipesTests(
"test_all_g3_roles_inherit_dio_without_changing_psram_or_full_policy")
control.recipes = recipes
result = unittest.TestResult()
control.run(result)
self.assertEqual(len(result.failures), 1)
self.assertFalse(result.errors)
self.assertIn("'qio' != 'dio'", result.failures[0][1])
def test_each_sensor_inherits_its_own_slot_and_only_one_pin_set(self):
text = (ROOT / "variants/station_g3_esp32/platformio.ini").read_text()
for slot, pins in SLOTS.items():
+65 -6
View File
@@ -13,7 +13,9 @@ VARIANT = ROOT / "variants/station_g3_esp32"
def harness(mutation=None):
source = (VARIANT / "target.cpp").read_text()
methods = "\n".join(body(source, signature) for signature in (
start = source.index("constexpr uint32_t kProbeTimeoutMs =")
methods = source[start:source.index(";", start) + 1] + "\n"
methods += "\n".join(body(source, signature) for signature in (
"bool waitForProbeReady(", "bool probeRadioSlot(",
"bool initializeSlot(", "bool radio_init()"))
if mutation == "accept-empty":
@@ -24,6 +26,10 @@ def harness(mutation=None):
guard = "delayMicroseconds(1);"
assert guard in methods, "production post-command BUSY guard missing"
methods = methods.replace(guard, "", 1)
if mutation == "old-busy-timeout":
methods = methods.replace("kProbeTimeoutMs = 1000", "kProbeTimeoutMs = 100")
if mutation == "old-attempt-limit":
methods = methods.replace("while (!found)", "for (unsigned attempt=0; attempt<3 && !found; ++attempt)", 1)
return r'''
#include <cassert>
#include <cstdio>
@@ -55,6 +61,8 @@ static unsigned now_ms=0,present=0,working=0,stuck=0,reads=0,recoveries=0,rx=0,r
static uint64_t now_us=0,busy_asserts_at[3]={},busy_clears_at[3]={};
static bool busy_scheduled[3]={};
static unsigned settle_guards=0,busy_high_samples=0;
static uint64_t reset_released_at[3]={},startup_busy_us=0;
static unsigned standby_count[3]={},refused_standby_count=0,usb_services=0;
static bool returned=false;
static int levels[64];
static std::vector<unsigned> initializations;
@@ -64,13 +72,19 @@ void delayMicroseconds(unsigned us) { assert(us==1);++settle_guards;now_us+=us;n
void pinMode(int,int) {}
void digitalWrite(int pin,int level) {
assert(pin>=0 && pin<64);levels[pin]=level;
for(unsigned slot=1;slot<=2;++slot)
if(pin==station_g3::pinsForSlot(slot).reset && level==LOW) busy_scheduled[slot]=false;
for(unsigned slot=1;slot<=2;++slot) {
if(pin!=station_g3::pinsForSlot(slot).reset) continue;
if(level==LOW) { busy_scheduled[slot]=false;standby_count[slot]=0; }
else reset_released_at[slot]=now_us;
}
}
int digitalRead(int pin) {
for(unsigned slot=1;slot<=2;++slot) {
if(pin!=station_g3::pinsForSlot(slot).busy) continue;
if(stuck&(1<<(slot-1))) return HIGH;
// Independent reset/command-readiness model. Some supported modules are
// busy longer at startup, or ignore several initial standby commands.
if((present&(1<<(slot-1))) && now_us<reset_released_at[slot]+startup_busy_us) return HIGH;
// Immediately after NSS rises BUSY still reads LOW. It asserts only
// after the guard interval, then remains HIGH during command handling.
if(busy_scheduled[slot] && now_us>=busy_asserts_at[slot] && now_us<busy_clears_at[slot]) {
@@ -113,6 +127,7 @@ struct SPI {
if(offset>=4) {
++reads;
if(!(present&(1<<(slot-1)))) return 0xff;
if(standby_count[slot]<=refused_standby_count) return 0xff;
return offset-4<6?"SX1261"[offset-4]:0;
}
return 0x22;
@@ -121,6 +136,7 @@ struct SPI {
assert(transaction && levels[station_g3::pinsForSlot(slot).nss]==HIGH);
assert(index==(command==0x80?2:20));transaction=false;
if(command==0x80 && (present&(1<<(slot-1)))) {
++standby_count[slot];
busy_scheduled[slot]=true;
busy_asserts_at[slot]=now_us+1;
busy_clears_at[slot]=now_us+51;
@@ -148,7 +164,7 @@ namespace station_g3 {
void rememberActiveSlot(uint8_t slot) { remembered=slot;slotState().last=slot; }
void serviceBootRecovery(StationG3Board&,bool first) { assert(first);++recoveries;throw Recovery(); }
}
namespace mesh { void serviceUsbLoggingPort() {} void serviceUsbTerminalPort() {} }
namespace mesh { void serviceUsbLoggingPort() {++usb_services;} void serviceUsbTerminalPort() {++usb_services;} }
@METHODS@
void reset(unsigned mask,unsigned successful,unsigned mode,unsigned last,unsigned busy=0) {
station_g3::slotState()=station_g3::SlotState();
@@ -157,8 +173,10 @@ void reset(unsigned mask,unsigned successful,unsigned mode,unsigned last,unsigne
spi.end();initializations.clear();
now_ms=reads=recoveries=rx=remembered=0;present=mask;working=successful;stuck=busy;
now_us=settle_guards=busy_high_samples=0;
startup_busy_us=refused_standby_count=usb_services=0;
for(unsigned slot=1;slot<=2;++slot) {
busy_scheduled[slot]=false;busy_asserts_at[slot]=busy_clears_at[slot]=0;
reset_released_at[slot]=standby_count[slot]=0;
}
for(int& value:levels) value=HIGH;
returned=false;
@@ -185,16 +203,47 @@ int main() {
assert(reads==oldreads && initializations.back()==expected);
assert(levels[station_g3::pinsForSlot(expected==1?2:1).reset]==LOW);
}
assert(now_ms<500);
assert(now_ms<=2000); // One shared one-second probe budget per socket.
}
reset(3,3,0,0,1);assert(radio_init());
assert(station_g3::slotState().detected==2 && station_g3::slotState().active==2);
assert(now_ms<500);
assert(now_ms<=2000);
// A present device exercises both the 1-us post-NSS guard and the ensuing
// real BUSY wait. Neither a no-op delay shim nor an early LOW is enough.
reset(1,1,0,0);assert(probeRadioSlot(1));
assert(settle_guards==1 && busy_high_samples==1 && reads==16 && now_ms>=12);
assert(!spi.transaction && levels[station_g3::pinsForSlot(1).reset]==LOW);
assert(now_ms<20); // No new delay for a fast, immediately responsive chip.
for(unsigned slot=1;slot<=2;++slot) for(bool wrap:{false,true}) {
for(unsigned scenario=0;scenario<5;++scenario) {
const unsigned bit=1U<<(slot-1);
reset(scenario==3?0:bit,bit,0,0,scenario==2?bit:0);
if(wrap) { now_us=uint64_t(UINT32_MAX-100)*1000;now_ms=now_us/1000; }
if(scenario==0) startup_busy_us=200000;
if(scenario==1) refused_standby_count=5;
// Mixed startup BUSY and command refusal must not each get a fresh
// one-second budget: this chip stays unresponsive after BUSY clears.
if(scenario==4) { startup_busy_us=800000;refused_standby_count=1000; }
const uint32_t started=millis();
const bool found=probeRadioSlot(slot);
const uint32_t elapsed=uint32_t(millis()-started);
if(scenario==0) {
assert(found && "200 ms startup BUSY must be tolerated");
assert(elapsed>=200 && elapsed<220 && usb_services>0);
} else if(scenario==1) {
assert(found && "five ignored startup commands must be retried");
assert(reads==96 && elapsed>=50 && elapsed<100 && usb_services>0);
} else {
assert(!found && elapsed==1000 && usb_services>0);
if(scenario==2) assert(reads==0); // Stuck BUSY never permits SPI reads.
}
assert(!spi.transaction && spi.slot==0);
assert(levels[station_g3::pinsForSlot(slot).reset]==LOW);
assert(levels[11]==HIGH && levels[43]==HIGH);
assert(initializations.empty() && rx==0 && remembered==0);
}
}
// Corrupt policy cannot shift outside its mask or silently become auto.
station_g3::slotState().bootMode=255;
assert(station_g3::slotState().choose(3)==0);
@@ -219,6 +268,10 @@ class StationG3DetectionTests(unittest.TestCase):
self.assertNotEqual(run.returncode,0,"negative control unexpectedly passed")
if mutation=="no-busy-settle":
self.assertIn("ReadRegister before delayed BUSY completed",run.stderr)
elif mutation=="old-busy-timeout":
self.assertIn("200 ms startup BUSY must be tolerated",run.stderr)
elif mutation=="old-attempt-limit":
self.assertIn("five ignored startup commands must be retried",run.stderr)
else:self.assertEqual(run.returncode,0,run.stdout+run.stderr)
def test_real_probe_selection_matrix_recovery_and_busy_deadlines(self):
@@ -233,5 +286,11 @@ class StationG3DetectionTests(unittest.TestCase):
def test_delayed_busy_requires_post_command_guard_negative_control(self):
self.run_case("no-busy-settle")
def test_old_short_busy_budget_rejects_supported_delayed_startup(self):
self.run_case("old-busy-timeout")
def test_old_three_attempt_limit_rejects_initial_command_refusals(self):
self.run_case("old-attempt-limit")
if __name__=="__main__":unittest.main()