LCOV - code coverage report
Current view: top level - src/modules/LR2021 - LR2021_cmds_chip_control.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 23 230 10.0 %
Date: 2026-10-11 07:31:10 Functions: 6 49 12.2 %

          Line data    Source code
       1             : #include "LR2021.h"
       2             : 
       3             : #include "../LR11x0/LR_common.h"
       4             : #include "LR2021_registers.h"
       5             : 
       6             : #include <string.h>
       7             : #include <math.h>
       8             : 
       9             : #if !RADIOLIB_EXCLUDE_LR2021
      10             : 
      11           0 : int16_t LR2021::readRadioRxFifo(uint8_t* data, size_t len) {
      12             :   // FIFO read is just a single transaction sent without the status code
      13           0 :   this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_STATUS] = Module::BITS_0;
      14           0 :   int16_t state = this->mod->SPIreadStream(RADIOLIB_LR2021_CMD_READ_RX_FIFO, data, len, true, false);
      15           0 :   this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_STATUS] = Module::BITS_16;
      16           0 :   return(state);
      17             : }
      18             : 
      19           0 : int16_t LR2021::writeRadioTxFifo(const uint8_t* data, size_t len) {
      20           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_WRITE_TX_FIFO, true, const_cast<uint8_t*>(data), len, NULL, 0));
      21             : }
      22             : 
      23           0 : int16_t LR2021::writeRegMem32(uint32_t addr, const uint32_t* data, size_t len) {
      24             :   // check maximum size
      25           0 :   if(len > (RADIOLIB_LRXXXX_SPI_MAX_READ_WRITE_LEN/sizeof(uint32_t))) {
      26           0 :     return(RADIOLIB_ERR_SPI_CMD_INVALID);
      27             :   }
      28           0 :   return(this->writeCommon(RADIOLIB_LR2021_CMD_WRITE_REG_MEM_32, addr, data, len, false));
      29             : }
      30             : 
      31           0 : int16_t LR2021::writeRegMemMask32(uint32_t addr, uint32_t mask, uint32_t data) {
      32             :   uint8_t buff[11] = {
      33           0 :     (uint8_t)((addr >> 16) & 0xFF), (uint8_t)((addr >> 8) & 0xFF), (uint8_t)(addr & 0xFF),
      34           0 :     (uint8_t)((mask >> 24) & 0xFF), (uint8_t)((mask >> 16) & 0xFF), (uint8_t)((mask >> 8) & 0xFF), (uint8_t)(mask & 0xFF),
      35           0 :     (uint8_t)((data >> 24) & 0xFF), (uint8_t)((data >> 16) & 0xFF), (uint8_t)((data >> 8) & 0xFF), (uint8_t)(data & 0xFF),
      36           0 :   };
      37           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_WRITE_REG_MEM_MASK_32, true, buff, sizeof(buff)));
      38             : }
      39             : 
      40           0 : int16_t LR2021::readRegMem32(uint32_t addr, uint32_t* data, size_t len) {
      41             :   // check maximum size
      42           0 :   if(len > (RADIOLIB_LRXXXX_SPI_MAX_READ_WRITE_LEN/sizeof(uint32_t))) {
      43           0 :     return(RADIOLIB_ERR_SPI_CMD_INVALID);
      44             :   }
      45             : 
      46             :   // the request contains the address and length
      47             :   uint8_t reqBuff[4] = {
      48           0 :     (uint8_t)((addr >> 16) & 0xFF), (uint8_t)((addr >> 8) & 0xFF),
      49             :     (uint8_t)(addr & 0xFF), (uint8_t)len,
      50           0 :   };
      51             : 
      52             :   // build buffers - later we need to ensure endians are correct, 
      53             :   // so there is probably no way to do this without copying buffers and iterating
      54             :   #if RADIOLIB_STATIC_ONLY
      55             :     uint8_t rplBuff[RADIOLIB_LRXXXX_SPI_MAX_READ_WRITE_LEN];
      56             :   #else
      57           0 :     uint8_t* rplBuff = new uint8_t[len*sizeof(uint32_t)];
      58             :   #endif
      59             : 
      60           0 :   int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_READ_REG_MEM_32, false, rplBuff, len*sizeof(uint32_t), reqBuff, sizeof(reqBuff));
      61             : 
      62             :   // convert endians
      63           0 :   if(data && (state == RADIOLIB_ERR_NONE)) {
      64           0 :     for(size_t i = 0; i < len; i++) {
      65           0 :       data[i] = ((uint32_t)rplBuff[i*sizeof(uint32_t)] << 24) | ((uint32_t)rplBuff[1 + i*sizeof(uint32_t)] << 16) | ((uint32_t)rplBuff[2 + i*sizeof(uint32_t)] << 8) | (uint32_t)rplBuff[3 + i*sizeof(uint32_t)];
      66             :     }
      67             :   }
      68             : 
      69             :   #if !RADIOLIB_STATIC_ONLY
      70           0 :     delete[] rplBuff;
      71             :   #endif
      72             :   
      73           0 :   return(state);
      74             : }
      75             : 
      76           0 : int16_t LR2021::setFs(void) {
      77           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_FS, true, NULL, 0));
      78             : }
      79             : 
      80           0 : int16_t LR2021::setAdditionalRegToRetain(uint8_t slot, uint32_t addr) {
      81             :   uint8_t buff[] = { 
      82           0 :     slot, (uint8_t)((addr >> 16) & 0xFF),
      83           0 :     (uint8_t)((addr >> 8) & 0xFF), (uint8_t)(addr & 0xFF),
      84           0 :   };
      85             : 
      86           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_ADDITIONAL_REG_TO_RETAIN, true, buff, sizeof(buff)));
      87             : }
      88             : 
      89           1 : int16_t LR2021::setRx(uint32_t timeout) {
      90             :   uint8_t buff[] = {
      91           1 :     (uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF),
      92           1 :   };
      93           2 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_RX, true, buff, sizeof(buff)));
      94             : }
      95             : 
      96           0 : int16_t LR2021::setTx(uint32_t timeout) {
      97             :   uint8_t buff[] = {
      98           0 :     (uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF),
      99           0 :   };
     100           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_TX, true, buff, sizeof(buff)));
     101             : }
     102             : 
     103           0 : int16_t LR2021::setRxTxFallbackMode(uint8_t mode) {
     104           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_RX_TX_FALLBACK_MODE, true, &mode, sizeof(mode)));
     105             : }
     106             : 
     107           0 : int16_t LR2021::setRxDutyCycle(uint32_t rxMaxTime, uint32_t cycleTime, uint8_t mode) {
     108             :   uint8_t buff[] = {
     109           0 :     (uint8_t)((rxMaxTime >> 16) & 0xFF), (uint8_t)((rxMaxTime >> 8) & 0xFF), (uint8_t)(rxMaxTime & 0xFF),
     110           0 :     (uint8_t)((cycleTime >> 16) & 0xFF), (uint8_t)((cycleTime >> 8) & 0xFF), (uint8_t)(cycleTime & 0xFF),
     111           0 :     (uint8_t)(mode << 4),
     112           0 :   };
     113           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_RX_DUTY_CYCLE, true, buff, sizeof(buff)));
     114             : }
     115             : 
     116           0 : int16_t LR2021::autoTxRx(uint32_t delay, uint8_t mode, uint32_t timeout) {
     117             :   uint8_t buff[] = {
     118           0 :     (uint8_t)((delay >> 16) & 0xFF), (uint8_t)((delay >> 8) & 0xFF), (uint8_t)(delay & 0xFF), mode,
     119           0 :     (uint8_t)((timeout >> 16) & 0xFF), (uint8_t)((timeout >> 8) & 0xFF), (uint8_t)(timeout & 0xFF),
     120           0 :   };
     121           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_AUTO_RX_TX, true, buff, sizeof(buff)));
     122             : }
     123             : 
     124           1 : int16_t LR2021::getRxPktLength(uint16_t* len) {
     125           1 :   uint8_t buff[2] = { 0 };
     126           1 :   int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_RX_PKT_LENGTH, false, buff, sizeof(buff));
     127           1 :   if(len) { *len = ((uint16_t)(buff[0]) << 8) | (uint16_t)buff[1]; }
     128           1 :   return(state);
     129             : }
     130             : 
     131           0 : int16_t LR2021::resetRxStats(void) {
     132           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_RESET_RX_STATS, true, NULL, 0));
     133             : }
     134             : 
     135           0 : int16_t LR2021::setDefaultRxTxTimeout(uint32_t rxTimeout, uint32_t txTimeout) {
     136             :   uint8_t buff[] = {
     137           0 :     (uint8_t)((rxTimeout >> 16) & 0xFF), (uint8_t)((rxTimeout >> 8) & 0xFF), (uint8_t)(rxTimeout & 0xFF),
     138           0 :     (uint8_t)((txTimeout >> 16) & 0xFF), (uint8_t)((txTimeout >> 8) & 0xFF), (uint8_t)(txTimeout & 0xFF),
     139           0 :   };
     140           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_DEFAULT_RX_TX_TIMEOUT, true, buff, sizeof(buff)));
     141             : }
     142             : 
     143           0 : int16_t LR2021::setRegMode(uint8_t mode) {
     144           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_REG_MODE, true, &mode, sizeof(mode)));
     145             : }
     146             : 
     147           0 : int16_t LR2021::calibrate(uint8_t blocks) {
     148           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_CALIBRATE, true, &blocks, sizeof(blocks)));
     149             : }
     150             : 
     151           0 : int16_t LR2021::calibrateFrontEnd(const uint16_t freq[3]) {
     152             :   uint8_t buff[] = {
     153           0 :     (uint8_t)((freq[0] >> 8) & 0xFF), (uint8_t)(freq[0] & 0xFF),
     154           0 :     (uint8_t)((freq[1] >> 8) & 0xFF), (uint8_t)(freq[1] & 0xFF),
     155           0 :     (uint8_t)((freq[2] >> 8) & 0xFF), (uint8_t)(freq[2] & 0xFF),
     156           0 :   };
     157           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_CALIB_FRONT_END, true, buff, sizeof(buff)));
     158             : }
     159             : 
     160           0 : int16_t LR2021::getVbat(uint8_t resolution, uint16_t* vbat) {
     161           0 :   uint8_t reqBuff[] = { (uint8_t)(RADIOLIB_LR2021_VBAT_FORMAT_MV | ((resolution - RADIOLIB_LR2021_MEAS_RESOLUTION_OFFSET) & 0x07)) };
     162           0 :   uint8_t rplBuff[2] = { 0 };
     163           0 :   int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_V_BAT, false, rplBuff, sizeof(rplBuff), reqBuff, sizeof(reqBuff));
     164           0 :   if(vbat) { *vbat = ((uint16_t)(rplBuff[0]) << 8) | (uint16_t)rplBuff[1]; }
     165           0 :   return(state);
     166             : }
     167             : 
     168           0 : int16_t LR2021::getTemp(uint8_t source, uint8_t resolution, float* temp) {
     169             :   // reading of temperature in degrees seems broken and the datasheet disagrees with reference implementation
     170             :   // so we read out the raw value and convert it here
     171           0 :   uint8_t reqBuff[] = { (uint8_t)((source & 0x30) | RADIOLIB_LR2021_TEMP_FORMAT_RAW | ((resolution - RADIOLIB_LR2021_MEAS_RESOLUTION_OFFSET) & 0x07)) };
     172           0 :   uint8_t rplBuff[2] = { 0 };
     173           0 :   int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_TEMP, false, rplBuff, sizeof(rplBuff), reqBuff, sizeof(reqBuff));
     174           0 :   if(temp) { 
     175           0 :     uint16_t raw = ((uint16_t)(rplBuff[0]) << 8) | (uint16_t)rplBuff[1];
     176           0 :     *temp = 25.0f + (0.7295f - 1.35f * raw / 8192.0f) * (1000.0f / 1.7f);
     177             :   }
     178           0 :   return(state);
     179             : }
     180             : 
     181           0 : int16_t LR2021::setEolConfig(bool enable, uint8_t trim) {
     182           0 :   uint8_t buff[] = { (uint8_t)((trim & 0x06) | (uint8_t)enable) };
     183           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_EOL_CONFIG, true, buff, sizeof(buff)));
     184             : }
     185             : 
     186           1 : int16_t LR2021::getRandomNumber(uint32_t* rnd) {
     187           1 :   uint8_t buff[4] = { 0 };
     188           1 :   int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_RANDOM_NUMBER, false, buff, sizeof(buff));
     189           1 :   if(rnd) { *rnd = ((uint32_t)(buff[0]) << 24) | ((uint32_t)(buff[1]) << 16) | ((uint32_t)(buff[2]) << 8) | (uint32_t)buff[3];  }
     190           1 :   return(state);
     191             : }
     192             : 
     193           0 : int16_t LR2021::getVersion(uint8_t* major, uint8_t* minor) {
     194           0 :   uint8_t buff[2] = { 0 };
     195           0 :   int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_VERSION, false, buff, sizeof(buff));
     196           0 :   if(major)   { *major = buff[0]; }
     197           0 :   if(minor)   { *minor = buff[1]; }
     198           0 :   return(state);
     199             : }
     200             : 
     201           0 : int16_t LR2021::clearErrors(void) {
     202           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_CLEAR_ERRORS, true, NULL, 0));
     203             : }
     204             : 
     205           0 : int16_t LR2021::getErrors(uint16_t* err) {
     206           0 :   uint8_t buff[2] = { 0 };
     207           0 :   int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_ERRORS, false, buff, sizeof(buff));
     208           0 :   if(err) { *err = ((uint16_t)(buff[0]) << 8) | (uint16_t)buff[1];  }
     209           0 :   return(state);
     210             : }
     211             : 
     212           0 : int16_t LR2021::setDioFunction(uint8_t dio, uint8_t func, uint8_t pullDrive) {
     213           0 :   uint8_t buff[] = { dio, (uint8_t)((func & 0xF0) | (pullDrive & 0x0F)) };
     214           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_DIO_FUNCTION, true, buff, sizeof(buff)));
     215             : }
     216             : 
     217           0 : int16_t LR2021::setDioRfSwitchConfig(uint8_t dio, uint8_t func) {
     218           0 :   uint8_t buff[] = { dio, func };
     219           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_DIO_RF_SWITCH_CONFIG, true, buff, sizeof(buff)));
     220             : }
     221             : 
     222           1 : int16_t LR2021::setDioIrqConfig(uint8_t dio, uint32_t irq) {
     223             :   uint8_t buff[] = { dio, 
     224           1 :     (uint8_t)((irq >> 24) & 0xFF), (uint8_t)((irq >> 16) & 0xFF),
     225           1 :     (uint8_t)((irq >> 8) & 0xFF), (uint8_t)(irq & 0xFF),
     226           1 :   };
     227           2 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_DIO_IRQ_CONFIG, true, buff, sizeof(buff)));
     228             : }
     229             : 
     230           2 : int16_t LR2021::clearIrqState(uint32_t irq) {
     231           2 :   return(this->setU32(RADIOLIB_LR2021_CMD_CLEAR_IRQ, irq));
     232             : }
     233             : 
     234           0 : int16_t LR2021::getAndClearIrqStatus(uint32_t* irq) {
     235           0 :   uint8_t buff[4] = { 0 };
     236           0 :   int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_AND_CLEAR_IRQ_STATUS, false, buff, sizeof(buff));
     237           0 :   if(irq) { *irq = ((uint32_t)(buff[0]) << 24) | ((uint32_t)(buff[1]) << 16) | ((uint32_t)(buff[2]) << 8) |(uint32_t)buff[3]; }
     238           0 :   return(state);
     239             : }
     240             : 
     241           0 : int16_t LR2021::configFifoIrq(uint8_t rxFifoIrq, uint8_t txFifoIrq, uint16_t rxHighThreshold, uint16_t txLowThreshold, uint16_t rxLowThreshold, uint16_t txHighThreshold) {
     242             :   uint8_t buff[] = {
     243             :         rxFifoIrq,
     244             :         txFifoIrq,
     245           0 :         (uint8_t)((rxHighThreshold >> 8) & 0xFF),
     246             :         (uint8_t)(rxHighThreshold & 0xFF),
     247           0 :         (uint8_t)((txLowThreshold >> 8) & 0xFF),
     248             :         (uint8_t)(txLowThreshold & 0xFF),
     249           0 :         (uint8_t)((rxLowThreshold >> 8) & 0xFF),
     250             :         (uint8_t)(rxLowThreshold & 0xFF),
     251           0 :         (uint8_t)((txHighThreshold >> 8) & 0xFF),
     252             :         (uint8_t)(txHighThreshold & 0xFF),
     253           0 :         };
     254           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_CONFIG_FIFO_IRQ, true, buff, sizeof(buff)));
     255             : }
     256             : 
     257           0 : int16_t LR2021::getFifoIrqFlags(uint8_t* rxFifoFlags, uint8_t* txFifoFlags) {
     258           0 :   uint8_t buff[2] = { 0 };
     259           0 :   int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_FIFO_IRQ_FLAGS, false, buff, sizeof(buff));
     260           0 :   if(rxFifoFlags) { *rxFifoFlags = buff[0]; }
     261           0 :   if(txFifoFlags) { *txFifoFlags = buff[1]; }
     262           0 :   return(state);
     263             : }
     264             : 
     265           0 : int16_t LR2021::clearFifoIrqFlags(uint8_t rxFifoFlags, uint8_t txFifoFlags) {
     266           0 :   uint8_t buff[] = { rxFifoFlags, txFifoFlags };
     267           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_CLEAR_FIFO_IRQ_FLAGS, true, buff, sizeof(buff)));
     268             : }
     269             : 
     270           0 : int16_t LR2021::getAndClearFifoIrqFlags(uint8_t* rxFifoFlags, uint8_t* txFifoFlags) {
     271           0 :   uint8_t buff[2] = { 0 };
     272           0 :   int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_AND_CLEAR_FIFO_IRQ_FLAGS, false, buff, sizeof(buff));
     273           0 :   if(rxFifoFlags) { *rxFifoFlags = buff[0]; }
     274           0 :   if(txFifoFlags) { *txFifoFlags = buff[1]; }
     275           0 :   return(state);
     276             : }
     277             : 
     278           0 : int16_t LR2021::getRxFifoLevel(uint16_t* level) {
     279           0 :   uint8_t buff[2] = { 0 };
     280           0 :   int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_RX_FIFO_LEVEL, false, buff, sizeof(buff));
     281           0 :   if(level) { *level = ((uint16_t)(buff[0]) << 8) | (uint16_t)buff[1];  }
     282           0 :   return(state);
     283             : }
     284             : 
     285           0 : int16_t LR2021::getTxFifoLevel(uint16_t* level) {
     286           0 :   uint8_t buff[2] = { 0 };
     287           0 :   int16_t state = this->SPIcommand(RADIOLIB_LR2021_CMD_GET_TX_FIFO_LEVEL, false, buff, sizeof(buff));
     288           0 :   if(level) { *level = ((uint16_t)(buff[0]) << 8) | (uint16_t)buff[1];  }
     289           0 :   return(state);
     290             : }
     291             : 
     292           0 : int16_t LR2021::clearRxFifo(void) {
     293           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_CLEAR_RX_FIFO, true, NULL, 0));
     294             : }
     295             : 
     296           2 : int16_t LR2021::clearTxFifo(void) {
     297           2 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_CLEAR_TX_FIFO, true, NULL, 0));
     298             : }
     299             : 
     300           0 : int16_t LR2021::configLfClock(uint8_t cfg) {
     301           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_CONFIG_LF_CLOCK, true, &cfg, sizeof(cfg)));
     302             : }
     303             : 
     304           0 : int16_t LR2021::configClkOutputs(uint8_t scaling) {
     305           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_CONFIG_CLK_OUTPUTS, true, &scaling, sizeof(scaling)));
     306             : }
     307             : 
     308           0 : int16_t LR2021::setTcxoMode(uint8_t tune, uint32_t startTime) {
     309           0 :   uint8_t buff[] = { (uint8_t)(tune & 0x07),
     310           0 :     (uint8_t)((startTime >> 24) & 0xFF), (uint8_t)((startTime >> 16) & 0xFF),
     311           0 :     (uint8_t)((startTime >> 8) & 0xFF), (uint8_t)(startTime & 0xFF),
     312           0 :   };
     313           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_TCXO_MODE, true, buff, sizeof(buff)));
     314             : }
     315             : 
     316           0 : int16_t LR2021::setXoscCpTrim(uint8_t xta, uint8_t xtb, uint8_t startTime) {
     317           0 :   uint8_t buff[] = { (uint8_t)(xta & 0x3F), (uint8_t)(xtb & 0x3F), startTime };
     318           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_SET_XOSC_CP_TRIM, true, buff, sizeof(buff)));
     319             : }
     320             : 
     321           0 : int16_t LR2021::activatePram(void) {
     322           0 :   uint8_t buff[] = { RADIOLIB_LR2021_CMD_NOP };
     323           0 :   return(this->SPIcommand(RADIOLIB_LR2021_CMD_ACTIVATE_PRAM, true, buff, sizeof(buff)));
     324             : }
     325             : 
     326           0 : int16_t LR2021::checkPramLoaded(bool* loaded) {
     327           0 :   uint32_t val = 0;
     328           0 :   int16_t state = this->readRegMem32(RADIOLIB_LR2021_PRAM_ADDR_LOADED, &val, 1);
     329           0 :   if(loaded) { *loaded = (val == RADIOLIB_LR2021_PRAM_LOADED_MAGIC);  }
     330           0 :   return(state);
     331             : }
     332             : 
     333           0 : int16_t LR2021::getPramVersion(uint16_t* version) {
     334           0 :   uint32_t val = 0;
     335           0 :   int16_t state = this->readRegMem32(RADIOLIB_LR2021_PRAM_ADDR_VERSION, &val, 1);
     336           0 :   if(version) { *version = ((val >>8) & 0xFFFF);  }
     337           0 :   return(state);
     338             : }
     339             : 
     340           0 : int16_t LR2021::setRegulatorLDO() {
     341           0 :   return(this->setRegMode(RADIOLIB_LR2021_REG_MODE_SIMO_OFF));
     342             : }
     343             : 
     344           0 : int16_t LR2021::setRegulatorDCDC() {
     345           0 :   return(this->setRegMode(RADIOLIB_LR2021_REG_MODE_SIMO_NORMAL));
     346             : }
     347             : 
     348             : // workaround: port of semtech's code altered to use local freqMHz/highFreq - magic numbers are theirs
     349             : // https://github.com/Lora-net/usp/blob/351b20153506/smtc_rac_lib/radio_drivers/lr20xx_driver/src/lr20xx_workarounds.c
     350           0 : int16_t LR2021::setDCDCworkaround() {
     351           0 :   uint32_t adcCtrlRaw = 0;
     352           0 :   int16_t state = this->readRegMem32(RADIOLIB_LR2021_REG_DCDC_ADC_CTRL, &adcCtrlRaw, 1);
     353           0 :   RADIOLIB_ASSERT(state);
     354           0 :   const uint32_t anaDec = ( adcCtrlRaw >> 8 ) & 0x7;
     355             : 
     356           0 :   if (!this->highFreq && (anaDec == 1 || anaDec == 2)) {
     357           0 :     state = this->writeRegMemMask32(RADIOLIB_LR2021_REG_DCDC_SWITCHER, 0x0FUL << 20, 11UL << 20);
     358           0 :     RADIOLIB_ASSERT(state);
     359           0 :     state = this->writeRegMemMask32(RADIOLIB_LR2021_REG_DCDC_SWITCHER, 0x0FUL << 16, 13UL << 16);
     360           0 :     RADIOLIB_ASSERT(state);
     361             :   } else {
     362           0 :     state = this->writeRegMemMask32(RADIOLIB_LR2021_REG_DCDC_SWITCHER, 0x0FUL << 20, 15UL << 20);
     363           0 :     RADIOLIB_ASSERT(state);    
     364           0 :     state = this->writeRegMemMask32(RADIOLIB_LR2021_REG_DCDC_SWITCHER, 0x0FUL << 16, 15UL << 16);
     365           0 :     RADIOLIB_ASSERT(state);
     366             :   }
     367             : 
     368             :   // semtech number: 2800000 * 1.048576f (lines 558, 666)
     369           0 :   uint32_t freq_lf = 2936012;
     370           0 :   if (anaDec == 1) {
     371             :     // semtech number: 4300000 * 1.048576f (lines 554, 666)
     372           0 :     freq_lf = 4508876;
     373             :   } 
     374           0 :   state = this->writeRegMem32(RADIOLIB_LR2021_REG_DCDC_FREQ_LF, &freq_lf, 1);
     375           0 :   RADIOLIB_ASSERT(state);
     376             :   
     377           0 :   state = this->setFrequency(this->freqMHz, true);
     378           0 :   return(state);
     379             : }
     380             : 
     381           0 : int16_t LR2021::resetDCDCworkaround() {
     382           0 :   int16_t state = this->writeRegMemMask32(RADIOLIB_LR2021_REG_DCDC_SWITCHER, 0x0FUL << 20, 15UL << 20);
     383           0 :   RADIOLIB_ASSERT(state);
     384             : 
     385           0 :   state = this->writeRegMemMask32(RADIOLIB_LR2021_REG_DCDC_SWITCHER, 0x0FUL << 16, 15UL << 16);
     386           0 :   RADIOLIB_ASSERT(state);
     387             : 
     388             :   // semtech number: 2800000 * 1.048576f (lines 558, 666)
     389           0 :   uint32_t freq_lf = 2936012;
     390           0 :   state = this->writeRegMem32(RADIOLIB_LR2021_REG_DCDC_FREQ_LF, &freq_lf, 1);
     391           0 :   RADIOLIB_ASSERT(state);
     392             : 
     393           0 :   if(this->freqMHz) {
     394           0 :     state = this->setFrequency(this->freqMHz, true);
     395             :   }
     396           0 :   return(state);
     397             : }
     398             : 
     399             : #endif

Generated by: LCOV version 1.14