Merge PowerSaving-v18 into keymindCascade
Integrate the ESP32 power-saving core and RXPS recovery/status updates while preserving radio2, telemetry and saved settings.
This commit is contained in:
commit
9b0e013f4b
62 files changed
+1679
-139
No files matched your search
@@ -5,6 +5,9 @@
|
||||
#include "CarrierTools.h"
|
||||
#include "PacketFileLoggingConfig.h"
|
||||
#include "CommonCLI.h"
|
||||
#ifdef WRAPPER_CLASS
|
||||
#include "target.h"
|
||||
#endif
|
||||
#include "PrefsSaveReplyGuard.h"
|
||||
#include "UsbLoggingWatchdog.h"
|
||||
#include "PowerSavingDiagnostics.h"
|
||||
@@ -466,6 +469,9 @@ static const char* retryPresetName(uint8_t preset) {
|
||||
|
||||
bool CommonCLI::calculateRxPowerSavingLevel(uint32_t level, uint8_t sf, float bw, uint32_t preamble,
|
||||
uint32_t* rx_us, uint32_t* sleep_us) {
|
||||
#ifdef WRAPPER_CLASS
|
||||
if (!WRAPPER_CLASS::SupportsRxPowerSaving) return false;
|
||||
#endif
|
||||
if (level < 1 || level > 10 || preamble > 0xFF
|
||||
|| !isRxPowerSavingPreamble((uint8_t)preamble)) {
|
||||
return false;
|
||||
@@ -477,6 +483,9 @@ bool CommonCLI::calculateRxPowerSavingLevel(uint32_t level, uint8_t sf, float bw
|
||||
|
||||
static void appendRxPowerSavingAdjustmentNote(char* reply, const NodePrefs* prefs,
|
||||
uint8_t sf, float bw) {
|
||||
#ifdef WRAPPER_CLASS
|
||||
if (!WRAPPER_CLASS::SupportsRxPowerSaving) return;
|
||||
#endif
|
||||
if (!reply || !prefs || !prefs->rx_powersaving_enabled
|
||||
|| prefs->rx_ps_level < 1 || prefs->rx_ps_level > 10) {
|
||||
return;
|
||||
@@ -520,6 +529,9 @@ static void appendRxPowerSavingAdjustmentNote(char* reply, const NodePrefs* pref
|
||||
// radio SF/BW. No-op (returns false) for manual timings (rx_ps_level == 0).
|
||||
// Lets level-based RX powersaving auto-retune when SF/BW change.
|
||||
bool CommonCLI::recalculateRxPowerSavingFromLevel(NodePrefs* prefs) {
|
||||
#ifdef WRAPPER_CLASS
|
||||
if (!WRAPPER_CLASS::SupportsRxPowerSaving) return false;
|
||||
#endif
|
||||
return recalcRxPowerSavingFromLevel(prefs->rx_ps_level, prefs->sf, prefs->bw,
|
||||
prefs->rx_ps_preamble, &prefs->rx_ps_rx_us,
|
||||
&prefs->rx_ps_sleep_us);
|
||||
@@ -1795,9 +1807,7 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
_prefs->rx_ps_preamble = 0; // 0 = auto (derive from SF)
|
||||
}
|
||||
ensureRxPowerSavingDefaults(&_prefs->rx_ps_rx_us, &_prefs->rx_ps_sleep_us);
|
||||
recalcRxPowerSavingFromLevel(_prefs->rx_ps_level, _prefs->sf, _prefs->bw, _prefs->rx_ps_preamble,
|
||||
&_prefs->rx_ps_rx_us,
|
||||
&_prefs->rx_ps_sleep_us); // retune level-based timings to the loaded SF/BW
|
||||
recalculateRxPowerSavingFromLevel(_prefs); // retune supported level-based timings to loaded SF/BW
|
||||
|
||||
// Read the footer independently of optional legacy UART tails/profile.
|
||||
#if defined(RAK_4631)
|
||||
@@ -4739,6 +4749,14 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
return;
|
||||
}
|
||||
using mesh::cli::RxPowerSavingArgumentMode;
|
||||
#ifdef WRAPPER_CLASS
|
||||
if (!WRAPPER_CLASS::SupportsRxPowerSaving) {
|
||||
if (parsed.mode != RxPowerSavingArgumentMode::Off) {
|
||||
strcpy(reply, "ERROR: RX powersaving unsupported");
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if (parsed.mode == RxPowerSavingArgumentMode::Off) {
|
||||
enable = 0;
|
||||
} else if (parsed.mode == RxPowerSavingArgumentMode::Manual) {
|
||||
@@ -5880,6 +5898,8 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
} else {
|
||||
sprintf(reply, "> %s", _callbacks->isRxPowerSavingRfRxDisabled() ? "on" : "off");
|
||||
}
|
||||
} else if (strcmp(config, "radio.rxps.status") == 0) {
|
||||
_prefs->getRadioPrefs()->formatRxPowerSavingStatus(reply, 157);
|
||||
} else if (strcmp(config, "radio.rxps.config") == 0) {
|
||||
ensureRxPowerSavingDefaults(&_prefs->rx_ps_rx_us, &_prefs->rx_ps_sleep_us);
|
||||
sprintf(reply, "> %s,level=%u,preamble=%u,rx=%lu,sleep=%lu",
|
||||
|
||||
@@ -316,10 +316,19 @@ 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->rx_powersaving_enabled);
|
||||
def("rxps_rx_us", _parent->rx_ps_rx_us);
|
||||
def("rxps_sleep_us", _parent->rx_ps_sleep_us);
|
||||
def("rxps_level", _parent->rx_ps_level);
|
||||
def("rxps_preamble", _parent->rx_ps_preamble);
|
||||
}
|
||||
|
||||
public:
|
||||
explicit RadioPrefs(NodePrefs* parent) : _parent(parent) { }
|
||||
RxPowerSavingConfig readRxPowerSavingConfig() const override {
|
||||
return {_parent->rx_powersaving_enabled, _parent->rx_ps_rx_us,
|
||||
_parent->rx_ps_sleep_us, _parent->rx_ps_level, _parent->rx_ps_preamble};
|
||||
}
|
||||
// CommonRadioPrefs interface
|
||||
float getFreq() const override { return _parent->freq; }
|
||||
void setFreq(float f) override { _parent->freq = f; markDirty(); }
|
||||
@@ -408,6 +417,7 @@ private:
|
||||
void structure() override {
|
||||
def("adc_mult", _parent->adc_multiplier);
|
||||
def("pwr_sav_en", _parent->powersaving_enabled);
|
||||
def("pwr_reboot_int", _parent->reboot_interval);
|
||||
#if defined(ESP32_PLATFORM)
|
||||
def("cpu_freq", _parent->cpu_freq_mhz);
|
||||
#endif
|
||||
@@ -478,6 +488,7 @@ public:
|
||||
repeat(this), room(this), custom(&radio) { }
|
||||
|
||||
CommonRadioPrefs* getRadioPrefs() { return &radio; }
|
||||
const CommonRadioPrefs* getRadioPrefs() const { return &radio; }
|
||||
KeyValueStore* getCustom() { return &custom; }
|
||||
|
||||
bool isDirty() const override { return ConfigSerializer::isDirty() || radio.isDirty() || custom.isDirty(); }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "CommonRadioPrefs.h"
|
||||
#include "TxtDataHelpers.h"
|
||||
#include "target.h"
|
||||
#include "radiolib/RXPowerSavingCLI.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
@@ -179,10 +180,37 @@ bool CommonRadioPrefs::setByKey(const char* key, const char* value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void CommonRadioPrefs::formatRxPowerSavingStatus(char* reply, size_t reply_size) const {
|
||||
auto config = readRxPowerSavingConfig();
|
||||
uint8_t sf = getSpreadFactor();
|
||||
float bw = getBandwidth();
|
||||
uint16_t wire_preamble = 0;
|
||||
RxPowerSavingStatus status;
|
||||
#ifdef WRAPPER_CLASS
|
||||
// The primary profile includes an active tempradio lease. A status read
|
||||
// must describe that hardware timing without modifying saved settings.
|
||||
const auto* profiles = radio_driver.profiles();
|
||||
if (profiles && profiles->primary.sf >= 5 && profiles->primary.sf <= 12
|
||||
&& profiles->primary.bw > 0.0f) {
|
||||
sf = profiles->primary.sf;
|
||||
bw = profiles->primary.bw;
|
||||
}
|
||||
radio_driver.getRxPowerSavingTimings(config.rx_us, config.sleep_us);
|
||||
wire_preamble = radio_driver.currentPreambleLength();
|
||||
status = radio_driver.getRxPowerSavingStatus();
|
||||
#endif
|
||||
RXPowerSavingCLI::get(config, status, sf, bw, reply, reply_size, wire_preamble);
|
||||
}
|
||||
|
||||
bool CommonRadioPrefs::handleCommand(const char* command, uint32_t sender_timestamp, char* reply) {
|
||||
(void)sender_timestamp;
|
||||
if (command == nullptr || reply == nullptr) return false;
|
||||
|
||||
if (strcmp(command, "get radio.rxps.status") == 0) {
|
||||
formatRxPowerSavingStatus(reply, 160);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (strcmp(command, "get radio") == 0) {
|
||||
char freq[16], bw[16];
|
||||
snprintf(freq, sizeof(freq), "%s", StrHelper::ftoa(getFreq()));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include "ConfigSerializer.h"
|
||||
#include "KeyValueStore.h"
|
||||
#include "radiolib/RXPowerSaving.h"
|
||||
|
||||
class CommonRadioPrefs : public ConfigSerializer, public KeyValueStore {
|
||||
bool _is_dirty = false;
|
||||
@@ -62,6 +63,9 @@ public:
|
||||
virtual uint8_t getFEMTxGain() const = 0;
|
||||
virtual void setFEMTxGain(uint8_t g) = 0;
|
||||
|
||||
virtual RxPowerSavingConfig readRxPowerSavingConfig() const { return {}; }
|
||||
void formatRxPowerSavingStatus(char* reply, size_t reply_size) const;
|
||||
|
||||
bool handleCommand(const char* command, uint32_t sender_timestamp, char* reply);
|
||||
|
||||
bool setByKey(const char* key, const char* value) override; // for dynamic key/value access
|
||||
|
||||
@@ -51,6 +51,10 @@ class ESP32Board : public mesh::MainBoard {
|
||||
protected:
|
||||
uint8_t startup_reason;
|
||||
bool inhibit_sleep = false;
|
||||
#if defined(MOMENTARY_BUTTON_WAKE_FROM_SLEEP) && MOMENTARY_BUTTON_WAKE_FROM_SLEEP
|
||||
bool button_wake_hold_active = false;
|
||||
uint32_t button_wake_at = 0;
|
||||
#endif
|
||||
bool ota_started_ap = false;
|
||||
bool ota_started_radio = false;
|
||||
bool companion_cpu_clock_policy = false;
|
||||
@@ -413,6 +417,12 @@ public:
|
||||
bool keep_awake = inhibit_sleep
|
||||
|| hold(usb_sleep_held, Reason::UsbSession, 1)
|
||||
|| hold(isRadioTestActive(), Reason::Board, 2);
|
||||
#if defined(MOMENTARY_BUTTON_WAKE_FROM_SLEEP) && MOMENTARY_BUTTON_WAKE_FROM_SLEEP
|
||||
if (button_wake_hold_active) {
|
||||
button_wake_hold_active = (uint32_t)(millis() - button_wake_at) < 30000UL;
|
||||
keep_awake = hold(button_wake_hold_active, Reason::Button) || keep_awake;
|
||||
}
|
||||
#endif
|
||||
#if !defined(ARDUINO_USB_CDC_ON_BOOT) || !ARDUINO_USB_CDC_ON_BOOT
|
||||
// External USB-to-UART chips cannot report host attachment here. Light
|
||||
// sleep loses their first input bytes; UART wake loses the wake character
|
||||
@@ -541,6 +551,13 @@ public:
|
||||
gpio_wakeup_disable((gpio_num_t)button_gpio);
|
||||
// ESP32 MomentaryButton polls; only its sleep wake source is temporary.
|
||||
gpio_set_intr_type((gpio_num_t)button_gpio, GPIO_INTR_DISABLE);
|
||||
#if defined(MOMENTARY_BUTTON_WAKE_FROM_SLEEP) && MOMENTARY_BUTTON_WAKE_FROM_SLEEP
|
||||
if (gpio_get_level((gpio_num_t)button_gpio) == USER_BTN_PRESSED) {
|
||||
// Keep the UI responsive after the wake press has been released.
|
||||
button_wake_at = millis();
|
||||
button_wake_hold_active = true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Enable CPU interrupt servicing
|
||||
|
||||
@@ -41,6 +41,8 @@ public:
|
||||
void onSendFinished() override;
|
||||
bool isInRecvMode() const override;
|
||||
|
||||
uint32_t getCADBusyCount() const { return 0; } // ESP-NOW has no LoRa CAD
|
||||
|
||||
uint32_t getPacketsRecv() const { return n_recv; }
|
||||
uint32_t getPacketsSent() const { return n_sent; }
|
||||
uint32_t getPacketsRecvErrors() const { return n_recv_errors; }
|
||||
|
||||
@@ -29,6 +29,7 @@ class CustomLR1110 : public LR1110 {
|
||||
uint32_t _activityAt = 0;
|
||||
bool _headerSeen = false;
|
||||
bool _rx_boosted = false;
|
||||
bool _lfClockRestoreNeeded = false;
|
||||
LR1110ProfileSwitchState _profileSwitch{MC_LR1110_FAST_PROFILE_SWITCH != 0};
|
||||
uint8_t _profileSwitchFeatures = profileSwitchCapabilities();
|
||||
bool _profileStandbyWarm = false;
|
||||
@@ -381,10 +382,22 @@ class CustomLR1110 : public LR1110 {
|
||||
}
|
||||
|
||||
float getFreqMHz() const { return freqMHz; }
|
||||
uint32_t getTcxoDelay() const { return tcxoDelay; }
|
||||
|
||||
// DIO11 also carries RadioLib's IRQ2, so retain the LF RC clock.
|
||||
bool needsLfClockRestore() const { return _lfClockRestoreNeeded; }
|
||||
int16_t restoreLfClock() {
|
||||
if (!_lfClockRestoreNeeded) return RADIOLIB_ERR_NONE;
|
||||
const int16_t state = configLfClock(
|
||||
RADIOLIB_LR11X0_LF_BUSY_RELEASE_DISABLED | RADIOLIB_LR11X0_LF_CLK_RC);
|
||||
if (state == RADIOLIB_ERR_NONE) _lfClockRestoreNeeded = false;
|
||||
return state;
|
||||
}
|
||||
|
||||
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) {
|
||||
RadioLibIrqFlags_t irqMask = RADIOLIB_IRQ_RX_DEFAULT_MASK,
|
||||
uint32_t* effectiveRxPeriod = nullptr) {
|
||||
uint32_t symbolPeriod = (uint32_t)(((1000.0f * (float)(1UL << this->spreadingFactor)) /
|
||||
this->bandwidthKhz) + 0.999f);
|
||||
uint32_t transitionTime = this->tcxoDelay + 1000;
|
||||
@@ -402,6 +415,7 @@ class CustomLR1110 : public LR1110 {
|
||||
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 =
|
||||
@@ -419,6 +433,7 @@ class CustomLR1110 : public LR1110 {
|
||||
// before SetRxDutyCycle. RadioLib does neither in its LoRa RXPS path.
|
||||
int16_t state = standby(RADIOLIB_LR11X0_STANDBY_RC);
|
||||
RADIOLIB_ASSERT(state);
|
||||
_lfClockRestoreNeeded = true;
|
||||
state = configLfClock(
|
||||
RADIOLIB_LR11X0_LF_CLK_RC | RADIOLIB_LR11X0_LF_BUSY_RELEASE_ENABLED);
|
||||
RADIOLIB_ASSERT(state);
|
||||
|
||||
@@ -13,6 +13,10 @@ class CustomLR1110Wrapper : public RadioLibWrapper {
|
||||
DeepInitCallback _deep_init;
|
||||
|
||||
public:
|
||||
static constexpr bool SupportsRxPowerSaving = true;
|
||||
uint32_t rxPowerSavingTransitionUs() const override {
|
||||
return static_cast<CustomLR1110*>(_radio)->getTcxoDelay() + 1000UL;
|
||||
}
|
||||
uint8_t regulatorModes() const override { return static_cast<CustomLR1110*>(_radio)->regulatorModes(); }
|
||||
mesh::power::RegulatorMode defaultRegulatorMode() const override {
|
||||
return static_cast<CustomLR1110*>(_radio)->defaultRegulatorMode();
|
||||
@@ -135,6 +139,7 @@ public:
|
||||
}
|
||||
|
||||
bool isReceivingPacket() override {
|
||||
if (_rx_ps_armed && isChipBusy()) return false;
|
||||
return ((CustomLR1110 *)_radio)->isReceiving();
|
||||
}
|
||||
bool isChipBusy() override {
|
||||
@@ -156,10 +161,11 @@ public:
|
||||
_radio->setPreambleLength(currentPreambleLength()); // overcomes weird issues with small and big pkts
|
||||
}
|
||||
|
||||
bool supportsRxPowerSaving() const override { return true; }
|
||||
bool supportsRxPowerSaving() const override { return SupportsRxPowerSaving; }
|
||||
void onReceiveProcessed() override { finishReceiveProcessing(); }
|
||||
|
||||
protected:
|
||||
int startReceiveMode() override { return startReceivePowerSaving(); }
|
||||
bool isPacketReady() override {
|
||||
// Header errors are recovery events, not packets. Handle them before the
|
||||
// generic duty-cycle readiness check can discard the IRQ or a stale buffer
|
||||
@@ -178,51 +184,26 @@ protected:
|
||||
return RadioLibWrapper::isPacketReady();
|
||||
}
|
||||
|
||||
int startReceiveMode() override {
|
||||
// Do not abort a frame that started while a calibration transition was
|
||||
// being scheduled. recvRaw() will move it to continuous RX afterward.
|
||||
if (_nf_calib_active && _rx_ps_armed && isPacketPendingOrReceiving()) {
|
||||
return RADIOLIB_ERR_NONE;
|
||||
}
|
||||
if (_rx_ps_armed) {
|
||||
// stop the previous duty-cycle sequence with an explicit standby before
|
||||
// reconfiguring the radio
|
||||
stopReceiveDutyCycle();
|
||||
}
|
||||
if (!_rx_ps_enabled || _nf_calib_active) {
|
||||
// plain continuous RX: powersaving off, or a periodic noise-floor
|
||||
// calibration window is in progress
|
||||
if (!_rx_ps_enabled) _rx_ps_continuous_fallback = false;
|
||||
return _radio->startReceive();
|
||||
}
|
||||
|
||||
if (rxPowerSavingUsesContinuousFallback(_rx_ps_rx_us, _rx_ps_sleep_us)) {
|
||||
_rx_ps_continuous_fallback = true;
|
||||
return _radio->startReceive();
|
||||
}
|
||||
|
||||
_rx_ps_continuous_fallback = false;
|
||||
|
||||
const RadioLibIrqFlags_t irqFlags = RADIOLIB_IRQ_RX_DEFAULT_FLAGS;
|
||||
// route RX timeout (false preamble detect) and error IRQs to the IRQ pin
|
||||
// as well, so the wrapper re-arms RX instead of waiting forever
|
||||
const RadioLibIrqFlags_t irqMask =
|
||||
(1UL << RADIOLIB_IRQ_RX_DONE) |
|
||||
(1UL << RADIOLIB_IRQ_TIMEOUT) |
|
||||
(1UL << RADIOLIB_IRQ_CRC_ERR) |
|
||||
(1UL << RADIOLIB_IRQ_HEADER_ERR);
|
||||
|
||||
int err = ((CustomLR1110 *)_radio)->startReceiveDutyCycle(_rx_ps_rx_us, _rx_ps_sleep_us, irqFlags, irqMask);
|
||||
if (err == RADIOLIB_ERR_NONE) {
|
||||
_rx_ps_armed = true;
|
||||
return err;
|
||||
}
|
||||
|
||||
_rx_ps_continuous_fallback = true;
|
||||
MESH_DEBUG_PRINTLN("CustomLR1110Wrapper: error: startReceiveDutyCycle(%d), falling back to continuous RX", err);
|
||||
return _radio->startReceive();
|
||||
int16_t armDutyCycle(RadioLibIrqFlags_t irq_flags, RadioLibIrqFlags_t irq_mask,
|
||||
uint32_t* effective_rx_us, uint32_t* effective_sleep_us) override {
|
||||
*effective_sleep_us = _rx_ps_sleep_us;
|
||||
return ((CustomLR1110*)_radio)->startReceiveDutyCycle(
|
||||
_rx_ps_rx_us, _rx_ps_sleep_us, irq_flags, irq_mask, effective_rx_us);
|
||||
}
|
||||
|
||||
int16_t stopDutyCycleHardware() override {
|
||||
const int16_t standby_error = _radio->standby();
|
||||
const int16_t clock_error = ((CustomLR1110*)_radio)->restoreLfClock();
|
||||
return standby_error != RADIOLIB_ERR_NONE ? standby_error : clock_error;
|
||||
}
|
||||
|
||||
int16_t startContinuousReceive() override {
|
||||
auto* radio = static_cast<CustomLR1110*>(_radio);
|
||||
if (!radio->needsLfClockRestore()) return _radio->startReceive();
|
||||
int16_t clock_error = _radio->standby();
|
||||
if (clock_error == RADIOLIB_ERR_NONE) clock_error = radio->restoreLfClock();
|
||||
return clock_error == RADIOLIB_ERR_NONE ? _radio->startReceive() : clock_error;
|
||||
}
|
||||
bool radioDeepInit() override {
|
||||
return _deep_init != NULL && prepareRadioHardReset() && _deep_init()
|
||||
&& _board->finishRadioHardReset();
|
||||
|
||||
@@ -74,6 +74,7 @@ class CustomSX1262 : public SX1262 {
|
||||
bool _tcxoWakePending = false;
|
||||
|
||||
public:
|
||||
uint32_t getTcxoDelay() const { return tcxoDelay; }
|
||||
CustomSX1262(Module *mod) : SX1262(mod) { }
|
||||
|
||||
uint8_t regulatorModes() const { return _regulator.modes(); }
|
||||
|
||||
@@ -53,6 +53,10 @@ protected:
|
||||
}
|
||||
|
||||
public:
|
||||
static constexpr bool SupportsRxPowerSaving = true;
|
||||
uint32_t rxPowerSavingTransitionUs() const override {
|
||||
return static_cast<CustomSX1262*>(_radio)->getTcxoDelay() + 1000UL;
|
||||
}
|
||||
uint8_t regulatorModes() const override { return static_cast<CustomSX1262*>(_radio)->regulatorModes(); }
|
||||
mesh::power::RegulatorMode defaultRegulatorMode() const override {
|
||||
return static_cast<CustomSX1262*>(_radio)->defaultRegulatorMode();
|
||||
@@ -182,6 +186,7 @@ public:
|
||||
}
|
||||
|
||||
bool isReceivingPacket() override {
|
||||
if (_rx_ps_armed && isChipBusy()) return false;
|
||||
return ((CustomSX1262 *)_radio)->isReceiving();
|
||||
}
|
||||
bool isChipBusy() override {
|
||||
@@ -198,11 +203,12 @@ public:
|
||||
uint8_t getSpreadingFactor() const override { return ((CustomSX1262 *)_radio)->spreadingFactor; }
|
||||
void powerOff() {
|
||||
if (isCarrierWaveActive()) setCarrierWave(0, 0);
|
||||
if (_rx_ps_armed) stopReceiveDutyCycle();
|
||||
((CustomSX1262 *)_radio)->standby();
|
||||
((CustomSX1262 *)_radio)->sleep(false);
|
||||
}
|
||||
|
||||
bool supportsRxPowerSaving() const override { return true; }
|
||||
bool supportsRxPowerSaving() const override { return SupportsRxPowerSaving; }
|
||||
void onReceiveProcessed() override { finishReceiveProcessing(); }
|
||||
|
||||
bool supportsRxPowerSavingRfRxDisable() const override {
|
||||
@@ -261,57 +267,20 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
int startReceiveMode() override {
|
||||
// Do not abort a frame that started while a calibration transition was
|
||||
// being scheduled. recvRaw() will move it to continuous RX afterward.
|
||||
if (_nf_calib_active && _rx_ps_armed && isPacketPendingOrReceiving()) {
|
||||
return RADIOLIB_ERR_NONE;
|
||||
}
|
||||
if (_rx_ps_armed) {
|
||||
// leaving duty-cycle mode (after RxDone or a reconfig): stop the
|
||||
// sequencer and the still-running RTC, or its pending event can
|
||||
// silently abort the RX we are about to start
|
||||
stopReceiveDutyCycle();
|
||||
}
|
||||
if (!_rx_ps_enabled || _nf_calib_active) {
|
||||
// plain continuous RX: powersaving off, or a periodic noise-floor
|
||||
// calibration window is in progress
|
||||
if (!_rx_ps_enabled) _rx_ps_continuous_fallback = false;
|
||||
return _radio->startReceive();
|
||||
}
|
||||
|
||||
if (!((CustomSX1262 *)_radio)->canUseRxPowerSavingDutyCycle(
|
||||
_rx_ps_rx_us, _rx_ps_sleep_us)) {
|
||||
_rx_ps_continuous_fallback = true;
|
||||
return _radio->startReceive();
|
||||
}
|
||||
|
||||
_rx_ps_continuous_fallback = false;
|
||||
|
||||
const RadioLibIrqFlags_t irqFlags = RADIOLIB_IRQ_RX_DEFAULT_FLAGS;
|
||||
const RadioLibIrqFlags_t irqMask =
|
||||
(1UL << RADIOLIB_IRQ_RX_DONE) |
|
||||
(1UL << RADIOLIB_IRQ_TIMEOUT) |
|
||||
(1UL << RADIOLIB_IRQ_CRC_ERR) |
|
||||
(1UL << RADIOLIB_IRQ_HEADER_ERR);
|
||||
|
||||
int err = ((CustomSX1262 *)_radio)->startReceiveDutyCycle(_rx_ps_rx_us, _rx_ps_sleep_us, irqFlags, irqMask);
|
||||
if (err == RADIOLIB_ERR_NONE) {
|
||||
_rx_ps_armed = true;
|
||||
return err;
|
||||
}
|
||||
|
||||
_rx_ps_continuous_fallback = true;
|
||||
MESH_DEBUG_PRINTLN("CustomSX1262Wrapper: error: startReceiveDutyCycle(%d), falling back to continuous RX", err);
|
||||
return _radio->startReceive();
|
||||
int startReceiveMode() override { return startReceivePowerSaving(); }
|
||||
int16_t armDutyCycle(RadioLibIrqFlags_t irq_flags, RadioLibIrqFlags_t irq_mask,
|
||||
uint32_t* effective_rx_us, uint32_t* effective_sleep_us) override {
|
||||
*effective_rx_us = _rx_ps_rx_us;
|
||||
*effective_sleep_us = _rx_ps_sleep_us;
|
||||
return ((CustomSX1262*)_radio)->startReceiveDutyCycle(
|
||||
_rx_ps_rx_us, _rx_ps_sleep_us, irq_flags, irq_mask);
|
||||
}
|
||||
|
||||
void stopReceiveDutyCycle() override {
|
||||
_radio->standby(); // also wakes the chip if it is in the sleep window
|
||||
((CustomSX1262 *)_radio)->stopRTC();
|
||||
_rx_ps_armed = false;
|
||||
int16_t stopDutyCycleHardware() override {
|
||||
const int16_t standby_error = _radio->standby();
|
||||
const int16_t rtc_error = ((CustomSX1262*)_radio)->stopRTC();
|
||||
return standby_error != RADIOLIB_ERR_NONE ? standby_error : rtc_error;
|
||||
}
|
||||
|
||||
bool radioDeepInit() override {
|
||||
// A board hook takes precedence over direct NRST because it may also need
|
||||
// to recover a regulator, SPI bus, or I/O expander. With neither mechanism
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "RXPowerSaving.h"
|
||||
#include <math.h>
|
||||
|
||||
bool normalizeRxPowerSavingConfig(RxPowerSavingConfig* config, uint8_t sf, float bw,
|
||||
float capture_cost_symbols, uint32_t transition_us) {
|
||||
if (config == nullptr || sf < 5 || sf > 12 || !isfinite(bw) || bw <= 0
|
||||
|| bw > 1000000.0f || !(capture_cost_symbols > 0.0f)
|
||||
|| !isfinite(capture_cost_symbols) || transition_us >= RX_POWERSAVING_MAX_PERIOD_US) return false;
|
||||
RxPowerSavingConfig proposed = *config;
|
||||
proposed.enabled = proposed.enabled ? 1 : 0;
|
||||
if (proposed.level > RX_POWERSAVING_RISKY_WORKING_MAX_LEVEL) {
|
||||
proposed.level = RX_POWERSAVING_BALANCED_LEVEL;
|
||||
}
|
||||
if (proposed.preamble != 0 && !isRxPowerSavingPreamble(proposed.preamble)) {
|
||||
proposed.preamble = RX_POWERSAVING_PROFILE_PREAMBLE;
|
||||
}
|
||||
if (proposed.level == 0) {
|
||||
if (proposed.rx_us < RX_POWERSAVING_MIN_MANUAL_PERIOD_US ||
|
||||
proposed.sleep_us < RX_POWERSAVING_MIN_MANUAL_PERIOD_US ||
|
||||
!isValidRxPowerSavingPeriod(proposed.rx_us) ||
|
||||
!isValidRxPowerSavingPeriod(proposed.sleep_us)) return false;
|
||||
} else if (!recalcRxPowerSavingFromLevel(proposed.level, sf, bw, proposed.preamble,
|
||||
&proposed.rx_us, &proposed.sleep_us,
|
||||
nullptr, nullptr, transition_us, capture_cost_symbols)) {
|
||||
return false;
|
||||
}
|
||||
*config = proposed;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool applyRxPowerSavingConfig(RxPowerSavingConfig* config, uint8_t sf, float bw,
|
||||
RxPowerSavingControl* control) {
|
||||
if (config == nullptr) return false;
|
||||
const bool valid = !config->enabled || normalizeRxPowerSavingConfig(
|
||||
config, sf, bw, rxPowerSavingCaptureCost(control), rxPowerSavingTransition(control));
|
||||
if (control == nullptr) return !config->enabled;
|
||||
const bool accepted = control->setRxPowerSaving(
|
||||
config->enabled && valid, config->rx_us, config->sleep_us);
|
||||
control->setRxPowerSavingConfigError(!valid);
|
||||
return valid && accepted;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
|
||||
#define RX_POWERSAVING_DEFAULT_RX_US 65625UL
|
||||
#define RX_POWERSAVING_DEFAULT_SLEEP_US 60000UL
|
||||
@@ -345,7 +346,9 @@ inline bool calcRxPowerSavingLevel(uint8_t level, uint8_t sf, float bw, uint8_t
|
||||
uint32_t transition_us = rxPowerSavingDefaultTransitionUs()) {
|
||||
if (rx_us == nullptr || sleep_us == nullptr || level < 1 ||
|
||||
level > RX_POWERSAVING_RISKY_WORKING_MAX_LEVEL || sf < 5 || sf > 12 ||
|
||||
!(bw > 0.0f) || bw > 1000000.0f || !isRxPowerSavingPreamble(preamble)) {
|
||||
!(bw > 0.0f) || bw > 1000000.0f || !isRxPowerSavingPreamble(preamble)
|
||||
|| !(capture_cost_symbols > 0.0f) || !(capture_cost_symbols < preamble)
|
||||
|| transition_us >= RX_POWERSAVING_MAX_PERIOD_US) {
|
||||
return false;
|
||||
}
|
||||
const bool unguarded = isRxPowerSavingUnguardedLevel(level);
|
||||
@@ -466,10 +469,12 @@ inline bool calcRxPowerSavingLevelAtOrAbove(
|
||||
uint8_t requested_level, uint8_t sf, float bw, uint8_t preamble,
|
||||
uint32_t transition_us, uint32_t *rx_us, uint32_t *sleep_us,
|
||||
uint8_t *effective_level = nullptr,
|
||||
uint8_t *effective_preamble = nullptr) {
|
||||
uint8_t *effective_preamble = nullptr,
|
||||
float capture_cost_symbols = rxPowerSavingDefaultCaptureCost()) {
|
||||
if (!rx_us || !sleep_us || requested_level < 1 || requested_level > 10
|
||||
|| sf < 5 || sf > 12 || !(bw > 0.0f) || bw > 1000000.0f
|
||||
|| !isRxPowerSavingPreamble(preamble)) {
|
||||
|| !isRxPowerSavingPreamble(preamble) || !(capture_cost_symbols > 0.0f)
|
||||
|| !(capture_cost_symbols < preamble) || transition_us >= RX_POWERSAVING_MAX_PERIOD_US) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -491,7 +496,7 @@ inline bool calcRxPowerSavingLevelAtOrAbove(
|
||||
uint32_t candidate_rx_us = 0, candidate_sleep_us = 0;
|
||||
if (!calcRxPowerSavingLevel(level, sf, bw, candidate_preamble,
|
||||
&candidate_rx_us, &candidate_sleep_us,
|
||||
rxPowerSavingDefaultCaptureCost(), transition_us)) continue;
|
||||
capture_cost_symbols, transition_us)) continue;
|
||||
|
||||
// The catch assumption can be conservative, but the receive timeout
|
||||
// must still cover the entire preamble our senders actually transmit.
|
||||
@@ -544,7 +549,8 @@ inline bool recalcRxPowerSavingFromLevel(uint8_t level, uint8_t sf, float bw, ui
|
||||
uint32_t *rx_ps_rx_us, uint32_t *rx_ps_sleep_us,
|
||||
uint8_t *effective_level = nullptr,
|
||||
uint8_t *effective_preamble = nullptr,
|
||||
uint32_t transition_us = rxPowerSavingDefaultTransitionUs()) {
|
||||
uint32_t transition_us = rxPowerSavingDefaultTransitionUs(),
|
||||
float capture_cost_symbols = rxPowerSavingDefaultCaptureCost()) {
|
||||
if (level < 1 || level > 10) {
|
||||
return false; // manual: nothing to recompute
|
||||
}
|
||||
@@ -556,7 +562,7 @@ inline bool recalcRxPowerSavingFromLevel(uint8_t level, uint8_t sf, float bw, ui
|
||||
uint32_t rx_us, sleep_us;
|
||||
if (!calcRxPowerSavingLevelAtOrAbove(
|
||||
level, sf, bw, preamble, transition_us, &rx_us, &sleep_us,
|
||||
effective_level, effective_preamble)) {
|
||||
effective_level, effective_preamble, capture_cost_symbols)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -569,3 +575,68 @@ inline bool recalcRxPowerSavingFromLevel(uint8_t level, uint8_t sf, float bw, ui
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Runtime RXPS control is separate from the persisted role-specific layout.
|
||||
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 (canAttempt()) ++_consecutive_failures;
|
||||
}
|
||||
void reset() { _consecutive_failures = 0; }
|
||||
uint8_t consecutiveFailures() const { return _consecutive_failures; }
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
constexpr RxPowerSavingConfig() = default;
|
||||
constexpr RxPowerSavingConfig(uint8_t enabled_value, uint32_t rx_value,
|
||||
uint32_t sleep_value, uint8_t level_value, uint8_t preamble_value)
|
||||
: enabled(enabled_value), rx_us(rx_value), sleep_us(sleep_value),
|
||||
level(level_value), preamble(preamble_value) { }
|
||||
};
|
||||
|
||||
struct RxPowerSavingStatus {
|
||||
bool supported = false;
|
||||
bool armed = false;
|
||||
int16_t last_error = 0;
|
||||
uint32_t arm_failures = 0;
|
||||
uint32_t effective_rx_us = 0;
|
||||
uint32_t effective_sleep_us = 0;
|
||||
};
|
||||
|
||||
class RxPowerSavingControl {
|
||||
protected:
|
||||
~RxPowerSavingControl() = default;
|
||||
public:
|
||||
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 void setRxPowerSavingConfigError(bool invalid) { (void)invalid; }
|
||||
virtual float rxPowerSavingCaptureCostSymbols() const { return rxPowerSavingDefaultCaptureCost(); }
|
||||
virtual uint32_t rxPowerSavingTransitionUs() const { return rxPowerSavingDefaultTransitionUs(); }
|
||||
};
|
||||
|
||||
inline uint8_t rxPowerSavingPreambleForSF(uint8_t sf) { return sf <= 8 ? 32 : 16; }
|
||||
inline bool isRxPowerSavingNumeric(const char* value) { return isNumeric(value); }
|
||||
inline float rxPowerSavingCaptureCost(const RxPowerSavingControl* control) {
|
||||
return control ? control->rxPowerSavingCaptureCostSymbols() : rxPowerSavingDefaultCaptureCost();
|
||||
}
|
||||
inline uint32_t rxPowerSavingTransition(const RxPowerSavingControl* control) {
|
||||
return control ? control->rxPowerSavingTransitionUs() : rxPowerSavingDefaultTransitionUs();
|
||||
}
|
||||
bool normalizeRxPowerSavingConfig(RxPowerSavingConfig* config, uint8_t sf, float bw,
|
||||
float capture_cost_symbols = rxPowerSavingDefaultCaptureCost(),
|
||||
uint32_t transition_us = rxPowerSavingDefaultTransitionUs());
|
||||
bool applyRxPowerSavingConfig(RxPowerSavingConfig* config, uint8_t sf, float bw,
|
||||
RxPowerSavingControl* control);
|
||||
static constexpr int16_t RX_POWERSAVING_ERR_TIMING = -10001;
|
||||
@@ -0,0 +1,120 @@
|
||||
#include "RXPowerSavingCLI.h"
|
||||
#include "../CLICommandUtils.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char* rxPowerSavingLevelTag(uint8_t level) {
|
||||
if (isRxPowerSavingRiskyWorkingMaxLevel(level)) return "(riskyWorkingMax)";
|
||||
if (isRxPowerSavingOverdriveLevel(level)) return "(overdrive)";
|
||||
return "";
|
||||
}
|
||||
|
||||
bool RXPowerSavingCLI::set(const char* value, uint8_t sf, float bw,
|
||||
RxPowerSavingConfig* config, RxPowerSavingControl* control,
|
||||
char* reply, size_t reply_size) {
|
||||
if (!config || !reply || !reply_size) return false;
|
||||
mesh::cli::RxPowerSavingArguments args = {};
|
||||
if (!mesh::cli::parseRxPowerSavingArgumentsStrict(value, args)) {
|
||||
snprintf(reply, reply_size, "ERROR: use off|on|conservative|balanced|max|overdrive|riskyWorkingMax|level <1-10>|<rx_us> <sleep_us>");
|
||||
return false;
|
||||
}
|
||||
RxPowerSavingConfig proposed = *config;
|
||||
using Mode = mesh::cli::RxPowerSavingArgumentMode;
|
||||
if (args.mode == Mode::Off) {
|
||||
proposed.enabled = 0;
|
||||
} else if (args.mode == Mode::Manual) {
|
||||
proposed.enabled = 1;
|
||||
proposed.level = 0;
|
||||
proposed.preamble = 0;
|
||||
proposed.rx_us = args.rx_us;
|
||||
proposed.sleep_us = args.sleep_us;
|
||||
} else {
|
||||
proposed.enabled = 1;
|
||||
proposed.level = args.mode == Mode::Conservative ? RX_POWERSAVING_CONSERVATIVE_LEVEL
|
||||
: args.mode == Mode::Balanced ? RX_POWERSAVING_BALANCED_LEVEL : args.level;
|
||||
const uint32_t preamble = args.mode == Mode::Level ? args.preamble : RX_POWERSAVING_PROFILE_PREAMBLE;
|
||||
if (args.level > RX_POWERSAVING_RISKY_WORKING_MAX_LEVEL ||
|
||||
proposed.level < 1 || proposed.level > RX_POWERSAVING_RISKY_WORKING_MAX_LEVEL ||
|
||||
(preamble != 0 && (preamble > 128 || !isRxPowerSavingPreamble(preamble)))) {
|
||||
snprintf(reply, reply_size, "ERROR: level range is 1-10; preamble is 16,32,64,128");
|
||||
return false;
|
||||
}
|
||||
proposed.preamble = static_cast<uint8_t>(preamble);
|
||||
}
|
||||
if (proposed.enabled && !normalizeRxPowerSavingConfig(&proposed, sf, bw,
|
||||
rxPowerSavingCaptureCost(control), rxPowerSavingTransition(control))) {
|
||||
snprintf(reply, reply_size, "ERROR: invalid RXPS timing or SF/BW");
|
||||
return false;
|
||||
}
|
||||
if (control ? !control->setRxPowerSaving(proposed.enabled != 0, proposed.rx_us, proposed.sleep_us)
|
||||
: proposed.enabled != 0) {
|
||||
snprintf(reply, reply_size, "ERROR: RX powersaving unsupported or radio busy");
|
||||
return false;
|
||||
}
|
||||
if (control) control->setRxPowerSavingConfigError(false);
|
||||
*config = proposed;
|
||||
snprintf(reply, reply_size, "OK - %s,level=%lu%s,preamble=%lu,rx=%lu,sleep=%lu",
|
||||
config->enabled ? "on" : "off", (unsigned long)config->level,
|
||||
rxPowerSavingLevelTag(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,
|
||||
uint8_t sf, float bw, char* reply, size_t reply_size,
|
||||
uint16_t wire_preamble) {
|
||||
if (config == nullptr) return;
|
||||
get(*config, control ? control->getRxPowerSavingStatus() : RxPowerSavingStatus{},
|
||||
sf, bw, reply, reply_size, wire_preamble);
|
||||
}
|
||||
|
||||
void RXPowerSavingCLI::get(const RxPowerSavingConfig& config,
|
||||
const RxPowerSavingStatus& status,
|
||||
uint8_t sf, float bw, char* reply, size_t reply_size,
|
||||
uint16_t wire_preamble) {
|
||||
if (reply == nullptr || reply_size == 0) return;
|
||||
// Preamble symbols the node actually catches. Not simply the number the level
|
||||
// asked for: the sleep floor can shorten the sleep further, and at SF6 with
|
||||
// the 16-symbol profile that collapses the first few levels onto the same
|
||||
// point. Reporting the requested value there would be a lie.
|
||||
char margin[24];
|
||||
margin[0] = 0;
|
||||
if (status.supported && status.armed && status.last_error == 0 && isValidRxPowerSavingPeriod(config.sleep_us) &&
|
||||
config.level >= 1 && config.level <= RX_POWERSAVING_GUARDED_LEVELS &&
|
||||
sf >= 5 && sf <= 12 && bw > 0.0f && bw <= 500.0f) {
|
||||
const uint16_t preamble = wire_preamble ? wire_preamble : rxPowerSavingPreambleForParams(sf, bw);
|
||||
const float symbol_us = (1000.0f * (float)(1UL << sf)) / bw;
|
||||
const float effective = (float)preamble - (float)config.sleep_us / symbol_us;
|
||||
// No %f on this platform's printf, and rounding the whole and fractional
|
||||
// parts separately overflows: 4.999 came out as "4.10".
|
||||
const float scaled = effective * 10.0f + (effective < 0.0f ? -0.5f : 0.5f);
|
||||
// Validated 30-second periods, SF5+ and these radio-family bandwidths
|
||||
// bound the result well inside int32, including a 65535-symbol override.
|
||||
const int tenths = (int)scaled;
|
||||
snprintf(margin, sizeof(margin), ",catch=%d.%d", tenths / 10,
|
||||
tenths < 0 ? -(tenths % 10) : tenths % 10);
|
||||
}
|
||||
|
||||
// Overdrive has to be visible here, otherwise it reads as an ordinary level
|
||||
// and nobody remembers it runs outside the datasheet rule.
|
||||
int len = snprintf(reply, reply_size,
|
||||
"> desired=%s,effective=%s,supported=%s,err=%d,fail=%lu,level=%lu%s%s,preamble=%lu,rx=%lu,sleep=%lu",
|
||||
config.enabled ? "on" : "off", status.armed ? "armed" : "continuous",
|
||||
status.supported ? "yes" : "no", (int)status.last_error,
|
||||
(unsigned long)status.arm_failures, (unsigned long)config.level,
|
||||
isRxPowerSavingUnguardedLevel(config.level) ? "(!)" : "", margin,
|
||||
(unsigned long)config.preamble, (unsigned long)config.rx_us,
|
||||
(unsigned long)config.sleep_us);
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#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);
|
||||
// sf/bw are needed to turn the stored periods back into symbols, which is
|
||||
// what the level scale is expressed in.
|
||||
static void get(const RxPowerSavingConfig* config, const RxPowerSavingControl* control,
|
||||
uint8_t sf, float bw, char* reply, size_t reply_size,
|
||||
uint16_t wire_preamble = 0);
|
||||
static void get(const RxPowerSavingConfig& config, const RxPowerSavingStatus& status,
|
||||
uint8_t sf, float bw, char* reply, size_t reply_size,
|
||||
uint16_t wire_preamble = 0);
|
||||
};
|
||||
@@ -588,6 +588,8 @@ mesh::RadioParamApplyResult RadioLibWrapper::trySetParams(float freq, float bw,
|
||||
else _rx_ps_enabled = true;
|
||||
_rx_ps_rx_us = rx_ps_timings[0];
|
||||
_rx_ps_sleep_us = rx_ps_timings[1];
|
||||
_rx_ps_arm_retry.reset();
|
||||
_rx_ps_last_error = RADIOLIB_ERR_NONE;
|
||||
_rx_ps_continuous_fallback = rxPowerSavingUsesContinuousFallback(
|
||||
_rx_ps_rx_us, _rx_ps_sleep_us);
|
||||
}
|
||||
@@ -2440,7 +2442,7 @@ void RadioLibWrapper::startRecv() {
|
||||
// plus a worst-case packet airtime, plus margin for TCXO/transitions.
|
||||
// Floored at 60s so a light-sleeping MCU (ESP32 wakes every ~30s) opens
|
||||
// an observation window every couple of wakeups instead of on each one.
|
||||
uint32_t rx_ms = _rx_ps_rx_us / 1000, sleep_ms = _rx_ps_sleep_us / 1000;
|
||||
uint32_t rx_ms = _rx_ps_eff_rx_us / 1000, sleep_ms = _rx_ps_eff_sleep_us / 1000;
|
||||
_wd_stuck_thresh = (rx_ms + sleep_ms) + 2 * (2 * rx_ms + sleep_ms)
|
||||
+ getEstAirtimeFor(MAX_TRANS_UNIT) + 1000;
|
||||
if (_wd_stuck_thresh < 60000) _wd_stuck_thresh = 60000;
|
||||
@@ -2464,14 +2466,65 @@ void RadioLibWrapper::startRecv() {
|
||||
}
|
||||
|
||||
int RadioLibWrapper::startReceiveMode() {
|
||||
return _radio->startReceive();
|
||||
return startContinuousReceive();
|
||||
}
|
||||
|
||||
int RadioLibWrapper::startReceivePowerSaving() {
|
||||
if (_nf_calib_active && _rx_ps_armed && isPacketPendingOrReceiving()) {
|
||||
return RADIOLIB_ERR_NONE;
|
||||
}
|
||||
if (_rx_ps_armed) stopReceiveDutyCycle();
|
||||
if (!_rx_ps_enabled || _nf_calib_active) {
|
||||
if (!_rx_ps_enabled) _rx_ps_continuous_fallback = false;
|
||||
const int16_t err = startContinuousReceive();
|
||||
if (err != RADIOLIB_ERR_NONE || !_rx_ps_enabled) _rx_ps_last_error = err;
|
||||
return err;
|
||||
}
|
||||
const bool planned_continuous = rxPowerSavingUsesContinuousFallback(_rx_ps_rx_us, _rx_ps_sleep_us);
|
||||
const bool invalid_timing = !planned_continuous
|
||||
&& !canStartRxPowerSavingDutyCycle(_rx_ps_rx_us, _rx_ps_sleep_us, rxPowerSavingTransitionUs());
|
||||
if (planned_continuous || invalid_timing || !_rx_ps_arm_retry.canAttempt()) {
|
||||
_rx_ps_continuous_fallback = true;
|
||||
if (invalid_timing) _rx_ps_last_error = RX_POWERSAVING_ERR_TIMING;
|
||||
const int16_t err = startContinuousReceive();
|
||||
if (err != RADIOLIB_ERR_NONE) _rx_ps_last_error = err;
|
||||
return err;
|
||||
}
|
||||
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 effective_rx_us = _rx_ps_rx_us, effective_sleep_us = _rx_ps_sleep_us;
|
||||
const int16_t err = armDutyCycle(irq_flags, irq_mask, &effective_rx_us, &effective_sleep_us);
|
||||
_rx_ps_last_error = err;
|
||||
if (err == RADIOLIB_ERR_NONE) {
|
||||
_rx_ps_armed = true;
|
||||
_rx_ps_continuous_fallback = false;
|
||||
_rx_ps_eff_rx_us = effective_rx_us;
|
||||
_rx_ps_eff_sleep_us = effective_sleep_us;
|
||||
_rx_ps_arm_retry.recordSuccess();
|
||||
return err;
|
||||
}
|
||||
_rx_ps_armed = false;
|
||||
_rx_ps_continuous_fallback = true;
|
||||
_rx_ps_eff_rx_us = _rx_ps_eff_sleep_us = 0;
|
||||
++_rx_ps_arm_failures;
|
||||
_rx_ps_arm_retry.recordFailure();
|
||||
MESH_DEBUG_PRINTLN("RadioLibWrapper: startReceiveDutyCycle(%d), continuous RX fallback", err);
|
||||
stopDutyCycleHardware();
|
||||
const int16_t fallback_error = startContinuousReceive();
|
||||
if (fallback_error != RADIOLIB_ERR_NONE) _rx_ps_last_error = fallback_error;
|
||||
return fallback_error;
|
||||
}
|
||||
|
||||
void RadioLibWrapper::stopReceiveDutyCycle() {
|
||||
// The duty-cycle sequencer only stops on RxDone or an explicit standby;
|
||||
// issuing other mode commands while it runs leads to undefined behaviour.
|
||||
_radio->standby();
|
||||
const int16_t err = stopDutyCycleHardware();
|
||||
_rx_ps_armed = false;
|
||||
_rx_ps_eff_rx_us = _rx_ps_eff_sleep_us = 0;
|
||||
if (err != RADIOLIB_ERR_NONE) _rx_ps_last_error = err;
|
||||
}
|
||||
|
||||
bool RadioLibWrapper::isPacketReady() {
|
||||
@@ -2547,12 +2600,17 @@ bool RadioLibWrapper::setRxPowerSaving(bool enabled, uint32_t rx_us, uint32_t sl
|
||||
if (_beacon_owned) return false;
|
||||
#endif
|
||||
if (_cw_active) return false;
|
||||
if (enabled && (!isValidRxPowerSavingPeriod(rx_us) || !isValidRxPowerSavingPeriod(sleep_us))) {
|
||||
return false;
|
||||
}
|
||||
// A rejected request must not become the policy restored after radio2 ends.
|
||||
if (enabled && !supportsRxPowerSaving()) return false;
|
||||
if (_profile_rxps_suspended) {
|
||||
_profile_saved_rxps = enabled;
|
||||
_rx_ps_rx_us = rx_us;
|
||||
_rx_ps_sleep_us = sleep_us;
|
||||
_rx_ps_arm_retry.reset();
|
||||
_rx_ps_last_error = RADIOLIB_ERR_NONE;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2562,12 +2620,25 @@ bool RadioLibWrapper::setRxPowerSaving(bool enabled, uint32_t rx_us, uint32_t sl
|
||||
_rx_ps_enabled = enabled;
|
||||
_rx_ps_rx_us = rx_us;
|
||||
_rx_ps_sleep_us = sleep_us;
|
||||
_rx_ps_arm_retry.reset();
|
||||
_rx_ps_last_error = RADIOLIB_ERR_NONE;
|
||||
_rx_ps_continuous_fallback = enabled
|
||||
&& rxPowerSavingUsesContinuousFallback(rx_us, sleep_us);
|
||||
endReconfigure(resume_rx);
|
||||
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 = _rx_ps_arm_failures;
|
||||
status.effective_rx_us = _rx_ps_armed ? _rx_ps_eff_rx_us : 0;
|
||||
status.effective_sleep_us = _rx_ps_armed ? _rx_ps_eff_sleep_us : 0;
|
||||
return status;
|
||||
}
|
||||
|
||||
int RadioLibWrapper::recvRaw(uint8_t* bytes, int sz) {
|
||||
#if MESH_RADIO_TRACE_ENABLED
|
||||
auto& radio_trace = mesh::radiotrace::trace();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "NoiseFloorEstimator.h"
|
||||
#include "../TelemetryHistoryConfig.h"
|
||||
#include "../CarrierTools.h"
|
||||
@@ -35,6 +37,8 @@ struct PacketMillis {
|
||||
};
|
||||
|
||||
class RadioLibWrapper : public mesh::Radio {
|
||||
public:
|
||||
static constexpr bool SupportsRxPowerSaving = false;
|
||||
protected:
|
||||
// Mode is owned by this loop, not by the ISR or another wrapper.
|
||||
uint8_t state = 0;
|
||||
@@ -228,6 +232,12 @@ protected:
|
||||
unsigned long _nf_last_calib; // millis of last completed/attempted window
|
||||
unsigned long _nf_calib_deadline; // abort window if the batch can't complete
|
||||
unsigned long _nf_sample_from; // no samples before this (RX entry settle)
|
||||
// Append diagnostic state so existing radio hot fields retain their offsets.
|
||||
uint32_t _rx_ps_eff_rx_us = 0;
|
||||
uint32_t _rx_ps_eff_sleep_us = 0;
|
||||
int16_t _rx_ps_last_error = RADIOLIB_ERR_NONE;
|
||||
uint32_t _rx_ps_arm_failures = 0;
|
||||
RxPowerSavingArmRetryState _rx_ps_arm_retry;
|
||||
// The T096 SX1262 bench test found RSSI valid and stable by 7 ms after RX
|
||||
// entry, including the 500 kHz/SF5 worst-case profile. Retain the
|
||||
// conservative 20 ms path for the other radio families.
|
||||
@@ -318,7 +328,15 @@ protected:
|
||||
int16_t performChannelScanWithTimeout(unsigned long timeout_ms);
|
||||
#endif
|
||||
virtual int startReceiveMode();
|
||||
int startReceivePowerSaving();
|
||||
virtual void stopReceiveDutyCycle();
|
||||
virtual int16_t armDutyCycle(RadioLibIrqFlags_t irq_flags, RadioLibIrqFlags_t irq_mask,
|
||||
uint32_t* effective_rx_us, uint32_t* effective_sleep_us) {
|
||||
(void)irq_flags; (void)irq_mask; (void)effective_rx_us; (void)effective_sleep_us;
|
||||
return RADIOLIB_ERR_UNSUPPORTED;
|
||||
}
|
||||
virtual int16_t stopDutyCycleHardware() { return _radio->standby(); }
|
||||
virtual int16_t startContinuousReceive() { return _radio->startReceive(); }
|
||||
virtual bool isPacketReady();
|
||||
// true while the chip cannot service SPI (RX duty-cycle sleep window, or
|
||||
// briefly while processing a command); radios expose this via the BUSY pin
|
||||
@@ -526,6 +544,13 @@ public:
|
||||
void onSendFinished() override;
|
||||
bool isInRecvMode() const override;
|
||||
bool setRxPowerSaving(bool enabled, uint32_t rx_us, uint32_t sleep_us) override;
|
||||
bool supportsRxPowerSaving() const override { return SupportsRxPowerSaving; }
|
||||
RxPowerSavingStatus getRxPowerSavingStatus() const;
|
||||
virtual uint32_t rxPowerSavingTransitionUs() const { return rxPowerSavingDefaultTransitionUs(); }
|
||||
void getRxPowerSavingTimings(uint32_t& rx_us, uint32_t& sleep_us) const {
|
||||
rx_us = _rx_ps_rx_us;
|
||||
sleep_us = _rx_ps_sleep_us;
|
||||
}
|
||||
bool isRxPowerSavingContinuousFallback() const override {
|
||||
return _rx_ps_enabled && _rx_ps_continuous_fallback;
|
||||
}
|
||||
@@ -639,7 +664,10 @@ public:
|
||||
bool getReceiveRetryDelay(uint32_t now, uint32_t& delay_ms) const override;
|
||||
bool getNoiseFloorWakeDelay(uint32_t now, uint32_t& delay_ms) const override;
|
||||
bool getProfileIdleDelay(uint32_t& delay_ms) const override;
|
||||
void resetStats() { n_recv = n_sent = n_recv_errors = 0; }
|
||||
void resetStats() {
|
||||
n_recv = n_sent = n_recv_errors = _rx_ps_arm_failures = 0;
|
||||
_rx_ps_arm_retry.reset();
|
||||
}
|
||||
|
||||
uint8_t getRadioState() const override;
|
||||
unsigned long getLastRecvMillis() const override { return last_recv_millis; }
|
||||
|
||||
Reference in new issue
Block a user