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

+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;