LCOV - code coverage report
Current view: top level - src/modules/LR11x0 - LR11x0.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 184 938 19.6 %
Date: 2026-04-06 12:28:13 Functions: 42 79 53.2 %

          Line data    Source code
       1             : #include "LR11x0.h"
       2             : 
       3             : #include <math.h>
       4             : #include <string.h>
       5             : 
       6             : #if !RADIOLIB_EXCLUDE_LR11X0
       7             : 
       8          16 : LR11x0::LR11x0(Module* mod) : LRxxxx(mod) {
       9          16 :   this->freqStep = RADIOLIB_LR11X0_FREQUENCY_STEP_SIZE;
      10          16 :   this->maxPacketLength = RADIOLIB_LR11X0_MAX_PACKET_LENGTH;
      11          16 :   this->irqMap[RADIOLIB_IRQ_TX_DONE] = RADIOLIB_LR11X0_IRQ_TX_DONE;
      12          16 :   this->irqMap[RADIOLIB_IRQ_RX_DONE] = RADIOLIB_LR11X0_IRQ_RX_DONE;
      13          16 :   this->irqMap[RADIOLIB_IRQ_PREAMBLE_DETECTED] = RADIOLIB_LR11X0_IRQ_PREAMBLE_DETECTED;
      14          16 :   this->irqMap[RADIOLIB_IRQ_SYNC_WORD_VALID] = RADIOLIB_LR11X0_IRQ_SYNC_WORD_HEADER_VALID;
      15          16 :   this->irqMap[RADIOLIB_IRQ_HEADER_VALID] = RADIOLIB_LR11X0_IRQ_SYNC_WORD_HEADER_VALID;
      16          16 :   this->irqMap[RADIOLIB_IRQ_HEADER_ERR] = RADIOLIB_LR11X0_IRQ_HEADER_ERR;
      17          16 :   this->irqMap[RADIOLIB_IRQ_CRC_ERR] = RADIOLIB_LR11X0_IRQ_CRC_ERR;
      18          16 :   this->irqMap[RADIOLIB_IRQ_CAD_DONE] = RADIOLIB_LR11X0_IRQ_CAD_DONE;
      19          16 :   this->irqMap[RADIOLIB_IRQ_CAD_DETECTED] = RADIOLIB_LR11X0_IRQ_CAD_DETECTED;
      20          16 :   this->irqMap[RADIOLIB_IRQ_TIMEOUT] = RADIOLIB_LR11X0_IRQ_TIMEOUT;
      21          16 : }
      22             : 
      23           0 : int16_t LR11x0::begin(float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, uint16_t preambleLength, float tcxoVoltage, bool high) {
      24             :   // set module properties and perform initial setup
      25           0 :   int16_t state = this->modSetup(tcxoVoltage, RADIOLIB_LR11X0_PACKET_TYPE_LORA);
      26           0 :   RADIOLIB_ASSERT(state);
      27             : 
      28             :   // configure publicly accessible settings
      29           0 :   state = setBandwidth(bw, high);
      30           0 :   RADIOLIB_ASSERT(state);
      31             : 
      32           0 :   state = setSpreadingFactor(sf);
      33           0 :   RADIOLIB_ASSERT(state);
      34             : 
      35           0 :   state = setCodingRate(cr);
      36           0 :   RADIOLIB_ASSERT(state);
      37             : 
      38           0 :   state = setSyncWord(syncWord);
      39           0 :   RADIOLIB_ASSERT(state);
      40             : 
      41           0 :   state = setPreambleLength(preambleLength);
      42           0 :   RADIOLIB_ASSERT(state);
      43             : 
      44             :   // set publicly accessible settings that are not a part of begin method
      45           0 :   state = setCRC(2);
      46           0 :   RADIOLIB_ASSERT(state);
      47             : 
      48           0 :   state = invertIQ(false);
      49           0 :   RADIOLIB_ASSERT(state);
      50             : 
      51           0 :   state = setRegulatorLDO();
      52           0 :   RADIOLIB_ASSERT(state);
      53             : 
      54           0 :   return(RADIOLIB_ERR_NONE);
      55             : }
      56             : 
      57           0 : int16_t LR11x0::beginGFSK(float br, float freqDev, float rxBw, uint16_t preambleLength, float tcxoVoltage) {
      58             :   // set module properties and perform initial setup
      59           0 :   int16_t state = this->modSetup(tcxoVoltage, RADIOLIB_LR11X0_PACKET_TYPE_GFSK);
      60           0 :   RADIOLIB_ASSERT(state);
      61             : 
      62             :   // configure publicly accessible settings
      63           0 :   state = setBitRate(br);
      64           0 :   RADIOLIB_ASSERT(state);
      65             : 
      66           0 :   state = setFrequencyDeviation(freqDev);
      67           0 :   RADIOLIB_ASSERT(state);
      68             : 
      69           0 :   state = setRxBandwidth(rxBw);
      70           0 :   RADIOLIB_ASSERT(state);
      71             : 
      72           0 :   state = setPreambleLength(preambleLength);
      73           0 :   RADIOLIB_ASSERT(state);
      74             : 
      75             :   // set publicly accessible settings that are not a part of begin method
      76           0 :   uint8_t sync[] = { 0x12, 0xAD };
      77           0 :   state = setSyncWord(sync, 2);
      78           0 :   RADIOLIB_ASSERT(state);
      79             : 
      80           0 :   state = setDataShaping(RADIOLIB_SHAPING_NONE);
      81           0 :   RADIOLIB_ASSERT(state);
      82             : 
      83           0 :   state = setEncoding(RADIOLIB_ENCODING_NRZ);
      84           0 :   RADIOLIB_ASSERT(state);
      85             : 
      86           0 :   state = variablePacketLengthMode(RADIOLIB_LR11X0_MAX_PACKET_LENGTH);
      87           0 :   RADIOLIB_ASSERT(state);
      88             : 
      89           0 :   state = setCRC(2);
      90           0 :   RADIOLIB_ASSERT(state);
      91             : 
      92           0 :   state = setRegulatorLDO();
      93           0 :   RADIOLIB_ASSERT(state);
      94             : 
      95           0 :   return(RADIOLIB_ERR_NONE);
      96             : }
      97             : 
      98           0 : int16_t LR11x0::beginLRFHSS(uint8_t bw, uint8_t cr, bool narrowGrid, float tcxoVoltage) {
      99             :   // set module properties and perform initial setup
     100           0 :   int16_t state = this->modSetup(tcxoVoltage, RADIOLIB_LR11X0_PACKET_TYPE_LR_FHSS);
     101           0 :   RADIOLIB_ASSERT(state);
     102             : 
     103             :   // set grid spacing
     104           0 :   this->lrFhssGrid = narrowGrid ? RADIOLIB_LRXXXX_LR_FHSS_GRID_STEP_NON_FCC : RADIOLIB_LRXXXX_LR_FHSS_GRID_STEP_FCC;
     105             : 
     106             :   // configure publicly accessible settings
     107           0 :   state = setLrFhssConfig(bw, cr);
     108           0 :   RADIOLIB_ASSERT(state);
     109             : 
     110           0 :   uint8_t syncWord[] = { 0x12, 0xAD, 0x10, 0x1B };
     111           0 :   state = setSyncWord(syncWord, 4);
     112           0 :   RADIOLIB_ASSERT(state);
     113             : 
     114           0 :   state = setRegulatorLDO();
     115           0 :   RADIOLIB_ASSERT(state);
     116             : 
     117             :   // set fixed configuration
     118           0 :   return(setModulationParamsLrFhss(RADIOLIB_LRXXXX_LR_FHSS_BIT_RATE_RAW, RADIOLIB_LR11X0_LR_FHSS_SHAPING_GAUSSIAN_BT_1_0));
     119             : }
     120             : 
     121           0 : int16_t LR11x0::beginGNSS(uint8_t constellations, float tcxoVoltage) {
     122             :   // set module properties and perform initial setup - packet type does not matter
     123           0 :   int16_t state = this->modSetup(tcxoVoltage, RADIOLIB_LR11X0_PACKET_TYPE_LORA);
     124           0 :   RADIOLIB_ASSERT(state);
     125             : 
     126           0 :   state = this->clearErrors();
     127           0 :   RADIOLIB_ASSERT(state);
     128             : 
     129             :   // set GNSS flag to reserve DIO11 for LF clock
     130           0 :   this->gnss = true;
     131           0 :   state = this->configLfClock(RADIOLIB_LR11X0_LF_BUSY_RELEASE_DISABLED | RADIOLIB_LR11X0_LF_CLK_XOSC);
     132           0 :   RADIOLIB_ASSERT(state);
     133             : 
     134           0 :   uint16_t errs = 0;
     135           0 :   state = this->getErrors(&errs);
     136           0 :   RADIOLIB_ASSERT(state);
     137           0 :   if(errs & 0x40) {
     138             :     RADIOLIB_DEBUG_BASIC_PRINTLN("LF_XOSC_START_ERR");
     139           0 :     return(RADIOLIB_ERR_SPI_CMD_FAILED);
     140             :   }
     141             : 
     142           0 :   state = this->gnssSetConstellationToUse(constellations);
     143           0 :   RADIOLIB_ASSERT(state);
     144             : 
     145           0 :   state = setRegulatorLDO();
     146           0 :   RADIOLIB_ASSERT(state);
     147             : 
     148           0 :   return(RADIOLIB_ERR_NONE);
     149             : }
     150             : 
     151           3 : int16_t LR11x0::transmit(const uint8_t* data, size_t len, uint8_t addr) {
     152             :    // set mode to standby
     153           3 :   int16_t state = standby();
     154           3 :   RADIOLIB_ASSERT(state);
     155             : 
     156             :   // check packet length
     157           0 :   if (this->codingRate > RADIOLIB_LR11X0_LORA_CR_4_8_SHORT) {
     158             :     // Long Interleaver needs at least 8 bytes
     159           0 :     if(len < 8) {
     160           0 :       return(RADIOLIB_ERR_PACKET_TOO_SHORT);
     161             :     }
     162             : 
     163             :     // Long Interleaver supports up to 253 bytes if CRC is enabled
     164           0 :     if (this->crcTypeLoRa == RADIOLIB_LRXXXX_LORA_CRC_ENABLED && (len > RADIOLIB_LR11X0_MAX_PACKET_LENGTH - 2)) {
     165           0 :       return(RADIOLIB_ERR_PACKET_TOO_LONG);
     166             :     }  
     167             :   } 
     168           0 :   if(len > RADIOLIB_LR11X0_MAX_PACKET_LENGTH) {
     169           0 :     return(RADIOLIB_ERR_PACKET_TOO_LONG);
     170             :   }
     171             : 
     172             :   // get currently active modem
     173           0 :   uint8_t modem = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     174           0 :   state = getPacketType(&modem);
     175           0 :   RADIOLIB_ASSERT(state);
     176           0 :   RadioLibTime_t timeout = getTimeOnAir(len);
     177           0 :   if(modem == RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
     178             :     // calculate timeout (150% of expected time-on-air)
     179           0 :     timeout = (timeout * 3) / 2;
     180             : 
     181           0 :   } else if((modem == RADIOLIB_LR11X0_PACKET_TYPE_GFSK) || (modem == RADIOLIB_LR11X0_PACKET_TYPE_LR_FHSS)) {
     182             :     // calculate timeout (500% of expected time-on-air)
     183           0 :     timeout = timeout * 5;
     184             : 
     185             :   } else {
     186           0 :     return(RADIOLIB_ERR_UNKNOWN);
     187             :   }
     188             : 
     189             :   RADIOLIB_DEBUG_BASIC_PRINTLN("Timeout in %lu us", timeout);
     190             : 
     191             :   // start transmission
     192           0 :   state = startTransmit(data, len, addr);
     193           0 :   RADIOLIB_ASSERT(state);
     194             : 
     195             :   // wait for packet transmission or timeout
     196           0 :   RadioLibTime_t start = this->mod->hal->micros();
     197           0 :   while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
     198           0 :     this->mod->hal->yield();
     199           0 :     if(this->mod->hal->micros() - start > timeout) {
     200           0 :       finishTransmit();
     201           0 :       return(RADIOLIB_ERR_TX_TIMEOUT);
     202             :     }
     203             :   }
     204             : 
     205           0 :   return(finishTransmit());
     206             : }
     207             : 
     208           3 : int16_t LR11x0::receive(uint8_t* data, size_t len, RadioLibTime_t timeout) {
     209             :   // set mode to standby
     210           3 :   int16_t state = standby();
     211           3 :   RADIOLIB_ASSERT(state);
     212             : 
     213             :   // calculate timeout based on the configured modem
     214           0 :   RadioLibTime_t timeoutInternal = timeout;
     215           0 :   if(!timeoutInternal) {
     216             :     // get currently active modem
     217           0 :     uint8_t modem = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     218           0 :     state = getPacketType(&modem);
     219           0 :     RADIOLIB_ASSERT(state);
     220           0 :     if((modem == RADIOLIB_LR11X0_PACKET_TYPE_LORA) || (modem == RADIOLIB_LR11X0_PACKET_TYPE_GFSK)) {
     221             :       // calculate timeout (500 % of expected time-one-air)
     222           0 :       size_t maxLen = len;
     223           0 :       if(len == 0) { maxLen = RADIOLIB_LR11X0_MAX_PACKET_LENGTH; }
     224           0 :       timeoutInternal = (getTimeOnAir(maxLen) * 5) / 1000;
     225             :     
     226           0 :     } else if(modem == RADIOLIB_LR11X0_PACKET_TYPE_LR_FHSS) {
     227             :       // this modem cannot receive
     228           0 :       return(RADIOLIB_ERR_WRONG_MODEM);
     229             : 
     230             :     } else {
     231           0 :       return(RADIOLIB_ERR_UNKNOWN);
     232             :     
     233             :     }
     234             :   }
     235             : 
     236             :   RADIOLIB_DEBUG_BASIC_PRINTLN("Timeout in %lu ms", timeoutInternal);
     237             : 
     238             :   // start reception
     239           0 :   uint32_t timeoutValue = (uint32_t)(((float)timeoutInternal * 1000.0f) / 30.52f);
     240           0 :   state = startReceive(timeoutValue);
     241           0 :   RADIOLIB_ASSERT(state);
     242             : 
     243             :   // wait for packet reception or timeout
     244           0 :   bool softTimeout = false;
     245           0 :   RadioLibTime_t start = this->mod->hal->millis();
     246           0 :   while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
     247           0 :     this->mod->hal->yield();
     248             :     // safety check, the timeout should be done by the radio
     249           0 :     if(this->mod->hal->millis() - start > timeoutInternal) {
     250           0 :       softTimeout = true;
     251           0 :       break;
     252             :     }
     253             :   }
     254             : 
     255             :   // if it was a timeout, this will return an error code
     256             :   // TODO taken from SX126x, does this really work?
     257           0 :   state = standby();
     258           0 :   if((state != RADIOLIB_ERR_NONE) && (state != RADIOLIB_ERR_SPI_CMD_TIMEOUT)) {
     259           0 :     return(state);
     260             :   }
     261             : 
     262             :   // check whether this was a timeout or not
     263           0 :   if(softTimeout || (getIrqFlags() & this->irqMap[RADIOLIB_IRQ_TIMEOUT])) {
     264           0 :     (void)finishReceive();
     265           0 :     return(RADIOLIB_ERR_RX_TIMEOUT);
     266             :   }
     267             : 
     268             :   // read the received data
     269           0 :   return(readData(data, len));
     270             : }
     271             : 
     272           3 : int16_t LR11x0::transmitDirect(uint32_t frf) {
     273             :   // set RF switch (if present)
     274           3 :   this->mod->setRfSwitchState(Module::MODE_TX);
     275             : 
     276             :   // user requested to start transmitting immediately (required for RTTY)
     277           3 :   int16_t state = RADIOLIB_ERR_NONE;
     278           3 :   if(frf != 0) {
     279           0 :     state = setRfFrequency(frf);
     280             :   }
     281           3 :   RADIOLIB_ASSERT(state);
     282             : 
     283             :   // start transmitting
     284           3 :   return(setTxCw());
     285             : }
     286             : 
     287           3 : int16_t LR11x0::receiveDirect() {
     288             :   // set RF switch (if present)
     289           3 :   this->mod->setRfSwitchState(Module::MODE_RX);
     290             : 
     291             :   // LR11x0 is unable to output received data directly
     292           3 :   return(RADIOLIB_ERR_UNKNOWN);
     293             : }
     294             : 
     295           3 : int16_t LR11x0::scanChannel() {
     296           3 :   ChannelScanConfig_t cfg = {
     297             :     .cad = {
     298             :       .symNum = RADIOLIB_LR11X0_CAD_PARAM_DEFAULT,
     299             :       .detPeak = RADIOLIB_LR11X0_CAD_PARAM_DEFAULT,
     300             :       .detMin = RADIOLIB_LR11X0_CAD_PARAM_DEFAULT,
     301             :       .exitMode = RADIOLIB_LR11X0_CAD_PARAM_DEFAULT,
     302             :       .timeout = 0,
     303             :       .irqFlags = RADIOLIB_IRQ_CAD_DEFAULT_FLAGS,
     304             :       .irqMask = RADIOLIB_IRQ_CAD_DEFAULT_MASK,
     305             :     },
     306             :   };
     307           6 :   return(this->scanChannel(cfg));
     308             : }
     309             : 
     310           6 : int16_t LR11x0::scanChannel(const ChannelScanConfig_t &config) {
     311             :   // set mode to CAD
     312           6 :   int state = startChannelScan(config);
     313           6 :   RADIOLIB_ASSERT(state);
     314             : 
     315             :   // wait for channel activity detected or timeout
     316           0 :   while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
     317           0 :     this->mod->hal->yield();
     318             :   }
     319             : 
     320             :   // check CAD result
     321           0 :   return(getChannelScanResult());
     322             : }
     323             : 
     324          15 : int16_t LR11x0::standby() {
     325          15 :   return(LR11x0::standby(RADIOLIB_LR11X0_STANDBY_RC));
     326             : }
     327             : 
     328          18 : int16_t LR11x0::standby(uint8_t mode) {
     329          18 :   return(LR11x0::standby(mode, true));
     330             : }
     331             : 
     332          18 : int16_t LR11x0::standby(uint8_t mode, bool wakeup) {
     333             :   // set RF switch (if present)
     334          18 :   this->mod->setRfSwitchState(Module::MODE_IDLE);
     335             : 
     336          18 :   if(wakeup) {
     337             :     // send a NOP command - this pulls the NSS low to exit the sleep mode,
     338             :     // while preventing interference with possible other SPI transactions
     339          18 :     (void)this->mod->SPIwriteStream((uint16_t)RADIOLIB_LR11X0_CMD_NOP, NULL, 0, false, false);
     340             :   }
     341             : 
     342          18 :   uint8_t buff[] = { mode };
     343          36 :   return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_STANDBY, true, buff, 1));
     344             : }
     345             : 
     346           3 : int16_t LR11x0::sleep() {
     347           3 :   return(LR11x0::sleep(true, 0));
     348             : }
     349             : 
     350           3 : int16_t LR11x0::sleep(bool retainConfig, uint32_t sleepTime) {
     351             :   // set RF switch (if present)
     352           3 :   this->mod->setRfSwitchState(Module::MODE_IDLE);
     353             : 
     354             :   uint8_t buff[] = { 
     355           3 :     (uint8_t)retainConfig,
     356           3 :     (uint8_t)((sleepTime >> 24) & 0xFF), (uint8_t)((sleepTime >> 16) & 0xFF),
     357           3 :     (uint8_t)((sleepTime >> 16) & 0xFF), (uint8_t)(sleepTime & 0xFF),
     358           3 :   };
     359           3 :   if(sleepTime) {
     360           0 :     buff[0] |= RADIOLIB_LR11X0_SLEEP_WAKEUP_ENABLED;
     361             :   }
     362             : 
     363             :   // in sleep, the busy line will remain high, so we have to use this method to disable waiting for it to go low
     364           3 :   int16_t state = this->mod->SPIwriteStream(RADIOLIB_LR11X0_CMD_SET_SLEEP, buff, sizeof(buff), false, false);
     365             : 
     366             :   // wait for the module to safely enter sleep mode
     367           3 :   this->mod->hal->delay(1);
     368             : 
     369           3 :   return(state);
     370             : }
     371             : 
     372           3 : int16_t LR11x0::finishTransmit() {
     373             :   // clear interrupt flags
     374           3 :   clearIrqState(RADIOLIB_LR11X0_IRQ_ALL);
     375             : 
     376             :   // set mode to standby to disable transmitter/RF switch
     377           3 :   return(standby());
     378             : }
     379             : 
     380           3 : int16_t LR11x0::startReceive() {
     381           3 :   return(this->startReceive(RADIOLIB_LR11X0_RX_TIMEOUT_INF, RADIOLIB_IRQ_RX_DEFAULT_FLAGS, RADIOLIB_IRQ_RX_DEFAULT_MASK, 0));
     382             : }
     383             : 
     384           3 : int16_t LR11x0::readData(uint8_t* data, size_t len) {
     385             :   // check active modem
     386           3 :   int16_t state = RADIOLIB_ERR_NONE;
     387           3 :   uint8_t modem = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     388           3 :   state = getPacketType(&modem);
     389           3 :   RADIOLIB_ASSERT(state);
     390           0 :   if((modem != RADIOLIB_LR11X0_PACKET_TYPE_LORA) && 
     391           0 :      (modem != RADIOLIB_LR11X0_PACKET_TYPE_GFSK)) {
     392           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     393             :   }
     394             : 
     395             :   // check integrity CRC
     396           0 :   uint32_t irq = getIrqStatus();
     397           0 :   int16_t crcState = RADIOLIB_ERR_NONE;
     398             :   // 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)
     399           0 :   if((irq & RADIOLIB_LR11X0_IRQ_CRC_ERR) || ((irq & RADIOLIB_LR11X0_IRQ_HEADER_ERR) && !(irq & RADIOLIB_LR11X0_IRQ_SYNC_WORD_HEADER_VALID))) {
     400           0 :     crcState = RADIOLIB_ERR_CRC_MISMATCH;
     401             :   }
     402             : 
     403             :   // get packet length
     404             :   // the offset is needed since LR11x0 seems to move the buffer base by 4 bytes on every packet
     405           0 :   uint8_t offset = 0;
     406           0 :   size_t length = getPacketLength(true, &offset);
     407           0 :   if((len != 0) && (len < length)) {
     408             :     // user requested less data than we got, only return what was requested
     409           0 :     length = len;
     410             :   }
     411             : 
     412             :   // read packet data
     413           0 :   state = readBuffer8(data, length, offset);
     414           0 :   RADIOLIB_ASSERT(state);
     415             : 
     416             :   // clear the Rx buffer
     417           0 :   state = clearRxBuffer();
     418           0 :   RADIOLIB_ASSERT(state);
     419             : 
     420             :   // clear interrupt flags
     421           0 :   state = clearIrqState(RADIOLIB_LR11X0_IRQ_ALL);
     422             : 
     423             :   // check if CRC failed - this is done after reading data to give user the option to keep them
     424           0 :   RADIOLIB_ASSERT(crcState);
     425             : 
     426           0 :   return(state);
     427             : }
     428             : 
     429           3 : int16_t LR11x0::finishReceive() {
     430             :   // set mode to standby to disable RF switch
     431           3 :   int16_t state = standby();
     432           3 :   RADIOLIB_ASSERT(state);
     433             : 
     434             :   // clear interrupt flags
     435           0 :   return(clearIrqState(RADIOLIB_LR11X0_IRQ_ALL));
     436             : }
     437             : 
     438           3 : int16_t LR11x0::startChannelScan() {
     439           3 :   ChannelScanConfig_t cfg = {
     440             :     .cad = {
     441             :       .symNum = RADIOLIB_LR11X0_CAD_PARAM_DEFAULT,
     442             :       .detPeak = RADIOLIB_LR11X0_CAD_PARAM_DEFAULT,
     443             :       .detMin = RADIOLIB_LR11X0_CAD_PARAM_DEFAULT,
     444             :       .exitMode = RADIOLIB_LR11X0_CAD_PARAM_DEFAULT,
     445             :       .timeout = 0,
     446             :       .irqFlags = RADIOLIB_IRQ_CAD_DEFAULT_FLAGS,
     447             :       .irqMask = RADIOLIB_IRQ_CAD_DEFAULT_MASK,
     448             :     },
     449             :   };
     450           6 :   return(this->startChannelScan(cfg));
     451             : }
     452             : 
     453          12 : int16_t LR11x0::startChannelScan(const ChannelScanConfig_t &config) {
     454             :   // check active modem
     455          12 :   int16_t state = RADIOLIB_ERR_NONE;
     456          12 :   uint8_t modem = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     457          12 :   state = getPacketType(&modem);
     458          12 :   RADIOLIB_ASSERT(state);
     459           0 :   if(modem != RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
     460           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     461             :   }
     462             : 
     463             :   // set mode to standby
     464           0 :   state = standby();
     465           0 :   RADIOLIB_ASSERT(state);
     466             : 
     467             :   // set RF switch (if present)
     468           0 :   this->mod->setRfSwitchState(Module::MODE_RX);
     469             : 
     470             :   // set DIO pin mapping
     471           0 :   uint16_t irqFlags = (config.cad.irqFlags == RADIOLIB_IRQ_NOT_SUPPORTED) ? RADIOLIB_LR11X0_IRQ_CAD_DETECTED | RADIOLIB_LR11X0_IRQ_CAD_DONE : config.cad.irqFlags;
     472           0 :   state = setDioIrqParams(getIrqMapped(irqFlags), getIrqMapped(irqFlags));
     473           0 :   RADIOLIB_ASSERT(state);
     474             : 
     475             :   // clear interrupt flags
     476           0 :   state = clearIrqState(RADIOLIB_LR11X0_IRQ_ALL);
     477           0 :   RADIOLIB_ASSERT(state);
     478             : 
     479             :   // set mode to CAD
     480           0 :   return(startCad(config.cad.symNum, config.cad.detPeak, config.cad.detMin, config.cad.exitMode, config.cad.timeout));
     481             : }
     482             : 
     483           3 : int16_t LR11x0::getChannelScanResult() {
     484             :   // check active modem
     485           3 :   int16_t state = RADIOLIB_ERR_NONE;
     486           3 :   uint8_t modem = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     487           3 :   state = getPacketType(&modem);
     488           3 :   RADIOLIB_ASSERT(state);
     489           0 :   if(modem != RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
     490           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     491             :   }
     492             : 
     493             :   // check CAD result
     494           0 :   uint32_t cadResult = getIrqStatus();
     495           0 :   if(cadResult & RADIOLIB_LR11X0_IRQ_CAD_DETECTED) {
     496             :     // detected some LoRa activity
     497           0 :     return(RADIOLIB_LORA_DETECTED);
     498           0 :   } else if(cadResult & RADIOLIB_LR11X0_IRQ_CAD_DONE) {
     499             :     // channel is free
     500           0 :     return(RADIOLIB_CHANNEL_FREE);
     501             :   }
     502             : 
     503           0 :   return(RADIOLIB_ERR_UNKNOWN);
     504             : }
     505             : 
     506           0 : int16_t LR11x0::setBandwidth(float bw, bool high) {
     507             :   // check active modem
     508           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     509           0 :   int16_t state = getPacketType(&type);
     510           0 :   RADIOLIB_ASSERT(state);
     511           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
     512           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     513             :   }
     514             : 
     515             :   // ensure byte conversion doesn't overflow
     516           0 :   if (high) {
     517           0 :     RADIOLIB_CHECK_RANGE(bw, 203.125f, 815.0f, RADIOLIB_ERR_INVALID_BANDWIDTH);
     518             : 
     519           0 :     if(fabsf(bw - 203.125f) <= 0.001f) {
     520           0 :       this->bandwidth = RADIOLIB_LR11X0_LORA_BW_203_125;
     521           0 :     } else if(fabsf(bw - 406.25f) <= 0.001f) {
     522           0 :       this->bandwidth = RADIOLIB_LR11X0_LORA_BW_406_25;
     523           0 :     } else if(fabsf(bw - 812.5f) <= 0.001f) {
     524           0 :       this->bandwidth = RADIOLIB_LR11X0_LORA_BW_812_50;
     525             :     } else {
     526           0 :       return(RADIOLIB_ERR_INVALID_BANDWIDTH);
     527             :     }
     528             :   } else {
     529           0 :     RADIOLIB_CHECK_RANGE(bw, 0.0f, 510.0f, RADIOLIB_ERR_INVALID_BANDWIDTH);
     530             :     
     531             :     // check allowed bandwidth values
     532           0 :     uint8_t bw_div2 = bw / 2 + 0.01f;
     533           0 :     switch (bw_div2)  {
     534           0 :       case 31: // 62.5:
     535           0 :         this->bandwidth = RADIOLIB_LR11X0_LORA_BW_62_5;
     536           0 :         break;
     537           0 :       case 62: // 125.0:
     538           0 :         this->bandwidth = RADIOLIB_LR11X0_LORA_BW_125_0;
     539           0 :         break;
     540           0 :       case 125: // 250.0
     541           0 :         this->bandwidth = RADIOLIB_LR11X0_LORA_BW_250_0;
     542           0 :         break;
     543           0 :       case 250: // 500.0
     544           0 :         this->bandwidth = RADIOLIB_LR11X0_LORA_BW_500_0;
     545           0 :         break;
     546           0 :       default:
     547           0 :         return(RADIOLIB_ERR_INVALID_BANDWIDTH);
     548             :     }
     549             :   }
     550             : 
     551             :   // update modulation parameters
     552           0 :   this->bandwidthKhz = bw;
     553           0 :   return(setModulationParamsLoRa(this->spreadingFactor, this->bandwidth, this->codingRate, this->ldrOptimize));
     554             : }
     555             : 
     556           0 : int16_t LR11x0::setSpreadingFactor(uint8_t sf, bool legacy) {
     557             :   // check active modem
     558           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     559           0 :   int16_t state = getPacketType(&type);
     560           0 :   RADIOLIB_ASSERT(state);
     561           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
     562           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     563             :   }
     564             : 
     565           0 :   RADIOLIB_CHECK_RANGE(sf, 5, 12, RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
     566             : 
     567             :   // TODO enable SF6 legacy mode
     568             :   if(legacy && (sf == 6)) {
     569             :     //this->mod->SPIsetRegValue(RADIOLIB_LR11X0_REG_SF6_SX127X_COMPAT, RADIOLIB_LR11X0_SF6_SX127X, 18, 18);
     570             :   }
     571             : 
     572             :   // update modulation parameters
     573           0 :   this->spreadingFactor = sf;
     574           0 :   return(setModulationParamsLoRa(this->spreadingFactor, this->bandwidth, this->codingRate, this->ldrOptimize));
     575             : }
     576             : 
     577           0 : int16_t LR11x0::setCodingRate(uint8_t cr, bool longInterleave) {
     578             :   // check active modem
     579           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     580           0 :   int16_t state = getPacketType(&type);
     581           0 :   RADIOLIB_ASSERT(state);
     582           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
     583           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     584             :   }
     585             : 
     586           0 :   RADIOLIB_CHECK_RANGE(cr, 4, 8, RADIOLIB_ERR_INVALID_CODING_RATE);
     587             : 
     588           0 :   if(longInterleave) {
     589           0 :     switch(cr) {
     590           0 :       case 4:
     591           0 :         this->codingRate = 0;
     592           0 :         break;
     593           0 :       case 5:
     594             :       case 6:
     595           0 :         this->codingRate = cr;
     596           0 :         break;
     597           0 :       case 8: 
     598           0 :         this->codingRate = cr - 1;
     599           0 :         break;
     600           0 :       default:
     601           0 :         return(RADIOLIB_ERR_INVALID_CODING_RATE);
     602             :     }
     603             :   
     604             :   } else {
     605           0 :     this->codingRate = cr - 4;
     606             :   
     607             :   }
     608             : 
     609             :   // update modulation parameters
     610           0 :   return(setModulationParamsLoRa(this->spreadingFactor, this->bandwidth, this->codingRate, this->ldrOptimize));
     611             : }
     612             : 
     613           0 : int16_t LR11x0::setSyncWord(uint8_t syncWord) {
     614             :   // check active modem
     615           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     616           0 :   int16_t state = getPacketType(&type);
     617           0 :   RADIOLIB_ASSERT(state);
     618           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
     619           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     620             :   }
     621             :   
     622           0 :   return(setLoRaSyncWord(syncWord));
     623             : }
     624             : 
     625           3 : int16_t LR11x0::setBitRate(float br) {
     626           3 :   RADIOLIB_CHECK_RANGE(br, 0.6f, 300.0f, RADIOLIB_ERR_INVALID_BIT_RATE);
     627             : 
     628             :   // check active modem
     629           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     630           0 :   int16_t state = getPacketType(&type);
     631           0 :   RADIOLIB_ASSERT(state);
     632           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
     633           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     634             :   }
     635             : 
     636             :   // set bit rate value
     637             :   // TODO implement fractional bit rate configuration
     638           0 :   this->bitRate = br * 1000.0f;
     639           0 :   state = setModulationParamsGFSK(this->bitRate, this->pulseShape, this->rxBandwidth, this->frequencyDev);
     640           0 :   RADIOLIB_ASSERT(state);
     641             : 
     642             :   // apply workaround
     643           0 :   return(workaroundGFSK());
     644             : }
     645             : 
     646           3 : int16_t LR11x0::setFrequencyDeviation(float freqDev) {
     647             :   // check active modem
     648           3 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     649           3 :   int16_t state = getPacketType(&type);
     650           3 :   RADIOLIB_ASSERT(state);
     651           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
     652           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     653             :   }
     654             : 
     655             :   // set frequency deviation to lowest available setting (required for digimodes)
     656           0 :   float newFreqDev = freqDev;
     657           0 :   if(freqDev < 0.0f) {
     658           0 :     newFreqDev = 0.6f;
     659             :   }
     660             : 
     661           0 :   RADIOLIB_CHECK_RANGE(newFreqDev, 0.6f, 200.0f, RADIOLIB_ERR_INVALID_FREQUENCY_DEVIATION);
     662           0 :   this->frequencyDev = newFreqDev * 1000.0f;
     663           0 :   state = setModulationParamsGFSK(this->bitRate, this->pulseShape, this->rxBandwidth, this->frequencyDev);
     664           0 :   RADIOLIB_ASSERT(state);
     665             : 
     666             :   // apply workaround
     667           0 :   return(workaroundGFSK());
     668             : }
     669             : 
     670           0 : int16_t LR11x0::setRxBandwidth(float rxBw) {
     671             :   // check active modem
     672           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     673           0 :   int16_t state = getPacketType(&type);
     674           0 :   RADIOLIB_ASSERT(state);
     675           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
     676           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     677             :   }
     678             : 
     679             :   // check modulation parameters
     680             :   /*if(2 * this->frequencyDev + this->bitRate > rxBw * 1000.0) {
     681             :     return(RADIOLIB_ERR_INVALID_MODULATION_PARAMETERS);
     682             :   }*/
     683             : 
     684           0 :   const uint8_t rxBwLut[] = {
     685             :     RADIOLIB_LR11X0_GFSK_RX_BW_4_8,
     686             :     RADIOLIB_LR11X0_GFSK_RX_BW_5_8,
     687             :     RADIOLIB_LR11X0_GFSK_RX_BW_7_3,
     688             :     RADIOLIB_LR11X0_GFSK_RX_BW_9_7,
     689             :     RADIOLIB_LR11X0_GFSK_RX_BW_11_7,
     690             :     RADIOLIB_LR11X0_GFSK_RX_BW_14_6,
     691             :     RADIOLIB_LR11X0_GFSK_RX_BW_19_5,
     692             :     RADIOLIB_LR11X0_GFSK_RX_BW_23_4,
     693             :     RADIOLIB_LR11X0_GFSK_RX_BW_29_3,
     694             :     RADIOLIB_LR11X0_GFSK_RX_BW_39_0,
     695             :     RADIOLIB_LR11X0_GFSK_RX_BW_46_9,
     696             :     RADIOLIB_LR11X0_GFSK_RX_BW_58_6,
     697             :     RADIOLIB_LR11X0_GFSK_RX_BW_78_2,
     698             :     RADIOLIB_LR11X0_GFSK_RX_BW_93_8,
     699             :     RADIOLIB_LR11X0_GFSK_RX_BW_117_3,
     700             :     RADIOLIB_LR11X0_GFSK_RX_BW_156_2,
     701             :     RADIOLIB_LR11X0_GFSK_RX_BW_187_2,
     702             :     RADIOLIB_LR11X0_GFSK_RX_BW_234_3,
     703             :     RADIOLIB_LR11X0_GFSK_RX_BW_312_0,
     704             :     RADIOLIB_LR11X0_GFSK_RX_BW_373_6,
     705             :     RADIOLIB_LR11X0_GFSK_RX_BW_467_0,
     706             :   };
     707             : 
     708           0 :   state = findRxBw(rxBw, rxBwLut, sizeof(rxBwLut)/sizeof(rxBwLut[0]), 467.0f, &this->rxBandwidth);
     709           0 :   RADIOLIB_ASSERT(state);
     710             : 
     711             :   // update modulation parameters
     712           0 :   state = setModulationParamsGFSK(this->bitRate, this->pulseShape, this->rxBandwidth, this->frequencyDev);
     713           0 :   RADIOLIB_ASSERT(state);
     714             : 
     715             :   // apply workaround
     716           0 :   return(workaroundGFSK());
     717             : }
     718             : 
     719           3 : int16_t LR11x0::setSyncWord(uint8_t* syncWord, size_t len) {
     720           3 :   if((!syncWord) || (!len) || (len > RADIOLIB_LR11X0_GFSK_SYNC_WORD_LEN)) {
     721           0 :     return(RADIOLIB_ERR_INVALID_SYNC_WORD);
     722             :   }
     723             : 
     724             :   // check active modem
     725           3 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     726           3 :   int16_t state = getPacketType(&type);
     727           3 :   RADIOLIB_ASSERT(state);
     728           0 :   if(type == RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
     729             :     // update sync word length
     730           0 :     this->syncWordLength = len*8;
     731           0 :     state = setPacketParamsGFSK(this->preambleLengthGFSK, this->preambleDetLength, this->syncWordLength, this->addrComp, this->packetType, RADIOLIB_LR11X0_MAX_PACKET_LENGTH, this->crcTypeGFSK, this->whitening);
     732           0 :     RADIOLIB_ASSERT(state);
     733             : 
     734             :     // sync word is passed most-significant byte first
     735           0 :     uint8_t fullSyncWord[RADIOLIB_LR11X0_GFSK_SYNC_WORD_LEN] = { 0 };
     736           0 :     memcpy(fullSyncWord, syncWord, len);
     737           0 :     return(setGfskSyncWord(fullSyncWord));
     738             : 
     739           0 :   } else if(type == RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
     740             :     // with length set to 1 and LoRa modem active, assume it is the LoRa sync word
     741           0 :     if(len > 1) {
     742           0 :       return(RADIOLIB_ERR_INVALID_SYNC_WORD);
     743             :     }
     744           0 :     return(setSyncWord(syncWord[0]));
     745             : 
     746           0 :   } else if(type == RADIOLIB_LR11X0_PACKET_TYPE_LR_FHSS) {
     747             :     // with length set to 4 and LR-FHSS modem active, assume it is the LR-FHSS sync word
     748           0 :     if(len != sizeof(uint32_t)) {
     749           0 :       return(RADIOLIB_ERR_INVALID_SYNC_WORD);
     750             :     }
     751           0 :     uint32_t sync = 0;
     752           0 :     memcpy(&sync, syncWord, sizeof(uint32_t));
     753           0 :     return(lrFhssSetSyncWord(sync));
     754             :   
     755             :   }
     756             : 
     757           0 :   return(RADIOLIB_ERR_WRONG_MODEM);
     758             : }
     759             : 
     760           0 : int16_t LR11x0::setNodeAddress(uint8_t nodeAddr) {
     761             :   // check active modem
     762           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     763           0 :   int16_t state = getPacketType(&type);
     764           0 :   RADIOLIB_ASSERT(state);
     765           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
     766           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     767             :   }
     768             : 
     769             :   // enable address filtering (node only)
     770           0 :   this->addrComp = RADIOLIB_LR11X0_GFSK_ADDR_FILTER_NODE;
     771           0 :   state = setPacketParamsGFSK(this->preambleLengthGFSK, this->preambleDetLength, this->syncWordLength, this->addrComp, this->packetType, RADIOLIB_LR11X0_MAX_PACKET_LENGTH, this->crcTypeGFSK, this->whitening);
     772           0 :   RADIOLIB_ASSERT(state);
     773             :   
     774             :   // set node address
     775           0 :   this->node = nodeAddr;
     776           0 :   return(setPacketAdrs(this->node, 0));
     777             : }
     778             : 
     779           0 : int16_t LR11x0::setBroadcastAddress(uint8_t broadAddr) {
     780             :   // check active modem
     781           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     782           0 :   int16_t state = getPacketType(&type);
     783           0 :   RADIOLIB_ASSERT(state);
     784           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
     785           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     786             :   }
     787             : 
     788             :   // enable address filtering (node and broadcast)
     789           0 :   this->addrComp = RADIOLIB_LR11X0_GFSK_ADDR_FILTER_NODE_BROADCAST;
     790           0 :   state = setPacketParamsGFSK(this->preambleLengthGFSK, this->preambleDetLength, this->syncWordLength, this->addrComp, this->packetType, RADIOLIB_LR11X0_MAX_PACKET_LENGTH, this->crcTypeGFSK, this->whitening);
     791           0 :   RADIOLIB_ASSERT(state);
     792             :   
     793             :   // set node and broadcast address
     794           0 :   return(setPacketAdrs(this->node, broadAddr));
     795             : }
     796             : 
     797           0 : int16_t LR11x0::disableAddressFiltering() {
     798             :   // check active modem
     799           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     800           0 :   int16_t state = getPacketType(&type);
     801           0 :   RADIOLIB_ASSERT(state);
     802           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
     803           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     804             :   }
     805             : 
     806             :   // disable address filtering
     807           0 :   this->addrComp = RADIOLIB_LR11X0_GFSK_ADDR_FILTER_DISABLED;
     808           0 :   return(setPacketParamsGFSK(this->preambleLengthGFSK, this->preambleDetLength, this->syncWordLength, this->addrComp, this->packetType, RADIOLIB_LR11X0_MAX_PACKET_LENGTH, this->crcTypeGFSK, this->whitening));
     809             : }
     810             : 
     811           3 : int16_t LR11x0::setDataShaping(uint8_t sh) {
     812             :   // check active modem
     813           3 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     814           3 :   int16_t state = getPacketType(&type);
     815           3 :   RADIOLIB_ASSERT(state);
     816           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
     817           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     818             :   }
     819             : 
     820             :   // set data shaping
     821           0 :   switch(sh) {
     822           0 :     case RADIOLIB_SHAPING_NONE:
     823           0 :       this->pulseShape = RADIOLIB_LR11X0_GFSK_SHAPING_NONE;
     824           0 :       break;
     825           0 :     case RADIOLIB_SHAPING_0_3:
     826           0 :       this->pulseShape = RADIOLIB_LR11X0_GFSK_SHAPING_GAUSSIAN_BT_0_3;
     827           0 :       break;
     828           0 :     case RADIOLIB_SHAPING_0_5:
     829           0 :       this->pulseShape = RADIOLIB_LR11X0_GFSK_SHAPING_GAUSSIAN_BT_0_5;
     830           0 :       break;
     831           0 :     case RADIOLIB_SHAPING_0_7:
     832           0 :       this->pulseShape = RADIOLIB_LR11X0_GFSK_SHAPING_GAUSSIAN_BT_0_7;
     833           0 :       break;
     834           0 :     case RADIOLIB_SHAPING_1_0:
     835           0 :       this->pulseShape = RADIOLIB_LR11X0_GFSK_SHAPING_GAUSSIAN_BT_1_0;
     836           0 :       break;
     837           0 :     default:
     838           0 :       return(RADIOLIB_ERR_INVALID_DATA_SHAPING);
     839             :   }
     840             : 
     841             :   // update modulation parameters
     842           0 :   return(setModulationParamsGFSK(this->bitRate, this->pulseShape, this->rxBandwidth, this->frequencyDev));
     843             : }
     844             : 
     845           3 : int16_t LR11x0::setEncoding(uint8_t encoding) {
     846           3 :   return(setWhitening(encoding));
     847             : }
     848             : 
     849           0 : int16_t LR11x0::fixedPacketLengthMode(uint8_t len) {
     850           0 :   return(setPacketMode(RADIOLIB_LR11X0_GFSK_PACKET_LENGTH_FIXED, len));
     851             : }
     852             : 
     853           0 : int16_t LR11x0::variablePacketLengthMode(uint8_t maxLen) {
     854           0 :   return(setPacketMode(RADIOLIB_LR11X0_GFSK_PACKET_LENGTH_VARIABLE, maxLen));
     855             : }
     856             : 
     857           3 : int16_t LR11x0::setWhitening(bool enabled, uint16_t initial) {
     858             :   // check active modem
     859           3 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     860           3 :   int16_t state = getPacketType(&type);
     861           3 :   RADIOLIB_ASSERT(state);
     862           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
     863           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
     864             :   }
     865             : 
     866           0 :   if(!enabled) {
     867             :     // disable whitening
     868           0 :     this->whitening = RADIOLIB_LR11X0_GFSK_WHITENING_DISABLED;
     869             : 
     870             :   } else {
     871             :     // enable whitening
     872           0 :     this->whitening = RADIOLIB_LR11X0_GFSK_WHITENING_ENABLED;
     873             : 
     874             :     // write initial whitening value
     875           0 :     state = setGfskWhitParams(initial);
     876           0 :     RADIOLIB_ASSERT(state);
     877             :   }
     878             : 
     879           0 :   return(setPacketParamsGFSK(this->preambleLengthGFSK, this->preambleDetLength, this->syncWordLength, this->addrComp, this->packetType, RADIOLIB_LR11X0_MAX_PACKET_LENGTH, this->crcTypeGFSK, this->whitening));
     880             : }
     881             : 
     882           3 : int16_t LR11x0::setDataRate(DataRate_t dr, ModemType_t modem) {
     883             :   // get the current modem
     884             :   ModemType_t currentModem;
     885           3 :   int16_t state = this->getModem(&currentModem);
     886           3 :   RADIOLIB_ASSERT(state);
     887             : 
     888             :   // switch over if the requested modem is different
     889           0 :   if(modem != RADIOLIB_MODEM_NONE && modem != currentModem) {
     890           0 :     state = this->standby();
     891           0 :     RADIOLIB_ASSERT(state);
     892           0 :     state = this->setModem(modem);
     893           0 :     RADIOLIB_ASSERT(state);
     894             :   }
     895             :   
     896           0 :   if(modem == RADIOLIB_MODEM_NONE) {
     897           0 :     modem = currentModem;
     898             :   }
     899             : 
     900             :   // select interpretation based on modem
     901           0 :   if(modem == RADIOLIB_MODEM_FSK) {
     902             :     // set the bit rate
     903           0 :     state = this->setBitRate(dr.fsk.bitRate);
     904           0 :     RADIOLIB_ASSERT(state);
     905             : 
     906             :     // set the frequency deviation
     907           0 :     state = this->setFrequencyDeviation(dr.fsk.freqDev);
     908             : 
     909           0 :   } else if(modem == RADIOLIB_MODEM_LORA) {
     910             :     // set the spreading factor
     911           0 :     state = this->setSpreadingFactor(dr.lora.spreadingFactor);
     912           0 :     RADIOLIB_ASSERT(state);
     913             : 
     914             :     // set the bandwidth
     915           0 :     state = this->setBandwidth(dr.lora.bandwidth);
     916           0 :     RADIOLIB_ASSERT(state);
     917             : 
     918             :     // set the coding rate
     919           0 :     state = this->setCodingRate(dr.lora.codingRate);
     920             :   
     921           0 :   } else if(modem == RADIOLIB_MODEM_LRFHSS) {
     922             :     // set the basic config
     923           0 :     state = this->setLrFhssConfig(dr.lrFhss.bw, dr.lrFhss.cr);
     924           0 :     RADIOLIB_ASSERT(state);
     925             : 
     926             :     // set hopping grid
     927           0 :     this->lrFhssGrid = dr.lrFhss.narrowGrid ? RADIOLIB_LRXXXX_LR_FHSS_GRID_STEP_NON_FCC : RADIOLIB_LRXXXX_LR_FHSS_GRID_STEP_FCC;
     928             :   
     929             :   }
     930             : 
     931           0 :   return(state);
     932             : }
     933             : 
     934           3 : int16_t LR11x0::checkDataRate(DataRate_t dr, ModemType_t modem) {
     935           3 :   int16_t state = RADIOLIB_ERR_UNKNOWN;
     936             : 
     937             :   // retrieve modem if not supplied
     938           3 :   if(modem == RADIOLIB_MODEM_NONE) {
     939           3 :     state = this->getModem(&modem);
     940           3 :     RADIOLIB_ASSERT(state);
     941             :   }
     942             : 
     943             :   // select interpretation based on modem
     944           0 :   if(modem == RADIOLIB_MODEM_FSK) {
     945           0 :     RADIOLIB_CHECK_RANGE(dr.fsk.bitRate, 0.6f, 300.0f, RADIOLIB_ERR_INVALID_BIT_RATE);
     946           0 :     RADIOLIB_CHECK_RANGE(dr.fsk.freqDev, 0.6f, 200.0f, RADIOLIB_ERR_INVALID_FREQUENCY_DEVIATION);
     947           0 :     return(RADIOLIB_ERR_NONE);
     948             : 
     949           0 :   } else if(modem == RADIOLIB_MODEM_LORA) {
     950           0 :     RADIOLIB_CHECK_RANGE(dr.lora.spreadingFactor, 5, 12, RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
     951           0 :     RADIOLIB_CHECK_RANGE(dr.lora.bandwidth, 0.0f, 510.0f, RADIOLIB_ERR_INVALID_BANDWIDTH);
     952           0 :     RADIOLIB_CHECK_RANGE(dr.lora.codingRate, 4, 8, RADIOLIB_ERR_INVALID_CODING_RATE);
     953           0 :     return(RADIOLIB_ERR_NONE);
     954             :   
     955             :   }
     956             : 
     957           0 :   return(state);
     958             : }
     959             : 
     960           3 : int16_t LR11x0::setPreambleLength(size_t preambleLength) {
     961             :   // check active modem
     962           3 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
     963           3 :   int16_t state = getPacketType(&type);
     964           3 :   RADIOLIB_ASSERT(state);
     965           0 :   if(type == RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
     966           0 :     this->preambleLengthLoRa = preambleLength;
     967           0 :     return(setPacketParamsLoRa(this->preambleLengthLoRa, this->headerType,  this->implicitLen, this->crcTypeLoRa, (uint8_t)this->invertIQEnabled));
     968           0 :   } else if(type == RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
     969           0 :     this->preambleLengthGFSK = preambleLength;
     970             :     //! \TODO: [LR11x0] this can probably be simplified with a simple calculation
     971           0 :     this->preambleDetLength = preambleLength >= 32 ? RADIOLIB_LR11X0_GFSK_PREAMBLE_DETECT_32_BITS :
     972             :                               preambleLength >= 24 ? RADIOLIB_LR11X0_GFSK_PREAMBLE_DETECT_24_BITS :
     973             :                               preambleLength >= 16 ? RADIOLIB_LR11X0_GFSK_PREAMBLE_DETECT_16_BITS :
     974             :                               preambleLength >   0 ? RADIOLIB_LR11X0_GFSK_PREAMBLE_DETECT_8_BITS :
     975             :                               RADIOLIB_LR11X0_GFSK_PREAMBLE_DETECT_DISABLED;
     976           0 :     return(setPacketParamsGFSK(this->preambleLengthGFSK, this->preambleDetLength, this->syncWordLength, this->addrComp, this->packetType, RADIOLIB_LR11X0_MAX_PACKET_LENGTH, this->crcTypeGFSK, this->whitening));
     977             :   }
     978             : 
     979           0 :   return(RADIOLIB_ERR_WRONG_MODEM);
     980             : }
     981             : 
     982           0 : int16_t LR11x0::setTCXO(float voltage, uint32_t delay) {
     983             :   // check if TCXO is enabled at all
     984           0 :   if(this->XTAL) {
     985           0 :     return(RADIOLIB_ERR_INVALID_TCXO_VOLTAGE);
     986             :   }
     987             : 
     988             :   // set mode to standby
     989           0 :   standby();
     990             : 
     991             :   // check RADIOLIB_LR11X0_ERROR_STAT_HF_XOSC_START_ERR flag and clear it
     992           0 :   uint16_t errors = 0;
     993           0 :   int16_t state = getErrors(&errors);
     994           0 :   RADIOLIB_ASSERT(state);
     995           0 :   if(errors & RADIOLIB_LR11X0_ERROR_STAT_HF_XOSC_START_ERR) {
     996           0 :     clearErrors();
     997             :   }
     998             : 
     999             :   // check 0 V disable
    1000           0 :   if(fabsf(voltage - 0.0f) <= 0.001f) {
    1001           0 :     setTcxoMode(0, 0);
    1002           0 :     return(reset());
    1003             :   }
    1004             : 
    1005             :   // check allowed voltage values
    1006           0 :   uint8_t tune = 0;
    1007           0 :   if(fabsf(voltage - 1.6f) <= 0.001f) {
    1008           0 :     tune = RADIOLIB_LRXXXX_TCXO_VOLTAGE_1_6;
    1009           0 :   } else if(fabsf(voltage - 1.7f) <= 0.001f) {
    1010           0 :     tune = RADIOLIB_LRXXXX_TCXO_VOLTAGE_1_7;
    1011           0 :   } else if(fabsf(voltage - 1.8f) <= 0.001f) {
    1012           0 :     tune = RADIOLIB_LRXXXX_TCXO_VOLTAGE_1_8;
    1013           0 :   } else if(fabsf(voltage - 2.2f) <= 0.001f) {
    1014           0 :     tune = RADIOLIB_LRXXXX_TCXO_VOLTAGE_2_2;
    1015           0 :   } else if(fabsf(voltage - 2.4f) <= 0.001f) {
    1016           0 :     tune = RADIOLIB_LRXXXX_TCXO_VOLTAGE_2_4;
    1017           0 :   } else if(fabsf(voltage - 2.7f) <= 0.001f) {
    1018           0 :     tune = RADIOLIB_LRXXXX_TCXO_VOLTAGE_2_7;
    1019           0 :   } else if(fabsf(voltage - 3.0f) <= 0.001f) {
    1020           0 :     tune = RADIOLIB_LRXXXX_TCXO_VOLTAGE_3_0;
    1021           0 :   } else if(fabsf(voltage - 3.3f) <= 0.001f) {
    1022           0 :     tune = RADIOLIB_LRXXXX_TCXO_VOLTAGE_3_3;
    1023             :   } else {
    1024           0 :     return(RADIOLIB_ERR_INVALID_TCXO_VOLTAGE);
    1025             :   }
    1026             : 
    1027             :   // calculate delay value
    1028           0 :   uint32_t delayValue = (uint32_t)((float)delay / 30.52f);
    1029           0 :   if(delayValue == 0) {
    1030           0 :     delayValue = 1;
    1031             :   }
    1032             :  
    1033             :   // enable TCXO control
    1034           0 :   return(setTcxoMode(tune, delayValue));
    1035             : }
    1036             : 
    1037           0 : int16_t LR11x0::setCRC(uint8_t len, uint32_t initial, uint32_t polynomial, bool inverted) {
    1038             :   // check active modem
    1039           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1040           0 :   int16_t state = getPacketType(&type);
    1041           0 :   RADIOLIB_ASSERT(state);
    1042           0 :   if(type == RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
    1043             :     // LoRa CRC doesn't allow to set CRC polynomial, initial value, or inversion
    1044           0 :     this->crcTypeLoRa = len > 0 ? RADIOLIB_LRXXXX_LORA_CRC_ENABLED : RADIOLIB_LRXXXX_LORA_CRC_DISABLED;
    1045           0 :     state = setPacketParamsLoRa(this->preambleLengthLoRa, this->headerType, this->implicitLen, this->crcTypeLoRa, (uint8_t)this->invertIQEnabled);
    1046             :   
    1047           0 :   } else if(type == RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
    1048             :     // update packet parameters
    1049           0 :     switch(len) {
    1050           0 :       case 0:
    1051           0 :         this->crcTypeGFSK = RADIOLIB_LR11X0_GFSK_CRC_DISABLED;
    1052           0 :         break;
    1053           0 :       case 1:
    1054           0 :         if(inverted) {
    1055           0 :           this->crcTypeGFSK = RADIOLIB_LR11X0_GFSK_CRC_1_BYTE_INV;
    1056             :         } else {
    1057           0 :           this->crcTypeGFSK = RADIOLIB_LR11X0_GFSK_CRC_1_BYTE;
    1058             :         }
    1059           0 :         break;
    1060           0 :       case 2:
    1061           0 :         if(inverted) {
    1062           0 :           this->crcTypeGFSK = RADIOLIB_LR11X0_GFSK_CRC_2_BYTE_INV;
    1063             :         } else {
    1064           0 :           this->crcTypeGFSK = RADIOLIB_LR11X0_GFSK_CRC_2_BYTE;
    1065             :         }
    1066           0 :         break;
    1067           0 :       default:
    1068           0 :         return(RADIOLIB_ERR_INVALID_CRC_CONFIGURATION);
    1069             :     }
    1070             : 
    1071           0 :     this->crcLenGFSK = len;
    1072           0 :     state = setPacketParamsGFSK(this->preambleLengthGFSK, this->preambleDetLength, this->syncWordLength, this->addrComp, this->packetType, RADIOLIB_LR11X0_MAX_PACKET_LENGTH, this->crcTypeGFSK, this->whitening);
    1073           0 :     RADIOLIB_ASSERT(state);
    1074             : 
    1075           0 :     state = setGfskCrcParams(initial, polynomial);
    1076             :   
    1077             :   }
    1078             : 
    1079           0 :   return(state);
    1080             : }
    1081             : 
    1082           3 : int16_t LR11x0::invertIQ(bool enable) {
    1083             :   // check active modem
    1084           3 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1085           3 :   int16_t state = getPacketType(&type);
    1086           3 :   RADIOLIB_ASSERT(state);
    1087           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
    1088           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
    1089             :   }
    1090             : 
    1091           0 :   this->invertIQEnabled = enable;
    1092           0 :   return(setPacketParamsLoRa(this->preambleLengthLoRa, this->headerType, this->implicitLen, this->crcTypeLoRa, (uint8_t)this->invertIQEnabled));
    1093             : }
    1094             : 
    1095           3 : float LR11x0::getRSSI() {
    1096           3 :   float val = 0;
    1097             : 
    1098             :   // check active modem
    1099           3 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1100           3 :   (void)getPacketType(&type);
    1101           3 :   if(type == RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
    1102           0 :     (void)getPacketStatusLoRa(&val, NULL, NULL);
    1103             : 
    1104           3 :   } else if(type == RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
    1105           0 :     (void)getPacketStatusGFSK(NULL, &val, NULL, NULL);
    1106             :   
    1107             :   }
    1108             : 
    1109           3 :   return(val);
    1110             : }
    1111             : 
    1112           0 : float LR11x0::getRSSI(bool packet, bool skipReceive) {
    1113           0 :   float val = 0;
    1114             : 
    1115             :   // check if RSSI of packet is requested
    1116           0 :   if (packet) {
    1117           0 :     val = getRSSI();
    1118             :   } else {
    1119           0 :     if(!skipReceive) { (void)startReceive(); }
    1120           0 :     int16_t state = getRssiInst(&val);
    1121           0 :     if(!skipReceive) { (void)standby(); }
    1122           0 :     if(state != RADIOLIB_ERR_NONE) { return(0); }
    1123             :   }
    1124             : 
    1125           0 :   return(val);
    1126             : }
    1127             : 
    1128           3 : float LR11x0::getSNR() {
    1129           3 :   float val = 0;
    1130             : 
    1131             :   // check active modem
    1132           3 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1133           3 :   (void)getPacketType(&type);
    1134           3 :   if(type == RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
    1135           0 :     (void)getPacketStatusLoRa(NULL, &val, NULL);
    1136             :   }
    1137             : 
    1138           3 :   return(val);
    1139             : }
    1140             : 
    1141           0 : float LR11x0::getFrequencyError() {
    1142             :   // TODO implement this
    1143           0 :   return(0);
    1144             : }
    1145             : 
    1146           3 : size_t LR11x0::getPacketLength(bool update) {
    1147           3 :   return(this->getPacketLength(update, NULL));
    1148             : }
    1149             : 
    1150           3 : size_t LR11x0::getPacketLength(bool update, uint8_t* offset) {
    1151             :   (void)update;
    1152             : 
    1153             :   // in implicit mode, return the cached value
    1154           3 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1155           3 :   (void)getPacketType(&type);
    1156           3 :   if((type == RADIOLIB_LR11X0_PACKET_TYPE_LORA) && (this->headerType == RADIOLIB_LRXXXX_LORA_HEADER_IMPLICIT)) {
    1157           0 :     return(this->implicitLen);
    1158             :   }
    1159             : 
    1160           3 :   uint8_t len = 0;
    1161           3 :   int state = getRxBufferStatus(&len, offset);
    1162             :   RADIOLIB_DEBUG_BASIC_PRINT("getRxBufferStatus state = %d\n", state);
    1163             :   (void)state;
    1164           3 :   return((size_t)len);
    1165             : }
    1166             : 
    1167           3 : RadioLibTime_t LR11x0::getTimeOnAir(size_t len) {
    1168             :   ModemType_t modem;
    1169           3 :   getModem(&modem);
    1170           6 :   return(LRxxxx::getTimeOnAir(len, modem));
    1171             : }
    1172             : 
    1173           3 : uint32_t LR11x0::getIrqFlags() {
    1174           3 :   return((uint32_t)this->getIrqStatus());
    1175             : }
    1176             : 
    1177           3 : int16_t LR11x0::setIrqFlags(uint32_t irq) {
    1178           3 :   return(this->setDioIrqParams(irq, irq));
    1179             : }
    1180             : 
    1181           3 : int16_t LR11x0::clearIrqFlags(uint32_t irq) {
    1182           3 :   return(this->clearIrqState(irq));
    1183             : }
    1184             : 
    1185           3 : uint8_t LR11x0::randomByte() {
    1186           3 :   uint32_t num = 0;
    1187           3 :   (void)getRandomNumber(&num);
    1188           3 :   return((uint8_t)num);
    1189             : }
    1190             : 
    1191           0 : int16_t LR11x0::implicitHeader(size_t len) {
    1192           0 :   return(this->setHeaderType(RADIOLIB_LRXXXX_LORA_HEADER_IMPLICIT, len));
    1193             : }
    1194             : 
    1195           0 : int16_t LR11x0::explicitHeader() {
    1196           0 :   return(this->setHeaderType(RADIOLIB_LRXXXX_LORA_HEADER_EXPLICIT));
    1197             : }
    1198             : 
    1199           0 : int16_t LR11x0::setRegulatorLDO() {
    1200           0 :   return(this->setRegMode(RADIOLIB_LR11X0_REG_MODE_LDO));
    1201             : }
    1202             : 
    1203           0 : int16_t LR11x0::setRegulatorDCDC() {
    1204           0 :   return(this->setRegMode(RADIOLIB_LR11X0_REG_MODE_DC_DC));
    1205             : }
    1206             : 
    1207           0 : int16_t LR11x0::setRxBoostedGainMode(bool en) {
    1208           0 :   uint8_t buff[1] = { (uint8_t)en };
    1209           0 :   return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_RX_BOOSTED, true, buff, sizeof(buff)));
    1210             : }
    1211             : 
    1212           3 : int16_t LR11x0::setOutputPower(int8_t power, uint8_t paSel, uint8_t regPaSupply, uint8_t paDutyCycle, uint8_t paHpSel, uint8_t rampTime) {
    1213             :   // set PA config
    1214           3 :   int16_t state = setPaConfig(paSel, regPaSupply, paDutyCycle, paHpSel);
    1215           3 :   RADIOLIB_ASSERT(state);
    1216             : 
    1217             :   // set output power
    1218           0 :   state = setTxParams(power, rampTime);
    1219           0 :   return(state);
    1220             : }
    1221             : 
    1222           0 : void LR11x0::setRfSwitchTable(const uint32_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
    1223             :   // find which pins are used
    1224           0 :   uint8_t enable = 0;
    1225           0 :   for(size_t i = 0; i < Module::RFSWITCH_MAX_PINS; i++) {
    1226             :     // check if this pin is unused
    1227           0 :     if(pins[i] == RADIOLIB_NC) {
    1228           0 :       continue;
    1229             :     }
    1230             : 
    1231             :     // only keep DIO pins, there may be some GPIOs in the switch tabke
    1232           0 :     if(pins[i] & RFSWITCH_PIN_FLAG) {
    1233           0 :       enable |= 1UL << RADIOLIB_LRXXXX_DIOx_VAL(pins[i]);
    1234             :     }
    1235             :     
    1236             :   }
    1237             : 
    1238             :   // now get the configuration
    1239           0 :   uint8_t modes[7] = { 0 };
    1240           0 :   for(size_t i = 0; i < 7; i++) {
    1241             :     // check end of table
    1242           0 :     if(table[i].mode == LR11x0::MODE_END_OF_TABLE) {
    1243           0 :       break;
    1244             :     }
    1245             : 
    1246             :     // get the mode ID in case the modes are out-of-order
    1247           0 :     uint8_t index = table[i].mode - LR11x0::MODE_STBY;
    1248             : 
    1249             :     // iterate over the pins
    1250           0 :     for(size_t j = 0; j < Module::RFSWITCH_MAX_PINS; j++) {
    1251             :       // only process modes for the DIOx pins, skip GPIO pins
    1252           0 :       if(!(pins[j] & RFSWITCH_PIN_FLAG)) {
    1253           0 :         continue;
    1254             :       }
    1255           0 :       modes[index] |= (table[i].values[j] == this->mod->hal->GpioLevelHigh) ? (1UL << j) : 0;
    1256             :     }
    1257             :   }
    1258             : 
    1259             :   // set it
    1260           0 :   this->setDioAsRfSwitch(enable, modes[0], modes[1], modes[2], modes[3], modes[4], modes[5], modes[6]);
    1261           0 : }
    1262             : 
    1263           0 : int16_t LR11x0::forceLDRO(bool enable) {
    1264             :   // check packet type
    1265           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1266           0 :   int16_t state = getPacketType(&type);
    1267           0 :   RADIOLIB_ASSERT(state);
    1268           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
    1269           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
    1270             :   }
    1271             : 
    1272             :   // update modulation parameters
    1273           0 :   this->ldroAuto = false;
    1274           0 :   this->ldrOptimize = (uint8_t)enable;
    1275           0 :   return(setModulationParamsLoRa(this->spreadingFactor, this->bandwidth, this->codingRate, this->ldrOptimize));
    1276             : }
    1277             : 
    1278           0 : int16_t LR11x0::autoLDRO() {
    1279           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1280           0 :   int16_t state = getPacketType(&type);
    1281           0 :   RADIOLIB_ASSERT(state);
    1282           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
    1283           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
    1284             :   }
    1285             : 
    1286           0 :   this->ldroAuto = true;
    1287           0 :   return(RADIOLIB_ERR_NONE);
    1288             : }
    1289             : 
    1290           0 : int16_t LR11x0::setLrFhssConfig(uint8_t bw, uint8_t cr, uint8_t hdrCount, uint16_t hopSeed) {
    1291             :   // check active modem
    1292           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1293           0 :   int16_t state = getPacketType(&type);
    1294           0 :   RADIOLIB_ASSERT(state);
    1295           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_LR_FHSS) {
    1296           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
    1297             :   }
    1298             : 
    1299             :   // check and cache all parameters
    1300           0 :   RADIOLIB_CHECK_RANGE((int8_t)cr, (int8_t)RADIOLIB_LRXXXX_LR_FHSS_CR_5_6, (int8_t)RADIOLIB_LRXXXX_LR_FHSS_CR_1_3, RADIOLIB_ERR_INVALID_CODING_RATE);
    1301           0 :   this->lrFhssCr = cr;
    1302           0 :   RADIOLIB_CHECK_RANGE((int8_t)bw, (int8_t)RADIOLIB_LRXXXX_LR_FHSS_BW_39_06, (int8_t)RADIOLIB_LRXXXX_LR_FHSS_BW_1574_2, RADIOLIB_ERR_INVALID_BANDWIDTH);
    1303           0 :   this->lrFhssBw = bw;
    1304           0 :   RADIOLIB_CHECK_RANGE(hdrCount, 1, 4, RADIOLIB_ERR_INVALID_BIT_RANGE);
    1305           0 :   this->lrFhssHdrCount = hdrCount;
    1306           0 :   RADIOLIB_CHECK_RANGE((int16_t)hopSeed, (int16_t)0x000, (int16_t)0x1FF, RADIOLIB_ERR_INVALID_DATA_SHAPING);
    1307           0 :   this->lrFhssHopSeq = hopSeed;
    1308           0 :   return(RADIOLIB_ERR_NONE);
    1309             : }
    1310             : 
    1311           0 : int16_t LR11x0::getVersionInfo(LR11x0VersionInfo_t* info) {
    1312           0 :   RADIOLIB_ASSERT_PTR(info);
    1313             : 
    1314           0 :   int16_t state = this->getVersion(&info->hardware, &info->device, &info->fwMajor, &info->fwMinor);
    1315           0 :   RADIOLIB_ASSERT(state);
    1316             :   
    1317             :   // LR1121 does not have GNSS and WiFi scanning
    1318           0 :   if(this->chipType == RADIOLIB_LR11X0_DEVICE_LR1121) {
    1319           0 :     info->fwMajorWiFi = 0;
    1320           0 :     info->fwMinorWiFi = 0;
    1321           0 :     info->fwGNSS = 0;
    1322           0 :     info->almanacGNSS = 0;
    1323           0 :     return(RADIOLIB_ERR_NONE);
    1324             :   }
    1325             : 
    1326           0 :   state = this->wifiReadVersion(&info->fwMajorWiFi, &info->fwMinorWiFi);
    1327           0 :   RADIOLIB_ASSERT(state);
    1328           0 :   return(this->gnssReadVersion(&info->fwGNSS, &info->almanacGNSS));
    1329             : }
    1330             : 
    1331           0 : int16_t LR11x0::updateFirmware(const uint32_t* image, size_t size, bool nonvolatile) {
    1332           0 :   RADIOLIB_ASSERT_PTR(image);
    1333             : 
    1334             :   // put the device to bootloader mode
    1335           0 :   int16_t state = this->reboot(true);
    1336           0 :   RADIOLIB_ASSERT(state);
    1337           0 :   this->mod->hal->delay(500);
    1338             : 
    1339             :   // check we're in bootloader
    1340           0 :   uint8_t device = 0xFF;
    1341           0 :   state = this->getVersion(NULL, &device, NULL, NULL);
    1342           0 :   RADIOLIB_ASSERT(state);
    1343           0 :   if(device != RADIOLIB_LR11X0_DEVICE_BOOT) {
    1344             :     RADIOLIB_DEBUG_BASIC_PRINTLN("Failed to put device to bootloader mode, %02x != %02x", (unsigned int)device, (unsigned int)RADIOLIB_LR11X0_DEVICE_BOOT);
    1345           0 :     return(RADIOLIB_ERR_CHIP_NOT_FOUND);
    1346             :   }
    1347             : 
    1348             :   // erase the image
    1349           0 :   state = this->bootEraseFlash();
    1350           0 :   RADIOLIB_ASSERT(state);
    1351             : 
    1352             :   // wait for BUSY to go low
    1353           0 :   RadioLibTime_t start = this->mod->hal->millis();
    1354           0 :   while(this->mod->hal->digitalRead(this->mod->getGpio())) {
    1355           0 :     this->mod->hal->yield();
    1356           0 :     if(this->mod->hal->millis() - start >= 3000) {
    1357             :       RADIOLIB_DEBUG_BASIC_PRINTLN("BUSY pin timeout after erase!");
    1358           0 :       return(RADIOLIB_ERR_SPI_CMD_TIMEOUT);
    1359             :     }
    1360             :   }
    1361             : 
    1362             :   // upload the new image
    1363           0 :   const size_t maxLen = 64;
    1364           0 :   size_t rem = size % maxLen;
    1365           0 :   size_t numWrites = (rem == 0) ? (size / maxLen) : ((size / maxLen) + 1);
    1366             :   RADIOLIB_DEBUG_BASIC_PRINTLN("Writing image in %lu chunks, last chunk size is %lu words", (unsigned long)numWrites, (unsigned long)rem);
    1367           0 :   for(size_t i = 0; i < numWrites; i ++) {
    1368           0 :     uint32_t offset = i * maxLen;
    1369           0 :     uint32_t len = (i == (numWrites - 1)) ? rem : maxLen;
    1370             :     RADIOLIB_DEBUG_BASIC_PRINTLN("Writing chunk %d at offset %08lx (%u words)", (int)i, (unsigned long)offset, (unsigned int)len);
    1371           0 :     this->bootWriteFlashEncrypted(offset*sizeof(uint32_t), const_cast<uint32_t*>(&image[offset]), len, nonvolatile);
    1372             :   }
    1373             : 
    1374             :   // kick the device from bootloader
    1375           0 :   state = this->reset();
    1376           0 :   RADIOLIB_ASSERT(state);
    1377             : 
    1378             :   // verify we are no longer in bootloader
    1379           0 :   state = this->getVersion(NULL, &device, NULL, NULL);
    1380           0 :   RADIOLIB_ASSERT(state);
    1381           0 :   if(device == RADIOLIB_LR11X0_DEVICE_BOOT) {
    1382             :     RADIOLIB_DEBUG_BASIC_PRINTLN("Failed to kick device from bootloader mode, %02x == %02x", (unsigned int)device, (unsigned int)RADIOLIB_LR11X0_DEVICE_BOOT);
    1383           0 :     return(RADIOLIB_ERR_CHIP_NOT_FOUND);
    1384             :   }
    1385             : 
    1386           0 :   return(state);
    1387             : }
    1388             : 
    1389          12 : int16_t LR11x0::getModem(ModemType_t* modem) {
    1390          12 :   RADIOLIB_ASSERT_PTR(modem);
    1391             : 
    1392          12 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1393          12 :   int16_t state = getPacketType(&type);
    1394          12 :   RADIOLIB_ASSERT(state);
    1395             : 
    1396           0 :   switch(type) {
    1397           0 :     case(RADIOLIB_LR11X0_PACKET_TYPE_LORA):
    1398           0 :       *modem = ModemType_t::RADIOLIB_MODEM_LORA;
    1399           0 :       return(RADIOLIB_ERR_NONE);
    1400           0 :     case(RADIOLIB_LR11X0_PACKET_TYPE_GFSK):
    1401           0 :       *modem = ModemType_t::RADIOLIB_MODEM_FSK;
    1402           0 :       return(RADIOLIB_ERR_NONE);
    1403           0 :     case(RADIOLIB_LR11X0_PACKET_TYPE_LR_FHSS):
    1404           0 :       *modem = ModemType_t::RADIOLIB_MODEM_LRFHSS;
    1405           0 :       return(RADIOLIB_ERR_NONE);
    1406             :   }
    1407             :   
    1408           0 :   return(RADIOLIB_ERR_WRONG_MODEM);
    1409             : }
    1410             : 
    1411          12 : int16_t LR11x0::stageMode(RadioModeType_t mode, RadioModeConfig_t* cfg) {
    1412             :   int16_t state;
    1413             : 
    1414          12 :   switch(mode) {
    1415           9 :     case(RADIOLIB_RADIO_MODE_RX): {
    1416             :       // check active modem
    1417           9 :       uint8_t modem = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1418           9 :       state = getPacketType(&modem);
    1419           9 :       RADIOLIB_ASSERT(state);
    1420           0 :       if((modem != RADIOLIB_LR11X0_PACKET_TYPE_LORA) && 
    1421           0 :         (modem != RADIOLIB_LR11X0_PACKET_TYPE_GFSK)) {
    1422           0 :         return(RADIOLIB_ERR_WRONG_MODEM);
    1423             :       }
    1424             : 
    1425             :       // set DIO mapping
    1426           0 :       if(cfg->receive.timeout != RADIOLIB_LR11X0_RX_TIMEOUT_INF) {
    1427           0 :         cfg->receive.irqMask |= (1UL << RADIOLIB_IRQ_TIMEOUT);
    1428             :       }
    1429           0 :       state = setDioIrqParams(getIrqMapped(cfg->receive.irqFlags & cfg->receive.irqMask));
    1430           0 :       RADIOLIB_ASSERT(state);
    1431             : 
    1432             :       // clear interrupt flags
    1433           0 :       state = clearIrqState(RADIOLIB_LR11X0_IRQ_ALL);
    1434           0 :       RADIOLIB_ASSERT(state);
    1435             : 
    1436             :       // set implicit mode and expected len if applicable
    1437           0 :       if((this->headerType == RADIOLIB_LRXXXX_LORA_HEADER_IMPLICIT) && (modem == RADIOLIB_LR11X0_PACKET_TYPE_LORA)) {
    1438           0 :         state = setPacketParamsLoRa(this->preambleLengthLoRa, this->headerType, this->implicitLen, this->crcTypeLoRa, this->invertIQEnabled);
    1439           0 :         RADIOLIB_ASSERT(state);
    1440             :       }
    1441             : 
    1442             :       // if max(uint32_t) is used, revert to RxContinuous
    1443           0 :       if(cfg->receive.timeout == 0xFFFFFFFF) {
    1444           0 :         cfg->receive.timeout = 0xFFFFFF;
    1445             :       }
    1446           0 :       this->rxTimeout = cfg->receive.timeout;
    1447           0 :     } break;
    1448             :   
    1449           3 :     case(RADIOLIB_RADIO_MODE_TX): {
    1450             :       // check packet length
    1451           3 :       if(cfg->transmit.len > RADIOLIB_LR11X0_MAX_PACKET_LENGTH) {
    1452           3 :         return(RADIOLIB_ERR_PACKET_TOO_LONG);
    1453             :       }
    1454             : 
    1455             :       // maximum packet length is decreased by 1 when address filtering is active
    1456           3 :       if((this->addrComp != RADIOLIB_LR11X0_GFSK_ADDR_FILTER_DISABLED) && (cfg->transmit.len > RADIOLIB_LR11X0_MAX_PACKET_LENGTH - 1)) {
    1457           0 :         return(RADIOLIB_ERR_PACKET_TOO_LONG);
    1458             :       }
    1459             : 
    1460             :       // set packet Length
    1461           3 :       uint8_t modem = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1462           3 :       state = getPacketType(&modem);
    1463           3 :       RADIOLIB_ASSERT(state);
    1464           0 :       if(modem == RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
    1465           0 :         state = setPacketParamsLoRa(this->preambleLengthLoRa, this->headerType, cfg->transmit.len, this->crcTypeLoRa, this->invertIQEnabled);
    1466             :       
    1467           0 :       } else if(modem == RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
    1468           0 :         state = setPacketParamsGFSK(this->preambleLengthGFSK, this->preambleDetLength, this->syncWordLength, this->addrComp, this->packetType, cfg->transmit.len, this->crcTypeGFSK, this->whitening);
    1469             :       
    1470           0 :       } else if(modem != RADIOLIB_LR11X0_PACKET_TYPE_LR_FHSS) {
    1471           0 :         return(RADIOLIB_ERR_UNKNOWN);
    1472             :       
    1473             :       }
    1474           0 :       RADIOLIB_ASSERT(state);
    1475             : 
    1476             :       // set DIO mapping
    1477           0 :       state = setDioIrqParams(RADIOLIB_LR11X0_IRQ_TX_DONE | RADIOLIB_LR11X0_IRQ_TIMEOUT);
    1478           0 :       RADIOLIB_ASSERT(state);
    1479             : 
    1480           0 :       if(modem == RADIOLIB_LR11X0_PACKET_TYPE_LR_FHSS) {
    1481             :         // in LR-FHSS mode, the packet is built by the device
    1482             :         // TODO add configurable device offset
    1483           0 :         state = LRxxxx::lrFhssBuildFrame(RADIOLIB_LR11X0_CMD_LR_FHSS_BUILD_FRAME, this->lrFhssHdrCount, this->lrFhssCr, this->lrFhssGrid, true, this->lrFhssBw, this->lrFhssHopSeq, 0, cfg->transmit.data, cfg->transmit.len);
    1484           0 :         RADIOLIB_ASSERT(state);
    1485             : 
    1486             :       } else {
    1487             :         // write packet to buffer
    1488           0 :         state = writeBuffer8(cfg->transmit.data, cfg->transmit.len);
    1489           0 :         RADIOLIB_ASSERT(state);
    1490             : 
    1491             :       }
    1492             : 
    1493             :       // clear interrupt flags
    1494           0 :       state = clearIrqState(RADIOLIB_LR11X0_IRQ_ALL);
    1495           0 :       RADIOLIB_ASSERT(state);
    1496           0 :     } break;
    1497             :     
    1498           0 :     default:
    1499           0 :       return(RADIOLIB_ERR_UNSUPPORTED);
    1500             :   }
    1501             : 
    1502           0 :   this->stagedMode = mode;
    1503           0 :   return(state);
    1504             : }
    1505             : 
    1506           3 : int16_t LR11x0::launchMode() {
    1507             :   int16_t state;
    1508           3 :   switch(this->stagedMode) {
    1509           3 :     case(RADIOLIB_RADIO_MODE_RX): {
    1510           3 :       this->mod->setRfSwitchState(Module::MODE_RX);
    1511           3 :       state = setRx(this->rxTimeout);
    1512           3 :     } break;
    1513             :   
    1514           0 :     case(RADIOLIB_RADIO_MODE_TX): {
    1515           0 :       this->mod->setRfSwitchState(Module::MODE_TX);
    1516           0 :       state = setTx(RADIOLIB_LR11X0_TX_TIMEOUT_NONE);
    1517           0 :       RADIOLIB_ASSERT(state);
    1518             : 
    1519             :       // wait for BUSY to go low (= PA ramp up done)
    1520           0 :       while(this->mod->hal->digitalRead(this->mod->getGpio())) {
    1521           0 :         this->mod->hal->yield();
    1522             :       }
    1523           0 :     } break;
    1524             :     
    1525           0 :     default:
    1526           0 :       return(RADIOLIB_ERR_UNSUPPORTED);
    1527             :   }
    1528             : 
    1529           3 :   this->stagedMode = RADIOLIB_RADIO_MODE_NONE;
    1530           3 :   return(state);
    1531             : }
    1532             : 
    1533           0 : int16_t LR11x0::workaroundGFSK() {
    1534             :   // first, check we are using GFSK modem
    1535           0 :   uint8_t modem = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1536           0 :   int16_t state = getPacketType(&modem);
    1537           0 :   RADIOLIB_ASSERT(state);
    1538           0 :   if(modem != RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
    1539             :     // not in GFSK, nothing to do here
    1540           0 :     return(RADIOLIB_ERR_NONE);
    1541             :   }
    1542             : 
    1543             :   // this seems to always be the first step (even when resetting)
    1544           0 :   state = this->writeRegMemMask32(RADIOLIB_LR11X0_REG_GFSK_FIX1, 0x30, 0x10);
    1545           0 :   RADIOLIB_ASSERT(state);
    1546             : 
    1547             :   // these are the default values that will be applied if nothing matches
    1548           0 :   uint32_t valFix2 = 0x01;
    1549           0 :   uint32_t valFix3 = 0x0A01;
    1550             : 
    1551             :   // next, decide what to change based on modulation properties
    1552           0 :   if((this->bitRate == 1200) && (this->frequencyDev == 5000) && (this->rxBandwidth == RADIOLIB_LR11X0_GFSK_RX_BW_19_5)) {
    1553             :     // workaround for 1.2 kbps
    1554           0 :     valFix2 = 0x04;
    1555             : 
    1556           0 :   } else if((this->bitRate == 600) && (this->frequencyDev == 800) && (this->rxBandwidth == RADIOLIB_LR11X0_GFSK_RX_BW_4_8))  {
    1557             :     // value to write depends on the frequency
    1558           0 :     valFix3 = (this->freqMHz >= 1000.0f) ? 0x1100 : 0x0600;
    1559             :   
    1560             :   }
    1561             : 
    1562             :   // update the registers
    1563           0 :   state = this->writeRegMemMask32(RADIOLIB_LR11X0_REG_GFSK_FIX2, 0x05, valFix2);
    1564           0 :   RADIOLIB_ASSERT(state);
    1565           0 :   return(this->writeRegMemMask32(RADIOLIB_LR11X0_REG_GFSK_FIX3, 0x01FF03, valFix3));
    1566             : }
    1567             : 
    1568           0 : int16_t LR11x0::modSetup(float tcxoVoltage, uint8_t modem) {
    1569           0 :   this->mod->init();
    1570           0 :   this->mod->hal->pinMode(this->mod->getIrq(), this->mod->hal->GpioModeInput);
    1571           0 :   this->mod->hal->pinMode(this->mod->getGpio(), this->mod->hal->GpioModeInput);
    1572           0 :   this->mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ] = RADIOLIB_LR11X0_CMD_READ_REG_MEM;
    1573           0 :   this->mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE] = RADIOLIB_LR11X0_CMD_WRITE_REG_MEM;
    1574           0 :   this->mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_NOP] = RADIOLIB_LR11X0_CMD_NOP;
    1575           0 :   this->mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_STATUS] = RADIOLIB_LR11X0_CMD_GET_STATUS;
    1576           0 :   this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR] = Module::BITS_32;
    1577           0 :   this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_STATUS] = Module::BITS_8;
    1578           0 :   this->gnss = false;
    1579             : 
    1580             :   // try to find the LR11x0 chip - this will also reset the module at least once
    1581           0 :   if(!LR11x0::findChip(this->chipType)) {
    1582             :     RADIOLIB_DEBUG_BASIC_PRINTLN("No LR11x0 found!");
    1583           0 :     this->mod->term();
    1584           0 :     return(RADIOLIB_ERR_CHIP_NOT_FOUND);
    1585             :   }
    1586             :   RADIOLIB_DEBUG_BASIC_PRINTLN("M\tLR11x0");
    1587             : 
    1588             :   // set mode to standby
    1589           0 :   int16_t state = standby();
    1590           0 :   RADIOLIB_ASSERT(state);
    1591             : 
    1592             :   // set TCXO control, if requested
    1593           0 :   if(!this->XTAL && tcxoVoltage > 0.0f) {
    1594           0 :     state = setTCXO(tcxoVoltage);
    1595           0 :     RADIOLIB_ASSERT(state);
    1596             :   }
    1597             : 
    1598             :   // configure settings not accessible by API
    1599           0 :   return(config(modem));
    1600             : }
    1601             : 
    1602           0 : bool LR11x0::findChip(uint8_t ver) {
    1603           0 :   uint8_t i = 0;
    1604           0 :   bool flagFound = false;
    1605           0 :   while((i < 10) && !flagFound) {
    1606             :     // reset the module
    1607           0 :     reset();
    1608             : 
    1609             :     // read the version
    1610             :     LR11x0VersionInfo_t info;
    1611           0 :     int16_t state = getVersionInfo(&info);
    1612           0 :     RADIOLIB_ASSERT(state);
    1613             : 
    1614           0 :     if((info.device == ver) || (info.device == RADIOLIB_LR11X0_DEVICE_BOOT)) {
    1615             :       RADIOLIB_DEBUG_BASIC_PRINTLN("Found LR11x0: RADIOLIB_LR11X0_CMD_GET_VERSION = 0x%02x", info.device);
    1616             :       RADIOLIB_DEBUG_BASIC_PRINTLN("Base FW version: %d.%d", (int)info.fwMajor, (int)info.fwMinor);
    1617           0 :       if(this->chipType != RADIOLIB_LR11X0_DEVICE_LR1121) {
    1618             :         RADIOLIB_DEBUG_BASIC_PRINTLN("WiFi FW version: %d.%d", (int)info.fwMajorWiFi, (int)info.fwMinorWiFi);
    1619             :         RADIOLIB_DEBUG_BASIC_PRINTLN("GNSS FW version: %d.%d", (int)info.fwGNSS, (int)info.almanacGNSS);
    1620             :       }
    1621           0 :       if(info.device == RADIOLIB_LR11X0_DEVICE_BOOT) {
    1622             :         RADIOLIB_DEBUG_BASIC_PRINTLN("Warning: device is in bootloader mode! Only FW update is possible now.");
    1623             :       }
    1624           0 :       flagFound = true;
    1625             :     } else {
    1626             :       RADIOLIB_DEBUG_BASIC_PRINTLN("LR11x0 not found! (%d of 10 tries) RADIOLIB_LR11X0_CMD_GET_VERSION = 0x%02x", i + 1, info.device);
    1627             :       RADIOLIB_DEBUG_BASIC_PRINTLN("Expected: 0x%02x", ver);
    1628           0 :       this->mod->hal->delay(10);
    1629           0 :       i++;
    1630             :     }
    1631             :   }
    1632             :   
    1633             : 
    1634           0 :   return(flagFound);
    1635             : }
    1636             : 
    1637           0 : int16_t LR11x0::config(uint8_t modem) {
    1638           0 :   int16_t state = RADIOLIB_ERR_UNKNOWN;
    1639             : 
    1640             :   // set Rx/Tx fallback mode to STDBY_RC
    1641           0 :   state = this->setRxTxFallbackMode(RADIOLIB_LR11X0_FALLBACK_MODE_STBY_RC);
    1642           0 :   RADIOLIB_ASSERT(state);
    1643             : 
    1644             :   // clear IRQ
    1645           0 :   state = this->clearIrqState(RADIOLIB_LR11X0_IRQ_ALL);
    1646           0 :   state |= this->setDioIrqParams(RADIOLIB_LR11X0_IRQ_NONE);
    1647           0 :   RADIOLIB_ASSERT(state);
    1648             : 
    1649             :   // calibrate all blocks
    1650           0 :   state = this->calibrate(RADIOLIB_LR11X0_CALIBRATE_ALL);
    1651             : 
    1652             :   // wait for calibration completion
    1653           0 :   this->mod->hal->delay(5);
    1654           0 :   while(this->mod->hal->digitalRead(this->mod->getGpio())) {
    1655           0 :     this->mod->hal->yield();
    1656             :   }
    1657             :   
    1658             :   // if something failed, show the device errors
    1659             :   #if RADIOLIB_DEBUG_BASIC
    1660             :   if(state != RADIOLIB_ERR_NONE) {
    1661             :     // unless mode is forced to standby, device errors will be 0
    1662             :     standby();
    1663             :     uint16_t errors = 0;
    1664             :     getErrors(&errors);
    1665             :     RADIOLIB_DEBUG_BASIC_PRINTLN("Calibration failed, device errors: 0x%X", errors);
    1666             :   }
    1667             :   #else
    1668           0 :   RADIOLIB_ASSERT(state);
    1669             :   #endif
    1670             : 
    1671             :   // set modem
    1672           0 :   state = this->setPacketType(modem);
    1673           0 :   return(state);
    1674             : }
    1675             : 
    1676           0 : int16_t LR11x0::setPacketMode(uint8_t mode, uint8_t len) {
    1677             :   // check active modem
    1678           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1679           0 :   int16_t state = getPacketType(&type);
    1680           0 :   RADIOLIB_ASSERT(state);
    1681           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_GFSK) {
    1682           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
    1683             :   }
    1684             : 
    1685             :   // set requested packet mode
    1686           0 :   state = setPacketParamsGFSK(this->preambleLengthGFSK, this->preambleDetLength, this->syncWordLength, this->addrComp, mode, len, this->crcTypeGFSK, this->whitening);
    1687           0 :   RADIOLIB_ASSERT(state);
    1688             : 
    1689             :   // update cached value
    1690           0 :   this->packetType = mode;
    1691           0 :   return(state);
    1692             : }
    1693             : 
    1694           0 : int16_t LR11x0::startCad(uint8_t symbolNum, uint8_t detPeak, uint8_t detMin, uint8_t exitMode, RadioLibTime_t timeout) {
    1695             :   // check active modem
    1696           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1697           0 :   int16_t state = getPacketType(&type);
    1698           0 :   RADIOLIB_ASSERT(state);
    1699           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
    1700           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
    1701             :   }
    1702             : 
    1703             :   // select CAD parameters
    1704             :   // TODO the magic numbers are based on Semtech examples, this is probably suboptimal
    1705           0 :   uint8_t num = symbolNum;
    1706           0 :   if(num == RADIOLIB_LR11X0_CAD_PARAM_DEFAULT) {
    1707           0 :     num = 2;
    1708             :   }
    1709             :   
    1710           0 :   const uint8_t detPeakValues[8] = { 48, 48, 50, 55, 55, 59, 61, 65 };
    1711           0 :   uint8_t peak = detPeak;
    1712           0 :   if(peak == RADIOLIB_LR11X0_CAD_PARAM_DEFAULT) {
    1713           0 :     peak = detPeakValues[this->spreadingFactor - 5];
    1714             :   }
    1715             : 
    1716           0 :   uint8_t min = detMin;
    1717           0 :   if(min == RADIOLIB_LR11X0_CAD_PARAM_DEFAULT) {
    1718           0 :     min = 10;
    1719             :   }
    1720             : 
    1721           0 :   uint8_t mode = exitMode; 
    1722           0 :   if(mode == RADIOLIB_LR11X0_CAD_PARAM_DEFAULT) {
    1723           0 :     mode = RADIOLIB_LR11X0_CAD_EXIT_MODE_STBY_RC;
    1724             :   }
    1725             : 
    1726           0 :   uint32_t timeout_raw = (float)timeout / 30.52f;
    1727             : 
    1728             :   // set CAD parameters
    1729             :   // TODO add configurable exit mode and timeout
    1730           0 :   state = setCadParams(num, peak, min, mode, timeout_raw);
    1731           0 :   RADIOLIB_ASSERT(state);
    1732             : 
    1733             :   // start CAD
    1734           0 :   return(setCad());
    1735             : }
    1736             : 
    1737           0 : int16_t LR11x0::setHeaderType(uint8_t hdrType, size_t len) {
    1738             :   // check active modem
    1739           0 :   uint8_t type = RADIOLIB_LR11X0_PACKET_TYPE_NONE;
    1740           0 :   int16_t state = getPacketType(&type);
    1741           0 :   RADIOLIB_ASSERT(state);
    1742           0 :   if(type != RADIOLIB_LR11X0_PACKET_TYPE_LORA) {
    1743           0 :     return(RADIOLIB_ERR_WRONG_MODEM);
    1744             :   }
    1745             : 
    1746             :   // set requested packet mode
    1747           0 :   state = setPacketParamsLoRa(this->preambleLengthLoRa, hdrType, len, this->crcTypeLoRa, this->invertIQEnabled);
    1748           0 :   RADIOLIB_ASSERT(state);
    1749             : 
    1750             :   // update cached value
    1751           0 :   this->headerType = hdrType;
    1752           0 :   this->implicitLen = len;
    1753             : 
    1754           0 :   return(state);
    1755             : }
    1756             : 
    1757           0 : Module* LR11x0::getMod() {
    1758           0 :   return(this->mod);
    1759             : }
    1760             : 
    1761             : #endif

Generated by: LCOV version 1.14