sx126x.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. else
  309. {
  310. calFreq[0] = 0x6B;
  311. calFreq[1] = 0x6F;
  312. }
  313. SX126xWriteCommand( RADIO_CALIBRATEIMAGE, calFreq, 2 );
  314. }
  315. void SX126xSetPaConfig( uint8_t paDutyCycle, uint8_t hpMax, uint8_t deviceSel, uint8_t paLut )
  316. {
  317. uint8_t buf[4];
  318. buf[0] = paDutyCycle;
  319. buf[1] = hpMax;
  320. buf[2] = deviceSel;
  321. buf[3] = paLut;
  322. SX126xWriteCommand( RADIO_SET_PACONFIG, buf, 4 );
  323. }
  324. void SX126xSetRxTxFallbackMode( uint8_t fallbackMode )
  325. {
  326. SX126xWriteCommand( RADIO_SET_TXFALLBACKMODE, &fallbackMode, 1 );
  327. }
  328. void SX126xSetDioIrqParams( uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask )
  329. {
  330. uint8_t buf[8];
  331. buf[0] = ( uint8_t )( ( irqMask >> 8 ) & 0x00FF );
  332. buf[1] = ( uint8_t )( irqMask & 0x00FF );
  333. buf[2] = ( uint8_t )( ( dio1Mask >> 8 ) & 0x00FF );
  334. buf[3] = ( uint8_t )( dio1Mask & 0x00FF );
  335. buf[4] = ( uint8_t )( ( dio2Mask >> 8 ) & 0x00FF );
  336. buf[5] = ( uint8_t )( dio2Mask & 0x00FF );
  337. buf[6] = ( uint8_t )( ( dio3Mask >> 8 ) & 0x00FF );
  338. buf[7] = ( uint8_t )( dio3Mask & 0x00FF );
  339. SX126xWriteCommand( RADIO_CFG_DIOIRQ, buf, 8 );
  340. }
  341. uint16_t SX126xGetIrqStatus( void )
  342. {
  343. uint8_t irqStatus[2];
  344. SX126xReadCommand( RADIO_GET_IRQSTATUS, irqStatus, 2 );
  345. return ( irqStatus[0] << 8 ) | irqStatus[1];
  346. }
  347. void SX126xSetDio2AsRfSwitchCtrl( uint8_t enable )
  348. {
  349. SX126xWriteCommand( RADIO_SET_RFSWITCHMODE, &enable, 1 );
  350. }
  351. void SX126xSetDio3AsTcxoCtrl( RadioTcxoCtrlVoltage_t tcxoVoltage, uint32_t timeout )
  352. {
  353. uint8_t buf[4];
  354. buf[0] = tcxoVoltage & 0x07;
  355. buf[1] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
  356. buf[2] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
  357. buf[3] = ( uint8_t )( timeout & 0xFF );
  358. SX126xWriteCommand( RADIO_SET_TCXOMODE, buf, 4 );
  359. }
  360. void SX126xSetRfFrequency( uint32_t frequency )
  361. {
  362. uint8_t buf[4];
  363. uint32_t freq = 0;
  364. if( ImageCalibrated == false )
  365. {
  366. SX126xCalibrateImage( frequency );
  367. ImageCalibrated = true;
  368. }
  369. freq = ( uint32_t )( ( double )frequency / ( double )FREQ_STEP );
  370. buf[0] = ( uint8_t )( ( freq >> 24 ) & 0xFF );
  371. buf[1] = ( uint8_t )( ( freq >> 16 ) & 0xFF );
  372. buf[2] = ( uint8_t )( ( freq >> 8 ) & 0xFF );
  373. buf[3] = ( uint8_t )( freq & 0xFF );
  374. SX126xWriteCommand( RADIO_SET_RFFREQUENCY, buf, 4 );
  375. }
  376. void SX126xSetPacketType( RadioPacketTypes_t packetType )
  377. {
  378. // Save packet type internally to avoid questioning the radio
  379. PacketType = packetType;
  380. SX126xWriteCommand( RADIO_SET_PACKETTYPE, ( uint8_t* )&packetType, 1 );
  381. }
  382. RadioPacketTypes_t SX126xGetPacketType( void )
  383. {
  384. return PacketType;
  385. }
  386. void SX126xSetTxParams( int8_t power, RadioRampTimes_t rampTime )
  387. {
  388. uint8_t buf[2];
  389. if( SX126xGetPaSelect( 0 ) == SX1261 )
  390. {
  391. if( power == 15 )
  392. {
  393. SX126xSetPaConfig( 0x06, 0x00, 0x01, 0x01 );
  394. }
  395. else
  396. {
  397. SX126xSetPaConfig( 0x04, 0x00, 0x01, 0x01 );
  398. }
  399. if( power >= 14 )
  400. {
  401. power = 14;
  402. }
  403. else if( power < -3 )
  404. {
  405. power = -3;
  406. }
  407. SX126xWriteRegister( REG_OCP, 0x18 ); // current max is 80 mA for the whole device
  408. }
  409. else // sx1262
  410. {
  411. SX126xSetPaConfig( 0x04, 0x07, 0x00, 0x01 );
  412. if( power > 22 )
  413. {
  414. power = 22;
  415. }
  416. else if( power < -3 )
  417. {
  418. power = -3;
  419. }
  420. SX126xWriteRegister( REG_OCP, 0x38 ); // current max 160mA for the whole device
  421. }
  422. buf[0] = power;
  423. buf[1] = ( uint8_t )rampTime;
  424. SX126xWriteCommand( RADIO_SET_TXPARAMS, buf, 2 );
  425. }
  426. void SX126xSetModulationParams( ModulationParams_t *modulationParams )
  427. {
  428. uint8_t n;
  429. uint32_t tempVal = 0;
  430. uint8_t buf[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  431. // Check if required configuration corresponds to the stored packet type
  432. // If not, silently update radio packet type
  433. if( PacketType != modulationParams->PacketType )
  434. {
  435. SX126xSetPacketType( modulationParams->PacketType );
  436. }
  437. switch( modulationParams->PacketType )
  438. {
  439. case PACKET_TYPE_GFSK:
  440. n = 8;
  441. tempVal = ( uint32_t )( 32 * ( ( double )XTAL_FREQ / ( double )modulationParams->Params.Gfsk.BitRate ) );
  442. buf[0] = ( tempVal >> 16 ) & 0xFF;
  443. buf[1] = ( tempVal >> 8 ) & 0xFF;
  444. buf[2] = tempVal & 0xFF;
  445. buf[3] = modulationParams->Params.Gfsk.ModulationShaping;
  446. buf[4] = modulationParams->Params.Gfsk.Bandwidth;
  447. tempVal = ( uint32_t )( ( double )modulationParams->Params.Gfsk.Fdev / ( double )FREQ_STEP );
  448. buf[5] = ( tempVal >> 16 ) & 0xFF;
  449. buf[6] = ( tempVal >> 8 ) & 0xFF;
  450. buf[7] = ( tempVal& 0xFF );
  451. SX126xWriteCommand( RADIO_SET_MODULATIONPARAMS, buf, n );
  452. break;
  453. case PACKET_TYPE_LORA:
  454. n = 4;
  455. buf[0] = modulationParams->Params.LoRa.SpreadingFactor;
  456. buf[1] = modulationParams->Params.LoRa.Bandwidth;
  457. buf[2] = modulationParams->Params.LoRa.CodingRate;
  458. buf[3] = modulationParams->Params.LoRa.LowDatarateOptimize;
  459. SX126xWriteCommand( RADIO_SET_MODULATIONPARAMS, buf, n );
  460. break;
  461. default:
  462. case PACKET_TYPE_NONE:
  463. return;
  464. }
  465. }
  466. void SX126xSetPacketParams( PacketParams_t *packetParams )
  467. {
  468. uint8_t n;
  469. uint8_t crcVal = 0;
  470. uint8_t buf[9] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  471. // Check if required configuration corresponds to the stored packet type
  472. // If not, silently update radio packet type
  473. if( PacketType != packetParams->PacketType )
  474. {
  475. SX126xSetPacketType( packetParams->PacketType );
  476. }
  477. switch( packetParams->PacketType )
  478. {
  479. case PACKET_TYPE_GFSK:
  480. if( packetParams->Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES_IBM )
  481. {
  482. SX126xSetCrcSeed( CRC_IBM_SEED );
  483. SX126xSetCrcPolynomial( CRC_POLYNOMIAL_IBM );
  484. crcVal = RADIO_CRC_2_BYTES;
  485. }
  486. else if( packetParams->Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES_CCIT )
  487. {
  488. SX126xSetCrcSeed( CRC_CCITT_SEED );
  489. SX126xSetCrcPolynomial( CRC_POLYNOMIAL_CCITT );
  490. crcVal = RADIO_CRC_2_BYTES_INV;
  491. }
  492. else
  493. {
  494. crcVal = packetParams->Params.Gfsk.CrcLength;
  495. }
  496. n = 9;
  497. buf[0] = ( packetParams->Params.Gfsk.PreambleLength >> 8 ) & 0xFF;
  498. buf[1] = packetParams->Params.Gfsk.PreambleLength;
  499. buf[2] = packetParams->Params.Gfsk.PreambleMinDetect;
  500. buf[3] = ( packetParams->Params.Gfsk.SyncWordLength /*<< 3*/ ); // convert from byte to bit
  501. buf[4] = packetParams->Params.Gfsk.AddrComp;
  502. buf[5] = packetParams->Params.Gfsk.HeaderType;
  503. buf[6] = packetParams->Params.Gfsk.PayloadLength;
  504. buf[7] = crcVal;
  505. buf[8] = packetParams->Params.Gfsk.DcFree;
  506. break;
  507. case PACKET_TYPE_LORA:
  508. n = 6;
  509. buf[0] = ( packetParams->Params.LoRa.PreambleLength >> 8 ) & 0xFF;
  510. buf[1] = packetParams->Params.LoRa.PreambleLength;
  511. buf[2] = packetParams->Params.LoRa.HeaderType;
  512. buf[3] = packetParams->Params.LoRa.PayloadLength;
  513. buf[4] = packetParams->Params.LoRa.CrcMode;
  514. buf[5] = packetParams->Params.LoRa.InvertIQ;
  515. break;
  516. default:
  517. case PACKET_TYPE_NONE:
  518. return;
  519. }
  520. SX126xWriteCommand( RADIO_SET_PACKETPARAMS, buf, n );
  521. }
  522. void SX126xSetCadParams( RadioLoRaCadSymbols_t cadSymbolNum, uint8_t cadDetPeak, uint8_t cadDetMin, RadioCadExitModes_t cadExitMode, uint32_t cadTimeout )
  523. {
  524. uint8_t buf[7];
  525. buf[0] = ( uint8_t )cadSymbolNum;
  526. buf[1] = cadDetPeak;
  527. buf[2] = cadDetMin;
  528. buf[3] = ( uint8_t )cadExitMode;
  529. buf[4] = ( uint8_t )( ( cadTimeout >> 16 ) & 0xFF );
  530. buf[5] = ( uint8_t )( ( cadTimeout >> 8 ) & 0xFF );
  531. buf[6] = ( uint8_t )( cadTimeout & 0xFF );
  532. SX126xWriteCommand( RADIO_SET_CADPARAMS, buf, 5 );
  533. OperatingMode = MODE_CAD;
  534. }
  535. void SX126xSetBufferBaseAddress( uint8_t txBaseAddress, uint8_t rxBaseAddress )
  536. {
  537. uint8_t buf[2];
  538. buf[0] = txBaseAddress;
  539. buf[1] = rxBaseAddress;
  540. SX126xWriteCommand( RADIO_SET_BUFFERBASEADDRESS, buf, 2 );
  541. }
  542. RadioStatus_t SX126xGetStatus( void )
  543. {
  544. uint8_t stat = 0;
  545. RadioStatus_t status;
  546. SX126xReadCommand( RADIO_GET_STATUS, ( uint8_t * )&stat, 1 );
  547. status.Value = stat;
  548. return status;
  549. }
  550. int8_t SX126xGetRssiInst( void )
  551. {
  552. uint8_t buf[1];
  553. int8_t rssi = 0;
  554. SX126xReadCommand( RADIO_GET_RSSIINST, buf, 1 );
  555. rssi = -buf[0] >> 1;
  556. return rssi;
  557. }
  558. void SX126xGetRxBufferStatus( uint8_t *payloadLength, uint8_t *rxStartBufferPointer )
  559. {
  560. uint8_t status[2];
  561. SX126xReadCommand( RADIO_GET_RXBUFFERSTATUS, status, 2 );
  562. // In case of LORA fixed header, the payloadLength is obtained by reading
  563. // the register REG_LR_PAYLOADLENGTH
  564. if( ( SX126xGetPacketType( ) == PACKET_TYPE_LORA ) && ( SX126xReadRegister( REG_LR_PACKETPARAMS ) >> 7 == 1 ) )
  565. {
  566. *payloadLength = SX126xReadRegister( REG_LR_PAYLOADLENGTH );
  567. }
  568. else
  569. {
  570. *payloadLength = status[0];
  571. }
  572. *rxStartBufferPointer = status[1];
  573. }
  574. void SX126xGetPacketStatus( PacketStatus_t *pktStatus )
  575. {
  576. uint8_t status[3];
  577. SX126xReadCommand( RADIO_GET_PACKETSTATUS, status, 3 );
  578. pktStatus->packetType = SX126xGetPacketType( );
  579. switch( pktStatus->packetType )
  580. {
  581. case PACKET_TYPE_GFSK:
  582. pktStatus->Params.Gfsk.RxStatus = status[0];
  583. pktStatus->Params.Gfsk.RssiSync = -status[1] >> 1;
  584. pktStatus->Params.Gfsk.RssiAvg = -status[2] >> 1;
  585. pktStatus->Params.Gfsk.FreqError = 0;
  586. break;
  587. case PACKET_TYPE_LORA:
  588. pktStatus->Params.LoRa.RssiPkt = -status[0] >> 1;
  589. ( status[1] < 128 ) ? ( pktStatus->Params.LoRa.SnrPkt = status[1] >> 2 ) : ( pktStatus->Params.LoRa.SnrPkt = ( ( status[1] - 256 ) >> 2 ) );
  590. pktStatus->Params.LoRa.SignalRssiPkt = -status[2] >> 1;
  591. pktStatus->Params.LoRa.FreqError = FrequencyError;
  592. break;
  593. default:
  594. case PACKET_TYPE_NONE:
  595. // In that specific case, we set everything in the pktStatus to zeros
  596. // and reset the packet type accordingly
  597. memset( pktStatus, 0, sizeof( PacketStatus_t ) );
  598. pktStatus->packetType = PACKET_TYPE_NONE;
  599. break;
  600. }
  601. }
  602. RadioError_t SX126xGetDeviceErrors( void )
  603. {
  604. RadioError_t error;
  605. SX126xReadCommand( RADIO_GET_ERROR, ( uint8_t * )&error, 2 );
  606. return error;
  607. }
  608. void SX126xClearDeviceErrors( void )
  609. {
  610. uint8_t buf[2] = { 0x00, 0x00 };
  611. SX126xWriteCommand( RADIO_CLR_ERROR, buf, 2 );
  612. }
  613. void SX126xClearIrqStatus( uint16_t irq )
  614. {
  615. uint8_t buf[2];
  616. buf[0] = ( uint8_t )( ( ( uint16_t )irq >> 8 ) & 0x00FF );
  617. buf[1] = ( uint8_t )( ( uint16_t )irq & 0x00FF );
  618. SX126xWriteCommand( RADIO_CLR_IRQSTATUS, buf, 2 );
  619. }