LCOV - code coverage report
Current view: top level - src/modules/SX126x - SX126x_commands.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 43 144 29.9 %
Date: 2026-02-22 10:42:45 Functions: 11 32 34.4 %

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

Generated by: LCOV version 1.14