RX power saving using rx duty cycle
This commit is contained in:
1 parent
d929643524
commit
285cf10a7a
17 files changed
+1436
-40
No files matched your search
@@ -3,6 +3,7 @@
|
||||
#include "TxtDataHelpers.h"
|
||||
#include "AdvertDataHelpers.h"
|
||||
#include "TxtDataHelpers.h"
|
||||
#include "radiolib/RXPowerSavingCLI.h"
|
||||
#include <RTClib.h>
|
||||
|
||||
#ifndef BRIDGE_MAX_BAUD
|
||||
@@ -585,6 +586,13 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
} else {
|
||||
strcpy(reply, "Error: state must be on or off");
|
||||
}
|
||||
} else if (strncmp(config, "radio.rxps.rfrx_disabled ", 25) == 0) {
|
||||
RXPowerSavingCLI::setRfRxDisabled(&config[25], _rxps_control, reply, 160);
|
||||
} else if (memcmp(config, "radio.rxps ", 11) == 0) {
|
||||
if (RXPowerSavingCLI::set(&config[11], _prefs->sf, _prefs->bw, &_prefs->rxps,
|
||||
_rxps_control, reply, 160)) {
|
||||
savePrefs();
|
||||
}
|
||||
} else if (memcmp(config, "radio ", 6) == 0) {
|
||||
strcpy(tmp, &config[6]);
|
||||
const char *parts[4];
|
||||
@@ -598,8 +606,11 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
_prefs->cr = cr;
|
||||
_prefs->freq = freq;
|
||||
_prefs->bw = bw;
|
||||
bool rxps_retuned = recalcRxPowerSavingFromLevel(
|
||||
_prefs->rxps.level, _prefs->sf, _prefs->bw, _prefs->rxps.preamble,
|
||||
&_prefs->rxps.rx_us, &_prefs->rxps.sleep_us);
|
||||
_callbacks->savePrefs();
|
||||
strcpy(reply, "OK - reboot to apply");
|
||||
strcpy(reply, rxps_retuned ? "OK - reboot to apply (rxps retuned)" : "OK - reboot to apply");
|
||||
} else {
|
||||
strcpy(reply, "Error, invalid radio params");
|
||||
}
|
||||
@@ -856,6 +867,10 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
} else {
|
||||
sprintf(reply, "> %s", _board->isLoRaFemPaGainEnabled() ? "on" : "off");
|
||||
}
|
||||
} else if (strcmp(config, "radio.rxps.rfrx_disabled") == 0) {
|
||||
RXPowerSavingCLI::getRfRxDisabled(_rxps_control, reply, 160);
|
||||
} else if (strcmp(config, "radio.rxps") == 0) {
|
||||
RXPowerSavingCLI::get(&_prefs->rxps, _rxps_control, reply, 160);
|
||||
} else if (memcmp(config, "radio", 5) == 0) {
|
||||
char freq[16], bw[16];
|
||||
strcpy(freq, StrHelper::ftoa(_prefs->freq));
|
||||
|
||||
+13
-2
@@ -6,6 +6,7 @@
|
||||
#include <helpers/ClientACL.h>
|
||||
#include <helpers/RegionMap.h>
|
||||
#include <helpers/ConfigSerializer.h>
|
||||
#include <helpers/radiolib/RXPowerSaving.h>
|
||||
|
||||
#if defined(WITH_RS232_BRIDGE) || defined(WITH_ESPNOW_BRIDGE)
|
||||
#define WITH_BRIDGE
|
||||
@@ -70,6 +71,7 @@ public:
|
||||
uint8_t loop_detect = 0;
|
||||
uint8_t cad_enabled = 0; // hardware Channel Activity Detection before TX (boolean)
|
||||
uint8_t extra_sf[4];
|
||||
RxPowerSavingConfig rxps;
|
||||
|
||||
private:
|
||||
class RadioPrefs : public ConfigSerializer {
|
||||
@@ -93,6 +95,11 @@ private:
|
||||
def("agc_int", _parent->agc_reset_interval);
|
||||
def("hash_mode", _parent->path_hash_mode);
|
||||
def("multi_ack", _parent->multi_acks);
|
||||
def("rxps_en", _parent->rxps.enabled);
|
||||
def("rxps_rx_us", _parent->rxps.rx_us);
|
||||
def("rxps_sleep_us", _parent->rxps.sleep_us);
|
||||
def("rxps_level", _parent->rxps.level);
|
||||
def("rxps_preamble", _parent->rxps.preamble);
|
||||
}
|
||||
public:
|
||||
RadioPrefs(NodePrefs* parent) : _parent(parent) { }
|
||||
@@ -253,6 +260,7 @@ class CommonCLI {
|
||||
mesh::RTCClock* _rtc;
|
||||
NodePrefs* _prefs;
|
||||
CommonCLICallbacks* _callbacks;
|
||||
RxPowerSavingControl* _rxps_control;
|
||||
mesh::MainBoard* _board;
|
||||
SensorManager* _sensors;
|
||||
RegionMap* _region_map;
|
||||
@@ -268,8 +276,11 @@ class CommonCLI {
|
||||
void handleSetCmd(uint32_t sender_timestamp, char* command, char* reply);
|
||||
|
||||
public:
|
||||
CommonCLI(mesh::MainBoard& board, mesh::RTCClock& rtc, SensorManager& sensors, RegionMap& region_map, ClientACL& acl, NodePrefs* prefs, CommonCLICallbacks* callbacks)
|
||||
: _board(&board), _rtc(&rtc), _sensors(&sensors), _region_map(®ion_map), _acl(&acl), _prefs(prefs), _callbacks(callbacks) { }
|
||||
CommonCLI(mesh::MainBoard& board, mesh::RTCClock& rtc, SensorManager& sensors,
|
||||
RegionMap& region_map, ClientACL& acl, NodePrefs* prefs,
|
||||
CommonCLICallbacks* callbacks, RxPowerSavingControl* rxps_control = nullptr)
|
||||
: _rtc(&rtc), _prefs(prefs), _callbacks(callbacks), _rxps_control(rxps_control),
|
||||
_board(&board), _sensors(&sensors), _region_map(®ion_map), _acl(&acl) { }
|
||||
|
||||
void loadPrefs(FILESYSTEM* _fs);
|
||||
bool savePrefs(FILESYSTEM* _fs);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <RadioLib.h>
|
||||
#include "MeshCore.h"
|
||||
#include "RXPowerSaving.h"
|
||||
|
||||
class CustomLR1110 : public LR1110 {
|
||||
uint32_t _preambleMillis = 66;
|
||||
@@ -28,6 +29,75 @@ class CustomLR1110 : public LR1110 {
|
||||
|
||||
float getFreqMHz() const { return freqMHz; }
|
||||
|
||||
// Restores the LF clock configuration RadioLib's begin() installs. Arming
|
||||
// the duty cycle switches it to RC + BUSY-release (required by Semtech for
|
||||
// SetRxDutyCycle); without this the change would outlive RXPS being turned
|
||||
// off, and it also alters what the BUSY pin means to isChipBusy().
|
||||
int16_t restoreLfClock() {
|
||||
return configLfClock(
|
||||
RADIOLIB_LR11X0_LF_BUSY_RELEASE_DISABLED | RADIOLIB_LR11X0_LF_CLK_XOSC);
|
||||
}
|
||||
|
||||
// effectiveRxPeriod reports the RX window actually programmed, which may be
|
||||
// stretched above the requested value to satisfy the extended-period rule.
|
||||
int16_t startReceiveDutyCycle(uint32_t rxPeriod, uint32_t sleepPeriod,
|
||||
RadioLibIrqFlags_t irqFlags = RADIOLIB_IRQ_RX_DEFAULT_FLAGS,
|
||||
RadioLibIrqFlags_t irqMask = RADIOLIB_IRQ_RX_DEFAULT_MASK,
|
||||
uint32_t* effectiveRxPeriod = nullptr) {
|
||||
// Keep the conversion local until RadioLib uses 64-bit multiplication;
|
||||
// its pinned implementation overflows for longer microsecond periods.
|
||||
uint32_t symbolPeriod = (uint32_t)(((1000.0f * (float)(1UL << this->spreadingFactor)) /
|
||||
this->bandwidthKhz) + 0.999f);
|
||||
uint32_t transitionTime = this->tcxoDelay + 1000;
|
||||
if (sleepPeriod <= transitionTime) return RADIOLIB_ERR_INVALID_SLEEP_PERIOD;
|
||||
uint32_t programmedSleepPeriod = sleepPeriod - transitionTime;
|
||||
|
||||
uint64_t requiredExtendedPeriod =
|
||||
((uint64_t)this->preambleLengthLoRa + 11ULL) * symbolPeriod + 1000ULL;
|
||||
uint64_t extendedPeriod = 2ULL * rxPeriod + programmedSleepPeriod;
|
||||
if (extendedPeriod < requiredExtendedPeriod) {
|
||||
rxPeriod = (uint32_t)((requiredExtendedPeriod - programmedSleepPeriod + 1ULL) / 2ULL);
|
||||
}
|
||||
if (effectiveRxPeriod) *effectiveRxPeriod = rxPeriod;
|
||||
|
||||
uint32_t rxPeriodRaw = (uint32_t)(((uint64_t)rxPeriod * 32768UL) / 1000000UL);
|
||||
uint32_t sleepPeriodRaw =
|
||||
(uint32_t)(((uint64_t)programmedSleepPeriod * 32768UL) / 1000000UL);
|
||||
if ((rxPeriodRaw & 0xFF000000) || rxPeriodRaw == 0) return RADIOLIB_ERR_INVALID_RX_PERIOD;
|
||||
if ((sleepPeriodRaw & 0xFF000000) || sleepPeriodRaw == 0) {
|
||||
return RADIOLIB_ERR_INVALID_SLEEP_PERIOD;
|
||||
}
|
||||
|
||||
int16_t state = standby(RADIOLIB_LR11X0_STANDBY_RC);
|
||||
RADIOLIB_ASSERT(state);
|
||||
// Semtech requires the RC standby/RTC setup before SetRxDutyCycle.
|
||||
RadioModeConfig_t cfg = {
|
||||
.receive = {
|
||||
.timeout = RADIOLIB_LR11X0_RX_TIMEOUT_INF,
|
||||
.irqFlags = irqFlags,
|
||||
.irqMask = irqMask,
|
||||
.len = 0,
|
||||
}
|
||||
};
|
||||
return runRxPowerSavingArmTransaction(
|
||||
[this]() {
|
||||
return configLfClock(
|
||||
RADIOLIB_LR11X0_LF_CLK_RC | RADIOLIB_LR11X0_LF_BUSY_RELEASE_ENABLED);
|
||||
},
|
||||
[this, &cfg]() {
|
||||
return this->stageMode(RADIOLIB_RADIO_MODE_RX, &cfg);
|
||||
},
|
||||
[this, rxPeriodRaw, sleepPeriodRaw]() {
|
||||
return this->setRxDutyCycle(
|
||||
rxPeriodRaw, sleepPeriodRaw, RADIOLIB_LR11X0_RX_DUTY_CYCLE_MODE_RX);
|
||||
},
|
||||
[this]() {
|
||||
// Preserve the arm error even if this best-effort rollback fails.
|
||||
// A failed ConfigLfClock response may still mean the command ran.
|
||||
restoreLfClock();
|
||||
});
|
||||
}
|
||||
|
||||
int16_t setRxBoostedGainMode(bool en) {
|
||||
_rx_boosted = en;
|
||||
return LR1110::setRxBoostedGainMode(en);
|
||||
@@ -40,6 +110,11 @@ class CustomLR1110 : public LR1110 {
|
||||
return LR1110::startReceive(RADIOLIB_LR11X0_RX_TIMEOUT_INF, RADIOLIB_IRQ_RX_DEFAULT_FLAGS | (1UL << RADIOLIB_IRQ_PREAMBLE_DETECTED), RADIOLIB_IRQ_RX_DEFAULT_MASK, 0);
|
||||
}
|
||||
|
||||
bool isChipBusy() {
|
||||
uint32_t busy = this->mod->getGpio();
|
||||
return busy != RADIOLIB_NC && this->mod->hal->digitalRead(busy);
|
||||
}
|
||||
|
||||
bool isReceiving() {
|
||||
uint32_t irq = getIrqStatus();
|
||||
bool preamble = irq & RADIOLIB_LR11X0_IRQ_PREAMBLE_DETECTED; // bit 4
|
||||
|
||||
@@ -9,6 +9,7 @@ public:
|
||||
CustomLR1110Wrapper(CustomLR1110& radio, mesh::MainBoard& board) : RadioLibWrapper(radio, board) { }
|
||||
|
||||
void setParams(float freq, float bw, uint8_t sf, uint8_t cr) override {
|
||||
prepareForRadioConfig();
|
||||
((CustomLR1110 *)_radio)->setFrequency(freq);
|
||||
((CustomLR1110 *)_radio)->setSpreadingFactor(sf);
|
||||
((CustomLR1110 *)_radio)->setBandwidth(bw);
|
||||
@@ -20,8 +21,16 @@ public:
|
||||
}
|
||||
|
||||
bool isReceivingPacket() override {
|
||||
// While duty-cycling, BUSY marks the sleep phase: probing IRQ status over
|
||||
// SPI there could wake the chip and break the cycle. Only skip the probe
|
||||
// in that case - outside RXPS this must stay a real channel check, since
|
||||
// Dispatcher::checkSend() relies on it to avoid transmitting over others.
|
||||
if (_rx_ps_armed && ((CustomLR1110 *)_radio)->isChipBusy()) return false;
|
||||
return ((CustomLR1110 *)_radio)->isReceiving();
|
||||
}
|
||||
bool isChipBusy() override {
|
||||
return ((CustomLR1110 *)_radio)->isChipBusy();
|
||||
}
|
||||
float getCurrentRSSI() override {
|
||||
float rssi = -110;
|
||||
((CustomLR1110 *)_radio)->getRssiInst(&rssi);
|
||||
@@ -38,12 +47,28 @@ public:
|
||||
_radio->setPreambleLength(preambleLengthForSF(getSpreadingFactor())); // overcomes weird issues with small and big pkts
|
||||
}
|
||||
|
||||
float getLastRSSI() const override { return ((CustomLR1110 *)_radio)->getRSSI(); }
|
||||
float getLastSNR() const override { return ((CustomLR1110 *)_radio)->getSNR(); }
|
||||
bool supportsRxPowerSaving() const override { return true; }
|
||||
|
||||
protected:
|
||||
int16_t armDutyCycle(RadioLibIrqFlags_t irq_flags, RadioLibIrqFlags_t irq_mask,
|
||||
uint32_t* eff_rx_us, uint32_t* eff_sleep_us) override {
|
||||
*eff_sleep_us = _rx_ps_sleep_us; // only the RX window can get clamped
|
||||
return ((CustomLR1110 *)_radio)->startReceiveDutyCycle(
|
||||
_rx_ps_rx_us, _rx_ps_sleep_us, irq_flags, irq_mask, eff_rx_us);
|
||||
}
|
||||
|
||||
int16_t stopDutyCycleHardware() override {
|
||||
int16_t standby_err = _radio->standby();
|
||||
int16_t lf_err = ((CustomLR1110 *)_radio)->restoreLfClock();
|
||||
return standby_err != RADIOLIB_ERR_NONE ? standby_err : lf_err;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
uint8_t getSpreadingFactor() const override { return ((CustomLR1110 *)_radio)->getSpreadingFactor(); }
|
||||
|
||||
bool setRxBoostedGainMode(bool en) override {
|
||||
prepareForRadioConfig();
|
||||
return ((CustomLR1110 *)_radio)->setRxBoostedGainMode(en) == RADIOLIB_ERR_NONE;
|
||||
}
|
||||
bool getRxBoostedGainMode() const override {
|
||||
|
||||
@@ -8,6 +8,7 @@ class CustomSX1262 : public SX1262 {
|
||||
uint32_t _maxPayloadMillis = 3934;
|
||||
uint32_t _activityAt = 0;
|
||||
bool _headerSeen = false;
|
||||
bool _rx_ps_rf_rx_disabled = false;
|
||||
|
||||
public:
|
||||
CustomSX1262(Module *mod) : SX1262(mod) { }
|
||||
@@ -105,6 +106,31 @@ class CustomSX1262 : public SX1262 {
|
||||
return SX1262::startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF, RADIOLIB_IRQ_RX_DEFAULT_FLAGS | (1UL << RADIOLIB_IRQ_PREAMBLE_DETECTED), RADIOLIB_IRQ_RX_DEFAULT_MASK, 0);
|
||||
}
|
||||
|
||||
int16_t startReceiveDutyCycle(uint32_t rxPeriod, uint32_t sleepPeriod,
|
||||
RadioLibIrqFlags_t irqFlags = RADIOLIB_IRQ_RX_DEFAULT_FLAGS,
|
||||
RadioLibIrqFlags_t irqMask = RADIOLIB_IRQ_RX_DEFAULT_MASK) {
|
||||
int16_t state = SX1262::startReceiveDutyCycle(rxPeriod, sleepPeriod, irqFlags, irqMask);
|
||||
if (state == RADIOLIB_ERR_NONE && !_rx_ps_rf_rx_disabled) {
|
||||
// RadioLib stages RX through standby, which leaves a host-controlled
|
||||
// RXEN switch in IDLE. Set it back to RX for the whole duty-cycle;
|
||||
// boards without an external RXEN table are unaffected.
|
||||
this->mod->setRfSwitchState(Module::MODE_RX);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
void setRxPowerSavingRfRxDisabled(bool disabled) {
|
||||
_rx_ps_rf_rx_disabled = disabled;
|
||||
}
|
||||
bool isRxPowerSavingRfRxDisabled() const {
|
||||
return _rx_ps_rf_rx_disabled;
|
||||
}
|
||||
|
||||
bool isChipBusy() {
|
||||
uint32_t busy = this->mod->getGpio();
|
||||
return busy != RADIOLIB_NC && this->mod->hal->digitalRead(busy);
|
||||
}
|
||||
|
||||
bool isReceiving() {
|
||||
uint32_t irq = getIrqFlags();
|
||||
bool preamble = irq & RADIOLIB_SX126X_IRQ_PREAMBLE_DETECTED; // bit 2
|
||||
@@ -148,6 +174,19 @@ class CustomSX1262 : public SX1262 {
|
||||
return false;
|
||||
}
|
||||
|
||||
int16_t stopRTC() {
|
||||
uint8_t rtcStop = 0x00;
|
||||
int16_t state = writeRegister(RADIOLIB_SX126X_REG_RTC_CTRL, &rtcStop, 1);
|
||||
RADIOLIB_ASSERT(state);
|
||||
|
||||
uint8_t rtcEvent = 0;
|
||||
state = readRegister(RADIOLIB_SX126X_REG_EVENT_MASK, &rtcEvent, 1);
|
||||
RADIOLIB_ASSERT(state);
|
||||
|
||||
rtcEvent |= 0x02;
|
||||
return writeRegister(RADIOLIB_SX126X_REG_EVENT_MASK, &rtcEvent, 1);
|
||||
}
|
||||
|
||||
void setPreambleMillis(uint32_t preambleMillis) {
|
||||
_preambleMillis = preambleMillis;
|
||||
MESH_DEBUG_PRINTLN("Set _preambleMillis=%u", _preambleMillis);
|
||||
@@ -162,4 +201,4 @@ class CustomSX1262 : public SX1262 {
|
||||
readRegister(RADIOLIB_SX126X_REG_RX_GAIN, &rxGain, 1);
|
||||
return (rxGain == RADIOLIB_SX126X_RX_GAIN_BOOSTED);
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -13,6 +13,7 @@ public:
|
||||
CustomSX1262Wrapper(CustomSX1262& radio, mesh::MainBoard& board) : RadioLibWrapper(radio, board) { }
|
||||
|
||||
void setParams(float freq, float bw, uint8_t sf, uint8_t cr) override {
|
||||
prepareForRadioConfig();
|
||||
((CustomSX1262 *)_radio)->setFrequency(freq);
|
||||
((CustomSX1262 *)_radio)->setSpreadingFactor(sf);
|
||||
((CustomSX1262 *)_radio)->setBandwidth(bw);
|
||||
@@ -23,14 +24,20 @@ public:
|
||||
((CustomSX1262 *)_radio)->setMaxPayloadMillis(pm.payloadMillis);
|
||||
}
|
||||
|
||||
bool isReceivingPacket() override {
|
||||
bool isReceivingPacket() override {
|
||||
// While duty-cycling, BUSY marks the sleep phase: probing IRQ flags over
|
||||
// SPI there could wake the chip and break the cycle. Only skip the probe
|
||||
// in that case - outside RXPS this must stay a real channel check, since
|
||||
// Dispatcher::checkSend() relies on it to avoid transmitting over others.
|
||||
if (_rx_ps_armed && ((CustomSX1262 *)_radio)->isChipBusy()) return false;
|
||||
return ((CustomSX1262 *)_radio)->isReceiving();
|
||||
}
|
||||
bool isChipBusy() override {
|
||||
return ((CustomSX1262 *)_radio)->isChipBusy();
|
||||
}
|
||||
float getCurrentRSSI() override {
|
||||
return ((CustomSX1262 *)_radio)->getRSSI(false);
|
||||
}
|
||||
float getLastRSSI() const override { return ((CustomSX1262 *)_radio)->getRSSI(); }
|
||||
float getLastSNR() const override { return ((CustomSX1262 *)_radio)->getSNR(); }
|
||||
|
||||
float packetScore(float snr, int packet_len) override {
|
||||
int sf = ((CustomSX1262 *)_radio)->spreadingFactor;
|
||||
@@ -38,10 +45,51 @@ public:
|
||||
}
|
||||
uint8_t getSpreadingFactor() const override { return ((CustomSX1262 *)_radio)->spreadingFactor; }
|
||||
virtual void powerOff() override {
|
||||
if (_rx_ps_armed) stopReceiveDutyCycle();
|
||||
((CustomSX1262 *)_radio)->sleep(false);
|
||||
}
|
||||
|
||||
bool supportsRxPowerSaving() const override { return true; }
|
||||
|
||||
bool supportsRxPowerSavingRfRxDisable() const override {
|
||||
#if defined(SX126X_RXEN)
|
||||
return SX126X_RXEN != RADIOLIB_NC;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool setRxPowerSavingRfRxDisabled(bool disabled) override {
|
||||
if (!supportsRxPowerSavingRfRxDisable()) return false;
|
||||
prepareForRadioConfig();
|
||||
((CustomSX1262 *)_radio)->setRxPowerSavingRfRxDisabled(disabled);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isRxPowerSavingRfRxDisabled() const override {
|
||||
return ((CustomSX1262 *)_radio)->isRxPowerSavingRfRxDisabled();
|
||||
}
|
||||
|
||||
protected:
|
||||
int16_t armDutyCycle(RadioLibIrqFlags_t irq_flags, RadioLibIrqFlags_t irq_mask,
|
||||
uint32_t* eff_rx_us, uint32_t* eff_sleep_us) override {
|
||||
// RadioLib programs the SX126x with exactly what we ask for (it only
|
||||
// subtracts the wake-up transition from the sleep period internally).
|
||||
*eff_rx_us = _rx_ps_rx_us;
|
||||
*eff_sleep_us = _rx_ps_sleep_us;
|
||||
return ((CustomSX1262 *)_radio)->startReceiveDutyCycle(
|
||||
_rx_ps_rx_us, _rx_ps_sleep_us, irq_flags, irq_mask);
|
||||
}
|
||||
|
||||
int16_t stopDutyCycleHardware() override {
|
||||
int16_t standby_err = _radio->standby();
|
||||
int16_t rtc_err = ((CustomSX1262 *)_radio)->stopRTC();
|
||||
return standby_err != RADIOLIB_ERR_NONE ? standby_err : rtc_err;
|
||||
}
|
||||
|
||||
public:
|
||||
bool setRxBoostedGainMode(bool en) override {
|
||||
prepareForRadioConfig();
|
||||
return ((CustomSX1262 *)_radio)->setRxBoostedGainMode(en) == RADIOLIB_ERR_NONE;
|
||||
}
|
||||
bool getRxBoostedGainMode() const override {
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
#include "RXPowerSaving.h"
|
||||
|
||||
static uint32_t ceilRxPowerSavingValue(float value) {
|
||||
uint32_t rounded = (uint32_t)value;
|
||||
return value > (float)rounded ? rounded + 1 : rounded;
|
||||
}
|
||||
|
||||
bool isValidRxPowerSavingPeriod(uint32_t us) {
|
||||
return us >= RX_POWERSAVING_MIN_PERIOD_US && us <= RX_POWERSAVING_MAX_PERIOD_US;
|
||||
}
|
||||
|
||||
uint8_t rxPowerSavingPreambleForSF(uint8_t sf) {
|
||||
return sf <= 8 ? 32 : 16;
|
||||
}
|
||||
|
||||
bool isRxPowerSavingNumeric(const char* value) {
|
||||
if (value == nullptr || *value == 0) return false;
|
||||
while (*value) {
|
||||
if (*value < '0' || *value > '9') return false;
|
||||
value++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool calcRxPowerSavingLevel(uint8_t level, uint8_t sf, float bw, uint8_t preamble,
|
||||
uint32_t* rx_us, uint32_t* sleep_us) {
|
||||
if (rx_us == nullptr || sleep_us == nullptr || level < 1 || level > 10 || sf < 5 || sf > 12 ||
|
||||
bw <= 0.0f || (preamble != 16 && preamble != 32)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const float symbol_us = (1000.0f * (float)(1UL << sf)) / bw;
|
||||
const float amount = (float)(level - 1) / 9.0f;
|
||||
const float rx_start_symbols = preamble == 16 ? 12.0f : 16.0f;
|
||||
const float sleep_start_symbols = preamble == 16 ? 2.0f : 15.0f;
|
||||
const float rx_edge_symbols = 8.0f;
|
||||
const float sleep_edge_symbols = (float)preamble + 4.25f - 8.0f;
|
||||
|
||||
const float rx_symbols = rx_start_symbols + amount * (rx_edge_symbols - rx_start_symbols);
|
||||
const float sleep_symbols = sleep_start_symbols + amount * (sleep_edge_symbols - sleep_start_symbols);
|
||||
|
||||
*rx_us = ceilRxPowerSavingValue(rx_symbols * symbol_us);
|
||||
*sleep_us = (uint32_t)(sleep_symbols * symbol_us);
|
||||
return true;
|
||||
}
|
||||
|
||||
void ensureRxPowerSavingDefaults(uint32_t* rx_us, uint32_t* sleep_us) {
|
||||
if (!isValidRxPowerSavingPeriod(*rx_us)) *rx_us = RX_POWERSAVING_DEFAULT_RX_US;
|
||||
if (!isValidRxPowerSavingPeriod(*sleep_us)) *sleep_us = RX_POWERSAVING_DEFAULT_SLEEP_US;
|
||||
}
|
||||
|
||||
bool recalcRxPowerSavingFromLevel(uint8_t level, uint8_t sf, float bw, uint8_t preamble,
|
||||
uint32_t* rx_us, uint32_t* sleep_us) {
|
||||
if (level < 1 || level > 10) return false;
|
||||
if (preamble == 0) preamble = rxPowerSavingPreambleForSF(sf);
|
||||
|
||||
uint32_t calculated_rx_us = 0;
|
||||
uint32_t calculated_sleep_us = 0;
|
||||
if (!calcRxPowerSavingLevel(level, sf, bw, preamble, &calculated_rx_us, &calculated_sleep_us) ||
|
||||
!isValidRxPowerSavingPeriod(calculated_rx_us) ||
|
||||
!isValidRxPowerSavingPeriod(calculated_sleep_us)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*rx_us = calculated_rx_us;
|
||||
*sleep_us = calculated_sleep_us;
|
||||
return true;
|
||||
}
|
||||
|
||||
void normalizeRxPowerSavingConfig(RxPowerSavingConfig* config, uint8_t sf, float bw) {
|
||||
if (config == nullptr) return;
|
||||
|
||||
config->enabled = config->enabled ? 1 : 0;
|
||||
if (config->level > 10) config->level = RX_POWERSAVING_BALANCED_LEVEL;
|
||||
if (config->preamble != 0 && config->preamble != 16 && config->preamble != 32) {
|
||||
config->preamble = RX_POWERSAVING_PROFILE_PREAMBLE;
|
||||
}
|
||||
ensureRxPowerSavingDefaults(&config->rx_us, &config->sleep_us);
|
||||
recalcRxPowerSavingFromLevel(config->level, sf, bw, config->preamble,
|
||||
&config->rx_us, &config->sleep_us);
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
static constexpr uint32_t RX_POWERSAVING_DEFAULT_RX_US = 65625UL;
|
||||
static constexpr uint32_t RX_POWERSAVING_DEFAULT_SLEEP_US = 60000UL;
|
||||
static constexpr uint32_t RX_POWERSAVING_MIN_PERIOD_US = 1000UL;
|
||||
static constexpr uint32_t RX_POWERSAVING_MAX_PERIOD_US = 30000000UL;
|
||||
|
||||
static constexpr uint8_t RX_POWERSAVING_CONSERVATIVE_LEVEL = 1;
|
||||
static constexpr uint8_t RX_POWERSAVING_BALANCED_LEVEL = 5;
|
||||
static constexpr uint8_t RX_POWERSAVING_PROFILE_PREAMBLE = 16;
|
||||
static constexpr uint8_t RX_POWERSAVING_MAX_CONSEC_ARM_FAILURES = 3;
|
||||
|
||||
class RxPowerSavingArmRetryState {
|
||||
uint8_t _consecutive_failures = 0;
|
||||
|
||||
public:
|
||||
bool canAttempt() const {
|
||||
return _consecutive_failures < RX_POWERSAVING_MAX_CONSEC_ARM_FAILURES;
|
||||
}
|
||||
void recordSuccess() { _consecutive_failures = 0; }
|
||||
void recordFailure() {
|
||||
if (_consecutive_failures < RX_POWERSAVING_MAX_CONSEC_ARM_FAILURES) {
|
||||
_consecutive_failures++;
|
||||
}
|
||||
}
|
||||
void reset() { _consecutive_failures = 0; }
|
||||
uint8_t consecutiveFailures() const { return _consecutive_failures; }
|
||||
};
|
||||
|
||||
// Runs the part of a hardware arm sequence that starts by changing persistent
|
||||
// radio configuration. Every failing step rolls that configuration back, while
|
||||
// the original arm error remains the result returned to the caller.
|
||||
template <typename Configure, typename Stage, typename Arm, typename Rollback>
|
||||
int16_t runRxPowerSavingArmTransaction(Configure configure, Stage stage,
|
||||
Arm arm, Rollback rollback) {
|
||||
int16_t state = configure();
|
||||
if (state != 0) {
|
||||
rollback();
|
||||
return state;
|
||||
}
|
||||
|
||||
state = stage();
|
||||
if (state != 0) {
|
||||
rollback();
|
||||
return state;
|
||||
}
|
||||
|
||||
state = arm();
|
||||
if (state != 0) rollback();
|
||||
return state;
|
||||
}
|
||||
|
||||
struct RxPowerSavingConfig {
|
||||
uint8_t enabled = 0;
|
||||
uint32_t rx_us = RX_POWERSAVING_DEFAULT_RX_US;
|
||||
uint32_t sleep_us = RX_POWERSAVING_DEFAULT_SLEEP_US;
|
||||
uint8_t level = RX_POWERSAVING_BALANCED_LEVEL;
|
||||
uint8_t preamble = RX_POWERSAVING_PROFILE_PREAMBLE;
|
||||
};
|
||||
|
||||
struct RxPowerSavingStatus {
|
||||
bool supported = false;
|
||||
bool armed = false;
|
||||
int16_t last_error = 0;
|
||||
uint32_t arm_failures = 0;
|
||||
// Periods the radio actually runs with. A driver may clamp the requested
|
||||
// values to satisfy a hardware constraint, so these can differ from the
|
||||
// configured rx_us/sleep_us. Zero means "nothing armed yet".
|
||||
uint32_t effective_rx_us = 0;
|
||||
uint32_t effective_sleep_us = 0;
|
||||
};
|
||||
|
||||
class RxPowerSavingControl {
|
||||
public:
|
||||
virtual ~RxPowerSavingControl() = default;
|
||||
|
||||
virtual bool setRxPowerSaving(bool enabled, uint32_t rx_us, uint32_t sleep_us) {
|
||||
(void)rx_us;
|
||||
(void)sleep_us;
|
||||
return !enabled;
|
||||
}
|
||||
virtual RxPowerSavingStatus getRxPowerSavingStatus() const { return {}; }
|
||||
virtual bool supportsRxPowerSavingRfRxDisable() const { return false; }
|
||||
virtual bool setRxPowerSavingRfRxDisabled(bool disabled) {
|
||||
(void)disabled;
|
||||
return false;
|
||||
}
|
||||
virtual bool isRxPowerSavingRfRxDisabled() const { return false; }
|
||||
virtual bool isRxPowerSavingCalibrationActive() const { return false; }
|
||||
};
|
||||
|
||||
bool isValidRxPowerSavingPeriod(uint32_t us);
|
||||
uint8_t rxPowerSavingPreambleForSF(uint8_t sf);
|
||||
bool isRxPowerSavingNumeric(const char* value);
|
||||
bool calcRxPowerSavingLevel(uint8_t level, uint8_t sf, float bw, uint8_t preamble,
|
||||
uint32_t* rx_us, uint32_t* sleep_us);
|
||||
void ensureRxPowerSavingDefaults(uint32_t* rx_us, uint32_t* sleep_us);
|
||||
bool recalcRxPowerSavingFromLevel(uint8_t level, uint8_t sf, float bw, uint8_t preamble,
|
||||
uint32_t* rx_us, uint32_t* sleep_us);
|
||||
void normalizeRxPowerSavingConfig(RxPowerSavingConfig* config, uint8_t sf, float bw);
|
||||
@@ -0,0 +1,197 @@
|
||||
#include "RXPowerSavingCLI.h"
|
||||
|
||||
#include <Utils.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static bool parseRxPowerSavingUint32(const char* value, uint32_t* parsed) {
|
||||
if (parsed == nullptr || !isRxPowerSavingNumeric(value)) return false;
|
||||
|
||||
uint32_t result = 0;
|
||||
while (*value) {
|
||||
uint32_t digit = (uint32_t)(*value++ - '0');
|
||||
if (result > (UINT32_MAX - digit) / 10U) return false;
|
||||
result = result * 10U + digit;
|
||||
}
|
||||
*parsed = result;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RXPowerSavingCLI::set(const char* value, uint8_t sf, float bw,
|
||||
RxPowerSavingConfig* config, RxPowerSavingControl* control,
|
||||
char* reply, size_t reply_size) {
|
||||
if (value == nullptr || config == nullptr || reply == nullptr || reply_size == 0) return false;
|
||||
|
||||
RxPowerSavingConfig proposed = *config;
|
||||
uint8_t level = 0;
|
||||
uint8_t preamble = rxPowerSavingPreambleForSF(sf);
|
||||
bool level_requested = false;
|
||||
bool preamble_overridden = false;
|
||||
|
||||
ensureRxPowerSavingDefaults(&proposed.rx_us, &proposed.sleep_us);
|
||||
|
||||
if (strcmp(value, "off") == 0) {
|
||||
proposed.enabled = 0;
|
||||
} else if (strcmp(value, "on") == 0 || strcmp(value, "conservative") == 0) {
|
||||
proposed.enabled = 1;
|
||||
level = RX_POWERSAVING_CONSERVATIVE_LEVEL;
|
||||
preamble = RX_POWERSAVING_PROFILE_PREAMBLE;
|
||||
level_requested = true;
|
||||
preamble_overridden = true;
|
||||
} else if (strcmp(value, "balanced") == 0) {
|
||||
proposed.enabled = 1;
|
||||
level = RX_POWERSAVING_BALANCED_LEVEL;
|
||||
preamble = RX_POWERSAVING_PROFILE_PREAMBLE;
|
||||
level_requested = true;
|
||||
preamble_overridden = true;
|
||||
} else {
|
||||
char input[128];
|
||||
if (strlen(value) >= sizeof(input)) {
|
||||
snprintf(reply, reply_size,
|
||||
"ERROR: use off|on|conservative|balanced|level <1-10>|<rx_us> <sleep_us>");
|
||||
return false;
|
||||
}
|
||||
strcpy(input, value);
|
||||
|
||||
const char* parts[4];
|
||||
int count = mesh::Utils::parseTextParts(input, parts, 4, ' ');
|
||||
uint32_t first = 0;
|
||||
uint32_t second = 0;
|
||||
if (count == 1 && parseRxPowerSavingUint32(parts[0], &first)) {
|
||||
if (first < 1 || first > 10) {
|
||||
snprintf(reply, reply_size, "ERROR: level range is 1-10; preamble is 16 or 32");
|
||||
return false;
|
||||
}
|
||||
level = (uint8_t)first;
|
||||
level_requested = true;
|
||||
proposed.enabled = 1;
|
||||
} else if (count == 2 && strcmp(parts[0], "level") == 0 &&
|
||||
parseRxPowerSavingUint32(parts[1], &first)) {
|
||||
if (first < 1 || first > 10) {
|
||||
snprintf(reply, reply_size, "ERROR: level range is 1-10; preamble is 16 or 32");
|
||||
return false;
|
||||
}
|
||||
level = (uint8_t)first;
|
||||
level_requested = true;
|
||||
proposed.enabled = 1;
|
||||
} else if (count == 4 && strcmp(parts[0], "level") == 0 &&
|
||||
parseRxPowerSavingUint32(parts[1], &first) &&
|
||||
strcmp(parts[2], "preamble") == 0 &&
|
||||
parseRxPowerSavingUint32(parts[3], &second)) {
|
||||
if (first < 1 || first > 10 || (second != 16 && second != 32)) {
|
||||
snprintf(reply, reply_size, "ERROR: level range is 1-10; preamble is 16 or 32");
|
||||
return false;
|
||||
}
|
||||
level = (uint8_t)first;
|
||||
preamble = (uint8_t)second;
|
||||
level_requested = true;
|
||||
preamble_overridden = true;
|
||||
proposed.enabled = 1;
|
||||
} else if (count == 2 && parseRxPowerSavingUint32(parts[0], &first) &&
|
||||
parseRxPowerSavingUint32(parts[1], &second)) {
|
||||
proposed.rx_us = first;
|
||||
proposed.sleep_us = second;
|
||||
proposed.enabled = 1;
|
||||
} else {
|
||||
snprintf(reply, reply_size,
|
||||
"ERROR: use off|on|conservative|balanced|level <1-10>|<rx_us> <sleep_us>");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (level_requested &&
|
||||
!calcRxPowerSavingLevel(level, sf, bw, preamble,
|
||||
&proposed.rx_us, &proposed.sleep_us)) {
|
||||
snprintf(reply, reply_size, "ERROR: level range is 1-10; preamble is 16 or 32");
|
||||
return false;
|
||||
}
|
||||
if (!isValidRxPowerSavingPeriod(proposed.rx_us) ||
|
||||
!isValidRxPowerSavingPeriod(proposed.sleep_us)) {
|
||||
snprintf(reply, reply_size, "ERROR: range is %lu-%lu us",
|
||||
(unsigned long)RX_POWERSAVING_MIN_PERIOD_US,
|
||||
(unsigned long)RX_POWERSAVING_MAX_PERIOD_US);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool applied = control != nullptr
|
||||
? control->setRxPowerSaving(proposed.enabled != 0, proposed.rx_us, proposed.sleep_us)
|
||||
: proposed.enabled == 0;
|
||||
if (!applied) {
|
||||
snprintf(reply, reply_size, "ERROR: RX powersaving unsupported");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (level_requested) {
|
||||
proposed.level = level;
|
||||
proposed.preamble = preamble_overridden ? preamble : 0;
|
||||
} else if (strcmp(value, "off") != 0) {
|
||||
proposed.level = 0;
|
||||
proposed.preamble = 0;
|
||||
}
|
||||
|
||||
*config = proposed;
|
||||
snprintf(reply, reply_size, "OK - %s,level=%lu,preamble=%lu,rx=%lu,sleep=%lu",
|
||||
config->enabled ? "on" : "off", (unsigned long)config->level,
|
||||
(unsigned long)config->preamble, (unsigned long)config->rx_us,
|
||||
(unsigned long)config->sleep_us);
|
||||
return true;
|
||||
}
|
||||
|
||||
void RXPowerSavingCLI::get(const RxPowerSavingConfig* config,
|
||||
const RxPowerSavingControl* control,
|
||||
char* reply, size_t reply_size) {
|
||||
if (config == nullptr || reply == nullptr || reply_size == 0) return;
|
||||
|
||||
RxPowerSavingStatus status = control != nullptr
|
||||
? control->getRxPowerSavingStatus()
|
||||
: RxPowerSavingStatus{};
|
||||
int len = snprintf(reply, reply_size,
|
||||
"> desired=%s,effective=%s,supported=%s,level=%lu,preamble=%lu,rx=%lu,sleep=%lu,err=%d,fail=%lu",
|
||||
config->enabled ? "on" : "off", status.armed ? "armed" : "continuous",
|
||||
status.supported ? "yes" : "no", (unsigned long)config->level,
|
||||
(unsigned long)config->preamble, (unsigned long)config->rx_us,
|
||||
(unsigned long)config->sleep_us, (int)status.last_error,
|
||||
(unsigned long)status.arm_failures);
|
||||
|
||||
// Only report the periods the hardware really runs with when a driver had to
|
||||
// clamp them - printing them always would not fit the 160 byte reply buffer.
|
||||
if (len > 0 && (size_t)len < reply_size && status.armed &&
|
||||
(status.effective_rx_us != config->rx_us ||
|
||||
status.effective_sleep_us != config->sleep_us)) {
|
||||
snprintf(reply + len, reply_size - (size_t)len, ",erx=%lu,eslp=%lu",
|
||||
(unsigned long)status.effective_rx_us,
|
||||
(unsigned long)status.effective_sleep_us);
|
||||
}
|
||||
}
|
||||
|
||||
void RXPowerSavingCLI::setRfRxDisabled(const char* value, RxPowerSavingControl* control,
|
||||
char* reply, size_t reply_size) {
|
||||
if (value == nullptr || reply == nullptr || reply_size == 0) return;
|
||||
|
||||
bool disabled;
|
||||
if (strcmp(value, "on") == 0) {
|
||||
disabled = true;
|
||||
} else if (strcmp(value, "off") == 0) {
|
||||
disabled = false;
|
||||
} else {
|
||||
snprintf(reply, reply_size, "Error: state must be on or off");
|
||||
return;
|
||||
}
|
||||
|
||||
if (control == nullptr || !control->setRxPowerSavingRfRxDisabled(disabled)) {
|
||||
snprintf(reply, reply_size, "Error: unsupported");
|
||||
} else {
|
||||
snprintf(reply, reply_size, "OK - radio.rxps.rfrx_disabled %s", disabled ? "on" : "off");
|
||||
}
|
||||
}
|
||||
|
||||
void RXPowerSavingCLI::getRfRxDisabled(const RxPowerSavingControl* control,
|
||||
char* reply, size_t reply_size) {
|
||||
if (reply == nullptr || reply_size == 0) return;
|
||||
if (control == nullptr || !control->supportsRxPowerSavingRfRxDisable()) {
|
||||
snprintf(reply, reply_size, "Error: unsupported");
|
||||
} else {
|
||||
snprintf(reply, reply_size, "> %s", control->isRxPowerSavingRfRxDisabled() ? "on" : "off");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "RXPowerSaving.h"
|
||||
|
||||
class RXPowerSavingCLI {
|
||||
public:
|
||||
static bool set(const char* value, uint8_t sf, float bw, RxPowerSavingConfig* config,
|
||||
RxPowerSavingControl* control, char* reply, size_t reply_size);
|
||||
static void get(const RxPowerSavingConfig* config, const RxPowerSavingControl* control,
|
||||
char* reply, size_t reply_size);
|
||||
static void setRfRxDisabled(const char* value, RxPowerSavingControl* control,
|
||||
char* reply, size_t reply_size);
|
||||
static void getRfRxDisabled(const RxPowerSavingControl* control,
|
||||
char* reply, size_t reply_size);
|
||||
};
|
||||
@@ -11,6 +11,10 @@
|
||||
#define NUM_NOISE_FLOOR_SAMPLES 64
|
||||
#define SAMPLING_THRESHOLD 14
|
||||
|
||||
#define NF_CALIB_INTERVAL_MS 60000UL
|
||||
#define NF_CALIB_TIMEOUT_MS 5000UL
|
||||
#define NF_CALIB_SETTLE_MS 20UL
|
||||
|
||||
static volatile uint8_t state = STATE_IDLE;
|
||||
|
||||
// this function is called when a complete packet
|
||||
@@ -48,14 +52,21 @@ uint32_t RadioLibWrapper::getRngSeed() {
|
||||
}
|
||||
|
||||
void RadioLibWrapper::setTxPower(int8_t dbm) {
|
||||
prepareForRadioConfig();
|
||||
_radio->setOutputPower(dbm);
|
||||
}
|
||||
|
||||
void RadioLibWrapper::idle() {
|
||||
if (_rx_ps_armed) stopReceiveDutyCycle();
|
||||
_radio->standby();
|
||||
state = STATE_IDLE; // need another startReceive()
|
||||
}
|
||||
|
||||
void RadioLibWrapper::powerOff() {
|
||||
if (_rx_ps_armed) stopReceiveDutyCycle();
|
||||
_radio->sleep();
|
||||
}
|
||||
|
||||
void RadioLibWrapper::triggerNoiseFloorCalibrate(int threshold) {
|
||||
_threshold = threshold;
|
||||
if (_num_floor_samples >= NUM_NOISE_FLOOR_SAMPLES) { // ignore trigger if currently sampling
|
||||
@@ -69,8 +80,10 @@ void RadioLibWrapper::doResetAGC() {
|
||||
}
|
||||
|
||||
void RadioLibWrapper::resetAGC() {
|
||||
// make sure we're not mid-receive of packet!
|
||||
if ((state & STATE_INT_READY) != 0 || isReceivingPacket()) return;
|
||||
// make sure we're not mid-receive or mid-transmit of a packet
|
||||
if ((state & STATE_INT_READY) != 0 || isReceivingPacket() || state == STATE_TX_WAIT) return;
|
||||
|
||||
if (_rx_ps_armed) stopReceiveDutyCycle();
|
||||
|
||||
doResetAGC();
|
||||
state = STATE_IDLE; // trigger a startReceive()
|
||||
@@ -84,9 +97,48 @@ void RadioLibWrapper::resetAGC() {
|
||||
_floor_sample_sum = 0;
|
||||
}
|
||||
|
||||
// Clear the RX/idle state so the next loop calls startRecv() again, without
|
||||
// dropping an STATE_INT_READY that the ISR may raise while we are in here.
|
||||
// A plain `state = STATE_IDLE` loses that flag (and with it, a received
|
||||
// packet) when setFlag() fires between the test and the store.
|
||||
void RadioLibWrapper::requestRestartRecv() {
|
||||
noInterrupts();
|
||||
if ((state & ~STATE_INT_READY) != STATE_TX_WAIT) {
|
||||
state &= STATE_INT_READY; // STATE_IDLE, but keep a pending interrupt
|
||||
}
|
||||
interrupts();
|
||||
}
|
||||
|
||||
void RadioLibWrapper::noiseFloorCalibCheck() {
|
||||
unsigned long now = millis();
|
||||
if (_nf_calib_active) {
|
||||
if (!_rx_ps_enabled || (long)(now - _nf_calib_deadline) >= 0) {
|
||||
endNoiseFloorCalib(now);
|
||||
}
|
||||
} else if (_rx_ps_enabled && _rx_ps_armed && state == STATE_RX &&
|
||||
(_nf_last_calib == 0 || now - _nf_last_calib >= NF_CALIB_INTERVAL_MS) &&
|
||||
!isReceivingPacket()) {
|
||||
_nf_calib_active = true;
|
||||
_nf_calib_deadline = now + NF_CALIB_TIMEOUT_MS;
|
||||
_nf_sample_from = now + NF_CALIB_SETTLE_MS;
|
||||
_num_floor_samples = 0;
|
||||
_floor_sample_sum = 0;
|
||||
requestRestartRecv();
|
||||
}
|
||||
}
|
||||
|
||||
void RadioLibWrapper::endNoiseFloorCalib(unsigned long now) {
|
||||
_nf_calib_active = false;
|
||||
_nf_last_calib = now;
|
||||
requestRestartRecv();
|
||||
}
|
||||
|
||||
void RadioLibWrapper::loop() {
|
||||
noiseFloorCalibCheck();
|
||||
|
||||
if (state == STATE_RX && _num_floor_samples < NUM_NOISE_FLOOR_SAMPLES) {
|
||||
if (!isReceivingPacket()) {
|
||||
if (!_rx_ps_armed && !(_nf_calib_active && (long)(millis() - _nf_sample_from) < 0) &&
|
||||
!isReceivingPacket()) {
|
||||
int rssi = getCurrentRSSI();
|
||||
if (rssi < _noise_floor + SAMPLING_THRESHOLD) { // only consider samples below current floor + sampling THRESHOLD
|
||||
_num_floor_samples++;
|
||||
@@ -103,6 +155,8 @@ void RadioLibWrapper::loop() {
|
||||
#ifdef MESH_DEBUG_NOISE_FLOOR
|
||||
MESH_DEBUG_PRINTLN("RadioLibWrapper: noise_floor = %d", (int)_noise_floor);
|
||||
#endif
|
||||
|
||||
if (_nf_calib_active) endNoiseFloorCalib(millis());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,7 +164,7 @@ void RadioLibWrapper::startRecv() {
|
||||
#if defined(USE_LR2021)
|
||||
_radio->standby(); // without this LR2021 can throw -706 when calling startReceive after hardware CAD when side detectors are enabled
|
||||
#endif
|
||||
int err = _radio->startReceive();
|
||||
int err = startReceiveMode();
|
||||
if (err == RADIOLIB_ERR_NONE) {
|
||||
state = STATE_RX;
|
||||
} else {
|
||||
@@ -118,6 +172,98 @@ void RadioLibWrapper::startRecv() {
|
||||
}
|
||||
}
|
||||
|
||||
int16_t RadioLibWrapper::startReceiveMode() {
|
||||
if (_rx_ps_armed) stopReceiveDutyCycle();
|
||||
if (!_rx_ps_enabled || _nf_calib_active) {
|
||||
_rx_ps_armed = false;
|
||||
_rx_ps_last_error = RADIOLIB_ERR_NONE;
|
||||
return _radio->startReceive();
|
||||
}
|
||||
|
||||
if (!_rx_ps_arm_retry.canAttempt()) {
|
||||
// arming has failed repeatedly; stop paying for a doomed SPI round-trip on
|
||||
// every RX restart. A config change (setRxPowerSaving) re-enables retries.
|
||||
_rx_ps_armed = false;
|
||||
return _radio->startReceive();
|
||||
}
|
||||
|
||||
const RadioLibIrqFlags_t irq_flags =
|
||||
RADIOLIB_IRQ_RX_DEFAULT_FLAGS |
|
||||
(1UL << RADIOLIB_IRQ_PREAMBLE_DETECTED);
|
||||
const RadioLibIrqFlags_t irq_mask =
|
||||
(1UL << RADIOLIB_IRQ_RX_DONE) |
|
||||
(1UL << RADIOLIB_IRQ_TIMEOUT) |
|
||||
(1UL << RADIOLIB_IRQ_CRC_ERR) |
|
||||
(1UL << RADIOLIB_IRQ_HEADER_ERR);
|
||||
|
||||
uint32_t eff_rx_us = _rx_ps_rx_us;
|
||||
uint32_t eff_sleep_us = _rx_ps_sleep_us;
|
||||
int16_t err = armDutyCycle(irq_flags, irq_mask, &eff_rx_us, &eff_sleep_us);
|
||||
if (err == RADIOLIB_ERR_NONE) {
|
||||
_rx_ps_armed = true;
|
||||
_rx_ps_last_error = RADIOLIB_ERR_NONE;
|
||||
_rx_ps_arm_retry.recordSuccess();
|
||||
_rx_ps_eff_rx_us = eff_rx_us;
|
||||
_rx_ps_eff_sleep_us = eff_sleep_us;
|
||||
return err;
|
||||
}
|
||||
|
||||
_rx_ps_armed = false;
|
||||
_rx_ps_last_error = err;
|
||||
_rx_ps_eff_rx_us = 0;
|
||||
_rx_ps_eff_sleep_us = 0;
|
||||
n_rx_ps_arm_failures++;
|
||||
_rx_ps_arm_retry.recordFailure();
|
||||
MESH_DEBUG_PRINTLN("RadioLibWrapper: startReceiveDutyCycle(%d), continuous RX fallback", err);
|
||||
int16_t fallback_err = _radio->startReceive();
|
||||
if (fallback_err != RADIOLIB_ERR_NONE) _rx_ps_last_error = fallback_err;
|
||||
return fallback_err;
|
||||
}
|
||||
|
||||
void RadioLibWrapper::stopReceiveDutyCycle() {
|
||||
int16_t err = stopDutyCycleHardware();
|
||||
_rx_ps_armed = false;
|
||||
_rx_ps_eff_rx_us = 0;
|
||||
_rx_ps_eff_sleep_us = 0;
|
||||
if (err != RADIOLIB_ERR_NONE) _rx_ps_last_error = err;
|
||||
}
|
||||
|
||||
bool RadioLibWrapper::isPacketReady() {
|
||||
if (!_rx_ps_armed) return true;
|
||||
return _radio->checkIrq(RADIOLIB_IRQ_RX_DONE) != 0;
|
||||
}
|
||||
|
||||
void RadioLibWrapper::prepareForRadioConfig() {
|
||||
if (!_rx_ps_armed) return;
|
||||
|
||||
stopReceiveDutyCycle();
|
||||
requestRestartRecv();
|
||||
}
|
||||
|
||||
bool RadioLibWrapper::setRxPowerSaving(bool enabled, uint32_t rx_us, uint32_t sleep_us) {
|
||||
if (!isValidRxPowerSavingPeriod(rx_us) || !isValidRxPowerSavingPeriod(sleep_us)) return false;
|
||||
if (enabled && !supportsRxPowerSaving()) return false;
|
||||
|
||||
_rx_ps_enabled = enabled;
|
||||
_rx_ps_rx_us = rx_us;
|
||||
_rx_ps_sleep_us = sleep_us;
|
||||
_rx_ps_last_error = RADIOLIB_ERR_NONE;
|
||||
_rx_ps_arm_retry.reset(); // new config, give the hardware a fresh chance
|
||||
requestRestartRecv();
|
||||
return true;
|
||||
}
|
||||
|
||||
RxPowerSavingStatus RadioLibWrapper::getRxPowerSavingStatus() const {
|
||||
RxPowerSavingStatus status;
|
||||
status.supported = supportsRxPowerSaving();
|
||||
status.armed = _rx_ps_armed;
|
||||
status.last_error = _rx_ps_last_error;
|
||||
status.arm_failures = n_rx_ps_arm_failures;
|
||||
status.effective_rx_us = _rx_ps_eff_rx_us;
|
||||
status.effective_sleep_us = _rx_ps_eff_sleep_us;
|
||||
return status;
|
||||
}
|
||||
|
||||
bool RadioLibWrapper::isInRecvMode() const {
|
||||
return (state & ~STATE_INT_READY) == STATE_RX;
|
||||
}
|
||||
@@ -125,17 +271,23 @@ bool RadioLibWrapper::isInRecvMode() const {
|
||||
int RadioLibWrapper::recvRaw(uint8_t* bytes, int sz) {
|
||||
int len = 0;
|
||||
if (state & STATE_INT_READY) {
|
||||
len = _radio->getPacketLength();
|
||||
if (len > 0) {
|
||||
if (len > sz) { len = sz; }
|
||||
int err = _radio->readData(bytes, len);
|
||||
if (err != RADIOLIB_ERR_NONE) {
|
||||
MESH_DEBUG_PRINTLN("RadioLibWrapper: error: readData(%d)", err);
|
||||
len = 0;
|
||||
n_recv_errors++;
|
||||
} else {
|
||||
// Serial.print(" readData() -> "); Serial.println(len);
|
||||
n_recv++;
|
||||
if (isPacketReady()) {
|
||||
if (_rx_ps_armed) stopReceiveDutyCycle();
|
||||
len = _radio->getPacketLength();
|
||||
if (len > 0) {
|
||||
if (len > sz) { len = sz; }
|
||||
_last_snr = _radio->getSNR();
|
||||
_last_rssi = _radio->getRSSI();
|
||||
_last_metrics_valid = true;
|
||||
int err = _radio->readData(bytes, len);
|
||||
if (err != RADIOLIB_ERR_NONE) {
|
||||
MESH_DEBUG_PRINTLN("RadioLibWrapper: error: readData(%d)", err);
|
||||
len = 0;
|
||||
n_recv_errors++;
|
||||
} else {
|
||||
// Serial.print(" readData() -> "); Serial.println(len);
|
||||
n_recv++;
|
||||
}
|
||||
}
|
||||
}
|
||||
#if defined(USE_LR2021)
|
||||
@@ -146,12 +298,7 @@ int RadioLibWrapper::recvRaw(uint8_t* bytes, int sz) {
|
||||
}
|
||||
|
||||
if (state != STATE_RX) {
|
||||
int err = _radio->startReceive();
|
||||
if (err == RADIOLIB_ERR_NONE) {
|
||||
state = STATE_RX;
|
||||
} else {
|
||||
MESH_DEBUG_PRINTLN("RadioLibWrapper: error: startReceive(%d)", err);
|
||||
}
|
||||
startRecv();
|
||||
}
|
||||
return len;
|
||||
}
|
||||
@@ -161,6 +308,7 @@ uint32_t RadioLibWrapper::getEstAirtimeFor(int len_bytes) {
|
||||
}
|
||||
|
||||
bool RadioLibWrapper::startSendRaw(const uint8_t* bytes, int len) {
|
||||
if (_rx_ps_armed) stopReceiveDutyCycle();
|
||||
_board->onBeforeTransmit();
|
||||
int err = _radio->startTransmit((uint8_t *) bytes, len);
|
||||
if (err == RADIOLIB_ERR_NONE) {
|
||||
@@ -194,10 +342,12 @@ int16_t RadioLibWrapper::performChannelScan() {
|
||||
|
||||
bool RadioLibWrapper::isChannelActive() {
|
||||
// int.thresh: RSSI-based interference detection (relative to noise floor)
|
||||
if (_threshold != 0 && getCurrentRSSI() > _noise_floor + _threshold) return true;
|
||||
if (_threshold != 0 && !(_rx_ps_armed && isChipBusy()) &&
|
||||
getCurrentRSSI() > _noise_floor + _threshold) return true;
|
||||
|
||||
// cad: hardware channel activity detection
|
||||
if (_cad_enabled) {
|
||||
if (_rx_ps_armed) stopReceiveDutyCycle();
|
||||
int16_t result = performChannelScan();
|
||||
// scanChannel() triggers DIO interrupt (CAD done) which sets STATE_INT_READY
|
||||
// via setFlag() ISR. Clear it before restarting RX so recvRaw() doesn't
|
||||
@@ -211,10 +361,12 @@ bool RadioLibWrapper::isChannelActive() {
|
||||
}
|
||||
|
||||
float RadioLibWrapper::getLastRSSI() const {
|
||||
return _radio->getRSSI();
|
||||
if (_last_metrics_valid) return _last_rssi;
|
||||
return _rx_ps_armed ? 0 : _radio->getRSSI();
|
||||
}
|
||||
float RadioLibWrapper::getLastSNR() const {
|
||||
return _radio->getSNR();
|
||||
if (_last_metrics_valid) return _last_snr;
|
||||
return _rx_ps_armed ? 0 : _radio->getSNR();
|
||||
}
|
||||
|
||||
// Approximate SNR threshold per SF for successful reception (based on Semtech datasheets)
|
||||
@@ -254,4 +406,4 @@ PacketMillis RadioLibWrapper::calcMaxPacketMillis(uint8_t sf, float bw, uint8_t
|
||||
if (cr >= 5 && cr < 8) { payload_us = (payload_us * 8) / cr; }
|
||||
|
||||
return PacketMillis {(preamble_us + 999) / 1000, (payload_us + 999) / 1000};
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <Mesh.h>
|
||||
#include <RadioLib.h>
|
||||
#include "RXPowerSaving.h"
|
||||
|
||||
#ifdef USE_CC310_HW_CRYPTO
|
||||
#include <Adafruit_nRFCrypto.h>
|
||||
@@ -11,28 +12,74 @@ struct PacketMillis {
|
||||
uint32_t payloadMillis; // header-valid -> rx-done deadline
|
||||
};
|
||||
|
||||
class RadioLibWrapper : public mesh::Radio {
|
||||
class RadioLibWrapper : public mesh::Radio, public RxPowerSavingControl {
|
||||
protected:
|
||||
PhysicalLayer* _radio;
|
||||
mesh::MainBoard* _board;
|
||||
uint32_t n_recv, n_sent, n_recv_errors;
|
||||
int16_t _noise_floor, _threshold;
|
||||
float _last_rssi, _last_snr;
|
||||
bool _last_metrics_valid;
|
||||
bool _cad_enabled;
|
||||
uint16_t _num_floor_samples;
|
||||
int32_t _floor_sample_sum;
|
||||
uint8_t _preamble_sf;
|
||||
|
||||
bool _rx_ps_enabled;
|
||||
bool _rx_ps_armed;
|
||||
uint32_t _rx_ps_rx_us;
|
||||
uint32_t _rx_ps_sleep_us;
|
||||
uint32_t _rx_ps_eff_rx_us; // periods the driver actually armed with
|
||||
uint32_t _rx_ps_eff_sleep_us;
|
||||
int16_t _rx_ps_last_error;
|
||||
uint32_t n_rx_ps_arm_failures;
|
||||
RxPowerSavingArmRetryState _rx_ps_arm_retry;
|
||||
|
||||
bool _nf_calib_active;
|
||||
unsigned long _nf_last_calib;
|
||||
unsigned long _nf_calib_deadline;
|
||||
unsigned long _nf_sample_from;
|
||||
|
||||
void idle();
|
||||
void startRecv();
|
||||
void requestRestartRecv();
|
||||
void prepareForRadioConfig();
|
||||
void noiseFloorCalibCheck();
|
||||
void endNoiseFloorCalib(unsigned long now);
|
||||
int16_t startReceiveMode();
|
||||
void stopReceiveDutyCycle();
|
||||
// eff_rx_us/eff_sleep_us report the periods the driver really programmed,
|
||||
// which may be clamped to satisfy a hardware constraint.
|
||||
virtual int16_t armDutyCycle(RadioLibIrqFlags_t irq_flags,
|
||||
RadioLibIrqFlags_t irq_mask,
|
||||
uint32_t* eff_rx_us, uint32_t* eff_sleep_us) {
|
||||
(void)irq_flags;
|
||||
(void)irq_mask;
|
||||
(void)eff_rx_us;
|
||||
(void)eff_sleep_us;
|
||||
return RADIOLIB_ERR_UNSUPPORTED;
|
||||
}
|
||||
virtual int16_t stopDutyCycleHardware() { return _radio->standby(); }
|
||||
virtual bool isPacketReady();
|
||||
virtual bool isChipBusy() { return false; }
|
||||
float packetScoreInt(float snr, int sf, int packet_len);
|
||||
virtual bool isReceivingPacket() =0;
|
||||
virtual void doResetAGC();
|
||||
|
||||
public:
|
||||
RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board) : _radio(&radio), _board(&board), _preamble_sf(0) { n_recv = n_sent = 0; }
|
||||
RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board)
|
||||
: _radio(&radio), _board(&board), _last_rssi(0), _last_snr(0), _last_metrics_valid(false),
|
||||
_preamble_sf(0), _rx_ps_enabled(false), _rx_ps_armed(false),
|
||||
_rx_ps_rx_us(RX_POWERSAVING_DEFAULT_RX_US),
|
||||
_rx_ps_sleep_us(RX_POWERSAVING_DEFAULT_SLEEP_US), _rx_ps_eff_rx_us(0),
|
||||
_rx_ps_eff_sleep_us(0), _rx_ps_last_error(RADIOLIB_ERR_NONE),
|
||||
n_rx_ps_arm_failures(0), _nf_calib_active(false),
|
||||
_nf_last_calib(0), _nf_calib_deadline(0), _nf_sample_from(0) {
|
||||
n_recv = n_sent = n_recv_errors = 0;
|
||||
}
|
||||
|
||||
void begin() override;
|
||||
virtual void powerOff() { _radio->sleep(); }
|
||||
virtual void powerOff();
|
||||
int recvRaw(uint8_t* bytes, int sz) override;
|
||||
uint32_t getEstAirtimeFor(int len_bytes) override;
|
||||
bool startSendRaw(const uint8_t* bytes, int len) override;
|
||||
@@ -40,6 +87,13 @@ public:
|
||||
void onSendFinished() override;
|
||||
bool isInRecvMode() const override;
|
||||
bool isChannelActive();
|
||||
virtual bool supportsRxPowerSaving() const { return false; }
|
||||
bool setRxPowerSaving(bool enabled, uint32_t rx_us, uint32_t sleep_us) override;
|
||||
RxPowerSavingStatus getRxPowerSavingStatus() const override;
|
||||
virtual bool supportsRxPowerSavingRfRxDisable() const override { return false; }
|
||||
virtual bool setRxPowerSavingRfRxDisabled(bool) override { return false; }
|
||||
virtual bool isRxPowerSavingRfRxDisabled() const override { return false; }
|
||||
bool isRxPowerSavingCalibrationActive() const override { return _nf_calib_active; }
|
||||
|
||||
bool isReceiving() override {
|
||||
if (isReceivingPacket()) return true;
|
||||
@@ -68,7 +122,12 @@ public:
|
||||
uint32_t getPacketsRecv() const { return n_recv; }
|
||||
uint32_t getPacketsRecvErrors() const { return n_recv_errors; }
|
||||
uint32_t getPacketsSent() const { return n_sent; }
|
||||
void resetStats() { n_recv = n_sent = n_recv_errors = 0; }
|
||||
void resetStats() {
|
||||
n_recv = n_sent = n_recv_errors = n_rx_ps_arm_failures = 0;
|
||||
// Clearing diagnostics also grants a fresh set of arm attempts. If they
|
||||
// fail again, the normal three-failure continuous-RX fallback applies.
|
||||
_rx_ps_arm_retry.reset();
|
||||
}
|
||||
|
||||
virtual float getLastRSSI() const override;
|
||||
virtual float getLastSNR() const override;
|
||||
|
||||
Reference in new issue
Block a user