diff --git a/examples/SX126x/SX126x_Channel_Activity_Detection/SX126x_Channel_Activity_Detection.ino b/examples/SX126x/SX126x_Channel_Activity_Detection/SX126x_Channel_Activity_Detection.ino index 91c53b09..9b2fb275 100644 --- a/examples/SX126x/SX126x_Channel_Activity_Detection/SX126x_Channel_Activity_Detection.ino +++ b/examples/SX126x/SX126x_Channel_Activity_Detection/SX126x_Channel_Activity_Detection.ino @@ -65,10 +65,10 @@ void loop() { // no preamble was detected, channel is free Serial.println(F("channel is free!")); - } else if (state == ERR_CAD_UNAVAILABLE) { - // no preamble was detected, channel is free - Serial.println(F("unable to perform scan!")); - Serial.println(F("[SX1262] Disable DIO2 RF control to run CAD.")); + } else { + // some other error occurred + Serial.print(F("failed, code ")); + Serial.println(state); } diff --git a/examples/SX126x/SX126x_Settings/SX126x_Settings.ino b/examples/SX126x/SX126x_Settings/SX126x_Settings.ino index 7d9114b0..beb4b455 100644 --- a/examples/SX126x/SX126x_Settings/SX126x_Settings.ino +++ b/examples/SX126x/SX126x_Settings/SX126x_Settings.ino @@ -150,7 +150,7 @@ void setup() { // Some SX126x modules use DIO2 as RF switch. To enable // this feature, the following method can be used. // NOTE: As long as DIO2 is configured to control RF switch, - // Channel Activity Detection is disabled! + // it can't be used as interrupt pin! if (loraSX1262.setDio2AsRfSwitch() != ERR_NONE) { Serial.println(F("Failed to set DIO2 as RF switch!")); while (true); diff --git a/keywords.txt b/keywords.txt index d9a7b7f2..31d82ea1 100644 --- a/keywords.txt +++ b/keywords.txt @@ -232,4 +232,3 @@ ERR_INVALID_MODULATION_PARAMETERS LITERAL1 ERR_SPI_CMD_TIMEOUT LITERAL1 ERR_SPI_CMD_INVALID LITERAL1 ERR_SPI_CMD_FAILED LITERAL1 -ERR_CAD_UNAVAILABLE LITERAL1 diff --git a/src/TypeDef.h b/src/TypeDef.h index 6a91a4c9..b007f408 100644 --- a/src/TypeDef.h +++ b/src/TypeDef.h @@ -461,11 +461,6 @@ */ #define ERR_SPI_CMD_FAILED -707 -/*! - \brief SX126x scan channel not possible because DIO2 is used as RF antenna switch. -*/ -#define ERR_CAD_UNAVAILABLE -708 - /*! \} */ diff --git a/src/modules/SX126x.cpp b/src/modules/SX126x.cpp index 17369cab..7332b340 100644 --- a/src/modules/SX126x.cpp +++ b/src/modules/SX126x.cpp @@ -285,11 +285,6 @@ int16_t SX126x::scanChannel() { return(ERR_WRONG_MODEM); } - if (_dio2RfSwitch) { - // If DIO2 is used as RF switch this function does not work - return(ERR_CAD_UNAVAILABLE); - } - // set mode to standby int16_t state = standby(); if(state != ERR_NONE) { @@ -297,7 +292,7 @@ int16_t SX126x::scanChannel() { } // set DIO pin mapping - state = setDioIrqParams(SX126X_IRQ_CAD_DETECTED | SX126X_IRQ_CAD_DONE, SX126X_IRQ_CAD_DONE, SX126X_IRQ_CAD_DETECTED); + state = setDioIrqParams(SX126X_IRQ_CAD_DETECTED | SX126X_IRQ_CAD_DONE, SX126X_IRQ_CAD_DETECTED | SX126X_IRQ_CAD_DONE); if(state != ERR_NONE) { return(state); } @@ -315,17 +310,21 @@ int16_t SX126x::scanChannel() { } // wait for channel activity detected or timeout - while(!digitalRead(_mod->getInt0())) { - if(digitalRead(_mod->getInt1())) { - clearIrqStatus(); - return(LORA_DETECTED); - } + while(!digitalRead(_mod->getInt0())); + + // check CAD result + uint16_t cadResult = getIrqStatus(); + if(cadResult & SX126X_IRQ_CAD_DETECTED) { + // detected some LoRa activity + clearIrqStatus(); + return(LORA_DETECTED); + } else if(cadResult & SX126X_IRQ_CAD_DONE) { + // channel is free + clearIrqStatus(); + return(CHANNEL_FREE); } - // clear interrupt flags - clearIrqStatus(); - - return(CHANNEL_FREE); + return(ERR_UNKNOWN); } int16_t SX126x::sleep() { @@ -911,6 +910,16 @@ int16_t SX126x::setTCXO(float voltage, uint32_t timeout) { return(ERR_NONE); } +int16_t SX126x::setDio2AsRfSwitch(bool enable) { + uint8_t data = 0; + if(enable) { + data = SX126X_DIO2_AS_RF_SWITCH; + } else { + data = SX126X_DIO2_AS_IRQ; + } + return(SPIwriteCommand(SX126X_CMD_SET_DIO2_AS_RF_SWITCH_CTRL, &data, 1)); +} + int16_t SX126x::setTx(uint32_t timeout) { uint8_t data[3] = {(uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF)}; return(SPIwriteCommand(SX126X_CMD_SET_TX, data, 3)); @@ -971,7 +980,7 @@ int16_t SX126x::setDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t di uint16_t SX126x::getIrqStatus() { uint8_t data[2]; SPIreadCommand(SX126X_CMD_GET_IRQ_STATUS, data, 2); - return(((uint16_t)(data[1]) << 8) | data[0]); + return(((uint16_t)(data[0]) << 8) | data[1]); } int16_t SX126x::clearIrqStatus(uint16_t clearIrqParams) { @@ -1074,21 +1083,6 @@ int16_t SX126x::setFrequencyRaw(float freq) { return(ERR_NONE); } -int16_t SX126x::setDio2AsRfSwitch(bool enable) { - uint8_t data = 0; - if(enable) { - data = SX126X_DIO2_AS_RF_SWITCH; - } else { - data = 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]; diff --git a/src/modules/SX126x.h b/src/modules/SX126x.h index 0139e191..e2d995bc 100644 --- a/src/modules/SX126x.h +++ b/src/modules/SX126x.h @@ -657,6 +657,13 @@ class SX126x: public PhysicalLayer { */ int16_t setTCXO(float voltage, uint32_t timeout = 5000); + /*! + \brief Set DIO2 to function as RF switch (default in Semtech example designs). + + \returns \ref status_codes + */ + int16_t setDio2AsRfSwitch(bool enable = true); + /*! \brief Gets effective data rate for the last transmitted packet. The value is calculated only for payload bytes. @@ -678,13 +685,6 @@ 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); @@ -727,8 +727,6 @@ class SX126x: public PhysicalLayer { float _dataRate; - bool _dio2RfSwitch = false; - int16_t config(uint8_t modem); // common low-level SPI interface