LCOV - code coverage report
Current view: top level - src - Module.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 168 228 73.7 %
Date: 2026-11-08 19:43:08 Functions: 19 24 79.2 %

          Line data    Source code
       1             : #include "Module.h"
       2             : 
       3             : // the following is probably only needed on non-Arduino builds
       4             : #include <stdio.h>
       5             : #include <string.h>
       6             : 
       7             : #if defined(RADIOLIB_BUILD_ARDUINO)
       8             : #include "hal/Arduino/ArduinoHal.h"
       9             : 
      10             : Module::Module(uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio) : csPin(cs), irqPin(irq), rstPin(rst), gpioPin(gpio) {
      11             :   this->hal = new ArduinoHal();
      12             : }
      13             : 
      14             : Module::Module(uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio, SPIClass& spi, SPISettings spiSettings) : csPin(cs), irqPin(irq), rstPin(rst), gpioPin(gpio) {
      15             :   this->hal = new ArduinoHal(spi, spiSettings);
      16             : }
      17             : #endif
      18             : 
      19          63 : Module::Module(RadioLibHal *hal, uint32_t cs, uint32_t irq, uint32_t rst, uint32_t gpio) : csPin(cs), irqPin(irq), rstPin(rst), gpioPin(gpio) {
      20          63 :   this->hal = hal;
      21          63 : }
      22             : 
      23           0 : Module::Module(const Module& mod) {
      24           0 :   *this = mod;
      25           0 : }
      26             : 
      27           1 : Module& Module::operator=(const Module& mod) {
      28           1 :   this->hal = mod.hal;
      29           1 :   memcpy(&this->spiConfig, reinterpret_cast<void*>(&(const_cast<Module&>(mod)).spiConfig), sizeof(SPIConfig_t));
      30           1 :   this->csPin = mod.csPin;
      31           1 :   this->irqPin = mod.irqPin;
      32           1 :   this->rstPin = mod.rstPin;
      33           1 :   this->gpioPin = mod.gpioPin;
      34             : 
      35           1 :   memcpy(this->rfSwitchPins, mod.rfSwitchPins, Module::RFSWITCH_MAX_PINS*sizeof(this->rfSwitchPins[0]));
      36           1 :   this->rfSwitchTable = mod.rfSwitchTable;
      37             : 
      38             :   #if RADIOLIB_INTERRUPT_TIMING
      39             :     this->TimerSetupCb = mod.TimerSetupCb;
      40             :     this->TimerFlag = mod.TimerFlag;
      41             :     this->prevTimingLen = mod.prevTimingLen;
      42             :   #endif
      43             :   
      44           1 :   return(*this);
      45             : }
      46             : 
      47             : static volatile const char rlb_info[] = RADIOLIB_INFO;
      48           7 : int16_t Module::init() {
      49           7 :   int16_t state = this->hal->init();
      50           7 :   RADIOLIB_ASSERT(state);
      51             : 
      52           7 :   this->hal->pinMode(csPin, this->hal->GpioModeOutput);
      53           7 :   this->hal->digitalWrite(csPin, this->hal->GpioLevelHigh);
      54             :   RADIOLIB_DEBUG_BASIC_PRINTLN(RADIOLIB_INFO);
      55           7 :   RADIOLIB_VALUE_USED(rlb_info);
      56           7 :   return(state);
      57             : }
      58             : 
      59           7 : int16_t Module::term() {
      60           7 :   return(this->hal->term());
      61             : }
      62             : 
      63         426 : int16_t Module::SPIgetRegValue(uint32_t reg, uint8_t msb, uint8_t lsb) {
      64         426 :   if((msb > 7) || (lsb > 7) || (lsb > msb)) {
      65           6 :     return(RADIOLIB_ERR_INVALID_BIT_RANGE);
      66             :   }
      67             : 
      68         420 :   uint8_t rawValue = SPIreadRegister(reg);
      69         420 :   uint8_t maskedValue = rawValue & ((0b11111111 << lsb) & (0b11111111 >> (7 - msb)));
      70         420 :   return(maskedValue);
      71             : }
      72             : 
      73         232 : int16_t Module::SPIsetRegValue(uint32_t reg, uint8_t value, uint8_t msb, uint8_t lsb, uint8_t checkInterval, uint8_t checkMask, bool force) {
      74         232 :   if((msb > 7) || (lsb > 7) || (lsb > msb)) {
      75           6 :     return(RADIOLIB_ERR_INVALID_BIT_RANGE);
      76             :   }
      77             : 
      78             :   // read the current value
      79         226 :   uint8_t currentValue = SPIreadRegister(reg);
      80         226 :   uint8_t mask = ~((0b11111111 << (msb + 1)) | (0b11111111 >> (8 - lsb)));
      81             : 
      82             :   // check if we actually need to update the register
      83         226 :   if((currentValue & mask) == (value & mask) && !force) {
      84          24 :     return(RADIOLIB_ERR_NONE);
      85             :   }
      86             : 
      87             :   // update the register
      88         202 :   uint8_t newValue = (currentValue & ~mask) | (value & mask);
      89         202 :   SPIwriteRegister(reg, newValue);
      90             : 
      91             :   #if RADIOLIB_SPI_PARANOID
      92             :     // check register value each millisecond until check interval is reached
      93             :     // some registers need a bit of time to process the change (e.g. SX127X_REG_OP_MODE)
      94         202 :     RadioLibTime_t start = this->hal->micros();
      95             :     #if RADIOLIB_DEBUG_SPI
      96             :     uint8_t readValue = 0x00;
      97             :     #endif
      98         526 :     while(this->hal->micros() - start < ((RadioLibTime_t)checkInterval * 1000UL)) {
      99         326 :       uint8_t val = SPIreadRegister(reg);
     100         326 :       if((val & checkMask) == (newValue & checkMask)) {
     101             :         // check passed, we can stop the loop
     102           2 :         return(RADIOLIB_ERR_NONE);
     103             :       }
     104             :       #if RADIOLIB_DEBUG_SPI
     105             :       readValue = val;
     106             :       #endif
     107             :     }
     108             : 
     109             :     // check failed, print debug info
     110             :     RADIOLIB_DEBUG_SPI_PRINTLN();
     111             :     RADIOLIB_DEBUG_SPI_PRINTLN("address:\t0x%X", reg);
     112             :     RADIOLIB_DEBUG_SPI_PRINTLN("bits:\t\t%d %d", msb, lsb);
     113             :     RADIOLIB_DEBUG_SPI_PRINTLN("value:\t\t0x%X", value);
     114             :     RADIOLIB_DEBUG_SPI_PRINTLN("current:\t0x%X", currentValue);
     115             :     RADIOLIB_DEBUG_SPI_PRINTLN("mask:\t\t0x%X", mask);
     116             :     RADIOLIB_DEBUG_SPI_PRINTLN("new:\t\t0x%X", newValue);
     117             :     RADIOLIB_DEBUG_SPI_PRINTLN("read:\t\t0x%X", readValue);
     118             : 
     119         200 :     return(RADIOLIB_ERR_SPI_WRITE_FAILED);
     120             :   #else
     121             :     return(RADIOLIB_ERR_NONE);
     122             :   #endif
     123             : }
     124             : 
     125          33 : void Module::SPIreadRegisterBurst(uint32_t reg, size_t numBytes, uint8_t* inBytes) {
     126          33 :   if(!this->spiConfig.stream) {
     127           0 :     SPItransfer(this->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ], reg, NULL, inBytes, numBytes);
     128             :   } else {
     129             :     uint8_t cmd[6];
     130          33 :     uint8_t* cmdPtr = cmd;
     131          99 :     for(int8_t i = (int8_t)this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8 - 1; i >= 0; i--) {
     132          66 :       *(cmdPtr++) = (this->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ] >> 8*i) & 0xFF;
     133             :     }
     134          66 :     for(int8_t i = (int8_t)((this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]/8) - 1); i >= 0; i--) {
     135          33 :       *(cmdPtr++) = (reg >> 8*i) & 0xFF;
     136             :     }
     137          33 :     SPItransferStream(cmd, this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8 + this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]/8, false, NULL, inBytes, numBytes, true);
     138             :   }
     139          33 : }
     140             : 
     141        2568 : uint8_t Module::SPIreadRegister(uint32_t reg) {
     142        2568 :   uint8_t resp = 0;
     143        2568 :   if(!spiConfig.stream) {
     144         105 :     SPItransfer(this->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ], reg, NULL, &resp, 1);
     145             :   } else {
     146             :     uint8_t cmd[6];
     147        2463 :     uint8_t* cmdPtr = cmd;
     148        7346 :     for(int8_t i = (int8_t)this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8 - 1; i >= 0; i--) {
     149        4883 :       *(cmdPtr++) = (this->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ] >> 8*i) & 0xFF;
     150             :     }
     151        4969 :     for(int8_t i = (int8_t)((this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]/8) - 1); i >= 0; i--) {
     152        2506 :       *(cmdPtr++) = (reg >> 8*i) & 0xFF;
     153             :     }
     154        2463 :     SPItransferStream(cmd, this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8 + this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]/8, false, NULL, &resp, 1, true);
     155             :   }
     156        2568 :   return(resp);
     157             : }
     158             : 
     159           6 : void Module::SPIwriteRegisterBurst(uint32_t reg, const uint8_t* data, size_t numBytes) {
     160           6 :   if(!spiConfig.stream) {
     161           0 :     SPItransfer(spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE], reg, data, NULL, numBytes);
     162             :   } else {
     163             :     uint8_t cmd[6];
     164           6 :     uint8_t* cmdPtr = cmd;
     165          18 :     for(int8_t i = (int8_t)this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8 - 1; i >= 0; i--) {
     166          12 :       *(cmdPtr++) = (this->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE] >> 8*i) & 0xFF;
     167             :     }
     168          12 :     for(int8_t i = (int8_t)((this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]/8) - 1); i >= 0; i--) {
     169           6 :       *(cmdPtr++) = (reg >> 8*i) & 0xFF;
     170             :     }
     171           6 :     SPItransferStream(cmd, this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8 + this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]/8, true, data, NULL, numBytes, true);
     172             :   }
     173           6 : }
     174             : 
     175         226 : void Module::SPIwriteRegister(uint32_t reg, uint8_t data) {
     176         226 :   if(!spiConfig.stream) {
     177           5 :     SPItransfer(spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE], reg, &data, NULL, 1);
     178             :   } else {
     179             :     uint8_t cmd[6];
     180         221 :     uint8_t* cmdPtr = cmd;
     181         658 :     for(int8_t i = (int8_t)this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8 - 1; i >= 0; i--) {
     182         437 :       *(cmdPtr++) = (this->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE] >> 8*i) & 0xFF;
     183             :     }
     184         447 :     for(int8_t i = (int8_t)((this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]/8) - 1); i >= 0; i--) {
     185         226 :       *(cmdPtr++) = (reg >> 8*i) & 0xFF;
     186             :     }
     187         221 :     SPItransferStream(cmd, this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8 + this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]/8, true, &data, NULL, 1, true);
     188             :   }
     189         226 : }
     190             : 
     191         110 : void Module::SPItransfer(uint16_t cmd, uint32_t reg, const uint8_t* dataOut, uint8_t* dataIn, size_t numBytes) {
     192             :   // prepare the buffers
     193         110 :   size_t buffLen = this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8 + this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]/8 + numBytes;
     194             :   #if RADIOLIB_STATIC_ONLY
     195             :     uint8_t buffOut[RADIOLIB_STATIC_SPI_ARRAY_SIZE];
     196             :     uint8_t buffIn[RADIOLIB_STATIC_SPI_ARRAY_SIZE];
     197             :   #else
     198         110 :     uint8_t* buffOut = new uint8_t[buffLen];
     199         110 :     uint8_t* buffIn = new uint8_t[buffLen];
     200             :   #endif
     201         110 :   uint8_t* buffOutPtr = buffOut;
     202             : 
     203             :   // copy the command
     204             :   // TODO properly handle variable commands and addresses
     205         110 :   if(this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR] <= 8) {
     206         110 :     *(buffOutPtr++) = reg | cmd;
     207             :   } else {
     208           0 :     *(buffOutPtr++) = (reg >> 8) | cmd;
     209           0 :     *(buffOutPtr++) = reg & 0xFF;
     210             :   }
     211             : 
     212             :   // copy the data
     213         110 :   if(cmd == spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE]) {
     214           5 :     memcpy(buffOutPtr, dataOut, numBytes);
     215             :   } else {
     216         105 :     memset(buffOutPtr, this->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_NOP], numBytes);
     217             :   }
     218             : 
     219             :   // do the transfer
     220         110 :   this->hal->spiBeginTransaction();
     221         110 :   this->hal->digitalWrite(this->csPin, this->hal->GpioLevelLow);
     222         110 :   int16_t state = this->hal->spiTransfer(buffOut, buffLen, buffIn);
     223         110 :   this->hal->digitalWrite(this->csPin, this->hal->GpioLevelHigh);
     224         110 :   this->hal->spiEndTransaction();
     225             : 
     226             :   // value is reported via debug only
     227         110 :   if(state != RADIOLIB_ERR_NONE) {
     228             :     RADIOLIB_DEBUG_BASIC_PRINTLN("SPI Transaction failed, code %d\n", state);
     229           0 :     return;
     230             :   }
     231             :   
     232             :   // copy the data
     233         110 :   if(cmd == spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ]) {
     234         105 :     memcpy(dataIn, &buffIn[this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]/8], numBytes);
     235             :   }
     236             : 
     237             :   // print debug information
     238             :   #if RADIOLIB_DEBUG_SPI
     239             :     const uint8_t* debugBuffPtr = NULL;
     240             :     if(cmd == spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE]) {
     241             :       RADIOLIB_DEBUG_SPI_PRINT("W\t%X\t", reg);
     242             :       debugBuffPtr = &buffOut[this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]/8];
     243             :     } else if(cmd == spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ]) {
     244             :       RADIOLIB_DEBUG_SPI_PRINT("R\t%X\t", reg);
     245             :       debugBuffPtr = &buffIn[this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR]/8];
     246             :     }
     247             :     for(size_t n = 0; n < numBytes; n++) {
     248             :       RADIOLIB_DEBUG_SPI_PRINT_NOTAG("%X\t", debugBuffPtr[n]);
     249             :     }
     250             :     RADIOLIB_DEBUG_SPI_PRINTLN_NOTAG("");
     251             :   #endif
     252             : 
     253             :   #if !RADIOLIB_STATIC_ONLY
     254         110 :     delete[] buffOut;
     255         110 :     delete[] buffIn;
     256             :   #endif
     257             : }
     258             : 
     259         168 : int16_t Module::SPIreadStream(uint16_t cmd, uint8_t* data, size_t numBytes, bool waitForGpio, bool verify) {
     260             :   uint8_t cmdBuf[2];
     261         168 :   uint8_t* cmdPtr = cmdBuf;
     262         504 :   for(int8_t i = (int8_t)this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8 - 1; i >= 0; i--) {
     263         336 :     *(cmdPtr++) = (cmd >> 8*i) & 0xFF;
     264             :   }
     265         336 :   return(this->SPIreadStream(cmdBuf, this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8, data, numBytes, waitForGpio, verify));
     266             : }
     267             : 
     268         168 : int16_t Module::SPIreadStream(const uint8_t* cmd, uint8_t cmdLen, uint8_t* data, size_t numBytes, bool waitForGpio, bool verify) {
     269             :   // send the command
     270         168 :   int16_t state = this->SPItransferStream(cmd, cmdLen, false, NULL, data, numBytes, waitForGpio);
     271         168 :   RADIOLIB_ASSERT(state);
     272             : 
     273             :   #if !RADIOLIB_SPI_PARANOID
     274             :   (void)verify;
     275             :   return(RADIOLIB_ERR_NONE);
     276             :   #else
     277             : 
     278             :   // check the status
     279           0 :   if(verify && (this->spiConfig.checkStatusCb != nullptr)) {
     280           0 :     state = this->spiConfig.checkStatusCb(this);
     281             :   }
     282             : 
     283           0 :   return(state);
     284             :   #endif
     285             : }
     286             : 
     287         381 : int16_t Module::SPIwriteStream(uint16_t cmd, const uint8_t* data, size_t numBytes, bool waitForGpio, bool verify) {
     288             :   uint8_t cmdBuf[2];
     289         381 :   uint8_t* cmdPtr = cmdBuf;
     290        1143 :   for(int8_t i = (int8_t)this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8 - 1; i >= 0; i--) {
     291         762 :     *(cmdPtr++) = (cmd >> 8*i) & 0xFF;
     292             :   }
     293         762 :   return(this->SPIwriteStream(cmdBuf, this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8, data, numBytes, waitForGpio, verify));
     294             : }
     295             : 
     296         381 : int16_t Module::SPIwriteStream(const uint8_t* cmd, uint8_t cmdLen, const uint8_t* data, size_t numBytes, bool waitForGpio, bool verify) {
     297             :   // send the command
     298         381 :   int16_t state = this->SPItransferStream(cmd, cmdLen, true, data, NULL, numBytes, waitForGpio);
     299         381 :   RADIOLIB_ASSERT(state);
     300             : 
     301             :   #if !RADIOLIB_SPI_PARANOID
     302             :   (void)verify;
     303             :   return(RADIOLIB_ERR_NONE);
     304             :   #else
     305             : 
     306             :   // check the status
     307           0 :   if(verify && (this->spiConfig.checkStatusCb != nullptr)) {
     308           0 :     state = this->spiConfig.checkStatusCb(this);
     309             :   }
     310             : 
     311           0 :   return(state);
     312             :   #endif
     313             : }
     314             : 
     315          30 : int16_t Module::SPIcheckStream() {
     316          30 :   int16_t state = RADIOLIB_ERR_NONE;
     317             : 
     318             :   #if RADIOLIB_SPI_PARANOID
     319             :   // get the status
     320          30 :   uint8_t spiStatus = 0;
     321             :   uint8_t cmdBuf[2];
     322          30 :   uint8_t* cmdPtr = cmdBuf;
     323          90 :   for(int8_t i = (int8_t)this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8 - 1; i >= 0; i--) {
     324          60 :     *(cmdPtr++) = ( this->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_STATUS] >> 8*i) & 0xFF;
     325             :   }
     326          30 :   state = this->SPItransferStream(cmdBuf, this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD]/8, false, NULL, &spiStatus, 1, true);
     327          30 :   RADIOLIB_ASSERT(state);
     328             : 
     329             :   // translate to RadioLib status code
     330           0 :   if(this->spiConfig.parseStatusCb != nullptr) {
     331           0 :     this->spiConfig.err = this->spiConfig.parseStatusCb(spiStatus);
     332             :   }
     333             :   #endif
     334             : 
     335           0 :   return(state);
     336             : }
     337             : 
     338        3318 : int16_t Module::SPItransferStream(const uint8_t* cmd, uint8_t cmdLen, bool write, const uint8_t* dataOut, uint8_t* dataIn, size_t numBytes, bool waitForGpio) {
     339             :   // prepare the output buffer
     340        3318 :   int16_t state = RADIOLIB_ERR_NONE;
     341        3318 :   size_t buffLen = cmdLen + numBytes;
     342        3318 :   if(!write) {
     343        2710 :     buffLen += (this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_STATUS] / 8);
     344             :   }
     345             :   #if RADIOLIB_STATIC_ONLY
     346             :     uint8_t buffOut[RADIOLIB_STATIC_SPI_ARRAY_SIZE];
     347             :   #else
     348        3318 :     uint8_t* buffOut = new uint8_t[buffLen];
     349             :   #endif
     350        3318 :   uint8_t* buffOutPtr = buffOut;
     351             : 
     352             :   // copy the command
     353       12648 :   for(uint8_t n = 0; n < cmdLen; n++) {
     354        9330 :     *(buffOutPtr++) = cmd[n];
     355             :   }
     356             : 
     357             :   // copy the data
     358        3318 :   if(write) {
     359         608 :     memcpy(buffOutPtr, dataOut, numBytes);
     360             :   } else {
     361        2710 :     memset(buffOutPtr, this->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_NOP], numBytes + (this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_STATUS] / 8));
     362             :   }
     363             : 
     364             :   // ensure GPIO is low
     365        3318 :   if(waitForGpio) {
     366        3237 :     if(this->gpioPin == RADIOLIB_NC) {
     367           0 :       this->hal->delay(50);
     368             :     } else {
     369        3237 :       RadioLibTime_t start = this->hal->millis();
     370        3237 :       while(this->hal->digitalRead(this->gpioPin)) {
     371           0 :         this->hal->yield();
     372             : 
     373             :         // wrap-safe duration check; signed cast avoids a cppcheck false positive
     374           0 :         if((long)(this->hal->millis() - start - this->spiConfig.timeout) >= 0) {
     375             :           RADIOLIB_DEBUG_BASIC_PRINTLN("GPIO pre-transfer timeout, is it connected?");
     376             :           #if !RADIOLIB_STATIC_ONLY
     377           0 :             delete[] buffOut;
     378             :           #endif
     379           0 :           return(RADIOLIB_ERR_SPI_CMD_TIMEOUT);
     380             :         }
     381             :       
     382             :       }
     383             :     }
     384             :   }
     385             : 
     386             :   // prepare the input buffer
     387             :   #if RADIOLIB_STATIC_ONLY
     388             :     uint8_t buffIn[RADIOLIB_STATIC_SPI_ARRAY_SIZE];
     389             :   #else
     390        3318 :     uint8_t* buffIn = new uint8_t[buffLen];
     391             :   #endif
     392             : 
     393             :   // do the transfer
     394        3318 :   this->hal->spiBeginTransaction();
     395        3318 :   this->hal->digitalWrite(this->csPin, this->hal->GpioLevelLow);
     396        3318 :   this->hal->spiTransfer(buffOut, buffLen, buffIn);
     397        3318 :   this->hal->digitalWrite(this->csPin, this->hal->GpioLevelHigh);
     398        3318 :   this->hal->spiEndTransaction();
     399             : 
     400             :   // wait for GPIO to go high and then low
     401        3318 :   if(waitForGpio) {
     402        3237 :     if(this->gpioPin == RADIOLIB_NC) {
     403           0 :       this->hal->delay(1);
     404             :     } else {
     405        3237 :       this->hal->delayMicroseconds(1);
     406        3237 :       RadioLibTime_t start = this->hal->millis();
     407        3237 :       while(this->hal->digitalRead(this->gpioPin)) {
     408           0 :         this->hal->yield();
     409             :         
     410             :         // wrap-safe duration check; signed cast avoids a cppcheck false positive
     411           0 :         if((long)(this->hal->millis() - start - this->spiConfig.timeout) >= 0) {
     412             :           RADIOLIB_DEBUG_BASIC_PRINTLN("GPIO post-transfer timeout, is it connected?");
     413             : 
     414             :           // do not return yet to display the debug output
     415           0 :           state = RADIOLIB_ERR_SPI_CMD_TIMEOUT;
     416           0 :           break;
     417             :         }
     418             :       
     419             :       }
     420             :     }
     421             :   }
     422             : 
     423             :   // parse status (only if GPIO did not timeout)
     424        3318 :   if((state == RADIOLIB_ERR_NONE) && (this->spiConfig.parseStatusCb != nullptr) && (buffLen > this->spiConfig.statusPos)) {
     425        3270 :     state = this->spiConfig.parseStatusCb(buffIn[this->spiConfig.statusPos]);
     426             :   }
     427             :   
     428             :   // copy the data
     429        3318 :   if(!write) {
     430             :     // skip the status bytes if present
     431        2710 :     memcpy(dataIn, &buffIn[cmdLen + (this->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_STATUS] / 8)], numBytes);
     432             :   }
     433             : 
     434             :   // print debug information
     435             :   #if RADIOLIB_DEBUG_SPI
     436             :     RADIOLIB_DEBUG_SPI_PRINTLN("LEN\t%d", buffLen);
     437             :     // print command byte(s)
     438             :     RADIOLIB_DEBUG_SPI_PRINT("CMD");
     439             :     if(write) {
     440             :       RADIOLIB_DEBUG_SPI_PRINT_NOTAG("W\t");
     441             :     } else {
     442             :       RADIOLIB_DEBUG_SPI_PRINT_NOTAG("R\t");
     443             :     }
     444             :     size_t n = 0;
     445             :     for(; n < cmdLen; n++) {
     446             :       // tab character intentionally omitted here
     447             :       // command is a single number so this is easier to parse
     448             :       RADIOLIB_DEBUG_SPI_PRINT_NOTAG("%02X", cmd[n]);
     449             :     }
     450             :     RADIOLIB_DEBUG_SPI_PRINTLN_NOTAG("");
     451             : 
     452             :     // print data bytes
     453             :     RADIOLIB_DEBUG_SPI_PRINT("SI\t");
     454             :     for(n = 0; n < cmdLen; n++) {
     455             :       RADIOLIB_DEBUG_SPI_PRINT_NOTAG("\t");
     456             :     }
     457             :     // initialization of n to 0 is skipped here, because we want to skip the command bytes
     458             :     for(; n < buffLen; n++) {
     459             :       RADIOLIB_DEBUG_SPI_PRINT_NOTAG("%02X\t", buffOut[n]);
     460             :     }
     461             :     RADIOLIB_DEBUG_SPI_PRINTLN_NOTAG("");
     462             :     RADIOLIB_DEBUG_SPI_PRINT("SO\t");
     463             :     for(n = 0; n < buffLen; n++) {
     464             :       RADIOLIB_DEBUG_SPI_PRINT_NOTAG("%02X\t", buffIn[n]);
     465             :     }
     466             :     RADIOLIB_DEBUG_SPI_PRINTLN_NOTAG("");
     467             :     // if there is enough data to evaluate the status byte, print it as well
     468             :     if(buffLen > this->spiConfig.statusPos) {
     469             :       RADIOLIB_DEBUG_SPI_PRINTLN("STAT\t%02X", buffIn[this->spiConfig.statusPos]);
     470             :     }
     471             :   #endif
     472             : 
     473             :   #if !RADIOLIB_STATIC_ONLY
     474        3318 :     delete[] buffOut;
     475        3318 :     delete[] buffIn;
     476             :   #endif
     477             : 
     478        3318 :   return(state);
     479             : }
     480             : 
     481           0 : void Module::waitForMicroseconds(RadioLibTime_t start, RadioLibTime_t len) {
     482             :   #if RADIOLIB_INTERRUPT_TIMING
     483             :   (void)start;
     484             :   if((this->TimerSetupCb != nullptr) && (len != this->prevTimingLen)) {
     485             :     prevTimingLen = len;
     486             :     this->TimerSetupCb(len);
     487             :   }
     488             :   this->TimerFlag = false;
     489             :   while(!this->TimerFlag) {
     490             :     this->hal->yield();
     491             :   }
     492             :   #else
     493           0 :    while(this->hal->micros() - start < len) {
     494           0 :     this->hal->yield();
     495             :   }
     496             :   #endif
     497           0 : }
     498             : 
     499             : #if RADIOLIB_DEBUG
     500             : void Module::regdump(const char* level, uint16_t start, size_t len) {
     501             :   #if RADIOLIB_STATIC_ONLY
     502             :     uint8_t buff[RADIOLIB_STATIC_ARRAY_SIZE];
     503             :   #else
     504             :     uint8_t* buff = new uint8_t[len];
     505             :   #endif
     506             :   SPIreadRegisterBurst(start, len, buff);
     507             :   rlb_hexdump(level, buff, len, start);
     508             :   #if !RADIOLIB_STATIC_ONLY
     509             :     delete[] buff;
     510             :   #endif
     511             : }
     512             : #endif
     513             : 
     514           0 : void Module::setRfSwitchPins(uint32_t rxEn, uint32_t txEn) {
     515             :   // This can be on the stack, setRfSwitchTable copies the contents
     516           0 :   const uint32_t pins[] = {
     517             :     rxEn, txEn, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC,
     518           0 :   };
     519             :   
     520             :   // This must be static, since setRfSwitchTable stores a reference.
     521             :   static const RfSwitchMode_t table[] = {
     522           0 :     { MODE_IDLE,  {this->hal->GpioLevelLow,  this->hal->GpioLevelLow} },
     523           0 :     { MODE_RX,    {this->hal->GpioLevelHigh, this->hal->GpioLevelLow} },
     524           0 :     { MODE_TX,    {this->hal->GpioLevelLow,  this->hal->GpioLevelHigh} },
     525             :     END_OF_MODE_TABLE,
     526           0 :   };
     527           0 :   setRfSwitchTable(pins, table);
     528           0 : }
     529             : 
     530           0 : void Module::setRfSwitchTable(const uint32_t (&pins)[RFSWITCH_MAX_PINS], const RfSwitchMode_t table[]) {
     531           0 :   memcpy(this->rfSwitchPins, pins, sizeof(this->rfSwitchPins));
     532           0 :   this->rfSwitchTable = table;
     533           0 :   for(size_t i = 0; i < RFSWITCH_MAX_PINS; i++) {
     534             :     // skip pins not controlled by the MCU (NC or radio-controlled DIO pins)
     535           0 :     if(pins[i] & RFSWITCH_PIN_FLAG) { continue; }
     536           0 :     this->hal->pinMode(pins[i], this->hal->GpioModeOutput);
     537             :   }
     538           0 : }
     539             : 
     540           0 : void Module::getRfSwitchTable(uint32_t* pins, const RfSwitchMode_t** table) {
     541           0 :   if(pins) { memcpy(pins, this->rfSwitchPins, sizeof(this->rfSwitchPins)); }
     542           0 :   if(table) { *table = this->rfSwitchTable; }
     543           0 : }
     544             : 
     545         156 : const Module::RfSwitchMode_t *Module::findRfSwitchMode(uint8_t mode) const {
     546         156 :   const RfSwitchMode_t *row = this->rfSwitchTable;
     547         156 :   while(row && row->mode != MODE_END_OF_TABLE) {
     548           0 :     if(row->mode == mode) {
     549           0 :       return row;
     550             :     }
     551           0 :     ++row;
     552             :   }
     553         156 :   return nullptr;
     554             : }
     555             : 
     556         156 : void Module::setRfSwitchState(uint8_t mode) {
     557         156 :   const RfSwitchMode_t *row = findRfSwitchMode(mode);
     558         156 :   if(!row) {
     559             :     // RF switch control is disabled or does not have this mode
     560         156 :     return;
     561             :   }
     562             : 
     563             :   // set pins
     564           0 :   const uint32_t *value = &row->values[0];
     565           0 :   for(size_t i = 0; i < RFSWITCH_MAX_PINS; i++) {
     566           0 :     uint32_t pin = this->rfSwitchPins[i];
     567           0 :     if(!(pin & RFSWITCH_PIN_FLAG)) {
     568           0 :       this->hal->digitalWrite(pin, *value);
     569             :     }
     570           0 :     ++value;
     571             :   }
     572             : }

Generated by: LCOV version 1.14