radio.c 37 KB

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