[SX126x] Changed CAD to only use DIO1 (#19)

This commit is contained in:
jgromes 2019-06-07 10:05:40 +02:00
parent c9b68163d5
commit eddf96bbb2
6 changed files with 37 additions and 51 deletions

View file

@ -65,10 +65,10 @@ void loop() {
// no preamble was detected, channel is 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) { } else {
// no preamble was detected, channel is free // some other error occurred
Serial.println(F("unable to perform scan!")); Serial.print(F("failed, code "));
Serial.println(F("[SX1262] Disable DIO2 RF control to run CAD.")); Serial.println(state);
} }

View file

@ -150,7 +150,7 @@ void setup() {
// Some SX126x modules use DIO2 as RF switch. To enable // Some SX126x modules use DIO2 as RF switch. To enable
// this feature, the following method can be used. // this feature, the following method can be used.
// NOTE: As long as DIO2 is configured to control RF switch, // 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) { if (loraSX1262.setDio2AsRfSwitch() != ERR_NONE) {
Serial.println(F("Failed to set DIO2 as RF switch!")); Serial.println(F("Failed to set DIO2 as RF switch!"));
while (true); while (true);

View file

@ -232,4 +232,3 @@ ERR_INVALID_MODULATION_PARAMETERS LITERAL1
ERR_SPI_CMD_TIMEOUT LITERAL1 ERR_SPI_CMD_TIMEOUT LITERAL1
ERR_SPI_CMD_INVALID LITERAL1 ERR_SPI_CMD_INVALID LITERAL1
ERR_SPI_CMD_FAILED LITERAL1 ERR_SPI_CMD_FAILED LITERAL1
ERR_CAD_UNAVAILABLE LITERAL1

View file

@ -461,11 +461,6 @@
*/ */
#define ERR_SPI_CMD_FAILED -707 #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
/*! /*!
\} \}
*/ */

View file

@ -285,11 +285,6 @@ int16_t SX126x::scanChannel() {
return(ERR_WRONG_MODEM); 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 // set mode to standby
int16_t state = standby(); int16_t state = standby();
if(state != ERR_NONE) { if(state != ERR_NONE) {
@ -297,7 +292,7 @@ int16_t SX126x::scanChannel() {
} }
// set DIO pin mapping // 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) { if(state != ERR_NONE) {
return(state); return(state);
} }
@ -315,17 +310,21 @@ int16_t SX126x::scanChannel() {
} }
// wait for channel activity detected or timeout // wait for channel activity detected or timeout
while(!digitalRead(_mod->getInt0())) { while(!digitalRead(_mod->getInt0()));
if(digitalRead(_mod->getInt1())) {
// check CAD result
uint16_t cadResult = getIrqStatus();
if(cadResult & SX126X_IRQ_CAD_DETECTED) {
// detected some LoRa activity
clearIrqStatus(); clearIrqStatus();
return(LORA_DETECTED); return(LORA_DETECTED);
} } else if(cadResult & SX126X_IRQ_CAD_DONE) {
} // channel is free
// clear interrupt flags
clearIrqStatus(); clearIrqStatus();
return(CHANNEL_FREE); return(CHANNEL_FREE);
}
return(ERR_UNKNOWN);
} }
int16_t SX126x::sleep() { int16_t SX126x::sleep() {
@ -911,6 +910,16 @@ int16_t SX126x::setTCXO(float voltage, uint32_t timeout) {
return(ERR_NONE); 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) { 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)}; 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)); 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() { uint16_t SX126x::getIrqStatus() {
uint8_t data[2]; uint8_t data[2];
SPIreadCommand(SX126X_CMD_GET_IRQ_STATUS, 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) { int16_t SX126x::clearIrqStatus(uint16_t clearIrqParams) {
@ -1074,21 +1083,6 @@ int16_t SX126x::setFrequencyRaw(float freq) {
return(ERR_NONE); 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) { int16_t SX126x::config(uint8_t modem) {
// set regulator mode // set regulator mode
uint8_t* data = new uint8_t[1]; uint8_t* data = new uint8_t[1];

View file

@ -657,6 +657,13 @@ class SX126x: public PhysicalLayer {
*/ */
int16_t setTCXO(float voltage, uint32_t timeout = 5000); 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. \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(); 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: protected:
// SX1276x SPI command implementations // SX1276x SPI command implementations
int16_t setTx(uint32_t timeout = 0); int16_t setTx(uint32_t timeout = 0);
@ -727,8 +727,6 @@ class SX126x: public PhysicalLayer {
float _dataRate; float _dataRate;
bool _dio2RfSwitch = false;
int16_t config(uint8_t modem); int16_t config(uint8_t modem);
// common low-level SPI interface // common low-level SPI interface