Add configurable FEM RX gain and recalibration

This commit is contained in:
mikecarper committed 2026-07-20 15:57:46 -07:00
1 parent 1a9b37d62b
commit 86905733ff
21 files changed
+910 -802

No files matched your search

+4
View File
@@ -234,6 +234,8 @@ void DataStore::loadPrefsInt(const char *filename, NodePrefs& _prefs, double& no
file.read((uint8_t *)_prefs.default_scope_name, sizeof(_prefs.default_scope_name)); // 90
file.read((uint8_t *)_prefs.default_scope_key, sizeof(_prefs.default_scope_key)); // 121
file.read((uint8_t *)&_prefs.radio_fem_rxgain, sizeof(_prefs.radio_fem_rxgain)); // 122
file.read((uint8_t *)&_prefs.radio_fem_rxgain_override,
sizeof(_prefs.radio_fem_rxgain_override)); // 123
file.close();
}
@@ -275,6 +277,8 @@ void DataStore::savePrefs(const NodePrefs& _prefs, double node_lat, double node_
file.write((uint8_t *)_prefs.default_scope_name, sizeof(_prefs.default_scope_name)); // 90
file.write((uint8_t *)_prefs.default_scope_key, sizeof(_prefs.default_scope_key)); // 121
file.write((uint8_t *)&_prefs.radio_fem_rxgain, sizeof(_prefs.radio_fem_rxgain)); // 122
file.write((uint8_t *)&_prefs.radio_fem_rxgain_override,
sizeof(_prefs.radio_fem_rxgain_override)); // 123
file.close();
}
+45 -7
View File
@@ -98,6 +98,12 @@ static const uint32_t COMMAND_RADIO_APPLY_TIMEOUT_MS = 5000UL;
#define CMD_GET_RADIO_FEM_RXGAIN 66
#define CMD_SET_RADIO_FEM_RXGAIN 67
#if defined(RADIO_FEM_RXGAIN) && (RADIO_FEM_RXGAIN == 0)
static constexpr uint8_t DEFAULT_FEM_RX_GAIN = 0;
#else
static constexpr uint8_t DEFAULT_FEM_RX_GAIN = 1;
#endif
// Stats sub-types for CMD_GET_STATS
#define STATS_TYPE_CORE 0
#define STATS_TYPE_RADIO 1
@@ -1191,7 +1197,7 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
_prefs.rx_boosted_gain = 1; // enabled by default
#endif
#endif
_prefs.radio_fem_rxgain = 1;
_prefs.radio_fem_rxgain = DEFAULT_FEM_RX_GAIN;
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
memset(&_mqtt_prefs, 0, sizeof(_mqtt_prefs));
@@ -1256,6 +1262,10 @@ void MyMesh::begin(bool has_display) {
_prefs.gps_interval = constrain(_prefs.gps_interval, 0, 86400); // Max 24 hours
_prefs.autoadd_config &= AUTO_ADD_OVERWRITE_OLDEST | AUTO_ADD_CHAT | AUTO_ADD_REPEATER | AUTO_ADD_ROOM_SERVER | AUTO_ADD_SENSOR;
_prefs.path_hash_mode = constrain(_prefs.path_hash_mode, 0, 2);
_prefs.radio_fem_rxgain_override = constrain(_prefs.radio_fem_rxgain_override, 0, 1);
if (!_prefs.radio_fem_rxgain_override) {
_prefs.radio_fem_rxgain = DEFAULT_FEM_RX_GAIN;
}
_prefs.radio_fem_rxgain = constrain(_prefs.radio_fem_rxgain, 0, 1);
#ifdef BLE_PIN_CODE // 123456 by default
@@ -1289,11 +1299,13 @@ void MyMesh::begin(bool has_display) {
radio_driver.setTxPower(_prefs.tx_power_dbm);
radio_driver.setRxBoostedGainMode(_prefs.rx_boosted_gain);
}
board.setLoRaFemLnaEnabled(_prefs.radio_fem_rxgain);
const bool fem_gain_changed = board.canControlLoRaFemLna()
&& board.isLoRaFemLnaEnabled() != (_prefs.radio_fem_rxgain != 0);
if (board.setLoRaFemLnaEnabled(_prefs.radio_fem_rxgain) && fem_gain_changed) {
_radio->recalibrateNoiseFloor();
}
MESH_DEBUG_PRINTLN("RX Boosted Gain Mode: %s",
radio_driver.getRxBoostedGainMode() ? "Enabled" : "Disabled");
// NOTE: no FEM LNA wiring here — companion has its own NodePrefs without
// radio_fem_rxgain, matching upstream (which also doesn't wire companion).
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
applyMQTTDefaults(&_mqtt_prefs);
@@ -1512,10 +1524,13 @@ void MyMesh::getNodeSnapshot(WebConfigServer::NodeSnapshot& s) {
s.airtime_factor = _prefs.airtime_factor;
s.rx_delay = _prefs.rx_delay_base;
s.rx_gain = _prefs.rx_boosted_gain;
s.fem_rx_gain = board.isLoRaFemLnaEnabled();
s.repeat = _prefs.client_repeat != 0;
s.capabilities = WebConfigServer::CAP_LOCATION | WebConfigServer::CAP_AIRTIME
| WebConfigServer::CAP_RX_DELAY | WebConfigServer::CAP_RX_GAIN
| WebConfigServer::CAP_REPEAT;
| WebConfigServer::CAP_RX_DELAY | WebConfigServer::CAP_RX_GAIN;
if (board.canControlLoRaFemLna()) {
s.capabilities |= WebConfigServer::CAP_FEM_RX_GAIN;
}
}
bool MyMesh::startWebConfig(bool force_ap, char* reply) {
@@ -1682,6 +1697,26 @@ void MyMesh::execCommand(char* cmd, char* reply) {
}
return;
}
if (strcmp(key, "radio.fem.rxgain") == 0) {
bool enabled;
if (!wcParseBool(value, enabled)) {
strcpy(reply, "Error: must be on or off");
} else if (!board.canControlLoRaFemLna()) {
strcpy(reply, "Error: unsupported");
} else {
bool changed = board.isLoRaFemLnaEnabled() != enabled;
if (!board.setLoRaFemLnaEnabled(enabled)) {
strcpy(reply, "Error: failed to apply FEM RX gain");
} else {
if (changed) _radio->recalibrateNoiseFloor();
_prefs.radio_fem_rxgain = enabled;
_prefs.radio_fem_rxgain_override = 1;
savePrefs();
strcpy(reply, "OK");
}
}
return;
}
#ifdef WITH_MQTT_BRIDGE
bool changed = false;
@@ -2829,8 +2864,11 @@ void MyMesh::handleCmdFrame(size_t len) {
if (!board.canControlLoRaFemLna()) {
writeErrFrame(ERR_CODE_UNSUPPORTED_CMD);
} else if (value <= 1) {
_prefs.radio_fem_rxgain = value;
bool changed = board.isLoRaFemLnaEnabled() != (value != 0);
if (board.setLoRaFemLnaEnabled(value != 0)) {
if (changed) _radio->recalibrateNoiseFloor();
_prefs.radio_fem_rxgain = value;
_prefs.radio_fem_rxgain_override = 1;
savePrefs();
writeOKFrame();
} else {
+1
View File
@@ -35,4 +35,5 @@ struct NodePrefs { // persisted to file
uint8_t autoadd_max_hops; // 0 = no limit, 1 = direct (0 hops), N = up to N-1 hops (max 64)
char default_scope_name[31];
uint8_t default_scope_key[16];
uint8_t radio_fem_rxgain_override; // 1 once the user overrides the build default
};
+9 -1
View File
@@ -2690,7 +2690,11 @@ void MyMesh::begin(FILESYSTEM *fs) {
}
MESH_DEBUG_PRINTLN("RX Boosted Gain Mode: %s",
radio_driver.getRxBoostedGainMode() ? "Enabled" : "Disabled");
board.setLoRaFemLnaEnabled(_prefs.radio_fem_rxgain); // LoRa FEM LNA (FEM boards only)
const bool fem_gain_changed = board.canControlLoRaFemLna()
&& board.isLoRaFemLnaEnabled() != (_prefs.radio_fem_rxgain != 0);
if (board.setLoRaFemLnaEnabled(_prefs.radio_fem_rxgain) && fem_gain_changed) {
_radio->recalibrateNoiseFloor();
}
setRxPowerSaving(_prefs.rx_powersaving_enabled, _prefs.rx_ps_rx_us, _prefs.rx_ps_sleep_us);
updateAdvertTimer();
@@ -6069,6 +6073,7 @@ void MyMesh::getNodeSnapshot(WebConfigServer::NodeSnapshot& s) {
s.tx_delay = _prefs.tx_delay_factor;
s.cad = _prefs.cad_enabled;
s.rx_gain = _prefs.rx_boosted_gain;
s.fem_rx_gain = board.isLoRaFemLnaEnabled();
s.repeat = !_prefs.disable_fwd;
s.advert_interval = _prefs.advert_interval * 2;
s.flood_advert_interval = _prefs.flood_advert_interval;
@@ -6081,6 +6086,9 @@ void MyMesh::getNodeSnapshot(WebConfigServer::NodeSnapshot& s) {
| WebConfigServer::CAP_RX_GAIN | WebConfigServer::CAP_REPEAT
| WebConfigServer::CAP_ADVERT | WebConfigServer::CAP_FLOOD
| WebConfigServer::CAP_LOOP | WebConfigServer::CAP_WIFI_POWER_SAVE;
if (board.canControlLoRaFemLna()) {
s.capabilities |= WebConfigServer::CAP_FEM_RX_GAIN;
}
}
bool MyMesh::startWebConfig(bool force_ap, char* reply) {
+1
View File
@@ -794,5 +794,6 @@ public:
bool hasPendingWork() const;
bool setRxBoostedGain(bool enable) override;
void recalibrateNoiseFloor() override { _radio->recalibrateNoiseFloor(); }
};
+9 -1
View File
@@ -830,7 +830,11 @@ void MyMesh::begin(FILESYSTEM *fs) {
if (!saved_radio_apply_pending) {
radio_driver.setTxPower(_prefs.tx_power_dbm);
}
board.setLoRaFemLnaEnabled(_prefs.radio_fem_rxgain); // LoRa FEM LNA (FEM boards only)
const bool fem_gain_changed = board.canControlLoRaFemLna()
&& board.isLoRaFemLnaEnabled() != (_prefs.radio_fem_rxgain != 0);
if (board.setLoRaFemLnaEnabled(_prefs.radio_fem_rxgain) && fem_gain_changed) {
_radio->recalibrateNoiseFloor();
}
setRxPowerSaving(_prefs.rx_powersaving_enabled, _prefs.rx_ps_rx_us, _prefs.rx_ps_sleep_us);
updateAdvertTimer();
@@ -1095,6 +1099,7 @@ void MyMesh::getNodeSnapshot(WebConfigServer::NodeSnapshot& s) {
s.tx_delay = _prefs.tx_delay_factor;
s.cad = _prefs.cad_enabled;
s.rx_gain = _prefs.rx_boosted_gain;
s.fem_rx_gain = board.isLoRaFemLnaEnabled();
s.repeat = !_prefs.disable_fwd;
s.advert_interval = _prefs.advert_interval * 2;
s.flood_advert_interval = _prefs.flood_advert_interval;
@@ -1107,6 +1112,9 @@ void MyMesh::getNodeSnapshot(WebConfigServer::NodeSnapshot& s) {
| WebConfigServer::CAP_RX_GAIN | WebConfigServer::CAP_REPEAT
| WebConfigServer::CAP_ADVERT | WebConfigServer::CAP_FLOOD
| WebConfigServer::CAP_LOOP | WebConfigServer::CAP_WIFI_POWER_SAVE;
if (board.canControlLoRaFemLna()) {
s.capabilities |= WebConfigServer::CAP_FEM_RX_GAIN;
}
}
bool MyMesh::startWebConfig(bool force_ap, char* reply) {
+1
View File
@@ -265,6 +265,7 @@ public:
void dumpLogFile() override;
void setTxPower(int8_t power_dbm) override;
bool setRxPowerSaving(bool enable, uint32_t rx_us, uint32_t sleep_us) override;
void recalibrateNoiseFloor() override { _radio->recalibrateNoiseFloor(); }
void getRxPsWatchdogCounts(uint32_t* soft, uint32_t* hard) override;
void formatNeighborsReply(char *reply) override {
+5 -1
View File
@@ -905,7 +905,11 @@ void SensorMesh::begin(FILESYSTEM* fs) {
if (!saved_radio_apply_pending) {
radio_driver.setTxPower(_prefs.tx_power_dbm);
}
board.setLoRaFemLnaEnabled(_prefs.radio_fem_rxgain); // LoRa FEM LNA (FEM boards only)
const bool fem_gain_changed = board.canControlLoRaFemLna()
&& board.isLoRaFemLnaEnabled() != (_prefs.radio_fem_rxgain != 0);
if (board.setLoRaFemLnaEnabled(_prefs.radio_fem_rxgain) && fem_gain_changed) {
_radio->recalibrateNoiseFloor();
}
setRxPowerSaving(_prefs.rx_powersaving_enabled, _prefs.rx_ps_rx_us, _prefs.rx_ps_sleep_us);
updateAdvertTimer();
+1
View File
@@ -69,6 +69,7 @@ public:
void dumpLogFile() override { }
void setTxPower(int8_t power_dbm) override;
bool setRxPowerSaving(bool enable, uint32_t rx_us, uint32_t sleep_us) override;
void recalibrateNoiseFloor() override { _radio->recalibrateNoiseFloor(); }
void getRxPsWatchdogCounts(uint32_t* soft, uint32_t* hard) override;
void formatNeighborsReply(char *reply) override {
strcpy(reply, "not supported");
+5
View File
@@ -82,6 +82,11 @@ public:
virtual void triggerNoiseFloorCalibrate(int threshold) { }
// Discard the current estimate and establish a new baseline. Use this after
// changing receive-path gain, where the old floor is no longer a valid
// filter reference for normal periodic calibration.
virtual void recalibrateNoiseFloor() { }
virtual void setCADEnabled(bool enable) { }
virtual void resetAGC() { }
+4
View File
@@ -2322,7 +2322,9 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
if (!_board->canControlLoRaFemLna()) {
strcpy(reply, "Error: unsupported");
} else if (memcmp(&config[17], "on", 2) == 0) {
bool changed = !_board->isLoRaFemLnaEnabled();
if (_board->setLoRaFemLnaEnabled(true)) {
if (changed) _callbacks->recalibrateNoiseFloor();
_prefs->radio_fem_rxgain = 1;
savePrefs();
strcpy(reply, "OK - LoRa FEM RX gain on");
@@ -2330,7 +2332,9 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
strcpy(reply, "Error: failed to apply LoRa FEM RX gain");
}
} else if (memcmp(&config[17], "off", 3) == 0) {
bool changed = _board->isLoRaFemLnaEnabled();
if (_board->setLoRaFemLnaEnabled(false)) {
if (changed) _callbacks->recalibrateNoiseFloor();
_prefs->radio_fem_rxgain = 0;
savePrefs();
strcpy(reply, "OK - LoRa FEM RX gain off");
+4
View File
@@ -514,6 +514,10 @@ public:
return false; // CommonCLI reports unsupported if not overridden by wrapper
};
virtual void recalibrateNoiseFloor() {
// no op by default for radios without a noise-floor estimator
};
// Fault-alert channel hooks (see NodePrefs::alert_*). The default no-op
// implementations keep CLI commands harmless on builds that don't wire up
// an AlertReporter.
File diff suppressed because it is too large. Load diff
+3 -1
View File
@@ -30,7 +30,8 @@ static const char SECRET_SENTINEL[] = "********";
static const char* const ALLOWED_SET_KEYS[] = {
// NodePrefs (radio / node)
"name", "lat", "lon", "radio", "tx", "af", "rxdelay", "txdelay",
"cad", "radio.rxgain", "repeat", "advert.interval", "flood.advert.interval",
"cad", "radio.rxgain", "radio.fem.rxgain", "repeat", "advert.interval",
"flood.advert.interval",
"flood.max", "flood.max.advert", "flood.max.unscoped", "loop.detect",
// MQTTPrefs (WiFi / MQTT / misc observer)
"wifi.ssid", "wifi.pwd", "wifi.powersave",
@@ -729,6 +730,7 @@ void WebConfigServer::handleConfigGet(AsyncWebServerRequest* req) {
radio["txdelay"] = node.tx_delay;
radio["cad"] = (bool)node.cad;
radio["rxgain"] = (bool)node.rx_gain;
radio["fem_rxgain"] = (bool)node.fem_rx_gain;
radio["repeat"] = (bool)node.repeat;
radio["flood_max"] = node.flood_max;
radio["flood_max_advert"] = node.flood_max_advert;
+2
View File
@@ -54,6 +54,7 @@ public:
CAP_LOOP = 1UL << 8,
CAP_TX_DELAY = 1UL << 9,
CAP_WIFI_POWER_SAVE = 1UL << 10,
CAP_FEM_RX_GAIN = 1UL << 11,
CAP_DELAYS = CAP_RX_DELAY | CAP_TX_DELAY,
};
@@ -75,6 +76,7 @@ public:
uint8_t cr;
uint8_t cad;
uint8_t rx_gain;
uint8_t fem_rx_gain;
uint8_t repeat;
uint16_t advert_interval;
uint8_t flood_advert_interval;
+33 -4
View File
@@ -151,10 +151,12 @@ bool RadioLibWrapper::setRxBoostedGainMode(bool enabled) {
uint8_t resume_rx = beginReconfigure();
if (resume_rx > 1) return false;
const bool gain_changed = !_rx_boosted_gain_valid || _cur_rx_boosted_gain != enabled;
bool success = applyRxBoostedGainMode(enabled);
if (success) {
_cur_rx_boosted_gain = enabled;
_rx_boosted_gain_valid = true;
if (gain_changed) recalibrateNoiseFloor();
}
endReconfigure(resume_rx);
@@ -174,6 +176,29 @@ void RadioLibWrapper::triggerNoiseFloorCalibrate(int threshold) {
requestNoiseFloorRefresh();
}
void RadioLibWrapper::recalibrateNoiseFloor() {
// A receive-gain change can move the real floor above the old floor's
// sampling gate. Invalidate that reference so the next batch is seeded from
// scratch instead of rejecting the new baseline as interference.
_noise_floor_valid = false;
_nf_refresh_requested = true;
_nf_last_calib = 0;
_num_floor_samples = 0;
_floor_sample_sum = 0;
const unsigned long now = millis();
_nf_sample_from = now + NF_CALIB_SETTLE_MS;
if (_nf_calib_active) {
// Keep an already-open continuous-RX calibration window active, but restart
// its sample and timeout bounds around the newly selected gain path.
_nf_calib_deadline = now + NF_CALIB_TIMEOUT_MS;
} else {
// Continuous RX starts its deadline in loop(); RX power saving opens a
// bounded continuous-RX window in noiseFloorCalibCheck().
_nf_calib_deadline = 0;
}
}
void RadioLibWrapper::requestNoiseFloorRefresh() {
if (_nf_refresh_requested) return;
_nf_refresh_requested = true;
@@ -386,13 +411,17 @@ void RadioLibWrapper::loop() {
&& _num_floor_samples < NUM_NOISE_FLOOR_SAMPLES) {
// Noise floor is only sampled outside RX duty-cycle mode: continuously in
// plain RX (powersaving off), or inside an on-demand calibration window
// (powersaving on), skipping the first moments after RX entry there while
// the frontend/AGC settles (unsettled GetRssiInst reads ~-127 dBm garbage).
// (powersaving on). Skip the first moments after entering RX or changing
// gain while the frontend/AGC settles (unsettled GetRssiInst reads
// ~-127 dBm garbage).
if (!_rx_ps_armed
&& !(_nf_calib_active && (long)(now - _nf_sample_from) < 0)
&& !(_nf_sample_from != 0 && (long)(now - _nf_sample_from) < 0)
&& !isReceivingPacket()) {
int rssi = getCurrentRSSI();
if (rssi < _noise_floor + SAMPLING_THRESHOLD) { // only consider samples below current floor + sampling THRESHOLD
if (!_noise_floor_valid || rssi < _noise_floor + SAMPLING_THRESHOLD) {
// With no valid baseline (startup, AGC reset, or gain change), seed
// unconditionally. Otherwise reject likely traffic above the current
// floor plus the sampling margin.
_num_floor_samples++;
_floor_sample_sum += rssi;
}
+1
View File
@@ -147,6 +147,7 @@ public:
int getNoiseFloor() const override { return _noise_floor; }
void triggerNoiseFloorCalibrate(int threshold) override;
void recalibrateNoiseFloor() override;
void setCADEnabled(bool enable) override { _cad_enabled = enable; }
void resetAGC() override;
-4
View File
@@ -122,10 +122,6 @@ const char* T096Board::getManufacturerName() const {
}
bool T096Board::setLoRaFemLnaEnabled(bool enable) {
#if defined(RADIO_FEM_RXGAIN) && (RADIO_FEM_RXGAIN == 0)
enable = false;
#endif
if (!loRaFEMControl.isLnaCanControl()) {
return false;
}
@@ -63,10 +63,6 @@ void HeltecTrackerV2Board::begin() {
}
bool HeltecTrackerV2Board::setLoRaFemLnaEnabled(bool enable) {
#if defined(RADIO_FEM_RXGAIN) && (RADIO_FEM_RXGAIN == 0)
enable = false;
#endif
if (!loRaFEMControl.isLnaCanControl()) {
return false;
}
-4
View File
@@ -64,10 +64,6 @@ void HeltecV4Board::begin() {
}
bool HeltecV4Board::setLoRaFemLnaEnabled(bool enable) {
#if defined(RADIO_FEM_RXGAIN) && (RADIO_FEM_RXGAIN == 0)
enable = false;
#endif
if (!loRaFEMControl.isLnaCanControl()) {
return false;
}
+3
View File
@@ -264,6 +264,8 @@ canvas{width:100%;height:56px;display:block}
<span class="tgl"><input type="checkbox" data-k="cad"><u></u></span></div>
<div class="sw" data-cap="16"><span><b>RX boosted gain</b><i>SX126x receivers only</i></span>
<span class="tgl"><input type="checkbox" data-k="radio.rxgain"><u></u></span></div>
<div class="sw" data-cap="2048"><span><b>FEM RX boost</b><i>External front-end-module LNA</i></span>
<span class="tgl"><input type="checkbox" data-k="radio.fem.rxgain"><u></u></span></div>
<div class="sw" data-cap="32"><span><b>Repeat</b><i>Forward mesh traffic. Off = listen-only (still observes and publishes).</i></span>
<span class="tgl"><input type="checkbox" data-k="repeat"><u></u></span></div>
<div class="row" style="margin-top:6px" data-cap="64">
@@ -513,6 +515,7 @@ function cfgVal(k){ // map a `set` key to its current value string, from st.cfg
case"tx":return String(r.tx);case"af":return String(r.af);
case"rxdelay":return String(r.rxdelay);case"txdelay":return String(r.txdelay);
case"cad":return r.cad?"on":"off";case"radio.rxgain":return r.rxgain?"on":"off";
case"radio.fem.rxgain":return r.fem_rxgain?"on":"off";
case"repeat":return r.repeat?"on":"off";
case"flood.max":return String(r.flood_max);
case"flood.max.advert":return String(r.flood_max_advert);