LCOV - code coverage report
Current view: top level - src/modules/SX127x - SX1277.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 33 111 29.7 %
Date: 2026-04-06 12:28:13 Functions: 6 8 75.0 %

          Line data    Source code
       1             : #include "SX1277.h"
       2             : #if !RADIOLIB_EXCLUDE_SX127X
       3             : 
       4           1 : SX1277::SX1277(Module* mod) : SX1278(mod) {
       5             : 
       6           1 : }
       7             : 
       8           0 : int16_t SX1277::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength, uint8_t gain) {
       9             :   // execute common part
      10           0 :   const uint8_t versions[] = { RADIOLIB_SX1278_CHIP_VERSION, RADIOLIB_SX1278_CHIP_VERSION_ALT, RADIOLIB_SX1278_CHIP_VERSION_RFM9X };
      11           0 :   int16_t state = SX127x::begin(versions, 3, syncWord, preambleLength);
      12           0 :   RADIOLIB_ASSERT(state);
      13             : 
      14             :   // configure publicly accessible settings
      15           0 :   state = setBandwidth(bw);
      16           0 :   RADIOLIB_ASSERT(state);
      17             : 
      18           0 :   state = setFrequency(freq);
      19           0 :   RADIOLIB_ASSERT(state);
      20             : 
      21           0 :   state = setSpreadingFactor(sf);
      22           0 :   RADIOLIB_ASSERT(state);
      23             : 
      24           0 :   state = setCodingRate(cr);
      25           0 :   RADIOLIB_ASSERT(state);
      26             : 
      27           0 :   state = setOutputPower(power);
      28           0 :   RADIOLIB_ASSERT(state);
      29             : 
      30           0 :   state = setGain(gain);
      31           0 :   RADIOLIB_ASSERT(state);
      32             : 
      33             :   // set publicly accessible settings that are not a part of begin method
      34           0 :   state = setCRC(true);
      35           0 :   RADIOLIB_ASSERT(state);
      36             : 
      37           0 :   return(state);
      38             : }
      39             : 
      40           0 : int16_t SX1277::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t power, uint16_t preambleLength, bool enableOOK) {
      41             :   // execute common part
      42           0 :   const uint8_t versions[] = { RADIOLIB_SX1278_CHIP_VERSION, RADIOLIB_SX1278_CHIP_VERSION_ALT, RADIOLIB_SX1278_CHIP_VERSION_RFM9X };
      43           0 :   int16_t state = SX127x::beginFSK(versions, 3, freqDev, rxBw, preambleLength, enableOOK);
      44           0 :   RADIOLIB_ASSERT(state);
      45             : 
      46             :   // configure settings not accessible by API
      47           0 :   state = configFSK();
      48           0 :   RADIOLIB_ASSERT(state);
      49             : 
      50             :   // configure publicly accessible settings
      51           0 :   state = setFrequency(freq);
      52           0 :   RADIOLIB_ASSERT(state);
      53             : 
      54           0 :   state = setBitRate(br);
      55           0 :   RADIOLIB_ASSERT(state);
      56             : 
      57           0 :   state = setOutputPower(power);
      58           0 :   RADIOLIB_ASSERT(state);
      59             : 
      60           0 :   if(enableOOK) {
      61           0 :     state = setDataShapingOOK(RADIOLIB_SHAPING_NONE);
      62           0 :     RADIOLIB_ASSERT(state);
      63             :   } else {
      64           0 :     state = setDataShaping(RADIOLIB_SHAPING_NONE);
      65           0 :     RADIOLIB_ASSERT(state);
      66             :   }
      67             : 
      68           0 :   return(state);
      69             : }
      70             : 
      71           1 : int16_t SX1277::setFrequency(float freq) {
      72             :   // NOTE: The datasheet specifies Band 2 as 410-525 MHz, but the hardware has been
      73             :   // verified to work down to ~395 MHz. The lower bound is set here to 395 MHz to
      74             :   // accommodate real-world use cases (e.g. TinyGS satellites, radiosondes) while
      75             :   // adding a small margin below the 400 MHz practical limit.
      76           1 :   if(!(((freq >= 137.0f) && (freq <= 175.0f)) ||
      77           1 :        ((freq >= 395.0f) && (freq <= 525.0f)) ||
      78           1 :        ((freq >= 862.0f) && (freq <= 1020.0f)))) {
      79           1 :     return(RADIOLIB_ERR_INVALID_FREQUENCY);
      80             :   }
      81             : 
      82             :   // set frequency and if successful, save the new setting
      83           0 :   int16_t state = SX127x::setFrequencyRaw(freq);
      84           0 :   if(state == RADIOLIB_ERR_NONE) {
      85           0 :     SX127x::frequency = freq;
      86             :   }
      87           0 :   return(state);
      88             : }
      89             : 
      90           1 : int16_t SX1277::setSpreadingFactor(uint8_t sf) {
      91             :   uint8_t newSpreadingFactor;
      92             : 
      93             :   // check allowed spreading factor values
      94           1 :   switch(sf) {
      95           0 :     case 6:
      96           0 :       newSpreadingFactor = RADIOLIB_SX127X_SF_6;
      97           0 :       break;
      98           0 :     case 7:
      99           0 :       newSpreadingFactor = RADIOLIB_SX127X_SF_7;
     100           0 :       break;
     101           0 :     case 8:
     102           0 :       newSpreadingFactor = RADIOLIB_SX127X_SF_8;
     103           0 :       break;
     104           0 :     case 9:
     105           0 :       newSpreadingFactor = RADIOLIB_SX127X_SF_9;
     106           0 :       break;
     107           1 :     default:
     108           1 :       return(RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
     109             :   }
     110             : 
     111             :   // set spreading factor and if successful, save the new setting
     112           0 :   int16_t state = SX1278::setSpreadingFactorRaw(newSpreadingFactor);
     113           0 :   if(state == RADIOLIB_ERR_NONE) {
     114           0 :     SX127x::spreadingFactor = sf;
     115             :   }
     116             : 
     117           0 :   return(state);
     118             : }
     119             : 
     120           1 : int16_t SX1277::setDataRate(DataRate_t dr, ModemType_t modem) {
     121             :   // get the current modem
     122             :   ModemType_t currentModem;
     123           1 :   int16_t state = this->getModem(&currentModem);
     124           1 :   RADIOLIB_ASSERT(state);
     125             : 
     126             :   // switch over if the requested modem is different
     127           1 :   if(modem != RADIOLIB_MODEM_NONE && modem != currentModem) {
     128           0 :     state = this->standby();
     129           0 :     RADIOLIB_ASSERT(state);
     130           0 :     state = this->setModem(modem);
     131           0 :     RADIOLIB_ASSERT(state);
     132             :   }
     133             :   
     134           1 :   if(modem == RADIOLIB_MODEM_NONE) {
     135           1 :     modem = currentModem;
     136             :   }
     137             : 
     138             :   // select interpretation based on modem
     139           1 :   if(modem == RADIOLIB_MODEM_FSK) {
     140             :     // set the bit rate
     141           0 :     state = this->setBitRate(dr.fsk.bitRate);
     142           0 :     RADIOLIB_ASSERT(state);
     143             : 
     144             :     // set the frequency deviation
     145           0 :     state = this->setFrequencyDeviation(dr.fsk.freqDev);
     146             : 
     147           1 :   } else if(modem == RADIOLIB_MODEM_LORA) {
     148             :     // set the spreading factor
     149           1 :     state = this->setSpreadingFactor(dr.lora.spreadingFactor);
     150           1 :     RADIOLIB_ASSERT(state);
     151             : 
     152             :     // set the bandwidth
     153           0 :     state = this->setBandwidth(dr.lora.bandwidth);
     154             :   }
     155             : 
     156           0 :   return(state);
     157             : }
     158             : 
     159           1 : int16_t SX1277::checkDataRate(DataRate_t dr, ModemType_t modem) {
     160           1 :   int16_t state = RADIOLIB_ERR_UNKNOWN;
     161             : 
     162             :   // retrieve modem if not supplied
     163           1 :   if(modem == RADIOLIB_MODEM_NONE) {
     164           1 :     state = this->getModem(&modem);
     165           1 :     RADIOLIB_ASSERT(state);
     166             :   }
     167             :   
     168             :   // select interpretation based on modem
     169           1 :   if(modem == RADIOLIB_MODEM_FSK) {
     170           0 :     RADIOLIB_CHECK_RANGE(dr.fsk.bitRate, 0.5f, 300.0f, RADIOLIB_ERR_INVALID_BIT_RATE);
     171           0 :     if(!((dr.fsk.freqDev + dr.fsk.bitRate/2.0f <= 250.0f) && (dr.fsk.freqDev <= 200.0f))) {
     172           0 :       return(RADIOLIB_ERR_INVALID_FREQUENCY_DEVIATION);
     173             :     }
     174           0 :     return(RADIOLIB_ERR_NONE);
     175             : 
     176           1 :   } else if(modem == RADIOLIB_MODEM_LORA) {
     177           1 :     RADIOLIB_CHECK_RANGE(dr.lora.spreadingFactor, 6, 9, RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
     178           0 :     RADIOLIB_CHECK_RANGE(dr.lora.bandwidth, 0.0f, 510.0f, RADIOLIB_ERR_INVALID_BANDWIDTH);
     179           0 :     RADIOLIB_CHECK_RANGE(dr.lora.codingRate, 4, 8, RADIOLIB_ERR_INVALID_CODING_RATE);
     180           0 :     return(RADIOLIB_ERR_NONE);
     181             :   
     182             :   }
     183             : 
     184           0 :   return(state);
     185             : }
     186             : 
     187           1 : int16_t SX1277::setModem(ModemType_t modem) {
     188           1 :   switch(modem) {
     189           0 :     case(ModemType_t::RADIOLIB_MODEM_LORA): {
     190           0 :       return(this->begin());
     191             :     } break;
     192           0 :     case(ModemType_t::RADIOLIB_MODEM_FSK): {
     193           0 :       return(this->beginFSK());
     194             :     } break;
     195           1 :     default:
     196           1 :       return(RADIOLIB_ERR_WRONG_MODEM);
     197             :   }
     198             : }
     199             : 
     200             : #endif

Generated by: LCOV version 1.14