diff --git a/keywords.txt b/keywords.txt index 36bfb988..4cd39fb4 100644 --- a/keywords.txt +++ b/keywords.txt @@ -124,6 +124,7 @@ setSyncWord KEYWORD2 setOutputPower KEYWORD2 setCurrentLimit KEYWORD2 setPreambleLength KEYWORD2 +invertPreamble KEYWORD2 setGain KEYWORD2 getFrequencyError KEYWORD2 getRSSI KEYWORD2 @@ -221,6 +222,7 @@ spectralScanStart KEYWORD2 spectralScanAbort KEYWORD2 spectralScanGetStatus KEYWORD2 spectralScanGetResult KEYWORD2 +setPaConfig KEYWORD2 # nRF24 setIrqAction KEYWORD2 @@ -289,8 +291,9 @@ setModem KEYWORD2 # LoRaWAN wipe KEYWORD2 +restoreOTAA KEYWORD2 beginOTAA KEYWORD2 -beginAPB KEYWORD2 +beginABP KEYWORD2 uplink KEYWORD2 downlink KEYWORD2 configureChannel KEYWORD2 @@ -399,5 +402,10 @@ RADIOLIB_ERR_INVALID_PORT LITERAL1 RADIOLIB_ERR_NO_RX_WINDOW LITERAL1 RADIOLIB_ERR_INVALID_CHANNEL LITERAL1 RADIOLIB_ERR_INVALID_CID LITERAL1 +RADIOLIB_ERR_UPLINK_UNAVAILABLE LITERAL1 RADIOLIB_ERR_COMMAND_QUEUE_FULL LITERAL1 RADIOLIB_ERR_COMMAND_QUEUE_EMPTY LITERAL1 +RADIOLIB_ERR_COMMAND_QUEUE_ITEM_NOT_FOUND LITERAL1 +RADIOLIB_ERR_JOIN_NONCE_INVALID LITERAL1 +RADIOLIB_ERR_N_FCNT_DOWN_INVALID LITERAL1 +RADIOLIB_ERR_A_FCNT_DOWN_INVALID LITERAL1 \ No newline at end of file diff --git a/src/ArduinoHal.cpp b/src/ArduinoHal.cpp index 99bdbd38..a044e111 100644 --- a/src/ArduinoHal.cpp +++ b/src/ArduinoHal.cpp @@ -122,6 +122,8 @@ void ArduinoHal::readPersistentStorage(uint32_t addr, uint8_t* buff, size_t len) #if !defined(RADIOLIB_EEPROM_UNSUPPORTED) #if defined(RADIOLIB_ESP32) EEPROM.begin(RADIOLIB_HAL_PERSISTENT_STORAGE_SIZE); + #elif defined(ARDUINO_ARCH_APOLLO3) + EEPROM.init(); #endif for(size_t i = 0; i < len; i++) { buff[i] = EEPROM.read(addr + i); @@ -136,6 +138,8 @@ void ArduinoHal::writePersistentStorage(uint32_t addr, uint8_t* buff, size_t len #if !defined(RADIOLIB_EEPROM_UNSUPPORTED) #if defined(RADIOLIB_ESP32) EEPROM.begin(RADIOLIB_HAL_PERSISTENT_STORAGE_SIZE); + #elif defined(ARDUINO_ARCH_APOLLO3) + EEPROM.init(); #endif for(size_t i = 0; i < len; i++) { EEPROM.write(addr + i, buff[i]); @@ -157,10 +161,16 @@ void inline ArduinoHal::tone(uint32_t pin, unsigned int frequency, unsigned long // ESP32 tone() emulation (void)duration; if(prev == -1) { + #if !defined(ESP_IDF_VERSION) || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5,0,0)) ledcAttachPin(pin, RADIOLIB_TONE_ESP32_CHANNEL); + #endif } if(prev != frequency) { + #if !defined(ESP_IDF_VERSION) || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5,0,0)) ledcWriteTone(RADIOLIB_TONE_ESP32_CHANNEL, frequency); + #else + ledcWriteTone(pin, frequency); + #endif } prev = frequency; #elif defined(RADIOLIB_MBED_TONE_OVERRIDE) @@ -190,8 +200,13 @@ void inline ArduinoHal::noTone(uint32_t pin) { return; } // ESP32 tone() emulation + #if !defined(ESP_IDF_VERSION) || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5,0,0)) ledcDetachPin(pin); ledcWrite(RADIOLIB_TONE_ESP32_CHANNEL, 0); + #else + ledcDetach(pin); + ledcWrite(pin, 0); + #endif prev = -1; #elif defined(RADIOLIB_MBED_TONE_OVERRIDE) if(pin == RADIOLIB_NC) { diff --git a/src/BuildOpt.h b/src/BuildOpt.h index b3fdbe7f..5ade1f49 100644 --- a/src/BuildOpt.h +++ b/src/BuildOpt.h @@ -68,7 +68,6 @@ //#define RADIOLIB_EXCLUDE_SI443X //#define RADIOLIB_EXCLUDE_RFM2X // dependent on RADIOLIB_EXCLUDE_SI443X //#define RADIOLIB_EXCLUDE_SX127X - //#define RADIOLIB_EXCLUDE_RFM9X // dependent on RADIOLIB_EXCLUDE_SX127X //#define RADIOLIB_EXCLUDE_SX126X //#define RADIOLIB_EXCLUDE_STM32WLX // dependent on RADIOLIB_EXCLUDE_SX126X //#define RADIOLIB_EXCLUDE_SX128X @@ -468,6 +467,9 @@ #if defined(RADIOLIB_BUILD_ARDUINO) #define RADIOLIB_DEBUG_PRINT(...) Module::serialPrintf(__VA_ARGS__) #define RADIOLIB_DEBUG_PRINTLN(M, ...) Module::serialPrintf(M "\n", ##__VA_ARGS__) + + // some platforms do not support printf("%f"), so it has to be done this way + #define RADIOLIB_DEBUG_PRINT_FLOAT(VAL, DECIMALS) RADIOLIB_DEBUG_PORT.print(VAL, DECIMALS) #else #if !defined(RADIOLIB_DEBUG_PRINT) #define RADIOLIB_DEBUG_PRINT(...) fprintf(RADIOLIB_DEBUG_PORT, __VA_ARGS__) @@ -475,11 +477,13 @@ #if !defined(RADIOLIB_DEBUG_PRINTLN) #define RADIOLIB_DEBUG_PRINTLN(M, ...) fprintf(RADIOLIB_DEBUG_PORT, M "\n", ##__VA_ARGS__) #endif + #define RADIOLIB_DEBUG_PRINT_FLOAT(VAL, DECIMALS) RADIOLIB_DEBUG_PRINT("%.3f", VAL) #endif #define RADIOLIB_DEBUG_HEXDUMP(...) Module::hexdump(__VA_ARGS__) #else #define RADIOLIB_DEBUG_PRINT(...) {} #define RADIOLIB_DEBUG_PRINTLN(...) {} + #define RADIOLIB_DEBUG_PRINT_FLOAT(VAL, DECIMALS) {} #define RADIOLIB_DEBUG_HEXDUMP(...) {} #endif diff --git a/src/RadioLib.h b/src/RadioLib.h index 8eaa072a..ee7026d2 100644 --- a/src/RadioLib.h +++ b/src/RadioLib.h @@ -76,9 +76,6 @@ #include "modules/RF69/RF69.h" #include "modules/RFM2x/RFM22.h" #include "modules/RFM2x/RFM23.h" -#include "modules/RFM9x/RFM95.h" -#include "modules/RFM9x/RFM96.h" -#include "modules/RFM9x/RFM97.h" #include "modules/Si443x/Si4430.h" #include "modules/Si443x/Si4431.h" #include "modules/Si443x/Si4432.h" diff --git a/src/modules/CC1101/CC1101.h b/src/modules/CC1101/CC1101.h index a13327e9..2e83d926 100644 --- a/src/modules/CC1101/CC1101.h +++ b/src/modules/CC1101/CC1101.h @@ -740,7 +740,8 @@ class CC1101: public PhysicalLayer { int16_t setBitRate(float br); /*! - \brief Sets receiver bandwidth. Allowed values range from 58.0 to 812.0 kHz. + \brief Sets receiver bandwidth. Allowed values are 58, 68, 81, 102, 116, 135, 162, + 203, 232, 270, 325, 406, 464, 541, 650 and 812 kHz. \param rxBw Receiver bandwidth to be set in kHz. \returns \ref status_codes */ diff --git a/src/modules/RFM9x/RFM95.cpp b/src/modules/RFM9x/RFM95.cpp deleted file mode 100644 index 90f1bc46..00000000 --- a/src/modules/RFM9x/RFM95.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include "RFM95.h" -#if !defined(RADIOLIB_EXCLUDE_RFM9X) - -RFM95::RFM95(Module* mod) : SX1278(mod) { - -} - -int16_t RFM95::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) { - // execute common part - int16_t state = SX127x::begin(RADIOLIB_RFM9X_CHIP_VERSION_OFFICIAL, syncWord, preambleLength); - if(state == RADIOLIB_ERR_CHIP_NOT_FOUND) { - // SX127X_REG_VERSION might be set 0x12 - state = SX127x::begin(RADIOLIB_RFM9X_CHIP_VERSION_UNOFFICIAL, syncWord, preambleLength); - RADIOLIB_ASSERT(state); - } else if(state != RADIOLIB_ERR_NONE) { - // some other error - return(state); - } - RADIOLIB_DEBUG_PRINTLN("M\tSX1278"); - RADIOLIB_DEBUG_PRINTLN("M\tRFM95"); - - // configure publicly accessible settings - state = setBandwidth(bw); - RADIOLIB_ASSERT(state); - - state = setFrequency(freq); - 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 RFM95::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) { - // execute common part - int16_t state = SX127x::beginFSK(RADIOLIB_RFM9X_CHIP_VERSION_OFFICIAL, freqDev, rxBw, preambleLength, enableOOK); - if(state == RADIOLIB_ERR_CHIP_NOT_FOUND) { - // SX127X_REG_VERSION might be set 0x12 - state = SX127x::beginFSK(RADIOLIB_RFM9X_CHIP_VERSION_UNOFFICIAL, freqDev, rxBw, preambleLength, enableOOK); - RADIOLIB_ASSERT(state); - } else if(state != RADIOLIB_ERR_NONE) { - // some other error - return(state); - } - RADIOLIB_DEBUG_PRINTLN("M\tSX1278"); - RADIOLIB_DEBUG_PRINTLN("M\tRFM95"); - - // configure settings not accessible by API - state = configFSK(); - RADIOLIB_ASSERT(state); - - // configure publicly accessible settings - state = setFrequency(freq); - RADIOLIB_ASSERT(state); - - state = setBitRate(br); - RADIOLIB_ASSERT(state); - - state = setOutputPower(power); - RADIOLIB_ASSERT(state); - - if(enableOOK) { - state = setDataShapingOOK(RADIOLIB_SHAPING_NONE); - RADIOLIB_ASSERT(state); - } else { - state = setDataShaping(RADIOLIB_SHAPING_NONE); - RADIOLIB_ASSERT(state); - } - - return(state); -} - -int16_t RFM95::setFrequency(float freq) { - RADIOLIB_CHECK_RANGE(freq, 862.0, 1020.0, RADIOLIB_ERR_INVALID_FREQUENCY); - - // set frequency and if successful, save the new setting - int16_t state = SX127x::setFrequencyRaw(freq); - if(state == RADIOLIB_ERR_NONE) { - SX127x::frequency = freq; - } - return(state); -} - -#endif diff --git a/src/modules/RFM9x/RFM95.h b/src/modules/RFM9x/RFM95.h deleted file mode 100644 index c0128003..00000000 --- a/src/modules/RFM9x/RFM95.h +++ /dev/null @@ -1,80 +0,0 @@ -#if !defined(_RADIOLIB_RFM95_H) -#define _RADIOLIB_RFM95_H - -#include "../../TypeDef.h" - -#if !defined(RADIOLIB_EXCLUDE_RFM9X) - -#include "../../Module.h" -#include "../SX127x/SX127x.h" -#include "../SX127x/SX1278.h" - -// SX127X_REG_VERSION -#define RADIOLIB_RFM9X_CHIP_VERSION_OFFICIAL 0x11 -#define RADIOLIB_RFM9X_CHIP_VERSION_UNOFFICIAL 0x12 // according to datasheet, only 0x11 should be possible, but some modules seem to have 0x12 - -/*! - \class RFM95 - \brief Derived class for %RFM95 modules. Overrides some methods from SX1278 due to different parameter ranges. -*/ -class RFM95: 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. - */ - RFM95(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 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 = RADIOLIB_SX127X_SYNC_WORD, int8_t power = 10, uint16_t preambleLength = 8, uint8_t gain = 0); - - /*! - \brief FSK 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 137.0 MHz to 525.0 MHz. - \param br Bit rate of the FSK transmission in kbps (kilobits per second). Allowed values range from 1.2 to 300.0 kbps. - \param freqDev Frequency deviation of the FSK transmission in kHz. Allowed values range from 0.6 to 200.0 kHz. - Note that the allowed range changes based on bit rate setting, so that the condition FreqDev + BitRate/2 <= 250 kHz is always met. - \param rxBw Receiver bandwidth in kHz. Allowed values are 2.6, 3.1, 3.9, 5.2, 6.3, 7.8, 10.4, 12.5, 15.6, 20.8, 25, 31.3, 41.7, 50, 62.5, 83.3, 100, 125, 166.7, 200 and 250 kHz. - \param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm. - \param preambleLength Length of FSK preamble in bits. - \param enableOOK Use OOK modulation instead of FSK. - \returns \ref status_codes - */ - int16_t beginFSK(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 125.0, int8_t power = 10, uint16_t preambleLength = 16, bool enableOOK = false); - - // 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); - -#if !defined(RADIOLIB_GODMODE) - private: -#endif - -}; - -#endif - -#endif diff --git a/src/modules/RFM9x/RFM96.cpp b/src/modules/RFM9x/RFM96.cpp deleted file mode 100644 index 0c744184..00000000 --- a/src/modules/RFM9x/RFM96.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#include "RFM96.h" -#if !defined(RADIOLIB_EXCLUDE_RFM9X) - -RFM96::RFM96(Module* mod) : SX1278(mod) { - -} - -int16_t RFM96::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) { - // execute common part - int16_t state = SX127x::begin(RADIOLIB_RFM9X_CHIP_VERSION_OFFICIAL, syncWord, preambleLength); - if(state == RADIOLIB_ERR_CHIP_NOT_FOUND) { - // SX127X_REG_VERSION might be set 0x12 - state = SX127x::begin(RADIOLIB_RFM9X_CHIP_VERSION_UNOFFICIAL, syncWord, preambleLength); - RADIOLIB_ASSERT(state); - } else if(state != RADIOLIB_ERR_NONE) { - // some other error - return(state); - } - RADIOLIB_DEBUG_PRINTLN("M\tSX1278"); - RADIOLIB_DEBUG_PRINTLN("M\tRFM96"); - - // configure publicly accessible settings - state = setBandwidth(bw); - RADIOLIB_ASSERT(state); - - state = setFrequency(freq); - RADIOLIB_ASSERT(state); - - state = setSpreadingFactor(sf); - RADIOLIB_ASSERT(state); - - state = setCodingRate(cr); - RADIOLIB_ASSERT(state); - - state = setOutputPower(power); - RADIOLIB_ASSERT(state); - - state = setGain(gain); - RADIOLIB_ASSERT(state); - - return(state); -} - -int16_t RFM96::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) { - // execute common part - int16_t state = SX127x::beginFSK(RADIOLIB_RFM9X_CHIP_VERSION_OFFICIAL, freqDev, rxBw, preambleLength, enableOOK); - if(state == RADIOLIB_ERR_CHIP_NOT_FOUND) { - // SX127X_REG_VERSION might be set 0x12 - state = SX127x::beginFSK(RADIOLIB_RFM9X_CHIP_VERSION_UNOFFICIAL, freqDev, rxBw, preambleLength, enableOOK); - RADIOLIB_ASSERT(state); - } else if(state != RADIOLIB_ERR_NONE) { - // some other error - return(state); - } - RADIOLIB_DEBUG_PRINTLN("M\tSX1278"); - RADIOLIB_DEBUG_PRINTLN("M\tRFM96"); - - // configure settings not accessible by API - state = configFSK(); - RADIOLIB_ASSERT(state); - - // configure publicly accessible settings - state = setFrequency(freq); - RADIOLIB_ASSERT(state); - - state = setBitRate(br); - RADIOLIB_ASSERT(state); - - state = setOutputPower(power); - RADIOLIB_ASSERT(state); - - if(enableOOK) { - state = setDataShapingOOK(RADIOLIB_SHAPING_NONE); - RADIOLIB_ASSERT(state); - } else { - state = setDataShaping(RADIOLIB_SHAPING_NONE); - RADIOLIB_ASSERT(state); - } - - return(state); -} - -int16_t RFM96::setFrequency(float freq) { - RADIOLIB_CHECK_RANGE(freq, 410.0, 525.0, RADIOLIB_ERR_INVALID_FREQUENCY); - - // set frequency and if successful, save the new setting - int16_t state = SX127x::setFrequencyRaw(freq); - if(state == RADIOLIB_ERR_NONE) { - SX127x::frequency = freq; - } - return(state); -} - -#endif diff --git a/src/modules/RFM9x/RFM96.h b/src/modules/RFM9x/RFM96.h deleted file mode 100644 index e5990197..00000000 --- a/src/modules/RFM9x/RFM96.h +++ /dev/null @@ -1,88 +0,0 @@ -#if !defined(_RADIOLIB_RFM96_H) -#define _RADIOLIB_RFM96_H - -#include "../../TypeDef.h" - -#if !defined(RADIOLIB_EXCLUDE_RFM9X) - -#include "../../Module.h" -#include "../SX127x/SX127x.h" -#include "../SX127x/SX1278.h" - -// SX127X_REG_VERSION -#define RADIOLIB_RFM9X_CHIP_VERSION_OFFICIAL 0x11 -#define RADIOLIB_RFM9X_CHIP_VERSION_UNOFFICIAL 0x12 // according to datasheet, only 0x11 should be possible, but some modules seem to have 0x12 - -/*! - \class RFM96 - \brief Derived class for %RFM96 modules. Overrides some methods from SX1278 due to different parameter ranges. -*/ -class RFM96: 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. - */ - RFM96(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 433.0 MHz to 470.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 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 = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = RADIOLIB_SX127X_SYNC_WORD, int8_t power = 10, uint16_t preambleLength = 8, uint8_t gain = 0); - - /*! - \brief FSK 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 137.0 MHz to 525.0 MHz. - \param br Bit rate of the FSK transmission in kbps (kilobits per second). Allowed values range from 1.2 to 300.0 kbps. - \param freqDev Frequency deviation of the FSK transmission in kHz. Allowed values range from 0.6 to 200.0 kHz. - Note that the allowed range changes based on bit rate setting, so that the condition FreqDev + BitRate/2 <= 250 kHz is always met. - \param rxBw Receiver bandwidth in kHz. Allowed values are 2.6, 3.1, 3.9, 5.2, 6.3, 7.8, 10.4, 12.5, 15.6, 20.8, 25, - 31.3, 41.7, 50, 62.5, 83.3, 100, 125, 166.7, 200 and 250 kHz. - \param power Transmission output power in dBm. Allowed values range from 2 to 17 dBm. - \param preambleLength Length of FSK preamble in bits. - \param enableOOK Use OOK modulation instead of FSK. - \returns \ref status_codes - */ - int16_t beginFSK(float freq = 434.0, float br = 4.8, float freqDev = 5.0, float rxBw = 125.0, int8_t power = 10, uint16_t preambleLength = 16, bool enableOOK = false); - - // configuration methods - - /*! - \brief Sets carrier frequency. Allowed values range from 433.0 MHz to 470.0 MHz. - \param freq Carrier frequency to be set in MHz. - \returns \ref status_codes - */ - int16_t setFrequency(float freq); - -#if !defined(RADIOLIB_GODMODE) - private: -#endif - -}; - -/*! - \class RFM98 - \brief Only exists as alias for RFM96, since there seems to be no difference between %RFM96 and %RFM98 modules. -*/ -RADIOLIB_TYPE_ALIAS(RFM96, RFM98); - -#endif - -#endif diff --git a/src/modules/RFM9x/RFM97.cpp b/src/modules/RFM9x/RFM97.cpp deleted file mode 100644 index 0d02c707..00000000 --- a/src/modules/RFM9x/RFM97.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "RFM97.h" -#if !defined(RADIOLIB_EXCLUDE_RFM9X) - -RFM97::RFM97(Module* mod) : RFM95(mod) { - -}; - -int16_t RFM97::setSpreadingFactor(uint8_t sf) { - // check active modem - if(getActiveModem() != RADIOLIB_SX127X_LORA) { - return(RADIOLIB_ERR_WRONG_MODEM); - } - - uint8_t newSpreadingFactor; - - // check allowed spreading factor values - switch(sf) { - case 6: - newSpreadingFactor = RADIOLIB_SX127X_SF_6; - break; - case 7: - newSpreadingFactor = RADIOLIB_SX127X_SF_7; - break; - case 8: - newSpreadingFactor = RADIOLIB_SX127X_SF_8; - break; - case 9: - newSpreadingFactor = RADIOLIB_SX127X_SF_9; - break; - default: - return(RADIOLIB_ERR_INVALID_SPREADING_FACTOR); - } - - // set spreading factor and if successful, save the new setting - int16_t state = SX1278::setSpreadingFactorRaw(newSpreadingFactor); - if(state == RADIOLIB_ERR_NONE) { - SX127x::spreadingFactor = sf; - } - return(state); -} - -#endif diff --git a/src/modules/RFM9x/RFM97.h b/src/modules/RFM9x/RFM97.h deleted file mode 100644 index b4623495..00000000 --- a/src/modules/RFM9x/RFM97.h +++ /dev/null @@ -1,45 +0,0 @@ -#if !defined(_RADIOLIB_RFM97_H) -#define _RADIOLIB_RFM97_H - -#include "../../TypeDef.h" - -#if !defined(RADIOLIB_EXCLUDE_RFM9X) - -#include "../../Module.h" -#include "../SX127x/SX127x.h" -#include "../SX127x/SX1278.h" -#include "RFM95.h" - -/*! - \class RFM97 - \brief Derived class for %RFM97 modules. Overrides some methods from RFM95 due to different parameter ranges. -*/ -class RFM97: public RFM95 { - 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. - */ - RFM97(Module* mod); - - // configuration methods - - /*! - \brief Sets %LoRa link spreading factor. Allowed values range from 6 to 9. Only available in %LoRa mode. - \param sf %LoRa link spreading factor to be set. - \returns \ref status_codes - */ - int16_t setSpreadingFactor(uint8_t sf); - -#if !defined(RADIOLIB_GODMODE) - private: -#endif - -}; - -#endif - -#endif diff --git a/src/modules/SX126x/STM32WLx.cpp b/src/modules/SX126x/STM32WLx.cpp index 095e13ec..f64b4933 100644 --- a/src/modules/SX126x/STM32WLx.cpp +++ b/src/modules/SX126x/STM32WLx.cpp @@ -126,4 +126,28 @@ void STM32WLx::clearDio1Action() { SubGhz.detachInterrupt(); } +void STM32WLx::setPacketReceivedAction(void (*func)(void)) { + this->setDio1Action(func); +} + +void STM32WLx::clearPacketReceivedAction() { + this->clearDio1Action(); +} + +void STM32WLx::setPacketSentAction(void (*func)(void)) { + this->setDio1Action(func); +} + +void STM32WLx::clearPacketSentAction() { + this->clearDio1Action(); +} + +void STM32WLx::setChannelScanAction(void (*func)(void)) { + this->setDio1Action(func); +} + +void STM32WLx::clearChannelScanAction() { + this->clearDio1Action(); +} + #endif // !defined(RADIOLIB_EXCLUDE_STM32WLX) diff --git a/src/modules/SX126x/STM32WLx.h b/src/modules/SX126x/STM32WLx.h index aa964917..cab2fc54 100644 --- a/src/modules/SX126x/STM32WLx.h +++ b/src/modules/SX126x/STM32WLx.h @@ -120,6 +120,39 @@ class STM32WLx : public SX1262 { */ void clearDio1Action(); + /*! + \brief Sets interrupt service routine to call when a packet is received. + \param func ISR to call. + */ + void setPacketReceivedAction(void (*func)(void)); + + /*! + \brief Clears interrupt service routine to call when a packet is received. + */ + void clearPacketReceivedAction(); + + /*! + \brief Sets interrupt service routine to call when a packet is sent. + \param func ISR to call. + */ + void setPacketSentAction(void (*func)(void)); + + /*! + \brief Clears interrupt service routine to call when a packet is sent. + */ + void clearPacketSentAction(); + + /*! + \brief Sets interrupt service routine to call when a channel scan is finished. + \param func ISR to call. + */ + void setChannelScanAction(void (*func)(void)); + + /*! + \brief Clears interrupt service routine to call when a channel scan is finished. + */ + void clearChannelScanAction(); + #if !defined(RADIOLIB_GODMODE) protected: #endif diff --git a/src/modules/SX126x/SX126x.cpp b/src/modules/SX126x/SX126x.cpp index ea50e843..412db354 100644 --- a/src/modules/SX126x/SX126x.cpp +++ b/src/modules/SX126x/SX126x.cpp @@ -220,7 +220,7 @@ int16_t SX126x::reset(bool verify) { } // wait a bit to not spam the module - this->mod->hal->delay(100); + this->mod->hal->delay(10); } } @@ -1720,27 +1720,41 @@ int16_t SX126x::setRx(uint32_t timeout) { return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_RX, data, 3, true, false)); } + int16_t SX126x::setCad(uint8_t symbolNum, uint8_t detPeak, uint8_t detMin) { - // default CAD parameters for assigned SF as per Semtech AN1200.48, Rev 2.1, Page 50 - uint8_t detPeakValues[8] = { 22, 22, 22, 22, 23, 24, 25, 28}; - uint8_t symbolNumValues[8] = { RADIOLIB_SX126X_CAD_ON_2_SYMB, - RADIOLIB_SX126X_CAD_ON_2_SYMB, - RADIOLIB_SX126X_CAD_ON_2_SYMB, - RADIOLIB_SX126X_CAD_ON_2_SYMB, - RADIOLIB_SX126X_CAD_ON_4_SYMB, - RADIOLIB_SX126X_CAD_ON_4_SYMB, - RADIOLIB_SX126X_CAD_ON_4_SYMB, - RADIOLIB_SX126X_CAD_ON_4_SYMB }; + // default CAD parameters are shown in Semtech AN1200.48, page 41. + uint8_t detPeakValues[6] = { 22, 22, 24, 25, 26, 30}; + + // CAD parameters aren't available for SF-6. Just to be safe. + if(this->spreadingFactor < 7) { + this->spreadingFactor = 7; + } else if(this->spreadingFactor > 12) { + this->spreadingFactor = 12; + } + // build the packet uint8_t data[7]; - data[0] = symbolNumValues[this->spreadingFactor - 5]; - data[1] = detPeakValues[this->spreadingFactor - 5]; + data[0] = RADIOLIB_SX126X_CAD_ON_2_SYMB; + data[1] = detPeakValues[this->spreadingFactor - 7]; data[2] = RADIOLIB_SX126X_CAD_PARAM_DET_MIN; data[3] = RADIOLIB_SX126X_CAD_GOTO_STDBY; data[4] = 0x00; data[5] = 0x00; data[6] = 0x00; + + /* + CAD Configuration Note: + The default CAD configuration applied by `scanChannel` overrides the optimal SF-specific configurations, leading to suboptimal detection. + I.e., anything that is not RADIOLIB_SX126X_CAD_PARAM_DEFAULT is overridden. But CAD settings are SF specific. + To address this, the user override has been commented out, ensuring consistent application of the optimal CAD settings as + per Semtech's Application Note AN1200.48 (page 41) for the 125KHz setting. This approach significantly reduces false CAD occurrences. + Testing has shown that there is no reason for a user to change CAD settings for anything other than most optimal ones described in AN1200.48 . + However, this change deos not respect CAD configs from the LoRaWAN layer. Future considerations or use cases might require revisiting this decision. + Hence this note. +*/ + +/* // set user-provided values if(symbolNum != RADIOLIB_SX126X_CAD_PARAM_DEFAULT) { data[0] = symbolNum; @@ -1754,6 +1768,9 @@ int16_t SX126x::setCad(uint8_t symbolNum, uint8_t detPeak, uint8_t detMin) { data[2] = detMin; } +*/ + + // configure parameters int16_t state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_CAD_PARAMS, data, 7); RADIOLIB_ASSERT(state); @@ -2048,7 +2065,7 @@ int16_t SX126x::config(uint8_t modem) { state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_RX_TX_FALLBACK_MODE, data, 1); RADIOLIB_ASSERT(state); - // set some CAD parameters - will be overwritten whel calling CAD anyway + // set some CAD parameters - will be overwritten when calling CAD anyway data[0] = RADIOLIB_SX126X_CAD_ON_8_SYMB; data[1] = this->spreadingFactor + 13; data[2] = RADIOLIB_SX126X_CAD_PARAM_DET_MIN; diff --git a/src/modules/SX126x/SX126x.h b/src/modules/SX126x/SX126x.h index 5e1945b4..976e90c5 100644 --- a/src/modules/SX126x/SX126x.h +++ b/src/modules/SX126x/SX126x.h @@ -870,11 +870,11 @@ class SX126x: public PhysicalLayer { /*! \brief Sets FSK whitening parameters. \param enabled True = Whitening enabled - \param initial Initial value used for the whitening LFSR in FSK mode. Defaults to 0x0100, - use 0x01FF for SX127x compatibility. + \param initial Initial value used for the whitening LFSR in FSK mode. + By default set to 0x01FF for compatibility with SX127x and LoRaWAN. \returns \ref status_codes */ - int16_t setWhitening(bool enabled, uint16_t initial = 0x0100); + int16_t setWhitening(bool enabled, uint16_t initial = 0x01FF); /*! \brief Sets TCXO (Temperature Compensated Crystal Oscillator) configuration. @@ -1042,7 +1042,7 @@ class SX126x: public PhysicalLayer { #if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE) /*! - \brief Set interrupt service routine function to call when data bit is receveid in direct mode. + \brief Set interrupt service routine function to call when data bit is received in direct mode. \param func Pointer to interrupt service routine. */ void setDirectAction(void (*func)(void)); @@ -1092,6 +1092,19 @@ class SX126x: public PhysicalLayer { */ int16_t spectralScanGetResult(uint16_t* results); + /*! + \brief Set the PA configuration. Allows user to optimize PA for a specific output power + and matching network. Any calls to this method must be done after calling begin/beginFSK and/or setOutputPower. + WARNING: Use at your own risk! Setting invalid values can and will lead to permanent damage! + \param paDutyCycle PA duty cycle raw value. + \param deviceSel Device select, usually RADIOLIB_SX126X_PA_CONFIG_SX1261, + RADIOLIB_SX126X_PA_CONFIG_SX1262 or RADIOLIB_SX126X_PA_CONFIG_SX1268. + \param hpMax hpMax raw value. + \param paLut paLut PA lookup table raw value. + \returns \ref status_codes + */ + int16_t setPaConfig(uint8_t paDutyCycle, uint8_t deviceSel, uint8_t hpMax = RADIOLIB_SX126X_PA_CONFIG_HP_MAX, uint8_t paLut = RADIOLIB_SX126X_PA_CONFIG_PA_LUT); + #if !defined(RADIOLIB_GODMODE) protected: #endif @@ -1100,7 +1113,6 @@ class SX126x: public PhysicalLayer { int16_t setTx(uint32_t timeout = 0); int16_t setRx(uint32_t timeout); int16_t setCad(uint8_t symbolNum, uint8_t detPeak, uint8_t detMin); - int16_t setPaConfig(uint8_t paDutyCycle, uint8_t deviceSel, uint8_t hpMax = RADIOLIB_SX126X_PA_CONFIG_HP_MAX, uint8_t paLut = RADIOLIB_SX126X_PA_CONFIG_PA_LUT); int16_t writeRegister(uint16_t addr, uint8_t* data, uint8_t numBytes); int16_t readRegister(uint16_t addr, uint8_t* data, uint8_t numBytes); int16_t writeBuffer(uint8_t* data, uint8_t numBytes, uint8_t offset = 0x00); diff --git a/src/modules/SX127x/SX1272.cpp b/src/modules/SX127x/SX1272.cpp index ac572101..45bf8802 100644 --- a/src/modules/SX127x/SX1272.cpp +++ b/src/modules/SX127x/SX1272.cpp @@ -8,7 +8,8 @@ SX1272::SX1272(Module* mod) : SX127x(mod) { int16_t SX1272::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) { // execute common part - int16_t state = SX127x::begin(RADIOLIB_SX1272_CHIP_VERSION, syncWord, preambleLength); + uint8_t version = RADIOLIB_SX1272_CHIP_VERSION; + int16_t state = SX127x::begin(&version, 1, syncWord, preambleLength); RADIOLIB_ASSERT(state); // configure publicly accessible settings @@ -39,7 +40,8 @@ int16_t SX1272::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t sync int16_t SX1272::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) { // execute common part - int16_t state = SX127x::beginFSK(RADIOLIB_SX1272_CHIP_VERSION, freqDev, rxBw, preambleLength, enableOOK); + uint8_t version = RADIOLIB_SX1272_CHIP_VERSION; + int16_t state = SX127x::beginFSK(&version, 1, freqDev, rxBw, preambleLength, enableOOK); RADIOLIB_ASSERT(state); // configure settings not accessible by API diff --git a/src/modules/SX127x/SX1273.cpp b/src/modules/SX127x/SX1273.cpp index 4b180694..ea021c66 100644 --- a/src/modules/SX127x/SX1273.cpp +++ b/src/modules/SX127x/SX1273.cpp @@ -7,7 +7,8 @@ SX1273::SX1273(Module* mod) : SX1272(mod) { int16_t SX1273::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) { // execute common part - int16_t state = SX127x::begin(RADIOLIB_SX1272_CHIP_VERSION, syncWord, preambleLength); + uint8_t version = RADIOLIB_SX1272_CHIP_VERSION; + int16_t state = SX127x::begin(&version, 1, syncWord, preambleLength); RADIOLIB_ASSERT(state); // configure publicly accessible settings diff --git a/src/modules/SX127x/SX1276.cpp b/src/modules/SX127x/SX1276.cpp index 21eadb57..b20fd7d1 100644 --- a/src/modules/SX127x/SX1276.cpp +++ b/src/modules/SX127x/SX1276.cpp @@ -7,7 +7,8 @@ SX1276::SX1276(Module* mod) : SX1278(mod) { int16_t SX1276::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) { // execute common part - int16_t state = SX127x::begin(RADIOLIB_SX1278_CHIP_VERSION, syncWord, preambleLength); + uint8_t versions[] = { RADIOLIB_SX1278_CHIP_VERSION, RADIOLIB_SX1278_CHIP_VERSION_ALT, RADIOLIB_SX1278_CHIP_VERSION_RFM9X }; + int16_t state = SX127x::begin(versions, 3, syncWord, preambleLength); RADIOLIB_ASSERT(state); // configure publicly accessible settings @@ -38,7 +39,8 @@ int16_t SX1276::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t sync int16_t SX1276::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) { // execute common part - int16_t state = SX127x::beginFSK(RADIOLIB_SX1278_CHIP_VERSION, freqDev, rxBw, preambleLength, enableOOK); + uint8_t versions[] = { RADIOLIB_SX1278_CHIP_VERSION, RADIOLIB_SX1278_CHIP_VERSION_ALT, RADIOLIB_SX1278_CHIP_VERSION_RFM9X }; + int16_t state = SX127x::beginFSK(versions, 3, freqDev, rxBw, preambleLength, enableOOK); RADIOLIB_ASSERT(state); // configure settings not accessible by API diff --git a/src/modules/SX127x/SX1276.h b/src/modules/SX127x/SX1276.h index 0d83fb36..659e22d7 100644 --- a/src/modules/SX127x/SX1276.h +++ b/src/modules/SX127x/SX1276.h @@ -69,6 +69,12 @@ class SX1276: public SX1278 { }; +/*! + \class RFM96 + \brief Only exists as alias for SX1276, since there seems to be no difference between %RFM96 and %SX1276 modules. +*/ +RADIOLIB_TYPE_ALIAS(SX1276, RFM96); + #endif #endif diff --git a/src/modules/SX127x/SX1277.cpp b/src/modules/SX127x/SX1277.cpp index d466a385..5875bd9f 100644 --- a/src/modules/SX127x/SX1277.cpp +++ b/src/modules/SX127x/SX1277.cpp @@ -7,7 +7,8 @@ SX1277::SX1277(Module* mod) : SX1278(mod) { int16_t SX1277::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) { // execute common part - int16_t state = SX127x::begin(RADIOLIB_SX1278_CHIP_VERSION, syncWord, preambleLength); + uint8_t versions[] = { RADIOLIB_SX1278_CHIP_VERSION, RADIOLIB_SX1278_CHIP_VERSION_ALT, RADIOLIB_SX1278_CHIP_VERSION_RFM9X }; + int16_t state = SX127x::begin(versions, 3, syncWord, preambleLength); RADIOLIB_ASSERT(state); // configure publicly accessible settings @@ -38,7 +39,8 @@ int16_t SX1277::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t sync int16_t SX1277::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) { // execute common part - int16_t state = SX127x::beginFSK(RADIOLIB_SX1278_CHIP_VERSION, freqDev, rxBw, preambleLength, enableOOK); + uint8_t versions[] = { RADIOLIB_SX1278_CHIP_VERSION, RADIOLIB_SX1278_CHIP_VERSION_ALT, RADIOLIB_SX1278_CHIP_VERSION_RFM9X }; + int16_t state = SX127x::beginFSK(versions, 3, freqDev, rxBw, preambleLength, enableOOK); RADIOLIB_ASSERT(state); // configure settings not accessible by API diff --git a/src/modules/SX127x/SX1277.h b/src/modules/SX127x/SX1277.h index 88fb58fe..15804d61 100644 --- a/src/modules/SX127x/SX1277.h +++ b/src/modules/SX127x/SX1277.h @@ -83,6 +83,12 @@ class SX1277: public SX1278 { }; +/*! + \class RFM97 + \brief Only exists as alias for SX1277, since there seems to be no difference between %RFM97 and %SX1277 modules. +*/ +RADIOLIB_TYPE_ALIAS(SX1277, RFM97); + #endif #endif diff --git a/src/modules/SX127x/SX1278.cpp b/src/modules/SX127x/SX1278.cpp index 9fb0c5df..90325ff0 100644 --- a/src/modules/SX127x/SX1278.cpp +++ b/src/modules/SX127x/SX1278.cpp @@ -8,7 +8,8 @@ SX1278::SX1278(Module* mod) : SX127x(mod) { int16_t SX1278::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) { // execute common part - int16_t state = SX127x::begin(RADIOLIB_SX1278_CHIP_VERSION, syncWord, preambleLength); + uint8_t versions[] = { RADIOLIB_SX1278_CHIP_VERSION, RADIOLIB_SX1278_CHIP_VERSION_ALT, RADIOLIB_SX1278_CHIP_VERSION_RFM9X }; + int16_t state = SX127x::begin(versions, 3, syncWord, preambleLength); RADIOLIB_ASSERT(state); // configure publicly accessible settings @@ -39,7 +40,8 @@ int16_t SX1278::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t sync int16_t SX1278::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) { // execute common part - int16_t state = SX127x::beginFSK(RADIOLIB_SX1278_CHIP_VERSION, freqDev, rxBw, preambleLength, enableOOK); + uint8_t versions[] = { RADIOLIB_SX1278_CHIP_VERSION, RADIOLIB_SX1278_CHIP_VERSION_ALT, RADIOLIB_SX1278_CHIP_VERSION_RFM9X }; + int16_t state = SX127x::beginFSK(versions, 3, freqDev, rxBw, preambleLength, enableOOK); RADIOLIB_ASSERT(state); // configure settings not accessible by API diff --git a/src/modules/SX127x/SX1278.h b/src/modules/SX127x/SX1278.h index 92c996ed..c9f966a2 100644 --- a/src/modules/SX127x/SX1278.h +++ b/src/modules/SX127x/SX1278.h @@ -67,7 +67,9 @@ #define RADIOLIB_SX1278_AGC_AUTO_ON 0b00000100 // 2 2 LNA gain set by internal AGC loop // SX127X_REG_VERSION -#define RADIOLIB_SX1278_CHIP_VERSION 0x12 +#define RADIOLIB_SX1278_CHIP_VERSION 0x12 // this is the "official" version listed in datasheet +#define RADIOLIB_SX1278_CHIP_VERSION_ALT 0x13 // appears sometimes +#define RADIOLIB_SX1278_CHIP_VERSION_RFM9X 0x11 // this value is used for the RFM9x // SX1278 FSK modem settings // SX127X_REG_PA_RAMP @@ -299,6 +301,12 @@ class SX1278: public SX127x { }; +/*! + \class RFM98 + \brief Only exists as alias for SX1278, since there seems to be no difference between %RFM98 and %SX1278 modules. +*/ +RADIOLIB_TYPE_ALIAS(SX1278, RFM98); + #endif #endif diff --git a/src/modules/SX127x/SX1279.cpp b/src/modules/SX127x/SX1279.cpp index ae79f0f8..4fdd4daa 100644 --- a/src/modules/SX127x/SX1279.cpp +++ b/src/modules/SX127x/SX1279.cpp @@ -7,7 +7,8 @@ SX1279::SX1279(Module* mod) : SX1278(mod) { int16_t SX1279::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) { // execute common part - int16_t state = SX127x::begin(RADIOLIB_SX1278_CHIP_VERSION, syncWord, preambleLength); + uint8_t versions[] = { RADIOLIB_SX1278_CHIP_VERSION, RADIOLIB_SX1278_CHIP_VERSION_ALT, RADIOLIB_SX1278_CHIP_VERSION_RFM9X }; + int16_t state = SX127x::begin(versions, 3, syncWord, preambleLength); RADIOLIB_ASSERT(state); // configure publicly accessible settings @@ -38,7 +39,8 @@ int16_t SX1279::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t sync int16_t SX1279::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) { // execute common part - int16_t state = SX127x::beginFSK(RADIOLIB_SX1278_CHIP_VERSION, freqDev, rxBw, preambleLength, enableOOK); + uint8_t versions[] = { RADIOLIB_SX1278_CHIP_VERSION, RADIOLIB_SX1278_CHIP_VERSION_ALT, RADIOLIB_SX1278_CHIP_VERSION_RFM9X }; + int16_t state = SX127x::beginFSK(versions, 3, freqDev, rxBw, preambleLength, enableOOK); RADIOLIB_ASSERT(state); // configure settings not accessible by API diff --git a/src/modules/SX127x/SX127x.cpp b/src/modules/SX127x/SX127x.cpp index 8d2c7a73..e0dffe0c 100644 --- a/src/modules/SX127x/SX127x.cpp +++ b/src/modules/SX127x/SX127x.cpp @@ -10,14 +10,14 @@ Module* SX127x::getMod() { return(this->mod); } -int16_t SX127x::begin(uint8_t chipVersion, uint8_t syncWord, uint16_t preambleLength) { +int16_t SX127x::begin(uint8_t* chipVersions, uint8_t numVersions, uint8_t syncWord, uint16_t preambleLength) { // set module properties this->mod->init(); this->mod->hal->pinMode(this->mod->getIrq(), this->mod->hal->GpioModeInput); this->mod->hal->pinMode(this->mod->getGpio(), this->mod->hal->GpioModeInput); // try to find the SX127x chip - if(!SX127x::findChip(chipVersion)) { + if(!SX127x::findChip(chipVersions, numVersions)) { RADIOLIB_DEBUG_PRINTLN("No SX127x found!"); this->mod->term(); return(RADIOLIB_ERR_CHIP_NOT_FOUND); @@ -61,14 +61,14 @@ int16_t SX127x::begin(uint8_t chipVersion, uint8_t syncWord, uint16_t preambleLe return(state); } -int16_t SX127x::beginFSK(uint8_t chipVersion, float freqDev, float rxBw, uint16_t preambleLength, bool enableOOK) { +int16_t SX127x::beginFSK(uint8_t* chipVersions, uint8_t numVersions, float freqDev, float rxBw, uint16_t preambleLength, bool enableOOK) { // set module properties this->mod->init(); this->mod->hal->pinMode(this->mod->getIrq(), this->mod->hal->GpioModeInput); this->mod->hal->pinMode(this->mod->getGpio(), this->mod->hal->GpioModeInput); // try to find the SX127x chip - if(!SX127x::findChip(chipVersion)) { + if(!SX127x::findChip(chipVersions, numVersions)) { RADIOLIB_DEBUG_PRINTLN("No SX127x found!"); this->mod->term(); return(RADIOLIB_ERR_CHIP_NOT_FOUND); @@ -118,6 +118,10 @@ int16_t SX127x::beginFSK(uint8_t chipVersion, float freqDev, float rxBw, uint16_ state = SX127x::setPreambleLength(preambleLength); RADIOLIB_ASSERT(state); + // set preamble polarity + state = invertPreamble(false); + RADIOLIB_ASSERT(state); + // set default sync word uint8_t syncWord[] = {0x12, 0xAD}; state = setSyncWord(syncWord, 2); @@ -789,6 +793,25 @@ int16_t SX127x::setPreambleLength(size_t preambleLength) { return(RADIOLIB_ERR_UNKNOWN); } +int16_t SX127x::invertPreamble(bool enable) { + // set mode to standby + int16_t state = setMode(RADIOLIB_SX127X_STANDBY); + RADIOLIB_ASSERT(state); + + // check active modem + uint8_t modem = getActiveModem(); + if(modem == RADIOLIB_SX127X_LORA) { + return(RADIOLIB_ERR_WRONG_MODEM); + } else if(modem == RADIOLIB_SX127X_FSK_OOK) { + // set preamble polarity + uint8_t polarity = enable ? RADIOLIB_SX127X_PREAMBLE_POLARITY_AA : RADIOLIB_SX127X_PREAMBLE_POLARITY_55; + state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_SYNC_CONFIG, polarity, 5, 5); + return(state); + } + + return(RADIOLIB_ERR_UNKNOWN); +} + float SX127x::getFrequencyError(bool autoCorrect) { int16_t modem = getActiveModem(); if(modem == RADIOLIB_SX127X_LORA) { @@ -1006,6 +1029,13 @@ int16_t SX127x::setSyncWord(uint8_t* syncWord, size_t len) { // check active modem uint8_t modem = getActiveModem(); if(modem == RADIOLIB_SX127X_FSK_OOK) { + + // disable sync word in case len is 0 + if(len == 0) { + int16_t state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_SYNC_CONFIG, RADIOLIB_SX127X_SYNC_OFF, 4, 4); + return(state); + } + RADIOLIB_CHECK_RANGE(len, 1, 8, RADIOLIB_ERR_INVALID_SYNC_WORD); // sync word must not contain value 0x00 @@ -1455,10 +1485,6 @@ int16_t SX127x::configFSK() { state |= this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_PACKET_CONFIG_2, RADIOLIB_SX127X_DATA_MODE_PACKET | RADIOLIB_SX127X_IO_HOME_OFF, 6, 5); RADIOLIB_ASSERT(state); - // set preamble polarity - state =this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_SYNC_CONFIG, RADIOLIB_SX127X_PREAMBLE_POLARITY_55, 5, 5); - RADIOLIB_ASSERT(state); - // set FIFO threshold state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_FIFO_THRESH, RADIOLIB_SX127X_TX_START_FIFO_NOT_EMPTY, 7, 7); state |= this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_FIFO_THRESH, RADIOLIB_SX127X_FIFO_THRESH, 5, 0); @@ -1500,7 +1526,7 @@ int16_t SX127x::setPacketMode(uint8_t mode, uint8_t len) { return(state); } -bool SX127x::findChip(uint8_t ver) { +bool SX127x::findChip(uint8_t* vers, uint8_t num) { uint8_t i = 0; bool flagFound = false; while((i < 10) && !flagFound) { @@ -1509,13 +1535,19 @@ bool SX127x::findChip(uint8_t ver) { // check version register int16_t version = getChipVersion(); - if(version == ver) { - flagFound = true; - } else { - RADIOLIB_DEBUG_PRINTLN("SX127x not found! (%d of 10 tries) RADIOLIB_SX127X_REG_VERSION == 0x%04X, expected 0x00%X", i + 1, version, ver); + for(uint8_t i = 0; i < num; i++) { + if(version == vers[i]) { + flagFound = true; + break; + } + } + + if(!flagFound) { + RADIOLIB_DEBUG_PRINTLN("SX127x not found! (%d of 10 tries) RADIOLIB_SX127X_REG_VERSION == 0x%04X", i + 1, version); this->mod->hal->delay(10); i++; } + } return(flagFound); diff --git a/src/modules/SX127x/SX127x.h b/src/modules/SX127x/SX127x.h index bbfb3db8..fbc3d77f 100644 --- a/src/modules/SX127x/SX127x.h +++ b/src/modules/SX127x/SX127x.h @@ -601,12 +601,13 @@ class SX127x: public PhysicalLayer { /*! \brief Initialization method. Will be called with appropriate parameters when calling initialization method from derived class. - \param chipVersion Value in SPI version register. Used to verify the connection and hardware version. + \param chipVersion Array of possible values in SPI version register. Used to verify the connection and hardware version. + \param numVersions Number of possible chip versions. \param syncWord %LoRa sync word. \param preambleLength Length of %LoRa transmission preamble in symbols. \returns \ref status_codes */ - int16_t begin(uint8_t chipVersion, uint8_t syncWord, uint16_t preambleLength); + int16_t begin(uint8_t* chipVersions, uint8_t numVersions, uint8_t syncWord, uint16_t preambleLength); /*! \brief Reset method. Will reset the chip to the default state using RST pin. Declared pure virtual since SX1272 and SX1278 implementations differ. @@ -615,14 +616,15 @@ class SX127x: public PhysicalLayer { /*! \brief Initialization method for FSK modem. Will be called with appropriate parameters when calling FSK initialization method from derived class. - \param chipVersion Value in SPI version register. Used to verify the connection and hardware version. + \param chipVersion Array of possible values in SPI version register. Used to verify the connection and hardware version. + \param numVersions Number of possible chip versions. \param freqDev Frequency deviation of the FSK transmission in kHz. \param rxBw Receiver bandwidth in kHz. \param preambleLength Length of FSK preamble in bits. \param enableOOK Flag to specify OOK mode. This modulation is similar to FSK. \returns \ref status_codes */ - int16_t beginFSK(uint8_t chipVersion, float freqDev, float rxBw, uint16_t preambleLength, bool enableOOK); + int16_t beginFSK(uint8_t* chipVersions, uint8_t numVersions, float freqDev, float rxBw, uint16_t preambleLength, bool enableOOK); /*! \brief Binary transmit method. Will transmit arbitrary binary data up to 255 bytes long using %LoRa or up to 63 bytes using FSK modem. @@ -876,6 +878,13 @@ class SX127x: public PhysicalLayer { */ int16_t setPreambleLength(size_t preambleLength) override; + /*! + \brief Invert FSK preamble polarity. The default (non-inverted) is 0x55, the inverted is 0xAA. + \param enable Preamble polarity in FSK mode - 0xAA when true, 0x55 when false. + \returns \ref status_codes + */ + int16_t invertPreamble(bool enable); + /*! \brief Gets frequency error of the latest received packet. \param autoCorrect When set to true, frequency will be automatically corrected. @@ -1233,7 +1242,7 @@ class SX127x: public PhysicalLayer { bool packetLengthQueried = false; // FSK packet length is the first byte in FIFO, length can only be queried once uint8_t packetLengthConfig = RADIOLIB_SX127X_PACKET_VARIABLE; - bool findChip(uint8_t ver); + bool findChip(uint8_t* vers, uint8_t num); int16_t setMode(uint8_t mode); int16_t setActiveModem(uint8_t modem); void clearIRQFlags(); diff --git a/src/modules/SX128x/SX128x.cpp b/src/modules/SX128x/SX128x.cpp index bdd2e0c2..3e26382e 100644 --- a/src/modules/SX128x/SX128x.cpp +++ b/src/modules/SX128x/SX128x.cpp @@ -1499,11 +1499,11 @@ int16_t SX128x::config(uint8_t modem) { } int16_t SX128x::SPIparseStatus(uint8_t in) { - if((in & 0b00001110) == RADIOLIB_SX128X_STATUS_CMD_TIMEOUT) { + if((in & 0b00011100) == RADIOLIB_SX128X_STATUS_CMD_TIMEOUT) { return(RADIOLIB_ERR_SPI_CMD_TIMEOUT); - } else if((in & 0b00001110) == RADIOLIB_SX128X_STATUS_CMD_ERROR) { + } else if((in & 0b00011100) == RADIOLIB_SX128X_STATUS_CMD_ERROR) { return(RADIOLIB_ERR_SPI_CMD_INVALID); - } else if((in & 0b00001110) == RADIOLIB_SX128X_STATUS_CMD_FAILED) { + } else if((in & 0b00011100) == RADIOLIB_SX128X_STATUS_CMD_FAILED) { return(RADIOLIB_ERR_SPI_CMD_FAILED); } else if((in == 0x00) || (in == 0xFF)) { return(RADIOLIB_ERR_CHIP_NOT_FOUND); diff --git a/src/modules/Si443x/Si443x.cpp b/src/modules/Si443x/Si443x.cpp index 62695fc4..2b2bf835 100644 --- a/src/modules/Si443x/Si443x.cpp +++ b/src/modules/Si443x/Si443x.cpp @@ -770,7 +770,8 @@ int16_t Si443x::updateClockRecovery() { // print that whole mess RADIOLIB_DEBUG_PRINTLN("%X\n%X\n%X", bypass, decRate, manch); - RADIOLIB_DEBUG_PRINTLN("%f\t%d\t%X\n%lu\t%lX\n%d\t%X", rxOsr, rxOsr_fixed, rxOsr_fixed, ncoOff, ncoOff, crGain, crGain); + RADIOLIB_DEBUG_PRINT_FLOAT(rxOsr, 2); + RADIOLIB_DEBUG_PRINTLN("\t%d\t%X\n%lu\t%lX\n%d\t%X", rxOsr_fixed, rxOsr_fixed, ncoOff, ncoOff, crGain, crGain); // update oversampling ratio int16_t state = this->mod->SPIsetRegValue(RADIOLIB_SI443X_REG_CLOCK_REC_OFFSET_2, (uint8_t)((rxOsr_fixed & 0x0700) >> 3), 7, 5); diff --git a/src/protocols/APRS/APRS.cpp b/src/protocols/APRS/APRS.cpp index 780dddf4..3458272e 100644 --- a/src/protocols/APRS/APRS.cpp +++ b/src/protocols/APRS/APRS.cpp @@ -35,14 +35,14 @@ int16_t APRSClient::begin(char sym, char* callsign, uint8_t ssid, bool alt) { } int16_t APRSClient::sendPosition(char* destCallsign, uint8_t destSSID, char* lat, char* lon, char* msg, char* time) { + size_t len = 1 + strlen(lat) + 1 + strlen(lon); + if(msg != NULL) { + len += 1 + strlen(msg); + } + if(time != NULL) { + len += strlen(time); + } #if !defined(RADIOLIB_STATIC_ONLY) - size_t len = 1 + strlen(lat) + 1 + strlen(lon); - if(msg != NULL) { - len += 1 + strlen(msg); - } - if(time != NULL) { - len += strlen(time); - } char* info = new char[len + 1]; #else char info[RADIOLIB_STATIC_ARRAY_SIZE]; diff --git a/src/protocols/LoRaWAN/LoRaWAN.cpp b/src/protocols/LoRaWAN/LoRaWAN.cpp index abd554ba..6f37255a 100644 --- a/src/protocols/LoRaWAN/LoRaWAN.cpp +++ b/src/protocols/LoRaWAN/LoRaWAN.cpp @@ -23,9 +23,10 @@ LoRaWANNode::LoRaWANNode(PhysicalLayer* phy, const LoRaWANBand_t* band) { this->phyLayer = phy; this->band = band; this->FSK = false; - this->startChannel = -1; - this->numChannels = -1; this->rx2 = this->band->rx2; + this->difsSlots = 2; + this->backoffMax = 6; + this->enableCSMA = false; } #if !defined(RADIOLIB_EEPROM_UNSUPPORTED) @@ -34,6 +35,12 @@ void LoRaWANNode::wipe() { mod->hal->wipePersistentStorage(); } +void LoRaWANNode::setCSMA(uint8_t backoffMax, uint8_t difsSlots, bool enableCSMA) { + this->backoffMax = backoffMax; + this->difsSlots = difsSlots; + this->enableCSMA = enableCSMA; +} + int16_t LoRaWANNode::restore() { // check the magic value Module* mod = this->phyLayer->getMod(); @@ -307,7 +314,7 @@ int16_t LoRaWANNode::beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t* nwkKe } } - + // parse other contents this->homeNetId = LoRaWANNode::ntoh(&joinAcceptMsg[RADIOLIB_LORAWAN_JOIN_ACCEPT_HOME_NET_ID_POS], 3); this->devAddr = LoRaWANNode::ntoh(&joinAcceptMsg[RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_ADDR_POS]); @@ -320,6 +327,7 @@ int16_t LoRaWANNode::beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t* nwkKe this->rxDelays[1] = this->rxDelays[0] + 1000; // process CFlist if present + uint8_t cfList[RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN] = { 0 }; if(lenRx == RADIOLIB_LORAWAN_JOIN_ACCEPT_MAX_LEN) { uint8_t cfList[RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN] = { 0 }; memcpy(&cfList[0], &joinAcceptMsg[RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_POS], RADIOLIB_LORAWAN_JOIN_ACCEPT_CFLIST_LEN); @@ -368,6 +376,7 @@ int16_t LoRaWANNode::beginOTAA(uint64_t joinEUI, uint64_t devEUI, uint8_t* nwkKe // 1.0 version, just derive the keys LoRaWANNode::hton(&keyDerivationBuff[RADIOLIB_LORAWAN_JOIN_ACCEPT_HOME_NET_ID_POS], this->homeNetId, 3); LoRaWANNode::hton(&keyDerivationBuff[RADIOLIB_LORAWAN_JOIN_ACCEPT_DEV_ADDR_POS], this->devNonce); + keyDerivationBuff[0] = RADIOLIB_LORAWAN_JOIN_ACCEPT_APP_S_KEY; RadioLibAES128Instance.init(nwkKey); RadioLibAES128Instance.encryptECB(keyDerivationBuff, RADIOLIB_AES128_BLOCK_SIZE, this->appSKey); @@ -648,6 +657,7 @@ int16_t LoRaWANNode::uplink(uint8_t* data, size_t len, uint8_t port, bool isConf this->isMACPayload = false; } + // check if there are some MAC commands to piggyback (only when piggybacking onto a application-frame) uint8_t foptsLen = 0; size_t foptsBufSize = 0; @@ -795,6 +805,7 @@ int16_t LoRaWANNode::uplink(uint8_t* data, size_t len, uint8_t port, bool isConf // encrypt the frame payload processAES(data, len, encKey, &uplinkMsg[RADIOLIB_LORAWAN_FRAME_PAYLOAD_POS(foptsLen)], this->fcntUp, RADIOLIB_LORAWAN_CHANNEL_DIR_UPLINK, 0x00, true); + // create blocks for MIC calculation uint8_t block0[RADIOLIB_AES128_BLOCK_SIZE] = { 0 }; block0[RADIOLIB_LORAWAN_BLOCK_MAGIC_POS] = RADIOLIB_LORAWAN_MIC_BLOCK_MAGIC; @@ -828,6 +839,11 @@ int16_t LoRaWANNode::uplink(uint8_t* data, size_t len, uint8_t port, bool isConf LoRaWANNode::hton(&uplinkMsg[uplinkMsgLen - sizeof(uint32_t)], micF); } + // perform CSMA if enabled. + if (enableCSMA) { + performCSMA(); + } + RADIOLIB_DEBUG_PRINTLN("uplinkMsg:"); RADIOLIB_DEBUG_HEXDUMP(uplinkMsg, uplinkMsgLen); @@ -2061,4 +2077,52 @@ void LoRaWANNode::hton(uint8_t* buff, T val, size_t size) { } } -#endif +// The following function enables LMAC, a CSMA scheme for LoRa as specified +// in the LoRa Alliance Technical Recommendation #13. +// A user may enable CSMA to provide frames an additional layer of protection from interference. +// https://resources.lora-alliance.org/technical-recommendations/tr013-1-0-0-csma +void LoRaWANNode::performCSMA() { + + // Compute initial random back-off. + // When BO is reduced to zero, the function returns and the frame is transmitted. + uint32_t BO = this->phyLayer->random(1, this->backoffMax + 1); + + while (BO > 0) { + // DIFS: Check channel for DIFS_slots + bool channelFreeDuringDIFS = true; + for (uint8_t i = 0; i < this->difsSlots; i++) { + if (performCAD()) { + RADIOLIB_DEBUG_PRINTLN("OCCUPIED CHANNEL DURING DIFS"); + channelFreeDuringDIFS = false; + // Channel is occupied during DIFS, hop to another. + this->setupChannels(); + break; + } + } + + // Start reducing BO counter if DIFS slot was free. + if (channelFreeDuringDIFS) { + // Continue decrementing BO with per each CAD reporting free channel. + while (BO > 0) { + if (performCAD()) { + RADIOLIB_DEBUG_PRINTLN("OCCUPIED CHANNEL DURING BO"); + // Channel is busy during CAD, hop to another and return to DIFS state again. + this->setupChannels(); + break; // Exit loop. Go back to DIFS state. + } + BO--; // Decrement BO by one if channel is free + } + } + } +} + +bool LoRaWANNode::performCAD() { + int16_t state = this->phyLayer->scanChannel(); + + if ((state == RADIOLIB_PREAMBLE_DETECTED) || (state == RADIOLIB_LORA_DETECTED)) { + return true; // Channel is busy + } + return false; // Channel is free +} + +#endif \ No newline at end of file diff --git a/src/protocols/LoRaWAN/LoRaWAN.h b/src/protocols/LoRaWAN/LoRaWAN.h index 5b936ed1..6ec96458 100644 --- a/src/protocols/LoRaWAN/LoRaWAN.h +++ b/src/protocols/LoRaWAN/LoRaWAN.h @@ -338,6 +338,17 @@ class LoRaWANNode { // RX2 channel properties - may be changed by MAC command LoRaWANChannel_t rx2; + /*! \brief Num of Back Off(BO) slots to be decremented after DIFS phase. 0 to disable BO. + A random BO avoids collisions in the case where two or more nodes start the CSMA + process at the same time. */ + uint8_t backoffMax; + + /*! \brief Num of CADs to estimate a clear CH. */ + uint8_t difsSlots; + + /*! \brief enable/disable CSMA for LoRaWAN. */ + bool enableCSMA; + /*! \brief Default constructor. \param phy Pointer to the PhysicalLayer radio module. @@ -359,6 +370,13 @@ class LoRaWANNode { int16_t restore(); #endif + \brief Configures CSMA for LoRaWAN as per TR-13, LoRa Alliance. + \param backoffMax Num of BO slots to be decremented after DIFS phase. 0 to disable BO. + \param difsSlots Num of CADs to estimate a clear CH. + \param enableCSMA enable/disable CSMA for LoRaWAN. + */ + void setCSMA(uint8_t backoffMax, uint8_t difsSlots, bool enableCSMA = false); + /*! \brief Join network by performing over-the-air activation. By this procedure, the device will perform an exchange with the network server and set all necessary configuration. @@ -619,6 +637,12 @@ class LoRaWANNode { // host-to-network conversion method - takes data from host variable and and converts it to network packet endians template static void hton(uint8_t* buff, T val, size_t size = 0); + + // perform a single CAD operation for the under SF/CH combination. Returns either busy or otherwise. + bool performCAD(); + + // Performs CSMA as per LoRa Alliance Technical Reccomendation 13 (TR-013). + void performCSMA(); }; #endif