LCOV - code coverage report
Current view: top level - src/modules/SX126x - SX126x.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 176 804 21.9 %
Date: 2026-04-20 19:58:46 Functions: 30 61 49.2 %

          Line data    Source code
       1             : #include "SX126x.h"
       2             : #include <string.h>
       3             : #include <math.h>
       4             : #if !RADIOLIB_EXCLUDE_SX126X
       5             : 
       6          18 : SX126x::SX126x(Module* mod) : PhysicalLayer() {
       7          18 :   this->freqStep = RADIOLIB_SX126X_FREQUENCY_STEP_SIZE;
       8          18 :   this->maxPacketLength = RADIOLIB_SX126X_MAX_PACKET_LENGTH;
       9          18 :   this->mod = mod;
      10          18 :   this->XTAL = false;
      11          18 :   this->standbyXOSC = false;
      12          18 :   this->irqMap[RADIOLIB_IRQ_TX_DONE] = RADIOLIB_SX126X_IRQ_TX_DONE;
      13          18 :   this->irqMap[RADIOLIB_IRQ_RX_DONE] = RADIOLIB_SX126X_IRQ_RX_DONE;
      14          18 :   this->irqMap[RADIOLIB_IRQ_PREAMBLE_DETECTED] = RADIOLIB_SX126X_IRQ_PREAMBLE_DETECTED;
      15          18 :   this->irqMap[RADIOLIB_IRQ_SYNC_WORD_VALID] = RADIOLIB_SX126X_IRQ_SYNC_WORD_VALID;
      16          18 :   this->irqMap[RADIOLIB_IRQ_HEADER_VALID] = RADIOLIB_SX126X_IRQ_HEADER_VALID;
      17          18 :   this->irqMap[RADIOLIB_IRQ_HEADER_ERR] = RADIOLIB_SX126X_IRQ_HEADER_ERR;
      18          18 :   this->irqMap[RADIOLIB_IRQ_CRC_ERR] = RADIOLIB_SX126X_IRQ_CRC_ERR;
      19          18 :   this->irqMap[RADIOLIB_IRQ_CAD_DONE] = RADIOLIB_SX126X_IRQ_CAD_DONE;
      20          18 :   this->irqMap[RADIOLIB_IRQ_CAD_DETECTED] = RADIOLIB_SX126X_IRQ_CAD_DETECTED;
      21          18 :   this->irqMap[RADIOLIB_IRQ_TIMEOUT] = RADIOLIB_SX126X_IRQ_TIMEOUT;
      22          18 : }
      23             : 
      24           0 : int16_t SX126x::begin(uint8_t cr, uint8_t syncWord, uint16_t preambleLength, float tcxoVoltage, bool useRegulatorLDO) {
      25             :   // BW in kHz and SF are required in order to calculate LDRO for setModulationParams
      26             :   // set the defaults, this will get overwritten later anyway
      27           0 :   this->bandwidthKhz = 500.0;
      28           0 :   this->spreadingFactor = 9;
      29             : 
      30             :   // initialize configuration variables (will be overwritten during public settings configuration)
      31           0 :   this->bandwidth = RADIOLIB_SX126X_LORA_BW_500_0;  // initialized to 500 kHz, since lower values will interfere with LLCC68
      32           0 :   this->codingRate = RADIOLIB_SX126X_LORA_CR_4_7;
      33           0 :   this->ldrOptimize = 0x00;
      34           0 :   this->crcTypeLoRa = RADIOLIB_SX126X_LORA_CRC_ON;
      35           0 :   this->preambleLengthLoRa = preambleLength;
      36           0 :   this->tcxoDelay = 0;
      37           0 :   this->headerType = RADIOLIB_SX126X_LORA_HEADER_EXPLICIT;
      38           0 :   this->implicitLen = 0xFF;
      39             : 
      40             :   // set module properties and perform initial setup
      41           0 :   int16_t state = this->modSetup(tcxoVoltage, useRegulatorLDO, RADIOLIB_SX126X_PACKET_TYPE_LORA);
      42           0 :   RADIOLIB_ASSERT(state);
      43             : 
      44             :   // configure publicly accessible settings
      45           0 :   state = setCodingRate(cr);
      46           0 :   RADIOLIB_ASSERT(state);
      47             : 
      48           0 :   state = setSyncWord(syncWord);
      49           0 :   RADIOLIB_ASSERT(state);
      50             : 
      51           0 :   state = setPreambleLength(preambleLength);
      52           0 :   RADIOLIB_ASSERT(state);
      53             : 
      54             :   // set publicly accessible settings that are not a part of begin method
      55           0 :   state = setCurrentLimit(60.0);
      56           0 :   RADIOLIB_ASSERT(state);
      57             : 
      58           0 :   state = setDio2AsRfSwitch(true);
      59           0 :   RADIOLIB_ASSERT(state);
      60             : 
      61           0 :   state = setCRC(2);
      62           0 :   RADIOLIB_ASSERT(state);
      63             : 
      64           0 :   state = invertIQ(false);
      65           0 :   RADIOLIB_ASSERT(state);
      66             : 
      67           0 :   return(state);
      68             : }
      69             : 
      70           0 : int16_t SX126x::beginFSK(float br, float freqDev, float rxBw, uint16_t preambleLength, float tcxoVoltage, bool useRegulatorLDO) {
      71             :   // initialize configuration variables (will be overwritten during public settings configuration)
      72           0 :   this->bitRate = 21333;                                  // 48.0 kbps
      73           0 :   this->frequencyDev = 52428;                             // 50.0 kHz
      74           0 :   this->rxBandwidth = RADIOLIB_SX126X_GFSK_RX_BW_156_2;
      75           0 :   this->rxBandwidthKhz = 156.2;
      76           0 :   this->pulseShape = RADIOLIB_SX126X_GFSK_FILTER_GAUSS_0_5;
      77           0 :   this->crcTypeFSK = RADIOLIB_SX126X_GFSK_CRC_2_BYTE_INV;     // CCITT CRC configuration
      78           0 :   this->preambleLengthFSK = preambleLength;
      79             : 
      80             :   // set module properties and perform initial setup
      81           0 :   int16_t state = this->modSetup(tcxoVoltage, useRegulatorLDO, RADIOLIB_SX126X_PACKET_TYPE_GFSK);
      82           0 :   RADIOLIB_ASSERT(state);
      83             : 
      84             :   // configure publicly accessible settings
      85           0 :   state = setBitRate(br);
      86           0 :   RADIOLIB_ASSERT(state);
      87             : 
      88           0 :   state = setFrequencyDeviation(freqDev);
      89           0 :   RADIOLIB_ASSERT(state);
      90             : 
      91           0 :   state = setRxBandwidth(rxBw);
      92           0 :   RADIOLIB_ASSERT(state);
      93             : 
      94           0 :   state = setCurrentLimit(60.0);
      95           0 :   RADIOLIB_ASSERT(state);
      96             : 
      97           0 :   state = setPreambleLength(preambleLength);
      98           0 :   RADIOLIB_ASSERT(state);
      99             : 
     100             :   // set publicly accessible settings that are not a part of begin method
     101           0 :   uint8_t sync[] = {0x12, 0xAD};
     102           0 :   state = setSyncWord(sync, 2);
     103           0 :   RADIOLIB_ASSERT(state);
     104             : 
     105           0 :   state = setDataShaping(RADIOLIB_SHAPING_NONE);
     106           0 :   RADIOLIB_ASSERT(state);
     107             : 
     108           0 :   state = setEncoding(RADIOLIB_ENCODING_NRZ);
     109           0 :   RADIOLIB_ASSERT(state);
     110             : 
     111           0 :   state = variablePacketLengthMode(RADIOLIB_SX126X_MAX_PACKET_LENGTH);
     112           0 :   RADIOLIB_ASSERT(state);
     113             : 
     114           0 :   state = setCRC(2);
     115           0 :   RADIOLIB_ASSERT(state);
     116             : 
     117           0 :   state = setDio2AsRfSwitch(true);
     118           0 :   RADIOLIB_ASSERT(state);
     119             : 
     120           0 :   return(state);
     121             : }
     122             : 
     123           0 : int16_t SX126x::beginBPSK(float br, float tcxoVoltage, bool useRegulatorLDO) {
     124             :   // set module properties and perform initial setup
     125           0 :   int16_t state = this->modSetup(tcxoVoltage, useRegulatorLDO, RADIOLIB_SX126X_PACKET_TYPE_BPSK);
     126           0 :   RADIOLIB_ASSERT(state);
     127             : 
     128             :   // configure publicly accessible settings
     129           0 :   state = setBitRate(br);
     130           0 :   RADIOLIB_ASSERT(state);
     131             :   
     132             :   // set publicly accessible settings that are not a part of begin method
     133           0 :   state = setDio2AsRfSwitch(true);
     134           0 :   RADIOLIB_ASSERT(state);
     135             : 
     136           0 :   return(state);
     137             : }
     138             : 
     139           0 : int16_t SX126x::beginLRFHSS(uint8_t bw, uint8_t cr, bool narrowGrid, float tcxoVoltage, bool useRegulatorLDO) {
     140           0 :   this->lrFhssGridNonFcc = narrowGrid;
     141             :   
     142             :   // set module properties and perform initial setup
     143           0 :   int16_t state = this->modSetup(tcxoVoltage, useRegulatorLDO, RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS);
     144           0 :   RADIOLIB_ASSERT(state);
     145             : 
     146             :   // set publicly accessible settings that are not a part of begin method
     147           0 :   state = setCurrentLimit(60.0);
     148           0 :   RADIOLIB_ASSERT(state);
     149             : 
     150           0 :   state = setDio2AsRfSwitch(true);
     151           0 :   RADIOLIB_ASSERT(state);
     152             : 
     153             :   // set all packet params to 0 (packet engine is disabled in LR-FHSS mode)
     154           0 :   state = setPacketParamsFSK(0, 0, 0, 0, 0, 0, 0, 0);
     155           0 :   RADIOLIB_ASSERT(state);
     156             : 
     157             :   // set bit rate
     158           0 :   this->rxBandwidth = 0;
     159           0 :   this->frequencyDev = 0;
     160           0 :   this->pulseShape = RADIOLIB_SX126X_GFSK_FILTER_GAUSS_1;
     161           0 :   state = setBitRate(0.48828125f);
     162           0 :   RADIOLIB_ASSERT(state);
     163             : 
     164           0 :   return(setLrFhssConfig(bw, cr));
     165             : }
     166             : 
     167           0 : int16_t SX126x::setLrFhssConfig(uint8_t bw, uint8_t cr, uint8_t hdrCount, uint16_t hopSeqId) {
     168             :   // check and cache all parameters
     169           0 :   RADIOLIB_CHECK_RANGE((int8_t)cr, (int8_t)RADIOLIB_SX126X_LR_FHSS_CR_5_6, (int8_t)RADIOLIB_SX126X_LR_FHSS_CR_1_3, RADIOLIB_ERR_INVALID_CODING_RATE);
     170           0 :   this->lrFhssCr = cr;
     171           0 :   RADIOLIB_CHECK_RANGE((int8_t)bw, (int8_t)RADIOLIB_SX126X_LR_FHSS_BW_39_06, (int8_t)RADIOLIB_SX126X_LR_FHSS_BW_1574_2, RADIOLIB_ERR_INVALID_BANDWIDTH);
     172           0 :   this->lrFhssBw = bw;
     173           0 :   RADIOLIB_CHECK_RANGE(hdrCount, 1, 4, RADIOLIB_ERR_INVALID_BIT_RANGE);
     174           0 :   this->lrFhssHdrCount = hdrCount;
     175           0 :   RADIOLIB_CHECK_RANGE((int16_t)hopSeqId, (int16_t)0x000, (int16_t)0x1FF, RADIOLIB_ERR_INVALID_DATA_SHAPING);
     176           0 :   this->lrFhssHopSeqId = hopSeqId;
     177           0 :   return(RADIOLIB_ERR_NONE);
     178             : }
     179             : 
     180           0 : int16_t SX126x::reset(bool verify) {
     181             :   // run the reset sequence
     182           0 :   this->mod->hal->pinMode(this->mod->getRst(), this->mod->hal->GpioModeOutput);
     183           0 :   this->mod->hal->digitalWrite(this->mod->getRst(), this->mod->hal->GpioLevelLow);
     184           0 :   this->mod->hal->delay(1);
     185           0 :   this->mod->hal->digitalWrite(this->mod->getRst(), this->mod->hal->GpioLevelHigh);
     186             : 
     187             :   // return immediately when verification is disabled
     188           0 :   if(!verify) {
     189           0 :     return(RADIOLIB_ERR_NONE);
     190             :   }
     191             : 
     192             :   // set mode to standby - SX126x often refuses first few commands after reset
     193           0 :   RadioLibTime_t start = this->mod->hal->millis();
     194             :   while(true) {
     195             :     // try to set mode to standby
     196           0 :     int16_t state = standby();
     197           0 :     if(state == RADIOLIB_ERR_NONE) {
     198             :       // standby command successful
     199           0 :       return(RADIOLIB_ERR_NONE);
     200             :     }
     201             : 
     202             :     // standby command failed, check timeout and try again
     203           0 :     if(this->mod->hal->millis() - start >= 1000) {
     204             :       // timed out, possibly incorrect wiring
     205           0 :       return(state);
     206             :     }
     207             : 
     208             :     // wait a bit to not spam the module
     209           0 :     this->mod->hal->delay(10);
     210           0 :   }
     211             : }
     212             : 
     213           3 : int16_t SX126x::transmit(const uint8_t* data, size_t len, uint8_t addr) {
     214             :   // set mode to standby
     215           3 :   int16_t state = standby();
     216           3 :   RADIOLIB_ASSERT(state);
     217             : 
     218             :   // check packet length
     219           0 :   if(this->codingRate > RADIOLIB_SX126X_LORA_CR_4_8) {
     220             :     // Long Interleaver needs at least 8 bytes
     221           0 :     if(len < 8) {
     222           0 :       return(RADIOLIB_ERR_PACKET_TOO_SHORT);
     223             :     }
     224             : 
     225             :     // Long Interleaver supports up to 253 bytes if CRC is enabled
     226           0 :     if(this->crcTypeLoRa == RADIOLIB_SX126X_LORA_CRC_ON && (len > RADIOLIB_SX126X_MAX_PACKET_LENGTH - 2)) {
     227           0 :       return(RADIOLIB_ERR_PACKET_TOO_LONG);
     228             :     }  
     229             :   } 
     230           0 :   if(len > RADIOLIB_SX126X_MAX_PACKET_LENGTH) {
     231           0 :     return(RADIOLIB_ERR_PACKET_TOO_LONG);
     232             :   }
     233             : 
     234             :   // calculate timeout in ms (5ms + 500 % of expected time-on-air)
     235           0 :   RadioLibTime_t timeout = 5 + (getTimeOnAir(len) * 5) / 1000;
     236             :   RADIOLIB_DEBUG_BASIC_PRINTLN("Timeout in %lu ms", timeout);
     237             : 
     238             :   // start transmission
     239           0 :   state = startTransmit(data, len, addr);
     240           0 :   RADIOLIB_ASSERT(state);
     241             : 
     242             :   // wait for packet transmission or timeout
     243           0 :   uint8_t modem = getPacketType();
     244           0 :   RadioLibTime_t start = this->mod->hal->millis();
     245             :   while(true) {
     246             :     // yield for  multi-threaded platforms
     247           0 :     this->mod->hal->yield();
     248             : 
     249             :     // check timeout
     250           0 :     if(this->mod->hal->millis() - start > timeout) {
     251           0 :       finishTransmit();
     252           0 :       return(RADIOLIB_ERR_TX_TIMEOUT);
     253             :     }
     254             : 
     255             :     // poll the interrupt pin
     256           0 :     if(this->mod->hal->digitalRead(this->mod->getIrq())) {
     257             :       // in LoRa or GFSK, only Tx done interrupt is enabled
     258           0 :       if(modem != RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS) {
     259           0 :         break;
     260             :       }
     261             : 
     262             :       // in LR-FHSS, IRQ signals both Tx done as frequency hop request
     263           0 :       if(this->getIrqFlags() & RADIOLIB_SX126X_IRQ_TX_DONE) {
     264           0 :         break;
     265             :       } else {
     266             :         // handle frequency hop
     267           0 :         this->hopLRFHSS();
     268             :       }
     269             :     }
     270             :   }
     271             : 
     272           0 :   return(finishTransmit());
     273             : }
     274             : 
     275           3 : int16_t SX126x::receive(uint8_t* data, size_t len, RadioLibTime_t timeout) {
     276             :   // set mode to standby
     277           3 :   int16_t state = standby();
     278           3 :   RADIOLIB_ASSERT(state);
     279             : 
     280           0 :   RadioLibTime_t timeoutInternal = timeout;
     281           0 :   if(!timeoutInternal) {
     282             :     // calculate timeout (500 % of expected time-one-air)
     283           0 :     size_t maxLen = len;
     284           0 :     if(len == 0) { maxLen = RADIOLIB_SX126X_MAX_PACKET_LENGTH; }
     285           0 :     timeoutInternal = (getTimeOnAir(maxLen) * 5) / 1000;
     286             :   }
     287             : 
     288             :   RADIOLIB_DEBUG_BASIC_PRINTLN("Timeout in %lu ms", timeoutInternal);
     289             : 
     290             :   // start reception
     291           0 :   uint32_t timeoutValue = (uint32_t)(((float)timeoutInternal * 1000.0f) / 15.625f);
     292           0 :   state = startReceive(timeoutValue);
     293           0 :   RADIOLIB_ASSERT(state);
     294             : 
     295             :   // wait for packet reception or timeout
     296           0 :   bool softTimeout = false;
     297           0 :   RadioLibTime_t start = this->mod->hal->millis();
     298           0 :   while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
     299           0 :     this->mod->hal->yield();
     300             :     // safety check, the timeout should be done by the radio
     301           0 :     if(this->mod->hal->millis() - start > timeoutInternal) {
     302           0 :       softTimeout = true;
     303           0 :       break;
     304             :     }
     305             :   }
     306             : 
     307             :   // if it was a timeout, this will return an error code
     308           0 :   state = standby();
     309           0 :   if((state != RADIOLIB_ERR_NONE) && (state != RADIOLIB_ERR_SPI_CMD_TIMEOUT)) {
     310           0 :     return(state);
     311             :   }
     312             : 
     313             :   // check whether this was a timeout or not
     314           0 :   if(softTimeout || (getIrqFlags() & this->irqMap[RADIOLIB_IRQ_TIMEOUT])) {
     315           0 :     (void)finishReceive();
     316           0 :     return(RADIOLIB_ERR_RX_TIMEOUT);
     317             :   }
     318             : 
     319             :   // read the received data
     320           0 :   return(readData(data, len));
     321             : }
     322             : 
     323           3 : int16_t SX126x::transmitDirect(uint32_t frf) {
     324             :   // set RF switch (if present)
     325           3 :   this->mod->setRfSwitchState(this->txMode);
     326             : 
     327             :   // user requested to start transmitting immediately (required for RTTY)
     328           3 :   int16_t state = RADIOLIB_ERR_NONE;
     329           3 :   if(frf != 0) {
     330           0 :     state = setRfFrequency(frf);
     331             :   }
     332           3 :   RADIOLIB_ASSERT(state);
     333             : 
     334             :   // direct mode activation intentionally skipped here, as it seems to lead to much worse results
     335           3 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_TX_CONTINUOUS_WAVE, NULL, 0));
     336             : }
     337             : 
     338           3 : int16_t SX126x::receiveDirect() {
     339             :   // set RF switch (if present)
     340           3 :   this->mod->setRfSwitchState(Module::MODE_RX);
     341             : 
     342             :   // SX126x is unable to output received data directly
     343           3 :   return(RADIOLIB_ERR_UNKNOWN);
     344             : }
     345             : 
     346           0 : int16_t SX126x::directMode() {
     347             :   // check modem
     348           0 :   if(getPacketType() != RADIOLIB_SX126X_PACKET_TYPE_GFSK) {
     349           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     350             :   }
     351             : 
     352             :   // set mode to standby
     353           0 :   int16_t state = standby();
     354           0 :   RADIOLIB_ASSERT(state);
     355             : 
     356             :   // disable DIO2 RF switch
     357           0 :   state = setDio2AsRfSwitch(false);
     358           0 :   RADIOLIB_ASSERT(state);
     359             : 
     360             :   // set DIO2 to clock output and DIO3 to data input
     361             :   // this is done exclusively by writing magic values to even more magic registers
     362           0 :   state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_TX_BITBANG_ENABLE_1, RADIOLIB_SX126X_TX_BITBANG_1_ENABLED, 6, 4);
     363           0 :   RADIOLIB_ASSERT(state);
     364           0 :   state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_TX_BITBANG_ENABLE_0, RADIOLIB_SX126X_TX_BITBANG_0_ENABLED, 3, 0);
     365           0 :   RADIOLIB_ASSERT(state);
     366           0 :   state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_DIOX_OUT_ENABLE, RADIOLIB_SX126X_DIO3_OUT_DISABLED, 3, 3);
     367           0 :   RADIOLIB_ASSERT(state);
     368           0 :   state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_DIOX_IN_ENABLE, RADIOLIB_SX126X_DIO3_IN_ENABLED, 3, 3);
     369           0 :   RADIOLIB_ASSERT(state);
     370             : 
     371             :   // enable TxDone interrupt
     372           0 :   state = setDioIrqParams(RADIOLIB_SX126X_IRQ_TX_DONE, RADIOLIB_SX126X_IRQ_TX_DONE);
     373           0 :   RADIOLIB_ASSERT(state);
     374             : 
     375             :   // set preamble length to the maximum to prevent SX126x from exiting Tx mode for a while
     376           0 :   state = setPreambleLength(0xFFFF);
     377           0 :   RADIOLIB_ASSERT(state);
     378             : 
     379           0 :   return(state);
     380             : }
     381             : 
     382           0 : int16_t SX126x::packetMode() {
     383             :   // set mode to standby
     384           0 :   int16_t state = standby();
     385           0 :   RADIOLIB_ASSERT(state);
     386             : 
     387             :   // set preamble length to the default
     388           0 :   state = setPreambleLength(16);
     389           0 :   RADIOLIB_ASSERT(state);
     390             : 
     391             :   // disable TxDone interrupt
     392           0 :   state = setDioIrqParams(RADIOLIB_SX126X_IRQ_NONE, RADIOLIB_SX126X_IRQ_NONE);
     393           0 :   RADIOLIB_ASSERT(state);
     394             : 
     395             :   // restore the magic registers
     396           0 :   state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_DIOX_IN_ENABLE, RADIOLIB_SX126X_DIO3_IN_DISABLED, 3, 3);
     397           0 :   RADIOLIB_ASSERT(state);
     398           0 :   state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_DIOX_OUT_ENABLE, RADIOLIB_SX126X_DIO3_OUT_ENABLED, 3, 3);
     399           0 :   RADIOLIB_ASSERT(state);
     400           0 :   state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_TX_BITBANG_ENABLE_0, RADIOLIB_SX126X_TX_BITBANG_0_DISABLED, 3, 0);
     401           0 :   RADIOLIB_ASSERT(state);
     402           0 :   state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_TX_BITBANG_ENABLE_1, RADIOLIB_SX126X_TX_BITBANG_1_DISABLED, 6, 4);
     403           0 :   RADIOLIB_ASSERT(state);
     404             : 
     405             :   // enable DIO2 RF switch
     406           0 :   state = setDio2AsRfSwitch(true);
     407           0 :   RADIOLIB_ASSERT(state);
     408             : 
     409           0 :   return(state);
     410             : }
     411             : 
     412           3 : int16_t SX126x::scanChannel() {
     413           3 :   ChannelScanConfig_t cfg = {
     414             :     .cad = {
     415             :       .symNum = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
     416             :       .detPeak = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
     417             :       .detMin = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
     418             :       .exitMode = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
     419             :       .timeout = 0,
     420             :       .irqFlags = RADIOLIB_IRQ_CAD_DEFAULT_FLAGS,
     421             :       .irqMask = RADIOLIB_IRQ_CAD_DEFAULT_MASK,
     422             :     },
     423             :   };
     424           6 :   return(this->scanChannel(cfg));
     425             : }
     426             : 
     427           6 : int16_t SX126x::scanChannel(const ChannelScanConfig_t &config) {
     428             :   // set mode to CAD
     429           6 :   int state = startChannelScan(config);
     430           6 :   RADIOLIB_ASSERT(state);
     431             : 
     432             :   // wait for channel activity detected or timeout
     433           0 :   while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
     434           0 :     this->mod->hal->yield();
     435             :   }
     436             : 
     437             :   // check CAD result
     438           0 :   return(getChannelScanResult());
     439             : }
     440             : 
     441           0 : int16_t SX126x::hopLRFHSS() {
     442           0 :   if(!(this->getIrqFlags() & RADIOLIB_SX126X_IRQ_LR_FHSS_HOP)) {
     443           0 :     return(RADIOLIB_ERR_TX_TIMEOUT);
     444             :   }
     445             : 
     446           0 :   int16_t state = this->setLRFHSSHop(this->lrFhssHopNum % 16);
     447           0 :   RADIOLIB_ASSERT(state);
     448           0 :   return(clearIrqStatus());
     449             : }
     450             : 
     451           3 : int16_t SX126x::finishTransmit() {
     452             :   // clear interrupt flags
     453           3 :   int16_t state = clearIrqStatus();
     454           3 :   RADIOLIB_ASSERT(state);
     455             : 
     456             :   // set mode to standby to disable transmitter/RF switch
     457           0 :   return(standby());
     458             : }
     459             : 
     460           3 : int16_t SX126x::finishReceive() {
     461             :   // set mode to standby to disable RF switch
     462           3 :   int16_t state = standby();
     463           3 :   RADIOLIB_ASSERT(state);
     464             : 
     465             :   // try to fix timeout error in implicit header mode
     466             :   // check for modem type and header mode is done in fixImplicitTimeout()
     467           0 :   state = fixImplicitTimeout();
     468           0 :   RADIOLIB_ASSERT(state);
     469             : 
     470             :   // clear interrupt flags
     471           0 :   return(clearIrqStatus());
     472             : }
     473             : 
     474           3 : int16_t SX126x::startReceive() {
     475           3 :   return(this->startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF, RADIOLIB_IRQ_RX_DEFAULT_FLAGS, RADIOLIB_IRQ_RX_DEFAULT_MASK, 0));
     476             : }
     477             : 
     478           0 : int16_t SX126x::startReceiveDutyCycle(uint32_t rxPeriod, uint32_t sleepPeriod, RadioLibIrqFlags_t irqFlags, RadioLibIrqFlags_t irqMask) {
     479             :   // datasheet claims time to go to sleep is ~500us, same to wake up, compensate for that with 1 ms + TCXO delay
     480           0 :   uint32_t transitionTime = this->tcxoDelay + 1000;
     481           0 :   sleepPeriod -= transitionTime;
     482             : 
     483             :   // divide by 15.625
     484           0 :   uint32_t rxPeriodRaw = (rxPeriod * 8) / 125;
     485           0 :   uint32_t sleepPeriodRaw = (sleepPeriod * 8) / 125;
     486             : 
     487             :   // check 24 bit limit and zero value (likely not intended)
     488           0 :   if((rxPeriodRaw & 0xFF000000) || (rxPeriodRaw == 0)) {
     489           0 :     return(RADIOLIB_ERR_INVALID_RX_PERIOD);
     490             :   }
     491             : 
     492             :   // this check of the high byte also catches underflow when we subtracted transitionTime
     493           0 :   if((sleepPeriodRaw & 0xFF000000) || (sleepPeriodRaw == 0)) {
     494           0 :     return(RADIOLIB_ERR_INVALID_SLEEP_PERIOD);
     495             :   }
     496             : 
     497           0 :   int16_t state = startReceiveCommon(RADIOLIB_SX126X_RX_TIMEOUT_INF, irqFlags, irqMask);
     498           0 :   RADIOLIB_ASSERT(state);
     499             : 
     500           0 :   const uint8_t data[6] = {(uint8_t)((rxPeriodRaw >> 16) & 0xFF), (uint8_t)((rxPeriodRaw >> 8) & 0xFF), (uint8_t)(rxPeriodRaw & 0xFF),
     501           0 :                      (uint8_t)((sleepPeriodRaw >> 16) & 0xFF), (uint8_t)((sleepPeriodRaw >> 8) & 0xFF), (uint8_t)(sleepPeriodRaw & 0xFF)};
     502           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_RX_DUTY_CYCLE, data, 6));
     503             : }
     504             : 
     505           0 : int16_t SX126x::startReceiveDutyCycleAuto(uint16_t senderPreambleLength, uint16_t minSymbols, RadioLibIrqFlags_t irqFlags, RadioLibIrqFlags_t irqMask) {
     506           0 :   if(senderPreambleLength == 0) {
     507           0 :     senderPreambleLength = this->preambleLengthLoRa;
     508           0 :   } else if (senderPreambleLength > this->preambleLengthLoRa) {
     509             :     // the unit must be configured to expect a preamble length at least as long as the sender is using
     510           0 :     return(RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH);
     511             :   }
     512           0 :   if(minSymbols == 0) {
     513           0 :     if (this->spreadingFactor <= 6) {
     514           0 :       minSymbols = 12;
     515             :     } else {
     516           0 :       minSymbols = 8;
     517             :     }
     518             :   }
     519             : 
     520             :   // worst case is that the sender starts transmitting when we're just less than minSymbols from going back to sleep.
     521             :   // in this case, we don't catch minSymbols before going to sleep,
     522             :   // so we must be awake for at least that long before the sender stops transmitting.
     523           0 :   uint16_t sleepSymbols = senderPreambleLength - 2 * minSymbols;
     524             : 
     525             :   // if we're not to sleep at all, just use the standard startReceive.
     526           0 :   if(2 * minSymbols > senderPreambleLength) {
     527           0 :     return(startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF, irqFlags, irqMask));
     528             :   }
     529             : 
     530           0 :   uint32_t symbolLength = ((uint32_t)(10 * 1000) << this->spreadingFactor) / (10 * this->bandwidthKhz);
     531           0 :   uint32_t sleepPeriod = symbolLength * sleepSymbols;
     532             :   RADIOLIB_DEBUG_BASIC_PRINTLN("Auto sleep period: %lu", (long unsigned int)sleepPeriod);
     533             : 
     534             :   // when the unit detects a preamble, it starts a timer that will timeout if it doesn't receive a header in time.
     535             :   // the duration is sleepPeriod + 2 * wakePeriod.
     536             :   // The sleepPeriod doesn't take into account shutdown and startup time for the unit (~1ms)
     537             :   // We need to ensure that the timeout is longer than senderPreambleLength.
     538             :   // So we must satisfy: wakePeriod > (preamblePeriod - (sleepPeriod - 1000)) / 2. (A)
     539             :   // we also need to ensure the unit is awake to see at least minSymbols. (B)
     540           0 :   uint32_t wakePeriod = RADIOLIB_MAX(
     541             :     (symbolLength * (senderPreambleLength + 1) - (sleepPeriod - 1000)) / 2, // (A)
     542             :     symbolLength * (minSymbols + 1)); //(B)
     543             :   RADIOLIB_DEBUG_BASIC_PRINTLN("Auto wake period: %lu", (long unsigned int)wakePeriod);
     544             : 
     545             :   // If our sleep period is shorter than our transition time, just use the standard startReceive
     546           0 :   if(sleepPeriod < this->tcxoDelay + 1016) {
     547           0 :     return(startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF, irqFlags, irqMask));
     548             :   }
     549             : 
     550           0 :   return(startReceiveDutyCycle(wakePeriod, sleepPeriod, irqFlags, irqMask));
     551             : }
     552             : 
     553           9 : int16_t SX126x::startReceiveCommon(uint32_t timeout, RadioLibIrqFlags_t irqFlags, RadioLibIrqFlags_t irqMask) {
     554             :   // ensure we are in standby
     555           9 :   int16_t state = standby();
     556           9 :   RADIOLIB_ASSERT(state);
     557             : 
     558             :   // set DIO mapping
     559           0 :   if(timeout != RADIOLIB_SX126X_RX_TIMEOUT_INF) {
     560           0 :     irqMask |= (1UL << RADIOLIB_IRQ_TIMEOUT);
     561             :   }
     562           0 :   state = setDioIrqParams(getIrqMapped(irqFlags), getIrqMapped(irqMask));
     563           0 :   RADIOLIB_ASSERT(state);
     564             : 
     565             :   // set buffer pointers
     566           0 :   state = setBufferBaseAddress();
     567           0 :   RADIOLIB_ASSERT(state);
     568             : 
     569             :   // clear interrupt flags
     570           0 :   state = clearIrqStatus();
     571           0 :   RADIOLIB_ASSERT(state);
     572             : 
     573             :   // restore original packet length
     574           0 :   uint8_t modem = getPacketType();
     575           0 :   if(modem == RADIOLIB_SX126X_PACKET_TYPE_LORA) {
     576           0 :     state = setPacketParams(this->preambleLengthLoRa, this->crcTypeLoRa, this->implicitLen, this->headerType, this->invertIQEnabled);
     577           0 :   } else if(modem == RADIOLIB_SX126X_PACKET_TYPE_GFSK) {
     578           0 :     state = setPacketParamsFSK(this->preambleLengthFSK, this->preambleDetLength, this->crcTypeFSK, this->syncWordLength, RADIOLIB_SX126X_GFSK_ADDRESS_FILT_OFF, this->whitening,
     579           0 :       this->packetType, (this->packetType == RADIOLIB_SX126X_GFSK_PACKET_FIXED) ? this->implicitLen : RADIOLIB_SX126X_MAX_PACKET_LENGTH);
     580             :   } else {
     581           0 :     return(RADIOLIB_ERR_UNKNOWN);
     582             :   }
     583             : 
     584           0 :   return(state);
     585             : }
     586             : 
     587           3 : int16_t SX126x::readData(uint8_t* data, size_t len) {
     588             :   // this method may get called from receive() after Rx timeout
     589             :   // if that's the case, the first call will return "SPI command timeout error"
     590             :   // check the IRQ to be sure this really originated from timeout event
     591           3 :   int16_t state = this->mod->SPIcheckStream();
     592           3 :   uint16_t irq = getIrqFlags();
     593           3 :   if((state == RADIOLIB_ERR_SPI_CMD_TIMEOUT) && (irq & RADIOLIB_SX126X_IRQ_TIMEOUT)) {
     594             :     // this is definitely Rx timeout
     595           0 :     return(RADIOLIB_ERR_RX_TIMEOUT);
     596             :   }
     597           3 :   RADIOLIB_ASSERT(state);
     598             : 
     599             :   // check integrity CRC
     600           0 :   int16_t crcState = RADIOLIB_ERR_NONE;
     601             :   // Report CRC mismatch when there's a payload CRC error, or a header error and no valid header (to avoid false alarm from previous packet)
     602           0 :   if((irq & RADIOLIB_SX126X_IRQ_CRC_ERR) || ((irq & RADIOLIB_SX126X_IRQ_HEADER_ERR) && !(irq & RADIOLIB_SX126X_IRQ_HEADER_VALID))) {
     603           0 :     crcState = RADIOLIB_ERR_CRC_MISMATCH;
     604             :   }
     605             :   
     606             :   // get packet length and Rx buffer offset
     607           0 :   uint8_t offset = 0;
     608           0 :   size_t length = getPacketLength(true, &offset);
     609           0 :   if((len != 0) && (len < length)) {
     610             :     // user requested less data than we got, only return what was requested
     611           0 :     length = len;
     612             :   }
     613             : 
     614             :   // read packet data starting at offset
     615           0 :   state = readBuffer(data, length, offset);
     616           0 :   RADIOLIB_ASSERT(state);
     617             : 
     618             :   // clear interrupt flags
     619           0 :   state = clearIrqStatus();
     620             : 
     621             :   // check if CRC failed - this is done after reading data to give user the option to keep them
     622           0 :   RADIOLIB_ASSERT(crcState);
     623             : 
     624           0 :   return(state);
     625             : }
     626             : 
     627           3 : int16_t SX126x::startChannelScan() {
     628           3 :   ChannelScanConfig_t cfg = {
     629             :     .cad = {
     630             :       .symNum = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
     631             :       .detPeak = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
     632             :       .detMin = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
     633             :       .exitMode = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
     634             :       .timeout = 0,
     635             :       .irqFlags = RADIOLIB_IRQ_CAD_DEFAULT_FLAGS,
     636             :       .irqMask = RADIOLIB_IRQ_CAD_DEFAULT_MASK,
     637             :     },
     638             :   };
     639           6 :   return(this->startChannelScan(cfg));
     640             : }
     641             : 
     642          12 : int16_t SX126x::startChannelScan(const ChannelScanConfig_t &config) {
     643             :   // check active modem
     644          12 :   if(getPacketType() != RADIOLIB_SX126X_PACKET_TYPE_LORA) {
     645          12 :     return(RADIOLIB_ERR_WRONG_MODEM);
     646             :   }
     647             : 
     648             :   // set mode to standby
     649           0 :   int16_t state = standby();
     650           0 :   RADIOLIB_ASSERT(state);
     651             : 
     652             :   // set RF switch (if present)
     653           0 :   this->mod->setRfSwitchState(Module::MODE_RX);
     654             : 
     655             :   // set DIO pin mapping
     656           0 :   state = setDioIrqParams(getIrqMapped(config.cad.irqFlags), getIrqMapped(config.cad.irqMask));
     657           0 :   RADIOLIB_ASSERT(state);
     658             : 
     659             :   // clear interrupt flags
     660           0 :   state = clearIrqStatus();
     661           0 :   RADIOLIB_ASSERT(state);
     662             : 
     663             :   // set mode to CAD
     664           0 :   state = setCad(config.cad.symNum, config.cad.detPeak, config.cad.detMin, config.cad.exitMode, config.cad.timeout);
     665           0 :   return(state);
     666             : }
     667             : 
     668           3 : int16_t SX126x::getChannelScanResult() {
     669             :   // check active modem
     670           3 :   if(getPacketType() != RADIOLIB_SX126X_PACKET_TYPE_LORA) {
     671           3 :     return(RADIOLIB_ERR_WRONG_MODEM);
     672             :   }
     673             : 
     674             :   // check CAD result
     675           0 :   uint16_t cadResult = getIrqFlags();
     676           0 :   if(cadResult & RADIOLIB_SX126X_IRQ_CAD_DETECTED) {
     677             :     // detected some LoRa activity
     678           0 :     return(RADIOLIB_LORA_DETECTED);
     679           0 :   } else if(cadResult & RADIOLIB_SX126X_IRQ_CAD_DONE) {
     680             :     // channel is free
     681           0 :     return(RADIOLIB_CHANNEL_FREE);
     682             :   }
     683             : 
     684           0 :   return(RADIOLIB_ERR_UNKNOWN);
     685             : }
     686             : 
     687           3 : float SX126x::getRSSI() {
     688           3 :   return(this->getRSSI(true));
     689             : }
     690             : 
     691           3 : float SX126x::getRSSI(bool packet) {
     692           3 :   if(packet) { 
     693             :     // get last packet RSSI from packet status
     694           3 :     uint32_t packetStatus = getPacketStatus();
     695           3 :     uint8_t rssiPkt = packetStatus & 0xFF;
     696           3 :     return(-1.0 * rssiPkt/2.0);
     697             :   } else {
     698             :     // get instantaneous RSSI value
     699           0 :     uint8_t rssiRaw = 0;
     700           0 :     this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_RSSI_INST, &rssiRaw, 1);
     701           0 :     return((float)rssiRaw / (-2.0f));
     702             :   }
     703             : }
     704             : 
     705           3 : float SX126x::getSNR() {
     706             :   // check active modem
     707           3 :   if(getPacketType() != RADIOLIB_SX126X_PACKET_TYPE_LORA) {
     708           3 :     return(RADIOLIB_ERR_WRONG_MODEM);
     709             :   }
     710             : 
     711             :   // get last packet SNR from packet status
     712           0 :   uint32_t packetStatus = getPacketStatus();
     713           0 :   uint8_t snrPkt = (packetStatus >> 8) & 0xFF;
     714           0 :   if(snrPkt < 128) {
     715           0 :     return(snrPkt/4.0);
     716             :   } else {
     717           0 :     return((snrPkt - 256)/4.0);
     718             :   }
     719             : }
     720             : 
     721           0 : float SX126x::getFrequencyError() {
     722             :   // check active modem
     723           0 :   uint8_t modem = getPacketType();
     724           0 :   if(modem != RADIOLIB_SX126X_PACKET_TYPE_LORA) {
     725           0 :     return(0.0);
     726             :   }
     727             : 
     728             :   // read the raw frequency error register values
     729           0 :   uint8_t efeRaw[3] = {0};
     730           0 :   int16_t state = readRegister(RADIOLIB_SX126X_REG_FREQ_ERROR_RX_CRC, &efeRaw[0], 1);
     731           0 :   RADIOLIB_ASSERT(state);
     732           0 :   state = readRegister(RADIOLIB_SX126X_REG_FREQ_ERROR_RX_CRC + 1, &efeRaw[1], 1);
     733           0 :   RADIOLIB_ASSERT(state);
     734           0 :   state = readRegister(RADIOLIB_SX126X_REG_FREQ_ERROR_RX_CRC + 2, &efeRaw[2], 1);
     735           0 :   RADIOLIB_ASSERT(state);
     736           0 :   uint32_t efe = ((uint32_t) efeRaw[0] << 16) | ((uint32_t) efeRaw[1] << 8) | efeRaw[2];
     737           0 :   efe &= 0x0FFFFF;
     738             : 
     739           0 :   float error = 0;
     740             : 
     741             :   // check the first bit
     742           0 :   if (efe & 0x80000) {
     743             :     // frequency error is negative
     744           0 :     efe |= (uint32_t) 0xFFF00000;
     745           0 :     efe = ~efe + 1;
     746           0 :     error = 1.55f * (float) efe / (1600.0f / (float) this->bandwidthKhz) * -1.0f;
     747             :   } else {
     748           0 :     error = 1.55f * (float) efe / (1600.0f / (float) this->bandwidthKhz);
     749             :   }
     750             : 
     751           0 :   return(error);
     752             : }
     753             : 
     754           3 : size_t SX126x::getPacketLength(bool update) {
     755           3 :   return(this->getPacketLength(update, NULL));
     756             : }
     757             : 
     758           3 : size_t SX126x::getPacketLength(bool update, uint8_t* offset) {
     759             :   (void)update;
     760             : 
     761             :   // in implicit mode, return the cached value if the offset was not requested
     762           3 :   if((getPacketType() == RADIOLIB_SX126X_PACKET_TYPE_LORA) && (this->headerType == RADIOLIB_SX126X_LORA_HEADER_IMPLICIT) && (!offset)) {
     763           0 :     return(this->implicitLen);
     764             :   }
     765             : 
     766             :   // if offset was requested, or in explicit mode, we always have to perform the SPI transaction
     767           3 :   uint8_t rxBufStatus[2] = {0, 0};
     768           3 :   this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_RX_BUFFER_STATUS, rxBufStatus, 2);
     769             : 
     770           3 :   if(offset) { *offset = rxBufStatus[1]; }
     771             : 
     772           3 :   return((size_t)rxBufStatus[0]);
     773             : }
     774             : 
     775           0 : int16_t SX126x::getLoRaRxHeaderInfo(uint8_t* cr, bool* hasCRC) {
     776           0 :   int16_t state = RADIOLIB_ERR_NONE;
     777             : 
     778             :   // check if in explicit header mode
     779           0 :   if(this->headerType == RADIOLIB_SX126X_LORA_HEADER_IMPLICIT) {
     780           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     781             :   }
     782             : 
     783           0 :   if(cr) { *cr = this->mod->SPIgetRegValue(RADIOLIB_SX126X_REG_LORA_RX_CODING_RATE, 6, 4) >> 4; }
     784           0 :   if(hasCRC) { *hasCRC = (this->mod->SPIgetRegValue(RADIOLIB_SX126X_REG_FREQ_ERROR_RX_CRC, 4, 4) != 0); }
     785             : 
     786           0 :   return(state);
     787             : }
     788             : 
     789          18 : RadioLibTime_t SX126x::calculateTimeOnAir(ModemType_t modem, DataRate_t dr, PacketConfig_t pc, size_t len) {
     790             :   // everything is in microseconds to allow integer arithmetic
     791             :   // some constants have .25, these are multiplied by 4, and have _x4 postfix to indicate that fact
     792          18 :   switch (modem) {
     793           8 :     case RADIOLIB_MODEM_LORA: {
     794           8 :       uint32_t symbolLength_us = ((uint32_t)(1000 * 10) << dr.lora.spreadingFactor) / (dr.lora.bandwidth * 10) ;
     795           8 :       uint8_t sfCoeff1_x4 = 17; // (4.25 * 4)
     796           8 :       uint8_t sfCoeff2 = 8;
     797           8 :       if(dr.lora.spreadingFactor == 5 || dr.lora.spreadingFactor == 6) {
     798           0 :         sfCoeff1_x4 = 25; // 6.25 * 4
     799           0 :         sfCoeff2 = 0;
     800             :       }
     801           8 :       uint8_t sfDivisor = 4*dr.lora.spreadingFactor;
     802           8 :       if(pc.lora.ldrOptimize) {
     803           4 :         sfDivisor = 4*(dr.lora.spreadingFactor - 2);
     804             :       }
     805           8 :       const int8_t bitsPerCrc = 16;
     806           8 :       const int8_t N_symbol_header = pc.lora.implicitHeader ? 0 : 20;
     807             : 
     808             :       // numerator of equation in section 6.1.4 of SX1268 datasheet v1.1 (might not actually be bitcount, but it has len * 8)
     809           8 :       int16_t bitCount = (int16_t) 8 * len + pc.lora.crcEnabled * bitsPerCrc - 4 * dr.lora.spreadingFactor  + sfCoeff2 + N_symbol_header;
     810           8 :       if(bitCount < 0) {
     811           0 :         bitCount = 0;
     812             :       }
     813             :       // add (sfDivisor) - 1 to the numerator to give integer CEIL(...)
     814           8 :       uint16_t nPreCodedSymbols = (bitCount + (sfDivisor - 1)) / (sfDivisor);
     815             : 
     816             :       // preamble can be 65k, therefore nSymbol_x4 needs to be 32 bit
     817           8 :       uint32_t nSymbol_x4 = (pc.lora.preambleLength + 8) * 4 + sfCoeff1_x4 + nPreCodedSymbols * dr.lora.codingRate * 4;
     818             : 
     819           8 :       return((symbolLength_us * nSymbol_x4) / 4);
     820             :     }
     821           4 :     case RADIOLIB_MODEM_FSK: {
     822           4 :        return((((float)(pc.fsk.crcLength * 8) + pc.fsk.syncWordLength + pc.fsk.preambleLength + (uint32_t)len * 8) / (dr.fsk.bitRate / 1000.0f)));
     823             :     }
     824           3 :     case RADIOLIB_MODEM_LRFHSS: {
     825             :       // calculate the number of bits based on coding rate
     826             :       uint16_t N_bits;
     827           3 :       switch(dr.lrFhss.cr) {
     828           0 :         case RADIOLIB_SX126X_LR_FHSS_CR_5_6:
     829           0 :           N_bits = ((len * 6) + 4) / 5; // this is from the official LR11xx driver, but why the extra +4?
     830           0 :           break;
     831           3 :         case RADIOLIB_SX126X_LR_FHSS_CR_2_3:
     832           3 :           N_bits = (len * 3) / 2;
     833           3 :           break;
     834           0 :         case RADIOLIB_SX126X_LR_FHSS_CR_1_2:
     835           0 :           N_bits = len * 2;
     836           0 :           break;
     837           0 :         case RADIOLIB_SX126X_LR_FHSS_CR_1_3:
     838           0 :           N_bits = len * 3;
     839           0 :           break;
     840           0 :         default:
     841           0 :           return(RADIOLIB_ERR_INVALID_CODING_RATE);
     842             :       }
     843             : 
     844             :       // calculate number of bits when accounting for unaligned last block
     845           3 :       uint16_t N_payBits = (N_bits / RADIOLIB_SX126X_LR_FHSS_FRAG_BITS) * RADIOLIB_SX126X_LR_FHSS_BLOCK_BITS;
     846           3 :       uint16_t N_lastBlockBits = N_bits % RADIOLIB_SX126X_LR_FHSS_FRAG_BITS;
     847           3 :       if(N_lastBlockBits) {
     848           3 :         N_payBits += N_lastBlockBits + 2;
     849             :       }
     850             : 
     851             :       // add header bits
     852           3 :       uint16_t N_totalBits = (RADIOLIB_SX126X_LR_FHSS_HEADER_BITS * pc.lrFhss.hdrCount) + N_payBits;
     853           3 :       return(((uint32_t)N_totalBits * 8 * 1000000UL) / 488.28215f);
     854             :     }
     855           3 :     default:
     856           3 :       return(RADIOLIB_ERR_WRONG_MODEM);
     857             :   }
     858             : 
     859             :   return(RADIOLIB_ERR_UNKNOWN);
     860             : }
     861             : 
     862           3 : RadioLibTime_t SX126x::getTimeOnAir(size_t len) {
     863           3 :   uint8_t type = getPacketType();
     864           3 :   ModemType_t modem = RADIOLIB_MODEM_LORA;
     865           3 :   DataRate_t dataRate = {};
     866           3 :   PacketConfig_t packetConfig = {};
     867             : 
     868           3 :   if(type == RADIOLIB_SX126X_PACKET_TYPE_LORA) {
     869           0 :     uint8_t cr = this->codingRate;
     870             :     // We assume same calculation for short and long interleaving, so map CR values 0-4 and 5-7 to the same values
     871           0 :     if (cr < 5) {
     872           0 :       cr = cr + 4;
     873           0 :     } else if (cr == 7) {
     874           0 :       cr = cr + 1;
     875             :     }
     876             : 
     877           0 :     dataRate.lora.codingRate = cr;
     878           0 :     dataRate.lora.spreadingFactor = this->spreadingFactor;
     879           0 :     dataRate.lora.bandwidth = this->bandwidthKhz;
     880             : 
     881           0 :     packetConfig.lora.preambleLength = this->preambleLengthLoRa;
     882           0 :     packetConfig.lora.crcEnabled = (bool)this->crcTypeLoRa;
     883           0 :     packetConfig.lora.implicitHeader = this->headerType == RADIOLIB_SX126X_LORA_HEADER_IMPLICIT;
     884           0 :     packetConfig.lora.ldrOptimize = (bool)this->ldrOptimize;
     885           3 :   } else if(type == RADIOLIB_SX126X_PACKET_TYPE_GFSK) {
     886           0 :     modem = RADIOLIB_MODEM_FSK;
     887             : 
     888           0 :     dataRate.fsk.bitRate = RADIOLIB_SX126X_CRYSTAL_FREQ * 32.0f * 1000.0f / (float)this->bitRate;
     889           0 :     dataRate.fsk.freqDev = (float)this->frequencyDev;
     890             : 
     891           0 :     uint8_t crcLen = 0;
     892           0 :     if(this->crcTypeFSK == RADIOLIB_SX126X_GFSK_CRC_1_BYTE || this->crcTypeFSK == RADIOLIB_SX126X_GFSK_CRC_1_BYTE_INV) {
     893           0 :       crcLen = 1;
     894           0 :     } else if(this->crcTypeFSK == RADIOLIB_SX126X_GFSK_CRC_2_BYTE || this->crcTypeFSK == RADIOLIB_SX126X_GFSK_CRC_2_BYTE_INV) {
     895           0 :       crcLen = 2;
     896             :     }
     897             : 
     898           0 :     packetConfig.fsk.preambleLength = this->preambleLengthFSK;
     899           0 :     packetConfig.fsk.syncWordLength = this->syncWordLength;
     900           0 :     packetConfig.fsk.crcLength = crcLen;
     901           3 :   } else if(type == RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS) {
     902           0 :     modem = RADIOLIB_MODEM_LRFHSS;
     903             : 
     904           0 :     dataRate.lrFhss.bw = this->lrFhssBw;
     905           0 :     dataRate.lrFhss.cr = this->lrFhssCr;
     906           0 :     dataRate.lrFhss.narrowGrid = this->lrFhssGridNonFcc;
     907             :     
     908           0 :     packetConfig.lrFhss.hdrCount = this->lrFhssHdrCount;
     909           3 :   } else if(type == RADIOLIB_SX126X_PACKET_TYPE_BPSK) {
     910             :     // BPSK is so experimental it does not have a specific data rate structure
     911             :     // so just reuse FSK
     912           0 :     modem = RADIOLIB_MODEM_FSK;
     913             : 
     914           0 :     dataRate.fsk.bitRate = RADIOLIB_SX126X_CRYSTAL_FREQ * 32.0f * 1000.0f / (float)this->bitRate;
     915           0 :     dataRate.fsk.freqDev = 0;
     916             : 
     917           0 :     packetConfig.fsk.preambleLength = 0;
     918           0 :     packetConfig.fsk.syncWordLength = 0;
     919           0 :     packetConfig.fsk.crcLength = 0;
     920             : 
     921             :   } else {
     922           3 :     return(RADIOLIB_ERR_WRONG_MODEM);
     923             :   
     924             :   }
     925             : 
     926           0 :   return(calculateTimeOnAir(modem, dataRate, packetConfig, len));
     927             : }
     928             : 
     929           3 : RadioLibTime_t SX126x::calculateRxTimeout(RadioLibTime_t timeoutUs) {
     930             :   // the timeout value is given in units of 15.625 microseconds
     931             :   // the calling function should provide some extra width, as this number of units is truncated to integer
     932           3 :   RadioLibTime_t timeout = timeoutUs / 15.625f;
     933           3 :   return(timeout);
     934             : }
     935             : 
     936           6 : uint32_t SX126x::getIrqFlags() {
     937           6 :   uint8_t data[] = { 0x00, 0x00 };
     938           6 :   this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_IRQ_STATUS, data, 2);
     939           6 :   return(((uint32_t)(data[0]) << 8) | data[1]);
     940             : }
     941             : 
     942           3 : int16_t SX126x::setIrqFlags(uint32_t irq) {
     943           3 :   return(this->setDioIrqParams(irq, irq));
     944             : }
     945             : 
     946           3 : int16_t SX126x::clearIrqFlags(uint32_t irq) {
     947           3 :   return(this->clearIrqStatus(irq));
     948             : }
     949             : 
     950           3 : uint8_t SX126x::randomByte() {
     951             :   // set some magic registers
     952           3 :   this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_ANA_LNA, RADIOLIB_SX126X_LNA_RNG_ENABLED, 0, 0);
     953           3 :   this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_ANA_MIXER, RADIOLIB_SX126X_MIXER_RNG_ENABLED, 0, 0);
     954             : 
     955             :   // set mode to Rx
     956           3 :   setRx(RADIOLIB_SX126X_RX_TIMEOUT_INF);
     957             : 
     958             :   // wait a bit for the RSSI reading to stabilise
     959           3 :   this->mod->hal->delay(10);
     960             : 
     961             :   // read RSSI value 8 times, always keep just the least significant bit
     962           3 :   uint8_t randByte = 0x00;
     963          27 :   for(uint8_t i = 0; i < 8; i++) {
     964          24 :     uint8_t val = 0x00;
     965          24 :     readRegister(RADIOLIB_SX126X_REG_RANDOM_NUMBER_0, &val, sizeof(uint8_t));
     966          24 :     randByte |= ((val & 0x01) << i);
     967             :   }
     968             : 
     969             :   // set mode to standby
     970           3 :   standby();
     971             : 
     972             :   // restore the magic registers
     973           3 :   this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_ANA_LNA, RADIOLIB_SX126X_LNA_RNG_DISABLED, 0, 0);
     974           3 :   this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_ANA_MIXER, RADIOLIB_SX126X_MIXER_RNG_DISABLED, 0, 0);
     975             : 
     976           3 :   return(randByte);
     977             : }
     978             : 
     979           9 : int16_t SX126x::getModem(ModemType_t* modem) {
     980           9 :   RADIOLIB_ASSERT_PTR(modem);
     981             : 
     982           9 :   uint8_t type = getPacketType();
     983           9 :   switch(type) {
     984           0 :     case(RADIOLIB_SX126X_PACKET_TYPE_LORA):
     985           0 :       *modem = ModemType_t::RADIOLIB_MODEM_LORA;
     986           0 :       return(RADIOLIB_ERR_NONE);
     987           0 :     case(RADIOLIB_SX126X_PACKET_TYPE_GFSK):
     988           0 :       *modem = ModemType_t::RADIOLIB_MODEM_FSK;
     989           0 :       return(RADIOLIB_ERR_NONE);
     990           0 :     case(RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS):
     991           0 :       *modem = ModemType_t::RADIOLIB_MODEM_LRFHSS;
     992           0 :       return(RADIOLIB_ERR_NONE);
     993             :   }
     994             :   
     995           9 :   return(RADIOLIB_ERR_WRONG_MODEM);
     996             : }
     997             : 
     998          12 : int16_t SX126x::stageMode(RadioModeType_t mode, RadioModeConfig_t* cfg) {
     999             :   int16_t state;
    1000             : 
    1001          12 :   switch(mode) {
    1002           9 :     case(RADIOLIB_RADIO_MODE_RX): {
    1003             :       // in implicit header mode, use the provided length if it is nonzero
    1004             :       // otherwise we trust the user has previously set the payload length manually
    1005           9 :       if((this->headerType == RADIOLIB_SX126X_LORA_HEADER_IMPLICIT) && (cfg->receive.len != 0)) {
    1006           0 :         this->implicitLen = cfg->receive.len;
    1007             :       }
    1008             : 
    1009           9 :       state = startReceiveCommon(cfg->receive.timeout, cfg->receive.irqFlags, cfg->receive.irqMask);
    1010           9 :       RADIOLIB_ASSERT(state);
    1011             : 
    1012             :       // if max(uint32_t) is used, revert to RxContinuous
    1013           0 :       if(cfg->receive.timeout == 0xFFFFFFFF) {
    1014           0 :         cfg->receive.timeout = 0xFFFFFF;
    1015             :       }
    1016           0 :       this->rxTimeout = cfg->receive.timeout;
    1017           0 :     } break;
    1018             :   
    1019           3 :     case(RADIOLIB_RADIO_MODE_TX): {
    1020             :       // check packet length
    1021           3 :       if(cfg->transmit.len > RADIOLIB_SX126X_MAX_PACKET_LENGTH) {
    1022           0 :         return(RADIOLIB_ERR_PACKET_TOO_LONG);
    1023             :       }
    1024             : 
    1025             :       // maximum packet length is decreased by 1 when address filtering is active
    1026             :       if((RADIOLIB_SX126X_GFSK_ADDRESS_FILT_OFF != RADIOLIB_SX126X_GFSK_ADDRESS_FILT_OFF) && 
    1027             :         (cfg->transmit.len > RADIOLIB_SX126X_MAX_PACKET_LENGTH - 1)) {
    1028             :         return(RADIOLIB_ERR_PACKET_TOO_LONG);
    1029             :       }
    1030             : 
    1031             :       // set packet Length
    1032           3 :       state = RADIOLIB_ERR_NONE;
    1033           3 :       uint8_t modem = getPacketType();
    1034           3 :       if(modem == RADIOLIB_SX126X_PACKET_TYPE_LORA) {
    1035           0 :         state = setPacketParams(this->preambleLengthLoRa, this->crcTypeLoRa, cfg->transmit.len, this->headerType, this->invertIQEnabled);
    1036             :       
    1037           3 :       } else if(modem == RADIOLIB_SX126X_PACKET_TYPE_GFSK) {
    1038           0 :         state = setPacketParamsFSK(this->preambleLengthFSK, this->preambleDetLength, this->crcTypeFSK, this->syncWordLength, RADIOLIB_SX126X_GFSK_ADDRESS_FILT_OFF, this->whitening, this->packetType, cfg->transmit.len);
    1039             :       
    1040           3 :       } else if(modem == RADIOLIB_SX126X_PACKET_TYPE_BPSK) {
    1041           0 :         uint16_t rampUp = RADIOLIB_SX126X_BPSK_RAMP_UP_TIME_600_BPS;
    1042           0 :         uint16_t rampDown = RADIOLIB_SX126X_BPSK_RAMP_DOWN_TIME_600_BPS;
    1043           0 :         if(this->bitRate == 100) {
    1044           0 :           rampUp = RADIOLIB_SX126X_BPSK_RAMP_UP_TIME_100_BPS;
    1045           0 :           rampDown = RADIOLIB_SX126X_BPSK_RAMP_DOWN_TIME_100_BPS;
    1046             :         }
    1047           0 :         state = setPacketParamsBPSK(cfg->transmit.len, rampUp, rampDown, 8*cfg->transmit.len);
    1048             :       
    1049           3 :       } else if(modem != RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS) {
    1050           3 :         return(RADIOLIB_ERR_UNKNOWN);
    1051             :       
    1052             :       }
    1053           0 :       RADIOLIB_ASSERT(state);
    1054             : 
    1055             :       // set DIO mapping
    1056           0 :       if(modem != RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS) {
    1057           0 :         state = setDioIrqParams(RADIOLIB_SX126X_IRQ_TX_DONE | RADIOLIB_SX126X_IRQ_TIMEOUT, RADIOLIB_SX126X_IRQ_TX_DONE);
    1058             :       } else {
    1059           0 :         state = setDioIrqParams(RADIOLIB_SX126X_IRQ_TX_DONE | RADIOLIB_SX126X_IRQ_LR_FHSS_HOP, RADIOLIB_SX126X_IRQ_TX_DONE | RADIOLIB_SX126X_IRQ_LR_FHSS_HOP);
    1060             :       }
    1061           0 :       RADIOLIB_ASSERT(state);
    1062             : 
    1063             :       // set buffer pointers
    1064           0 :       state = setBufferBaseAddress();
    1065           0 :       RADIOLIB_ASSERT(state);
    1066             : 
    1067             :       // write packet to buffer
    1068           0 :       if(modem != RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS) {
    1069           0 :         state = writeBuffer(cfg->transmit.data, cfg->transmit.len);
    1070             :       
    1071             :       } else {
    1072             :         // first, reset the LR-FHSS state machine
    1073           0 :         state = resetLRFHSS();
    1074           0 :         RADIOLIB_ASSERT(state);
    1075             : 
    1076             :         // skip hopping for the first 4 - lrFhssHdrCount blocks
    1077           0 :         for(int i = 0; i < 4 - this->lrFhssHdrCount; ++i ) {
    1078           0 :           stepLRFHSS();
    1079             :         }
    1080             : 
    1081             :         // in LR-FHSS mode, we need to build the entire packet manually
    1082           0 :         uint8_t frame[RADIOLIB_SX126X_MAX_PACKET_LENGTH] = { 0 };
    1083           0 :         size_t frameLen = 0;
    1084           0 :         this->lrFhssFrameBitsRem = 0;
    1085           0 :         this->lrFhssFrameHopsRem = 0;
    1086           0 :         this->lrFhssHopNum = 0;
    1087           0 :         state = buildLRFHSSPacket(cfg->transmit.data, cfg->transmit.len, frame, &frameLen, &this->lrFhssFrameBitsRem, &this->lrFhssFrameHopsRem);
    1088           0 :         RADIOLIB_ASSERT(state);
    1089             : 
    1090             :         // FIXME check max len for FHSS
    1091           0 :         state = writeBuffer(frame, frameLen);
    1092           0 :         RADIOLIB_ASSERT(state);
    1093             : 
    1094             :         // activate hopping
    1095           0 :         const uint8_t hopCfg[] = { RADIOLIB_SX126X_HOPPING_ENABLED, (uint8_t)frameLen, (uint8_t)this->lrFhssFrameHopsRem };
    1096           0 :         state = writeRegister(RADIOLIB_SX126X_REG_HOPPING_ENABLE, hopCfg, 3);
    1097           0 :         RADIOLIB_ASSERT(state);
    1098             : 
    1099             :         // write the initial hopping table
    1100           0 :         uint8_t initHops = this->lrFhssFrameHopsRem;
    1101           0 :         if(initHops > 16) {
    1102           0 :           initHops = 16;
    1103             :         };
    1104           0 :         for(size_t i = 0; i < initHops; i++) {
    1105             :           // set the hop frequency and symbols
    1106           0 :           state = this->setLRFHSSHop(i);
    1107           0 :           RADIOLIB_ASSERT(state);
    1108             :         }
    1109             :       
    1110             :       }
    1111           0 :       RADIOLIB_ASSERT(state);
    1112             : 
    1113             :       // clear interrupt flags
    1114           0 :       state = clearIrqStatus();
    1115           0 :       RADIOLIB_ASSERT(state);
    1116             : 
    1117             :       // fix sensitivity
    1118           0 :       state = fixSensitivity();
    1119           0 :       RADIOLIB_ASSERT(state);
    1120           0 :     } break;
    1121             :     
    1122           0 :     default:
    1123           0 :       return(RADIOLIB_ERR_UNSUPPORTED);
    1124             :   }
    1125             : 
    1126           0 :   this->stagedMode = mode;
    1127           0 :   return(state);
    1128             : }
    1129             : 
    1130           3 : int16_t SX126x::launchMode() {
    1131             :   int16_t state;
    1132           3 :   switch(this->stagedMode) {
    1133           3 :     case(RADIOLIB_RADIO_MODE_RX): {
    1134           3 :       this->mod->setRfSwitchState(Module::MODE_RX);
    1135           3 :       state = setRx(this->rxTimeout);
    1136           3 :     } break;
    1137             :   
    1138           0 :     case(RADIOLIB_RADIO_MODE_TX): {
    1139           0 :       this->mod->setRfSwitchState(this->txMode);
    1140           0 :       state = setTx(RADIOLIB_SX126X_TX_TIMEOUT_NONE);
    1141           0 :       RADIOLIB_ASSERT(state);
    1142             : 
    1143             :       // wait for BUSY to go low (= PA ramp up done)
    1144           0 :       while(this->mod->hal->digitalRead(this->mod->getGpio())) {
    1145           0 :         this->mod->hal->yield();
    1146             :       }
    1147           0 :     } break;
    1148             :     
    1149           0 :     default:
    1150           0 :       return(RADIOLIB_ERR_UNSUPPORTED);
    1151             :   }
    1152             : 
    1153           3 :   this->stagedMode = RADIOLIB_RADIO_MODE_NONE;
    1154           3 :   return(state);
    1155             : }
    1156             : 
    1157             : #if !RADIOLIB_EXCLUDE_DIRECT_RECEIVE
    1158           0 : void SX126x::setDirectAction(void (*func)(void)) {
    1159           0 :   setDio1Action(func);
    1160           0 : }
    1161             : 
    1162           0 : void SX126x::readBit(uint32_t pin) {
    1163           0 :   updateDirectBuffer((uint8_t)this->mod->hal->digitalRead(pin));
    1164           0 : }
    1165             : #endif
    1166             : 
    1167           0 : int16_t SX126x::uploadPatch(const uint32_t* patch, size_t len, bool nonvolatile) {
    1168             :   // set to standby RC mode
    1169           0 :   int16_t state = standby(RADIOLIB_SX126X_STANDBY_RC);
    1170           0 :   RADIOLIB_ASSERT(state);
    1171             : 
    1172             :   // check the version
    1173             :   #if RADIOLIB_DEBUG_BASIC
    1174             :   char ver_pre[16];
    1175             :   this->mod->SPIreadRegisterBurst(RADIOLIB_SX126X_REG_VERSION_STRING, 16, reinterpret_cast<uint8_t*>(ver_pre));
    1176             :   RADIOLIB_DEBUG_BASIC_PRINTLN("Pre-update version string: %s", ver_pre);
    1177             :   #endif
    1178             : 
    1179             :   // enable patch update
    1180           0 :   this->mod->SPIwriteRegister(RADIOLIB_SX126X_REG_PATCH_UPDATE_ENABLE, RADIOLIB_SX126X_PATCH_UPDATE_ENABLED);
    1181             :   
    1182             :   // upload the patch
    1183             :   uint8_t data[4];
    1184           0 :   for(uint32_t i = 0; i < len / sizeof(uint32_t); i++) {
    1185           0 :     uint32_t bin = 0;
    1186           0 :     if(nonvolatile) {
    1187           0 :       uint32_t* ptr = const_cast<uint32_t*>(patch) + i;
    1188           0 :       bin = RADIOLIB_NONVOLATILE_READ_DWORD(ptr);
    1189             :     } else {
    1190           0 :       bin = patch[i];
    1191             :     }
    1192           0 :     data[0] = (bin >> 24) & 0xFF;
    1193           0 :     data[1] = (bin >> 16) & 0xFF;
    1194           0 :     data[2] = (bin >> 8) & 0xFF;
    1195           0 :     data[3] = bin & 0xFF;
    1196           0 :     this->mod->SPIwriteRegisterBurst(RADIOLIB_SX126X_REG_PATCH_MEMORY_BASE + i*sizeof(uint32_t), data, sizeof(uint32_t));
    1197             :   }
    1198             : 
    1199             :   // disable patch update
    1200           0 :   this->mod->SPIwriteRegister(RADIOLIB_SX126X_REG_PATCH_UPDATE_ENABLE, RADIOLIB_SX126X_PATCH_UPDATE_DISABLED);
    1201             : 
    1202             :   // update
    1203           0 :   this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_PRAM_UPDATE, NULL, 0);
    1204             : 
    1205             :   // check the version again
    1206             :   #if RADIOLIB_DEBUG_BASIC
    1207             :   char ver_post[16];
    1208             :   this->mod->SPIreadRegisterBurst(RADIOLIB_SX126X_REG_VERSION_STRING, 16, reinterpret_cast<uint8_t*>(ver_post));
    1209             :   RADIOLIB_DEBUG_BASIC_PRINTLN("Post-update version string: %s", ver_post);
    1210             :   #endif
    1211             : 
    1212           0 :   return(state);
    1213             : }
    1214             : 
    1215           0 : int16_t SX126x::spectralScanStart(uint16_t numSamples, uint8_t window, uint8_t interval) {
    1216             :   // abort first - not sure if this is strictly needed, but the example code does this
    1217           0 :   spectralScanAbort();
    1218             : 
    1219             :   // set the RSSI window size
    1220           0 :   this->mod->SPIwriteRegister(RADIOLIB_SX126X_REG_RSSI_AVG_WINDOW, window);
    1221             : 
    1222             :   // start Rx with infinite timeout
    1223           0 :   int16_t state = setRx(RADIOLIB_SX126X_RX_TIMEOUT_INF);
    1224           0 :   RADIOLIB_ASSERT(state);
    1225             : 
    1226             :   // now set the actual spectral scan parameters
    1227           0 :   const uint8_t data[3] = { (uint8_t)((numSamples >> 8) & 0xFF), (uint8_t)(numSamples & 0xFF), interval };
    1228           0 :   return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_SPECTR_SCAN_PARAMS, data, 3));
    1229             : }
    1230             : 
    1231           0 : void SX126x::spectralScanAbort() {
    1232           0 :   this->mod->SPIwriteRegister(RADIOLIB_SX126X_REG_RSSI_AVG_WINDOW, 0x00);
    1233           0 : }
    1234             : 
    1235           0 : int16_t SX126x::spectralScanGetStatus() {
    1236           0 :   uint8_t status = this->mod->SPIreadRegister(RADIOLIB_SX126X_REG_SPECTRAL_SCAN_STATUS);
    1237           0 :   if(status == RADIOLIB_SX126X_SPECTRAL_SCAN_COMPLETED) {
    1238           0 :     return(RADIOLIB_ERR_NONE);
    1239             :   }
    1240           0 :   return(RADIOLIB_ERR_RANGING_TIMEOUT);
    1241             : }
    1242             : 
    1243           0 : int16_t SX126x::spectralScanGetResult(uint16_t* results) {
    1244             :   // read the raw results
    1245             :   uint8_t data[2*RADIOLIB_SX126X_SPECTRAL_SCAN_RES_SIZE];
    1246           0 :   this->mod->SPIreadRegisterBurst(RADIOLIB_SX126X_REG_SPECTRAL_SCAN_RESULT, 2*RADIOLIB_SX126X_SPECTRAL_SCAN_RES_SIZE, data);
    1247             : 
    1248             :   // convert it
    1249           0 :   for(uint8_t i = 0; i < RADIOLIB_SX126X_SPECTRAL_SCAN_RES_SIZE; i++) {
    1250           0 :     results[i] = ((uint16_t)data[i*2] << 8) | ((uint16_t)data[i*2 + 1]);
    1251             :   }
    1252           0 :   return(RADIOLIB_ERR_NONE);
    1253             : }
    1254             : 
    1255           0 : int16_t SX126x::calibrateImage(float freq) {
    1256           0 :   uint8_t data[2] = { 0, 0 };
    1257             : 
    1258             :   // try to match the frequency ranges
    1259           0 :   int freqBand = (int)freq;
    1260           0 :   if((freqBand >= 902) && (freqBand <= 928)) {
    1261           0 :     data[0] = RADIOLIB_SX126X_CAL_IMG_902_MHZ_1;
    1262           0 :     data[1] = RADIOLIB_SX126X_CAL_IMG_902_MHZ_2;
    1263           0 :   } else if((freqBand >= 863) && (freqBand <= 870)) {
    1264           0 :     data[0] = RADIOLIB_SX126X_CAL_IMG_863_MHZ_1;
    1265           0 :     data[1] = RADIOLIB_SX126X_CAL_IMG_863_MHZ_2;
    1266           0 :   } else if((freqBand >= 779) && (freqBand <= 787)) {
    1267           0 :     data[0] = RADIOLIB_SX126X_CAL_IMG_779_MHZ_1;
    1268           0 :     data[1] = RADIOLIB_SX126X_CAL_IMG_779_MHZ_2;
    1269           0 :   } else if((freqBand >= 470) && (freqBand <= 510)) {
    1270           0 :     data[0] = RADIOLIB_SX126X_CAL_IMG_470_MHZ_1;
    1271           0 :     data[1] = RADIOLIB_SX126X_CAL_IMG_470_MHZ_2;
    1272           0 :   } else if((freqBand >= 430) && (freqBand <= 440)) {
    1273           0 :     data[0] = RADIOLIB_SX126X_CAL_IMG_430_MHZ_1;
    1274           0 :     data[1] = RADIOLIB_SX126X_CAL_IMG_430_MHZ_2;
    1275             :   }
    1276             : 
    1277             :   int16_t state;
    1278           0 :   if(data[0]) {
    1279             :     // matched with predefined ranges, do the calibration
    1280           0 :     state = SX126x::calibrateImage(data);
    1281             :   
    1282             :   } else {
    1283             :     // if nothing matched, try custom calibration - the may or may not work
    1284             :     RADIOLIB_DEBUG_BASIC_PRINTLN("Failed to match predefined frequency range, trying custom");
    1285           0 :     state = SX126x::calibrateImageRejection(freq - 4.0f, freq + 4.0f);
    1286             :   
    1287             :   }
    1288             :   
    1289           0 :   return(state);
    1290             : }
    1291             : 
    1292           0 : int16_t SX126x::calibrateImageRejection(float freqMin, float freqMax) {
    1293             :   // calculate the calibration coefficients and calibrate image
    1294           0 :   uint8_t data[] = { (uint8_t)floor((freqMin - 1.0f) / 4.0f), (uint8_t)ceil((freqMax + 1.0f) / 4.0f) };
    1295           0 :   data[0] = (data[0] % 2) ? data[0] : data[0] - 1;
    1296           0 :   data[1] = (data[1] % 2) ? data[1] : data[1] + 1;
    1297           0 :   return(this->calibrateImage(data));
    1298             : }
    1299             : 
    1300           0 : int16_t SX126x::fixSensitivity() {
    1301             :   // fix receiver sensitivity for 500 kHz LoRa
    1302             :   // see SX1262/SX1268 datasheet, chapter 15 Known Limitations, section 15.1 for details
    1303             : 
    1304             :   // read current sensitivity configuration
    1305           0 :   uint8_t sensitivityConfig = 0;
    1306           0 :   int16_t state = readRegister(RADIOLIB_SX126X_REG_SENSITIVITY_CONFIG, &sensitivityConfig, 1);
    1307           0 :   RADIOLIB_ASSERT(state);
    1308             : 
    1309             :   // fix the value for LoRa with 500 kHz bandwidth
    1310           0 :   if((getPacketType() == RADIOLIB_SX126X_PACKET_TYPE_LORA) && (fabsf(this->bandwidthKhz - 500.0f) <= 0.001f)) {
    1311           0 :     sensitivityConfig &= 0xFB;
    1312             :   } else {
    1313           0 :     sensitivityConfig |= 0x04;
    1314             :   }
    1315           0 :   return(writeRegister(RADIOLIB_SX126X_REG_SENSITIVITY_CONFIG, &sensitivityConfig, 1));
    1316             : }
    1317             : 
    1318           0 : int16_t SX126x::fixPaClamping(bool enable) {
    1319             :   // fixes overly eager PA clamping
    1320             :   // see SX1262/SX1268 datasheet, chapter 15 Known Limitations, section 15.2 for details
    1321             : 
    1322             :   // read current clamping configuration
    1323           0 :   uint8_t clampConfig = 0;
    1324           0 :   int16_t state = readRegister(RADIOLIB_SX126X_REG_TX_CLAMP_CONFIG, &clampConfig, 1);
    1325           0 :   RADIOLIB_ASSERT(state);
    1326             : 
    1327             :   // apply or undo workaround
    1328           0 :   if (enable)
    1329           0 :     clampConfig |= 0x1E;
    1330             :   else
    1331           0 :     clampConfig = (clampConfig & ~0x1E) | 0x08;
    1332             : 
    1333           0 :   return(writeRegister(RADIOLIB_SX126X_REG_TX_CLAMP_CONFIG, &clampConfig, 1));
    1334             : }
    1335             : 
    1336           0 : int16_t SX126x::fixImplicitTimeout() {
    1337             :   // fixes timeout in implicit header mode
    1338             :   // see SX1262/SX1268 datasheet, chapter 15 Known Limitations, section 15.3 for details
    1339             : 
    1340             :   //check if we're in implicit LoRa mode
    1341           0 :   if(!((this->headerType == RADIOLIB_SX126X_LORA_HEADER_IMPLICIT) && (getPacketType() == RADIOLIB_SX126X_PACKET_TYPE_LORA))) {
    1342             :     // not in the correct mode, nothing to do here
    1343           0 :     return(RADIOLIB_ERR_NONE);
    1344             :   }
    1345             : 
    1346             :   // stop RTC counter
    1347           0 :   uint8_t rtcStop = 0x00;
    1348           0 :   int16_t state = writeRegister(RADIOLIB_SX126X_REG_RTC_CTRL, &rtcStop, 1);
    1349           0 :   RADIOLIB_ASSERT(state);
    1350             : 
    1351             :   // read currently active event
    1352           0 :   uint8_t rtcEvent = 0;
    1353           0 :   state = readRegister(RADIOLIB_SX126X_REG_EVENT_MASK, &rtcEvent, 1);
    1354           0 :   RADIOLIB_ASSERT(state);
    1355             : 
    1356             :   // clear events
    1357           0 :   rtcEvent |= 0x02;
    1358           0 :   return(writeRegister(RADIOLIB_SX126X_REG_EVENT_MASK, &rtcEvent, 1));
    1359             : }
    1360             : 
    1361           0 : int16_t SX126x::fixInvertedIQ(uint8_t iqConfig) {
    1362             :   // fixes IQ configuration for inverted IQ
    1363             :   // see SX1262/SX1268 datasheet, chapter 15 Known Limitations, section 15.4 for details
    1364             : 
    1365             :   // read current IQ configuration
    1366           0 :   uint8_t iqConfigCurrent = 0;
    1367           0 :   int16_t state = readRegister(RADIOLIB_SX126X_REG_IQ_CONFIG, &iqConfigCurrent, 1);
    1368           0 :   RADIOLIB_ASSERT(state);
    1369             : 
    1370             :   // set correct IQ configuration
    1371           0 :   if(iqConfig == RADIOLIB_SX126X_LORA_IQ_INVERTED) {
    1372           0 :     iqConfigCurrent &= 0xFB;
    1373             :   } else {
    1374           0 :     iqConfigCurrent |= 0x04;
    1375             :   }
    1376             : 
    1377             :   // update with the new value
    1378           0 :   return(writeRegister(RADIOLIB_SX126X_REG_IQ_CONFIG, &iqConfigCurrent, 1));
    1379             : }
    1380             : 
    1381           0 : int16_t SX126x::fixGFSK() {
    1382             :   // method that applies some magic workaround for specific bitrate, frequency deviation,
    1383             :   // receiver bandwidth and carrier frequencies for GFSK (and resets it in all other cases)
    1384             :   // this is not documented in the datasheet, only in Semtech repositories for SX126x and LR11xx
    1385             : 
    1386             :   // first, check we are using GFSK modem
    1387           0 :   if(getPacketType() != RADIOLIB_SX126X_PACKET_TYPE_GFSK) {
    1388             :     // not in GFSK, nothing to do here
    1389           0 :     return(RADIOLIB_ERR_NONE);
    1390             :   }
    1391             : 
    1392             :   // next, decide what to change based on modulation properties
    1393           0 :   int16_t state = RADIOLIB_ERR_UNKNOWN;
    1394           0 :   if(this->bitRate == 1200) {
    1395             :     // workaround for 1.2 kbps
    1396           0 :     state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_3, 0x00, 4, 4);
    1397             : 
    1398           0 :   } else if(this->bitRate == 600)  {
    1399             :     // workaround for 0.6 kbps
    1400           0 :     state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_1, 0x18, 4, 3);
    1401           0 :     RADIOLIB_ASSERT(state);
    1402           0 :     state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_RSSI_AVG_WINDOW, 0x04, 4, 2);
    1403           0 :     RADIOLIB_ASSERT(state);
    1404           0 :     state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_3, 0x00, 4, 4);
    1405           0 :     RADIOLIB_ASSERT(state);
    1406           0 :     state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_4, 0x50, 6, 4);
    1407             :   
    1408             :   } else {
    1409             :     // reset
    1410           0 :     state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_1, 0x08, 4, 3);
    1411           0 :     RADIOLIB_ASSERT(state);
    1412           0 :     state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_RSSI_AVG_WINDOW, 0x00, 4, 2);
    1413           0 :     RADIOLIB_ASSERT(state);
    1414           0 :     state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_3, 0x10, 4, 4);
    1415           0 :     RADIOLIB_ASSERT(state);
    1416           0 :     state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_4, 0x00, 6, 4);
    1417             :   
    1418             :   }
    1419             : 
    1420           0 :   return(state);
    1421             : }
    1422             : 
    1423           0 : Module* SX126x::getMod() {
    1424           0 :   return(this->mod);
    1425             : }
    1426             : 
    1427           0 : int16_t SX126x::modSetup(float tcxoVoltage, bool useRegulatorLDO, uint8_t modem) {
    1428             :   // set module properties
    1429           0 :   this->mod->init();
    1430           0 :   this->mod->hal->pinMode(this->mod->getIrq(), this->mod->hal->GpioModeInput);
    1431           0 :   this->mod->hal->pinMode(this->mod->getGpio(), this->mod->hal->GpioModeInput);
    1432           0 :   this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR] = Module::BITS_16;
    1433           0 :   this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD] = Module::BITS_8;
    1434           0 :   this->mod->spiConfig.statusPos = 1;
    1435           0 :   this->mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ] = RADIOLIB_SX126X_CMD_READ_REGISTER;
    1436           0 :   this->mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE] = RADIOLIB_SX126X_CMD_WRITE_REGISTER;
    1437           0 :   this->mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_NOP] = RADIOLIB_SX126X_CMD_NOP;
    1438           0 :   this->mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_STATUS] = RADIOLIB_SX126X_CMD_GET_STATUS;
    1439           0 :   this->mod->spiConfig.stream = true;
    1440           0 :   this->mod->spiConfig.parseStatusCb = SPIparseStatus;
    1441             : 
    1442             :   // find the SX126x chip - this will also reset the module and verify the module
    1443           0 :   if(!SX126x::findChip(this->chipType)) {
    1444             :     RADIOLIB_DEBUG_BASIC_PRINTLN("No SX126x found!");
    1445           0 :     this->mod->term();
    1446           0 :     return(RADIOLIB_ERR_CHIP_NOT_FOUND);
    1447             :   }
    1448             :   RADIOLIB_DEBUG_BASIC_PRINTLN("M\tSX126x");
    1449             : 
    1450           0 :   int16_t state = RADIOLIB_ERR_NONE;
    1451             : 
    1452             :   // set TCXO control, if requested
    1453           0 :   if(!this->XTAL && tcxoVoltage > 0.0f) {
    1454           0 :     state = setTCXO(tcxoVoltage);
    1455           0 :     RADIOLIB_ASSERT(state);
    1456             :   }
    1457             : 
    1458             :   // configure settings not accessible by API
    1459           0 :   state = config(modem);
    1460             : 
    1461             :   // if something failed, check the device errors
    1462           0 :   if(state != RADIOLIB_ERR_NONE) {
    1463             :     // unless mode is forced to standby, device errors will be 0
    1464           0 :     (void)standby();
    1465           0 :     uint16_t errors = getDeviceErrors();
    1466             :     RADIOLIB_DEBUG_BASIC_PRINTLN("Config failed, device errors: 0x%X", errors);
    1467             : 
    1468             :     // SPI command fail and oscillator start error flag indicate incorrectly set oscillator
    1469           0 :     if((state == RADIOLIB_ERR_SPI_CMD_FAILED) && (errors & RADIOLIB_SX126X_XOSC_START_ERR)) {
    1470             :       // typically users with XTAL devices will try to call the default begin method
    1471             :       // disable TCXO and try to run config again
    1472           0 :       this->XTAL = false;
    1473             :       RADIOLIB_DEBUG_BASIC_PRINTLN("Bad oscillator selected, trying XTAL");
    1474             : 
    1475           0 :       state = setTCXO(0);
    1476           0 :       RADIOLIB_ASSERT(state);
    1477             : 
    1478           0 :       state = config(modem);
    1479             :     }
    1480             :   }
    1481           0 :   RADIOLIB_ASSERT(state);
    1482             : 
    1483           0 :   if (useRegulatorLDO) {
    1484           0 :     state = setRegulatorLDO();
    1485             :   } else {
    1486           0 :     state = setRegulatorDCDC();
    1487             :   }
    1488           0 :   return(state);
    1489             : }
    1490             : 
    1491           0 : int16_t SX126x::SPIparseStatus(uint8_t in) {
    1492           0 :   if((in & 0b00001110) == RADIOLIB_SX126X_STATUS_CMD_TIMEOUT) {
    1493           0 :     return(RADIOLIB_ERR_SPI_CMD_TIMEOUT);
    1494           0 :   } else if((in & 0b00001110) == RADIOLIB_SX126X_STATUS_CMD_INVALID) {
    1495           0 :     return(RADIOLIB_ERR_SPI_CMD_INVALID);
    1496           0 :   } else if((in & 0b00001110) == RADIOLIB_SX126X_STATUS_CMD_FAILED) {
    1497           0 :     return(RADIOLIB_ERR_SPI_CMD_FAILED);
    1498           0 :   } else if((in == 0x00) || (in == 0xFF)) {
    1499           0 :     return(RADIOLIB_ERR_CHIP_NOT_FOUND);
    1500             :   }
    1501           0 :   return(RADIOLIB_ERR_NONE);
    1502             : }
    1503             : 
    1504           0 : bool SX126x::findChip(const char* verStr) {
    1505           0 :   uint8_t i = 0;
    1506           0 :   bool flagFound = false;
    1507           0 :   while((i < 10) && !flagFound) {
    1508             :     // reset the module
    1509           0 :     reset(true);
    1510             : 
    1511             :     // read the version string
    1512           0 :     char version[16] = { 0 };
    1513           0 :     this->mod->SPIreadRegisterBurst(RADIOLIB_SX126X_REG_VERSION_STRING, 16, reinterpret_cast<uint8_t*>(version));
    1514             : 
    1515             :     // check version register
    1516           0 :     if(strncmp(verStr, version, 6) == 0) {
    1517             :       RADIOLIB_DEBUG_BASIC_PRINTLN("Found SX126x: RADIOLIB_SX126X_REG_VERSION_STRING:");
    1518             :       RADIOLIB_DEBUG_BASIC_HEXDUMP(reinterpret_cast<uint8_t*>(version), 16, RADIOLIB_SX126X_REG_VERSION_STRING);
    1519             :       RADIOLIB_DEBUG_BASIC_PRINTLN();
    1520           0 :       flagFound = true;
    1521             :     } else {
    1522             :       #if RADIOLIB_DEBUG_BASIC
    1523             :         RADIOLIB_DEBUG_BASIC_PRINTLN("SX126x not found! (%d of 10 tries) RADIOLIB_SX126X_REG_VERSION_STRING:", i + 1);
    1524             :         RADIOLIB_DEBUG_BASIC_HEXDUMP(reinterpret_cast<uint8_t*>(version), 16, RADIOLIB_SX126X_REG_VERSION_STRING);
    1525             :         RADIOLIB_DEBUG_BASIC_PRINTLN("Expected string: %s", verStr);
    1526             :       #endif
    1527           0 :       this->mod->hal->delay(10);
    1528           0 :       i++;
    1529             :     }
    1530             :   }
    1531             : 
    1532           0 :   return(flagFound);
    1533             : }
    1534             : 
    1535             : #endif

Generated by: LCOV version 1.14