jgromes / RadioLib

Universal wireless communication library for embedded devices
https://jgromes.github.io/RadioLib/
MIT License
1.49k stars 376 forks source link

Add function to set DIO2 as RF switch control #16

Closed beegee-tokyo closed 5 years ago

beegee-tokyo commented 5 years ago

Hope I am not getting annoying with my issues.

I got my Insight DSP4520 modules. These modules combines a nRF52 and a SX1262 into one package. Unfortunately on these boards the DIO2 is used as RF switch control and this cannot be changed. Is there any hope that I can change the library to setup DIO2 as RF switch control? I found in SX126x.cpp this:

int16_t SX126x::config(uint8_t modem) {
  // set DIO2 as IRQ
  uint8_t* data = new uint8_t[1];
  data[0] = SX126X_DIO2_AS_IRQ;
  Serial.println("Setting DIO2 as RF switch control");
  int16_t state = SPIwriteCommand(SX126X_CMD_SET_DIO2_AS_RF_SWITCH_CTRL, data, 1);
  if(state != ERR_NONE) {
    return(state);
  }
...

and

int16_t SX126x::setDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask) {
  uint8_t data[8] = {(uint8_t)((irqMask >> 8) & 0xFF), (uint8_t)(irqMask & 0xFF),
                     (uint8_t)((dio1Mask >> 8) & 0xFF), (uint8_t)(dio1Mask & 0xFF),
                     (uint8_t)((dio2Mask >> 8) & 0xFF), (uint8_t)(dio2Mask & 0xFF),
                     (uint8_t)((dio3Mask >> 8) & 0xFF), (uint8_t)(dio3Mask & 0xFF)};
  return(SPIwriteCommand(SX126X_CMD_SET_DIO_IRQ_PARAMS, data, 8));
}

I changed it to

int16_t SX126x::config(uint8_t modem) {
  // set DIO2 as IRQ
  uint8_t* data = new uint8_t[1];
#ifdef DSP4520
  data[0] = SX126X_DIO2_AS_RF_SWITCH;
#else
  data[0] = SX126X_DIO2_AS_IRQ;
  Serial.println("Setting DIO2 as RF switch control");
#endif
  int16_t state = SPIwriteCommand(SX126X_CMD_SET_DIO2_AS_RF_SWITCH_CTRL, data, 1);
  if(state != ERR_NONE) {
    return(state);
  }
...

and

int16_t SX126x::setDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask) {
#ifdef DSP4520
  // This will break the CAD functionality
  dio2Mask = dio2Mask = SX126X_IRQ_NONE;
  Serial.println("Setting DIO2 to SX126X_IRQ_NONE");
  uint8_t data[8] = {(uint8_t)((irqMask >> 8) & 0xFF), (uint8_t)(irqMask & 0xFF),
                     (uint8_t)((dio1Mask >> 8) & 0xFF), (uint8_t)(dio1Mask & 0xFF),
                     (uint8_t)((dio2Mask >> 8) & 0xFF), (uint8_t)(dio2Mask & 0xFF),
                     (uint8_t)((dio3Mask >> 8) & 0xFF), (uint8_t)(dio3Mask & 0xFF)};
#else
  uint8_t data[8] = {(uint8_t)((irqMask >> 8) & 0xFF), (uint8_t)(irqMask & 0xFF),
                     (uint8_t)((dio1Mask >> 8) & 0xFF), (uint8_t)(dio1Mask & 0xFF),
                     (uint8_t)((dio2Mask >> 8) & 0xFF), (uint8_t)(dio2Mask & 0xFF),
                     (uint8_t)((dio3Mask >> 8) & 0xFF), (uint8_t)(dio3Mask & 0xFF)};
#endif
  return(SPIwriteCommand(SX126X_CMD_SET_DIO_IRQ_PARAMS, data, 8));
}

Just for testing. But that might not be the only functions that needs to be changed. Can you point me to where I should search for additional locations that needs to be changed. I know that this will break the CAD function, but I really would like to use your library, because it is much easier to setup than the SEMTECH example library for the SX126x chips.

If I get it to work, I will think about how to make this configurable (e.g. in the begin() function) and prepare a pull-request.

jgromes commented 5 years ago

You shouldn't change anything in the SX126x SPI command implementations (such as setDioIrqParams() method). Instead, find all calls to setDioIrqParams() and remove any references to DIO2 configuration, so that it doesn't get overwritten after setting it as RF switch in config(). The only call to setDioIrqParams() that changes DIO2 should be in CAD method.

DIO2 configuration could be added, but I don't think it should be in the begin() method, there's already enough parameters as it is. Since there's already a method to set DIO3 as TCXO control, something similar to that could be implemented. Default setting would be DIO2 as IRQ, user would be able to change that by calling e.g. setRFSwitch(bool enable) to change it to RF switch control (and disable CAD in the process).

beegee-tokyo commented 5 years ago

Got it. I like your idea with the additional method to set DIO2 as RF switch. Will look how to implement it and test it.

Got the French ISP4250 (with DIO2 as RF switch) already talking to my Chinese E22 modules, your library makes it so easy to adapt.

beegee-tokyo commented 5 years ago

Closed against https://github.com/jgromes/RadioLib/pull/17