sx126x.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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 <math.h>
  24. #include <string.h>
  25. #include "sx126x.h"
  26. #include "sx126x-board.h"
  27. /*!
  28. * \brief Radio registers definition
  29. */
  30. typedef struct
  31. {
  32. uint16_t Addr; //!< The address of the register
  33. uint8_t Value; //!< The value of the register
  34. }RadioRegisters_t;
  35. /*!
  36. * \brief Holds the internal operating mode of the radio
  37. */
  38. static RadioOperatingModes_t OperatingMode;
  39. /*!
  40. * \brief Stores the current packet type set in the radio
  41. */
  42. static RadioPacketTypes_t PacketType;
  43. /*!
  44. * \brief Stores the last frequency error measured on LoRa received packet
  45. */
  46. volatile uint32_t FrequencyError = 0;
  47. /*!
  48. * \brief Hold the status of the Image calibration
  49. */
  50. static bool ImageCalibrated = false;
  51. static bool isTxcoModule = false;
  52. /*
  53. * SX126x DIO IRQ callback functions prototype
  54. */
  55. /*!
  56. * \brief DIO 0 IRQ callback
  57. */
  58. void SX126xOnDioIrq( void );
  59. /*!
  60. * \brief DIO 0 IRQ callback
  61. */
  62. void SX126xSetPollingMode( void );
  63. /*!
  64. * \brief DIO 0 IRQ callback
  65. */
  66. void SX126xSetInterruptMode( void );
  67. /*
  68. * \brief Process the IRQ if handled by the driver
  69. */
  70. void SX126xProcessIrqs( void );
  71. void SX126xEnableTxco(bool sta)
  72. {
  73. isTxcoModule = sta;
  74. }
  75. void SX126xInit( DioIrqHandler dioIrq )
  76. {
  77. ImageCalibrated = false;
  78. CalibrationParams_t calibParam;
  79. SX126xReset( );
  80. SX126xWakeup( );
  81. if (isTxcoModule)
  82. {
  83. SX126xSetStandby( STDBY_XOSC );
  84. OperatingMode = MODE_STDBY_XOSC;
  85. SX126xSetDio3AsTcxoCtrl( TCXO_CTRL_3_3V, 2000/15 ); // convert from ms to SX126x time base
  86. calibParam.Value = 0x7F;
  87. SX126xCalibrate( calibParam );
  88. }
  89. else
  90. {
  91. SX126xSetStandby( STDBY_RC );
  92. OperatingMode = MODE_STDBY_RC;
  93. }
  94. SX126xSetDio2AsRfSwitchCtrl( true );
  95. }
  96. RadioOperatingModes_t SX126xGetOperatingMode( void )
  97. {
  98. return OperatingMode;
  99. }
  100. void SX126xCheckDeviceReady( void )
  101. {
  102. if( ( SX126xGetOperatingMode( ) == MODE_SLEEP ) || ( SX126xGetOperatingMode( ) == MODE_RX_DC ) )
  103. {
  104. SX126xWakeup( );
  105. // Switch is turned off when device is in sleep mode and turned on is all other modes
  106. SX126xAntSwOn( );
  107. }
  108. SX126xWaitOnBusy( );
  109. }
  110. void SX126xSetPayload( uint8_t *payload, uint8_t size )
  111. {
  112. SX126xWriteBuffer( 0x00, payload, size );
  113. }
  114. uint8_t SX126xGetPayload( uint8_t *buffer, uint8_t *size, uint8_t maxSize )
  115. {
  116. uint8_t offset = 0;
  117. SX126xGetRxBufferStatus( size, &offset );
  118. if( *size > maxSize )
  119. {
  120. return 1;
  121. }
  122. SX126xReadBuffer( offset, buffer, *size );
  123. return 0;
  124. }
  125. void SX126xSendPayload( uint8_t *payload, uint8_t size, uint32_t timeout )
  126. {
  127. SX126xSetPayload( payload, size );
  128. SX126xSetTx( timeout );
  129. }
  130. uint8_t SX126xSetSyncWord( uint8_t *syncWord )
  131. {
  132. SX126xWriteRegisters( REG_LR_SYNCWORDBASEADDRESS, syncWord, 8 );
  133. return 0;
  134. }
  135. void SX126xSetCrcSeed( uint16_t seed )
  136. {
  137. uint8_t buf[2];
  138. buf[0] = ( uint8_t )( ( seed >> 8 ) & 0xFF );
  139. buf[1] = ( uint8_t )( seed & 0xFF );
  140. switch( SX126xGetPacketType( ) )
  141. {
  142. case PACKET_TYPE_GFSK:
  143. SX126xWriteRegisters( REG_LR_CRCSEEDBASEADDR, buf, 2 );
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. void SX126xSetCrcPolynomial( uint16_t polynomial )
  150. {
  151. uint8_t buf[2];
  152. buf[0] = ( uint8_t )( ( polynomial >> 8 ) & 0xFF );
  153. buf[1] = ( uint8_t )( polynomial & 0xFF );
  154. switch( SX126xGetPacketType( ) )
  155. {
  156. case PACKET_TYPE_GFSK:
  157. SX126xWriteRegisters( REG_LR_CRCPOLYBASEADDR, buf, 2 );
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. void SX126xSetWhiteningSeed( uint16_t seed )
  164. {
  165. uint8_t regValue = 0;
  166. switch( SX126xGetPacketType( ) )
  167. {
  168. case PACKET_TYPE_GFSK:
  169. regValue = SX126xReadRegister( REG_LR_WHITSEEDBASEADDR_MSB ) & 0xFE;
  170. regValue = ( ( seed >> 8 ) & 0x01 ) | regValue;
  171. SX126xWriteRegister( REG_LR_WHITSEEDBASEADDR_MSB, regValue ); // only 1 bit.
  172. SX126xWriteRegister( REG_LR_WHITSEEDBASEADDR_LSB, ( uint8_t )seed );
  173. break;
  174. default:
  175. break;
  176. }
  177. }
  178. uint32_t SX126xGetRandom( void )
  179. {
  180. uint8_t buf[] = { 0, 0, 0, 0 };
  181. // Set radio in continuous reception
  182. SX126xSetRx( 0 );
  183. HAL_Delay_nMS( 1 );
  184. SX126xReadRegisters( RANDOM_NUMBER_GENERATORBASEADDR, buf, 4 );
  185. SX126xSetStandby( STDBY_RC );
  186. return ( buf[0] << 24 ) | ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3];
  187. }
  188. void SX126xSetSleep( SleepParams_t sleepConfig )
  189. {
  190. SX126xAntSwOff( );
  191. SX126xWriteCommand( RADIO_SET_SLEEP, &sleepConfig.Value, 1 );
  192. OperatingMode = MODE_SLEEP;
  193. }
  194. void SX126xSetStandby( RadioStandbyModes_t standbyConfig )
  195. {
  196. SX126xWriteCommand( RADIO_SET_STANDBY, ( uint8_t* )&standbyConfig, 1 );
  197. if( standbyConfig == STDBY_RC )
  198. {
  199. OperatingMode = MODE_STDBY_RC;
  200. }
  201. else
  202. {
  203. OperatingMode = MODE_STDBY_XOSC;
  204. }
  205. }
  206. void SX126xSetFs( void )
  207. {
  208. SX126xWriteCommand( RADIO_SET_FS, 0, 0 );
  209. OperatingMode = MODE_FS;
  210. }
  211. void SX126xSetTx( uint32_t timeout )
  212. {
  213. uint8_t buf[3];
  214. OperatingMode = MODE_TX;
  215. buf[0] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
  216. buf[1] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
  217. buf[2] = ( uint8_t )( timeout & 0xFF );
  218. SX126xWriteCommand( RADIO_SET_TX, buf, 3 );
  219. }
  220. void SX126xSetRx( uint32_t timeout )
  221. {
  222. uint8_t buf[3];
  223. OperatingMode = MODE_RX;
  224. buf[0] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
  225. buf[1] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
  226. buf[2] = ( uint8_t )( timeout & 0xFF );
  227. SX126xWriteCommand( RADIO_SET_RX, buf, 3 );
  228. }
  229. void SX126xSetRxBoosted( uint32_t timeout )
  230. {
  231. uint8_t buf[3];
  232. OperatingMode = MODE_RX;
  233. SX126xWriteRegister( REG_RX_GAIN, 0x96 ); // max LNA gain, increase current by ~2mA for around ~3dB in sensivity
  234. buf[0] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
  235. buf[1] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
  236. buf[2] = ( uint8_t )( timeout & 0xFF );
  237. SX126xWriteCommand( RADIO_SET_RX, buf, 3 );
  238. }
  239. void SX126xSetRxDutyCycle( uint32_t rxTime, uint32_t sleepTime )
  240. {
  241. uint8_t buf[6];
  242. buf[0] = ( uint8_t )( ( rxTime >> 16 ) & 0xFF );
  243. buf[1] = ( uint8_t )( ( rxTime >> 8 ) & 0xFF );
  244. buf[2] = ( uint8_t )( rxTime & 0xFF );
  245. buf[3] = ( uint8_t )( ( sleepTime >> 16 ) & 0xFF );
  246. buf[4] = ( uint8_t )( ( sleepTime >> 8 ) & 0xFF );
  247. buf[5] = ( uint8_t )( sleepTime & 0xFF );
  248. SX126xWriteCommand( RADIO_SET_RXDUTYCYCLE, buf, 6 );
  249. OperatingMode = MODE_RX_DC;
  250. }
  251. void SX126xSetCad( void )
  252. {
  253. SX126xWriteCommand( RADIO_SET_CAD, 0, 0 );
  254. OperatingMode = MODE_CAD;
  255. }
  256. void SX126xSetTxContinuousWave( void )
  257. {
  258. SX126xWriteCommand( RADIO_SET_TXCONTINUOUSWAVE, 0, 0 );
  259. }
  260. void SX126xSetTxInfinitePreamble( void )
  261. {
  262. SX126xWriteCommand( RADIO_SET_TXCONTINUOUSPREAMBLE, 0, 0 );
  263. }
  264. void SX126xSetStopRxTimerOnPreambleDetect( bool enable )
  265. {
  266. SX126xWriteCommand( RADIO_SET_STOPRXTIMERONPREAMBLE, ( uint8_t* )&enable, 1 );
  267. }
  268. void SX126xSetLoRaSymbNumTimeout( uint8_t SymbNum )
  269. {
  270. SX126xWriteCommand( RADIO_SET_LORASYMBTIMEOUT, &SymbNum, 1 );
  271. }
  272. void SX126xSetRegulatorMode( RadioRegulatorMode_t mode )
  273. {
  274. SX126xWriteCommand( RADIO_SET_REGULATORMODE, ( uint8_t* )&mode, 1 );
  275. }
  276. void SX126xCalibrate( CalibrationParams_t calibParam )
  277. {
  278. SX126xWriteCommand( RADIO_CALIBRATE, ( uint8_t* )&calibParam, 1 );
  279. }
  280. void SX126xCalibrateImage( uint32_t freq )
  281. {
  282. uint8_t calFreq[2];
  283. if( freq > 900000000 )
  284. {
  285. calFreq[0] = 0xE1;
  286. calFreq[1] = 0xE9;
  287. }
  288. else if( freq > 850000000 )
  289. {
  290. calFreq[0] = 0xD7;
  291. calFreq[1] = 0xD8;
  292. }
  293. else if( freq > 770000000 )
  294. {
  295. calFreq[0] = 0xC1;
  296. calFreq[1] = 0xC5;
  297. }
  298. else if( freq > 460000000 )
  299. {
  300. calFreq[0] = 0x75;
  301. calFreq[1] = 0x81;
  302. }
  303. else if( freq > 425000000 )
  304. {
  305. calFreq[0] = 0x6B;
  306. calFreq[1] = 0x6F;
  307. }
  308. SX126xWriteCommand( RADIO_CALIBRATEIMAGE, calFreq, 2 );
  309. }
  310. void SX126xSetPaConfig( uint8_t paDutyCycle, uint8_t hpMax, uint8_t deviceSel, uint8_t paLut )
  311. {
  312. uint8_t buf[4];
  313. buf[0] = paDutyCycle;
  314. buf[1] = hpMax;
  315. buf[2] = deviceSel;
  316. buf[3] = paLut;
  317. SX126xWriteCommand( RADIO_SET_PACONFIG, buf, 4 );
  318. }
  319. void SX126xSetRxTxFallbackMode( uint8_t fallbackMode )
  320. {
  321. SX126xWriteCommand( RADIO_SET_TXFALLBACKMODE, &fallbackMode, 1 );
  322. }
  323. void SX126xSetDioIrqParams( uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask )
  324. {
  325. uint8_t buf[8];
  326. buf[0] = ( uint8_t )( ( irqMask >> 8 ) & 0x00FF );
  327. buf[1] = ( uint8_t )( irqMask & 0x00FF );
  328. buf[2] = ( uint8_t )( ( dio1Mask >> 8 ) & 0x00FF );
  329. buf[3] = ( uint8_t )( dio1Mask & 0x00FF );
  330. buf[4] = ( uint8_t )( ( dio2Mask >> 8 ) & 0x00FF );
  331. buf[5] = ( uint8_t )( dio2Mask & 0x00FF );
  332. buf[6] = ( uint8_t )( ( dio3Mask >> 8 ) & 0x00FF );
  333. buf[7] = ( uint8_t )( dio3Mask & 0x00FF );
  334. SX126xWriteCommand( RADIO_CFG_DIOIRQ, buf, 8 );
  335. }
  336. uint16_t SX126xGetIrqStatus( void )
  337. {
  338. uint8_t irqStatus[2];
  339. SX126xReadCommand( RADIO_GET_IRQSTATUS, irqStatus, 2 );
  340. return ( irqStatus[0] << 8 ) | irqStatus[1];
  341. }
  342. void SX126xSetDio2AsRfSwitchCtrl( uint8_t enable )
  343. {
  344. SX126xWriteCommand( RADIO_SET_RFSWITCHMODE, &enable, 1 );
  345. }
  346. void SX126xSetDio3AsTcxoCtrl( RadioTcxoCtrlVoltage_t tcxoVoltage, uint32_t timeout )
  347. {
  348. uint8_t buf[4];
  349. buf[0] = tcxoVoltage & 0x07;
  350. buf[1] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
  351. buf[2] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
  352. buf[3] = ( uint8_t )( timeout & 0xFF );
  353. SX126xWriteCommand( RADIO_SET_TCXOMODE, buf, 4 );
  354. }
  355. void SX126xSetRfFrequency( uint32_t frequency )
  356. {
  357. uint8_t buf[4];
  358. uint32_t freq = 0;
  359. if( ImageCalibrated == false )
  360. {
  361. SX126xCalibrateImage( frequency );
  362. ImageCalibrated = true;
  363. }
  364. freq = ( uint32_t )( ( double )frequency / ( double )FREQ_STEP );
  365. buf[0] = ( uint8_t )( ( freq >> 24 ) & 0xFF );
  366. buf[1] = ( uint8_t )( ( freq >> 16 ) & 0xFF );
  367. buf[2] = ( uint8_t )( ( freq >> 8 ) & 0xFF );
  368. buf[3] = ( uint8_t )( freq & 0xFF );
  369. SX126xWriteCommand( RADIO_SET_RFFREQUENCY, buf, 4 );
  370. }
  371. void SX126xSetPacketType( RadioPacketTypes_t packetType )
  372. {
  373. // Save packet type internally to avoid questioning the radio
  374. PacketType = packetType;
  375. SX126xWriteCommand( RADIO_SET_PACKETTYPE, ( uint8_t* )&packetType, 1 );
  376. }
  377. RadioPacketTypes_t SX126xGetPacketType( void )
  378. {
  379. return PacketType;
  380. }
  381. void SX126xSetTxParams( int8_t power, RadioRampTimes_t rampTime )
  382. {
  383. uint8_t buf[2];
  384. if( SX126xGetPaSelect( 0 ) == SX1261 )
  385. {
  386. if( power == 15 )
  387. {
  388. SX126xSetPaConfig( 0x06, 0x00, 0x01, 0x01 );
  389. }
  390. else
  391. {
  392. SX126xSetPaConfig( 0x04, 0x00, 0x01, 0x01 );
  393. }
  394. if( power >= 14 )
  395. {
  396. power = 14;
  397. }
  398. else if( power < -3 )
  399. {
  400. power = -3;
  401. }
  402. SX126xWriteRegister( REG_OCP, 0x18 ); // current max is 80 mA for the whole device
  403. }
  404. else // sx1262
  405. {
  406. SX126xSetPaConfig( 0x04, 0x07, 0x00, 0x01 );
  407. if( power > 22 )
  408. {
  409. power = 22;
  410. }
  411. else if( power < -3 )
  412. {
  413. power = -3;
  414. }
  415. SX126xWriteRegister( REG_OCP, 0x38 ); // current max 160mA for the whole device
  416. }
  417. buf[0] = power;
  418. buf[1] = ( uint8_t )rampTime;
  419. SX126xWriteCommand( RADIO_SET_TXPARAMS, buf, 2 );
  420. }
  421. void SX126xSetModulationParams( ModulationParams_t *modulationParams )
  422. {
  423. uint8_t n;
  424. uint32_t tempVal = 0;
  425. uint8_t buf[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  426. // Check if required configuration corresponds to the stored packet type
  427. // If not, silently update radio packet type
  428. if( PacketType != modulationParams->PacketType )
  429. {
  430. SX126xSetPacketType( modulationParams->PacketType );
  431. }
  432. switch( modulationParams->PacketType )
  433. {
  434. case PACKET_TYPE_GFSK:
  435. n = 8;
  436. tempVal = ( uint32_t )( 32 * ( ( double )XTAL_FREQ / ( double )modulationParams->Params.Gfsk.BitRate ) );
  437. buf[0] = ( tempVal >> 16 ) & 0xFF;
  438. buf[1] = ( tempVal >> 8 ) & 0xFF;
  439. buf[2] = tempVal & 0xFF;
  440. buf[3] = modulationParams->Params.Gfsk.ModulationShaping;
  441. buf[4] = modulationParams->Params.Gfsk.Bandwidth;
  442. tempVal = ( uint32_t )( ( double )modulationParams->Params.Gfsk.Fdev / ( double )FREQ_STEP );
  443. buf[5] = ( tempVal >> 16 ) & 0xFF;
  444. buf[6] = ( tempVal >> 8 ) & 0xFF;
  445. buf[7] = ( tempVal& 0xFF );
  446. SX126xWriteCommand( RADIO_SET_MODULATIONPARAMS, buf, n );
  447. break;
  448. case PACKET_TYPE_LORA:
  449. n = 4;
  450. buf[0] = modulationParams->Params.LoRa.SpreadingFactor;
  451. buf[1] = modulationParams->Params.LoRa.Bandwidth;
  452. buf[2] = modulationParams->Params.LoRa.CodingRate;
  453. buf[3] = modulationParams->Params.LoRa.LowDatarateOptimize;
  454. SX126xWriteCommand( RADIO_SET_MODULATIONPARAMS, buf, n );
  455. break;
  456. default:
  457. case PACKET_TYPE_NONE:
  458. return;
  459. }
  460. }
  461. void SX126xSetPacketParams( PacketParams_t *packetParams )
  462. {
  463. uint8_t n;
  464. uint8_t crcVal = 0;
  465. uint8_t buf[9] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  466. // Check if required configuration corresponds to the stored packet type
  467. // If not, silently update radio packet type
  468. if( PacketType != packetParams->PacketType )
  469. {
  470. SX126xSetPacketType( packetParams->PacketType );
  471. }
  472. switch( packetParams->PacketType )
  473. {
  474. case PACKET_TYPE_GFSK:
  475. if( packetParams->Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES_IBM )
  476. {
  477. SX126xSetCrcSeed( CRC_IBM_SEED );
  478. SX126xSetCrcPolynomial( CRC_POLYNOMIAL_IBM );
  479. crcVal = RADIO_CRC_2_BYTES;
  480. }
  481. else if( packetParams->Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES_CCIT )
  482. {
  483. SX126xSetCrcSeed( CRC_CCITT_SEED );
  484. SX126xSetCrcPolynomial( CRC_POLYNOMIAL_CCITT );
  485. crcVal = RADIO_CRC_2_BYTES_INV;
  486. }
  487. else
  488. {
  489. crcVal = packetParams->Params.Gfsk.CrcLength;
  490. }
  491. n = 9;
  492. buf[0] = ( packetParams->Params.Gfsk.PreambleLength >> 8 ) & 0xFF;
  493. buf[1] = packetParams->Params.Gfsk.PreambleLength;
  494. buf[2] = packetParams->Params.Gfsk.PreambleMinDetect;
  495. buf[3] = ( packetParams->Params.Gfsk.SyncWordLength /*<< 3*/ ); // convert from byte to bit
  496. buf[4] = packetParams->Params.Gfsk.AddrComp;
  497. buf[5] = packetParams->Params.Gfsk.HeaderType;
  498. buf[6] = packetParams->Params.Gfsk.PayloadLength;
  499. buf[7] = crcVal;
  500. buf[8] = packetParams->Params.Gfsk.DcFree;
  501. break;
  502. case PACKET_TYPE_LORA:
  503. n = 6;
  504. buf[0] = ( packetParams->Params.LoRa.PreambleLength >> 8 ) & 0xFF;
  505. buf[1] = packetParams->Params.LoRa.PreambleLength;
  506. buf[2] = packetParams->Params.LoRa.HeaderType;
  507. buf[3] = packetParams->Params.LoRa.PayloadLength;
  508. buf[4] = packetParams->Params.LoRa.CrcMode;
  509. buf[5] = packetParams->Params.LoRa.InvertIQ;
  510. break;
  511. default:
  512. case PACKET_TYPE_NONE:
  513. return;
  514. }
  515. SX126xWriteCommand( RADIO_SET_PACKETPARAMS, buf, n );
  516. }
  517. void SX126xSetCadParams( RadioLoRaCadSymbols_t cadSymbolNum, uint8_t cadDetPeak, uint8_t cadDetMin, RadioCadExitModes_t cadExitMode, uint32_t cadTimeout )
  518. {
  519. uint8_t buf[7];
  520. buf[0] = ( uint8_t )cadSymbolNum;
  521. buf[1] = cadDetPeak;
  522. buf[2] = cadDetMin;
  523. buf[3] = ( uint8_t )cadExitMode;
  524. buf[4] = ( uint8_t )( ( cadTimeout >> 16 ) & 0xFF );
  525. buf[5] = ( uint8_t )( ( cadTimeout >> 8 ) & 0xFF );
  526. buf[6] = ( uint8_t )( cadTimeout & 0xFF );
  527. SX126xWriteCommand( RADIO_SET_CADPARAMS, buf, 5 );
  528. OperatingMode = MODE_CAD;
  529. }
  530. void SX126xSetBufferBaseAddress( uint8_t txBaseAddress, uint8_t rxBaseAddress )
  531. {
  532. uint8_t buf[2];
  533. buf[0] = txBaseAddress;
  534. buf[1] = rxBaseAddress;
  535. SX126xWriteCommand( RADIO_SET_BUFFERBASEADDRESS, buf, 2 );
  536. }
  537. RadioStatus_t SX126xGetStatus( void )
  538. {
  539. uint8_t stat = 0;
  540. RadioStatus_t status;
  541. SX126xReadCommand( RADIO_GET_STATUS, ( uint8_t * )&stat, 1 );
  542. status.Value = stat;
  543. return status;
  544. }
  545. int8_t SX126xGetRssiInst( void )
  546. {
  547. uint8_t buf[1];
  548. int8_t rssi = 0;
  549. SX126xReadCommand( RADIO_GET_RSSIINST, buf, 1 );
  550. rssi = -buf[0] >> 1;
  551. return rssi;
  552. }
  553. void SX126xGetRxBufferStatus( uint8_t *payloadLength, uint8_t *rxStartBufferPointer )
  554. {
  555. uint8_t status[2];
  556. SX126xReadCommand( RADIO_GET_RXBUFFERSTATUS, status, 2 );
  557. // In case of LORA fixed header, the payloadLength is obtained by reading
  558. // the register REG_LR_PAYLOADLENGTH
  559. if( ( SX126xGetPacketType( ) == PACKET_TYPE_LORA ) && ( SX126xReadRegister( REG_LR_PACKETPARAMS ) >> 7 == 1 ) )
  560. {
  561. *payloadLength = SX126xReadRegister( REG_LR_PAYLOADLENGTH );
  562. }
  563. else
  564. {
  565. *payloadLength = status[0];
  566. }
  567. *rxStartBufferPointer = status[1];
  568. }
  569. void SX126xGetPacketStatus( PacketStatus_t *pktStatus )
  570. {
  571. uint8_t status[3];
  572. SX126xReadCommand( RADIO_GET_PACKETSTATUS, status, 3 );
  573. pktStatus->packetType = SX126xGetPacketType( );
  574. switch( pktStatus->packetType )
  575. {
  576. case PACKET_TYPE_GFSK:
  577. pktStatus->Params.Gfsk.RxStatus = status[0];
  578. pktStatus->Params.Gfsk.RssiSync = -status[1] >> 1;
  579. pktStatus->Params.Gfsk.RssiAvg = -status[2] >> 1;
  580. pktStatus->Params.Gfsk.FreqError = 0;
  581. break;
  582. case PACKET_TYPE_LORA:
  583. pktStatus->Params.LoRa.RssiPkt = -status[0] >> 1;
  584. ( status[1] < 128 ) ? ( pktStatus->Params.LoRa.SnrPkt = status[1] >> 2 ) : ( pktStatus->Params.LoRa.SnrPkt = ( ( status[1] - 256 ) >> 2 ) );
  585. pktStatus->Params.LoRa.SignalRssiPkt = -status[2] >> 1;
  586. pktStatus->Params.LoRa.FreqError = FrequencyError;
  587. break;
  588. default:
  589. case PACKET_TYPE_NONE:
  590. // In that specific case, we set everything in the pktStatus to zeros
  591. // and reset the packet type accordingly
  592. memset( pktStatus, 0, sizeof( PacketStatus_t ) );
  593. pktStatus->packetType = PACKET_TYPE_NONE;
  594. break;
  595. }
  596. }
  597. RadioError_t SX126xGetDeviceErrors( void )
  598. {
  599. RadioError_t error;
  600. SX126xReadCommand( RADIO_GET_ERROR, ( uint8_t * )&error, 2 );
  601. return error;
  602. }
  603. void SX126xClearDeviceErrors( void )
  604. {
  605. uint8_t buf[2] = { 0x00, 0x00 };
  606. SX126xWriteCommand( RADIO_CLR_ERROR, buf, 2 );
  607. }
  608. void SX126xClearIrqStatus( uint16_t irq )
  609. {
  610. uint8_t buf[2];
  611. buf[0] = ( uint8_t )( ( ( uint16_t )irq >> 8 ) & 0x00FF );
  612. buf[1] = ( uint8_t )( ( uint16_t )irq & 0x00FF );
  613. SX126xWriteCommand( RADIO_CLR_IRQSTATUS, buf, 2 );
  614. }