Add saved controls for radio switching acceleration

This commit is contained in:
mikecarper committed 2026-10-23 10:41:26 -07:00
1 parent 5a4e81c4d5
commit 61d024bc72
78 files changed
+1696 -160

No files matched your search

+400
View File
@@ -0,0 +1,400 @@
// Exercise the production shared parser and persistence with deterministic
// radio admission and storage failures. The physical driver has its own tests.
#define main existing_cli_main
#include "cli_test.cpp"
#undef main
struct TuningRadio : Radio {
mesh::RadioSwitchCapabilities caps;
mesh::RadioSwitchTuning tuning;
mesh::RadioParamApplyResult admission = mesh::RadioParamApplyResult::APPLIED;
uint32_t default_spi_hz = 8888888;
unsigned boot_configurations = 0, attempts = 0, changes = 0, persists = 0;
TuningRadio() {
caps.flags = 127;
caps.spi_clocks = 0x1e;
tuning.flags = caps.flags;
}
mesh::RadioSwitchCapabilities switchTuningCapabilities() const override { return caps; }
mesh::RadioSwitchTuning switchTuning() const override { return tuning; }
uint32_t switchSpiHz() const override {
return tuning.spi_mhz ? uint32_t(tuning.spi_mhz) * 1000000UL : default_spi_hz;
}
bool configureBootSwitchTuning(const mesh::RadioSwitchTuning& value) override {
++boot_configurations;
if (!mesh::validRadioSwitchTuning(value, caps)) return false;
tuning = value;
return true;
}
mesh::RadioParamApplyResult trySetSwitchTuning(const mesh::RadioSwitchTuning& value,
bool (*persist)(void*, const mesh::RadioSwitchTuning&) = nullptr,
void* context = nullptr) override {
++attempts;
if (!mesh::validRadioSwitchTuning(value, caps)) return mesh::RadioParamApplyResult::FAILED;
if (admission != mesh::RadioParamApplyResult::APPLIED) return admission;
const auto previous = tuning;
tuning = value;
if (persist) {
++persists;
if (!persist(context, value)) {
tuning = previous;
return mesh::RadioParamApplyResult::FAILED;
}
}
if (value != previous) {
++changes;
p.resetSwitchTest();
}
return mesh::RadioParamApplyResult::APPLIED;
}
};
struct TuningFixture {
MemoryFS fs;
Clock clock;
TuningRadio radio;
mesh::RadioProfileCLI cli;
char reply[160];
TuningFixture() { g_mock_millis = 0; cli.begin(&fs, &radio, &clock); }
const char* cmd(const char* text, bool success = true, bool remote = false) {
memset(reply, 0x55, sizeof(reply));
assert(cli.handle(text, reply, sizeof(reply), remote));
assert(memchr(reply, 0, sizeof(reply)));
if (!strncmp(text, "set ", 4)) {
if ((!strncmp(reply, "OK", 2)) != success) {
fprintf(stderr, "%s -> %s\n", text, reply);
assert(false);
}
}
return reply;
}
void advance(uint32_t milliseconds) { g_mock_millis += milliseconds; cli.loop(); }
};
static void crc(std::vector<uint8_t>& image) {
assert(image.size() == 24);
uint32_t value = 0xffffffffU;
for (size_t i = 0; i < image.size() - 4; ++i) {
value ^= image[i];
for (unsigned bit = 0; bit < 8; ++bit)
value = (value >> 1) ^ ((value & 1) ? 0xedb88320U : 0);
}
memcpy(image.data() + image.size() - 4, &value, 4);
}
static void controlsAndPersistence() {
TuningFixture f;
assert(f.radio.tuning.flags == 127 && !f.radio.tuning.spi_mhz);
assert(f.fs.files.empty());
const char* features[] = {"warm", "fs", "packet", "iq", "modem", "batch", "buffer"};
for (unsigned i = 0; i < 7; ++i) {
char command[48];
snprintf(command, sizeof(command), "get radio.fast.%s", features[i]);
assert(!strcmp(f.cmd(command), "> on"));
snprintf(command, sizeof(command), "set radio.fast.%s off", features[i]);
f.cmd(command);
assert(!(f.radio.tuning.flags & (1U << i)));
const auto expected = f.radio.tuning;
TuningRadio reboot;
mesh::RadioProfileCLI restored;
restored.begin(&f.fs, &reboot, &f.clock);
assert(reboot.tuning == expected);
// An absent radio2 record must not prevent independent fast-policy restore.
assert(!f.fs.files.count("/radio_profiles"));
snprintf(command, sizeof(command), "set radio.fast.%s on", features[i]);
f.cmd(command);
assert(f.radio.tuning.flags & (1U << i));
}
for (unsigned mhz : {2, 4, 8, 16, 0}) {
char command[48];
snprintf(command, sizeof(command), "set radio.fast.spi %u", mhz);
f.cmd(command);
assert(f.radio.tuning.spi_mhz == mhz);
TuningRadio reboot;
mesh::RadioProfileCLI restored;
restored.begin(&f.fs, &reboot, &f.clock);
assert(reboot.tuning == f.radio.tuning);
assert(reboot.switchSpiHz() == (mhz ? mhz * 1000000UL : 8888888));
}
assert(strstr(f.cmd("get radio.fast"), "8888888"));
assert(!strcmp(f.cmd("get radio.fast.caps"),
"> features: warm fs packet iq modem batch buffer; spi=0,2,4,8,16"));
f.cmd("set radio.fast off");
assert(!f.radio.tuning.flags && f.radio.tuning.spi_mhz == 2);
f.cmd("set radio.fast on");
assert(f.radio.tuning.flags == f.radio.caps.flags && !f.radio.tuning.spi_mhz);
for (size_t size = 1; size <= sizeof(f.reply); ++size) {
char buffer[161];
memset(buffer, 0x55, sizeof(buffer));
for (const char* command : {"get radio.fast", "get radio.fast.caps"}) {
memset(buffer, 0x55, sizeof(buffer));
assert(f.cli.handle(command, buffer, size));
assert(memchr(buffer, 0, size) && buffer[size] == 0x55);
}
}
puts("Fast tuning: independent features, board clocks, saved restore and bounded output passed");
}
static void strictInputsAndUnsupportedFeatures() {
TuningFixture f;
const auto original = f.radio.tuning;
for (const char* command : {
"set radio.fast", "set radio.fast maybe", "set radio.fast on extra",
"set radio.fast.fs", "set radio.fast.fs ON", "set radio.fast.fs on extra",
"set radio.fast.spi", "set radio.fast.spi -2", "set radio.fast.spi +8",
"set radio.fast.spi 8mhz", "set radio.fast.spi 3", "set radio.fast.spi 32",
"set radio.fast.spi 256", "set radio.fast.spi 8 16", "set radio.fast.caps on"}) {
f.cmd(command, false);
assert(f.radio.tuning == original && f.fs.files.empty());
}
for (const char* command : {"get radio.fast on", "get radio.fast.fs extra",
"get radio.fast.spi extra", "get radio.fast.caps extra"}) {
assert(strstr(f.cmd(command), "Error"));
assert(f.radio.tuning == original && f.fs.files.empty());
}
f.radio.caps.flags &= ~mesh::RadioSwitchTuning::Buffer;
f.radio.tuning.flags &= f.radio.caps.flags;
f.radio.caps.spi_clocks &= ~(1U << 4);
const auto limited = f.radio.tuning;
f.cmd("set radio.fast.buffer on", false);
f.cmd("set radio.fast.spi 16", false);
assert(f.radio.tuning == limited && f.fs.files.empty());
assert(strstr(f.cmd("get radio.fast.buffer"), "unsupported"));
assert(!strstr(f.cmd("get radio.fast.caps"), "buffer"));
assert(strstr(f.reply, "; spi=0,2,4,8") && !strstr(f.reply, ",16"));
f.cmd("set radio.fast off");
f.cmd("set radio.fast on");
assert(f.radio.tuning.flags == f.radio.caps.flags);
f.radio.caps.spi_clocks = 0;
assert(strstr(f.cmd("get radio.fast.caps"), "; spi=na"));
assert(strstr(f.cmd("get radio.fast"), "> spi=na"));
f.cmd("set radio.fast.spi 0", false);
const auto no_clock_control = f.radio.tuning;
f.cmd("set radio.fast off");
assert(!f.radio.tuning.flags && !f.radio.tuning.spi_mhz);
f.cmd("set radio.fast on");
assert(f.radio.tuning == no_clock_control);
puts("Fast tuning: strict parsing and unsupported controls passed");
}
static void localOnlyAndBusyTransactions() {
TuningFixture f;
f.cmd("set radio.fast.fs off");
const auto original = f.radio.tuning;
const auto saved = f.fs.files["/radio_fast"];
const unsigned persists = f.radio.persists;
for (const auto result : {mesh::RadioParamApplyResult::BUSY,
mesh::RadioParamApplyResult::FAILED}) {
f.radio.admission = result;
f.cmd("set radio.fast.fs on", false);
assert(f.radio.tuning == original && f.radio.persists == persists);
assert(f.fs.files["/radio_fast"] == saved);
}
f.radio.admission = mesh::RadioParamApplyResult::APPLIED;
assert(strstr(f.cmd("set radio.fast.fs on", false, true), "local"));
f.cli.beginReplyCommand();
assert(strstr(f.cmd("set radio.fast.fs on", false), "local"));
assert(strstr(f.cmd("set radio.fast.fs on", false, true), "local"));
f.cli.endReplyCommand();
assert(!f.cli.hasReplyMutation());
assert(f.radio.tuning == original && f.fs.files["/radio_fast"] == saved);
assert(!strcmp(f.cmd("get radio.fast.fs", true, true), "> off"));
puts("Fast tuning: busy admission and local-only changes passed");
}
static void storageFailureKeepsCommittedPolicy() {
for (unsigned failure = 0; failure < 6; ++failure) {
TuningFixture f;
f.cmd("set radio.fast.fs off");
const auto original = f.radio.tuning;
const auto saved = f.fs.files["/radio_fast"];
if (failure == 0) f.fs.fail_write = true;
else if (failure == 1) f.fs.fail_write_after = 7;
else if (failure == 2) f.fs.fail_read_after = 7;
else if (failure == 3) f.fs.fail_rename = 1;
else if (failure == 4) f.fs.fail_rename = 2;
else {
f.fs.files["/radio_fast.tmp"] = {0};
f.fs.fail_remove = true;
}
f.cmd("set radio.fast.fs on", false);
assert(f.radio.tuning == original);
f.fs.fail_write = f.fs.fail_remove = false;
f.fs.fail_write_after = f.fs.fail_read_after = -1;
f.fs.fail_rename = 0;
TuningRadio reboot;
mesh::RadioProfileCLI restored;
restored.begin(&f.fs, &reboot, &f.clock);
assert(reboot.tuning == original);
assert(f.fs.files["/radio_fast"] == saved);
}
puts("Fast tuning: short writes, readback and atomic rename failures passed");
}
static void bootRecoveryAndDeferredAdmission() {
for (unsigned damaged_primary = 0; damaged_primary < 4; ++damaged_primary) {
TuningFixture f;
f.cmd("set radio.fast.modem off");
const auto expected = f.radio.tuning;
const auto saved = f.fs.files["/radio_fast"];
assert(f.fs.rename("/radio_fast", "/radio_fast.bak"));
if (damaged_primary == 1) f.fs.files["/radio_fast"] = {0};
else if (damaged_primary >= 2) {
auto& invalid = f.fs.files["/radio_fast"] = saved;
invalid[damaged_primary == 2 ? 3 : 4] = damaged_primary == 2 ? 128 : 3;
crc(invalid);
}
TuningRadio reboot;
mesh::RadioProfileCLI restored;
restored.begin(&f.fs, &reboot, &f.clock);
assert(reboot.tuning == expected);
assert(f.fs.files["/radio_fast"] == saved);
}
{
TuningFixture f;
f.cmd("set radio.fast.packet off");
const auto expected = f.radio.tuning;
const auto saved = f.fs.files;
TuningRadio reboot;
reboot.admission = mesh::RadioParamApplyResult::BUSY;
mesh::RadioProfileCLI restored;
restored.begin(&f.fs, &reboot, &f.clock);
assert(reboot.tuning != expected && f.fs.files == saved);
restored.loop();
assert(reboot.tuning != expected);
reboot.admission = mesh::RadioParamApplyResult::APPLIED;
g_mock_millis += 1000;
restored.loop();
assert(reboot.tuning == expected && f.fs.files == saved);
}
{
TuningFixture f;
f.cmd("set radio.fast.iq off");
const auto expected = f.radio.tuning;
f.fs.files["/radio_profiles"] = {0};
TuningRadio reboot;
mesh::RadioProfileCLI restored;
restored.begin(&f.fs, &reboot, &f.clock);
assert(reboot.tuning == expected); // A corrupt radio2 image is isolated.
}
puts("Fast tuning: interrupted commit, deferred boot admission and store isolation passed");
}
static void malformedAndUnreadableImagesStayProtected() {
for (unsigned corruption = 0; corruption < 5; ++corruption) {
TuningFixture f;
f.cmd("set radio.fast.fs off");
auto& image = f.fs.files["/radio_fast"];
if (corruption == 0) image[0] ^= 1;
else if (corruption == 1) { image[3] = 128; crc(image); }
else if (corruption == 2) { image[4] = 3; crc(image); }
else if (corruption == 3) { image[5] = 1; crc(image); }
else { image[2] = 2; crc(image); }
const auto damaged = image;
TuningRadio reboot;
mesh::RadioProfileCLI restored;
restored.begin(&f.fs, &reboot, &f.clock);
assert(reboot.tuning.flags == 127 && !reboot.tuning.spi_mhz);
assert(f.fs.files["/radio_fast"] == damaged);
}
{
TuningFixture f;
f.cmd("set radio.fast.fs off");
const auto saved = f.fs.files["/radio_fast"];
f.fs.fail_read_open = true;
TuningRadio reboot;
mesh::RadioProfileCLI restored;
restored.begin(&f.fs, &reboot, &f.clock);
f.fs.fail_read_open = false;
assert(restored.handle("set radio.fast.fs off", f.reply, sizeof(f.reply)));
assert(strstr(f.reply, "Error"));
assert(f.fs.files["/radio_fast"] == saved);
}
puts("Fast tuning: corrupt, unsupported, reserved and unreadable images protected");
}
static void bootRetriesAreBoundedAcrossRollover() {
for (bool rollover : {false, true}) {
for (const auto admission : {mesh::RadioParamApplyResult::BUSY,
mesh::RadioParamApplyResult::FAILED}) {
TuningFixture f;
f.cmd("set radio.fast.fs off");
const auto expected = f.radio.tuning;
const auto saved = f.fs.files;
TuningRadio reboot;
reboot.admission = admission;
mesh::RadioProfileCLI restored;
g_mock_millis = rollover ? UINT32_MAX - 500 : 100;
restored.begin(&f.fs, &reboot, &f.clock);
assert(reboot.attempts == 1);
// Hundreds of main-loop visits must not keep trying a rejected clock or
// RX policy. Millisecond rollover does not make its deadline due early.
for (unsigned i = 0; i < 999; ++i) {
++g_mock_millis;
restored.loop();
assert(reboot.attempts == 1);
}
++g_mock_millis;
restored.loop();
assert(reboot.attempts == 2 && reboot.tuning != expected);
restored.loop();
assert(reboot.attempts == 2);
reboot.admission = mesh::RadioParamApplyResult::APPLIED;
g_mock_millis += 1000;
restored.loop();
assert(reboot.attempts == 3 && reboot.tuning == expected);
assert(f.fs.files == saved && !reboot.persists);
}
}
puts("Fast tuning: failed/busy boot retries bounded across timer rollover");
}
static void savedSlowClockPrecedesRadioInitialization() {
for (bool backup : {false, true}) {
TuningFixture f;
f.cmd("set radio.fast off");
const auto expected = f.radio.tuning;
assert(!expected.flags && expected.spi_mhz == 2);
if (backup) {
assert(f.fs.rename("/radio_fast", "/radio_fast.bak"));
f.fs.files["/radio_fast"] = {0};
}
TuningRadio reboot;
reboot.admission = mesh::RadioParamApplyResult::FAILED;
assert(reboot.switchSpiHz() == 8888888);
mesh::RadioProfileCLI::configureBootTuning(&f.fs, reboot);
// This is the policy used by the chip's first initialization transaction.
// No runtime admission, RX restart, or persistence callback is permitted
// before that initialization; a runtime failure cannot veto boot policy.
assert(reboot.tuning == expected && reboot.switchSpiHz() == 2000000);
assert(reboot.boot_configurations == 1 && !reboot.attempts);
assert(!reboot.changes && !reboot.persists);
reboot.admission = mesh::RadioParamApplyResult::APPLIED;
mesh::RadioProfileCLI restored;
restored.begin(&f.fs, &reboot, &f.clock);
assert(reboot.tuning == expected);
}
for (unsigned unavailable = 0; unavailable < 4; ++unavailable) {
TuningFixture f;
if (unavailable != 0) f.cmd("set radio.fast off");
if (unavailable == 1) f.fs.files["/radio_fast"][0] ^= 1;
if (unavailable == 2) f.fs.fail_read_open = true;
TuningRadio reboot;
const auto defaults = reboot.tuning;
mesh::RadioProfileCLI::configureBootTuning(unavailable == 3 ? nullptr : &f.fs, reboot);
assert(reboot.tuning == defaults && reboot.switchSpiHz() == 8888888);
assert(!reboot.boot_configurations && !reboot.attempts && !reboot.persists);
}
puts("Fast tuning: saved fallback clock configured before first radio initialization");
}
int main() {
controlsAndPersistence();
strictInputsAndUnsupportedFeatures();
localOnlyAndBusyTransactions();
storageFailureKeepsCommittedPolicy();
bootRecoveryAndDeferredAdmission();
malformedAndUnreadableImagesStayProtected();
bootRetriesAreBoundedAcrossRollover();
savedSlowClockPrecedesRadioInitialization();
}
+12 -3
View File
@@ -35,6 +35,9 @@ PROFILES = {
# suffix. Non-RAK call hashes bind print/println order as well as all text bytes.
GOLDEN = {'rak_full': {'text': '9a68ce5a10905a201c3bddeb19f4819ce03c6dcaff248cff9c140803b70b14a7', 'calls': '56ed09dee7d53881b2c9fcf3f4ec9b6223e1562ce63e110a065e46706702b53b'}, 'nrf_full': {'text': 'b114ac12ae2ea01b7c47609b72f7eafb616c1dbeb7fb796a95b1377465fd9105', 'calls': '4973a7f21887d07b129cce4ae4cc8145f3f35e664bae09be658957210440d52e'}, 'nrf_minimal': {'text': '15fa8f290cd8c75cfde2fd2ac1cb5b5151a6755c68448fb2793dde3f27669033', 'calls': '6063fd1a699f9c0b588252bd8ce5922e0ff31cec6bbd24522789284ab2a37644'}, 'esp_full': {'text': '1e3b6781c81b5603f7092108e5e309543c60cef8ff4d7cb41a891f67396f716f', 'calls': 'b6335df2bd05b9cab44a7933469b76c5b2611c4b81057436a959feaf3bfbaf60'}}
FAST_HELP = (b' get radio.fast / get radio.fast.caps (supported saved controls)\r\n'
b' set radio.fast <on|off> / set radio.fast.spi <0|2|4|8|16>\r\n')
HARNESS = r'''
#include "CompanionHelpWriter.h"
#include <algorithm>
@@ -198,9 +201,14 @@ class CompanionHelpWriterTests(unittest.TestCase):
self.assertEqual(result.returncode,0,result.stdout+result.stderr)
result = subprocess.run([str(work/'test')],capture_output=True)
self.assertEqual(result.returncode,0,result.stderr.decode(errors='replace'))
self.assertEqual(hashlib.sha256(result.stdout).hexdigest(),GOLDEN[name]['text'])
self.assertEqual(result.stdout.count(FAST_HELP), 1)
previous_help = result.stdout.replace(FAST_HELP, b'')
self.assertEqual(hashlib.sha256(previous_help).hexdigest(),GOLDEN[name]['text'])
if name != 'rak_full':
self.assertEqual(hashlib.sha256(result.stderr).hexdigest(),GOLDEN[name]['calls'])
fast_call = b'P' + len(FAST_HELP).to_bytes(4, 'little') + FAST_HELP
self.assertEqual(result.stderr.count(fast_call), 1)
previous_calls = result.stderr.replace(fast_call, b'')
self.assertEqual(hashlib.sha256(previous_calls).hexdigest(),GOLDEN[name]['calls'])
else:
image = (work / 'test').read_bytes()
self.assertNotIn(b'Commands:\r\n stats-core / stats-radio / stats-radio-diag',image)
@@ -228,7 +236,8 @@ class CompanionHelpWriterTests(unittest.TestCase):
self.assertEqual(result.returncode,0,result.stdout+result.stderr)
result=subprocess.run([str(work/'test')],capture_output=True)
self.assertEqual(result.returncode,0,result.stderr.decode(errors='replace'))
self.assertEqual(hashlib.sha256(result.stdout).hexdigest(),GOLDEN['rak_full']['text'])
self.assertEqual(result.stdout.count(FAST_HELP), 1)
self.assertEqual(hashlib.sha256(result.stdout.replace(FAST_HELP, b'')).hexdigest(),GOLDEN['rak_full']['text'])
self.assertNotIn(b'Commands:\r\n stats-core / stats-radio / stats-radio-diag',(work/'test').read_bytes())
if __name__ == '__main__':
+1 -1
View File
@@ -88,7 +88,7 @@ struct Radio {
clock = 8888888 if target == "lilygo_tdeck" else 8000000
buffered = local or target not in LEGACY_FALLBACKS
expected = (4000000 if local else 2000000) if target == "heltec_rc32" else (
clock if buffered else 2000000)
clock)
selected = "spi" if local else "::SPI"
name = f"target_{index}_{int(local)}"
options = f"#define P_LORA_SCLK 5\n" if local else ""
+3 -1
View File
@@ -37,6 +37,7 @@ class LR1110PacketSetterCacheTests(unittest.TestCase):
state = (ROOT / "src/helpers/radiolib/LR1110ProfileSwitchState.h").read_text()
harness = r"""
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cstddef>
#include <cstdint>
#include <string>
@@ -106,6 +107,7 @@ struct LR1110 {
struct Radio : LR1110 {
uint32_t _activityAt=0;bool _headerSeen=false;
LR1110ProfileSwitchState _profileSwitch{true};
uint8_t _profileSwitchFeatures=55;
bool _profilePacketDirty = true;
uint32_t _profileFastResumes = 0;
Radio() { ownership = &_profileSwitch; }
@@ -182,7 +184,7 @@ int main(int argc, char** argv) {
flags = [] if os.name == "nt" else ["-fsanitize=address,undefined", "-fno-sanitize-recover=all",
"-fno-pie", "-no-pie"]
built = subprocess.run([compiler, "-std=gnu++11", "-Wall", "-Wextra", *flags,
str(cpp), "-o", str(cls.binary)],
"-I", str(ROOT / "src"), str(cpp), "-o", str(cls.binary)],
capture_output=True, text=True, timeout=60)
if built.returncode:
raise AssertionError(built.stdout + built.stderr)
+50 -6
View File
@@ -20,7 +20,7 @@ class LR1110ProfileSwitchTests(unittest.TestCase):
"thinknode_m3": "Nrf52BufferedRadioHal",
# Fast RX ownership is independent of the SPI transport. Keep M4
# on its prior bus until its SPIM3/BLE transfer path is qualified.
"thinknode_m4": None,
"thinknode_m4": "TunableRadioHal",
"minewsemi_me25ls01": "Nrf52BufferedRadioHal",
"thinknode_m7": "ESP32BufferedRadioHal",
"thinknode_m9": "ESP32BufferedRadioHal",
@@ -33,8 +33,9 @@ class LR1110ProfileSwitchTests(unittest.TestCase):
self.assertIn("-D RADIO_CLASS=CustomLR1110", config)
self.assertIn("-D WRAPPER_CLASS=CustomLR1110Wrapper", config)
if hal is not None:
self.assertIn(f"{hal} radioHal(", target_source)
self.assertIn("new Module(&radioHal,", target_source)
name = "radio_hal" if target == "thinknode_m4" else "radioHal"
self.assertIn(f"{hal} {name}(", target_source)
self.assertIn(f"new Module(&{name},", target_source)
else:
self.assertNotIn("BufferedRadioHal", target_source)
self.assertIn(
@@ -43,9 +44,11 @@ class LR1110ProfileSwitchTests(unittest.TestCase):
esp32_hal = (ROOT / "src/helpers/radiolib/ESP32BufferedRadioHal.h").read_text()
nrf52_hal = (ROOT / "src/helpers/radiolib/Nrf52BufferedRadioHal.h").read_text()
self.assertIn("SPISettings(hz, MSBFIRST, SPI_MODE0)", esp32_hal)
tunable_hal = (ROOT / "src/helpers/radiolib/TunableRadioHal.h").read_text()
self.assertIn("SPISettings(hz, MSBFIRST, SPI_MODE0)", tunable_hal)
self.assertIn("TunableRadioHal(bus, hz, false, true,", esp32_hal)
self.assertIn("spi->transferBytes(out, in, len)", esp32_hal)
self.assertIn("SPISettings(hz, MSBFIRST, SPI_MODE0)", nrf52_hal)
self.assertIn("TunableRadioHal(bus, hz, false, true,", nrf52_hal)
self.assertIn("spi->transfer(out, in, len)", nrf52_hal)
for target in ("thinknode_m7", "thinknode_m9"):
source = (ROOT / "variants" / target / "target.cpp").read_text()
@@ -62,9 +65,11 @@ class LR1110ProfileSwitchTests(unittest.TestCase):
implementation = method(source, "int16_t snapshotIrqAfterRxExit(")
harness = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cstddef>
#include <cstdint>
#include <cstring>
#define MC_LR1110_FAST_PROFILE_SWITCH 1
#define RADIOLIB_ERR_NONE 0
#define RADIOLIB_MODULE_SPI_WIDTH_STATUS 2
struct Module {
@@ -96,7 +101,8 @@ int main() {
def test_fast_rx_skips_only_unchanged_packet_tuple_and_fails_closed(self):
source = (ROOT / "src/helpers/radiolib/CustomLR1110.h").read_text()
signatures = (
"void setProfileSwitchOptimization(", "void setProfileStandbyWarm(",
"uint8_t profileSwitchCapabilities(", "uint8_t getProfileSwitchFeatures(",
"void setProfileSwitchFeatures(", "void setProfileSwitchOptimization(", "void setProfileStandbyWarm(",
"void beginProfileSwitch(", "void endProfileSwitch(",
"bool profileSwitchFailed(", "int16_t standby()", "int16_t standby(uint8_t",
"int16_t sleep()", "int16_t reset()", "int16_t stageMode(",
@@ -109,10 +115,12 @@ int main() {
)
harness = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cstdint>
#include <cstddef>
#include <string>
@STATE@
#define MC_LR1110_FAST_PROFILE_SWITCH 1
#define RADIOLIB_ERR_NONE 0
#define RADIOLIB_LR11X0_STANDBY_XOSC 0
#define RADIOLIB_LR11X0_RX_TIMEOUT_INF 0xffffff
@@ -161,6 +169,7 @@ struct LR1110 {
struct Radio:LR1110 {
uint32_t _activityAt=0;bool _headerSeen=false;
LR1110ProfileSwitchState _profileSwitch{true};
uint8_t _profileSwitchFeatures=55;
bool _profileStandbyWarm=false,_profilePacketDirty=false;
uint32_t _profileFastResumes=0;
uint32_t pendingIrq=0;
@@ -216,7 +225,29 @@ int main() {
prime(r);r.stageMode(2,nullptr);assert(!r._profileSwitch.rxValid);
prime(r);r.sleep();assert(!r._profileSwitch.rxValid);
prime(r);r.reset();assert(!r._profileSwitch.rxValid);
// FS and packet reuse remain independently controllable without disabling
// warm receive ownership; unsupported SX1262 IQ/transport bits stay absent.
for(unsigned requested=0;requested<256;++requested) {
Radio x;x.setProfileSwitchFeatures(uint8_t(requested));
const uint8_t features=requested & x.profileSwitchCapabilities();
assert(x.getProfileSwitchFeatures()==features && x.trace.empty());
assert(!(features & (mesh::RadioSwitchTuning::Iq | mesh::RadioSwitchTuning::Buffer)));
assert(x._profileSwitch.enabled==bool(features & mesh::RadioSwitchTuning::Warm));
assert(!x._profileSwitch.rxValid && !x._profileSwitch.modulationValid);
assert(x.startReceive()==0);x.setProfileStandbyWarm(true);x.trace.clear();
x.beginProfileSwitch(true);assert(x.standby()==0);
const bool warm=features & mesh::RadioSwitchTuning::Warm;
const bool fs=features & mesh::RadioSwitchTuning::Fs;
assert(x.trace==(warm ? (fs ? "IF":"s") : "S"));x.trace.clear();
assert(x.startReceive()==0);
const bool packet=features & mesh::RadioSwitchTuning::Packet;
assert(x.trace==(warm ? (packet ? "QWR":"QPWR") : "N"));
x.endProfileSwitch(true);x.trace.clear();x._profilePacketDirty=true;
x.setProfileSwitchFeatures(features);
assert(x.trace.empty() && !x._profileSwitch.rxValid && !x._profilePacketDirty);
}
}
'''
self.compile_run(harness.replace("@STATE@", state).replace("@METHODS@", methods))
@@ -226,6 +257,7 @@ int main() {
)
harness = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cstdint>
@STATE@
int main() {
@@ -279,10 +311,12 @@ int main() {
)
harness = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cmath>
#include <cstdint>
#include <initializer_list>
@STATE@
#define MC_LR1110_FAST_PROFILE_SWITCH 1
#define RADIOLIB_ERR_NONE 0
#define RADIOLIB_ERR_INVALID_SPREADING_FACTOR -1
#define RADIOLIB_ERR_INVALID_CODING_RATE -2
@@ -298,6 +332,7 @@ int main() {
#define RADIOLIB_LR11X0_LORA_LDRO_DISABLED 0
struct Radio {
LR1110ProfileSwitchState _profileSwitch{true};
uint8_t _profileSwitchFeatures=55;
uint8_t spreadingFactor=7, bandwidth=4, codingRate=1, ldrOptimize=0;
float bandwidthKhz=125;
bool ldroAuto=true;
@@ -350,6 +385,15 @@ int main() {
assert(r.spreadingFactor==oldSf && r.bandwidth==oldBw && r.codingRate==oldCr);
assert(r.bandwidthKhz==oldKhz && r.ldrOptimize==oldLdro);
assert(r._profileSwitch.failed() && !r._profileSwitch.rxValid);
for(unsigned features=0;features<64;++features) {
Radio x;x._profileSwitchFeatures=features;
assert(x.setLoRaModulationParams(125,7,5)==0 && x.writes==1 && x.queries==1);
x._profileSwitch.rxResult(0,true);x._profileSwitch.begin(true,true);
x._profileSwitch.standbyResult(0);
assert(x.setLoRaModulationParams(125,7,5)==0);
assert(x.queries==((features & mesh::RadioSwitchTuning::Modem) ? 1U:2U));
assert(x.writes==((features & mesh::RadioSwitchTuning::Batch) ? 1U:2U));
}
}
'''
self.compile_run(harness.replace("@STATE@", state).replace("@PRODUCTION@", production))
+1 -1
View File
@@ -185,7 +185,7 @@ int main() {
assert(SPI1._spim.drv_inst_idx==unsigned(MC_NRF52_HIL_SPIM1_RADIO));
assert(c_indices()==(MC_NRF52_HIL_SPIM1_RADIO?0x3210u:0x2100u));
assert(radio.module->hal->spi==&SPI && !radio.module->hal->initInterface);
assert(radio.module->hal->spiSettings.hz==(MC_NRF52_HIL_SPIM1_RADIO?8000000:2000000));
assert(radio.module->hal->spiSettings.hz==8000000);
assert(radio_init() && SPI.begins==1 && SPI1.begins==0);
uint8_t tx[]={0x86,1,2,3},rx[4]={};radio.module->hal->spiTransfer(tx,4,rx);
assert(SPI.bursts==unsigned(MC_NRF52_HIL_SPIM1_RADIO));
+1 -1
View File
@@ -23,7 +23,7 @@ int main() {
ArduinoHal& radio=concrete;
constexpr unsigned budget=MC_NRF_RADIO_BUSY_SPIN_POLLS;
#if MC_NRF_RADIO_BUSY_SPIN_POLLS == 0
static_assert(sizeof(Nrf52BufferedRadioHal)==sizeof(ArduinoHal),"default RAM growth");
static_assert(sizeof(Nrf52BufferedRadioHal)==sizeof(TunableRadioHal),"default RAM growth");
#endif
// The first pre-transfer wait delegates immediately, even in opt-in builds.
for(unsigned i=0;i<1000;++i) radio.yield();
+14 -2
View File
@@ -52,7 +52,7 @@ class ProfileIqCacheTest(unittest.TestCase):
sanitizers = [] if os.name == "nt" else [
"-fsanitize=address,undefined", "-fno-omit-frame-pointer", "-no-pie"]
built = subprocess.run([os.environ.get("CXX", "g++"), "-std=c++17",
"-Wall", "-Wextra", *sanitizers, str(cpp), "-o", str(binary)],
"-Wall", "-Wextra", *sanitizers, "-I", str(ROOT / "src"), str(cpp), "-o", str(binary)],
capture_output=True, text=True)
self.assertEqual(built.returncode, 0, built.stderr)
ran = subprocess.run([str(binary)], capture_output=True, text=True)
@@ -61,7 +61,8 @@ class ProfileIqCacheTest(unittest.TestCase):
def test_owned_iq_ack_wire_payload_setters_lifecycle_and_errors(self):
source = (ROOT / "src/helpers/radiolib/CustomSX1262.h").read_text()
state = (ROOT / "src/helpers/radiolib/SX1262ProfileSwitchState.h").read_text().replace("#pragma once", "")
signatures = ("void setProfileSwitchOptimization(", "void beginProfileSwitch(",
signatures = ("uint8_t defaultProfileSwitchFeatures(", "uint8_t profileSwitchCapabilities(", "uint8_t getProfileSwitchFeatures(",
"void setProfileSwitchFeatures(", "void setProfileSwitchOptimization(", "void beginProfileSwitch(",
"void endProfileSwitch(", "int16_t reset(bool", "int16_t sleep(bool",
"int16_t stageMode(", "int16_t setPreambleLength(", "int16_t setCRC(",
"int16_t implicitHeader(", "int16_t explicitHeader()", "int16_t invertIQ(",
@@ -71,11 +72,15 @@ class ProfileIqCacheTest(unittest.TestCase):
methods = "\n".join(method(source, signature).replace(" override", "") for signature in signatures)
fixture = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cstdint>
#include <cstddef>
#include <string>
#include <vector>
@DEFINES@
#define MC_SX1262_FAST_PROFILE_SWITCH 1
#define MC_SX1262_FAST_FS 1
#define MC_SX1262_FAST_MODEM_CACHE 0
#define MC_SX1262_FAST_PACKET_CACHE 0
#define RADIOLIB_ERR_NONE 0
#define RADIOLIB_SX126X_STANDBY_RC 0
@@ -153,10 +158,12 @@ struct SX1262 {
struct Radio : SX1262 {
uint32_t _activityAt=0;bool _headerSeen=false;
SX1262ProfileSwitchState _profileSwitch{true};
uint8_t _profileSwitchFeatures=defaultProfileSwitchFeatures();
bool _coldStandby=true,_tcxoWakePending=false,_rx_ps_rf_rx_disabled=false;
uint32_t _profileFastResumes=0;
#ifdef MC_SX1262_HIL
bool hilSkipUnchangedPacket=false,hilSkipUnchangedIq=true;
bool hilUseFsOnRetune=false,hilSkipConfirmedModemQuery=false;
uint32_t hilPacketSkips=0,hilIqWorkaroundSkips=0;
#endif
@METHODS@
@@ -197,6 +204,11 @@ int main() {
#endif
assert(r.startReceive()==0 && r.trace=="CIiPWR");
}
// A runtime IQ opt-out keeps owned RX but forces its checked workaround.
{ Radio r;prime(r);r.setProfileSwitchFeatures(
r.getProfileSwitchFeatures() & ~mesh::RadioSwitchTuning::Iq);
r.trace.clear();prime(r);arm(r);
assert(r.startReceive()==0 && r.trace=="CIiPWR"); }
// Owned preamble staging is the intended fast case, unlike an ordinary
// preamble command. Every ordinary packet/direct-register path revokes IQ.
{ Radio r;prime(r);arm(r);assert(r.setPreambleLength(64)==0 && r.trace.empty());
+4
View File
@@ -24,6 +24,7 @@ def build_source(family='CustomSX1262',remove_reset=False,include_duty=False):
source=source.replace('#include <cassert>', '''#include <cassert>
#include <cstdio>
#include <RadioProfiles.h>
#include <helpers/RadioSwitchTuning.h>
#include <helpers/radiolib/NoiseFloorEstimator.h>
#include <helpers/radiolib/SX1262ProfileSwitchState.h>
#include <helpers/radiolib/LR1110ProfileSwitchState.h>
@@ -59,11 +60,14 @@ namespace mesh { enum class RadioParamApplyResult { APPLIED, BUSY, FAILED }; }
int64_t esp_timer_get_time(){return int64_t(now_ms)*1000;}
uint32_t profileTimestamp(bool){return now_ms*1000;}
uint32_t profileElapsedUs(uint32_t a,uint32_t b,bool){return b-a;}''')
source=source.replace('struct Board {', 'struct Board { bool prepareRadioProfileCpuClock(bool, bool& changed) {changed=false;return true;}')
source=source.replace('struct Module {', '''struct Module {
enum {MODE_RX=1};void setRfSwitchState(int){}''')
source=source.replace('struct Chip : LR2021 {', '''using SX1262=LR2021;using SX1268=LR2021;using LLCC68=LR2021;using STM32WLx=LR2021;using LR1110=LR2021;using LR1121=LR2021;
struct Chip : LR2021 {
SX1262ProfileSwitchState _profileSwitch{true};bool _coldStandby=true;
uint8_t _profileSwitchFeatures=63;
uint8_t getProfileSwitchFeatures() const { return _profileSwitchFeatures; }
uint32_t _profileFastResumes=0;
bool _profilePacketDirty=false;Module* mod=&module;
int16_t snapshotIrqAfterRxExit(uint32_t* pending){*pending=irq;return snapshot_failure;}
+143
View File
@@ -0,0 +1,143 @@
"""Execute production switch-feature masks and conservative modulation paths."""
from pathlib import Path
import unittest
from test_radio_receive_contract import method
import test_sx1262_batched_modulation as compiler
ROOT = Path(__file__).resolve().parents[1]
class RadioSwitchDriverControlsTests(unittest.TestCase):
compile_run = compiler.BatchedModulationTests.compile_run
def test_sx1262_compile_opt_outs_cannot_be_enabled_at_runtime(self):
source = (ROOT / "src/helpers/radiolib/CustomSX1262.h").read_text()
definitions = source[source.index("#ifndef MC_SX1262_FAST_PROFILE_SWITCH"):
source.index("#ifndef SX126X_TX_BUSY_TIMEOUT_MS")]
methods = "\n".join(method(source, signature) for signature in (
"uint8_t defaultProfileSwitchFeatures(", "uint8_t profileSwitchCapabilities(",
"uint8_t getProfileSwitchFeatures(", "void setProfileSwitchFeatures("))
state = (ROOT / "src/helpers/radiolib/SX1262ProfileSwitchState.h").read_text()
harness = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
@OVERRIDES@
@DEFINITIONS@
@STATE@
struct Radio {
SX1262ProfileSwitchState _profileSwitch{true};
uint8_t _profileSwitchFeatures=defaultProfileSwitchFeatures();
@METHODS@
};
int main() {
Radio r;
assert(r.profileSwitchCapabilities()==@CAPS@);
assert(r.getProfileSwitchFeatures()==@CAPS@);
for(unsigned bits=0;bits<256;++bits) {
r._profileSwitch.rxResult(0,true);
r._profileSwitch.begin(true,true);r._profileSwitch.standbyResult(0);
r._profileSwitch.packetResult(0,32,1,255,0,0);
r._profileSwitch.modulationResult(0,7,3,1,0);
r.setProfileSwitchFeatures(uint8_t(bits));
assert(r.getProfileSwitchFeatures()==(bits & @CAPS@));
assert(r._profileSwitch.enabled==bool(bits & @CAPS@ & mesh::RadioSwitchTuning::Warm));
assert(!r._profileSwitch.rxValid && !r._profileSwitch.active
&& !r._profileSwitch.modulationValid && !r._profileSwitch.packetValid);
}
}
'''
policies = (
("", 63),
("#define MC_SX1262_FAST_PROFILE_SWITCH 0", 32),
("#define MC_SX1262_FAST_FS 0", 37),
("#define MC_SX1262_FAST_PACKET_CACHE 0", 59),
# The I/O-expander board retains its proven IQ workaround cache.
("#define MC_SX1262_FAST_FS 0\n#define MC_SX1262_FAST_PACKET_CACHE 0\n"
"#define MC_SX1262_FAST_IQ_CACHE 1", 41),
)
for overrides, caps in policies:
with self.subTest(overrides=overrides):
self.compile_run(harness.replace("@OVERRIDES@", overrides)
.replace("@DEFINITIONS@", definitions)
.replace("@STATE@", state)
.replace("@METHODS@", methods)
.replace("@CAPS@", str(caps)))
def test_lr1110_compile_opt_out_keeps_all_shortcuts_unavailable(self):
source = (ROOT / "src/helpers/radiolib/CustomLR1110.h").read_text()
methods = "\n".join(method(source, signature) for signature in (
"uint8_t profileSwitchCapabilities(", "uint8_t getProfileSwitchFeatures(",
"void setProfileSwitchFeatures("))
state = (ROOT / "src/helpers/radiolib/LR1110ProfileSwitchState.h").read_text()
harness = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#define MC_LR1110_FAST_PROFILE_SWITCH @ENABLED@
@STATE@
struct Radio {
LR1110ProfileSwitchState _profileSwitch{true};
uint8_t _profileSwitchFeatures=profileSwitchCapabilities();
bool _profilePacketDirty=true;
@METHODS@
};
int main() {
Radio r;
assert(r.profileSwitchCapabilities()==@CAPS@);
for(unsigned bits=0;bits<256;++bits) {
r._profilePacketDirty=true;r.setProfileSwitchFeatures(uint8_t(bits));
assert(r.getProfileSwitchFeatures()==(bits & @CAPS@));
assert(r._profileSwitch.enabled==bool(bits & @CAPS@ & mesh::RadioSwitchTuning::Warm));
assert(!r._profileSwitch.rxValid && !r._profileSwitch.modulationValid
&& !r._profilePacketDirty);
}
}
'''
for enabled in (0, 1):
with self.subTest(enabled=enabled):
self.compile_run(harness.replace("@STATE@", state)
.replace("@METHODS@", methods)
.replace("@ENABLED@", str(enabled))
.replace("@CAPS@", "55" if enabled else "0"))
def test_both_wrappers_execute_normal_setters_when_batch_is_disabled(self):
harness = r'''
#include <cassert>
#include <string>
#include <helpers/RadioSwitchTuning.h>
#define RADIOLIB_ERR_NONE 0
struct @CHIP@ {
std::string trace;char failure=0;
int call(char op) {trace+=op;return failure==op?-7:0;}
int setLoRaModulationParams(float bw,uint8_t sf,uint8_t cr) {
assert(bw==62.5f && sf==7 && cr==5);return call('M');
}
int setSpreadingFactor(uint8_t sf) {assert(sf==7);return call('S');}
int setBandwidth(float bw) {assert(bw==62.5f);return call('B');}
int setCodingRate(uint8_t cr) {assert(cr==5);return call('C');}
};
struct Wrapper {
@CHIP@ radio;@CHIP@* _radio=&radio;uint8_t features=0;
uint8_t getDriverSwitchFeatures() const {return features;}
@METHOD@
};
int main() {
for(unsigned flags=0;flags<128;++flags) for(char failure:std::string("0MSBC")) {
Wrapper w;w.features=flags;w.radio.failure=failure;
const bool batch=flags & mesh::RadioSwitchTuning::Batch;
assert(w.applyProfileModulation(62.5f,7,5)==(batch ? failure!='M'
: failure!='S' && failure!='B' && failure!='C'));
assert(w.radio.trace==(batch ? "M":failure=='S'?"S":failure=='B'?"SB":"SBC"));
}
}
'''
for family in ("SX1262", "LR1110"):
with self.subTest(family=family):
source = (ROOT / f"src/helpers/radiolib/Custom{family}Wrapper.h").read_text()
self.compile_run(harness.replace("@CHIP@", f"Custom{family}")
.replace("@METHOD@", method(source, "bool applyProfileModulation(")))
if __name__ == "__main__":
unittest.main()
+59
View File
@@ -0,0 +1,59 @@
"""Exercise saved fast-switch controls through the production shared CLI."""
from pathlib import Path
import re
import shutil
import subprocess
import tempfile
import unittest
from cpp_source import body
ROOT = Path(__file__).resolve().parents[1]
class RadioSwitchTuningCLITest(unittest.TestCase):
def test_five_cli_roles_restore_tuning_before_first_radio_initialization(self):
for role in ('companion_radio', 'simple_repeater', 'simple_room_server',
'simple_sensor', 'simple_secure_chat'):
with self.subTest(role=role):
source = (ROOT / f'examples/{role}/main.cpp').read_text()
setup = body(source, '\nvoid setup(')
setup = re.sub(r'//[^\n]*|/\*.*?\*/', '', setup, flags=re.S)
hook = 'mesh::RadioProfileCLI::configureBootTuning('
self.assertEqual(setup.count(hook), 1)
if role != 'simple_secure_chat':
self.assertLess(setup.index('beginRegulatorSettings('), setup.index(hook))
self.assertRegex(setup, r'configureBootTuning\(regulator_prefs_fs,\s*radio_driver\)')
else:
self.assertRegex(setup, r'configureBootTuning\(\s*volatile_primary_fs\s*\?\s*nullptr\s*:\s*primary_fs,\s*radio_driver\)')
for mount in ('beginInternalPrimaryFilesystemSafely(',
'LittleFS.begin()', 'SPIFFS.begin(true)'):
self.assertEqual(setup.count(mount), 1)
self.assertLess(setup.index(mount), setup.index(hook))
self.assertEqual(setup.count('the_mesh.begin('), 1)
self.assertLess(setup.index('fast_rng.begin('), setup.index('the_mesh.begin('))
radio_init = re.search(r'\bradio_init\s*\(', setup)
self.assertIsNotNone(radio_init)
self.assertLess(setup.index(hook), radio_init.start())
if role == 'simple_secure_chat':
self.assertLess(radio_init.start(), setup.index('fast_rng.begin('))
def test_saved_controls_and_failure_transactions(self):
compiler = shutil.which('g++') or shutil.which('clang++')
self.assertIsNotNone(compiler)
with tempfile.TemporaryDirectory() as work:
for fleet_enabled in (False, True):
with self.subTest(fleet=fleet_enabled):
executable = Path(work) / f'switch-{int(fleet_enabled)}'
subprocess.run([
compiler, '-std=c++17', '-Wall', '-Wextra',
f'-DMESH_ENABLE_FLEET_CONTROL={int(fleet_enabled)}',
'-I', str(ROOT / 'test/fixtures/radio_profiles/mocks'),
'-I', str(ROOT / 'test/mocks'), '-I', str(ROOT / 'src'),
str(ROOT / 'src/helpers/RadioProfileCLI.cpp'),
str(ROOT / 'test/fixtures/radio_profiles/switch_tuning_test.cpp'),
'-o', str(executable)], check=True, capture_output=True, text=True)
subprocess.run([str(executable)], check=True)
if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,91 @@
"""Run the actual tuning transaction with radio ownership and RX/save faults."""
from pathlib import Path
import unittest
from test_radio_receive_contract import method
from test_radio_interrupt_recovery import compile_run
ROOT = Path(__file__).resolve().parents[1]
class RadioSwitchTransactionTest(unittest.TestCase):
def test_admission_restore_and_commit_order(self):
source = (ROOT / 'src/helpers/radiolib/RadioLibWrappers.cpp').read_text()
transaction = method(source, 'mesh::RadioParamApplyResult RadioLibWrapper::trySetSwitchTuning(')
compile_run(r'''
#include <cassert>
#include <initializer_list>
#include <helpers/RadioSwitchTuning.h>
namespace mesh { enum class RadioParamApplyResult { APPLIED, BUSY, FAILED }; }
using Result=mesh::RadioParamApplyResult;
struct RadioLibWrapper {
mesh::RadioSwitchTuning policy;
struct { unsigned resets=0; void resetSwitchTest(){++resets;} } _profiles;
uint8_t admission=1;
bool receiving=true, fail_rx=false, inject_packet=false, pending_packet=false;
unsigned admissions=0, resumes=0, mutations=0;
mesh::RadioSwitchCapabilities switchTuningCapabilities()const {
mesh::RadioSwitchCapabilities caps; caps.flags=63; return caps;
}
mesh::RadioSwitchTuning switchTuning()const{return policy;}
uint8_t beginReconfigure(){++admissions;return pending_packet?2:admission;}
void setDriverSwitchFeatures(uint8_t value){++mutations;policy.flags=value;}
bool configureBootSwitchTuning(const mesh::RadioSwitchTuning& value){
setDriverSwitchFeatures(value.flags);return true;
}
void endReconfigure(bool resume) {
if(resume){++resumes;receiving=!fail_rx;if(inject_packet)pending_packet=true;}
}
bool isInRecvMode()const{return receiving;}
Result trySetSwitchTuning(const mesh::RadioSwitchTuning&,
bool(*)(void*,const mesh::RadioSwitchTuning&)=nullptr,void*=nullptr);
};
struct Commit {
RadioLibWrapper* radio;
bool fail=false;
unsigned calls=0;
static bool save(void* context,const mesh::RadioSwitchTuning& value){
auto& self=*static_cast<Commit*>(context);
++self.calls;
assert(self.radio->receiving && self.radio->policy==value);
return !self.fail;
}
};
'''.replace('void*=nullptr', 'void* = nullptr') + transaction + r'''
int main(){
mesh::RadioSwitchTuning requested;requested.flags=63;
for(uint8_t rejected:{uint8_t(2),uint8_t(3)}){
RadioLibWrapper radio;radio.admission=rejected;Commit store{&radio};
auto result=radio.trySetSwitchTuning(requested,Commit::save,&store);
assert(result==(rejected==2?Result::BUSY:Result::FAILED));
assert(!radio.policy.flags && !radio.mutations && !store.calls && !radio._profiles.resets);
}
{
RadioLibWrapper radio;radio.pending_packet=true;Commit store{&radio};
assert(radio.trySetSwitchTuning(requested,Commit::save,&store)==Result::BUSY);
assert(radio.pending_packet && !radio.mutations && !radio.resumes && !store.calls);
}
{
RadioLibWrapper radio;Commit store{&radio};
auto invalid=requested;invalid.flags|=64;
assert(radio.trySetSwitchTuning(invalid,Commit::save,&store)==Result::FAILED);
assert(!radio.admissions && !radio.mutations && !store.calls);
assert(radio.trySetSwitchTuning(requested,Commit::save,&store)==Result::APPLIED);
assert(radio.policy==requested && radio._profiles.resets==1 && store.calls==1);
}
{
RadioLibWrapper radio;radio.fail_rx=true;Commit store{&radio};
assert(radio.trySetSwitchTuning(requested,Commit::save,&store)==Result::FAILED);
assert(!radio.policy.flags && !store.calls && radio.mutations==2);
}
{
RadioLibWrapper radio;radio.inject_packet=true;Commit store{&radio};store.fail=true;
assert(radio.trySetSwitchTuning(requested,Commit::save,&store)==Result::FAILED);
assert(!radio.policy.flags && store.calls==1 && radio.pending_packet);
assert(radio.receiving && radio.resumes==1 && radio.mutations==2);
}
}
''')
if __name__ == '__main__':
unittest.main()
+3 -1
View File
@@ -42,7 +42,9 @@ struct SPIClass {
}
uint8_t transfer(uint8_t value) { ++bytes; return value ^ 0xa5; }
};
struct ArduinoHal {
struct RadioLibHal { virtual ~RadioLibHal()=default; };
struct ArduinoHal : RadioLibHal {
bool initInterface = false;
SPIClass* spi;
SPISettings spiSettings;
explicit ArduinoHal(SPIClass& bus, SPISettings settings)
+18 -3
View File
@@ -16,7 +16,7 @@ class BatchedModulationTests(unittest.TestCase):
cpp, exe = Path(folder) / "test.cpp", Path(folder) / "test.exe"
cpp.write_text(source)
built = subprocess.run([os.environ.get("CXX", "g++"), "-std=c++17",
"-Wall", "-Wextra", str(cpp), "-o", str(exe)],
"-Wall", "-Wextra", "-I", str(ROOT / "src"), str(cpp), "-o", str(exe)],
capture_output=True, text=True)
self.assertEqual(built.returncode, 0, built.stderr)
ran = subprocess.run([str(exe)], capture_output=True, text=True)
@@ -27,6 +27,7 @@ class BatchedModulationTests(unittest.TestCase):
"int16_t setLoRaModulationParams(")
harness = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cmath>
#include <cstdint>
#include <cstddef>
@@ -51,6 +52,7 @@ using std::isfinite;
#define RADIOLIB_SX126X_LORA_BW_500_0 0x06
struct Radio {
SX1262ProfileSwitchState _profileSwitch{true};
uint8_t _profileSwitchFeatures=63;
uint8_t spreadingFactor=9, bandwidth=4, codingRate=3, ldrOptimize=0;
float bandwidthKhz=125;
bool ldroAuto=true;
@@ -181,6 +183,7 @@ int main() {
state = (ROOT / "src/helpers/radiolib/SX1262ProfileSwitchState.h").read_text().replace("#pragma once", "")
harness = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cstdint>
@STATE@
struct SX1262 {
@@ -193,6 +196,7 @@ struct SX1262 {
};
struct Radio: SX1262 {
SX1262ProfileSwitchState _profileSwitch{true};
uint8_t _profileSwitchFeatures=63;
@METHODS@
};
int main() {
@@ -218,13 +222,18 @@ int main() {
def test_wrapper_batches_once_and_preserves_failure_short_circuit(self):
source = (ROOT / "src/helpers/radiolib/CustomSX1262Wrapper.h").read_text()
production = method(source, "bool applyParams(").replace(" override", "")
production = "\n".join(method(source, signature).replace(" override", "")
for signature in ("bool applyProfileModulation(", "bool applyParams("))
harness = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cstdint>
#define RADIOLIB_ERR_NONE 0
struct CustomSX1262 {
unsigned freq=0,mod=0,limits=0;int failure=0;
unsigned freq=0,mod=0,limits=0,ordinary=0;int failure=0;
int setSpreadingFactor(uint8_t sf) { assert(sf==8);++ordinary;return failure==2?-1:0; }
int setBandwidth(float bw) { assert(bw==500);++ordinary;return 0; }
int setCodingRate(uint8_t cr) { assert(cr==5);++ordinary;return 0; }
bool profileSwitchFailed() const { return failure==4; }
int setFrequency(float f) { assert(f==910.5f);++freq;return failure==1?-1:0; }
int setLoRaModulationParams(float bw,uint8_t sf,uint8_t cr) {
@@ -236,6 +245,8 @@ struct CustomSX1262 {
struct PacketMillis { uint32_t preambleMillis,payloadMillis; };
struct Wrapper {
CustomSX1262 chip;CustomSX1262* _radio=&chip;unsigned preamble=0,calcs=0;
uint8_t features=mesh::RadioSwitchTuning::Batch;
uint8_t getDriverSwitchFeatures() const { return features; }
bool updatePreamble(uint8_t sf,float bw) {
assert(sf==8 && bw==500);++preamble;return chip.failure!=3;
}
@@ -256,6 +267,10 @@ int main() {
assert(w.chip.mod==(failure==1?0u:1u));
assert(w.preamble==((failure==1 || failure==2)?0u:1u));
assert(w.calcs==(failure?0u:1u) && w.chip.limits==(failure?0u:2u));
Wrapper slow;slow.features=0;slow.chip.failure=failure;
assert(slow.applyParams(910.5f,500,8,5)==(failure==0));
assert(slow.chip.mod==0 && slow.chip.ordinary==(failure==1?0u:failure==2?1u:3u));
assert(slow.preamble==((failure==1 || failure==2)?0u:1u));
}
}
'''
+13 -13
View File
@@ -15,17 +15,23 @@ class ModemCacheTests(unittest.TestCase):
def test_confirmed_modem_requires_owned_warm_rx_and_survives_no_failure(self):
source = (ROOT / "src/helpers/radiolib/CustomSX1262.h").read_text()
state = (ROOT / "src/helpers/radiolib/SX1262ProfileSwitchState.h").read_text().replace("#pragma once", "")
signatures = ("void invalidateMorseModemCache(", "void setProfileSwitchOptimization(",
signatures = ("uint8_t defaultProfileSwitchFeatures(", "uint8_t profileSwitchCapabilities(", "uint8_t getProfileSwitchFeatures(",
"void setProfileSwitchFeatures(", "void invalidateMorseModemCache(", "void setProfileSwitchOptimization(",
"int16_t begin(const ConfigLoRa_t&", "int16_t beginFSK(const ConfigFSK_t&",
"int16_t beginLRFHSS(const ConfigLRFHSS_t&", "int16_t setModem(",
"int16_t setLoRaModulationParams(")
production = "\n".join(method(source, signature).replace(" override", "") for signature in signatures)
fixture = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cmath>
#include <cstdint>
#include <cstddef>
@DEFINES@
#define MC_SX1262_FAST_PROFILE_SWITCH 1
#define MC_SX1262_FAST_FS 1
#define MC_SX1262_FAST_PACKET_CACHE 1
#define MC_SX1262_FAST_IQ_CACHE 1
@STATE@
using std::isfinite;
#define RADIOLIB_ERR_NONE 0
@@ -67,6 +73,7 @@ struct SX1262 {
};
struct Radio : SX1262 {
SX1262ProfileSwitchState _profileSwitch{true};
uint8_t _profileSwitchFeatures=defaultProfileSwitchFeatures();
bool _coldStandby = false, _tcxoWakePending = true;
uint8_t spreadingFactor = 9, bandwidth = 4, codingRate = 3, ldrOptimize = 0;
float bandwidthKhz = 125;
@@ -76,6 +83,7 @@ struct Radio : SX1262 {
int16_t writeResult = 0;
#ifdef MC_SX1262_HIL
bool hilSkipConfirmedModemQuery = false;
bool hilUseFsOnRetune=false,hilSkipUnchangedPacket=false,hilSkipUnchangedIq=false;
uint32_t hilModemQuerySkips = 0;
#endif
using SX1262::beginFSK;
@@ -91,19 +99,11 @@ struct Radio : SX1262 {
@PRODUCTION@
};
bool skipsModem(const Radio& r) {
#ifdef MC_SX1262_HIL
return r.hilSkipConfirmedModemQuery;
#else
(void)r;
return MC_SX1262_FAST_MODEM_CACHE != 0;
#endif
return (r.getProfileSwitchFeatures() & mesh::RadioSwitchTuning::Modem) != 0;
}
void selectCache(Radio& r, bool enabled) {
#ifdef MC_SX1262_HIL
r.hilSkipConfirmedModemQuery = enabled;
#else
(void)r; (void)enabled;
#endif
const auto flags=r.getProfileSwitchFeatures() & ~mesh::RadioSwitchTuning::Modem;
r.setProfileSwitchFeatures(flags | (enabled ? mesh::RadioSwitchTuning::Modem : 0));
}
void prime(Radio& r) {
// Models the full RX path's successful live LoRa confirmation. The ordinary
@@ -126,7 +126,7 @@ int main() {
assert(r.setLoRaModulationParams(500, 8, 5) == 0 && r.writes == 2);
assert(r.queries == (skipsModem(r) ? 1U : 3U));
#ifdef MC_SX1262_HIL
assert(r.hilModemQuerySkips == (requested ? 2U : 0U));
assert(r.hilModemQuerySkips == (skipsModem(r) ? 2U : 0U));
#endif
// Every ownership gate independently restores the live query.
for (int gate = 0; gate < 9; ++gate) {
+30 -2
View File
@@ -37,6 +37,7 @@ class FastProfileSwitchTests(unittest.TestCase):
state = (ROOT / "src/helpers/radiolib/SX1262ProfileSwitchState.h").read_text()
harness = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cstddef>
#include <cstdint>
@STATE@
@@ -66,7 +67,8 @@ int main() {
def test_owned_rx_window_commands_errors_and_lifecycle_invalidation(self):
src = (ROOT / "src/helpers/radiolib/CustomSX1262.h").read_text()
state = (ROOT / "src/helpers/radiolib/SX1262ProfileSwitchState.h").read_text().replace("#pragma once", "")
signatures = ["void setProfileSwitchOptimization(", "void beginProfileSwitch(",
signatures = ["uint8_t defaultProfileSwitchFeatures(", "uint8_t profileSwitchCapabilities(", "uint8_t getProfileSwitchFeatures(",
"void setProfileSwitchFeatures(", "void setProfileSwitchOptimization(", "void beginProfileSwitch(",
"void endProfileSwitch(", "bool profileSwitchFailed(", "int16_t restoreTcxoAfterSleep(",
"int16_t standby()", "int16_t standby(uint8_t", "int16_t sleep()",
"int16_t sleep(bool", "int16_t reset(bool", "int16_t stageMode(",
@@ -77,10 +79,14 @@ int main() {
methods = "\n".join(method(src, signature).replace(" override", "") for signature in signatures)
harness = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cstdint>
#include <cstddef>
#include <string>
@STATE@
#define MC_SX1262_FAST_PROFILE_SWITCH 1
#define MC_SX1262_FAST_IQ_CACHE 0
#define MC_SX1262_FAST_MODEM_CACHE 0
#define MC_SX1262_FAST_FS @FS@
#define MC_SX1262_FAST_PACKET_CACHE @PACKET@
#define RADIOLIB_ERR_NONE 0
@@ -142,7 +148,8 @@ struct SX1262 {
};
struct Radio: SX1262 {
uint32_t _activityAt=0;bool _headerSeen=false;
SX1262ProfileSwitchState _profileSwitch{true};bool _rx_ps_rf_rx_disabled=false;uint32_t _profileFastResumes=0;
SX1262ProfileSwitchState _profileSwitch{true};
uint8_t _profileSwitchFeatures=defaultProfileSwitchFeatures();bool _rx_ps_rf_rx_disabled=false;uint32_t _profileFastResumes=0;
bool _coldStandby=true;
bool _tcxoWakePending=false;
@METHODS@
@@ -232,6 +239,27 @@ int main() {
fault.errors=32;fault.trace.clear();assert(fault.standby()==0 && fault.trace=="UEV");
assert(!fault._tcxoWakePending);
Radio wrong;wrong.modem=0;assert(wrong.startReceive()==0 && !wrong._profileSwitch.rxValid);
// Every supported feature is independently selectable. Unsupported request
// bits are masked before ownership changes; setters issue no radio command.
for(unsigned requested=0;requested<256;++requested) {
Radio x;x.setProfileSwitchFeatures(uint8_t(requested));
const uint8_t features=requested & x.profileSwitchCapabilities();
assert(x.getProfileSwitchFeatures()==features && x.trace.empty());
assert(x._profileSwitch.enabled==bool(features & mesh::RadioSwitchTuning::Warm));
assert(!x._profileSwitch.rxValid && !x._profileSwitch.packetValid
&& !x._profileSwitch.modulationValid);
assert(x.startReceive()==0);x.trace.clear();x.beginProfileSwitch(true);
assert(x.standby()==0);
const bool warm=features & mesh::RadioSwitchTuning::Warm;
const bool fs=features & mesh::RadioSwitchTuning::Fs;
assert(x.trace==(warm ? (fs ? "If":"s") : "S"));x.trace.clear();
assert(x.startReceive()==0);
const bool packet=features & mesh::RadioSwitchTuning::Packet;
assert(x.trace==(warm ? (packet ? "CWR":"CPWR") : "GSF"));
x.endProfileSwitch(true);x.trace.clear();
x.setProfileSwitchFeatures(features);
assert(x.trace.empty() && !x._profileSwitch.rxValid && !x._profileSwitch.packetValid);
}
Radio off;off.setProfileSwitchOptimization(false);assert(off.startReceive()==0);
assert(off.trace=="GUF" && !off._profileSwitch.rxValid);
}
+3
View File
@@ -19,6 +19,7 @@ class ProtocolBatchingTests(unittest.TestCase):
"int16_t setProfileProtocol(", "int16_t startReceive()"))
harness = r'''
#include <cassert>
#include <helpers/RadioSwitchTuning.h>
#include <cstdint>
#include <cstddef>
#include <string>
@@ -86,6 +87,8 @@ struct SX1262 {
struct Radio: SX1262 {
uint32_t _activityAt=0;bool _headerSeen=false;
SX1262ProfileSwitchState _profileSwitch{true};
uint8_t _profileSwitchFeatures=63;
uint8_t getProfileSwitchFeatures() const { return _profileSwitchFeatures; }
bool _coldStandby=false;uint32_t _profileFastResumes=0;
@METHODS@
};
+64 -3
View File
@@ -41,7 +41,8 @@ struct SPIClass {
#endif
uint8_t transfer(uint8_t value) { ++bytes; return value^0xa5; }
};
struct ArduinoHal {
struct RadioLibHal { virtual ~RadioLibHal()=default; };
struct ArduinoHal : RadioLibHal {
SPIClass* spi; SPISettings spiSettings; bool initInterface=false;
explicit ArduinoHal(SPIClass& bus,SPISettings settings)
: spi(&bus),spiSettings(settings) {}
@@ -92,6 +93,63 @@ int main() {
hal.spiTransfer(nullptr,0,nullptr);
assert(explicitBus.bursts==before && explicitBus.bytes==0);
assert(unused.bursts==0 && fallbackBus.bursts==0);
using Tuning=mesh::RadioSwitchTuning;
assert(TunableRadioHal::find(&hal)==&external);
assert(external.defaultHz()==MC_SX1262_SPI_HZ);
assert(external.effectiveHz()==MC_SX1262_SPI_HZ);
assert(external.getTuning().spi_mhz==0);
assert(external.getTuning().flags==Tuning::Buffer);
assert(external.capabilities().flags==Tuning::Buffer);
assert(external.capabilities().spi_clocks==(2|4|8));
// Disable bulk transfers without changing the controller, bus lifetime or
// packet response. Driver-owned feature bits are handled by the wrapper.
Tuning tuning;
tuning.flags=Tuning::Warm;
tuning.spi_mhz=2;
assert(external.setTuning(tuning));
assert(external.getTuning().flags==0 && external.effectiveHz()==2000000);
assert(external.spiSettings.hz==2000000 && hal.spi==&explicitBus);
const unsigned bytes=explicitBus.bytes;
rx.fill(0x77);
hal.spiTransfer(tx.data(),7,rx.data());
assert(explicitBus.bursts==before && explicitBus.bytes==bytes+7);
for (unsigned i=0;i<7;++i) assert(rx[i]==uint8_t(tx[i]^0xa5));
for (unsigned i=7;i<rx.size();++i) assert(rx[i]==0x77);
for (uint8_t clock : {uint8_t(4),uint8_t(8)}) {
tuning.spi_mhz=clock;
assert(external.setTuning(tuning));
assert(external.effectiveHz()==unsigned(clock)*1000000);
assert(external.spiSettings.hz==external.effectiveHz());
}
const Tuning previous=external.getTuning();
for (uint8_t clock : {uint8_t(1),uint8_t(3),uint8_t(16),uint8_t(255)}) {
tuning.spi_mhz=clock;
assert(!external.setTuning(tuning) && external.getTuning()==previous);
}
tuning.spi_mhz=0;
tuning.flags=Tuning::Buffer;
assert(external.setTuning(tuning));
assert(external.effectiveHz()==MC_SX1262_SPI_HZ);
assert(external.spiSettings.hz==MC_SX1262_SPI_HZ);
hal.spiTransfer(tx.data(),7,rx.data());
assert(explicitBus.bursts==before+1 && explicitBus.bytes==bytes+7);
assert(explicitBus.begins==1 && explicitBus.ends==1);
// The plain nRF SPIM3 byte route exposes clock selection but never admits
// the buffered path that needs a separately qualified controller mapping.
{
TunableRadioHal byteHal(unused);
assert(TunableRadioHal::find(&byteHal)==&byteHal);
assert(byteHal.capabilities().flags==0 && byteHal.defaultHz()==8000000);
assert(!byteHal.setTuning(tuning));
tuning.flags=0;tuning.spi_mhz=2;
assert(byteHal.setTuning(tuning) && byteHal.spiSettings.hz==2000000);
}
ArduinoHal foreign(unused,SPISettings(2000000,MSBFIRST,SPI_MODE0));
assert(TunableRadioHal::find(&foreign)==nullptr);
assert(TunableRadioHal::find(nullptr)==nullptr);
assert(TunableRadioHal::find(&external)==&external);
}
'''
@@ -101,12 +159,15 @@ class SX126xBufferedRadioHalTests(unittest.TestCase):
compiler=shutil.which(os.environ.get("CXX","g++"))
self.assertIsNotNone(compiler)
headers={name:(RADIO/name).read_text(encoding="ascii") for name in (
"SX126xBufferedRadioHal.h","Nrf52BufferedRadioHal.h","ESP32BufferedRadioHal.h")}
"SX126xBufferedRadioHal.h","Nrf52BufferedRadioHal.h","ESP32BufferedRadioHal.h",
"TunableRadioHal.h")}
with tempfile.TemporaryDirectory(prefix="sx126x-buffered-radio-") as folder:
base=Path(folder)
(base/"hal/Arduino").mkdir(parents=True)
(base/"helpers/radiolib").mkdir(parents=True)
(base/"hal/Arduino/ArduinoHal.h").write_text(MOCK,encoding="ascii")
(base/"helpers/RadioSwitchTuning.h").write_text(
(ROOT/"src/helpers/RadioSwitchTuning.h").read_text(),encoding="ascii")
cpp,executable=base/"test.cpp",base/"test"
cpp.write_text(FIXTURE,encoding="ascii")
for arch,header in (("NRF52_PLATFORM","Nrf52BufferedRadioHal.h"),
@@ -125,7 +186,7 @@ class SX126xBufferedRadioHalTests(unittest.TestCase):
implementation if file==header else content,encoding="ascii")
built=subprocess.run([compiler,"-std=c++17","-Wall","-Wextra",
"-Werror",f"-D{arch}",f"-DMC_SX1262_SPI_HZ={clock}",
"-I",str(base),str(cpp),"-o",str(executable)],
"-I",str(base),"-I",str(ROOT/"src"),"-I",str(RADIO),str(cpp),"-o",str(executable)],
capture_output=True,text=True,timeout=60)
self.assertEqual(built.returncode,0,built.stdout+built.stderr)
ran=subprocess.run([str(executable)],capture_output=True,
+19 -12
View File
@@ -1,4 +1,4 @@
"""Compile actual conservative nRF52 and buffered RP2040 radio constructors."""
"""Compile actual tunable nRF52 and buffered RP2040 radio constructors."""
from pathlib import Path
import os
import re
@@ -118,7 +118,7 @@ void check() {{
fixture+="\nint main() {"+"".join(f"{name}::check();" for name,_ in cases)+"}\n"
self.compile_run(arch,fixture)
def test_all_new_nrf52_spim3_defaults_keep_prior_2mhz_byte_path(self):
def test_all_nrf52_spim3_defaults_use_tunable_8mhz_byte_path(self):
cases=[]
sources=target_sources({"nrf52_base","rak4631_hw"})
self.assertEqual(len(sources),32)
@@ -134,12 +134,15 @@ void check() {{
self.assertIn("#if MC_NRF52_HIL_SPIM1_RADIO",text)
else:
self.assertNotIn("BufferedRadioHal",text,str(source))
constructions=re.findall(r"^RADIO_CLASS radio = new Module\(P_LORA_NSS[^;]+;",text,re.MULTILINE)
constructions=re.findall(r"^static TunableRadioHal radio_hal\(([^;]+)\);\n"
r"(RADIO_CLASS radio = new Module\(&radio_hal,[^;]+;)",
text,re.MULTILINE)
fallback=source.parent.name in {"heltec_t096","heltec_t1","heltec_t114"}
self.assertEqual(len(constructions),2 if fallback else 1,str(source))
for ordinal,construction in enumerate(constructions):
for ordinal,(arguments,module) in enumerate(constructions):
owns=fallback and ordinal==1
self.assertEqual(construction.endswith(", SPI);"),not owns,str(source))
self.assertEqual(arguments, "SPI, 8000000, true" if owns else "SPI",str(source))
construction=f"static TunableRadioHal radio_hal({arguments});\n{module}"
name=f"case_{len(cases)}"
cases.append((name,f'''
namespace {name} {{
@@ -147,8 +150,9 @@ SPIClass SPI;
struct BindDefaultBus {{ BindDefaultBus() {{testDefaultBus=&SPI;}} }} bindDefaultBus;
{construction}
void check() {{
assert(radio.mod->owned && radio.mod->hal->spi==&SPI);
assert(radio.mod->hal->spiSettings.hz==2000000);
assert(!radio.mod->owned && radio.mod->hal==&radio_hal && radio.mod->hal->spi==&SPI);
assert(radio.mod->hal->spiSettings.hz==8000000);
assert(radio_hal.capabilities().flags==0 && radio_hal.capabilities().spi_clocks==(2|4|8));
assert(radio.mod->hal->initInterface=={str(owns).lower()});
radio.mod->hal->init();radio.mod->hal->term();
assert(SPI.begins=={int(owns)} && SPI.ends=={int(owns)});
@@ -163,15 +167,17 @@ void check() {{
fixture+="\nint main() {"+"".join(f"{name}::check();" for name,_ in cases)+"}\n"
self.compile_run("NRF52_PLATFORM",fixture)
def test_muzi_uno_and_lr1121_duo_keep_prior_constructor(self):
def test_muzi_uno_and_lr1121_duo_keep_bus_and_gain_tunable_byte_clock(self):
text=(ROOT/"variants/muzi_base/target.cpp").read_text()
constructor=re.search(r"^RADIO_CLASS radio = new Module\(P_LORA_NSS[^;]+;",text,re.MULTILINE)
constructor=re.search(r"^static TunableRadioHal radio_hal\([^;]+;\n"
r"RADIO_CLASS radio = new Module\(&radio_hal,[^;]+;",text,re.MULTILINE)
self.assertIsNotNone(constructor)
self.assertNotIn("BufferedRadioHal",text)
fixture=PREFIX+"\nSPIClass SPI;\n"+constructor.group(0)+r'''
int main() {
assert(radio.mod->hal->spi==&SPI);
assert(radio.mod->owned && radio.mod->hal->spiSettings.hz==2000000);
assert(!radio.mod->owned && radio.mod->hal->spiSettings.hz==8000000);
assert(TunableRadioHal::find(radio.mod->hal)==&radio_hal);
}
'''
for definitions in ((),("USE_SX1262",)):
@@ -180,7 +186,8 @@ int main() {
def test_lr1110_m4_keeps_manual_spi_start_and_prior_2mhz_byte_path(self):
text=(ROOT/"variants/thinknode_m4/target.cpp").read_text()
construction=re.search(r"^RADIO_CLASS radio = new Module\(P_LORA_NSS[^;]+;",text,re.MULTILINE)
construction=re.search(r"^static TunableRadioHal radio_hal\([^;]+;\n"
r"RADIO_CLASS radio = new Module\(&radio_hal,[^;]+;",text,re.MULTILINE)
self.assertIsNotNone(construction)
self.assertNotIn("BufferedRadioHal",text)
self.assertIn("SPI.setPins(P_LORA_MISO, P_LORA_SCLK, P_LORA_MOSI);",text)
@@ -188,7 +195,7 @@ int main() {
self.assertIn("int status = radio.begin(",text)
fixture=PREFIX+"\nSPIClass SPI;\n"+construction.group(0)+r'''
int main() {
assert(radio.mod->owned && radio.mod->hal->spi==&SPI);
assert(!radio.mod->owned && radio.mod->hal==&radio_hal && radio.mod->hal->spi==&SPI);
assert(radio.mod->hal->spiSettings.hz==2000000 && !radio.mod->hal->initInterface);
radio.mod->hal->init();radio.mod->hal->term();
assert(SPI.begins==0 && SPI.ends==0);
+3 -2
View File
@@ -73,6 +73,7 @@ class XiaoNrf52BufferedSpiTests(unittest.TestCase):
#include <cstdint>
#include <hal/Arduino/ArduinoHal.h>
#include "XiaoRadioSpiConfig.h"
#include <helpers/radiolib/TunableRadioHal.h>
#define SPI_INTERFACES_COUNT 2
#define PIN_SPI_MISO 9
#define PIN_SPI_MOSI 10
@@ -118,7 +119,7 @@ int main() {
assert(radio.module->hal->spi==&SPI);
assert(SPI.device->number==(MC_XIAO_NRF52_HIL_FAST_SPI?2:3));
assert(SPI1.device->number==(MC_XIAO_NRF52_HIL_FAST_SPI?3:2));
assert(radio.module->hal->spiSettings.hz==(MC_XIAO_NRF52_HIL_FAST_SPI?8000000:2000000));
assert(radio.module->hal->spiSettings.hz==8000000);
assert(!radio.module->hal->initInterface && SPI.begins==0 && SPI1.begins==0);
radio.module->hal->init();radio.module->hal->term();
assert(SPI.begins==0 && SPI.ends==0); // The board still owns bus lifetime.
@@ -187,7 +188,7 @@ int main() {
self.assertIn(diagnostic, result.stderr)
def test_default_candidate_has_no_code_or_sdk_dependency_and_recipes_do_not_enable_it(self):
result = subprocess.run([os.environ.get("CXX", "g++"), "-E", "-P", "-x", "c++", str(CONFIG)],
result = subprocess.run([os.environ.get("CXX", "g++"), "-E", "-P", "-x", "c++", "-I", str(ROOT / "src"), str(CONFIG)],
capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertFalse(result.stdout.strip())