Add SetRxBoostedGain (0x20) / GetRxBoostedGain (0x21, reply 0xA1) and capability bit 3, the KISS equivalent of the radio.rxgain CLI setting. It is separate from FEM RX gain; a board may support either or both. - Support is declared by the driver via a new RadioLibWrapper::supportsRxBoostedGain() (SX1262, SX1268, LLCC68, LR1110, LR1121, LR2021), so nothing is written to the radio at boot. - The set is refused with TxBusy during TX or while host output is backed up. Otherwise any completed RX packet is delivered first, because some drivers (LR2021) drop to standby to apply the gain. - The reply always carries the state read back from the radio. Driver fixes found along the way, which also affect the radio.rxgain CLI in regular firmware: - CustomLR2021 never updated its cached gain state, so the getter always reported off. It now tracks the applied level per RX path and rolls RadioLib's cached level back if a write fails. - CustomLR1110/LR1121 cached the requested state even when the write failed. - lr11x0ResetAGC only reapplied the gain when RX_BOOSTED_GAIN was defined, so a runtime setting was lost on the next AGC reset.
107 lines
3.5 KiB
C++
107 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include <Mesh.h>
|
|
#include <RadioLib.h>
|
|
|
|
#ifdef USE_CC310_HW_CRYPTO
|
|
#include <Adafruit_nRFCrypto.h>
|
|
#endif
|
|
struct PacketMillis {
|
|
uint32_t preambleMillis; // preamble-detect -> header-valid deadline
|
|
uint32_t payloadMillis; // header-valid -> rx-done deadline
|
|
};
|
|
|
|
class RadioLibWrapper : public mesh::Radio {
|
|
protected:
|
|
PhysicalLayer* _radio;
|
|
mesh::MainBoard* _board;
|
|
uint32_t n_recv, n_sent, n_recv_errors;
|
|
int16_t _noise_floor, _threshold;
|
|
bool _cad_enabled;
|
|
uint16_t _num_floor_samples;
|
|
int32_t _floor_sample_sum;
|
|
uint8_t _preamble_sf;
|
|
|
|
void idle();
|
|
void startRecv();
|
|
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; }
|
|
|
|
void begin() override;
|
|
virtual void powerOff() { _radio->sleep(); }
|
|
int recvRaw(uint8_t* bytes, int sz) override;
|
|
uint32_t getEstAirtimeFor(int len_bytes) override;
|
|
bool startSendRaw(const uint8_t* bytes, int len) override;
|
|
bool isSendComplete() override;
|
|
void onSendFinished() override;
|
|
bool isInRecvMode() const override;
|
|
bool isChannelActive();
|
|
|
|
bool isReceiving() override {
|
|
if (isReceivingPacket()) return true;
|
|
|
|
return isChannelActive();
|
|
}
|
|
|
|
virtual void setParams(float freq, float bw, uint8_t sf, uint8_t cr) = 0;
|
|
uint32_t getRngSeed();
|
|
void setTxPower(int8_t dbm);
|
|
|
|
virtual float getCurrentRSSI() =0;
|
|
virtual uint8_t getSpreadingFactor() const { return LORA_SF; }
|
|
static uint16_t preambleLengthForSF(uint8_t sf) { return sf <= 8 ? 32 : 16; }
|
|
void updatePreamble(uint8_t sf) { _preamble_sf = sf; _radio->setPreambleLength(preambleLengthForSF(sf)); }
|
|
PacketMillis calcMaxPacketMillis(uint8_t sf, float bw, uint8_t cr, uint8_t preambleSymbols);
|
|
virtual int16_t performChannelScan();
|
|
|
|
int getNoiseFloor() const override { return _noise_floor; }
|
|
void triggerNoiseFloorCalibrate(int threshold) override;
|
|
void setCADEnabled(bool enable) override { _cad_enabled = enable; }
|
|
void resetAGC() override;
|
|
|
|
void loop() override;
|
|
|
|
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; }
|
|
|
|
virtual float getLastRSSI() const override;
|
|
virtual float getLastSNR() const override;
|
|
|
|
float packetScore(float snr, int packet_len) override { return packetScoreInt(snr, 10, packet_len); } // assume sf=10
|
|
|
|
virtual bool supportsRxBoostedGain() const { return false; }
|
|
virtual bool setRxBoostedGainMode(bool) { return false; }
|
|
virtual bool getRxBoostedGainMode() const { return false; }
|
|
|
|
virtual bool configSideDetectors(const uint8_t sideDetSFs[], uint8_t num, float bw) { return false; }
|
|
};
|
|
|
|
/**
|
|
* \brief an RNG impl using the noise from the LoRa radio as entropy.
|
|
* NOTE: this is VERY SLOW! Use only for things like creating new LocalIdentity
|
|
*/
|
|
class RadioNoiseListener : public mesh::RNG {
|
|
PhysicalLayer* _radio;
|
|
public:
|
|
RadioNoiseListener(PhysicalLayer& radio): _radio(&radio) { }
|
|
|
|
void random(uint8_t* dest, size_t sz) override {
|
|
#ifdef USE_CC310_HW_CRYPTO
|
|
nRFCrypto.Random.generate(dest, (uint16_t)sz);
|
|
for (int i = 0; i < sz; i++) {
|
|
dest[i] ^= _radio->randomByte() ^ (::random(0, 256) & 0xFF); // combine with Radio's entropy
|
|
}
|
|
#else
|
|
for (int i = 0; i < sz; i++) {
|
|
dest[i] = _radio->randomByte() ^ (::random(0, 256) & 0xFF);
|
|
}
|
|
#endif
|
|
}
|
|
};
|