radio.c 38 KB

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