LCOV - code coverage report
Current view: top level - src/modules/SX127x - SX1277.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 21 94 22.3 %
Date: 2026-11-08 19:43:08 Functions: 5 7 71.4 %

          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(const ConfigLoRa_t& cfg) {
       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, cfg.syncWord, cfg.preambleLength);
      12           0 :   RADIOLIB_ASSERT(state);
      13             : 
      14             :   // configure publicly accessible settings
      15           0 :   state = setBandwidth(cfg.bandwidth);
      16           0 :   RADIOLIB_ASSERT(state);
      17             : 
      18           0 :   state = setFrequency(cfg.frequency);
      19           0 :   RADIOLIB_ASSERT(state);
      20             : 
      21           0 :   state = setSpreadingFactor(cfg.spreadingFactor);
      22           0 :   RADIOLIB_ASSERT(state);
      23             : 
      24           0 :   state = setCodingRate(cfg.codingRate);
      25           0 :   RADIOLIB_ASSERT(state);
      26             : 
      27           0 :   state = setOutputPower(cfg.power);
      28           0 :   RADIOLIB_ASSERT(state);
      29             : 
      30           0 :   state = setGain(this->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 :   return(state);
      36             : }
      37             : 
      38           0 : int16_t SX1277::beginFSK(const ConfigFSK_t& cfg) {
      39             :   // execute common part
      40           0 :   const uint8_t versions[] = { RADIOLIB_SX1278_CHIP_VERSION, RADIOLIB_SX1278_CHIP_VERSION_ALT, RADIOLIB_SX1278_CHIP_VERSION_RFM9X };
      41           0 :   int16_t state = SX127x::beginFSK(versions, 3, cfg.frequencyDeviation, cfg.receiverBandwidth, cfg.preambleLength);
      42           0 :   RADIOLIB_ASSERT(state);
      43             : 
      44             :   // configure settings not accessible by API
      45           0 :   state = configFSK();
      46           0 :   RADIOLIB_ASSERT(state);
      47             : 
      48             :   // configure publicly accessible settings
      49           0 :   state = setFrequency(cfg.frequency);
      50           0 :   RADIOLIB_ASSERT(state);
      51             : 
      52           0 :   state = setBitRate(cfg.bitRate);
      53           0 :   RADIOLIB_ASSERT(state);
      54             : 
      55           0 :   state = setOutputPower(cfg.power);
      56           0 :   RADIOLIB_ASSERT(state);
      57             : 
      58           0 :   if(this->enableOOK) {
      59           0 :     state = setDataShapingOOK(RADIOLIB_SHAPING_NONE);
      60           0 :     RADIOLIB_ASSERT(state);
      61             :   } else {
      62           0 :     state = setDataShaping(RADIOLIB_SHAPING_NONE);
      63           0 :     RADIOLIB_ASSERT(state);
      64             :   }
      65             : 
      66             :   // set publicly accessible settings that are not a part of begin method
      67           0 :   state = setCRC(true);
      68           0 :   return(state);
      69             : }
      70             : 
      71           1 : int16_t SX1277::setFrequency(uint32_t 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 >= RADIOLIB_UNIT_MEGA(137)) && (freq <= RADIOLIB_UNIT_MEGA(175))) ||
      77           0 :        ((freq >= RADIOLIB_UNIT_MEGA(395)) && (freq <= RADIOLIB_UNIT_MEGA(525))) ||
      78           0 :        ((freq >= RADIOLIB_UNIT_MEGA(862)) && (freq <= RADIOLIB_UNIT_MEGA(1020))))) {
      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           2 : int16_t SX1277::setSpreadingFactor(uint8_t sf) {
      91             :   uint8_t newSpreadingFactor;
      92             : 
      93             :   // check allowed spreading factor values
      94           2 :   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           2 :     default:
     108           2 :       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::checkDataRate(DataRate_t dr, ModemType_t modem) {
     121           1 :   int16_t state = RADIOLIB_ERR_UNKNOWN;
     122             : 
     123             :   // retrieve modem if not supplied
     124           1 :   if(modem == RADIOLIB_MODEM_NONE) {
     125           1 :     state = this->getModem(&modem);
     126           1 :     RADIOLIB_ASSERT(state);
     127             :   }
     128             :   
     129             :   // select interpretation based on modem
     130           1 :   if(modem == RADIOLIB_MODEM_FSK) {
     131           0 :     RADIOLIB_CHECK_RANGE(dr.fsk.bitRate, 500, RADIOLIB_UNIT_KILO(300), RADIOLIB_ERR_INVALID_BIT_RATE);
     132           0 :     if(!((dr.fsk.freqDev + dr.fsk.bitRate/2 <= RADIOLIB_UNIT_KILO(250)) && (dr.fsk.freqDev <= RADIOLIB_UNIT_KILO(200)))) {
     133           0 :       return(RADIOLIB_ERR_INVALID_FREQUENCY_DEVIATION);
     134             :     }
     135           0 :     return(RADIOLIB_ERR_NONE);
     136             : 
     137           1 :   } else if(modem == RADIOLIB_MODEM_LORA) {
     138           1 :     RADIOLIB_CHECK_RANGE(dr.lora.spreadingFactor, 6, 9, RADIOLIB_ERR_INVALID_SPREADING_FACTOR);
     139           0 :     RADIOLIB_CHECK_RANGE(dr.lora.bandwidth, 7800, RADIOLIB_UNIT_KILO(500), RADIOLIB_ERR_INVALID_BANDWIDTH);
     140           0 :     RADIOLIB_CHECK_RANGE(dr.lora.codingRate, 4, 8, RADIOLIB_ERR_INVALID_CODING_RATE);
     141           0 :     return(RADIOLIB_ERR_NONE);
     142             :   
     143             :   }
     144             : 
     145           0 :   return(state);
     146             : }
     147             : 
     148           1 : int16_t SX1277::setModem(ModemType_t modem) {
     149           1 :   switch(modem) {
     150           0 :     case(ModemType_t::RADIOLIB_MODEM_LORA): {
     151           0 :       ConfigLoRa_t cfg;
     152           0 :       return(this->begin(cfg));
     153             :     } break;
     154           0 :     case(ModemType_t::RADIOLIB_MODEM_FSK): {
     155           0 :       ConfigFSK_t cfg;
     156           0 :       return(this->beginFSK(cfg));
     157             :     } break;
     158           1 :     default:
     159           1 :       return(RADIOLIB_ERR_WRONG_MODEM);
     160             :   }
     161             : }
     162             : 
     163             : #endif

Generated by: LCOV version 1.14