From bd2bfd682235c8b0e4df46b2586fc8c2a8c25757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Sun, 27 Nov 2022 14:03:50 +0100 Subject: [PATCH] update board definition, update copy/paste errors, fix SX1280. --- src/main.cpp | 26 ++++++++++++++++++- src/mesh/InterfacesTemplates.cpp | 3 --- src/mesh/RadioInterface.h | 2 ++ src/mesh/SX1280Interface.cpp | 4 --- src/mesh/SX1280Interface.h | 3 --- src/mesh/SX128xInterface.cpp | 44 +++++++++++++++++--------------- src/mesh/SX128xInterface.h | 12 ++++----- variants/tlora_v2_1_16/variant.h | 5 ---- variants/tlora_v2_1_18/variant.h | 12 ++------- 9 files changed, 59 insertions(+), 52 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 98f22ecf1..692837ad8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -387,7 +387,7 @@ void setup() } #endif -#if defined(USE_SX1280) && !defined(ARCH_PORTDUINO) +#if defined(USE_SX1280) if (!rIf) { rIf = new SX1280Interface(SX128X_CS, SX128X_DIO1, SX128X_RESET, SX128X_BUSY, SPI); if (!rIf->init()) { @@ -452,6 +452,30 @@ void setup() } #endif +// check if the radio chip matches the selected region + +if((config.lora.region == Config_LoRaConfig_RegionCode_LORA_24) && (!rIf->wideLora())){ + DEBUG_MSG("Warning: Radio chip does not support 2.4GHz LoRa. Reverting to unset.\n"); + config.lora.region = Config_LoRaConfig_RegionCode_UNSET; + nodeDB.saveToDisk(SEGMENT_CONFIG); + if(!rIf->reconfigure()) { + DEBUG_MSG("Reconfigure failed, rebooting\n"); + screen->startRebootScreen(); + rebootAtMsec = millis() + 5000; + } +} + +if((config.lora.region != Config_LoRaConfig_RegionCode_LORA_24) && (rIf->wideLora())){ + DEBUG_MSG("Warning: Radio chip only supports 2.4GHz LoRa. Adjusting Region.\n"); + config.lora.region = Config_LoRaConfig_RegionCode_LORA_24; + nodeDB.saveToDisk(SEGMENT_CONFIG); + if(!rIf->reconfigure()) { + DEBUG_MSG("Reconfigure failed, rebooting\n"); + screen->startRebootScreen(); + rebootAtMsec = millis() + 5000; + } +} + #if HAS_WIFI || HAS_ETHERNET mqttInit(); #endif diff --git a/src/mesh/InterfacesTemplates.cpp b/src/mesh/InterfacesTemplates.cpp index 9602525b5..0d2246428 100644 --- a/src/mesh/InterfacesTemplates.cpp +++ b/src/mesh/InterfacesTemplates.cpp @@ -7,7 +7,4 @@ template class SX126xInterface; template class SX126xInterface; template class SX126xInterface; - -#if defined(RADIOLIB_GODMODE) template class SX128xInterface; -#endif \ No newline at end of file diff --git a/src/mesh/RadioInterface.h b/src/mesh/RadioInterface.h index e9f725c89..437f294a4 100644 --- a/src/mesh/RadioInterface.h +++ b/src/mesh/RadioInterface.h @@ -97,6 +97,8 @@ class RadioInterface */ virtual bool canSleep() { return true; } + virtual bool wideLora() { return false; } + /// Prepare hardware for sleep. Call this _only_ for deep sleep, not needed for light sleep. virtual bool sleep() { return true; } diff --git a/src/mesh/SX1280Interface.cpp b/src/mesh/SX1280Interface.cpp index 97a3febe3..7fc6b45e1 100644 --- a/src/mesh/SX1280Interface.cpp +++ b/src/mesh/SX1280Interface.cpp @@ -2,12 +2,8 @@ #include "SX1280Interface.h" #include "error.h" -#if defined(RADIOLIB_GODMODE) - SX1280Interface::SX1280Interface(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE busy, SPIClass &spi) : SX128xInterface(cs, irq, rst, busy, spi) { } - -#endif \ No newline at end of file diff --git a/src/mesh/SX1280Interface.h b/src/mesh/SX1280Interface.h index a9661501a..190ca3cf4 100644 --- a/src/mesh/SX1280Interface.h +++ b/src/mesh/SX1280Interface.h @@ -6,12 +6,9 @@ * Our adapter for SX1280 radios */ -#if defined(RADIOLIB_GODMODE) class SX1280Interface : public SX128xInterface { public: SX1280Interface(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE busy, SPIClass &spi); }; - -#endif \ No newline at end of file diff --git a/src/mesh/SX128xInterface.cpp b/src/mesh/SX128xInterface.cpp index 36eb0bb94..0a51d618a 100644 --- a/src/mesh/SX128xInterface.cpp +++ b/src/mesh/SX128xInterface.cpp @@ -2,8 +2,6 @@ #include "SX128xInterface.h" #include "error.h" -#if defined(RADIOLIB_GODMODE) - // Particular boards might define a different max power based on what their hardware can do #ifndef SX128X_MAX_POWER #define SX128X_MAX_POWER 13 @@ -27,11 +25,11 @@ bool SX128xInterface::init() pinMode(SX128X_POWER_EN, OUTPUT); #endif -#ifdef SX128X_RXEN // set not rx or tx mode +#if defined(SX128X_RXEN) && (SX128X_RXEN != RADIOLIB_NC) // set not rx or tx mode digitalWrite(SX128X_RXEN, LOW); // Set low before becoming an output pinMode(SX128X_RXEN, OUTPUT); #endif -#ifdef SX128X_TXEN +#if defined(SX128X_TXEN) && (SX128X_TXEN != RADIOLIB_NC) digitalWrite(SX128X_TXEN, LOW); pinMode(SX128X_TXEN, OUTPUT); #endif @@ -46,6 +44,8 @@ bool SX128xInterface::init() limitPower(); + preambleLength = 12; // 12 is the default for this chip, 32 does not RX at all + int res = lora.begin(getFreq(), bw, sf, cr, syncWord, power, preambleLength); // \todo Display actual typename of the adapter, not just `SX128x` DEBUG_MSG("SX128x init result %d\n", res); @@ -54,12 +54,6 @@ bool SX128xInterface::init() DEBUG_MSG("Bandwidth set to %f\n", bw); DEBUG_MSG("Power output set to %d\n", power); -#ifdef SX128X_TXEN - // lora.begin sets Dio2 as RF switch control, which is not true if we are manually controlling RX and TX - if (res == RADIOLIB_ERR_NONE) - res = lora.setDio2AsRfSwitch(true); -#endif - if (res == RADIOLIB_ERR_NONE) res = lora.setCRC(2); @@ -122,18 +116,28 @@ void INTERRUPT_ATTR SX128xInterface::disableInterrupt() lora.clearDio1Action(); } +template +bool SX128xInterface::wideLora() +{ + return true; +} + template void SX128xInterface::setStandby() { checkNotification(); // handle any pending interrupts before we force standby int err = lora.standby(); + + if (err != RADIOLIB_ERR_NONE) + DEBUG_MSG("SX128x standby failed with error %d\n", err); + assert(err == RADIOLIB_ERR_NONE); -#ifdef SX128X_RXEN // we have RXEN/TXEN control - turn off RX and TX power +#if defined(SX128X_RXEN) && (SX128X_RXEN != RADIOLIB_NC) // we have RXEN/TXEN control - turn off RX and TX power digitalWrite(SX128X_RXEN, LOW); #endif -#ifdef SX128X_TXEN +#if defined(SX128X_TXEN) && (SX128X_TXEN != RADIOLIB_NC) digitalWrite(SX128X_TXEN, LOW); #endif @@ -158,10 +162,10 @@ void SX128xInterface::addReceiveMetadata(MeshPacket *mp) template void SX128xInterface::configHardwareForSend() { -#ifdef SX128X_TXEN // we have RXEN/TXEN control - turn on TX power / off RX power +#if defined(SX128X_TXEN) && (SX128X_TXEN != RADIOLIB_NC) // we have RXEN/TXEN control - turn on TX power / off RX power digitalWrite(SX128X_TXEN, HIGH); #endif -#ifdef SX128X_RXEN +#if defined(SX128X_RXEN) && (SX128X_RXEN != RADIOLIB_NC) digitalWrite(SX128X_RXEN, LOW); #endif @@ -180,10 +184,10 @@ void SX128xInterface::startReceive() setStandby(); -#ifdef SX128X_RXEN // we have RXEN/TXEN control - turn on RX power / off TX power +#if defined(SX128X_RXEN) && (SX128X_RXEN != RADIOLIB_NC) // we have RXEN/TXEN control - turn on RX power / off TX power digitalWrite(SX128X_RXEN, HIGH); #endif -#ifdef SX128X_TXEN +#if defined(SX128X_TXEN) && (SX128X_TXEN != RADIOLIB_NC) digitalWrite(SX128X_TXEN, LOW); #endif @@ -219,11 +223,13 @@ bool SX128xInterface::isChannelActive() template bool SX128xInterface::isActivelyReceiving() { - // return isChannelActive(); - +#ifdef RADIOLIB_GODMODE uint16_t irq = lora.getIrqStatus(); bool hasPreamble = (irq & RADIOLIB_SX128X_IRQ_HEADER_VALID); return hasPreamble; +#else + return isChannelActive(); +#endif } template @@ -248,5 +254,3 @@ bool SX128xInterface::sleep() return true; } - -#endif \ No newline at end of file diff --git a/src/mesh/SX128xInterface.h b/src/mesh/SX128xInterface.h index 9994ce850..5a6ed95f9 100644 --- a/src/mesh/SX128xInterface.h +++ b/src/mesh/SX128xInterface.h @@ -1,7 +1,5 @@ #pragma once -#if defined(RADIOLIB_GODMODE) - #include "RadioLibInterface.h" /** @@ -19,6 +17,8 @@ class SX128xInterface : public RadioLibInterface /// \return true if initialisation succeeded. virtual bool init() override; + virtual bool wideLora() override; + /// Apply any radio provisioning changes /// Make sure the Driver is properly configured before calling init(). /// \return true if initialisation succeeded. @@ -27,9 +27,11 @@ class SX128xInterface : public RadioLibInterface /// Prepare hardware for sleep. Call this _only_ for deep sleep, not needed for light sleep. virtual bool sleep() override; - protected: +#ifdef RADIOLIB_GODMODE + bool isIRQPending() override { return lora.getIrqStatus() != 0; } +#endif - float currentLimit = 140; // Higher OCP limit for SX128x PA + protected: /** * Specific module instance @@ -71,5 +73,3 @@ class SX128xInterface : public RadioLibInterface private: }; - -#endif \ No newline at end of file diff --git a/variants/tlora_v2_1_16/variant.h b/variants/tlora_v2_1_16/variant.h index a58b10b81..14175f48b 100644 --- a/variants/tlora_v2_1_16/variant.h +++ b/variants/tlora_v2_1_16/variant.h @@ -3,8 +3,6 @@ #define GPS_RX_PIN 15 // per @der_bear on the forum, 36 is incorrect for this board type and 15 is a better pick #define GPS_TX_PIN 13 -#define EXT_NOTIFY_OUT 2 // Default pin to use for Ext Notify Module. - #define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage // ratio of voltage divider = 2.0 (R42=100k, R43=100k) #define ADC_MULTIPLIER 2.11 // 2.0 + 10% for correction of display undervoltage. @@ -12,9 +10,6 @@ #define I2C_SDA 21 // I2C pins for this board #define I2C_SCL 22 -// #define RESET_OLED 16 // If defined, this pin will be used to reset the display controller. Crashes on newer ESP-IDF and not needed per schematic - -#define VEXT_ENABLE 21 // active low, powers the oled display and the lora antenna boost #define LED_PIN 25 // If defined we will blink this LED #define BUTTON_PIN 12 // If defined, this will be used for user button presses, diff --git a/variants/tlora_v2_1_18/variant.h b/variants/tlora_v2_1_18/variant.h index dd94847be..95d699767 100644 --- a/variants/tlora_v2_1_18/variant.h +++ b/variants/tlora_v2_1_18/variant.h @@ -1,9 +1,5 @@ #undef GPS_RX_PIN #undef GPS_TX_PIN -#define GPS_RX_PIN 15 // per @der_bear on the forum, 36 is incorrect for this board type and 15 is a better pick -#define GPS_TX_PIN 13 - -#define EXT_NOTIFY_OUT 2 // Default pin to use for Ext Notify Module. #define BATTERY_PIN 35 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage // ratio of voltage divider = 2.0 (R42=100k, R43=100k) @@ -20,11 +16,7 @@ #define USE_SX1280 #define LORA_RESET 23 -#define SX128X_CS 18 // FIXME - we really should define LORA_CS instead +#define SX128X_CS 18 #define SX128X_DIO1 26 -#define SX128X_DIO2 33 #define SX128X_BUSY 32 -#define SX128X_RESET LORA_RESET -#define SX128X_E22 // Not really an E22 but TTGO seems to be trying to clone that -// Internally the TTGO module hooks the SX1280-DIO2 in to control the TX/RX switch (which is the default for the sx1280interface -// code) +#define SX128X_RESET LORA_RESET \ No newline at end of file