sx126x.c 19 KB

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