radio.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /*!
  2. * \file radio.c
  3. *
  4. * \brief Radio driver API definition
  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 <stdbool.h>
  26. #include "radio.h"
  27. #include "sx126x.h"
  28. #include "sx126x-board.h"
  29. /*!
  30. * \brief Initializes the radio
  31. *
  32. * \param [IN] events Structure containing the driver callback functions
  33. */
  34. static void RadioInit( lora_device_t* lora_device,RadioEvents_t *events );
  35. /*!
  36. * Return current radio status
  37. *
  38. * \param status Radio status.[RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
  39. */
  40. static RadioState_t RadioGetStatus( lora_device_t* lora_device );
  41. /*!
  42. * \brief Configures the radio with the given modem
  43. *
  44. * \param [IN] modem Modem to be used [0: FSK, 1: LoRa]
  45. */
  46. static void RadioSetModem( lora_device_t* lora_device,RadioModems_t modem );
  47. /*!
  48. * \brief Sets the channel frequency
  49. *
  50. * \param [IN] freq Channel RF frequency
  51. */
  52. static void RadioSetChannel( lora_device_t* lora_device,uint32_t freq );
  53. /*!
  54. * \brief Checks if the channel is free for the given time
  55. *
  56. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  57. * \param [IN] freq Channel RF frequency
  58. * \param [IN] rssiThresh RSSI threshold
  59. * \param [IN] maxCarrierSenseTime Max time while the RSSI is measured
  60. *
  61. * \retval isFree [true: Channel is free, false: Channel is not free]
  62. */
  63. static bool RadioIsChannelFree( lora_device_t* lora_device,RadioModems_t modem, uint32_t freq, int16_t rssiThresh, uint32_t maxCarrierSenseTime );
  64. /*!
  65. * \brief Generates a 32 bits random value based on the RSSI readings
  66. *
  67. * \remark This function sets the radio in LoRa modem mode and disables
  68. * all interrupts.
  69. * After calling this function either Radio.SetRxConfig or
  70. * Radio.SetTxConfig functions must be called.
  71. *
  72. * \retval randomValue 32 bits random value
  73. */
  74. static uint32_t RadioRandom( lora_device_t* lora_device );
  75. /*!
  76. * \brief Sets the reception parameters
  77. *
  78. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  79. * \param [IN] bandwidth Sets the bandwidth
  80. * FSK : >= 2600 and <= 250000 Hz
  81. * LoRa: [0: 125 kHz, 1: 250 kHz,
  82. * 2: 500 kHz, 3: Reserved]
  83. * \param [IN] datarate Sets the Datarate
  84. * FSK : 600..300000 bits/s
  85. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  86. * 10: 1024, 11: 2048, 12: 4096 chips]
  87. * \param [IN] coderate Sets the coding rate (LoRa only)
  88. * FSK : N/A ( set to 0 )
  89. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  90. * \param [IN] bandwidthAfc Sets the AFC Bandwidth (FSK only)
  91. * FSK : >= 2600 and <= 250000 Hz
  92. * LoRa: N/A ( set to 0 )
  93. * \param [IN] preambleLen Sets the Preamble length
  94. * FSK : Number of bytes
  95. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  96. * \param [IN] symbTimeout Sets the RxSingle timeout value
  97. * FSK : timeout in number of bytes
  98. * LoRa: timeout in symbols
  99. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  100. * \param [IN] payloadLen Sets payload length when fixed length is used
  101. * \param [IN] crcOn Enables/Disables the CRC [0: OFF, 1: ON]
  102. * \param [IN] FreqHopOn Enables disables the intra-packet frequency hopping
  103. * FSK : N/A ( set to 0 )
  104. * LoRa: [0: OFF, 1: ON]
  105. * \param [IN] HopPeriod Number of symbols between each hop
  106. * FSK : N/A ( set to 0 )
  107. * LoRa: Number of symbols
  108. * \param [IN] iqInverted Inverts IQ signals (LoRa only)
  109. * FSK : N/A ( set to 0 )
  110. * LoRa: [0: not inverted, 1: inverted]
  111. * \param [IN] rxContinuous Sets the reception in continuous mode
  112. * [false: single mode, true: continuous mode]
  113. */
  114. static void RadioSetRxConfig( lora_device_t* lora_device,RadioModems_t modem, uint32_t bandwidth,
  115. uint32_t datarate, uint8_t coderate,
  116. uint32_t bandwidthAfc, uint16_t preambleLen,
  117. uint16_t symbTimeout, bool fixLen,
  118. uint8_t payloadLen,
  119. bool crcOn, bool FreqHopOn, uint8_t HopPeriod,
  120. bool iqInverted, bool rxContinuous ,bool LowDatarateOptimize);
  121. /*!
  122. * \brief Sets the transmission parameters
  123. *
  124. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  125. * \param [IN] power Sets the output power [dBm]
  126. * \param [IN] fdev Sets the frequency deviation (FSK only)
  127. * FSK : [Hz]
  128. * LoRa: 0
  129. * \param [IN] bandwidth Sets the bandwidth (LoRa only)
  130. * FSK : 0
  131. * LoRa: [0: 125 kHz, 1: 250 kHz,
  132. * 2: 500 kHz, 3: Reserved]
  133. * \param [IN] datarate Sets the Datarate
  134. * FSK : 600..300000 bits/s
  135. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  136. * 10: 1024, 11: 2048, 12: 4096 chips]
  137. * \param [IN] coderate Sets the coding rate (LoRa only)
  138. * FSK : N/A ( set to 0 )
  139. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  140. * \param [IN] preambleLen Sets the preamble length
  141. * FSK : Number of bytes
  142. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  143. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  144. * \param [IN] crcOn Enables disables the CRC [0: OFF, 1: ON]
  145. * \param [IN] FreqHopOn Enables disables the intra-packet frequency hopping
  146. * FSK : N/A ( set to 0 )
  147. * LoRa: [0: OFF, 1: ON]
  148. * \param [IN] HopPeriod Number of symbols between each hop
  149. * FSK : N/A ( set to 0 )
  150. * LoRa: Number of symbols
  151. * \param [IN] iqInverted Inverts IQ signals (LoRa only)
  152. * FSK : N/A ( set to 0 )
  153. * LoRa: [0: not inverted, 1: inverted]
  154. * \param [IN] timeout Transmission timeout [ms]
  155. */
  156. static void RadioSetTxConfig( lora_device_t* lora_device,RadioModems_t modem, int8_t power, uint32_t fdev,
  157. uint32_t bandwidth, uint32_t datarate,
  158. uint8_t coderate, uint16_t preambleLen,
  159. bool fixLen, bool crcOn, bool FreqHopOn,
  160. uint8_t HopPeriod, bool iqInverted, uint32_t timeout ,bool LowDatarateOptimize);
  161. /*!
  162. * \brief Checks if the given RF frequency is supported by the hardware
  163. *
  164. * \param [IN] frequency RF frequency to be checked
  165. * \retval isSupported [true: supported, false: unsupported]
  166. */
  167. static bool RadioCheckRfFrequency( lora_device_t* lora_device,uint32_t frequency );
  168. /*!
  169. * \brief Computes the packet time on air in ms for the given payload
  170. *
  171. * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
  172. *
  173. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  174. * \param [IN] pktLen Packet payload length
  175. *
  176. * \retval airTime Computed airTime (ms) for the given packet payload length
  177. */
  178. static uint32_t RadioTimeOnAir( lora_device_t* lora_device,RadioModems_t modem, uint8_t pktLen );
  179. /*!
  180. * \brief Sends the buffer of size. Prepares the packet to be sent and sets
  181. * the radio in transmission
  182. *
  183. * \param [IN]: buffer Buffer pointer
  184. * \param [IN]: size Buffer size
  185. */
  186. static void RadioSend( lora_device_t* lora_device,uint8_t *buffer, uint8_t size );
  187. /*!
  188. * \brief Sets the radio in sleep mode
  189. */
  190. static void RadioSleep( lora_device_t* lora_device );
  191. /*!
  192. * \brief Sets the radio in standby mode
  193. */
  194. static void RadioStandby( lora_device_t* lora_device );
  195. /*!
  196. * \brief Sets the radio in reception mode for the given time
  197. * \param [IN] timeout Reception timeout [ms]
  198. * [0: continuous, others timeout]
  199. */
  200. static void RadioRx( lora_device_t* lora_device,uint32_t timeout );
  201. /*!
  202. * \brief Start a Channel Activity Detection
  203. */
  204. static void RadioStartCad( lora_device_t* lora_device );
  205. /*!
  206. * \brief Sets the radio in continuous wave transmission mode
  207. *
  208. * \param [IN]: freq Channel RF frequency
  209. * \param [IN]: power Sets the output power [dBm]
  210. * \param [IN]: time Transmission mode timeout [s]
  211. */
  212. static void RadioSetTxContinuousWave( lora_device_t* lora_device,uint32_t freq, int8_t power, uint16_t time );
  213. /*!
  214. * \brief Reads the current RSSI value
  215. *
  216. * \retval rssiValue Current RSSI value in [dBm]
  217. */
  218. static int16_t RadioRssi( lora_device_t* lora_device,RadioModems_t modem );
  219. /*!
  220. * \brief Writes the radio register at the specified address
  221. *
  222. * \param [IN]: addr Register address
  223. * \param [IN]: data New register value
  224. */
  225. static void RadioWrite( lora_device_t* lora_device,uint16_t addr, uint8_t data );
  226. /*!
  227. * \brief Reads the radio register at the specified address
  228. *
  229. * \param [IN]: addr Register address
  230. * \retval data Register value
  231. */
  232. static uint8_t RadioRead( lora_device_t* lora_device,uint16_t addr );
  233. /*!
  234. * \brief Writes multiple radio registers starting at address
  235. *
  236. * \param [IN] addr First Radio register address
  237. * \param [IN] buffer Buffer containing the new register's values
  238. * \param [IN] size Number of registers to be written
  239. */
  240. static void RadioWriteBuffer( lora_device_t* lora_device,uint16_t addr, uint8_t *buffer, uint8_t size );
  241. /*!
  242. * \brief Reads multiple radio registers starting at address
  243. *
  244. * \param [IN] addr First Radio register address
  245. * \param [OUT] buffer Buffer where to copy the registers data
  246. * \param [IN] size Number of registers to be read
  247. */
  248. static void RadioReadBuffer( lora_device_t* lora_device,uint16_t addr, uint8_t *buffer, uint8_t size );
  249. /*!
  250. * \brief Sets the maximum payload length.
  251. *
  252. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  253. * \param [IN] max Maximum payload length in bytes
  254. */
  255. static void RadioSetMaxPayloadLength( lora_device_t* lora_device,RadioModems_t modem, uint8_t max );
  256. /*!
  257. * \brief Sets the network to public or private. Updates the sync byte.
  258. *
  259. * \remark Applies to LoRa modem only
  260. *
  261. * \param [IN] enable if true, it enables a public network
  262. */
  263. static void RadioSetPublicNetwork( lora_device_t* lora_device,bool enable );
  264. /*!
  265. * \brief Gets the time required for the board plus radio to get out of sleep.[ms]
  266. *
  267. * \retval time Radio plus board wakeup time in ms.
  268. */
  269. static uint32_t RadioGetWakeupTime( lora_device_t* lora_device );
  270. /*!
  271. * \brief Process radio irq
  272. */
  273. static void RadioIrqProcess( lora_device_t* lora_device );
  274. /*!
  275. * \brief Sets the radio in reception mode with Max LNA gain for the given time
  276. * \param [IN] timeout Reception timeout [ms]
  277. * [0: continuous, others timeout]
  278. */
  279. static void RadioRxBoosted( lora_device_t* lora_device,uint32_t timeout );
  280. /*!
  281. * \brief Sets the Rx duty cycle management parameters
  282. *
  283. * \param [in] rxTime Structure describing reception timeout value
  284. * \param [in] sleepTime Structure describing sleep timeout value
  285. */
  286. static void RadioSetRxDutyCycle( lora_device_t* lora_device,uint32_t rxTime, uint32_t sleepTime );
  287. /*!
  288. * Radio driver structure initialization
  289. */
  290. const struct Radio_s Radio2 =
  291. {
  292. RadioInit,
  293. RadioGetStatus,
  294. RadioSetModem,
  295. RadioSetChannel,
  296. RadioIsChannelFree,
  297. RadioRandom,
  298. RadioSetRxConfig,
  299. RadioSetTxConfig,
  300. RadioCheckRfFrequency,
  301. RadioTimeOnAir,
  302. RadioSend,
  303. RadioSleep,
  304. RadioStandby,
  305. RadioRx,
  306. RadioStartCad,
  307. RadioSetTxContinuousWave,
  308. RadioRssi,
  309. RadioWrite,
  310. RadioRead,
  311. RadioWriteBuffer,
  312. RadioReadBuffer,
  313. RadioSetMaxPayloadLength,
  314. RadioSetPublicNetwork,
  315. RadioGetWakeupTime,
  316. RadioIrqProcess,
  317. // Available on SX126x only
  318. RadioRxBoosted,
  319. RadioSetRxDutyCycle
  320. };
  321. /*
  322. * Local types definition
  323. */
  324. /*!
  325. * FSK bandwidth definition
  326. */
  327. typedef struct
  328. {
  329. uint32_t bandwidth;
  330. uint8_t RegValue;
  331. }FskBandwidth_t;
  332. /*!
  333. * Precomputed FSK bandwidth registers values
  334. */
  335. static const FskBandwidth_t FskBandwidths[] =
  336. {
  337. { 4800 , 0x1F },
  338. { 5800 , 0x17 },
  339. { 7300 , 0x0F },
  340. { 9700 , 0x1E },
  341. { 11700 , 0x16 },
  342. { 14600 , 0x0E },
  343. { 19500 , 0x1D },
  344. { 23400 , 0x15 },
  345. { 29300 , 0x0D },
  346. { 39000 , 0x1C },
  347. { 46900 , 0x14 },
  348. { 58600 , 0x0C },
  349. { 78200 , 0x1B },
  350. { 93800 , 0x13 },
  351. { 117300, 0x0B },
  352. { 156200, 0x1A },
  353. { 187200, 0x12 },
  354. { 234300, 0x0A },
  355. { 312000, 0x19 },
  356. { 373600, 0x11 },
  357. { 467000, 0x09 },
  358. { 500000, 0x00 }, // Invalid Bandwidth
  359. };
  360. static const RadioLoRaBandwidths_t Bandwidths[] = { LORA_BW_125, LORA_BW_250, LORA_BW_500 };
  361. // SF12 SF11 SF10 SF9 SF8 SF7
  362. static double RadioLoRaSymbTime[3][6] = {{ 32.768, 16.384, 8.192, 4.096, 2.048, 1.024 }, // 125 KHz
  363. { 16.384, 8.192, 4.096, 2.048, 1.024, 0.512 }, // 250 KHz
  364. { 8.192, 4.096, 2.048, 1.024, 0.512, 0.256 }}; // 500 KHz
  365. /*
  366. * SX126x DIO IRQ callback functions prototype
  367. */
  368. /*!
  369. * \brief DIO 0 IRQ callback
  370. */
  371. static void RadioOnDioIrq( lora_device_t* lora_device );
  372. /*!
  373. * \brief Tx timeout timer callback
  374. */
  375. static void RadioOnTxTimeoutIrq( lora_device_t* lora_device );
  376. /*!
  377. * \brief Rx timeout timer callback
  378. */
  379. static void RadioOnRxTimeoutIrq( lora_device_t* lora_device );
  380. /*
  381. * Private global variables
  382. */
  383. /*!
  384. * Holds the current network type for the radio
  385. */
  386. typedef struct
  387. {
  388. bool Previous;
  389. bool Current;
  390. }RadioPublicNetwork_t;
  391. static RadioPublicNetwork_t RadioPublicNetwork = { false };
  392. /*!
  393. * Radio callbacks variable
  394. */
  395. /*
  396. * Public global variables
  397. */
  398. /*!
  399. * Radio hardware and global parameters
  400. */
  401. // SX126x_t SX126x;
  402. /*!
  403. * Tx and Rx timers
  404. */
  405. //TimerEvent_t TxTimeoutTimer;
  406. //TimerEvent_t RxTimeoutTimer;
  407. /*!
  408. * Returns the known FSK bandwidth registers value
  409. *
  410. * \param [IN] bandwidth Bandwidth value in Hz
  411. * \retval regValue Bandwidth register value.
  412. */
  413. static uint8_t RadioGetFskBandwidthRegValue( lora_device_t* lora_device,uint32_t bandwidth )
  414. {
  415. uint8_t i;
  416. if( bandwidth == 0 )
  417. {
  418. return( 0x1F );
  419. }
  420. for( i = 0; i < ( sizeof( FskBandwidths ) / sizeof( FskBandwidth_t ) ) - 1; i++ )
  421. {
  422. if( ( bandwidth >= FskBandwidths[i].bandwidth ) && ( bandwidth < FskBandwidths[i + 1].bandwidth ) )
  423. {
  424. return FskBandwidths[i+1].RegValue;
  425. }
  426. }
  427. // ERROR: Value not found
  428. while( 1 );
  429. }
  430. void RadioEventsInit2(lora_device_t* lora_device,RadioEvents_t *events){
  431. memcpy(&lora_device->RadioEvents,events,sizeof(RadioEvents_t));
  432. }
  433. static void RadioInit( lora_device_t* lora_device,RadioEvents_t *events )
  434. {
  435. memcpy(&lora_device->RadioEvents,events,sizeof(RadioEvents_t));
  436. lora_device->MaxPayloadLength = 0xFF;
  437. SX126xInit2( lora_device,RadioOnDioIrq );
  438. SX126xSetStandby2( lora_device,STDBY_RC );
  439. SX126xSetRegulatorMode2( lora_device,USE_DCDC );
  440. SX126xSetBufferBaseAddress2( lora_device,0x00, 0x00 );
  441. SX126xSetTx2Params2( lora_device,0, RADIO_RAMP_200_US );
  442. SX126xSetDioIrqParams2( lora_device,IRQ_RADIO_ALL, IRQ_RADIO_ALL, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
  443. }
  444. static RadioState_t RadioGetStatus( lora_device_t* lora_device )
  445. {
  446. switch( SX126xGetOperatingMode2( lora_device ) )
  447. {
  448. case MODE_TX:
  449. return RF_TX_RUNNING;
  450. case MODE_RX:
  451. return RF_RX_RUNNING;
  452. case RF_CAD:
  453. return RF_CAD;
  454. default:
  455. return RF_IDLE;
  456. }
  457. }
  458. static void RadioSetModem(lora_device_t* lora_device, RadioModems_t modem )
  459. {
  460. switch( modem )
  461. {
  462. default:
  463. case MODEM_FSK:
  464. SX126xSetPacketType2( lora_device,PACKET_TYPE_GFSK );
  465. // When switching to GFSK mode the LoRa SyncWord register value is reset
  466. // Thus, we also reset the RadioPublicNetwork variable
  467. RadioPublicNetwork.Current = false;
  468. break;
  469. case MODEM_LORA:
  470. SX126xSetPacketType2( lora_device,PACKET_TYPE_LORA );
  471. // Public/Private network register is reset when switching modems
  472. if( RadioPublicNetwork.Current != RadioPublicNetwork.Previous )
  473. {
  474. RadioPublicNetwork.Current = RadioPublicNetwork.Previous;
  475. RadioSetPublicNetwork( lora_device,RadioPublicNetwork.Current );
  476. }
  477. break;
  478. }
  479. }
  480. static void RadioSetChannel( lora_device_t* lora_device,uint32_t freq )
  481. {
  482. SX126xSetRfFrequency2( lora_device,freq );
  483. }
  484. static bool RadioIsChannelFree( lora_device_t* lora_device,RadioModems_t modem, uint32_t freq, int16_t rssiThresh, uint32_t maxCarrierSenseTime )
  485. {
  486. bool status = true;
  487. // int16_t rssi = 0;
  488. // uint32_t carrierSenseTime = 0;
  489. RadioSetModem( lora_device,modem );
  490. RadioSetChannel( lora_device,freq );
  491. RadioRx(lora_device,0 );
  492. SX126xDelayMs2( 1 );
  493. //carrierSenseTime = TimerGetCurrentTime( );
  494. //Perform carrier sense for maxCarrierSenseTime
  495. // while( TimerGetElapsedTime( carrierSenseTime ) < maxCarrierSenseTime )
  496. // {
  497. // rssi = RadioRssi( modem );
  498. //
  499. // if( rssi > rssiThresh )
  500. // {
  501. // status = false;
  502. // break;
  503. // }
  504. // }
  505. RadioSleep( lora_device);
  506. return status;
  507. }
  508. static uint32_t RadioRandom( lora_device_t* lora_device )
  509. {
  510. uint8_t i;
  511. uint32_t rnd = 0;
  512. /*
  513. * Radio setup for random number generation
  514. */
  515. // Set LoRa modem ON
  516. RadioSetModem(lora_device,MODEM_LORA );
  517. // Set radio in continuous reception
  518. SX126xSetRx2( lora_device,0 );
  519. for( i = 0; i < 32; i++ )
  520. {
  521. SX126xDelayMs2( 1 );
  522. // Unfiltered RSSI value reading. Only takes the LSB value
  523. rnd |= ( ( uint32_t )SX126xGetRssiInst2( lora_device ) & 0x01 ) << i;
  524. }
  525. RadioSleep(lora_device );
  526. return rnd;
  527. }
  528. static void RadioSetRxConfig( lora_device_t* lora_device,RadioModems_t modem, uint32_t bandwidth,
  529. uint32_t datarate, uint8_t coderate,
  530. uint32_t bandwidthAfc, uint16_t preambleLen,
  531. uint16_t symbTimeout, bool fixLen,
  532. uint8_t payloadLen,
  533. bool crcOn, bool freqHopOn, uint8_t hopPeriod,
  534. bool iqInverted, bool rxContinuous ,bool LowDatarateOptimize)
  535. {
  536. lora_device->RxContinuous = rxContinuous;
  537. if( fixLen == true )
  538. {
  539. lora_device->MaxPayloadLength = payloadLen;
  540. }
  541. else
  542. {
  543. lora_device->MaxPayloadLength = 0xFF;
  544. }
  545. switch( modem )
  546. {
  547. case MODEM_FSK:
  548. SX126xSetStopRxTimerOnPreambleDetect2( lora_device,false );
  549. lora_device->SX126x.ModulationParams.PacketType = PACKET_TYPE_GFSK;
  550. lora_device->SX126x.ModulationParams.Params.Gfsk.BitRate = datarate;
  551. lora_device->SX126x.ModulationParams.Params.Gfsk.ModulationShaping = MOD_SHAPING_G_BT_1;
  552. lora_device->SX126x.ModulationParams.Params.Gfsk.Bandwidth = RadioGetFskBandwidthRegValue( lora_device,bandwidth );
  553. lora_device->SX126x.PacketParams.PacketType = PACKET_TYPE_GFSK;
  554. lora_device->SX126x.PacketParams.Params.Gfsk.PreambleLength = ( preambleLen << 3 ); // convert byte into bit
  555. lora_device->SX126x.PacketParams.Params.Gfsk.PreambleMinDetect = RADIO_PREAMBLE_DETECTOR_08_BITS;
  556. lora_device->SX126x.PacketParams.Params.Gfsk.SyncWordLength = 3 << 3; // convert byte into bit
  557. lora_device->SX126x.PacketParams.Params.Gfsk.AddrComp = RADIO_ADDRESSCOMP_FILT_OFF;
  558. lora_device->SX126x.PacketParams.Params.Gfsk.HeaderType = ( fixLen == true ) ? RADIO_PACKET_FIXED_LENGTH : RADIO_PACKET_VARIABLE_LENGTH;
  559. lora_device->SX126x.PacketParams.Params.Gfsk.PayloadLength = lora_device->MaxPayloadLength;
  560. if( crcOn == true )
  561. {
  562. lora_device->SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_2_BYTES_CCIT;
  563. }
  564. else
  565. {
  566. lora_device->SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_OFF;
  567. }
  568. lora_device->SX126x.PacketParams.Params.Gfsk.DcFree = RADIO_DC_FREE_OFF;
  569. RadioStandby( lora_device);
  570. RadioSetModem( lora_device,( lora_device->SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
  571. SX126xSetModulationParams2( lora_device,&lora_device->SX126x.ModulationParams );
  572. SX126xSetPacketParams2( lora_device,&lora_device->SX126x.PacketParams );
  573. SX126xSetSyncWord2( lora_device,( uint8_t[] ){ 0xC1, 0x94, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00 } );
  574. SX126xSetWhiteningSeed2( lora_device,0x01FF );
  575. // RxTimeout = ( uint32_t )( symbTimeout * ( ( 1.0 / ( double )datarate ) * 8.0 ) * 1000 );
  576. break;
  577. case MODEM_LORA:
  578. SX126xSetStopRxTimerOnPreambleDetect2(lora_device, false );
  579. SX126xSetLoRaSymbNumTimeout2( lora_device,symbTimeout );
  580. lora_device->SX126x.ModulationParams.PacketType = PACKET_TYPE_LORA;
  581. lora_device->SX126x.ModulationParams.Params.LoRa.SpreadingFactor = ( RadioLoRaSpreadingFactors_t )datarate;
  582. lora_device->SX126x.ModulationParams.Params.LoRa.Bandwidth = Bandwidths[bandwidth];
  583. lora_device->SX126x.ModulationParams.Params.LoRa.CodingRate = ( RadioLoRaCodingRates_t )coderate;
  584. if (LowDatarateOptimize){
  585. lora_device->SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x01;
  586. }else{
  587. if( ( ( bandwidth == 0 ) && ( ( datarate == 11 ) || ( datarate == 12 ) ) ) ||
  588. ( ( bandwidth == 1 ) && ( datarate == 12 ) ) )
  589. {
  590. lora_device->SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x01;
  591. }
  592. else
  593. {
  594. lora_device->SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x00;
  595. }
  596. }
  597. lora_device->SX126x.PacketParams.PacketType = PACKET_TYPE_LORA;
  598. if( ( lora_device->SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF5 ) ||
  599. ( lora_device->SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF6 ) )
  600. {
  601. if( preambleLen < 12 )
  602. {
  603. lora_device->SX126x.PacketParams.Params.LoRa.PreambleLength = 12;
  604. }
  605. else
  606. {
  607. lora_device->SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
  608. }
  609. }
  610. else
  611. {
  612. lora_device->SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
  613. }
  614. lora_device->SX126x.PacketParams.Params.LoRa.HeaderType = ( RadioLoRaPacketLengthsMode_t )fixLen;
  615. lora_device->SX126x.PacketParams.Params.LoRa.PayloadLength = lora_device->MaxPayloadLength;
  616. lora_device->SX126x.PacketParams.Params.LoRa.CrcMode = ( RadioLoRaCrcModes_t )crcOn;
  617. lora_device->SX126x.PacketParams.Params.LoRa.InvertIQ = ( RadioLoRaIQModes_t )iqInverted;
  618. RadioSetModem( lora_device,( lora_device->SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
  619. SX126xSetModulationParams2( lora_device,&lora_device->SX126x.ModulationParams );
  620. SX126xSetPacketParams2( lora_device,&lora_device->SX126x.PacketParams );
  621. // Timeout Max, Timeout handled directly in SetRx function
  622. // RxTimeout = 0xFFFF;
  623. break;
  624. }
  625. }
  626. static void RadioSetTxConfig( lora_device_t* lora_device,RadioModems_t modem, int8_t power, uint32_t fdev,
  627. uint32_t bandwidth, uint32_t datarate,
  628. uint8_t coderate, uint16_t preambleLen,
  629. bool fixLen, bool crcOn, bool freqHopOn,
  630. uint8_t hopPeriod, bool iqInverted, uint32_t timeout ,bool LowDatarateOptimize)
  631. {
  632. switch( modem )
  633. {
  634. case MODEM_FSK:
  635. lora_device->SX126x.ModulationParams.PacketType = PACKET_TYPE_GFSK;
  636. lora_device->SX126x.ModulationParams.Params.Gfsk.BitRate = datarate;
  637. lora_device->SX126x.ModulationParams.Params.Gfsk.ModulationShaping = MOD_SHAPING_G_BT_1;
  638. lora_device->SX126x.ModulationParams.Params.Gfsk.Bandwidth = RadioGetFskBandwidthRegValue( lora_device,bandwidth );
  639. lora_device->SX126x.ModulationParams.Params.Gfsk.Fdev = fdev;
  640. lora_device->SX126x.PacketParams.PacketType = PACKET_TYPE_GFSK;
  641. lora_device->SX126x.PacketParams.Params.Gfsk.PreambleLength = ( preambleLen << 3 ); // convert byte into bit
  642. lora_device->SX126x.PacketParams.Params.Gfsk.PreambleMinDetect = RADIO_PREAMBLE_DETECTOR_08_BITS;
  643. lora_device->SX126x.PacketParams.Params.Gfsk.SyncWordLength = 3 << 3 ; // convert byte into bit
  644. lora_device->SX126x.PacketParams.Params.Gfsk.AddrComp = RADIO_ADDRESSCOMP_FILT_OFF;
  645. lora_device->SX126x.PacketParams.Params.Gfsk.HeaderType = ( fixLen == true ) ? RADIO_PACKET_FIXED_LENGTH : RADIO_PACKET_VARIABLE_LENGTH;
  646. if( crcOn == true )
  647. {
  648. lora_device->SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_2_BYTES_CCIT;
  649. }
  650. else
  651. {
  652. lora_device->SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_OFF;
  653. }
  654. lora_device->SX126x.PacketParams.Params.Gfsk.DcFree = RADIO_DC_FREEWHITENING;
  655. RadioStandby( lora_device );
  656. RadioSetModem( lora_device,( lora_device->SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
  657. SX126xSetModulationParams2( lora_device, &lora_device->SX126x.ModulationParams );
  658. SX126xSetPacketParams2( lora_device,&lora_device->SX126x.PacketParams );
  659. SX126xSetSyncWord2( lora_device,( uint8_t[] ){ 0xC1, 0x94, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00 } );
  660. SX126xSetWhiteningSeed2( lora_device,0x01FF );
  661. break;
  662. case MODEM_LORA:
  663. lora_device->SX126x.ModulationParams.PacketType = PACKET_TYPE_LORA;
  664. lora_device->SX126x.ModulationParams.Params.LoRa.SpreadingFactor = ( RadioLoRaSpreadingFactors_t ) datarate;
  665. lora_device->SX126x.ModulationParams.Params.LoRa.Bandwidth = Bandwidths[bandwidth];
  666. lora_device->SX126x.ModulationParams.Params.LoRa.CodingRate= ( RadioLoRaCodingRates_t )coderate;
  667. if (LowDatarateOptimize){
  668. lora_device->SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x01;
  669. }else{
  670. if( ( ( bandwidth == 0 ) && ( ( datarate == 11 ) || ( datarate == 12 ) ) ) ||
  671. ( ( bandwidth == 1 ) && ( datarate == 12 ) ) )
  672. {
  673. lora_device->SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x01;
  674. }
  675. else
  676. {
  677. lora_device->SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x00;
  678. }
  679. }
  680. lora_device->SX126x.PacketParams.PacketType = PACKET_TYPE_LORA;
  681. if( ( lora_device->SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF5 ) ||
  682. ( lora_device->SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF6 ) )
  683. {
  684. if( preambleLen < 12 )
  685. {
  686. lora_device->SX126x.PacketParams.Params.LoRa.PreambleLength = 12;
  687. }
  688. else
  689. {
  690. lora_device->SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
  691. }
  692. }
  693. else
  694. {
  695. lora_device->SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
  696. }
  697. lora_device->SX126x.PacketParams.Params.LoRa.HeaderType = ( RadioLoRaPacketLengthsMode_t )fixLen;
  698. lora_device->SX126x.PacketParams.Params.LoRa.PayloadLength = lora_device->MaxPayloadLength;
  699. lora_device->SX126x.PacketParams.Params.LoRa.CrcMode = ( RadioLoRaCrcModes_t )crcOn;
  700. lora_device->SX126x.PacketParams.Params.LoRa.InvertIQ = ( RadioLoRaIQModes_t )iqInverted;
  701. RadioStandby( lora_device);
  702. RadioSetModem( lora_device,( lora_device->SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
  703. SX126xSetModulationParams2( lora_device,&lora_device->SX126x.ModulationParams );
  704. SX126xSetPacketParams2( lora_device,&lora_device->SX126x.PacketParams );
  705. break;
  706. }
  707. SX126xSetRfTxPower2( lora_device,power );
  708. // TxTimeout = timeout;
  709. }
  710. static bool RadioCheckRfFrequency( lora_device_t* lora_device,uint32_t frequency )
  711. {
  712. return true;
  713. }
  714. static uint32_t RadioTimeOnAir( lora_device_t* lora_device,RadioModems_t modem, uint8_t pktLen )
  715. {
  716. uint32_t airTime = 0;
  717. switch( modem )
  718. {
  719. case MODEM_FSK:
  720. {
  721. airTime = rint( ( 8 * ( lora_device->SX126x.PacketParams.Params.Gfsk.PreambleLength +
  722. ( lora_device->SX126x.PacketParams.Params.Gfsk.SyncWordLength >> 3 ) +
  723. ( ( lora_device->SX126x.PacketParams.Params.Gfsk.HeaderType == RADIO_PACKET_FIXED_LENGTH ) ? 0.0 : 1.0 ) +
  724. pktLen +
  725. ( ( lora_device->SX126x.PacketParams.Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES ) ? 2.0 : 0 ) ) /
  726. lora_device->SX126x.ModulationParams.Params.Gfsk.BitRate ) * 1e3 );
  727. }
  728. break;
  729. case MODEM_LORA:
  730. {
  731. double ts = RadioLoRaSymbTime[lora_device->SX126x.ModulationParams.Params.LoRa.Bandwidth - 4][12 - lora_device->SX126x.ModulationParams.Params.LoRa.SpreadingFactor];
  732. // time of preamble
  733. double tPreamble = ( lora_device->SX126x.PacketParams.Params.LoRa.PreambleLength + 4.25 ) * ts;
  734. // Symbol length of payload and time
  735. double tmp = ceil( ( 8 * pktLen - 4 * lora_device->SX126x.ModulationParams.Params.LoRa.SpreadingFactor +
  736. 28 + 16 * lora_device->SX126x.PacketParams.Params.LoRa.CrcMode -
  737. ( ( lora_device->SX126x.PacketParams.Params.LoRa.HeaderType == LORA_PACKET_FIXED_LENGTH ) ? 20 : 0 ) ) /
  738. ( double )( 4 * ( lora_device->SX126x.ModulationParams.Params.LoRa.SpreadingFactor -
  739. ( ( lora_device->SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize > 0 ) ? 2 : 0 ) ) ) ) *
  740. ( ( lora_device->SX126x.ModulationParams.Params.LoRa.CodingRate % 4 ) + 4 );
  741. double nPayload = 8 + ( ( tmp > 0 ) ? tmp : 0 );
  742. double tPayload = nPayload * ts;
  743. // Time on air
  744. double tOnAir = tPreamble + tPayload;
  745. // return milli seconds
  746. airTime = floor( tOnAir + 0.999 );
  747. }
  748. break;
  749. }
  750. return airTime;
  751. }
  752. static void RadioSend( lora_device_t* lora_device,uint8_t *buffer, uint8_t size )
  753. {
  754. SX126xSetDioIrqParams2( lora_device,IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT,
  755. IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT,
  756. IRQ_RADIO_NONE,
  757. IRQ_RADIO_NONE );
  758. if( SX126xGetPacketType2( lora_device ) == PACKET_TYPE_LORA )
  759. {
  760. lora_device->SX126x.PacketParams.Params.LoRa.PayloadLength = size;
  761. }
  762. else
  763. {
  764. lora_device->SX126x.PacketParams.Params.Gfsk.PayloadLength = size;
  765. }
  766. SX126xSetPacketParams2( lora_device,&lora_device->SX126x.PacketParams );
  767. SX126xSendPayload2( lora_device, buffer, size, 0 );
  768. // TimerSetValue( &TxTimeoutTimer, TxTimeout );
  769. // TimerStart( &TxTimeoutTimer );
  770. }
  771. static void RadioSleep( lora_device_t* lora_device )
  772. {
  773. SleepParams_t params = { 0 };
  774. params.Fields.WarmStart = 1;
  775. SX126xSetSleep2( lora_device,params );
  776. SX126xDelayMs2( 2 );
  777. }
  778. static void RadioStandby( lora_device_t* lora_device )
  779. {
  780. SX126xSetStandby2( lora_device,STDBY_RC );
  781. }
  782. static void RadioRx( lora_device_t* lora_device,uint32_t timeout )
  783. {
  784. SX126xSetDioIrqParams2( lora_device,IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
  785. IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
  786. IRQ_RADIO_NONE,
  787. IRQ_RADIO_NONE );
  788. if( lora_device->RxContinuous == true )
  789. {
  790. SX126xSetRx2(lora_device,0xFFFFFF ); // Rx Continuous
  791. }
  792. else
  793. {
  794. SX126xSetRx2( lora_device,timeout << 6 );
  795. }
  796. }
  797. static void RadioRxBoosted( lora_device_t* lora_device,uint32_t timeout )
  798. {
  799. SX126xSetDioIrqParams2( lora_device,IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
  800. IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
  801. IRQ_RADIO_NONE,
  802. IRQ_RADIO_NONE );
  803. if( lora_device->RxContinuous == true )
  804. {
  805. SX126xSetRxBoosted2( lora_device,0xFFFFFF ); // Rx Continuous
  806. }
  807. else
  808. {
  809. SX126xSetRxBoosted2( lora_device,timeout << 6 );
  810. }
  811. }
  812. static void RadioSetRxDutyCycle( lora_device_t* lora_device,uint32_t rxTime, uint32_t sleepTime )
  813. {
  814. SX126xSetRxDutyCycle2( lora_device,rxTime, sleepTime );
  815. }
  816. static void RadioStartCad( lora_device_t* lora_device )
  817. {
  818. SX126xSetCad2( lora_device );
  819. }
  820. static void RadioTx( lora_device_t* lora_device,uint32_t timeout )
  821. {
  822. SX126xSetTx2( lora_device,timeout << 6 );
  823. }
  824. static void RadioSetTxContinuousWave( lora_device_t* lora_device,uint32_t freq, int8_t power, uint16_t time )
  825. {
  826. SX126xSetRfFrequency2( lora_device,freq );
  827. SX126xSetRfTxPower2( lora_device,power );
  828. SX126xSetTx2ContinuousWave2(lora_device );
  829. // TimerSetValue( &RxTimeoutTimer, time * 1e3 );
  830. // TimerStart( &RxTimeoutTimer );
  831. }
  832. static int16_t RadioRssi( lora_device_t* lora_device,RadioModems_t modem )
  833. {
  834. return SX126xGetRssiInst2(lora_device );
  835. }
  836. static void RadioWrite( lora_device_t* lora_device,uint16_t addr, uint8_t data )
  837. {
  838. SX126xWriteRegister2( lora_device,addr, data );
  839. }
  840. static uint8_t RadioRead( lora_device_t* lora_device,uint16_t addr )
  841. {
  842. return SX126xReadRegister2( lora_device,addr );
  843. }
  844. static void RadioWriteBuffer( lora_device_t* lora_device,uint16_t addr, uint8_t *buffer, uint8_t size )
  845. {
  846. SX126xWriteRegister2s2( lora_device,addr, buffer, size );
  847. }
  848. static void RadioReadBuffer( lora_device_t* lora_device,uint16_t addr, uint8_t *buffer, uint8_t size )
  849. {
  850. SX126xReadRegister2s2( lora_device,addr, buffer, size );
  851. }
  852. static void RadioWriteFifo( lora_device_t* lora_device,uint8_t *buffer, uint8_t size )
  853. {
  854. SX126xWriteBuffer2( lora_device,0, buffer, size );
  855. }
  856. static void RadioReadFifo( lora_device_t* lora_device,uint8_t *buffer, uint8_t size )
  857. {
  858. SX126xReadBuffer2( lora_device,0, buffer, size );
  859. }
  860. static void RadioSetMaxPayloadLength( lora_device_t* lora_device,RadioModems_t modem, uint8_t max )
  861. {
  862. if( modem == MODEM_LORA )
  863. {
  864. lora_device->SX126x.PacketParams.Params.LoRa.PayloadLength = lora_device->MaxPayloadLength = max;
  865. SX126xSetPacketParams2( lora_device,&lora_device->SX126x.PacketParams );
  866. }
  867. else
  868. {
  869. if( lora_device->SX126x.PacketParams.Params.Gfsk.HeaderType == RADIO_PACKET_VARIABLE_LENGTH )
  870. {
  871. lora_device->SX126x.PacketParams.Params.Gfsk.PayloadLength = lora_device->MaxPayloadLength = max;
  872. SX126xSetPacketParams2( lora_device,&lora_device->SX126x.PacketParams );
  873. }
  874. }
  875. }
  876. static void RadioSetPublicNetwork( lora_device_t* lora_device,bool enable )
  877. {
  878. RadioPublicNetwork.Current = RadioPublicNetwork.Previous = enable;
  879. RadioSetModem( lora_device, MODEM_LORA );
  880. if( enable == true )
  881. {
  882. // Change LoRa modem SyncWord
  883. SX126xWriteRegister2( lora_device,REG_LR_SYNCWORD, ( LORA_MAC_PUBLIC_SYNCWORD >> 8 ) & 0xFF );
  884. SX126xWriteRegister2( lora_device,REG_LR_SYNCWORD + 1, LORA_MAC_PUBLIC_SYNCWORD & 0xFF );
  885. }
  886. else
  887. {
  888. // Change LoRa modem SyncWord
  889. SX126xWriteRegister2(lora_device,REG_LR_SYNCWORD, ( LORA_MAC_PRIVATE_SYNCWORD >> 8 ) & 0xFF );
  890. SX126xWriteRegister2(lora_device,REG_LR_SYNCWORD + 1, LORA_MAC_PRIVATE_SYNCWORD & 0xFF );
  891. }
  892. }
  893. static uint32_t RadioGetWakeupTime( lora_device_t* lora_device )
  894. {
  895. return( RADIO_TCXO_SETUP_TIME + RADIO_WAKEUP_TIME );
  896. }
  897. static void RadioOnTxTimeoutIrq( lora_device_t* lora_device )
  898. {
  899. if( lora_device->RadioEvents.TxTimeout != NULL )
  900. {
  901. lora_device->RadioEvents.TxTimeout( lora_device);
  902. }
  903. }
  904. static void RadioOnRxTimeoutIrq( lora_device_t* lora_device )
  905. {
  906. if( lora_device->RadioEvents.RxTimeout != NULL )
  907. {
  908. lora_device->RadioEvents.RxTimeout( lora_device);
  909. }
  910. }
  911. static void RadioOnDioIrq( lora_device_t* lora_device )
  912. {
  913. }
  914. static void RadioIrqProcess( lora_device_t* lora_device )
  915. {
  916. if(SX126xGetIrqFired2(lora_device)==1)
  917. {
  918. uint16_t irqRegs = SX126xGetIrqStatus2( lora_device);
  919. SX126xClearIrqStatus2( lora_device,IRQ_RADIO_ALL );
  920. if( ( irqRegs & IRQ_TX_DONE ) == IRQ_TX_DONE )
  921. {
  922. if( lora_device->RadioEvents.TxDone != NULL )
  923. {
  924. lora_device->RadioEvents.TxDone(lora_device);
  925. }
  926. }
  927. if( ( irqRegs & IRQ_RX_DONE ) == IRQ_RX_DONE )
  928. {
  929. uint8_t size;
  930. SX126xGetPayload2( lora_device,lora_device->RadioRxPayload, &size , 255 );
  931. SX126xGetPacketStatus2( lora_device,&lora_device->RadioPktStatus );
  932. if( lora_device->RadioEvents.RxDone != NULL )
  933. {
  934. lora_device->RadioEvents.RxDone( lora_device,lora_device->RadioRxPayload, size, lora_device->RadioPktStatus.Params.LoRa.RssiPkt, lora_device->RadioPktStatus.Params.LoRa.SnrPkt );
  935. }
  936. }
  937. if( ( irqRegs & IRQ_CRC_ERROR ) == IRQ_CRC_ERROR )
  938. {
  939. if( lora_device->RadioEvents.RxError )
  940. {
  941. lora_device->RadioEvents.RxError( lora_device);
  942. }
  943. }
  944. if( ( irqRegs & IRQ_CAD_DONE ) == IRQ_CAD_DONE )
  945. {
  946. if( lora_device->RadioEvents.CadDone != NULL )
  947. {
  948. lora_device->RadioEvents.CadDone( lora_device,( ( irqRegs & IRQ_CAD_ACTIVITY_DETECTED ) == IRQ_CAD_ACTIVITY_DETECTED ) );
  949. }
  950. }
  951. if( ( irqRegs & IRQ_RX_TX_TIMEOUT ) == IRQ_RX_TX_TIMEOUT )
  952. {
  953. if( SX126xGetOperatingMode2( lora_device) == MODE_TX )
  954. {
  955. if(lora_device->RadioEvents.TxTimeout != NULL )
  956. {
  957. lora_device->RadioEvents.TxTimeout( lora_device );
  958. }
  959. }
  960. else if( SX126xGetOperatingMode2( lora_device) == MODE_RX )
  961. {
  962. if( lora_device->RadioEvents.RxTimeout != NULL )
  963. {
  964. lora_device->RadioEvents.RxTimeout( lora_device);
  965. }
  966. }
  967. }
  968. if( ( irqRegs & IRQ_PREAMBLE_DETECTED ) == IRQ_PREAMBLE_DETECTED )
  969. {
  970. //__NOP( );
  971. }
  972. if( ( irqRegs & IRQ_SYNCWORD_VALID ) == IRQ_SYNCWORD_VALID )
  973. {
  974. //__NOP( );
  975. }
  976. if( ( irqRegs & IRQ_HEADER_VALID ) == IRQ_HEADER_VALID )
  977. {
  978. //__NOP( );
  979. }
  980. if( ( irqRegs & IRQ_HEADER_ERROR ) == IRQ_HEADER_ERROR )
  981. {
  982. if( lora_device->RadioEvents.RxTimeout != NULL )
  983. {
  984. lora_device->RadioEvents.RxTimeout( lora_device);
  985. }
  986. }
  987. }
  988. }