From a8f64c3cc826bd775350457a81c985ff2e2ca78e Mon Sep 17 00:00:00 2001 From: geeksville Date: Thu, 30 Apr 2020 21:11:03 -0700 Subject: [PATCH] make a custom version fo rf95 class, so we can can deal with chips that have bad version codes. --- src/rf95/RF95Interface.cpp | 19 +++-------- src/rf95/RadioLibRF95.cpp | 48 +++++++++++++++++++++++++++ src/rf95/RadioLibRF95.h | 67 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 src/rf95/RadioLibRF95.cpp create mode 100644 src/rf95/RadioLibRF95.h diff --git a/src/rf95/RF95Interface.cpp b/src/rf95/RF95Interface.cpp index 8076046ef..0fe04477e 100644 --- a/src/rf95/RF95Interface.cpp +++ b/src/rf95/RF95Interface.cpp @@ -1,6 +1,6 @@ #include "RF95Interface.h" #include "MeshRadio.h" // kinda yucky, but we need to know which region we are in - +#include "RadioLibRF95.h" #include RF95Interface::RF95Interface(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, SPIClass &spi) @@ -18,20 +18,9 @@ bool RF95Interface::init() if (power > 20) // This chip has lower power limits than some power = 20; - int res; - /** - * We do a nasty check on freq range to figure our RFM96 vs RFM95 - * - */ - if (CH0 < 530.0) { - auto dev = new RFM96(&module); - iface = lora = dev; - res = dev->begin(freq, bw, sf, cr, syncWord, power, currentLimit, preambleLength); - } else { - auto dev = new RFM95(&module); - iface = lora = dev; - res = dev->begin(freq, bw, sf, cr, syncWord, power, currentLimit, preambleLength); - } + auto dev = new RadioLibRF95(&module); + iface = lora = dev; + int res = dev->begin(freq, bw, sf, cr, syncWord, power, currentLimit, preambleLength); DEBUG_MSG("LORA init result %d\n", res); if (res == ERR_NONE) diff --git a/src/rf95/RadioLibRF95.cpp b/src/rf95/RadioLibRF95.cpp new file mode 100644 index 000000000..6ea982f01 --- /dev/null +++ b/src/rf95/RadioLibRF95.cpp @@ -0,0 +1,48 @@ +#include "RadioLibRF95.h" + +#define RFM95_CHIP_VERSION 0x12 +#define RFM95_ALT_VERSION 0x11 // Supposedly some versions of the chip have id 0x11 + +RadioLibRF95::RadioLibRF95(Module *mod) : SX1278(mod) {} + +int16_t RadioLibRF95::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint8_t currentLimit, + uint16_t preambleLength, uint8_t gain) +{ + // execute common part + int16_t state = SX127x::begin(RFM95_CHIP_VERSION, syncWord, currentLimit, preambleLength); + if (state != ERR_NONE) + state = SX127x::begin(RFM95_ALT_VERSION, syncWord, currentLimit, preambleLength); + RADIOLIB_ASSERT(state); + + // configure settings not accessible by API + state = config(); + RADIOLIB_ASSERT(state); + + // configure publicly accessible settings + state = setFrequency(freq); + RADIOLIB_ASSERT(state); + + state = setBandwidth(bw); + RADIOLIB_ASSERT(state); + + state = setSpreadingFactor(sf); + RADIOLIB_ASSERT(state); + + state = setCodingRate(cr); + RADIOLIB_ASSERT(state); + + state = setOutputPower(power); + RADIOLIB_ASSERT(state); + + state = setGain(gain); + + return (state); +} + +int16_t RadioLibRF95::setFrequency(float freq) +{ + // RADIOLIB_CHECK_RANGE(freq, 862.0, 1020.0, ERR_INVALID_FREQUENCY); + + // set frequency + return (SX127x::setFrequencyRaw(freq)); +} diff --git a/src/rf95/RadioLibRF95.h b/src/rf95/RadioLibRF95.h new file mode 100644 index 000000000..a36683370 --- /dev/null +++ b/src/rf95/RadioLibRF95.h @@ -0,0 +1,67 @@ +#pragma once +#include + +/*! + \class RFM95 + + \brief Derived class for %RFM95 modules. Overrides some methods from SX1278 due to different parameter ranges. +*/ +class RadioLibRF95: public SX1278 { + public: + + // constructor + + /*! + \brief Default constructor. Called from Arduino sketch when creating new LoRa instance. + + \param mod Instance of Module that will be used to communicate with the %LoRa chip. + */ + RadioLibRF95(Module* mod); + + // basic methods + + /*! + \brief %LoRa modem initialization method. Must be called at least once from Arduino sketch to initialize the module. + + \param freq Carrier frequency in MHz. Allowed values range from 868.0 MHz to 915.0 MHz. + + \param bw %LoRa link bandwidth in kHz. Allowed values are 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 250 and 500 kHz. + + \param sf %LoRa link spreading factor. Allowed values range from 6 to 12. + + \param cr %LoRa link coding rate denominator. Allowed values range from 5 to 8. + + \param syncWord %LoRa sync word. Can be used to distinguish different networks. Note that value 0x34 is reserved for LoRaWAN networks. + + \param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm. + + \param currentLimit Trim value for OCP (over current protection) in mA. Can be set to multiplies of 5 in range 45 to 120 mA and to multiples of 10 in range 120 to 240 mA. + Set to 0 to disable OCP (not recommended). + + \param preambleLength Length of %LoRa transmission preamble in symbols. The actual preamble length is 4.25 symbols longer than the set number. + Allowed values range from 6 to 65535. + + \param gain Gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain. + Set to 0 to enable automatic gain control (recommended). + + \returns \ref status_codes + */ + int16_t begin(float freq = 915.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = SX127X_SYNC_WORD, int8_t power = 17, uint8_t currentLimit = 100, uint16_t preambleLength = 8, uint8_t gain = 0); + + // configuration methods + + /*! + \brief Sets carrier frequency. Allowed values range from 868.0 MHz to 915.0 MHz. + + \param freq Carrier frequency to be set in MHz. + + \returns \ref status_codes + */ + int16_t setFrequency(float freq); + +#ifndef RADIOLIB_GODMODE + private: +#endif + +}; +