LCOV - code coverage report
Current view: top level - src - Module.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 105 219 47.9 %
Date: 2025-02-19 18:47:57 Functions: 9 23 39.1 %

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

Generated by: LCOV version 1.14