mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-11-21 22:47:05 +00:00
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.
170 lines
4.3 KiB
C++
170 lines
4.3 KiB
C++
#include <Arduino.h>
|
|
#include <target.h>
|
|
#include <helpers/ArduinoHelpers.h>
|
|
#include <helpers/IdentityStore.h>
|
|
#include "KissModem.h"
|
|
|
|
#if defined(NRF52_PLATFORM)
|
|
#include <InternalFileSystem.h>
|
|
#elif defined(RP2040_PLATFORM)
|
|
#include <LittleFS.h>
|
|
#elif defined(ESP32)
|
|
#include <SPIFFS.h>
|
|
#else
|
|
#include <InternalFileSystem.h>
|
|
#endif
|
|
|
|
#if defined(KISS_UART_RX) && defined(KISS_UART_TX)
|
|
#include <HardwareSerial.h>
|
|
#endif
|
|
|
|
#define NOISE_FLOOR_CALIB_INTERVAL_MS 2000
|
|
#define USB_TX_TIMEOUT_MS 50
|
|
#define USB_TX_BUFFER_SIZE 1024
|
|
|
|
StdRNG rng;
|
|
mesh::LocalIdentity identity;
|
|
KissModem* modem;
|
|
static uint32_t next_noise_floor_calib_ms = 0;
|
|
|
|
void halt() {
|
|
while (1) ;
|
|
}
|
|
|
|
void loadOrCreateIdentity() {
|
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
|
InternalFS.begin();
|
|
IdentityStore store(InternalFS, "");
|
|
#elif defined(ESP32)
|
|
SPIFFS.begin(true);
|
|
IdentityStore store(SPIFFS, "/identity");
|
|
#elif defined(RP2040_PLATFORM)
|
|
LittleFS.begin();
|
|
IdentityStore store(LittleFS, "/identity");
|
|
store.begin();
|
|
#else
|
|
#error "Filesystem not defined"
|
|
#endif
|
|
|
|
if (!store.load("_main", identity)) {
|
|
identity = radio_new_identity();
|
|
while (identity.pub_key[0] == 0x00 || identity.pub_key[0] == 0xFF) {
|
|
identity = radio_new_identity();
|
|
}
|
|
store.save("_main", identity);
|
|
}
|
|
}
|
|
|
|
void onSetRadio(float freq, float bw, uint8_t sf, uint8_t cr) {
|
|
radio_driver.setParams(freq, bw, sf, cr);
|
|
}
|
|
|
|
void onSetTxPower(uint8_t power) {
|
|
radio_driver.setTxPower(power);
|
|
}
|
|
|
|
float onGetCurrentRssi() {
|
|
return radio_driver.getCurrentRSSI();
|
|
}
|
|
|
|
void onGetStats(uint32_t* rx, uint32_t* tx, uint32_t* errors) {
|
|
*rx = radio_driver.getPacketsRecv();
|
|
*tx = radio_driver.getPacketsSent();
|
|
*errors = radio_driver.getPacketsRecvErrors();
|
|
}
|
|
|
|
void pollRadioRx() {
|
|
uint8_t rx_buf[256];
|
|
int rx_len = radio_driver.recvRaw(rx_buf, sizeof(rx_buf));
|
|
if (rx_len > 0) {
|
|
int8_t snr = (int8_t)(radio_driver.getLastSNR() * 4);
|
|
int8_t rssi = (int8_t)radio_driver.getLastRSSI();
|
|
modem->onPacketReceived(snr, rssi, rx_buf, rx_len);
|
|
}
|
|
}
|
|
|
|
bool onSetRxBoostedGain(bool enable) {
|
|
return radio_driver.setRxBoostedGainMode(enable);
|
|
}
|
|
|
|
bool onGetRxBoostedGain() {
|
|
return radio_driver.getRxBoostedGainMode();
|
|
}
|
|
|
|
void setup() {
|
|
board.begin();
|
|
|
|
if (!radio_init()) {
|
|
halt();
|
|
}
|
|
|
|
radio_driver.begin();
|
|
|
|
rng.begin(radio_driver.getRngSeed());
|
|
loadOrCreateIdentity();
|
|
|
|
sensors.begin();
|
|
|
|
#if defined(KISS_UART_RX) && defined(KISS_UART_TX)
|
|
#if defined(ESP32)
|
|
Serial1.setPins(KISS_UART_RX, KISS_UART_TX);
|
|
Serial1.begin(115200);
|
|
#elif defined(NRF52_PLATFORM)
|
|
((Uart *)&Serial1)->setPins(KISS_UART_RX, KISS_UART_TX);
|
|
Serial1.begin(115200);
|
|
#elif defined(RP2040_PLATFORM)
|
|
((SerialUART *)&Serial1)->setRX(KISS_UART_RX);
|
|
((SerialUART *)&Serial1)->setTX(KISS_UART_TX);
|
|
Serial1.begin(115200);
|
|
#elif defined(STM32_PLATFORM)
|
|
((HardwareSerial *)&Serial1)->setRx(KISS_UART_RX);
|
|
((HardwareSerial *)&Serial1)->setTx(KISS_UART_TX);
|
|
Serial1.begin(115200);
|
|
#else
|
|
#error "KISS UART not supported on this platform"
|
|
#endif
|
|
modem = new KissModem(Serial1, identity, rng, radio_driver, board, sensors);
|
|
#else
|
|
Serial.begin(115200);
|
|
uint32_t start = millis();
|
|
while (!Serial && millis() - start < 3000) delay(10);
|
|
delay(100);
|
|
#if defined(ESP32) && defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT
|
|
Serial.setTxTimeoutMs(USB_TX_TIMEOUT_MS);
|
|
#if defined(ARDUINO_USB_MODE) && ARDUINO_USB_MODE
|
|
// HWCDC only; TinyUSB's USBCDC TX buffer is fixed at build time
|
|
Serial.setTxBufferSize(USB_TX_BUFFER_SIZE);
|
|
#endif
|
|
#endif
|
|
modem = new KissModem(Serial, identity, rng, radio_driver, board, sensors);
|
|
#endif
|
|
|
|
modem->setRadioCallback(onSetRadio);
|
|
modem->setTxPowerCallback(onSetTxPower);
|
|
modem->setGetCurrentRssiCallback(onGetCurrentRssi);
|
|
modem->setGetStatsCallback(onGetStats);
|
|
modem->setPollRxCallback(pollRadioRx);
|
|
if (radio_driver.supportsRxBoostedGain()) {
|
|
modem->setRxBoostedGainCallbacks(onSetRxBoostedGain, onGetRxBoostedGain);
|
|
}
|
|
modem->begin();
|
|
|
|
board.onBootComplete();
|
|
}
|
|
|
|
void loop() {
|
|
modem->loop();
|
|
|
|
if (!modem->isActuallyTransmitting() && !modem->isHostOutputBackedUp()) {
|
|
pollRadioRx();
|
|
}
|
|
|
|
board.loop();
|
|
|
|
if ((uint32_t)(millis() - next_noise_floor_calib_ms) >= NOISE_FLOOR_CALIB_INTERVAL_MS) {
|
|
radio_driver.triggerNoiseFloorCalibrate(0);
|
|
next_noise_floor_calib_ms = millis();
|
|
}
|
|
radio_driver.loop();
|
|
}
|