From 5e2ca8aed4239f6682dede26485c63fcb2fdd521 Mon Sep 17 00:00:00 2001 From: Tom <116762865+NomDeTom@users.noreply.github.com> Date: Fri, 8 May 2026 02:24:39 +0100 Subject: [PATCH] LR2021 radio on NRF_Promicro (#10401) * LR2021 radio on NRF_Promicro Co-authored-by: Copilot * Refactor LR2021 interface includes and conditional compilation for improved clarity Co-authored-by: Copilot * Refactor LR20x0 interface: remove unused includes and update comments for clarity * Fix LR2021 max power definitions and add radio type detection tests * remove potato radio type detection tests * Include placeholder for DCDC - currently requires godmode * Added godmode features - not enabled by default --------- Co-authored-by: Copilot Co-authored-by: Ben Meadors --- src/detect/LoRaRadioType.h | 3 +- src/mesh/InterfacesTemplates.cpp | 7 + src/mesh/LR11x0Interface.cpp | 24 +- src/mesh/LR2021Interface.cpp | 18 + src/mesh/LR2021Interface.h | 15 + src/mesh/LR20x0Interface.cpp | 399 ++++++++++++++++++ src/mesh/LR20x0Interface.h | 75 ++++ src/mesh/RadioInterface.cpp | 15 + .../nrf52_promicro_diy_tcxo/platformio.ini | 1 + .../diy/nrf52_promicro_diy_tcxo/rfswitch.h | 43 +- .../diy/nrf52_promicro_diy_tcxo/variant.h | 10 + 11 files changed, 596 insertions(+), 14 deletions(-) create mode 100644 src/mesh/LR2021Interface.cpp create mode 100644 src/mesh/LR2021Interface.h create mode 100644 src/mesh/LR20x0Interface.cpp create mode 100644 src/mesh/LR20x0Interface.h diff --git a/src/detect/LoRaRadioType.h b/src/detect/LoRaRadioType.h index a059a3668..f741b929c 100644 --- a/src/detect/LoRaRadioType.h +++ b/src/detect/LoRaRadioType.h @@ -11,7 +11,8 @@ enum LoRaRadioType { SX1280_RADIO, LR1110_RADIO, LR1120_RADIO, - LR1121_RADIO + LR1121_RADIO, + LR2021_RADIO }; extern LoRaRadioType radioType; \ No newline at end of file diff --git a/src/mesh/InterfacesTemplates.cpp b/src/mesh/InterfacesTemplates.cpp index 57abbf0ee..907cc2a4e 100644 --- a/src/mesh/InterfacesTemplates.cpp +++ b/src/mesh/InterfacesTemplates.cpp @@ -1,5 +1,9 @@ +#include "configuration.h" + #include "LR11x0Interface.cpp" #include "LR11x0Interface.h" +#include "LR20x0Interface.cpp" +#include "LR20x0Interface.h" #include "SX126xInterface.cpp" #include "SX126xInterface.h" #include "SX128xInterface.cpp" @@ -21,6 +25,9 @@ template class LR11x0Interface; template class LR11x0Interface; template class LR11x0Interface; #endif +#if defined(USE_LR2021) && RADIOLIB_EXCLUDE_LR2021 != 1 +template class LR20x0Interface; +#endif #ifdef ARCH_STM32WL template class SX126xInterface; #endif diff --git a/src/mesh/LR11x0Interface.cpp b/src/mesh/LR11x0Interface.cpp index 441cab833..1d1616ed6 100644 --- a/src/mesh/LR11x0Interface.cpp +++ b/src/mesh/LR11x0Interface.cpp @@ -57,16 +57,19 @@ template bool LR11x0Interface::init() #if ARCH_PORTDUINO float tcxoVoltage = (float)portduino_config.dio3_tcxo_voltage / 1000; // FIXME: correct logic to default to not using TCXO if no voltage is specified for LR11x0_DIO3_TCXO_VOLTAGE -#elif !defined(LR11X0_DIO3_TCXO_VOLTAGE) +#elif defined(LR11X0_DIO3_TCXO_VOLTAGE) + float tcxoVoltage = LR11X0_DIO3_TCXO_VOLTAGE; + LOG_DEBUG("LR11X0_DIO3_TCXO_VOLTAGE defined, using DIO3 as TCXO reference voltage at %f V", LR11X0_DIO3_TCXO_VOLTAGE); + // (DIO3 is not free to be used as an IRQ) +#elif defined(TCXO_OPTIONAL) + float tcxoVoltage = 1.6f; // TCXO_OPTIONAL: try default 1.6 V first, fall back to XTAL on failure + LOG_DEBUG("TCXO_OPTIONAL: no LR11X0_DIO3_TCXO_VOLTAGE defined, trying default TCXO Vref 1.6 V first"); +#else float tcxoVoltage = 0; // "TCXO reference voltage to be set on DIO3. Defaults to 1.6 V, set to 0 to skip." per // https://github.com/jgromes/RadioLib/blob/690a050ebb46e6097c5d00c371e961c1caa3b52e/src/modules/LR11x0/LR11x0.h#L471C26-L471C104 // (DIO3 is free to be used as an IRQ) LOG_DEBUG("LR11X0_DIO3_TCXO_VOLTAGE not defined, not using DIO3 as TCXO reference voltage"); -#else - float tcxoVoltage = LR11X0_DIO3_TCXO_VOLTAGE; - LOG_DEBUG("LR11X0_DIO3_TCXO_VOLTAGE defined, using DIO3 as TCXO reference voltage at %f V", LR11X0_DIO3_TCXO_VOLTAGE); - // (DIO3 is not free to be used as an IRQ) #endif RadioLibInterface::init(); @@ -101,6 +104,17 @@ template bool LR11x0Interface::init() res = lora.begin(getFreq(), bw, sf, cr, syncWord, power, preambleLength, tcxoVoltage); } +#if defined(TCXO_OPTIONAL) + // If init failed for any reason other than chip not found, retry without TCXO (XTAL mode) + if (res != RADIOLIB_ERR_NONE && res != RADIOLIB_ERR_CHIP_NOT_FOUND && tcxoVoltage > 0) { + LOG_WARN("LR11x0 init failed with TCXO Vref %f V (err %d), retrying without TCXO", tcxoVoltage, res); + tcxoVoltage = 0; + res = lora.begin(getFreq(), bw, sf, cr, syncWord, power, preambleLength, tcxoVoltage); + if (res == RADIOLIB_ERR_NONE) + LOG_INFO("LR11x0 init success without TCXO (XTAL mode)"); + } +#endif + // \todo Display actual typename of the adapter, not just `LR11x0` LOG_INFO("LR11x0 init result %d", res); if (res == RADIOLIB_ERR_CHIP_NOT_FOUND || res == RADIOLIB_ERR_SPI_CMD_FAILED) diff --git a/src/mesh/LR2021Interface.cpp b/src/mesh/LR2021Interface.cpp new file mode 100644 index 000000000..9aa4d5f1a --- /dev/null +++ b/src/mesh/LR2021Interface.cpp @@ -0,0 +1,18 @@ +#include "configuration.h" + +#if defined(USE_LR2021) && RADIOLIB_EXCLUDE_LR2021 != 1 + +#include "LR2021Interface.h" +#include "error.h" + +LR2021Interface::LR2021Interface(LockingArduinoHal *hal, RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, + RADIOLIB_PIN_TYPE busy) + : LR20x0Interface(hal, cs, irq, rst, busy) +{ +} + +bool LR2021Interface::wideLora() +{ + return true; +} +#endif diff --git a/src/mesh/LR2021Interface.h b/src/mesh/LR2021Interface.h new file mode 100644 index 000000000..52c04ee90 --- /dev/null +++ b/src/mesh/LR2021Interface.h @@ -0,0 +1,15 @@ +#pragma once +#if RADIOLIB_EXCLUDE_LR2021 != 1 +#include "LR20x0Interface.h" + +/** + * Our adapter for LR2021 radios + */ +class LR2021Interface : public LR20x0Interface +{ + public: + LR2021Interface(LockingArduinoHal *hal, RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, + RADIOLIB_PIN_TYPE busy); + bool wideLora() override; +}; +#endif diff --git a/src/mesh/LR20x0Interface.cpp b/src/mesh/LR20x0Interface.cpp new file mode 100644 index 000000000..699663cf0 --- /dev/null +++ b/src/mesh/LR20x0Interface.cpp @@ -0,0 +1,399 @@ +#include "configuration.h" + +#if defined(USE_LR2021) && RADIOLIB_EXCLUDE_LR2021 != 1 +#include "LR20x0Interface.h" +#include "error.h" +#include "mesh/NodeDB.h" + +// Keep LR20x0 naming while RadioLib exposes LR2021 symbols. +#ifndef LR20x0 +#define LR20x0 LR2021 +#endif + +#ifdef LR2021_DIO_AS_RF_SWITCH +#include "rfswitch.h" +#elif ARCH_PORTDUINO +#include "PortduinoGlue.h" +#define lr20x0_rfswitch_dio_pins portduino_config.rfswitch_dio_pins +#define lr20x0_rfswitch_table portduino_config.rfswitch_table +#else +static const uint32_t lr20x0_rfswitch_dio_pins[] = {RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC}; +static const Module::RfSwitchMode_t lr20x0_rfswitch_table[] = { + {LR20x0::MODE_STBY, {}}, {LR20x0::MODE_RX, {}}, {LR20x0::MODE_TX, {}}, + {LR20x0::MODE_RX_HF, {}}, {LR20x0::MODE_TX_HF, {}}, END_OF_MODE_TABLE, +}; +#endif + +// Particular boards might define a different max power based on what their hardware can do, default to max power output if not +// specified (may be dangerous if using external PA and LR20x0 power config forgotten) +#if ARCH_PORTDUINO +#define LR2021_MAX_POWER portduino_config.lr2021_max_power +#endif +#ifndef LR2021_MAX_POWER +#define LR2021_MAX_POWER 22 +#endif + +// the 2.4G part maxes at 12dBm + +#if ARCH_PORTDUINO +#define LR2021_MAX_POWER_HF portduino_config.lr2021_max_power_hf +#endif +#ifndef LR2021_MAX_POWER_HF +#define LR2021_MAX_POWER_HF 12 +#endif + +template +LR20x0Interface::LR20x0Interface(LockingArduinoHal *hal, RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, + RADIOLIB_PIN_TYPE busy) + : RadioLibInterface(hal, cs, irq, rst, busy, &lora), lora(&module) +{ + LOG_WARN("LR20x0Interface(cs=%d, irq=%d, rst=%d, busy=%d)", cs, irq, rst, busy); +} + +/// Initialise the Driver transport hardware and software. +/// Make sure the Driver is properly configured before calling init(). +/// \return true if initialisation succeeded. +template bool LR20x0Interface::init() +{ +#ifdef LR2021_POWER_EN + pinMode(LR2021_POWER_EN, OUTPUT); + digitalWrite(LR2021_POWER_EN, HIGH); +#endif + +#if ARCH_PORTDUINO + float tcxoVoltage = (float)portduino_config.dio3_tcxo_voltage / 1000; +// FIXME: correct logic to default to not using TCXO if no voltage is specified for LR20x0_DIO3_TCXO_VOLTAGE +#elif defined(LR2021_DIO3_TCXO_VOLTAGE) + float tcxoVoltage = LR2021_DIO3_TCXO_VOLTAGE; + LOG_DEBUG("LR2021_DIO3_TCXO_VOLTAGE defined, using DIO3 as TCXO reference voltage at %f V", LR2021_DIO3_TCXO_VOLTAGE); + // (DIO3 is not free to be used as an IRQ) +#elif defined(TCXO_OPTIONAL) + float tcxoVoltage = 1.6f; // TCXO_OPTIONAL: try default 1.6 V first, fall back to XTAL on failure + LOG_DEBUG("TCXO_OPTIONAL: no LR2021_DIO3_TCXO_VOLTAGE defined, trying default TCXO Vref 1.6 V first"); +#else + float tcxoVoltage = + 0; // "TCXO reference voltage to be set on DIO3. Defaults to 1.6 V, set to 0 to skip." per + // https://github.com/jgromes/RadioLib/blob/690a050ebb46e6097c5d00c371e961c1caa3b52e/src/modules/LR11x0/LR11x0.h#L471C26-L471C104 + // (DIO3 is free to be used as an IRQ) + LOG_DEBUG("LR2021_DIO3_TCXO_VOLTAGE not defined, not using DIO3 as TCXO reference voltage"); +#endif + + RadioLibInterface::init(); + +#ifdef LR2021_IRQ_DIO_NUM + lora.irqDioNum = LR2021_IRQ_DIO_NUM; + LOG_DEBUG("Set irqDioNum %d", lora.irqDioNum); +#elif defined(IRQ_DIO_NUM) + lora.irqDioNum = IRQ_DIO_NUM; + LOG_DEBUG("Set irqDioNum %d", lora.irqDioNum); +#else + LOG_DEBUG("Use default irqDioNum %d", lora.irqDioNum); +#endif + + if (config.lora.region == meshtastic_Config_LoRaConfig_RegionCode_LORA_24) { // clamp if wide freq range + limitPower(LR2021_MAX_POWER_HF); + } else { + limitPower(LR2021_MAX_POWER); // default clamp for non-wide freq range + } + +#ifdef LR2021_RF_SWITCH_SUBGHZ + pinMode(LR2021_RF_SWITCH_SUBGHZ, OUTPUT); + digitalWrite(LR2021_RF_SWITCH_SUBGHZ, getFreq() < 1e9 ? HIGH : LOW); + LOG_DEBUG("Set RF0 switch to %s", getFreq() < 1e9 ? "SubGHz" : "2.4GHz"); +#endif + +#ifdef LR2021_RF_SWITCH_2_4GHZ + pinMode(LR2021_RF_SWITCH_2_4GHZ, OUTPUT); + digitalWrite(LR2021_RF_SWITCH_2_4GHZ, getFreq() < 1e9 ? LOW : HIGH); + LOG_DEBUG("Set RF1 switch to %s", getFreq() < 1e9 ? "SubGHz" : "2.4GHz"); +#endif + + // Allow extra time for TCXO to stabilize after power-on + delay(10); + + int res = lora.begin(getFreq(), bw, sf, cr, syncWord, power, preambleLength, tcxoVoltage); + + // Retry if we get SPI command failed - some units need extra TCXO stabilization time + if (res == RADIOLIB_ERR_SPI_CMD_FAILED) { + LOG_WARN("LR20x0 init failed with %d (SPI_CMD_FAILED), retrying after delay...", res); + delay(100); + res = lora.begin(getFreq(), bw, sf, cr, syncWord, power, preambleLength, tcxoVoltage); + } + +#if defined(TCXO_OPTIONAL) + // If init failed for any reason other than chip not found, retry without TCXO (XTAL mode) + if (res != RADIOLIB_ERR_NONE && res != RADIOLIB_ERR_CHIP_NOT_FOUND && tcxoVoltage > 0) { + LOG_WARN("LR20x0 init failed with TCXO Vref %f V (err %d), retrying without TCXO", tcxoVoltage, res); + tcxoVoltage = 0; + res = lora.begin(getFreq(), bw, sf, cr, syncWord, power, preambleLength, tcxoVoltage); + if (res == RADIOLIB_ERR_NONE) + LOG_INFO("LR20x0 init success without TCXO (XTAL mode)"); + } +#endif + + // \todo Display actual typename of the adapter, not just `LR20x0` + LOG_INFO("LR20x0 init result %d", res); + if (res == RADIOLIB_ERR_CHIP_NOT_FOUND || res == RADIOLIB_ERR_SPI_CMD_FAILED) + return false; + + // Some basic info about the module's explicit firmware version - no other info available + // Currently requires radiolib godmode + +#if RADIOLIB_GODMODE + uint8_t fwMajor = 0; + uint8_t fwMinor = 0; + int versionRes = lora.getVersion(&fwMajor, &fwMinor); + if (versionRes == RADIOLIB_ERR_NONE) + LOG_DEBUG("LR20x0 FW %d.%d", fwMajor, fwMinor); +#endif + + LOG_INFO("Frequency set to %f", getFreq()); + LOG_INFO("Bandwidth set to %f", bw); + LOG_INFO("Power output set to %d", power); + + if (res == RADIOLIB_ERR_NONE) + res = lora.setCRC(2); + + // Standard DCDC ramp timing from RadioLib workarounds (register 0x00F20024) + // Currently requires radiolib godmode +#if RADIOLIB_GODMODE + if (res == RADIOLIB_ERR_NONE) { + uint8_t rampTimes[4] = {15, 15, 15, 15}; // Standard case for all conditions + res = lora.setRegMode(RADIOLIB_LR2021_REG_MODE_SIMO_NORMAL, rampTimes); + if (res != RADIOLIB_ERR_NONE) + LOG_WARN("LR2021 setRegMode failed: %d", res); + } +#endif + +#ifdef LR2021_DIO_AS_RF_SWITCH + bool dioAsRfSwitch = true; +#elif defined(ARCH_PORTDUINO) + bool dioAsRfSwitch = portduino_config.has_rfswitch_table; +#else + bool dioAsRfSwitch = false; +#endif + + if (dioAsRfSwitch) { + lora.setRfSwitchTable(lr20x0_rfswitch_dio_pins, lr20x0_rfswitch_table); + LOG_DEBUG("Set DIO RF switch"); + } + + if (res == RADIOLIB_ERR_NONE) { + if (config.lora.sx126x_rx_boosted_gain) { // the name is unfortunate but historically accurate + res = lora.setRxBoostedGainMode(true); + LOG_INFO("Set RX gain to boosted mode; result: %d", res); + } else { + res = lora.setRxBoostedGainMode(false); + LOG_INFO("Set RX gain to power saving mode (boosted mode off); result: %d", res); + } + } + + if (res == RADIOLIB_ERR_NONE) + startReceive(); // start receiving + + return res == RADIOLIB_ERR_NONE; +} + +template bool LR20x0Interface::reconfigure() +{ + RadioLibInterface::reconfigure(); + + // set mode to standby + setStandby(); + + // configure publicly accessible settings + int err = lora.setSpreadingFactor(sf); + if (err != RADIOLIB_ERR_NONE) + RECORD_CRITICALERROR(meshtastic_CriticalErrorCode_INVALID_RADIO_SETTING); + + err = lora.setBandwidth(bw); // different form than LR11xx + if (err != RADIOLIB_ERR_NONE) + RECORD_CRITICALERROR(meshtastic_CriticalErrorCode_INVALID_RADIO_SETTING); + + err = lora.setCodingRate(cr, cr != 7); // use long interleaving except if CR is 4/7 which doesn't support it + if (err != RADIOLIB_ERR_NONE) + RECORD_CRITICALERROR(meshtastic_CriticalErrorCode_INVALID_RADIO_SETTING); + + err = lora.setSyncWord(syncWord); + assert(err == RADIOLIB_ERR_NONE); + + if (config.lora.region == meshtastic_Config_LoRaConfig_RegionCode_LORA_24) { // clamp if wide freq range + limitPower(LR2021_MAX_POWER_HF); + } else { + limitPower(LR2021_MAX_POWER); // default clamp for non-wide freq range + } + + err = lora.setPreambleLength(preambleLength); + assert(err == RADIOLIB_ERR_NONE); + + err = lora.setFrequency(getFreq()); + if (err != RADIOLIB_ERR_NONE) + RECORD_CRITICALERROR(meshtastic_CriticalErrorCode_INVALID_RADIO_SETTING); + + err = lora.setOutputPower(power); + assert(err == RADIOLIB_ERR_NONE); + + // Apply RX gain mode — valid in STDBY, matches resetAGC() pattern + err = lora.setRxBoostedGainMode(config.lora.sx126x_rx_boosted_gain); + if (err != RADIOLIB_ERR_NONE) + LOG_WARN("LR20x0 setRxBoostedGainMode %s%d", radioLibErr, err); + + startReceive(); // restart receiving + + return true; +} + +template void LR20x0Interface::disableInterrupt() +{ + lora.clearIrqAction(); +} + +template void LR20x0Interface::setStandby() +{ + checkNotification(); // handle any pending interrupts before we force standby + + int err = lora.standby(); + + if (err != RADIOLIB_ERR_NONE) { + LOG_DEBUG("LR20x0 standby failed with error %d", err); + } + + assert(err == RADIOLIB_ERR_NONE); + + isReceiving = false; // If we were receiving, not any more + activeReceiveStart = 0; + disableInterrupt(); + completeSending(); // If we were sending, not anymore + RadioLibInterface::setStandby(); +} + +/** + * Add SNR data to received messages + */ +template void LR20x0Interface::addReceiveMetadata(meshtastic_MeshPacket *mp) +{ + // LOG_DEBUG("PacketStatus %x", lora.getPacketStatus()); + mp->rx_snr = lora.getSNR(); + mp->rx_rssi = lround(lora.getRSSI()); + // LOG_DEBUG("Corrected frequency offset: %f", lora.getFrequencyError()); // not implemented for LR20x0, but noop for LR11x0 + // too(!) +} + +/** We override to turn on transmitter power as needed. + */ +template void LR20x0Interface::configHardwareForSend() +{ + RadioLibInterface::configHardwareForSend(); +} + +// For power draw measurements, helpful to force radio to stay sleeping +// #define SLEEP_ONLY + +template void LR20x0Interface::startReceive() +{ +#ifdef SLEEP_ONLY + sleep(); +#else + + setStandby(); + + lora.setPreambleLength(preambleLength); // Solve RX ack fail after direct message sent. Not sure why this is needed. + + // We use a 16 bit preamble so this should save some power by letting radio sit in standby mostly. + int err = + lora.startReceive(RADIOLIB_LR2021_RX_TIMEOUT_INF, MESHTASTIC_RADIOLIB_IRQ_RX_FLAGS, RADIOLIB_IRQ_RX_DEFAULT_MASK, 0); + if (err) + LOG_ERROR("StartReceive error: %d", err); + assert(err == RADIOLIB_ERR_NONE); + + RadioLibInterface::startReceive(); + + // Must be done AFTER starting receive, because startReceive clears (possibly stale) interrupt pending register bits + enableInterrupt(isrRxLevel0); + checkRxDoneIrqFlag(); +#endif +} + +/** Is the channel currently active? */ +template bool LR20x0Interface::isChannelActive() +{ + // check if we can detect a LoRa preamble on the current channel + ChannelScanConfig_t cfg = {.cad = {.symNum = NUM_SYM_CAD, + .detPeak = RADIOLIB_LR2021_CAD_PARAM_DEFAULT, + .detMin = RADIOLIB_LR2021_CAD_PARAM_DEFAULT, + .exitMode = RADIOLIB_LR2021_CAD_PARAM_DEFAULT, + .timeout = 0, + .irqFlags = RADIOLIB_IRQ_CAD_DEFAULT_FLAGS, + .irqMask = RADIOLIB_IRQ_CAD_DEFAULT_MASK}}; + int16_t result; + + setStandby(); + result = lora.scanChannel(cfg); + if (result == RADIOLIB_LORA_DETECTED) + return true; + + assert(result != RADIOLIB_ERR_WRONG_MODEM); + + return false; +} + +/** Could we send right now (i.e. either not actively receiving or transmitting)? */ +template bool LR20x0Interface::isActivelyReceiving() +{ + // The IRQ status will be cleared when we start our read operation. Check if we've started a header, but haven't yet + // received and handled the interrupt for reading the packet/handling errors. + return receiveDetected(lora.getIrqStatus(), RADIOLIB_LR2021_IRQ_LORA_HEADER_VALID, RADIOLIB_LR2021_IRQ_PREAMBLE_DETECTED); +} + +#ifdef LR20X0_AGC_RESET +template void LR20x0Interface::resetAGC() +{ + // Safety: don't reset mid-packet + if (sendingPacket != NULL || (isReceiving && isActivelyReceiving())) + return; + + LOG_DEBUG("LR20x0 AGC reset: warm sleep + Calibrate(0x3F)"); + + // 1. Warm sleep — powers down the analog frontend, resetting AGC state + lora.sleep(true, 0); + + // 2. Wake to RC standby for stable calibration + lora.standby(RADIOLIB_LR20X0_STANDBY_RC, true); + + // 3. Calibrate all blocks (PLL, ADC, image, RC oscillators) + // calibrate() is protected on LR20x0, so use raw SPI (same as internal implementation) + uint8_t calData = RADIOLIB_LR20X0_CALIBRATE_ALL; + module.SPIwriteStream(RADIOLIB_LR20X0_CMD_CALIBRATE, &calData, 1, true, true); + + // 4. Re-calibrate image rejection for actual operating frequency + // Calibrate(0x3F) defaults to 902-928 MHz which is wrong for other regions. + lora.calibrateImageRejection(getFreq() - 4.0f, getFreq() + 4.0f); + + // 5. Re-apply RX boosted gain mode + lora.setRxBoostedGainMode(config.lora.sx126x_rx_boosted_gain); + + // 6. Resume receiving + startReceive(); +} +#endif + +template bool LR20x0Interface::sleep() +{ + // \todo Display actual typename of the adapter, not just `LR20x0` + LOG_DEBUG("LR20x0 entering sleep mode"); + setStandby(); // Stop any pending operations + + // turn off TCXO if it was powered + lora.setTCXO(0); + + // put chipset into sleep mode (we've already disabled interrupts by now) + bool keepConfig = false; + lora.sleep(keepConfig, 0); // Note: we do not keep the config, full reinit will be needed + +#ifdef LR2021_POWER_EN + digitalWrite(LR2021_POWER_EN, LOW); +#endif + + return true; +} +#endif diff --git a/src/mesh/LR20x0Interface.h b/src/mesh/LR20x0Interface.h new file mode 100644 index 000000000..6176cd0b8 --- /dev/null +++ b/src/mesh/LR20x0Interface.h @@ -0,0 +1,75 @@ +#pragma once +#if RADIOLIB_EXCLUDE_LR2021 != 1 +#include "RadioLibInterface.h" + +/** + * \brief Adapter for LR20x0 radio family. Implements common logic for child classes. + * \tparam T RadioLib module type for LR20x0, e.g. LR2021. + */ +template class LR20x0Interface : public RadioLibInterface +{ + public: + LR20x0Interface(LockingArduinoHal *hal, RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, + RADIOLIB_PIN_TYPE busy); + + /// Initialise the Driver transport hardware and software. + /// Make sure the Driver is properly configured before calling init(). + /// \return true if initialisation succeeded. + virtual bool init() override; + + /// Apply any radio provisioning changes + /// Make sure the Driver is properly configured before calling init(). + /// \return true if initialisation succeeded. + virtual bool reconfigure() override; + + /// Prepare hardware for sleep. Call this _only_ for deep sleep, not needed for light sleep. + virtual bool sleep() override; + + bool isIRQPending() override { return lora.getIrqFlags() != 0; } + +#ifdef LR20X0_AGC_RESET + void resetAGC() override; +#endif + + protected: + /** + * Specific module instance + */ + T lora; + + /** + * Glue functions called from ISR land + */ + virtual void disableInterrupt() override; + + /** + * Enable a particular ISR callback glue function + */ + virtual void enableInterrupt(void (*callback)()) { lora.setIrqAction(callback); } + + /** can we detect a LoRa preamble on the current channel? */ + virtual bool isChannelActive() override; + + /** are we actively receiving a packet (only called during receiving state) */ + virtual bool isActivelyReceiving() override; + + /** + * Start waiting to receive a message + */ + virtual void startReceive() override; + + /** + * We override to turn on transmitter power as needed. + */ + virtual void configHardwareForSend() override; + + /** + * Add SNR data to received messages + */ + virtual void addReceiveMetadata(meshtastic_MeshPacket *mp) override; + + virtual void setStandby() override; + + uint32_t getPacketTime(uint32_t pl, bool received) override { return computePacketTime(lora, pl, received); } +}; +#endif diff --git a/src/mesh/RadioInterface.cpp b/src/mesh/RadioInterface.cpp index b7b672d8b..f4f25f80c 100644 --- a/src/mesh/RadioInterface.cpp +++ b/src/mesh/RadioInterface.cpp @@ -5,6 +5,7 @@ #include "LR1110Interface.h" #include "LR1120Interface.h" #include "LR1121Interface.h" +#include "LR2021Interface.h" #include "MeshRadio.h" #include "MeshService.h" #include "NodeDB.h" @@ -478,6 +479,20 @@ std::unique_ptr initLoRa() } #endif +#if defined(USE_LR2021) && RADIOLIB_EXCLUDE_LR2021 != 1 + if (!rIf) { + rIf = std::unique_ptr( + new LR2021Interface(loraHal, LR2021_SPI_NSS_PIN, LR2021_IRQ_PIN, LR2021_NRESET_PIN, LR2021_BUSY_PIN)); + if (!rIf->init()) { + LOG_WARN("No LR2021 radio"); + rIf = nullptr; + } else { + LOG_INFO("LR2021 init success"); + radioType = LR2021_RADIO; + } + } +#endif + #if defined(USE_SX1280) && RADIOLIB_EXCLUDE_SX128X != 1 if (!rIf) { rIf = std::unique_ptr(new SX1280Interface(loraHal, SX128X_CS, SX128X_DIO1, SX128X_RESET, SX128X_BUSY)); diff --git a/variants/nrf52840/diy/nrf52_promicro_diy_tcxo/platformio.ini b/variants/nrf52840/diy/nrf52_promicro_diy_tcxo/platformio.ini index 82a4e1953..d111b011f 100644 --- a/variants/nrf52840/diy/nrf52_promicro_diy_tcxo/platformio.ini +++ b/variants/nrf52840/diy/nrf52_promicro_diy_tcxo/platformio.ini @@ -15,6 +15,7 @@ board = promicro-nrf52840 build_flags = ${nrf52840_base.build_flags} -I variants/nrf52840/diy/nrf52_promicro_diy_tcxo -D NRF52_PROMICRO_DIY + ; -D RADIOLIB_GODMODE=1 ; needed for some LR2021 items, but not enabled by default. build_src_filter = ${nrf52_base.build_src_filter} +<../variants/nrf52840/diy/nrf52_promicro_diy_tcxo> debug_tool = jlink diff --git a/variants/nrf52840/diy/nrf52_promicro_diy_tcxo/rfswitch.h b/variants/nrf52840/diy/nrf52_promicro_diy_tcxo/rfswitch.h index ac7ef57c4..714c58c24 100644 --- a/variants/nrf52840/diy/nrf52_promicro_diy_tcxo/rfswitch.h +++ b/variants/nrf52840/diy/nrf52_promicro_diy_tcxo/rfswitch.h @@ -1,5 +1,11 @@ +#pragma once #include "RadioLib.h" +// Keep LR20x0 naming while RadioLib exposes LR2021 symbols. +#ifndef LR20x0 +#define LR20x0 LR2021 +#endif + // This is rewritten to match the requirements of the E80-900M2213S // The E80 does not conform to the reference Semtech switches(!) and therefore needs a custom matrix. // See footnote #3 in "https://www.cdebyte.com/products/E80-900M2213S/2#Pin" @@ -13,14 +19,35 @@ static const uint32_t rfswitch_dio_pins[] = {RADIOLIB_LR11X0_DIO5, RADIOLIB_LR11 static const Module::RfSwitchMode_t rfswitch_table[] = { // clang-format off - // mode DIO5 DIO6 DIO7 - {LR11x0::MODE_STBY, {LOW, LOW, LOW}}, - {LR11x0::MODE_RX, {LOW, HIGH, LOW}}, - {LR11x0::MODE_TX, {HIGH, HIGH, LOW}}, - {LR11x0::MODE_TX_HP, {HIGH, LOW, LOW}}, - {LR11x0::MODE_TX_HF, {LOW, LOW, LOW}}, - {LR11x0::MODE_GNSS, {LOW, LOW, HIGH}}, - {LR11x0::MODE_WIFI, {LOW, LOW, LOW}}, + // mode DIO5 DIO6 DIO7 + {LR11x0::MODE_STBY, {LOW, LOW, LOW}}, + {LR11x0::MODE_RX, {LOW, HIGH, LOW}}, + {LR11x0::MODE_TX, {HIGH, HIGH, LOW}}, + {LR11x0::MODE_TX_HP, {HIGH, LOW, LOW}}, + {LR11x0::MODE_TX_HF, {LOW, LOW, LOW}}, + {LR11x0::MODE_GNSS, {LOW, LOW, HIGH}}, + {LR11x0::MODE_WIFI, {LOW, LOW, LOW}}, + END_OF_MODE_TABLE, + // clang-format on +}; + +// LR2021 RF switch matrix following the standard Semtech / Seeed T1000-E reference topology. +// DIO5 -> antenna path select (HIGH = sub-GHz LF) +// DIO6 -> TX enable / HP PA select +// DIO7 -> not connected (no GNSS on LR2021) +// DIO8 -> RF front-end power enable + +static const uint32_t lr20x0_rfswitch_dio_pins[] = {RADIOLIB_LR2021_DIO5, RADIOLIB_LR2021_DIO6, RADIOLIB_LR2021_DIO7, + RADIOLIB_LR2021_DIO8, RADIOLIB_NC}; + +static const Module::RfSwitchMode_t lr20x0_rfswitch_table[] = { + // clang-format off + // mode DIO5 DIO6 DIO7 DIO8 + {LR20x0::MODE_STBY, {LOW, LOW, LOW, LOW}}, + {LR20x0::MODE_RX, {HIGH, LOW, LOW, HIGH}}, + {LR20x0::MODE_TX, {HIGH, HIGH, LOW, HIGH}}, + {LR20x0::MODE_RX_HF, {LOW, LOW, LOW, LOW}}, + {LR20x0::MODE_TX_HF, {LOW, LOW, LOW, LOW}}, END_OF_MODE_TABLE, // clang-format on }; diff --git a/variants/nrf52840/diy/nrf52_promicro_diy_tcxo/variant.h b/variants/nrf52840/diy/nrf52_promicro_diy_tcxo/variant.h index 8e10141f5..82178d321 100644 --- a/variants/nrf52840/diy/nrf52_promicro_diy_tcxo/variant.h +++ b/variants/nrf52840/diy/nrf52_promicro_diy_tcxo/variant.h @@ -155,6 +155,16 @@ NRF52 PRO MICRO PIN ASSIGNMENT #define LR11X0_DIO_AS_RF_SWITCH #endif +// LR2021 +#define USE_LR2021 +#define LR2021_IRQ_PIN (0 + 10) // P0.10 IRQ +#define LR2021_NRESET_PIN LORA_RESET // P0.09 NRST +#define LR2021_BUSY_PIN (0 + 29) // P0.29 BUSY +#define LR2021_SPI_NSS_PIN LORA_CS // P1.13 +#define LR2021_DIO3_TCXO_VOLTAGE 1.8 +#define LR2021_DIO_AS_RF_SWITCH +#define LR2021_IRQ_DIO_NUM 9 // DIO9 → P0.10 + // #define SX126X_MAX_POWER 8 set this if using a high-power board! /*