From c9b68163d51af68b6603b6a32e06c16b6aa5b6ef Mon Sep 17 00:00:00 2001 From: jgromes Date: Tue, 4 Jun 2019 10:13:04 +0200 Subject: [PATCH] [SX126x] Added notes about DIO2 RF control --- .../SX126x_Channel_Activity_Detection.ino | 9 ++++- .../SX126x_Settings/SX126x_Settings.ino | 9 +++++ keywords.txt | 1 + src/TypeDef.h | 2 +- src/modules/SX126x.cpp | 38 +++++++++---------- 5 files changed, 37 insertions(+), 22 deletions(-) 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 0750f181..91c53b09 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 @@ -59,11 +59,16 @@ void loop() { if (state == LORA_DETECTED) { // LoRa preamble was detected - Serial.println(F(" detected!")); + Serial.println(F("detected!")); } else if (state == CHANNEL_FREE) { // no preamble was detected, channel is free - Serial.println(F(" 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.")); } diff --git a/examples/SX126x/SX126x_Settings/SX126x_Settings.ino b/examples/SX126x/SX126x_Settings/SX126x_Settings.ino index 52553045..7d9114b0 100644 --- a/examples/SX126x/SX126x_Settings/SX126x_Settings.ino +++ b/examples/SX126x/SX126x_Settings/SX126x_Settings.ino @@ -13,6 +13,7 @@ - CRC - preamble length - TCXO voltage + - DIO2 RF switch control Other modules from SX126x family can also be used. @@ -146,6 +147,14 @@ void setup() { while (true); } + // 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! + if (loraSX1262.setDio2AsRfSwitch() != ERR_NONE) { + Serial.println(F("Failed to set DIO2 as RF switch!")); + while (true); + } Serial.println(F("All settings succesfully changed!")); } diff --git a/keywords.txt b/keywords.txt index 31d82ea1..d9a7b7f2 100644 --- a/keywords.txt +++ b/keywords.txt @@ -232,3 +232,4 @@ 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 efda5ef0..6a91a4c9 100644 --- a/src/TypeDef.h +++ b/src/TypeDef.h @@ -464,7 +464,7 @@ /*! \brief SX126x scan channel not possible because DIO2 is used as RF antenna switch. */ -#define ERR_DIO2_UNAVAIL_CAD_FAILED -708 +#define ERR_CAD_UNAVAILABLE -708 /*! \} diff --git a/src/modules/SX126x.cpp b/src/modules/SX126x.cpp index 8291857c..17369cab 100644 --- a/src/modules/SX126x.cpp +++ b/src/modules/SX126x.cpp @@ -33,11 +33,6 @@ 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); @@ -59,6 +54,12 @@ int16_t SX126x::begin(float bw, uint8_t sf, uint8_t cr, uint16_t syncWord, uint1 } state = setPreambleLength(preambleLength); + if(state != ERR_NONE) { + return(state); + } + + // set publicly accessible settings that are not a part of begin method + state = setDio2AsRfSwitch(false); return(state); } @@ -91,11 +92,6 @@ 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); @@ -121,9 +117,14 @@ int16_t SX126x::beginFSK(float br, float freqDev, float rxBw, uint16_t preambleL return(state); } - // set default sync word 0x2D01 - not a beginFSK attribute + // set publicly accessible settings that are not a part of begin method uint8_t sync[] = {0x2D, 0x01}; state = setSyncWord(sync, 2); + if(state != ERR_NONE) { + return(state); + } + + state = setDio2AsRfSwitch(false); return(state); } @@ -286,7 +287,7 @@ int16_t SX126x::scanChannel() { if (_dio2RfSwitch) { // If DIO2 is used as RF switch this function does not work - return(ERR_DIO2_UNAVAIL_CAD_FAILED); + return(ERR_CAD_UNAVAILABLE); } // set mode to standby @@ -1074,16 +1075,15 @@ int16_t SX126x::setFrequencyRaw(float freq) { } int16_t SX126x::setDio2AsRfSwitch(bool enable) { - uint8_t data[1]; - if (enable) { - // set DIO2 as RF switch - data[0] = SX126X_DIO2_AS_RF_SWITCH; + uint8_t data = 0; + if(enable) { + data = SX126X_DIO2_AS_RF_SWITCH; } else { - data[0] = SX126X_DIO2_AS_IRQ; + data = SX126X_DIO2_AS_IRQ; } - int16_t state = SPIwriteCommand(SX126X_CMD_SET_DIO2_AS_RF_SWITCH_CTRL, data, 1); + int16_t state = SPIwriteCommand(SX126X_CMD_SET_DIO2_AS_RF_SWITCH_CTRL, &data, 1); - if (state == ERR_NONE) { + if(state == ERR_NONE) { _dio2RfSwitch = enable; } return(state);