Fix W12 TX prearming without standby PA drain

This commit is contained in:
mikecarper committed 2026-10-17 13:36:38 -07:00
1 parent f1d281a766
commit 8874cf62ca
9 files changed
+747 -10

No files matched your search

+4 -2
View File
@@ -70,8 +70,10 @@ inline esp_err_t configureHeldOutput(gpio_num_t pin, uint32_t level) {
return gpio_config(&config);
}
// Terminal shutdown only: explicitly select a known output before latching
// it. The IDF GPIO API selects the correct RTC/digital hold implementation.
// Explicitly select a known output before latching it. RTC pads can also be
// retained for packet-wake sleep (e.g. W12 FEM rails). Non-RTC/digital pads
// require the terminal-only commit below; never globally freeze live pads.
// The IDF GPIO API selects the correct RTC/digital hold implementation.
inline esp_err_t holdOutputForDeepSleep(gpio_num_t pin, uint32_t level) {
esp_err_t result = configureHeldOutput(pin, level);
if (result != ESP_OK) return result;
+131 -1
View File
@@ -15,6 +15,11 @@
#define LR2021_USE_DCDC 0
#endif
// Board-specific FEM warm-up. Do not leave a PA enabled in ordinary standby.
#ifndef LR2021_TX_PREARM_MS
#define LR2021_TX_PREARM_MS 0
#endif
class CustomLR2021 : public LR2021 {
uint32_t _preambleMillis = 66;
uint32_t _maxPayloadMillis = 3934;
@@ -24,6 +29,70 @@ class CustomLR2021 : public LR2021 {
uint32_t _rf_switch_pins[Module::RFSWITCH_MAX_PINS] = {};
const Module::RfSwitchMode_t* _rf_switch_table = nullptr;
#if LR2021_TX_PREARM_MS > 0
bool _tx_standby_armed = false;
int16_t setTxStandbyMask(bool arm) {
if (!_rf_switch_table || (!arm && !_tx_standby_armed)) return RADIOLIB_ERR_NONE;
// Set the flag before writing: a failed SPI response may still have changed
// a DIO. Cleanup must attempt every affected pin, not stop at the first error.
_tx_standby_armed = true;
int16_t result = RADIOLIB_ERR_NONE;
for (size_t i = 0; i < Module::RFSWITCH_MAX_PINS; ++i) {
const uint32_t pin = _rf_switch_pins[i];
if (pin == RADIOLIB_NC || !(pin & RFSWITCH_PIN_FLAG)) continue;
const uint8_t dio = RADIOLIB_LRXXXX_DIOx_VAL(pin);
if (dio >= 7) {
if (result == RADIOLIB_ERR_NONE) result = RADIOLIB_ERR_UNKNOWN;
continue;
}
uint8_t config = 0;
for (size_t j = 0; j < 8 && _rf_switch_table[j].mode != LR2021::MODE_END_OF_TABLE; ++j) {
const uint8_t mode = _rf_switch_table[j].mode;
if (mode > 0 && mode <= 8
&& _rf_switch_table[j].values[i] == this->mod->hal->GpioLevelHigh)
config |= 1UL << (mode - 1);
}
const uint8_t standby_bit = 1U << (LR2021::MODE_STBY - 1);
const uint8_t tx_bit = 1U << (LR2021::MODE_TX - 1);
if (!(config & tx_bit) || (config & standby_bit)) continue;
const int16_t status = setDioRfSwitchConfig(dio + 5, arm ? config | standby_bit : config);
if (result == RADIOLIB_ERR_NONE) result = status;
}
if (!arm && result == RADIOLIB_ERR_NONE) _tx_standby_armed = false;
return result;
}
int16_t prepareTxRfSwitch() {
// stageMode(TX) does not enter standby. Stop RX before temporarily enabling
// the LF PA there; HF routing and boards without this opt-in are unchanged.
int16_t status = standby();
if (status == RADIOLIB_ERR_NONE && !mesh::lr2021::isHighBand(freqMHz)) {
status = setTxStandbyMask(true);
// Reissue the mode after programming its mask: do not rely on a mask
// write changing an already-selected mode's outputs immediately. Bypass
// our cleanup override here so this one intentional warm-up stays armed.
if (status == RADIOLIB_ERR_NONE) status = LR2021::standby();
if (status == RADIOLIB_ERR_NONE) this->mod->hal->delay(LR2021_TX_PREARM_MS);
}
return status;
}
int16_t finishTxRfSwitch(int16_t status) {
// Only change masks while transmitting. SetDioFunction (used by the full
// reset-restoration helper) is restricted to Standby RC by the LR2021.
// TX bits remain HIGH: disarming standby does not interrupt the packet/CW.
const int16_t restored = setTxStandbyMask(false);
if (status != RADIOLIB_ERR_NONE || restored != RADIOLIB_ERR_NONE) {
const int16_t stopped = standby();
// Request the wrapper's hard-reset recovery if the PA state is uncertain.
if (restored != RADIOLIB_ERR_NONE || stopped != RADIOLIB_ERR_NONE)
return RADIOLIB_ERR_SPI_CMD_TIMEOUT;
}
return status;
}
#endif
int16_t restoreRfSwitchTable() {
if (!_rf_switch_table) return RADIOLIB_ERR_NONE;
for (size_t i = 0; i < Module::RFSWITCH_MAX_PINS; ++i) {
@@ -44,6 +113,9 @@ class CustomLR2021 : public LR2021 {
if (status == RADIOLIB_ERR_NONE) status = setDioRfSwitchConfig(dio + 5, config);
if (status != RADIOLIB_ERR_NONE) return status;
}
#if LR2021_TX_PREARM_MS > 0
_tx_standby_armed = false;
#endif
return RADIOLIB_ERR_NONE;
}
@@ -58,6 +130,31 @@ class CustomLR2021 : public LR2021 {
public:
CustomLR2021(Module *mod) : LR2021(mod) { irqDioNum = LR2021_IRQ_DIO; }
#if LR2021_TX_PREARM_MS > 0
using LR2021::standby;
int16_t standby() override {
const bool was_armed = _tx_standby_armed;
const int16_t status = LR2021::standby();
const int16_t restored = setTxStandbyMask(false);
if (restored != RADIOLIB_ERR_NONE) return RADIOLIB_ERR_SPI_CMD_TIMEOUT;
// Also latch the normal mask after a dirty/prearmed-state cleanup.
if (was_armed && (status != RADIOLIB_ERR_NONE || LR2021::standby() != RADIOLIB_ERR_NONE)) {
// A mask write alone does not prove the PA turned off. Keep retrying
// cleanup (or hard reset) until a normal standby is confirmed.
_tx_standby_armed = true;
return RADIOLIB_ERR_SPI_CMD_TIMEOUT;
}
return status;
}
using LR2021::sleep;
int16_t sleep() override {
const int16_t status = standby();
if (status != RADIOLIB_ERR_NONE) return status;
return LR2021::sleep();
}
#endif
int16_t setProfileProtocol(uint8_t syncWord) {
int16_t status = LR2021::setSyncWord(syncWord);
if (status == RADIOLIB_ERR_NONE) status = LR2021::setCRC(2);
@@ -174,8 +271,17 @@ class CustomLR2021 : public LR2021 {
int16_t transmitMorseCarrier() {
// RadioLib's generic direct-TX path always selects MODE_TX. Use the
// band-aware routing also used by our normal packet transmit path.
#if LR2021_TX_PREARM_MS > 0
const int16_t prepared = prepareTxRfSwitch();
if (prepared != RADIOLIB_ERR_NONE) return finishTxRfSwitch(prepared);
#endif
this->mod->setRfSwitchState(rfSwitchTxMode());
return this->setTxTestMode(RADIOLIB_LR2021_TX_TEST_MODE_CW);
const int16_t status = this->setTxTestMode(RADIOLIB_LR2021_TX_TEST_MODE_CW);
#if LR2021_TX_PREARM_MS > 0
return finishTxRfSwitch(status);
#else
return status;
#endif
}
int16_t setRxBoostedGainMode(uint8_t level) {
@@ -191,9 +297,23 @@ class CustomLR2021 : public LR2021 {
const bool transmitting = this->stagedMode == RADIOLIB_RADIO_MODE_TX;
int16_t state;
if (this->stagedMode == RADIOLIB_RADIO_MODE_RX) {
#if LR2021_TX_PREARM_MS > 0
state = setTxStandbyMask(false);
if (state != RADIOLIB_ERR_NONE) {
this->stagedMode = RADIOLIB_RADIO_MODE_NONE;
return RADIOLIB_ERR_SPI_CMD_TIMEOUT;
}
#endif
this->mod->setRfSwitchState(rfSwitchRxMode());
state = this->setRx(this->rxTimeout);
} else if (transmitting) {
#if LR2021_TX_PREARM_MS > 0
state = prepareTxRfSwitch();
if (state != RADIOLIB_ERR_NONE) {
this->stagedMode = RADIOLIB_RADIO_MODE_NONE;
return finishTxRfSwitch(state);
}
#endif
this->mod->setRfSwitchState(rfSwitchTxMode());
state = this->setTx(RADIOLIB_LR2021_TX_TIMEOUT_NONE);
} else {
@@ -202,6 +322,9 @@ class CustomLR2021 : public LR2021 {
if (state != RADIOLIB_ERR_NONE) {
this->stagedMode = RADIOLIB_RADIO_MODE_NONE;
#if LR2021_TX_PREARM_MS > 0
if (transmitting) return finishTxRfSwitch(state);
#endif
return state;
}
@@ -211,12 +334,19 @@ class CustomLR2021 : public LR2021 {
this->mod->hal->yield();
if (this->mod->hal->millis() - started >= LR2021_TX_BUSY_TIMEOUT_MS) {
this->stagedMode = RADIOLIB_RADIO_MODE_NONE;
#if LR2021_TX_PREARM_MS > 0
return finishTxRfSwitch(RADIOLIB_ERR_SPI_CMD_TIMEOUT);
#else
return RADIOLIB_ERR_SPI_CMD_TIMEOUT;
#endif
}
}
}
this->stagedMode = RADIOLIB_RADIO_MODE_NONE;
#if LR2021_TX_PREARM_MS > 0
if (transmitting) return finishTxRfSwitch(RADIOLIB_ERR_NONE);
#endif
return RADIOLIB_ERR_NONE;
}