Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

821 lines
23 KiB

  1. /*!
  2. * \file sx126x.c
  3. *
  4. * \brief SX126x driver implementation
  5. *
  6. * \copyright Revised BSD License, see section \ref LICENSE.
  7. *
  8. * \code
  9. * ______ _
  10. * / _____) _ | |
  11. * ( (____ _____ ____ _| |_ _____ ____| |__
  12. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  13. * _____) ) ____| | | || |_| ____( (___| | | |
  14. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  15. * (C)2013-2017 Semtech
  16. *
  17. * \endcode
  18. *
  19. * \author Miguel Luis ( Semtech )
  20. *
  21. * \author Gregory Cristian ( Semtech )
  22. */
  23. #include <string.h>
  24. #include "utilities.h"
  25. #include "timer.h"
  26. #include "radio.h"
  27. #include "delay.h"
  28. #include "sx126x.h"
  29. #include "sx126x-board.h"
  30. /*!
  31. * \brief Internal frequency of the radio
  32. */
  33. #define SX126X_XTAL_FREQ 32000000UL
  34. /*!
  35. * \brief Scaling factor used to perform fixed-point operations
  36. */
  37. #define SX126X_PLL_STEP_SHIFT_AMOUNT ( 14 )
  38. /*!
  39. * \brief PLL step - scaled with SX126X_PLL_STEP_SHIFT_AMOUNT
  40. */
  41. #define SX126X_PLL_STEP_SCALED ( SX126X_XTAL_FREQ >> ( 25 - SX126X_PLL_STEP_SHIFT_AMOUNT ) )
  42. /*!
  43. * \brief Maximum value for parameter symbNum in \ref SX126xSetLoRaSymbNumTimeout
  44. */
  45. #define SX126X_MAX_LORA_SYMB_NUM_TIMEOUT 248
  46. /*!
  47. * \brief Radio registers definition
  48. */
  49. typedef struct
  50. {
  51. uint16_t Addr; //!< The address of the register
  52. uint8_t Value; //!< The value of the register
  53. }RadioRegisters_t;
  54. /*!
  55. * \brief Stores the current packet type set in the radio
  56. */
  57. static RadioPacketTypes_t PacketType;
  58. /*!
  59. * \brief Stores the current packet header type set in the radio
  60. */
  61. static volatile RadioLoRaPacketLengthsMode_t LoRaHeaderType;
  62. /*!
  63. * \brief Stores the last frequency error measured on LoRa received packet
  64. */
  65. volatile uint32_t FrequencyError = 0;
  66. /*!
  67. * \brief Hold the status of the Image calibration
  68. */
  69. static bool ImageCalibrated = false;
  70. /*!
  71. * \brief Get the number of PLL steps for a given frequency in Hertz
  72. *
  73. * \param [in] freqInHz Frequency in Hertz
  74. *
  75. * \returns Number of PLL steps
  76. */
  77. static uint32_t SX126xConvertFreqInHzToPllStep( uint32_t freqInHz );
  78. /*
  79. * SX126x DIO IRQ callback functions prototype
  80. */
  81. /*!
  82. * \brief DIO 0 IRQ callback
  83. */
  84. void SX126xOnDioIrq( void );
  85. /*!
  86. * \brief DIO 0 IRQ callback
  87. */
  88. void SX126xSetPollingMode( void );
  89. /*!
  90. * \brief DIO 0 IRQ callback
  91. */
  92. void SX126xSetInterruptMode( void );
  93. /*
  94. * \brief Process the IRQ if handled by the driver
  95. */
  96. void SX126xProcessIrqs( void );
  97. void SX126xInit( DioIrqHandler dioIrq )
  98. {
  99. SX126xReset( );
  100. SX126xIoIrqInit( dioIrq );
  101. SX126xWakeup( );
  102. SX126xSetStandby( STDBY_RC );
  103. // Initialize TCXO control
  104. SX126xIoTcxoInit( );
  105. // Initialize RF switch control
  106. SX126xIoRfSwitchInit( );
  107. // Force image calibration
  108. ImageCalibrated = false;
  109. SX126xSetOperatingMode( MODE_STDBY_RC );
  110. }
  111. void SX126xCheckDeviceReady( void )
  112. {
  113. if( ( SX126xGetOperatingMode( ) == MODE_SLEEP ) || ( SX126xGetOperatingMode( ) == MODE_RX_DC ) )
  114. {
  115. SX126xWakeup( );
  116. // Switch is turned off when device is in sleep mode and turned on is all other modes
  117. SX126xAntSwOn( );
  118. }
  119. SX126xWaitOnBusy( );
  120. }
  121. void SX126xSetPayload( uint8_t *payload, uint8_t size )
  122. {
  123. SX126xWriteBuffer( 0x00, payload, size );
  124. }
  125. uint8_t SX126xGetPayload( uint8_t *buffer, uint8_t *size, uint8_t maxSize )
  126. {
  127. uint8_t offset = 0;
  128. SX126xGetRxBufferStatus( size, &offset );
  129. if( *size > maxSize )
  130. {
  131. return 1;
  132. }
  133. SX126xReadBuffer( offset, buffer, *size );
  134. return 0;
  135. }
  136. void SX126xSendPayload( uint8_t *payload, uint8_t size, uint32_t timeout )
  137. {
  138. SX126xSetPayload( payload, size );
  139. SX126xSetTx( timeout );
  140. }
  141. uint8_t SX126xSetSyncWord( uint8_t *syncWord )
  142. {
  143. SX126xWriteRegisters( REG_LR_SYNCWORDBASEADDRESS, syncWord, 8 );
  144. return 0;
  145. }
  146. void SX126xSetCrcSeed( uint16_t seed )
  147. {
  148. uint8_t buf[2];
  149. buf[0] = ( uint8_t )( ( seed >> 8 ) & 0xFF );
  150. buf[1] = ( uint8_t )( seed & 0xFF );
  151. switch( SX126xGetPacketType( ) )
  152. {
  153. case PACKET_TYPE_GFSK:
  154. SX126xWriteRegisters( REG_LR_CRCSEEDBASEADDR, buf, 2 );
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. void SX126xSetCrcPolynomial( uint16_t polynomial )
  161. {
  162. uint8_t buf[2];
  163. buf[0] = ( uint8_t )( ( polynomial >> 8 ) & 0xFF );
  164. buf[1] = ( uint8_t )( polynomial & 0xFF );
  165. switch( SX126xGetPacketType( ) )
  166. {
  167. case PACKET_TYPE_GFSK:
  168. SX126xWriteRegisters( REG_LR_CRCPOLYBASEADDR, buf, 2 );
  169. break;
  170. default:
  171. break;
  172. }
  173. }
  174. void SX126xSetWhiteningSeed( uint16_t seed )
  175. {
  176. uint8_t regValue = 0;
  177. switch( SX126xGetPacketType( ) )
  178. {
  179. case PACKET_TYPE_GFSK:
  180. regValue = SX126xReadRegister( REG_LR_WHITSEEDBASEADDR_MSB ) & 0xFE;
  181. regValue = ( ( seed >> 8 ) & 0x01 ) | regValue;
  182. SX126xWriteRegister( REG_LR_WHITSEEDBASEADDR_MSB, regValue ); // only 1 bit.
  183. SX126xWriteRegister( REG_LR_WHITSEEDBASEADDR_LSB, ( uint8_t )seed );
  184. break;
  185. default:
  186. break;
  187. }
  188. }
  189. uint32_t SX126xGetRandom( void )
  190. {
  191. uint32_t number = 0;
  192. uint8_t regAnaLna = 0;
  193. uint8_t regAnaMixer = 0;
  194. regAnaLna = SX126xReadRegister( REG_ANA_LNA );
  195. SX126xWriteRegister( REG_ANA_LNA, regAnaLna & ~( 1 << 0 ) );
  196. regAnaMixer = SX126xReadRegister( REG_ANA_MIXER );
  197. SX126xWriteRegister( REG_ANA_MIXER, regAnaMixer & ~( 1 << 7 ) );
  198. // Set radio in continuous reception
  199. SX126xSetRx( 0xFFFFFF ); // Rx Continuous
  200. SX126xReadRegisters( RANDOM_NUMBER_GENERATORBASEADDR, ( uint8_t* )&number, 4 );
  201. SX126xSetStandby( STDBY_RC );
  202. SX126xWriteRegister( REG_ANA_LNA, regAnaLna );
  203. SX126xWriteRegister( REG_ANA_MIXER, regAnaMixer );
  204. return number;
  205. }
  206. void SX126xSetSleep( SleepParams_t sleepConfig )
  207. {
  208. SX126xAntSwOff( );
  209. uint8_t value = ( ( ( uint8_t )sleepConfig.Fields.WarmStart << 2 ) |
  210. ( ( uint8_t )sleepConfig.Fields.Reset << 1 ) |
  211. ( ( uint8_t )sleepConfig.Fields.WakeUpRTC ) );
  212. if( sleepConfig.Fields.WarmStart == 0 )
  213. {
  214. // Force image calibration
  215. ImageCalibrated = false;
  216. }
  217. SX126xWriteCommand( RADIO_SET_SLEEP, &value, 1 );
  218. SX126xSetOperatingMode( MODE_SLEEP );
  219. }
  220. void SX126xSetStandby( RadioStandbyModes_t standbyConfig )
  221. {
  222. SX126xWriteCommand( RADIO_SET_STANDBY, ( uint8_t* )&standbyConfig, 1 );
  223. if( standbyConfig == STDBY_RC )
  224. {
  225. SX126xSetOperatingMode( MODE_STDBY_RC );
  226. }
  227. else
  228. {
  229. SX126xSetOperatingMode( MODE_STDBY_XOSC );
  230. }
  231. }
  232. void SX126xSetFs( void )
  233. {
  234. SX126xWriteCommand( RADIO_SET_FS, 0, 0 );
  235. SX126xSetOperatingMode( MODE_FS );
  236. }
  237. void SX126xSetTx( uint32_t timeout )
  238. {
  239. uint8_t buf[3];
  240. SX126xSetOperatingMode( MODE_TX );
  241. buf[0] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
  242. buf[1] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
  243. buf[2] = ( uint8_t )( timeout & 0xFF );
  244. SX126xWriteCommand( RADIO_SET_TX, buf, 3 );
  245. }
  246. void SX126xSetRx( uint32_t timeout )
  247. {
  248. uint8_t buf[3];
  249. SX126xSetOperatingMode( MODE_RX );
  250. SX126xWriteRegister( REG_RX_GAIN, 0x94 ); // default gain
  251. buf[0] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
  252. buf[1] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
  253. buf[2] = ( uint8_t )( timeout & 0xFF );
  254. SX126xWriteCommand( RADIO_SET_RX, buf, 3 );
  255. }
  256. void SX126xSetRxBoosted( uint32_t timeout )
  257. {
  258. uint8_t buf[3];
  259. SX126xSetOperatingMode( MODE_RX );
  260. SX126xWriteRegister( REG_RX_GAIN, 0x96 ); // max LNA gain, increase current by ~2mA for around ~3dB in sensitivity
  261. buf[0] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
  262. buf[1] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
  263. buf[2] = ( uint8_t )( timeout & 0xFF );
  264. SX126xWriteCommand( RADIO_SET_RX, buf, 3 );
  265. }
  266. void SX126xSetRxDutyCycle( uint32_t rxTime, uint32_t sleepTime )
  267. {
  268. uint8_t buf[6];
  269. buf[0] = ( uint8_t )( ( rxTime >> 16 ) & 0xFF );
  270. buf[1] = ( uint8_t )( ( rxTime >> 8 ) & 0xFF );
  271. buf[2] = ( uint8_t )( rxTime & 0xFF );
  272. buf[3] = ( uint8_t )( ( sleepTime >> 16 ) & 0xFF );
  273. buf[4] = ( uint8_t )( ( sleepTime >> 8 ) & 0xFF );
  274. buf[5] = ( uint8_t )( sleepTime & 0xFF );
  275. SX126xWriteCommand( RADIO_SET_RXDUTYCYCLE, buf, 6 );
  276. SX126xSetOperatingMode( MODE_RX_DC );
  277. }
  278. void SX126xSetCad( void )
  279. {
  280. SX126xWriteCommand( RADIO_SET_CAD, 0, 0 );
  281. SX126xSetOperatingMode( MODE_CAD );
  282. }
  283. void SX126xSetTxContinuousWave( void )
  284. {
  285. SX126xWriteCommand( RADIO_SET_TXCONTINUOUSWAVE, 0, 0 );
  286. SX126xSetOperatingMode( MODE_TX );
  287. }
  288. void SX126xSetTxInfinitePreamble( void )
  289. {
  290. SX126xWriteCommand( RADIO_SET_TXCONTINUOUSPREAMBLE, 0, 0 );
  291. SX126xSetOperatingMode( MODE_TX );
  292. }
  293. void SX126xSetStopRxTimerOnPreambleDetect( bool enable )
  294. {
  295. SX126xWriteCommand( RADIO_SET_STOPRXTIMERONPREAMBLE, ( uint8_t* )&enable, 1 );
  296. }
  297. void SX126xSetLoRaSymbNumTimeout( uint8_t symbNum )
  298. {
  299. uint8_t mant = ( ( ( symbNum > SX126X_MAX_LORA_SYMB_NUM_TIMEOUT ) ?
  300. SX126X_MAX_LORA_SYMB_NUM_TIMEOUT :
  301. symbNum ) + 1 ) >> 1;
  302. uint8_t exp = 0;
  303. uint8_t reg = 0;
  304. while( mant > 31 )
  305. {
  306. mant = ( mant + 3 ) >> 2;
  307. exp++;
  308. }
  309. reg = mant << ( 2 * exp + 1 );
  310. SX126xWriteCommand( RADIO_SET_LORASYMBTIMEOUT, &reg, 1 );
  311. if( symbNum != 0 )
  312. {
  313. reg = exp + ( mant << 3 );
  314. SX126xWriteRegister( REG_LR_SYNCH_TIMEOUT, reg );
  315. }
  316. }
  317. void SX126xSetRegulatorMode( RadioRegulatorMode_t mode )
  318. {
  319. SX126xWriteCommand( RADIO_SET_REGULATORMODE, ( uint8_t* )&mode, 1 );
  320. }
  321. void SX126xCalibrate( CalibrationParams_t calibParam )
  322. {
  323. uint8_t value = ( ( ( uint8_t )calibParam.Fields.ImgEnable << 6 ) |
  324. ( ( uint8_t )calibParam.Fields.ADCBulkPEnable << 5 ) |
  325. ( ( uint8_t )calibParam.Fields.ADCBulkNEnable << 4 ) |
  326. ( ( uint8_t )calibParam.Fields.ADCPulseEnable << 3 ) |
  327. ( ( uint8_t )calibParam.Fields.PLLEnable << 2 ) |
  328. ( ( uint8_t )calibParam.Fields.RC13MEnable << 1 ) |
  329. ( ( uint8_t )calibParam.Fields.RC64KEnable ) );
  330. SX126xWriteCommand( RADIO_CALIBRATE, &value, 1 );
  331. }
  332. void SX126xCalibrateImage( uint32_t freq )
  333. {
  334. uint8_t calFreq[2];
  335. if( freq > 900000000 )
  336. {
  337. calFreq[0] = 0xE1;
  338. calFreq[1] = 0xE9;
  339. }
  340. else if( freq > 850000000 )
  341. {
  342. calFreq[0] = 0xD7;
  343. calFreq[1] = 0xDB;
  344. }
  345. else if( freq > 770000000 )
  346. {
  347. calFreq[0] = 0xC1;
  348. calFreq[1] = 0xC5;
  349. }
  350. else if( freq > 460000000 )
  351. {
  352. calFreq[0] = 0x75;
  353. calFreq[1] = 0x81;
  354. }
  355. else if( freq > 425000000 )
  356. {
  357. calFreq[0] = 0x6B;
  358. calFreq[1] = 0x6F;
  359. }
  360. SX126xWriteCommand( RADIO_CALIBRATEIMAGE, calFreq, 2 );
  361. }
  362. void SX126xSetPaConfig( uint8_t paDutyCycle, uint8_t hpMax, uint8_t deviceSel, uint8_t paLut )
  363. {
  364. uint8_t buf[4];
  365. buf[0] = paDutyCycle;
  366. buf[1] = hpMax;
  367. buf[2] = deviceSel;
  368. buf[3] = paLut;
  369. SX126xWriteCommand( RADIO_SET_PACONFIG, buf, 4 );
  370. }
  371. void SX126xSetRxTxFallbackMode( uint8_t fallbackMode )
  372. {
  373. SX126xWriteCommand( RADIO_SET_TXFALLBACKMODE, &fallbackMode, 1 );
  374. }
  375. void SX126xSetDioIrqParams( uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask )
  376. {
  377. uint8_t buf[8];
  378. buf[0] = ( uint8_t )( ( irqMask >> 8 ) & 0x00FF );
  379. buf[1] = ( uint8_t )( irqMask & 0x00FF );
  380. buf[2] = ( uint8_t )( ( dio1Mask >> 8 ) & 0x00FF );
  381. buf[3] = ( uint8_t )( dio1Mask & 0x00FF );
  382. buf[4] = ( uint8_t )( ( dio2Mask >> 8 ) & 0x00FF );
  383. buf[5] = ( uint8_t )( dio2Mask & 0x00FF );
  384. buf[6] = ( uint8_t )( ( dio3Mask >> 8 ) & 0x00FF );
  385. buf[7] = ( uint8_t )( dio3Mask & 0x00FF );
  386. SX126xWriteCommand( RADIO_CFG_DIOIRQ, buf, 8 );
  387. }
  388. uint16_t SX126xGetIrqStatus( void )
  389. {
  390. uint8_t irqStatus[2];
  391. SX126xReadCommand( RADIO_GET_IRQSTATUS, irqStatus, 2 );
  392. return ( irqStatus[0] << 8 ) | irqStatus[1];
  393. }
  394. void SX126xSetDio2AsRfSwitchCtrl( uint8_t enable )
  395. {
  396. SX126xWriteCommand( RADIO_SET_RFSWITCHMODE, &enable, 1 );
  397. }
  398. void SX126xSetDio3AsTcxoCtrl( RadioTcxoCtrlVoltage_t tcxoVoltage, uint32_t timeout )
  399. {
  400. uint8_t buf[4];
  401. buf[0] = tcxoVoltage & 0x07;
  402. buf[1] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
  403. buf[2] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
  404. buf[3] = ( uint8_t )( timeout & 0xFF );
  405. SX126xWriteCommand( RADIO_SET_TCXOMODE, buf, 4 );
  406. }
  407. void SX126xSetRfFrequency( uint32_t frequency )
  408. {
  409. uint8_t buf[4];
  410. if( ImageCalibrated == false )
  411. {
  412. SX126xCalibrateImage( frequency );
  413. ImageCalibrated = true;
  414. }
  415. uint32_t freqInPllSteps = SX126xConvertFreqInHzToPllStep( frequency );
  416. buf[0] = ( uint8_t )( ( freqInPllSteps >> 24 ) & 0xFF );
  417. buf[1] = ( uint8_t )( ( freqInPllSteps >> 16 ) & 0xFF );
  418. buf[2] = ( uint8_t )( ( freqInPllSteps >> 8 ) & 0xFF );
  419. buf[3] = ( uint8_t )( freqInPllSteps & 0xFF );
  420. SX126xWriteCommand( RADIO_SET_RFFREQUENCY, buf, 4 );
  421. }
  422. void SX126xSetPacketType( RadioPacketTypes_t packetType )
  423. {
  424. // Save packet type internally to avoid questioning the radio
  425. PacketType = packetType;
  426. SX126xWriteCommand( RADIO_SET_PACKETTYPE, ( uint8_t* )&packetType, 1 );
  427. }
  428. RadioPacketTypes_t SX126xGetPacketType( void )
  429. {
  430. return PacketType;
  431. }
  432. void SX126xSetTxParams( int8_t power, RadioRampTimes_t rampTime )
  433. {
  434. uint8_t buf[2];
  435. if( SX126xGetDeviceId( ) == SX1261 )
  436. {
  437. if( power == 15 )
  438. {
  439. SX126xSetPaConfig( 0x06, 0x00, 0x01, 0x01 );
  440. }
  441. else
  442. {
  443. SX126xSetPaConfig( 0x04, 0x00, 0x01, 0x01 );
  444. }
  445. if( power >= 14 )
  446. {
  447. power = 14;
  448. }
  449. else if( power < -17 )
  450. {
  451. power = -17;
  452. }
  453. }
  454. else // sx1262
  455. {
  456. // WORKAROUND - Better Resistance of the SX1262 Tx to Antenna Mismatch, see DS_SX1261-2_V1.2 datasheet chapter 15.2
  457. SX126xWriteRegister( REG_TX_CLAMP_CFG, SX126xReadRegister( REG_TX_CLAMP_CFG ) | ( 0x0F << 1 ) );
  458. // WORKAROUND END
  459. SX126xSetPaConfig( 0x04, 0x07, 0x00, 0x01 );
  460. if( power > 22 )
  461. {
  462. power = 22;
  463. }
  464. else if( power < -9 )
  465. {
  466. power = -9;
  467. }
  468. }
  469. buf[0] = power;
  470. buf[1] = ( uint8_t )rampTime;
  471. SX126xWriteCommand( RADIO_SET_TXPARAMS, buf, 2 );
  472. }
  473. void SX126xSetModulationParams( ModulationParams_t *modulationParams )
  474. {
  475. uint8_t n;
  476. uint32_t tempVal = 0;
  477. uint8_t buf[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  478. // Check if required configuration corresponds to the stored packet type
  479. // If not, silently update radio packet type
  480. if( PacketType != modulationParams->PacketType )
  481. {
  482. SX126xSetPacketType( modulationParams->PacketType );
  483. }
  484. switch( modulationParams->PacketType )
  485. {
  486. case PACKET_TYPE_GFSK:
  487. n = 8;
  488. tempVal = ( uint32_t )( 32 * SX126X_XTAL_FREQ / modulationParams->Params.Gfsk.BitRate );
  489. buf[0] = ( tempVal >> 16 ) & 0xFF;
  490. buf[1] = ( tempVal >> 8 ) & 0xFF;
  491. buf[2] = tempVal & 0xFF;
  492. buf[3] = modulationParams->Params.Gfsk.ModulationShaping;
  493. buf[4] = modulationParams->Params.Gfsk.Bandwidth;
  494. tempVal = SX126xConvertFreqInHzToPllStep( modulationParams->Params.Gfsk.Fdev );
  495. buf[5] = ( tempVal >> 16 ) & 0xFF;
  496. buf[6] = ( tempVal >> 8 ) & 0xFF;
  497. buf[7] = ( tempVal& 0xFF );
  498. SX126xWriteCommand( RADIO_SET_MODULATIONPARAMS, buf, n );
  499. break;
  500. case PACKET_TYPE_LORA:
  501. n = 4;
  502. buf[0] = modulationParams->Params.LoRa.SpreadingFactor;
  503. buf[1] = modulationParams->Params.LoRa.Bandwidth;
  504. buf[2] = modulationParams->Params.LoRa.CodingRate;
  505. buf[3] = modulationParams->Params.LoRa.LowDatarateOptimize;
  506. SX126xWriteCommand( RADIO_SET_MODULATIONPARAMS, buf, n );
  507. break;
  508. default:
  509. case PACKET_TYPE_NONE:
  510. return;
  511. }
  512. }
  513. void SX126xSetPacketParams( PacketParams_t *packetParams )
  514. {
  515. uint8_t n;
  516. uint8_t crcVal = 0;
  517. uint8_t buf[9] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  518. // Check if required configuration corresponds to the stored packet type
  519. // If not, silently update radio packet type
  520. if( PacketType != packetParams->PacketType )
  521. {
  522. SX126xSetPacketType( packetParams->PacketType );
  523. }
  524. switch( packetParams->PacketType )
  525. {
  526. case PACKET_TYPE_GFSK:
  527. if( packetParams->Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES_IBM )
  528. {
  529. SX126xSetCrcSeed( CRC_IBM_SEED );
  530. SX126xSetCrcPolynomial( CRC_POLYNOMIAL_IBM );
  531. crcVal = RADIO_CRC_2_BYTES;
  532. }
  533. else if( packetParams->Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES_CCIT )
  534. {
  535. SX126xSetCrcSeed( CRC_CCITT_SEED );
  536. SX126xSetCrcPolynomial( CRC_POLYNOMIAL_CCITT );
  537. crcVal = RADIO_CRC_2_BYTES_INV;
  538. }
  539. else
  540. {
  541. crcVal = packetParams->Params.Gfsk.CrcLength;
  542. }
  543. n = 9;
  544. buf[0] = ( packetParams->Params.Gfsk.PreambleLength >> 8 ) & 0xFF;
  545. buf[1] = packetParams->Params.Gfsk.PreambleLength;
  546. buf[2] = packetParams->Params.Gfsk.PreambleMinDetect;
  547. buf[3] = ( packetParams->Params.Gfsk.SyncWordLength /*<< 3*/ ); // convert from byte to bit
  548. buf[4] = packetParams->Params.Gfsk.AddrComp;
  549. buf[5] = packetParams->Params.Gfsk.HeaderType;
  550. buf[6] = packetParams->Params.Gfsk.PayloadLength;
  551. buf[7] = crcVal;
  552. buf[8] = packetParams->Params.Gfsk.DcFree;
  553. break;
  554. case PACKET_TYPE_LORA:
  555. n = 6;
  556. buf[0] = ( packetParams->Params.LoRa.PreambleLength >> 8 ) & 0xFF;
  557. buf[1] = packetParams->Params.LoRa.PreambleLength;
  558. buf[2] = LoRaHeaderType = packetParams->Params.LoRa.HeaderType;
  559. buf[3] = packetParams->Params.LoRa.PayloadLength;
  560. buf[4] = packetParams->Params.LoRa.CrcMode;
  561. buf[5] = packetParams->Params.LoRa.InvertIQ;
  562. break;
  563. default:
  564. case PACKET_TYPE_NONE:
  565. return;
  566. }
  567. SX126xWriteCommand( RADIO_SET_PACKETPARAMS, buf, n );
  568. }
  569. void SX126xSetCadParams( RadioLoRaCadSymbols_t cadSymbolNum, uint8_t cadDetPeak, uint8_t cadDetMin, RadioCadExitModes_t cadExitMode, uint32_t cadTimeout )
  570. {
  571. uint8_t buf[7];
  572. buf[0] = ( uint8_t )cadSymbolNum;
  573. buf[1] = cadDetPeak;
  574. buf[2] = cadDetMin;
  575. buf[3] = ( uint8_t )cadExitMode;
  576. buf[4] = ( uint8_t )( ( cadTimeout >> 16 ) & 0xFF );
  577. buf[5] = ( uint8_t )( ( cadTimeout >> 8 ) & 0xFF );
  578. buf[6] = ( uint8_t )( cadTimeout & 0xFF );
  579. SX126xWriteCommand( RADIO_SET_CADPARAMS, buf, 7 );
  580. SX126xSetOperatingMode( MODE_CAD );
  581. }
  582. void SX126xSetBufferBaseAddress( uint8_t txBaseAddress, uint8_t rxBaseAddress )
  583. {
  584. uint8_t buf[2];
  585. buf[0] = txBaseAddress;
  586. buf[1] = rxBaseAddress;
  587. SX126xWriteCommand( RADIO_SET_BUFFERBASEADDRESS, buf, 2 );
  588. }
  589. RadioStatus_t SX126xGetStatus( void )
  590. {
  591. uint8_t stat = 0;
  592. RadioStatus_t status = { .Value = 0 };
  593. stat = SX126xReadCommand( RADIO_GET_STATUS, NULL, 0 );
  594. status.Fields.CmdStatus = ( stat & ( 0x07 << 1 ) ) >> 1;
  595. status.Fields.ChipMode = ( stat & ( 0x07 << 4 ) ) >> 4;
  596. return status;
  597. }
  598. int8_t SX126xGetRssiInst( void )
  599. {
  600. uint8_t buf[1];
  601. int8_t rssi = 0;
  602. SX126xReadCommand( RADIO_GET_RSSIINST, buf, 1 );
  603. rssi = -buf[0] >> 1;
  604. return rssi;
  605. }
  606. void SX126xGetRxBufferStatus( uint8_t *payloadLength, uint8_t *rxStartBufferPointer )
  607. {
  608. uint8_t status[2];
  609. SX126xReadCommand( RADIO_GET_RXBUFFERSTATUS, status, 2 );
  610. // In case of LORA fixed header, the payloadLength is obtained by reading
  611. // the register REG_LR_PAYLOADLENGTH
  612. if( ( SX126xGetPacketType( ) == PACKET_TYPE_LORA ) && ( LoRaHeaderType == LORA_PACKET_FIXED_LENGTH ) )
  613. {
  614. *payloadLength = SX126xReadRegister( REG_LR_PAYLOADLENGTH );
  615. }
  616. else
  617. {
  618. *payloadLength = status[0];
  619. }
  620. *rxStartBufferPointer = status[1];
  621. }
  622. void SX126xGetPacketStatus( PacketStatus_t *pktStatus )
  623. {
  624. uint8_t status[3];
  625. SX126xReadCommand( RADIO_GET_PACKETSTATUS, status, 3 );
  626. pktStatus->packetType = SX126xGetPacketType( );
  627. switch( pktStatus->packetType )
  628. {
  629. case PACKET_TYPE_GFSK:
  630. pktStatus->Params.Gfsk.RxStatus = status[0];
  631. pktStatus->Params.Gfsk.RssiSync = -status[1] >> 1;
  632. pktStatus->Params.Gfsk.RssiAvg = -status[2] >> 1;
  633. pktStatus->Params.Gfsk.FreqError = 0;
  634. break;
  635. case PACKET_TYPE_LORA:
  636. pktStatus->Params.LoRa.RssiPkt = -status[0] >> 1;
  637. // Returns SNR value [dB] rounded to the nearest integer value
  638. pktStatus->Params.LoRa.SnrPkt = ( ( ( int8_t )status[1] ) + 2 ) >> 2;
  639. pktStatus->Params.LoRa.SignalRssiPkt = -status[2] >> 1;
  640. pktStatus->Params.LoRa.FreqError = FrequencyError;
  641. break;
  642. default:
  643. case PACKET_TYPE_NONE:
  644. // In that specific case, we set everything in the pktStatus to zeros
  645. // and reset the packet type accordingly
  646. memset( pktStatus, 0, sizeof( PacketStatus_t ) );
  647. pktStatus->packetType = PACKET_TYPE_NONE;
  648. break;
  649. }
  650. }
  651. RadioError_t SX126xGetDeviceErrors( void )
  652. {
  653. uint8_t err[] = { 0, 0 };
  654. RadioError_t error = { .Value = 0 };
  655. SX126xReadCommand( RADIO_GET_ERROR, ( uint8_t* )err, 2 );
  656. error.Fields.PaRamp = ( err[0] & ( 1 << 0 ) ) >> 0;
  657. error.Fields.PllLock = ( err[1] & ( 1 << 6 ) ) >> 6;
  658. error.Fields.XoscStart = ( err[1] & ( 1 << 5 ) ) >> 5;
  659. error.Fields.ImgCalib = ( err[1] & ( 1 << 4 ) ) >> 4;
  660. error.Fields.AdcCalib = ( err[1] & ( 1 << 3 ) ) >> 3;
  661. error.Fields.PllCalib = ( err[1] & ( 1 << 2 ) ) >> 2;
  662. error.Fields.Rc13mCalib = ( err[1] & ( 1 << 1 ) ) >> 1;
  663. error.Fields.Rc64kCalib = ( err[1] & ( 1 << 0 ) ) >> 0;
  664. return error;
  665. }
  666. void SX126xClearDeviceErrors( void )
  667. {
  668. uint8_t buf[2] = { 0x00, 0x00 };
  669. SX126xWriteCommand( RADIO_CLR_ERROR, buf, 2 );
  670. }
  671. void SX126xClearIrqStatus( uint16_t irq )
  672. {
  673. uint8_t buf[2];
  674. buf[0] = ( uint8_t )( ( ( uint16_t )irq >> 8 ) & 0x00FF );
  675. buf[1] = ( uint8_t )( ( uint16_t )irq & 0x00FF );
  676. SX126xWriteCommand( RADIO_CLR_IRQSTATUS, buf, 2 );
  677. }
  678. static uint32_t SX126xConvertFreqInHzToPllStep( uint32_t freqInHz )
  679. {
  680. uint32_t stepsInt;
  681. uint32_t stepsFrac;
  682. // pllSteps = freqInHz / (SX126X_XTAL_FREQ / 2^19 )
  683. // Get integer and fractional parts of the frequency computed with a PLL step scaled value
  684. stepsInt = freqInHz / SX126X_PLL_STEP_SCALED;
  685. stepsFrac = freqInHz - ( stepsInt * SX126X_PLL_STEP_SCALED );
  686. // Apply the scaling factor to retrieve a frequency in Hz (+ ceiling)
  687. return ( stepsInt << SX126X_PLL_STEP_SHIFT_AMOUNT ) +
  688. ( ( ( stepsFrac << SX126X_PLL_STEP_SHIFT_AMOUNT ) + ( SX126X_PLL_STEP_SCALED >> 1 ) ) /
  689. SX126X_PLL_STEP_SCALED );
  690. }