diff --git a/keywords.txt b/keywords.txt index 7621a797..31d82ea1 100644 --- a/keywords.txt +++ b/keywords.txt @@ -104,6 +104,7 @@ setGdo1Action KEYWORD2 # SX126x-specific setDio2Action KEYWORD2 setTCXO KEYWORD2 +setDio2AsRfSwitch KEYWORD2 # ESP8266 join KEYWORD2 diff --git a/src/TypeDef.h b/src/TypeDef.h index b007f408..efda5ef0 100644 --- a/src/TypeDef.h +++ b/src/TypeDef.h @@ -461,6 +461,11 @@ */ #define ERR_SPI_CMD_FAILED -707 +/*! + \brief SX126x scan channel not possible because DIO2 is used as RF antenna switch. +*/ +#define ERR_DIO2_UNAVAIL_CAD_FAILED -708 + /*! \} */ diff --git a/src/modules/ESP8266.cpp b/src/modules/ESP8266.cpp index c6dcc55a..8586cf96 100644 --- a/src/modules/ESP8266.cpp +++ b/src/modules/ESP8266.cpp @@ -180,7 +180,7 @@ size_t ESP8266::receive(uint8_t* data, size_t len, uint32_t timeout) { return(i); } -uint16_t ESP8266::getNumBytes(uint32_t timeout, size_t minBytes) { +size_t ESP8266::getNumBytes(uint32_t timeout, size_t minBytes) { // wait for available data uint32_t start = millis(); while(_mod->ModuleSerial->available() < (int16_t)minBytes) { diff --git a/src/modules/SX126x.cpp b/src/modules/SX126x.cpp index 05274763..8291857c 100644 --- a/src/modules/SX126x.cpp +++ b/src/modules/SX126x.cpp @@ -33,6 +33,11 @@ int16_t SX126x::begin(float bw, uint8_t sf, uint8_t cr, uint16_t syncWord, uint1 } // configure publicly accessible settings + state = setDio2AsRfSwitch(false); + if(state != ERR_NONE) { + return(state); + } + state = setSpreadingFactor(sf); if(state != ERR_NONE) { return(state); @@ -86,6 +91,11 @@ int16_t SX126x::beginFSK(float br, float freqDev, float rxBw, uint16_t preambleL } // configure publicly accessible settings + state = setDio2AsRfSwitch(false); + if(state != ERR_NONE) { + return(state); + } + state = setBitRate(br); if(state != ERR_NONE) { return(state); @@ -274,6 +284,11 @@ int16_t SX126x::scanChannel() { return(ERR_WRONG_MODEM); } + if (_dio2RfSwitch) { + // If DIO2 is used as RF switch this function does not work + return(ERR_DIO2_UNAVAIL_CAD_FAILED); + } + // set mode to standby int16_t state = standby(); if(state != ERR_NONE) { @@ -1058,18 +1073,27 @@ int16_t SX126x::setFrequencyRaw(float freq) { return(ERR_NONE); } -int16_t SX126x::config(uint8_t modem) { - // set DIO2 as IRQ - uint8_t* data = new uint8_t[1]; - data[0] = SX126X_DIO2_AS_IRQ; - int16_t state = SPIwriteCommand(SX126X_CMD_SET_DIO2_AS_RF_SWITCH_CTRL, data, 1); - if(state != ERR_NONE) { - return(state); +int16_t SX126x::setDio2AsRfSwitch(bool enable) { + uint8_t data[1]; + if (enable) { + // set DIO2 as RF switch + data[0] = SX126X_DIO2_AS_RF_SWITCH; + } else { + data[0] = SX126X_DIO2_AS_IRQ; } + int16_t state = SPIwriteCommand(SX126X_CMD_SET_DIO2_AS_RF_SWITCH_CTRL, data, 1); + if (state == ERR_NONE) { + _dio2RfSwitch = enable; + } + return(state); +} + +int16_t SX126x::config(uint8_t modem) { // set regulator mode + uint8_t* data = new uint8_t[1]; data[0] = SX126X_REGULATOR_DC_DC; - state = SPIwriteCommand(SX126X_CMD_SET_REGULATOR_MODE, data, 1); + int16_t state = SPIwriteCommand(SX126X_CMD_SET_REGULATOR_MODE, data, 1); if(state != ERR_NONE) { return(state); } diff --git a/src/modules/SX126x.h b/src/modules/SX126x.h index a9a84b4b..0139e191 100644 --- a/src/modules/SX126x.h +++ b/src/modules/SX126x.h @@ -678,6 +678,13 @@ class SX126x: public PhysicalLayer { */ float getSNR(); + /*! + \brief Set DIO2 to function as RF switch (default in Semtech example designs). + + \returns \ref status_codes + */ + int16_t setDio2AsRfSwitch(bool enable = true); + protected: // SX1276x SPI command implementations int16_t setTx(uint32_t timeout = 0); @@ -720,6 +727,8 @@ class SX126x: public PhysicalLayer { float _dataRate; + bool _dio2RfSwitch = false; + int16_t config(uint8_t modem); // common low-level SPI interface