From d5ce384bdaa6ec3abbed7972fb765eeb204610a4 Mon Sep 17 00:00:00 2001 From: jgromes Date: Thu, 6 Jul 2023 11:17:29 +0200 Subject: [PATCH] [SX127x] Implemented new common PHY methods --- src/modules/SX127x/SX1272.cpp | 29 +++++++++++++++++++++++++ src/modules/SX127x/SX1272.h | 19 ++++++++++++++-- src/modules/SX127x/SX1273.cpp | 25 +++++++++++++++++++++ src/modules/SX127x/SX1273.h | 7 ++++++ src/modules/SX127x/SX1277.cpp | 25 +++++++++++++++++++++ src/modules/SX127x/SX1277.h | 7 ++++++ src/modules/SX127x/SX1278.cpp | 29 +++++++++++++++++++++++++ src/modules/SX127x/SX1278.h | 19 ++++++++++++++-- src/modules/SX127x/SX127x.cpp | 41 +++++++++++++++++++++-------------- src/modules/SX127x/SX127x.h | 6 ++--- 10 files changed, 184 insertions(+), 23 deletions(-) diff --git a/src/modules/SX127x/SX1272.cpp b/src/modules/SX127x/SX1272.cpp index 5d5b27ac..b198d46a 100644 --- a/src/modules/SX127x/SX1272.cpp +++ b/src/modules/SX127x/SX1272.cpp @@ -220,6 +220,35 @@ int16_t SX1272::setBitRate(float br) { return(SX127x::setBitRateCommon(br, RADIOLIB_SX1272_REG_BIT_RATE_FRAC)); } +int16_t SX1272::setDataRate(DataRate_t dr) { + int16_t state = RADIOLIB_ERR_UNKNOWN; + + // select interpretation based on active modem + uint8_t modem = this->getActiveModem(); + if(modem == RADIOLIB_SX127X_FSK_OOK) { + // set the bit rate + state = this->setBitRate(dr.fsk.bitRate); + RADIOLIB_ASSERT(state); + + // set the frequency deviation + state = this->setFrequencyDeviation(dr.fsk.freqDev); + + } else if(modem == RADIOLIB_SX127X_LORA) { + // set the spreading factor + state = this->setSpreadingFactor(dr.lora.spreadingFactor); + RADIOLIB_ASSERT(state); + + // set the bandwidth + state = this->setBandwidth(dr.lora.bandwidth); + } + + return(state); +} + +int16_t SX1272::setOutputPower(int8_t power) { + return(this->setOutputPower(power, false)); +} + int16_t SX1272::setOutputPower(int8_t power, bool useRfo) { // check allowed power range if(useRfo) { diff --git a/src/modules/SX127x/SX1272.h b/src/modules/SX127x/SX1272.h index f997647d..f389230b 100644 --- a/src/modules/SX127x/SX1272.h +++ b/src/modules/SX127x/SX1272.h @@ -176,14 +176,29 @@ class SX1272: public SX127x { \returns \ref status_codes */ int16_t setBitRate(float br) override; + + /*! + \brief Set data. + \param dr Data rate struct. Interpretation depends on currently active modem (FSK or LoRa). + \returns \ref status_codes + */ + int16_t setDataRate(DataRate_t dr) override; + + /*! + \brief Sets transmission output power. Allowed values range from -1 to 14 dBm (RFO pin) or +2 to +20 dBm (PA_BOOST pin). + High power +20 dBm operation is also supported, on the PA_BOOST pin. Defaults to PA_BOOST. + \param power Transmission output power in dBm. + \returns \ref status_codes + */ + int16_t setOutputPower(int8_t power) override; /*! \brief Sets transmission output power. Allowed values range from -1 to 14 dBm (RFO pin) or +2 to +20 dBm (PA_BOOST pin). \param power Transmission output power in dBm. - \param useRfo Whether to use the RFO (true) or the PA_BOOST (false) pin for the RF output. Defaults to PA_BOOST. + \param useRfo Whether to use the RFO (true) or the PA_BOOST (false) pin for the RF output. \returns \ref status_codes */ - int16_t setOutputPower(int8_t power, bool useRfo = false); + int16_t setOutputPower(int8_t power, bool useRfo); /*! \brief Sets gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain. diff --git a/src/modules/SX127x/SX1273.cpp b/src/modules/SX127x/SX1273.cpp index 0e4d287c..4b180694 100644 --- a/src/modules/SX127x/SX1273.cpp +++ b/src/modules/SX127x/SX1273.cpp @@ -66,4 +66,29 @@ int16_t SX1273::setSpreadingFactor(uint8_t sf) { return(state); } +int16_t SX1273::setDataRate(DataRate_t dr) { + int16_t state = RADIOLIB_ERR_UNKNOWN; + + // select interpretation based on active modem + uint8_t modem = this->getActiveModem(); + if(modem == RADIOLIB_SX127X_FSK_OOK) { + // set the bit rate + state = this->setBitRate(dr.fsk.bitRate); + RADIOLIB_ASSERT(state); + + // set the frequency deviation + state = this->setFrequencyDeviation(dr.fsk.freqDev); + + } else if(modem == RADIOLIB_SX127X_LORA) { + // set the spreading factor + state = this->setSpreadingFactor(dr.lora.spreadingFactor); + RADIOLIB_ASSERT(state); + + // set the bandwidth + state = this->setBandwidth(dr.lora.bandwidth); + } + + return(state); +} + #endif diff --git a/src/modules/SX127x/SX1273.h b/src/modules/SX127x/SX1273.h index 4fab6215..2ceb58bc 100644 --- a/src/modules/SX127x/SX1273.h +++ b/src/modules/SX127x/SX1273.h @@ -49,6 +49,13 @@ class SX1273: public SX1272 { */ int16_t setSpreadingFactor(uint8_t sf); + /*! + \brief Set data. + \param dr Data rate struct. Interpretation depends on currently active modem (FSK or LoRa). + \returns \ref status_codes + */ + int16_t setDataRate(DataRate_t dr) override; + #if !defined(RADIOLIB_GODMODE) private: #endif diff --git a/src/modules/SX127x/SX1277.cpp b/src/modules/SX127x/SX1277.cpp index 7e635756..d466a385 100644 --- a/src/modules/SX127x/SX1277.cpp +++ b/src/modules/SX127x/SX1277.cpp @@ -107,4 +107,29 @@ int16_t SX1277::setSpreadingFactor(uint8_t sf) { return(state); } +int16_t SX1277::setDataRate(DataRate_t dr) { + int16_t state = RADIOLIB_ERR_UNKNOWN; + + // select interpretation based on active modem + uint8_t modem = this->getActiveModem(); + if(modem == RADIOLIB_SX127X_FSK_OOK) { + // set the bit rate + state = this->setBitRate(dr.fsk.bitRate); + RADIOLIB_ASSERT(state); + + // set the frequency deviation + state = this->setFrequencyDeviation(dr.fsk.freqDev); + + } else if(modem == RADIOLIB_SX127X_LORA) { + // set the spreading factor + state = this->setSpreadingFactor(dr.lora.spreadingFactor); + RADIOLIB_ASSERT(state); + + // set the bandwidth + state = this->setBandwidth(dr.lora.bandwidth); + } + + return(state); +} + #endif diff --git a/src/modules/SX127x/SX1277.h b/src/modules/SX127x/SX1277.h index 76d8854b..88fb58fe 100644 --- a/src/modules/SX127x/SX1277.h +++ b/src/modules/SX127x/SX1277.h @@ -69,6 +69,13 @@ class SX1277: public SX1278 { \returns \ref status_codes */ int16_t setSpreadingFactor(uint8_t sf); + + /*! + \brief Set data. + \param dr Data rate struct. Interpretation depends on currently active modem (FSK or LoRa). + \returns \ref status_codes + */ + int16_t setDataRate(DataRate_t dr) override; #if !defined(RADIOLIB_GODMODE) private: diff --git a/src/modules/SX127x/SX1278.cpp b/src/modules/SX127x/SX1278.cpp index ec5266ad..e61a1ef3 100644 --- a/src/modules/SX127x/SX1278.cpp +++ b/src/modules/SX127x/SX1278.cpp @@ -234,6 +234,35 @@ int16_t SX1278::setBitRate(float br) { return(SX127x::setBitRateCommon(br, RADIOLIB_SX1278_REG_BIT_RATE_FRAC)); } +int16_t SX1278::setDataRate(DataRate_t dr) { + int16_t state = RADIOLIB_ERR_UNKNOWN; + + // select interpretation based on active modem + uint8_t modem = this->getActiveModem(); + if(modem == RADIOLIB_SX127X_FSK_OOK) { + // set the bit rate + state = this->setBitRate(dr.fsk.bitRate); + RADIOLIB_ASSERT(state); + + // set the frequency deviation + state = this->setFrequencyDeviation(dr.fsk.freqDev); + + } else if(modem == RADIOLIB_SX127X_LORA) { + // set the spreading factor + state = this->setSpreadingFactor(dr.lora.spreadingFactor); + RADIOLIB_ASSERT(state); + + // set the bandwidth + state = this->setBandwidth(dr.lora.bandwidth); + } + + return(state); +} + +int16_t SX1278::setOutputPower(int8_t power) { + return(this->setOutputPower(power, false)); +} + int16_t SX1278::setOutputPower(int8_t power, bool useRfo) { // check allowed power range if(useRfo) { diff --git a/src/modules/SX127x/SX1278.h b/src/modules/SX127x/SX1278.h index e5f8781c..92c996ed 100644 --- a/src/modules/SX127x/SX1278.h +++ b/src/modules/SX127x/SX1278.h @@ -184,15 +184,30 @@ class SX1278: public SX127x { \returns \ref status_codes */ int16_t setBitRate(float br) override; + + /*! + \brief Set data. + \param dr Data rate struct. Interpretation depends on currently active modem (FSK or LoRa). + \returns \ref status_codes + */ + int16_t setDataRate(DataRate_t dr) override; + + /*! + \brief Sets transmission output power. Allowed values range from -3 to 15 dBm (RFO pin) or +2 to +17 dBm (PA_BOOST pin). + High power +20 dBm operation is also supported, on the PA_BOOST pin. Defaults to PA_BOOST. + \param power Transmission output power in dBm. + \returns \ref status_codes + */ + int16_t setOutputPower(int8_t power) override; /*! \brief Sets transmission output power. Allowed values range from -3 to 15 dBm (RFO pin) or +2 to +17 dBm (PA_BOOST pin). High power +20 dBm operation is also supported, on the PA_BOOST pin. \param power Transmission output power in dBm. - \param useRfo Whether to use the RFO (true) or the PA_BOOST (false) pin for the RF output. Defaults to PA_BOOST. + \param useRfo Whether to use the RFO (true) or the PA_BOOST (false) pin for the RF output. \returns \ref status_codes */ - int16_t setOutputPower(int8_t power, bool useRfo = false); + int16_t setOutputPower(int8_t power, bool useRfo); /*! \brief Sets gain of receiver LNA (low-noise amplifier). Can be set to any integer in range 1 to 6 where 1 is the highest gain. diff --git a/src/modules/SX127x/SX127x.cpp b/src/modules/SX127x/SX127x.cpp index 4e9d5adc..027d512e 100644 --- a/src/modules/SX127x/SX127x.cpp +++ b/src/modules/SX127x/SX127x.cpp @@ -729,7 +729,7 @@ int16_t SX127x::setCurrentLimit(uint8_t currentLimit) { return(state); } -int16_t SX127x::setPreambleLength(uint16_t preambleLength) { +int16_t SX127x::setPreambleLength(size_t preambleLength) { // set mode to standby int16_t state = setMode(RADIOLIB_SX127X_STANDBY); RADIOLIB_ASSERT(state); @@ -973,27 +973,36 @@ int16_t SX127x::setAFCAGCTrigger(uint8_t trigger) { int16_t SX127x::setSyncWord(uint8_t* syncWord, size_t len) { // check active modem - if(getActiveModem() != RADIOLIB_SX127X_FSK_OOK) { - return(RADIOLIB_ERR_WRONG_MODEM); - } + uint8_t modem = getActiveModem(); + if(modem == RADIOLIB_SX127X_FSK_OOK) { + RADIOLIB_CHECK_RANGE(len, 1, 8, RADIOLIB_ERR_INVALID_SYNC_WORD); - RADIOLIB_CHECK_RANGE(len, 1, 8, RADIOLIB_ERR_INVALID_SYNC_WORD); + // sync word must not contain value 0x00 + for(size_t i = 0; i < len; i++) { + if(syncWord[i] == 0x00) { + return(RADIOLIB_ERR_INVALID_SYNC_WORD); + } + } - // sync word must not contain value 0x00 - for(size_t i = 0; i < len; i++) { - if(syncWord[i] == 0x00) { + // enable sync word recognition + int16_t state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_SYNC_CONFIG, RADIOLIB_SX127X_SYNC_ON, 4, 4); + state |= this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_SYNC_CONFIG, len - 1, 2, 0); + RADIOLIB_ASSERT(state); + + // set sync word + this->mod->SPIwriteRegisterBurst(RADIOLIB_SX127X_REG_SYNC_VALUE_1, syncWord, len); + return(RADIOLIB_ERR_NONE); + + } else if(modem == RADIOLIB_SX127X_LORA) { + // with length set to 1 and LoRa modem active, assume it is the LoRa sync word + if(len > 1) { return(RADIOLIB_ERR_INVALID_SYNC_WORD); } + + return(this->setSyncWord(syncWord[0])); } - // enable sync word recognition - int16_t state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_SYNC_CONFIG, RADIOLIB_SX127X_SYNC_ON, 4, 4); - state |= this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_SYNC_CONFIG, len - 1, 2, 0); - RADIOLIB_ASSERT(state); - - // set sync word - this->mod->SPIwriteRegisterBurst(RADIOLIB_SX127X_REG_SYNC_VALUE_1, syncWord, len); - return(RADIOLIB_ERR_NONE); + return(RADIOLIB_ERR_WRONG_MODEM); } int16_t SX127x::setNodeAddress(uint8_t nodeAddr) { diff --git a/src/modules/SX127x/SX127x.h b/src/modules/SX127x/SX127x.h index dcd84790..3ba9a319 100644 --- a/src/modules/SX127x/SX127x.h +++ b/src/modules/SX127x/SX127x.h @@ -856,7 +856,7 @@ class SX127x: public PhysicalLayer { \param preambleLength Preamble length to be set (in symbols when in LoRa mode or bits in FSK mode). \returns \ref status_codes */ - int16_t setPreambleLength(uint16_t preambleLength); + int16_t setPreambleLength(size_t preambleLength) override; /*! \brief Gets frequency error of the latest received packet. @@ -924,7 +924,7 @@ class SX127x: public PhysicalLayer { \param len Sync word length (in bytes). \returns \ref status_codes */ - int16_t setSyncWord(uint8_t* syncWord, size_t len); + int16_t setSyncWord(uint8_t* syncWord, size_t len) override; /*! \brief Sets FSK node address. Calling this method will enable address filtering. Only available in FSK mode. @@ -1089,7 +1089,7 @@ class SX127x: public PhysicalLayer { \param enable QI inversion enabled (true) or disabled (false); \returns \ref status_codes */ - int16_t invertIQ(bool enable); + int16_t invertIQ(bool enable) override; #if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE) /*!