sx126x.c 21 KB

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