LCOV - code coverage report
Current view: top level - src/modules/SX126x - SX126x_commands.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 142 0.0 %
Date: 2025-10-24 15:14:50 Functions: 0 31 0.0 %

          Line data    Source code
       1             : #include "SX126x.h"
       2             : 
       3             : // this file contains implementation of all commands
       4             : // supported by the SX126x SPI interface
       5             : // in most cases, the names of methods match those in the datasheet
       6             : // however, sometimes slight changes had to be made in order to
       7             : // better fit the RadioLib API
       8             : 
       9             : #if !RADIOLIB_EXCLUDE_SX126X
      10             : 
      11           0 : int16_t SX126x::sleep() {
      12           0 :   return(SX126x::sleep(true));
      13             : }
      14             : 
      15           0 : int16_t SX126x::sleep(bool retainConfig) {
      16             :   // set RF switch (if present)
      17           0 :   this->mod->setRfSwitchState(Module::MODE_IDLE);
      18             : 
      19           0 :   uint8_t sleepMode = RADIOLIB_SX126X_SLEEP_START_WARM | RADIOLIB_SX126X_SLEEP_RTC_OFF;
      20           0 :   if(!retainConfig) {
      21           0 :     sleepMode = RADIOLIB_SX126X_SLEEP_START_COLD | RADIOLIB_SX126X_SLEEP_RTC_OFF;
      22             :   }
      23           0 :   int16_t state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_SLEEP, &sleepMode, 1, false, false);
      24             : 
      25             :   // wait for SX126x to safely enter sleep mode
      26           0 :   this->mod->hal->delay(1);
      27             : 
      28           0 :   return(state);
      29             : }
      30             : 
      31           0 : int16_t SX126x::standby() {
      32           0 :   return(SX126x::standby(this->standbyXOSC ? RADIOLIB_SX126X_STANDBY_XOSC : RADIOLIB_SX126X_STANDBY_RC));
      33             : }
      34             : 
      35           0 : int16_t SX126x::standby(uint8_t mode, bool wakeup) {
      36             :   // set RF switch (if present)
      37           0 :   this->mod->setRfSwitchState(Module::MODE_IDLE);
      38             : 
      39           0 :   if(wakeup) {
      40             :     // send a NOP command - this pulls the NSS low to exit the sleep mode,
      41             :     // while preventing interference with possible other SPI transactions
      42             :     // see https://github.com/jgromes/RadioLib/discussions/1364
      43           0 :     (void)this->mod->SPIwriteStream((uint16_t)RADIOLIB_SX126X_CMD_NOP, NULL, 0, false, false);
      44             :   }
      45             : 
      46           0 :   const uint8_t data[] = { mode };
      47           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_STANDBY, data, 1));
      48             : }
      49             : 
      50           0 : int16_t SX126x::setFs() {
      51           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_FS, NULL, 0));
      52             : }
      53             : 
      54           0 : int16_t SX126x::setTx(uint32_t timeout) {
      55           0 :   const uint8_t data[] = { (uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF)} ;
      56           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_TX, data, 3));
      57             : }
      58             : 
      59           0 : int16_t SX126x::setRx(uint32_t timeout) {
      60           0 :   const uint8_t data[] = { (uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF) };
      61           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_RX, data, 3, true, false));
      62             : }
      63             : 
      64           0 : int16_t SX126x::setCad(uint8_t symbolNum, uint8_t detPeak, uint8_t detMin, uint8_t exitMode, RadioLibTime_t timeout) {
      65             :   // default CAD parameters are selected according to recommendations on Semtech DS.SX1261-2.W.APP rev. 1.1, page 92.
      66             : 
      67             :   // build the packet with default configuration
      68             :   uint8_t data[7];
      69           0 :   data[0] = RADIOLIB_SX126X_CAD_ON_4_SYMB;
      70           0 :   data[1] = this->spreadingFactor + 13;
      71           0 :   data[2] = RADIOLIB_SX126X_CAD_PARAM_DET_MIN;
      72           0 :   data[3] = RADIOLIB_SX126X_CAD_GOTO_STDBY;
      73           0 :   uint32_t timeout_raw = (float)timeout / 15.625f;
      74           0 :   data[4] = (uint8_t)((timeout_raw >> 16) & 0xFF);
      75           0 :   data[5] = (uint8_t)((timeout_raw >> 8) & 0xFF);
      76           0 :   data[6] = (uint8_t)(timeout_raw & 0xFF);
      77             : 
      78             :   // set user-provided values
      79           0 :   if(symbolNum != RADIOLIB_SX126X_CAD_PARAM_DEFAULT) {
      80           0 :     data[0] = symbolNum;
      81             :   }
      82             : 
      83           0 :   if(detPeak != RADIOLIB_SX126X_CAD_PARAM_DEFAULT) {
      84           0 :     data[1] = detPeak;
      85             :   }
      86             : 
      87           0 :   if(detMin != RADIOLIB_SX126X_CAD_PARAM_DEFAULT) {
      88           0 :     data[2] = detMin;
      89             :   }
      90             : 
      91           0 :   if(exitMode != RADIOLIB_SX126X_CAD_PARAM_DEFAULT) {
      92           0 :     data[3] = exitMode;
      93             :   }
      94             : 
      95             :   // configure parameters
      96           0 :   int16_t state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_CAD_PARAMS, data, 7);
      97           0 :   RADIOLIB_ASSERT(state);
      98             : 
      99             :   // start CAD
     100           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_CAD, NULL, 0));
     101             : }
     102             : 
     103           0 : int16_t SX126x::setPaConfig(uint8_t paDutyCycle, uint8_t deviceSel, uint8_t hpMax, uint8_t paLut) {
     104           0 :   const uint8_t data[] = { paDutyCycle, hpMax, deviceSel, paLut };
     105           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_PA_CONFIG, data, 4));
     106             : }
     107             : 
     108           0 : int16_t SX126x::writeRegister(uint16_t addr, const uint8_t* data, uint8_t numBytes) {
     109           0 :   this->mod->SPIwriteRegisterBurst(addr, data, numBytes);
     110           0 :   return(RADIOLIB_ERR_NONE);
     111             : }
     112             : 
     113           0 : int16_t SX126x::readRegister(uint16_t addr, uint8_t* data, uint8_t numBytes) {
     114             :   // send the command
     115           0 :   this->mod->SPIreadRegisterBurst(addr, numBytes, data);
     116             : 
     117             :   // check the status
     118           0 :   int16_t state = this->mod->SPIcheckStream();
     119           0 :   return(state);
     120             : }
     121             : 
     122           0 : int16_t SX126x::writeBuffer(const uint8_t* data, uint8_t numBytes, uint8_t offset) {
     123           0 :   const uint8_t cmd[] = { RADIOLIB_SX126X_CMD_WRITE_BUFFER, offset };
     124           0 :   return(this->mod->SPIwriteStream(cmd, 2, data, numBytes));
     125             : }
     126             : 
     127           0 : int16_t SX126x::readBuffer(uint8_t* data, uint8_t numBytes, uint8_t offset) {
     128           0 :   const uint8_t cmd[] = { RADIOLIB_SX126X_CMD_READ_BUFFER, offset };
     129           0 :   return(this->mod->SPIreadStream(cmd, 2, data, numBytes));
     130             : }
     131             : 
     132           0 : int16_t SX126x::setDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask) {
     133           0 :   const uint8_t data[8] = {(uint8_t)((irqMask >> 8) & 0xFF), (uint8_t)(irqMask & 0xFF),
     134           0 :                      (uint8_t)((dio1Mask >> 8) & 0xFF), (uint8_t)(dio1Mask & 0xFF),
     135           0 :                      (uint8_t)((dio2Mask >> 8) & 0xFF), (uint8_t)(dio2Mask & 0xFF),
     136           0 :                      (uint8_t)((dio3Mask >> 8) & 0xFF), (uint8_t)(dio3Mask & 0xFF)};
     137           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_DIO_IRQ_PARAMS, data, 8));
     138             : }
     139             : 
     140           0 : int16_t SX126x::clearIrqStatus(uint16_t clearIrqParams) {
     141           0 :   const uint8_t data[] = { (uint8_t)((clearIrqParams >> 8) & 0xFF), (uint8_t)(clearIrqParams & 0xFF) };
     142           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CLEAR_IRQ_STATUS, data, 2));
     143             : }
     144             : 
     145           0 : int16_t SX126x::setRfFrequency(uint32_t frf) {
     146           0 :   const uint8_t data[] = { (uint8_t)((frf >> 24) & 0xFF), (uint8_t)((frf >> 16) & 0xFF), (uint8_t)((frf >> 8) & 0xFF), (uint8_t)(frf & 0xFF) };
     147           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_RF_FREQUENCY, data, 4));
     148             : }
     149             : 
     150           0 : int16_t SX126x::calibrateImage(const uint8_t* data) {
     151           0 :   int16_t state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CALIBRATE_IMAGE, data, 2);
     152             : 
     153             :   // if something failed, show the device errors
     154             :   #if RADIOLIB_DEBUG_BASIC
     155             :   if(state != RADIOLIB_ERR_NONE) {
     156             :     // unless mode is forced to standby, device errors will be 0
     157             :     standby();
     158             :     uint16_t errors = getDeviceErrors();
     159             :     RADIOLIB_DEBUG_BASIC_PRINTLN("Calibration failed, device errors: 0x%X", errors);
     160             :   }
     161             :   #endif
     162           0 :   return(state);
     163             : }
     164             : 
     165           0 : uint8_t SX126x::getPacketType() {
     166           0 :   uint8_t data = 0xFF;
     167           0 :   this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_PACKET_TYPE, &data, 1);
     168           0 :   return(data);
     169             : }
     170             : 
     171           0 : int16_t SX126x::setTxParams(uint8_t pwr, uint8_t rampTime) {
     172           0 :   const uint8_t data[] = { pwr, rampTime };
     173           0 :   int16_t state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_TX_PARAMS, data, 2);
     174           0 :   if(state == RADIOLIB_ERR_NONE) {
     175           0 :     this->pwr = pwr;
     176             :   }
     177           0 :   return(state);
     178             : }
     179             : 
     180           0 : int16_t SX126x::setModulationParams(uint8_t sf, uint8_t bw, uint8_t cr, uint8_t ldro) {
     181             :   // calculate symbol length and enable low data rate optimization, if auto-configuration is enabled
     182           0 :   if(this->ldroAuto) {
     183           0 :     float symbolLength = (float)(uint32_t(1) << this->spreadingFactor) / (float)this->bandwidthKhz;
     184           0 :     if(symbolLength >= 16.0f) {
     185           0 :       this->ldrOptimize = RADIOLIB_SX126X_LORA_LOW_DATA_RATE_OPTIMIZE_ON;
     186             :     } else {
     187           0 :       this->ldrOptimize = RADIOLIB_SX126X_LORA_LOW_DATA_RATE_OPTIMIZE_OFF;
     188             :     }
     189             :   } else {
     190           0 :     this->ldrOptimize = ldro;
     191             :   }
     192             :   // 500/9/8  - 0x09 0x04 0x03 0x00 - SF9, BW125, 4/8
     193             :   // 500/11/8 - 0x0B 0x04 0x03 0x00 - SF11 BW125, 4/7
     194           0 :   const uint8_t data[4] = {sf, bw, cr, this->ldrOptimize};
     195           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_MODULATION_PARAMS, data, 4));
     196             : }
     197             : 
     198           0 : int16_t SX126x::setModulationParamsFSK(uint32_t br, uint8_t sh, uint8_t rxBw, uint32_t freqDev) {
     199           0 :   const uint8_t data[8] = {(uint8_t)((br >> 16) & 0xFF), (uint8_t)((br >> 8) & 0xFF), (uint8_t)(br & 0xFF),
     200             :                      sh, rxBw,
     201           0 :                      (uint8_t)((freqDev >> 16) & 0xFF), (uint8_t)((freqDev >> 8) & 0xFF), (uint8_t)(freqDev & 0xFF)};
     202           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_MODULATION_PARAMS, data, 8));
     203             : }
     204             : 
     205           0 : int16_t SX126x::setModulationParamsBPSK(uint32_t br, uint8_t sh) {
     206           0 :   const uint8_t data[] = {(uint8_t)((br >> 16) & 0xFF), (uint8_t)((br >> 8) & 0xFF), (uint8_t)(br & 0xFF), sh};
     207           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_MODULATION_PARAMS, data, sizeof(data)));
     208             : }
     209             : 
     210           0 : int16_t SX126x::setPacketParams(uint16_t preambleLen, uint8_t crcType, uint8_t payloadLen, uint8_t hdrType, uint8_t invertIQ) {
     211           0 :   int16_t state = fixInvertedIQ(invertIQ);
     212           0 :   RADIOLIB_ASSERT(state);
     213           0 :   const uint8_t data[6] = {(uint8_t)((preambleLen >> 8) & 0xFF), (uint8_t)(preambleLen & 0xFF), hdrType, payloadLen, crcType, invertIQ};
     214           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_PACKET_PARAMS, data, 6));
     215             : }
     216             : 
     217           0 : int16_t SX126x::setPacketParamsFSK(uint16_t preambleLen, uint8_t preambleDetectorLen, uint8_t crcType, uint8_t syncWordLen, uint8_t addrCmp, uint8_t whiten, uint8_t packType, uint8_t payloadLen) {
     218           0 :   const uint8_t data[9] = {(uint8_t)((preambleLen >> 8) & 0xFF), (uint8_t)(preambleLen & 0xFF),
     219             :                      preambleDetectorLen, syncWordLen, addrCmp,
     220           0 :                      packType, payloadLen, crcType, whiten};
     221           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_PACKET_PARAMS, data, 9));
     222             : }
     223             : 
     224           0 : int16_t SX126x::setPacketParamsBPSK(uint8_t payloadLen, uint16_t rampUpDelay, uint16_t rampDownDelay, uint16_t payloadLenBits) {
     225             :   const uint8_t data[] = { payloadLen,
     226           0 :     (uint8_t)((rampUpDelay >> 8) & 0xFF), (uint8_t)(rampUpDelay & 0xFF),
     227           0 :     (uint8_t)((rampDownDelay >> 8) & 0xFF), (uint8_t)(rampDownDelay & 0xFF),
     228           0 :     (uint8_t)((payloadLenBits >> 8) & 0xFF), (uint8_t)(payloadLenBits & 0xFF)
     229           0 :   };
     230             :   
     231             :   // this one is a bit different, it seems to be split into command transaction and then a register write
     232           0 :   int16_t state = this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_PACKET_PARAMS, data, sizeof(uint8_t));
     233           0 :   RADIOLIB_ASSERT(state);
     234           0 :   return(this->writeRegister(RADIOLIB_SX126X_REG_BPSK_PACKET_PARAMS, &data[1], sizeof(data) - sizeof(uint8_t)));
     235             : }
     236             : 
     237           0 : int16_t SX126x::setBufferBaseAddress(uint8_t txBaseAddress, uint8_t rxBaseAddress) {
     238           0 :   const uint8_t data[2] = {txBaseAddress, rxBaseAddress};
     239           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_BUFFER_BASE_ADDRESS, data, 2));
     240             : }
     241             : 
     242           0 : int16_t SX126x::setRegulatorMode(uint8_t mode) {
     243           0 :   const uint8_t data[1] = {mode};
     244           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_REGULATOR_MODE, data, 1));
     245             : }
     246             : 
     247           0 : uint8_t SX126x::getStatus() {
     248           0 :   uint8_t data = 0;
     249           0 :   this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_STATUS, &data, 0);
     250           0 :   return(data);
     251             : }
     252             : 
     253           0 : uint32_t SX126x::getPacketStatus() {
     254           0 :   uint8_t data[3] = {0, 0, 0};
     255           0 :   this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_PACKET_STATUS, data, 3);
     256           0 :   return((((uint32_t)data[0]) << 16) | (((uint32_t)data[1]) << 8) | (uint32_t)data[2]);
     257             : }
     258             : 
     259           0 : uint16_t SX126x::getDeviceErrors() {
     260           0 :   uint8_t data[2] = {0, 0};
     261           0 :   this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_DEVICE_ERRORS, data, 2);
     262           0 :   uint16_t opError = (((uint16_t)data[0] & 0xFF) << 8) | ((uint16_t)data[1]);
     263           0 :   return(opError);
     264             : }
     265             : 
     266           0 : int16_t SX126x::clearDeviceErrors() {
     267           0 :   const uint8_t data[2] = {RADIOLIB_SX126X_CMD_NOP, RADIOLIB_SX126X_CMD_NOP};
     268           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_CLEAR_DEVICE_ERRORS, data, 2));
     269             : }
     270             : 
     271             : #endif

Generated by: LCOV version 1.14