Integrate W12 RF switching and LR2021 receive PRs

Adapt upstream MeshCore #3579 at 7457cff43c22697119e06ad7d192a37de0990403:
retain GC1109 LF standby bias, with checked cold controls before band supply
changes and shutdown. Preserve exclusive LF/HF rails and TX error cleanup.
Reset before boot rail changes and before SPI-dependent hard recovery.

Retain our stronger implementation of #3512 at
d208bfef94ab83280de30ba4a0675ff73b3ecef3. Add regressions for suspect FIFO
contents, failed RX_DONE acknowledgement, and fresh preamble timing.
W12 TX power defaults from #3319 were already included via 65aa1138a.

Validation: 79 targeted native tests; canonical W12 Full repeater and Full
Companion builds, capability checks, app-size and RAM checks passed.
Physical W12 RF envelope, current and LoRa management confirmation remain.

Upstream PRs:
https://github.com/meshcore-dev/MeshCore/pull/3579
https://github.com/meshcore-dev/MeshCore/pull/3512
https://github.com/meshcore-dev/MeshCore/pull/3319
This commit is contained in:
mikecarper committed 2026-10-25 15:46:18 -07:00
1 parent 73c5f572eb
commit ad34fd3a0d
15 files changed
+514 -137

No files matched your search

+1
View File
@@ -102,6 +102,7 @@ struct Native {
int16_t startReceive() { const auto result=call(7); receiving=result==0; return result; }
int16_t setRSSIConfig(uint8_t samples) { rssi_samples=samples; return call(6); }
int16_t receiveDirect() { const auto result=call(7); receiving=result==0; return result; }
int16_t standby() { return 0; }
int16_t setDioFunction(uint8_t dio,uint8_t function,uint8_t pull) {
assert(dio>=5 && function==6 && (pull==0 || pull==1)); return call(4);
}
+1 -1
View File
@@ -141,7 +141,7 @@ struct RadioLibWrapper : mesh::Radio {
uint32_t rssi_latency=0;
unsigned rssi_reads=0;
bool _params_valid=true, _rx_ps_armed=true, _rx_hold_continuous=false;
bool _rx_boosted_gain_valid=false, _cur_rx_boosted_gain=false;
bool _rx_boosted_gain_valid=false, _cur_rx_boosted_gain=false, _rx_boosted_gain_applied=false;
bool rx_gain_apply_ok=true, physical_rx_gain=false;
unsigned rx_gain_applies=0,rx_gain_order=0;
virtual bool applyRxBoostedGainMode(bool enabled) {
+179 -87
View File
@@ -25,6 +25,7 @@ inline unsigned held=0,released=0,deinitialized=0,digital_held=0;
inline int api_result=0;
struct Pad { int latch=0; bool output=false, hold=false; int retained=-1; };
inline Pad pads[64];
inline void (*pad_changed)(int)=nullptr;
inline bool deep_hold=false;
inline bool require_configured_release=false;
inline unsigned deep_hold_en_calls=0, deep_hold_dis_calls=0;
@@ -37,12 +38,12 @@ inline bool valid_output(int pin) {
}
#define GPIO_IS_VALID_OUTPUT_GPIO(pin) valid_output(pin)
inline int physical(int pin) { return pads[pin].hold ? pads[pin].retained : (pads[pin].output ? pads[pin].latch : -1); }
inline int gpio_set_level(int pin,unsigned level) { assert(valid_output(pin)); pads[pin].latch=level; return api_result; }
inline int gpio_set_level(int pin,unsigned level) { assert(valid_output(pin)); pads[pin].latch=level; if(pad_changed) pad_changed(pin); return api_result; }
inline int gpio_config(const gpio_config_t* config) {
assert(config->mode==GPIO_MODE_OUTPUT && config->pull_up_en==0 && config->pull_down_en==0 && config->intr_type==0);
if (api_result) return api_result;
for(int pin=0;pin<64;++pin) if(config->pin_bit_mask&(1ULL<<pin)) {
assert(valid_output(pin)); pads[pin].output=true;
assert(valid_output(pin)); pads[pin].output=true; if(pad_changed) pad_changed(pin);
}
return ESP_OK;
}
@@ -56,7 +57,7 @@ inline bool rtc_gpio_is_valid_gpio(int pin) {
#endif
}
inline int rtc_gpio_hold_en(int pin) { assert(pin>=0 && rtc_gpio_is_valid_gpio(pin)); ++held; if(!api_result) { pads[pin].retained=physical(pin); pads[pin].hold=true; } return api_result; }
inline int rtc_gpio_hold_dis(int pin) { assert(pin>=0 && rtc_gpio_is_valid_gpio(pin)); assert(!require_configured_release || !pads[pin].hold || pads[pin].output); ++released; if(!api_result) pads[pin].hold=false; return api_result; }
inline int rtc_gpio_hold_dis(int pin) { assert(pin>=0 && rtc_gpio_is_valid_gpio(pin)); assert(!require_configured_release || !pads[pin].hold || pads[pin].output); ++released; if(!api_result) pads[pin].hold=false; if(pad_changed) pad_changed(pin); return api_result; }
inline int rtc_gpio_deinit(int pin) { assert(pin>=0 && rtc_gpio_is_valid_gpio(pin)); ++deinitialized; return api_result; }
#endif
inline int gpio_hold_en(int pin) {
@@ -75,6 +76,7 @@ inline int gpio_hold_dis(int pin) {
if(rtc_gpio_is_valid_gpio(pin)) return rtc_gpio_hold_dis(pin);
#endif
if(!api_result) pads[pin].hold=false;
if(pad_changed) pad_changed(pin);
return api_result;
}
#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
@@ -423,47 +425,15 @@ int main(){ EnvironmentSensorManager sensors;
):
self.compile_run(code,defines=definitions)
def test_w12_fem_rails_restore_known_output_before_releasing_terminal_hold(self):
def w12_board_harness(self, case):
source=(ROOT/'variants/meshnology_w12/MeshnologyW12Board.cpp').read_text()
begin=body(source,'void MeshnologyW12Board::begin()')
prefix=begin[:begin.index(' prepareRadioFrequency(')]+'}\n'
code=r'''
#include <helpers/ESP32GpioHold.h>
#include <initializer_list>
#define HIGH 1
#define LOW 0
#define OUTPUT 1
#define P_LORA_LF_PA_POWER 4
#define P_LORA_HF_PA_POWER 3
#define PIN_ADC_CTRL 2
void pinMode(int pin,int mode){assert(mode==OUTPUT);pads[pin].output=true;}
void digitalWrite(int pin,int level){gpio_set_level(pin,level);}
class ESP32Board {public:void begin(){mesh::esp32::resetTerminalDeepSleepHolds();}};
class MeshnologyW12Board:public ESP32Board {public:void begin();};
@BEGIN@
int main(){ using namespace mesh::esp32;
for(int active : {-1,3,4}) {
// Wake safely from terminal shutdown or either band's packet-wake sleep.
holdOutputForDeepSleep(4,active==4?HIGH:LOW);
holdOutputForDeepSleep(3,active==3?HIGH:LOW);
pads[4].output=false;pads[3].output=false;require_configured_release=true;
MeshnologyW12Board board;board.begin();
assert(!pads[3].hold && !pads[4].hold && physical(3)==LOW && physical(4)==LOW);
assert(!deep_hold && !terminalDigitalHoldPending());
}
}
'''
self.compile_run(code.replace('@BEGIN@',prefix),s3=1)
# The prior release-before-pinMode sequence must be caught.
broken=prefix.replace('releaseOutputAfterDeepSleep((gpio_num_t)P_LORA_LF_PA_POWER, LOW)',
'releaseRtcGpioIfSupported((gpio_num_t)P_LORA_LF_PA_POWER)')
self.compile_run(code.replace('@BEGIN@',broken),s3=1,helper_mutation=lambda text:text)
def test_w12_packet_wake_retains_only_selected_band_and_terminal_shutdown_disables_both(self):
source=(ROOT/'variants/meshnology_w12/MeshnologyW12Board.cpp').read_text()
methods='\n'.join(body(source,signature) for signature in (
begin=begin[:begin.index(' periph_power.begin();')]+'}\n'
methods=begin+'\n'+'\n'.join(body(source,signature) for signature in (
'bool MeshnologyW12Board::prepareRadioFrequency(float frequency)',
'bool MeshnologyW12Board::resetRadio()',
'void MeshnologyW12Board::enterDeepSleep(uint32_t secs, int pin_wake_btn)',
'void MeshnologyW12Board::shutdownPeripherals()',
'void MeshnologyW12Board::powerOff()'))
code=r'''
#include <helpers/ESP32GpioHold.h>
@@ -471,86 +441,208 @@ int main(){ using namespace mesh::esp32;
#include <vector>
#define HIGH 1
#define LOW 0
#define OUTPUT 1
#define PIN_ADC_CTRL 2
#define P_LORA_LF_PA_POWER 4
#define P_LORA_HF_PA_POWER 3
#define P_LORA_DIO_1 14
#define P_LORA_NSS 8
#define P_LORA_RESET 12
#define P_LORA_BUSY 13
#define LORA_FREQ 909.5f
constexpr int ESP_PD_DOMAIN_RTC_PERIPH=1,ESP_PD_OPTION_ON=1;
constexpr int RTC_GPIO_MODE_INPUT_ONLY=0,ESP_EXT1_WAKEUP_ANY_HIGH=1;
static bool slept=false,shutdown=false;
static unsigned settles=0;
static bool slept=false,shutdown=false,controls_high=false,spi_working=true;
static bool busy_stuck=false,base_restore_lf=false;
static int fail_callback_at=0,callback_calls=0,sleep_callbacks=0,reset_pulses=0;
static uint32_t clock_ms=0,reset_low_at=0;
static uint64_t wake_mask=0,timer=0;
static std::vector<int> writes;
void digitalWrite(int pin,int level){
assert(!pads[pin].hold);pads[pin].output=true;gpio_set_level(pin,level);
writes.push_back(pin*2+level);
// Never power both FEMs, even transiently, while changing bands.
assert(physical(3)!=HIGH || physical(4)!=HIGH);
static std::vector<unsigned> delays;
void observe_pad(int pin){
if(pin==12 && physical(12)==LOW) reset_low_at=clock_ms;
if(pin==3 || pin==4){
// Supplies can be removed only after checked cold controls or settled NRST.
assert(physical(4)!=LOW || !controls_high);
assert(physical(3)!=HIGH || physical(4)!=HIGH);
writes.push_back(pin*2+(physical(pin)==HIGH));
}
}
void delay(unsigned ms){assert(ms==1);++settles;}
void pinMode(int pin,int mode){assert(mode==OUTPUT);pads[pin].output=true;}
void digitalWrite(int pin,int level){
assert(!pads[pin].hold);gpio_set_level(pin,level);
if(pin==12 && level==HIGH && clock_ms-reset_low_at>=1){++reset_pulses;spi_working=true;}
}
void delay(unsigned ms){
clock_ms+=ms;delays.push_back(ms);
if(physical(12)==LOW && clock_ms-reset_low_at>=1) controls_high=false;
}
uint32_t millis(){return clock_ms;}
void yield(){++clock_ms;}
int digitalRead(int pin){assert(pin==13);return busy_stuck;}
void esp_sleep_pd_config(int domain,int option){assert(domain==1 && option==1);}
void rtc_gpio_set_direction(int pin,int mode){assert(pin==14 && mode==0);}
void rtc_gpio_pulldown_en(int pin){assert(pin==14);}
void esp_sleep_enable_ext1_wakeup(uint64_t mask,int mode){assert(mode==1);wake_mask=mask;}
void esp_sleep_enable_timer_wakeup(uint64_t value){timer=value;}
void sleep_pads(){
// Unretained outputs may float once the CPU domain powers down.
for(int pin : {3,4}) if(!pads[pin].hold) pads[pin].output=false;
}
void sleep_pads(){for(int pin:{3,4}) if(!pads[pin].hold) pads[pin].output=false;}
void esp_deep_sleep_start(){sleep_pads();slept=true;}
class ESP32Board {public:void powerOff(){
assert(pads[3].hold && pads[4].hold && physical(3)==LOW && physical(4)==LOW);
sleep_pads();shutdown=true;
}};
class ESP32Board {public:
virtual bool prepareRadioFrequency(float){return false;}
void begin(){mesh::esp32::resetTerminalDeepSleepHolds();}
virtual void shutdownPeripherals(){
assert(physical(3)==HIGH || physical(4)==HIGH);
// Stopping CW can restore the LF packet profile and its warm standby table.
if(base_restore_lf){assert(prepareRadioFrequency(909.5f));controls_high=true;}
mesh::esp32::holdOutputForDeepSleep(P_LORA_NSS,HIGH);
}
void powerOff(){shutdownPeripherals();
assert(pads[3].hold && pads[4].hold && physical(3)==LOW && physical(4)==LOW);
assert(pads[8].hold && physical(8)==HIGH);shutdown=true;sleep_pads();
}
};
class MeshnologyW12Board:public ESP32Board {
public:
bool radio_band_initialized=false,radio_high_band=false;
bool prepareRadioFrequency(float frequency);
bool (*radio_rf_switch_handler)(bool,bool)=nullptr;
void begin();
bool prepareRadioFrequency(float frequency) override;
bool resetRadio();
void enterDeepSleep(uint32_t secs,int pin_wake_btn);
void shutdownPeripherals() override;
void powerOff();
};
@METHODS@
bool rf_switch(bool lf_awake,bool sleep_after){
++callback_calls;
// Checked SPI cannot execute with a retained HIGH chip select.
assert(!pads[8].hold && physical(8)==HIGH);
if(!spi_working || callback_calls==fail_callback_at) return false;
if(lf_awake){assert(physical(4)==HIGH && physical(3)==LOW);assert(!delays.empty() && delays.back()==1);}
controls_high=lf_awake;
if(sleep_after){assert(!lf_awake);++sleep_callbacks;}
return true;
}
void fresh(){
pad_changed=nullptr;
for(auto& pad:pads) pad=Pad{};
clock_ms=reset_low_at=0;controls_high=false;spi_working=true;busy_stuck=false;
callback_calls=sleep_callbacks=reset_pulses=fail_callback_at=0;
base_restore_lf=slept=shutdown=false;writes.clear();delays.clear();
mesh::esp32::releaseOutputAfterDeepSleep(12,HIGH);
mesh::esp32::releaseOutputAfterDeepSleep(8,HIGH);
mesh::esp32::releaseOutputAfterDeepSleep(3,LOW);
mesh::esp32::releaseOutputAfterDeepSleep(4,LOW);
pad_changed=observe_pad;
}
int main(){using namespace mesh::esp32;
for(bool high_band : {false,true}) {
resetTerminalDeepSleepHolds();
releaseOutputAfterDeepSleep(3,LOW);releaseOutputAfterDeepSleep(4,LOW);
MeshnologyW12Board board;
@CASE@
}
'''
return code.replace('@METHODS@',methods).replace('@CASE@',case)
def test_w12_boot_quiesces_retained_warm_radio_before_releasing_fem_rails(self):
case=r'''
for(int active:{-1,3,4}){
fresh();pad_changed=nullptr;
holdOutputForDeepSleep(4,active==4?HIGH:LOW);
holdOutputForDeepSleep(3,active==3?HIGH:LOW);
if(active<0) holdOutputForDeepSleep(12,LOW);
// MCU-only restart preserves radio DIOs, including the LF warm standby.
controls_high=active==4;
pads[4].output=false;pads[3].output=false;pads[12].output=false;
require_configured_release=true;pad_changed=observe_pad;
MeshnologyW12Board board;board.begin();
assert(!pads[3].hold && !pads[4].hold && !pads[12].hold);
assert(physical(3)==LOW && physical(4)==HIGH && physical(12)==HIGH);
assert(!controls_high && reset_pulses==1 && board.radio_band_initialized);
assert(!deep_hold && !terminalDigitalHoldPending());
}
'''
code=self.w12_board_harness(case)
self.compile_run(code,s3=1)
# Releasing retained rails before quiescing a warm radio must fail.
broken=code.replace('releaseOutputAfterDeepSleep((gpio_num_t)P_LORA_RESET, LOW)',
'releaseOutputAfterDeepSleep((gpio_num_t)P_LORA_RESET, HIGH)',1)
self.compile_run(broken,s3=1,helper_mutation=lambda text:text)
def test_w12_band_switch_failures_retry_and_physical_reset_recover_spi(self):
case=r'''
fresh();MeshnologyW12Board board;board.begin();board.radio_rf_switch_handler=rf_switch;
board.radio_band_initialized=false;assert(board.prepareRadioFrequency(909.5f));
assert(controls_high);const auto original_writes=writes.size();
// A failed cold callback must not drop the powered LF rail.
fail_callback_at=callback_calls+1;
assert(!board.prepareRadioFrequency(2440.0f));
assert(!board.radio_band_initialized && writes.size()==original_writes && physical(4)==HIGH);
fail_callback_at=0;
// Even returning to the cached old band repeats a previously failed transition.
int callbacks_before=callback_calls;
assert(board.prepareRadioFrequency(909.5f) && callback_calls==callbacks_before+2);
assert(board.prepareRadioFrequency(2440.0f) && !controls_high && physical(3)==HIGH);
// Failure after powering LF also invalidates the band cache for a same-band retry.
fail_callback_at=callback_calls+2;
assert(!board.prepareRadioFrequency(909.5f));
assert(!board.radio_band_initialized && physical(4)==HIGH);
fail_callback_at=0;callbacks_before=callback_calls;
assert(board.prepareRadioFrequency(909.5f) && callback_calls==callbacks_before+2 && controls_high);
// A physical reset must work without SPI; only then may frequency prep use SPI.
spi_working=false;const auto before_reset=writes.size();
assert(board.resetRadio() && spi_working && !board.radio_band_initialized);
assert(writes.size()==before_reset && physical(4)==HIGH && !controls_high);
assert(board.prepareRadioFrequency(909.5f) && controls_high);
busy_stuck=true;const uint32_t started=clock_ms;
assert(!board.resetRadio() && clock_ms-started>=3310 && clock_ms-started<3400);
assert(!board.radio_band_initialized && writes.size()>before_reset);
'''
self.compile_run(self.w12_board_harness(case),s3=1)
def test_w12_packet_wake_retains_selected_rail_and_shutdown_quiesces_after_cw(self):
case=r'''
for(bool high_band:{false,true}){
fresh();MeshnologyW12Board board;board.begin();board.radio_rf_switch_handler=rf_switch;
const float frequency=high_band?2440.0f:909.5f;
const unsigned before=settles;
assert(board.prepareRadioFrequency(frequency) && settles==before+1);
assert(writes[writes.size()-3]==4*2 && writes[writes.size()-2]==3*2);
assert(physical(3)==high_band && physical(4)==!high_band);
assert(board.prepareRadioFrequency(frequency) && settles==before+1);
// A real LF/HF crossing disables the previous rail before enabling the next.
assert(board.prepareRadioFrequency(high_band?909.5f:2440.0f));
assert(physical(3)==!high_band && physical(4)==high_band);
assert(board.prepareRadioFrequency(frequency) && settles==before+3);
for(int button : {-1,0}) {
board.radio_band_initialized=false;assert(board.prepareRadioFrequency(frequency));
for(int button:{-1,0}){
slept=false;timer=0;
board.enterDeepSleep(button<0?5:0,button);
assert(slept && pads[3].hold && pads[4].hold);
assert(physical(3)==high_band && physical(4)==!high_band);
assert(wake_mask==((1ULL<<14)|(button<0?0:(1ULL<<button))));
assert(timer==(button<0?5000000:0));
assert(!deep_hold && !terminalDigitalHoldPending());
// Deep sleep never returns: model begin() releasing the retained rails,
// then its normal band selection, before the next runtime operation.
releaseOutputAfterDeepSleep(3,LOW);releaseOutputAfterDeepSleep(4,LOW);
board.radio_band_initialized=false;
assert(board.prepareRadioFrequency(frequency));
// Model a new MCU instance: radio retains state, the callback is not ready.
board.radio_rf_switch_handler=nullptr;board.radio_band_initialized=false;
board.begin();releaseOutputAfterDeepSleep(8,HIGH);
board.radio_rf_switch_handler=rf_switch;
board.radio_band_initialized=false;assert(board.prepareRadioFrequency(frequency));
}
// Explicit runtime shutdown must disable whichever band was powered.
board.powerOff();assert(shutdown && physical(3)==LOW && physical(4)==LOW);
// Base shutdown may finish a HF CW and rewarm LF before holding NSS HIGH.
base_restore_lf=true;board.powerOff();
assert(shutdown && sleep_callbacks==1 && !controls_high);
assert(physical(3)==LOW && physical(4)==LOW && !board.radio_band_initialized);
assert(pads[8].hold && physical(8)==HIGH && !pads[12].hold);
}
}
'''
self.compile_run(code.replace('@METHODS@',methods),s3=1)
# Detect either active-rail loss or a floating inactive rail on sleep.
for rail in ('P_LORA_LF_PA_POWER','P_LORA_HF_PA_POWER'):
broken=re.sub(r'(?m)^\s*mesh::esp32::holdOutputForDeepSleep\(\(gpio_num_t\)'+rail+
r', radio_high_band[^;]*;', '', methods)
self.assertNotEqual(broken,methods)
self.compile_run(code.replace('@METHODS@',broken),s3=1,helper_mutation=lambda text:text)
code=self.w12_board_harness(case)
self.compile_run(code,s3=1)
broken=code.replace('mesh::esp32::releaseOutputAfterDeepSleep((gpio_num_t)P_LORA_NSS, HIGH);','')
self.compile_run(broken,s3=1,helper_mutation=lambda text:text)
def test_w12_failed_shutdown_quiescence_holds_reset_before_removing_rails(self):
case=r'''
fresh();MeshnologyW12Board board;board.begin();board.radio_rf_switch_handler=rf_switch;
board.radio_band_initialized=false;assert(board.prepareRadioFrequency(909.5f));
assert(controls_high);
// A failed SPI response leaves controls unknown; reset is the independent fallback.
spi_working=false;board.powerOff();
assert(shutdown && physical(3)==LOW && physical(4)==LOW && !controls_high);
assert(pads[12].hold && physical(12)==LOW && !board.radio_band_initialized);
assert(pads[8].hold && physical(8)==HIGH && sleep_callbacks==0);
'''
code=self.w12_board_harness(case)
self.compile_run(code,s3=1)
broken=code.replace('mesh::esp32::holdOutputForDeepSleep((gpio_num_t)P_LORA_RESET, LOW);','')
self.compile_run(broken,s3=1,helper_mutation=lambda text:text)
def test_all_production_hold_release_deinit_sites_use_capability_wrapper(self):
# No board or FEM may keep using unsupported RTC calls behind the fix.
+1
View File
@@ -110,6 +110,7 @@ class LR2021 {
}
int16_t setCRC(uint8_t value) { assert(value==0 || value==2);return step(3); }
int16_t explicitHeader() { return step(4); }
int16_t standby() { return 0; }
int16_t setRxBoostedGainMode(uint8_t value) { gain=value;return step(5); }
void setRfSwitchTable(const uint32_t (&)[Module::RFSWITCH_MAX_PINS],const Module::RfSwitchMode_t*) {}
int16_t setDioFunction(uint8_t dio,uint8_t function,uint8_t pull) {
+105 -14
View File
@@ -3,6 +3,8 @@
The native radio mock models the distinction between host RF-switch GPIOs and
LR2021 DIO masks. No PlatformIO or RF hardware is needed. Failed SPI responses
may still apply a mask, so cleanup must not assume an error left a pin unchanged.
The actual warm W12 and band-switch cold tables are exercised separately from
optional transient prearming on the cold table.
"""
from pathlib import Path
import os
@@ -39,6 +41,9 @@ HARNESS = r'''
@POLICY@
#define LR2021_TX_BUSY_TIMEOUT_MS 4UL
#define LR2021_USE_DCDC 0
#ifndef TEST_WARM_STANDBY
#define TEST_WARM_STANDBY 0
#endif
#define RADIOLIB_ERR_NONE 0
#define RADIOLIB_ERR_UNKNOWN -16
#define RADIOLIB_ERR_UNSUPPORTED -99
@@ -75,7 +80,10 @@ HARNESS = r'''
using RadioLibTime_t = uint32_t;
struct SPIClass {};
struct Logger { void print(const char*) {} void println(int) {} };
namespace mesh { Logger& usbLoggingPort() { static Logger logger;return logger; } }
namespace mesh {
Logger& usbLoggingPort() { static Logger logger;return logger; }
struct MorseRx { static constexpr float ReceiverBitRateKbps=4.8f; };
}
struct Hal {
uint32_t GpioLevelHigh=HIGH;
uint32_t now=0;
@@ -109,7 +117,8 @@ public:
MODE_TX_HF,
};
Module* mod;
float freqMHz=909.5f;
float freqMHz=909.5f,tcxoVoltage=0;
uint8_t gainModeLf=1,gainModeHf=3,preambleDetLength=4;
uint8_t stagedMode=RADIOLIB_RADIO_MODE_NONE,chip_mode=MODE_STBY;
uint32_t rxTimeout=0xffffff;
uint8_t masks[7]={};
@@ -159,9 +168,6 @@ public:
int16_t setTxTestMode(uint8_t mode) { assert(mode==0);return setTx(0); }
int16_t setRx(uint32_t timeout) {
assert(timeout==rxTimeout);++rx_calls;
#if LR2021_TX_PREARM_MS > 0
assert(offInStandby());
#endif
applyMode(mesh::lr2021::isHighBand(freqMHz) ? MODE_RX_HF : MODE_RX);
return 0;
}
@@ -197,6 +203,13 @@ public:
int16_t begin(float,float,uint8_t,uint8_t,uint8_t,int8_t,uint16_t,float) {
std::fill(masks,masks+7,0);applyMode(MODE_STBY);return 0;
}
int16_t beginGFSK(float frequency,float rate,float deviation,float bandwidth,
int8_t power,uint16_t preamble,float tcxo) {
assert(rate==4.8f && deviation==0.6f && bandwidth==4.8f && power==2 && preamble==16 && tcxo==0);
freqMHz=frequency;std::fill(masks,masks+7,0);applyMode(MODE_STBY);return 0;
}
int16_t setRxBoostedGainMode(uint8_t) { return 0; }
int16_t setSyncWord(uint8_t*,size_t len) { assert(len==0);return 0; }
int16_t setRegulatorDCDC() { assert(false); return -1; }
int16_t setCRC(uint8_t) { return 0; }
int16_t explicitHeader() { return 0; }
@@ -221,7 +234,9 @@ public:
#endif
@RESTORE@
@TABLE@
@REPLACE_TABLE@
@INIT@
@MORSE_RECEIVE@
@RX_MODE@
@TX_MODE@
@LAUNCH@
@@ -230,15 +245,20 @@ public:
};
@BOARD_PINS@
@BOARD_TABLE@
@BOARD_COLD_TABLE@
static CustomLR2021* target_radio=nullptr;
@BOARD_CALLBACK@
static void configure(CustomLR2021& radio) {
radio.setRfSwitchTable(rfswitch_dios,rfswitch_table);
assert(radio.offInStandby());
radio.setRfSwitchTable(rfswitch_dios,
TEST_WARM_STANDBY ? rfswitch_table : rfswitch_table_cold);
assert(radio.offInStandby()==!TEST_WARM_STANDBY);
radio.writes.clear();radio.clear_attempts.clear();
}
static void noBias(CustomLR2021& radio) {
assert(radio.offInStandby());
const bool warm=radio._rf_switch_table==rfswitch_table;
assert(radio.offInStandby()==!warm);
if(radio.chip_mode==LR2021::MODE_STBY)
assert(!radio.level(9) && !radio.level(10) && !radio.level(11));
assert(radio.level(9)==warm && radio.level(10)==warm && radio.level(11)==warm);
#if LR2021_TX_PREARM_MS > 0
assert(!radio._tx_standby_armed);
#endif
@@ -251,11 +271,70 @@ static int packet(CustomLR2021& radio) {
}
int main(int argc,char** argv) {
assert(argc==2);std::string test=argv[1];
Module module;CustomLR2021 radio(&module);configure(radio);
Module module;CustomLR2021 radio(&module);configure(radio);target_radio=&radio;
if(test.rfind("latched_",0)==0) {
radio.latch_masks_on_mode=true;test=test.substr(8);
}
if(test=="packet" || test=="cw") {
if(test=="warm_routes" || test=="warm_reset" || test=="warm_failures" || test=="warm_replace_failures" || test=="warm_sleep") {
assert(TEST_WARM_STANDBY);
// This is exactly PR #3579's LF standby/RX/TX mask, not a timed substitute.
assert(radio.masks[9-5]==5 && radio.masks[10-5]==7 && radio.masks[11-5]==7);
noBias(radio);
if(test=="warm_routes") {
for(float frequency:{909.5f,2440.0f,909.5f,2440.0f,909.5f}) {
// The board first selects checked cold standby, then changes supplies,
// then restores warm standby only after the LF rail is powered.
assert(set_w12_rf_switch_standby(false,false));
assert(radio.offInStandby() && !radio.level(5) && !radio.level(6));noBias(radio);
radio.freqMHz=frequency;
assert(set_w12_rf_switch_standby(frequency<1500,false));
for(bool cw:{false,true}) {
radio.writes.clear();
assert((cw ? radio.transmitMorseCarrier() : packet(radio))==0);
assert(radio.chip_mode==(frequency<1500 ? LR2021::MODE_TX : LR2021::MODE_TX_HF));
assert(radio.writes.empty()); // Warm standby never removes retained LF bias.
noBias(radio);
assert(radio.standby()==0);noBias(radio);
assert(radio.startReceive()==0);
assert(radio.chip_mode==(frequency<1500 ? LR2021::MODE_RX : LR2021::MODE_RX_HF));
assert(!radio.level(9) && radio.level(10)==(frequency<1500) && radio.level(11)==(frequency<1500));
}
}
} else if(test=="warm_reset") {
for(bool warm:{true,false,true}) {
assert(radio.replaceRfSwitchTable(rfswitch_dios,warm?rfswitch_table:rfswitch_table_cold)==0);
assert(radio.beginMorseReceive(warm?909.5f:2440.0f)==0);noBias(radio);
assert(radio.std_init());noBias(radio);
assert(radio.sleep()==0);noBias(radio);
}
} else if(test=="warm_failures") {
for(int fault:{0,1,2}) {
radio.fail_tx=fault<2;radio.busy_after_tx=fault==2;
const int status=fault==1 ? radio.transmitMorseCarrier() : packet(radio);
assert(status==(fault==2 ? RADIOLIB_ERR_SPI_CMD_TIMEOUT : -72));
assert(radio.chip_mode==LR2021::MODE_STBY);noBias(radio);
assert(radio.writes.empty());module.hal->busy=false;
}
} else if(test=="warm_sleep") {
assert(set_w12_rf_switch_standby(false,true));
assert(radio.sleep_calls==1 && radio.offInStandby());noBias(radio);
assert(set_w12_rf_switch_standby(true,false));noBias(radio);
radio.fail_standby=true;
assert(!set_w12_rf_switch_standby(false,true));
assert(radio.sleep_calls==1); // Failed quiescence must not claim sleep.
} else {
// A replacement is checked at the initial standby, each mask write,
// and the final physical standby latch; errors may follow applied writes.
for(int fault:{0,1,2}) {
radio.fail_standby_at=fault==0 ? radio.standby_calls+1 : fault==2 ? radio.standby_calls+2 : 0;
radio.fail_dio=10;radio.fail_disarm=fault==1;radio.fail_count=1;
assert(radio.replaceRfSwitchTable(rfswitch_dios,rfswitch_table_cold)==(fault==1 ? -73 : -71));
radio.fail_standby_at=0;radio.fail_disarm=false;
assert(radio.replaceRfSwitchTable(rfswitch_dios,rfswitch_table_cold)==0);noBias(radio);
assert(radio.replaceRfSwitchTable(rfswitch_dios,rfswitch_table)==0);noBias(radio);
}
}
} else if(test=="packet" || test=="cw") {
radio.chip_mode=LR2021::MODE_RX;
assert((test=="packet" ? packet(radio) : radio.transmitMorseCarrier())==0);
assert(radio.tx_calls==1 && radio.chip_mode==LR2021::MODE_TX);
@@ -382,7 +461,9 @@ class LR2021TxPrearmTest(unittest.TestCase):
"@SLEEP@": "int16_t sleep()",
"@RESTORE@": "int16_t restoreRfSwitchTable(",
"@TABLE@": "void setRfSwitchTable(",
"@REPLACE_TABLE@": "int16_t replaceRfSwitchTable(",
"@INIT@": "bool std_init(",
"@MORSE_RECEIVE@": "int16_t beginMorseReceive(",
"@RX_MODE@": "uint8_t rfSwitchRxMode(",
"@TX_MODE@": "uint8_t rfSwitchTxMode(",
"@LAUNCH@": "int16_t launchMode(",
@@ -390,14 +471,18 @@ class LR2021TxPrearmTest(unittest.TestCase):
"@RECEIVE@": "int16_t startReceive()",
}.items():
source = source.replace(marker, method(driver, signature))
source = source.replace("@BOARD_CALLBACK@", method(board,
"static bool set_w12_rf_switch_standby(").replace("radio.", "target_radio->"))
source = source.replace("@POLICY@", re.search(
r"#ifndef LR2021_TX_PREARM_MS\b.*?#endif", driver, re.S).group(0))
for marker, pattern in {
"@BOARD_PINS@": r"const uint32_t rfswitch_dios\[\].*?\n};",
"@BOARD_TABLE@": r"static const Module::RfSwitchMode_t rfswitch_table\[\].*?\n};",
"@BOARD_COLD_TABLE@": r"static const Module::RfSwitchMode_t rfswitch_table_cold\[\].*?\n};",
}.items():
source = source.replace(marker, re.search(pattern, board, re.S).group(0))
for name, flags in {"w12": ["-DLR2021_TX_PREARM_MS=1"], "default": [],
for name, flags in {"w12": ["-DLR2021_TX_PREARM_MS=1", "-DTEST_WARM_STANDBY=1"],
"cold_prearm": ["-DLR2021_TX_PREARM_MS=1"], "default": [],
"disabled": ["-DLR2021_TX_PREARM_MS=0"]}.items():
cpp = Path(cls.temp.name) / (name + ".cpp")
binary = Path(cls.temp.name) / (name + (".exe" if os.name == "nt" else ""))
@@ -411,11 +496,17 @@ class LR2021TxPrearmTest(unittest.TestCase):
raise AssertionError(built.stdout + built.stderr)
cls.binaries[name] = binary
def run_case(self, case, build="w12"):
def run_case(self, case, build="cold_prearm"):
ran = subprocess.run([str(self.binaries[build]), case], capture_output=True,
text=True, timeout=10)
self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr)
def test_actual_w12_warm_table_and_checked_cold_band_transitions(self):
for case in ["warm_routes", "warm_reset", "warm_failures", "warm_replace_failures", "warm_sleep"]:
for prefix in ["", "latched_"]:
with self.subTest(case=case, latched=bool(prefix)):
self.run_case(prefix + case, "w12")
def test_packet_settles_pa_without_retaining_standby_bias(self):
self.run_case("packet")
@@ -466,7 +557,7 @@ class LR2021TxPrearmTest(unittest.TestCase):
with self.subTest(case=case):
self.run_case(case)
def test_only_w12_enables_prearm_and_normal_standby_remains_off(self):
def test_only_w12_enables_the_tx_settle_and_cleanup_guard(self):
opted_in = []
for recipe in sorted((ROOT / "variants").glob("*/platformio.ini")):
if re.search(r"(?m)^\s*-D\s+LR2021_TX_PREARM_MS=[1-9]\d*\b", recipe.read_text()):
+80 -5
View File
@@ -82,6 +82,7 @@ uint32_t millis(){return now_ms;}
struct LR2021 {
bool stopped=false, inject_read_irq=false;
int rx_status=1, tx_status=1, fifo_status=0, clear_status=0, base_status=0, standby_status=0;
int irq_clear_status=0;
uint32_t irq=0;
uint16_t fifo=4;
int packet_length=4;
@@ -95,11 +96,23 @@ struct LR2021 {
void setPacketReceivedAction(void (*fn)()){callback=fn;}
int checkIrq(int type){++irq_probes;return type==RADIOLIB_IRQ_RX_DONE?rx_status:tx_status;}
int standby(){++standbys;if(standby_status)return standby_status;stopped=true;return 0;}
int clearIrqFlags(uint32_t){assert(stopped);++irq_clears;rx_status=0;return 0;}
int getPacketLength(){return packet_length;}
float getSNR(){return 7;}
float getRSSI(){return -90;}
int getRxFifoLevel(uint16_t* output){*output=fifo;return fifo_status;}
int clearIrqFlags(uint32_t flags){
if(flags&RADIOLIB_LR2021_IRQ_RX_DONE)assert(stopped);
++irq_clears;
if(irq_clear_status)return irq_clear_status;
irq &= ~flags;
if(flags&RADIOLIB_LR2021_IRQ_RX_DONE)rx_status=0;
return 0;
}
void assertStoppedSnapshot(){
#if defined(USE_LR2021)
assert(stopped && "LR2021 RX must stop before metadata/FIFO snapshots");
#endif
}
int getPacketLength(){assertStoppedSnapshot();return packet_length;}
float getSNR(){assertStoppedSnapshot();return 7;}
float getRSSI(){assertStoppedSnapshot();return -90;}
int getRxFifoLevel(uint16_t* output){assertStoppedSnapshot();*output=fifo;return fifo_status;}
int clearRxFifo(){++clears;if(clear_status)return clear_status;fifo=0;return 0;}
virtual int16_t readData(uint8_t* output,size_t len){
++reads;
@@ -726,6 +739,68 @@ int main(){
}
''', ["-DUSE_LR2021=1"])
def test_lr2021_upstream_3512_fifo_failures_never_copy_suspect_bytes(self):
# #3512 at d208bfef94ab83280de30ba4a0675ff73b3ecef3 rejects FIFO
# length mismatches. Keep the checked, stop-before-copy integration.
compile_run(self.source() + r'''
int main(){
Chip chip;chip.stopped=true;uint8_t bytes[8];memset(bytes,0x3c,sizeof(bytes));
for(int fifo:{0,3,5}){
chip.fifo=fifo;
assert(chip.readData(bytes,4)==RADIOLIB_ERR_PACKET_TOO_SHORT);
assert(chip.reads==0&&chip.fifo==0);
for(uint8_t byte:bytes)assert(byte==0x3c);
}
chip.fifo=4;chip.fifo_status=-99;
const unsigned clears=chip.clears;
assert(chip.readData(bytes,4)==-99&&chip.clears==clears&&chip.reads==0);
chip.fifo_status=0;chip.fifo=3;chip.clear_status=-44;
assert(chip.readData(bytes,4)==-44&&chip.fifo==3&&chip.reads==0);
for(uint8_t byte:bytes)assert(byte==0x3c);
chip.clear_status=0;chip.fifo=4;
// RadioLib permits len=0 to select the actual packet length.
assert(chip.readData(bytes,0)==0&&chip.reads==1);
chip.packet_length=0;chip.fifo=0;
assert(chip.readData(bytes,0)==RADIOLIB_ERR_PACKET_TOO_SHORT&&chip.reads==1);
}
''', ["-DUSE_LR2021=1"])
def test_lr2021_failed_rx_done_ack_blocks_rearm_until_recovery(self):
compile_run(self.source() + r'''
int main(){
CustomLR2021Wrapper w;assert(w.registerInterruptAction());uint8_t bytes[16]={};
w.chip.irq_clear_status=-44;w.chip.callback();
assert(w.recvRaw(bytes,sizeof(bytes))==0);
assert(w.chip.stopped&&w.state==STATE_IDLE&&w.chip.arms==0);
assert(w.chip.irq_clears==1&&w.chip.rx_status==1&&w.n_recv_errors==1);
w.chip.irq_clear_status=0;
assert(w.recvRaw(bytes,sizeof(bytes))==0&&w.chip.arms==1&&w.state==STATE_RX);
w.chip.fifo=4;w.chip.rx_status=1;w.chip.callback();
assert(w.recvRaw(bytes,sizeof(bytes))==4&&w.chip.irq_clears==2);
assert(!w.chip.stopped&&w.state==STATE_RX&&w.chip.arms==2);
}
''', ["-DUSE_LR2021=1"])
def test_lr2021_upstream_3512_fresh_preamble_gets_a_new_activity_timeout(self):
compile_run(self.source() + r'''
int main(){
Chip chip;
chip.irq=RADIOLIB_LR2021_IRQ_LORA_HEADER_VALID|RADIOLIB_LR2021_IRQ_LORA_HDR_CRC_ERROR;
assert(chip.isReceiving()&&chip._headerSeen);
// A stale error flag and the preceding header timer must not cancel the
// next frame's preamble after continuous RX cleared HEADER_VALID.
now_ms+=chip._maxPayloadMillis;
chip.irq=RADIOLIB_LR2021_IRQ_PREAMBLE_DETECTED|RADIOLIB_LR2021_IRQ_LORA_HDR_CRC_ERROR;
assert(chip.isReceiving()&&!chip._headerSeen&&chip._activityAt==now_ms);
assert(chip.irq_clears==0);
now_ms+=chip._preambleMillis;
assert(chip.isReceiving());
++now_ms;
assert(!chip.isReceiving()&&chip._activityAt==0&&chip.irq_clears==1);
assert(!(chip.irq&RADIOLIB_LR2021_IRQ_PREAMBLE_DETECTED));
}
''', ["-DUSE_LR2021=1"])
if __name__ == "__main__":
unittest.main()