sx126x.c 19 KB

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