radio.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*!
  2. * \file radio.h
  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. #ifndef __RADIO_H__
  24. #define __RADIO_H__
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. #include <stdint.h>
  30. #include <stdbool.h>
  31. #include <sx126x.h>
  32. /*!
  33. * Radio driver supported modems
  34. */
  35. typedef enum
  36. {
  37. MODEM_FSK = 0,
  38. MODEM_LORA,
  39. }RadioModems_t;
  40. /*!
  41. * Radio driver internal state machine states definition
  42. */
  43. typedef enum
  44. {
  45. RF_IDLE = 0, //!< The radio is idle
  46. RF_RX_RUNNING, //!< The radio is in reception state
  47. RF_TX_RUNNING, //!< The radio is in transmission state
  48. RF_CAD, //!< The radio is doing channel activity detection
  49. }RadioState_t;
  50. /*!
  51. * \brief Radio driver callback functions
  52. */
  53. typedef struct
  54. {
  55. /*!
  56. * \brief Tx Done callback prototype.
  57. */
  58. void ( *TxDone )( void );
  59. /*!
  60. * \brief Tx Timeout callback prototype.
  61. */
  62. void ( *TxTimeout )( void );
  63. /*!
  64. * \brief Rx Done callback prototype.
  65. *
  66. * \param [IN] payload Received buffer pointer
  67. * \param [IN] size Received buffer size
  68. * \param [IN] rssi RSSI value computed while receiving the frame [dBm]
  69. * \param [IN] snr SNR value computed while receiving the frame [dB]
  70. * FSK : N/A ( set to 0 )
  71. * LoRa: SNR value in dB
  72. */
  73. void ( *RxDone )( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
  74. /*!
  75. * \brief Rx Timeout callback prototype.
  76. */
  77. void ( *RxTimeout )( void );
  78. /*!
  79. * \brief Rx Error callback prototype.
  80. */
  81. void ( *RxError )( void );
  82. /*!
  83. * \brief FHSS Change Channel callback prototype.
  84. *
  85. * \param [IN] currentChannel Index number of the current channel
  86. */
  87. void ( *FhssChangeChannel )( uint8_t currentChannel );
  88. /*!
  89. * \brief CAD Done callback prototype.
  90. *
  91. * \param [IN] channelDetected Channel Activity detected during the CAD
  92. */
  93. void ( *CadDone ) ( bool channelActivityDetected );
  94. /*!
  95. * \brief Gnss Done Done callback prototype.
  96. */
  97. void ( *GnssDone )( void );
  98. /*!
  99. * \brief Gnss Done Done callback prototype.
  100. */
  101. void ( *WifiDone )( void );
  102. /*!
  103. * \brief 检测到前导码.
  104. */
  105. bool lora_preamble_detected;
  106. /*!
  107. * \brief 检测到有效包头.
  108. */
  109. bool lora_header_valid;
  110. /*!
  111. * \brief 从cad到rx.
  112. */
  113. bool fromCadtoRx;
  114. }RadioEvents_t;
  115. /*!
  116. * \brief Radio driver definition
  117. */
  118. struct Radio_s
  119. {
  120. /*!
  121. * \brief Initializes the radio
  122. *
  123. * \param [IN] events Structure containing the driver callback functions
  124. */
  125. void ( *Init )( RadioEvents_t *events );
  126. /*!
  127. * Return current radio status
  128. *
  129. * \param status Radio status.[RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
  130. */
  131. RadioState_t ( *GetStatus )( void );
  132. /*!
  133. * \brief Configures the radio with the given modem
  134. *
  135. * \param [IN] modem Modem to be used [0: FSK, 1: LoRa]
  136. */
  137. void ( *SetModem )( RadioModems_t modem );
  138. /*!
  139. * \brief Sets the channel frequency
  140. *
  141. * \param [IN] freq Channel RF frequency
  142. */
  143. void ( *SetChannel )( uint32_t freq );
  144. /*!
  145. * \brief Checks if the channel is free for the given time
  146. *
  147. * \remark The FSK modem is always used for this task as we can select the Rx bandwidth at will.
  148. *
  149. * \param [IN] freq Channel RF frequency in Hertz
  150. * \param [IN] rxBandwidth Rx bandwidth in Hertz
  151. * \param [IN] rssiThresh RSSI threshold in dBm
  152. * \param [IN] maxCarrierSenseTime Max time in milliseconds while the RSSI is measured
  153. *
  154. * \retval isFree [true: Channel is free, false: Channel is not free]
  155. */
  156. bool ( *IsChannelFree )( uint32_t freq, uint32_t rxBandwidth, int16_t rssiThresh, uint32_t maxCarrierSenseTime );
  157. /*!
  158. * \brief Generates a 32 bits random value based on the RSSI readings
  159. *
  160. * \remark This function sets the radio in LoRa modem mode and disables
  161. * all interrupts.
  162. * After calling this function either Radio.SetRxConfig or
  163. * Radio.SetTxConfig functions must be called.
  164. *
  165. * \retval randomValue 32 bits random value
  166. */
  167. uint32_t ( *Random )( void );
  168. /*!
  169. * \brief Sets the reception parameters
  170. *
  171. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  172. * \param [IN] bandwidth Sets the bandwidth
  173. * FSK : >= 2600 and <= 250000 Hz
  174. * LoRa: [0: 125 kHz, 1: 250 kHz,
  175. * 2: 500 kHz, 3: Reserved]
  176. * \param [IN] datarate Sets the Datarate
  177. * FSK : 600..300000 bits/s
  178. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  179. * 10: 1024, 11: 2048, 12: 4096 chips]
  180. * \param [IN] coderate Sets the coding rate (LoRa only)
  181. * FSK : N/A ( set to 0 )
  182. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  183. * \param [IN] bandwidthAfc Sets the AFC Bandwidth (FSK only)
  184. * FSK : >= 2600 and <= 250000 Hz
  185. * LoRa: N/A ( set to 0 )
  186. * \param [IN] preambleLen Sets the Preamble length
  187. * FSK : Number of bytes
  188. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  189. * \param [IN] symbTimeout Sets the RxSingle timeout value
  190. * FSK : timeout in number of bytes
  191. * LoRa: timeout in symbols
  192. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  193. * \param [IN] payloadLen Sets payload length when fixed length is used
  194. * \param [IN] crcOn Enables/Disables the CRC [0: OFF, 1: ON]
  195. * \param [IN] freqHopOn Enables disables the intra-packet frequency hopping
  196. * FSK : N/A ( set to 0 )
  197. * LoRa: [0: OFF, 1: ON]
  198. * \param [IN] hopPeriod Number of symbols between each hop
  199. * FSK : N/A ( set to 0 )
  200. * LoRa: Number of symbols
  201. * \param [IN] iqInverted Inverts IQ signals (LoRa only)
  202. * FSK : N/A ( set to 0 )
  203. * LoRa: [0: not inverted, 1: inverted]
  204. * \param [IN] rxContinuous Sets the reception in continuous mode
  205. * [false: single mode, true: continuous mode]
  206. */
  207. void ( *SetRxConfig )( RadioModems_t modem, uint32_t bandwidth,
  208. uint32_t datarate, uint8_t coderate,
  209. uint32_t bandwidthAfc, uint16_t preambleLen,
  210. uint16_t symbTimeout, bool fixLen,
  211. uint8_t payloadLen,
  212. bool crcOn, bool freqHopOn, uint8_t hopPeriod,
  213. bool iqInverted, bool rxContinuous );
  214. /*!
  215. * \brief Sets the transmission parameters
  216. *
  217. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  218. * \param [IN] power Sets the output power [dBm]
  219. * \param [IN] fdev Sets the frequency deviation (FSK only)
  220. * FSK : [Hz]
  221. * LoRa: 0
  222. * \param [IN] bandwidth Sets the bandwidth (LoRa only)
  223. * FSK : 0
  224. * LoRa: [0: 125 kHz, 1: 250 kHz,
  225. * 2: 500 kHz, 3: Reserved]
  226. * \param [IN] datarate Sets the Datarate
  227. * FSK : 600..300000 bits/s
  228. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  229. * 10: 1024, 11: 2048, 12: 4096 chips]
  230. * \param [IN] coderate Sets the coding rate (LoRa only)
  231. * FSK : N/A ( set to 0 )
  232. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  233. * \param [IN] preambleLen Sets the preamble length
  234. * FSK : Number of bytes
  235. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  236. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  237. * \param [IN] crcOn Enables disables the CRC [0: OFF, 1: ON]
  238. * \param [IN] freqHopOn Enables disables the intra-packet frequency hopping
  239. * FSK : N/A ( set to 0 )
  240. * LoRa: [0: OFF, 1: ON]
  241. * \param [IN] hopPeriod Number of symbols between each hop
  242. * FSK : N/A ( set to 0 )
  243. * LoRa: Number of symbols
  244. * \param [IN] iqInverted Inverts IQ signals (LoRa only)
  245. * FSK : N/A ( set to 0 )
  246. * LoRa: [0: not inverted, 1: inverted]
  247. * \param [IN] timeout Transmission timeout [ms]
  248. */
  249. void ( *SetTxConfig )( RadioModems_t modem, int8_t power, uint32_t fdev,
  250. uint32_t bandwidth, uint32_t datarate,
  251. uint8_t coderate, uint16_t preambleLen,
  252. bool fixLen, bool crcOn, bool freqHopOn,
  253. uint8_t hopPeriod, bool iqInverted, uint32_t timeout );
  254. /*!
  255. * \brief Checks if the given RF frequency is supported by the hardware
  256. *
  257. * \param [IN] frequency RF frequency to be checked
  258. * \retval isSupported [true: supported, false: unsupported]
  259. */
  260. bool ( *CheckRfFrequency )( uint32_t frequency );
  261. /*!
  262. * \brief Computes the packet time on air in ms for the given payload
  263. *
  264. * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
  265. *
  266. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  267. * \param [IN] bandwidth Sets the bandwidth
  268. * FSK : >= 2600 and <= 250000 Hz
  269. * LoRa: [0: 125 kHz, 1: 250 kHz,
  270. * 2: 500 kHz, 3: Reserved]
  271. * \param [IN] datarate Sets the Datarate
  272. * FSK : 600..300000 bits/s
  273. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  274. * 10: 1024, 11: 2048, 12: 4096 chips]
  275. * \param [IN] coderate Sets the coding rate (LoRa only)
  276. * FSK : N/A ( set to 0 )
  277. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  278. * \param [IN] preambleLen Sets the Preamble length
  279. * FSK : Number of bytes
  280. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  281. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  282. * \param [IN] payloadLen Sets payload length when fixed length is used
  283. * \param [IN] crcOn Enables/Disables the CRC [0: OFF, 1: ON]
  284. *
  285. * \retval airTime Computed airTime (ms) for the given packet payload length
  286. */
  287. uint32_t ( *TimeOnAir )( RadioModems_t modem, uint32_t bandwidth,
  288. uint32_t datarate, uint8_t coderate,
  289. uint16_t preambleLen, bool fixLen, uint8_t payloadLen,
  290. bool crcOn );
  291. /*!
  292. * \brief Sends the buffer of size. Prepares the packet to be sent and sets
  293. * the radio in transmission
  294. *
  295. * \param [IN]: buffer Buffer pointer
  296. * \param [IN]: size Buffer size
  297. */
  298. void ( *Send )( uint8_t *buffer, uint8_t size );
  299. /*!
  300. * \brief Sets the radio in sleep mode
  301. */
  302. void ( *Sleep )( void );
  303. /*!
  304. * \brief Sets the radio in standby mode
  305. */
  306. void ( *Standby )( void );
  307. /*!
  308. * \brief Sets the radio in reception mode for the given time
  309. * \param [IN] timeout Reception timeout [ms]
  310. * [0: continuous, others timeout]
  311. */
  312. void ( *Rx )( uint32_t timeout );
  313. /*!
  314. * \brief Start a Channel Activity Detection
  315. */
  316. void ( *StartCad )( void );
  317. /*!
  318. * \brief Sets the radio in continuous wave transmission mode
  319. *
  320. * \param [IN]: freq Channel RF frequency
  321. * \param [IN]: power Sets the output power [dBm]
  322. * \param [IN]: time Transmission mode timeout [s]
  323. */
  324. void ( *SetTxContinuousWave )( uint32_t freq, int8_t power, uint16_t time );
  325. /*!
  326. * \brief Reads the current RSSI value
  327. *
  328. * \retval rssiValue Current RSSI value in [dBm]
  329. */
  330. int16_t ( *Rssi )( RadioModems_t modem );
  331. /*!
  332. * \brief Writes the radio register at the specified address
  333. *
  334. * \param [IN]: addr Register address
  335. * \param [IN]: data New register value
  336. */
  337. void ( *Write )( uint32_t addr, uint8_t data );
  338. /*!
  339. * \brief Reads the radio register at the specified address
  340. *
  341. * \param [IN]: addr Register address
  342. * \retval data Register value
  343. */
  344. uint8_t ( *Read )( uint32_t addr );
  345. /*!
  346. * \brief Writes multiple radio registers starting at address
  347. *
  348. * \param [IN] addr First Radio register address
  349. * \param [IN] buffer Buffer containing the new register's values
  350. * \param [IN] size Number of registers to be written
  351. */
  352. void ( *WriteBuffer )( uint32_t addr, uint8_t *buffer, uint8_t size );
  353. /*!
  354. * \brief Reads multiple radio registers starting at address
  355. *
  356. * \param [IN] addr First Radio register address
  357. * \param [OUT] buffer Buffer where to copy the registers data
  358. * \param [IN] size Number of registers to be read
  359. */
  360. void ( *ReadBuffer )( uint32_t addr, uint8_t *buffer, uint8_t size );
  361. /*!
  362. * \brief Sets the maximum payload length.
  363. *
  364. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  365. * \param [IN] max Maximum payload length in bytes
  366. */
  367. void ( *SetMaxPayloadLength )( RadioModems_t modem, uint8_t max );
  368. /*!
  369. * \brief Sets the network to public or private. Updates the sync byte.
  370. *
  371. * \remark Applies to LoRa modem only
  372. *
  373. * \param [IN] enable if true, it enables a public network
  374. */
  375. void ( *SetPublicNetwork )( bool enable );
  376. /*!
  377. * \brief Gets the time required for the board plus radio to get out of sleep.[ms]
  378. *
  379. * \retval time Radio plus board wakeup time in ms.
  380. */
  381. uint32_t ( *GetWakeupTime )( void );
  382. /*!
  383. * \brief Process radio irq
  384. */
  385. void ( *IrqProcess )( void );
  386. /*
  387. * The next functions are available only on SX126x radios.
  388. */
  389. /*!
  390. * \brief Sets the radio in reception mode with Max LNA gain for the given time
  391. *
  392. * \remark Available on SX126x radios only.
  393. *
  394. * \param [IN] timeout Reception timeout [ms]
  395. * [0: continuous, others timeout]
  396. */
  397. void ( *RxBoosted )( uint32_t timeout );
  398. /*!
  399. * \brief Sets the Rx duty cycle management parameters
  400. *
  401. * \remark Available on SX126x radios only.
  402. *
  403. * \param [in] rxTime Structure describing reception timeout value
  404. * \param [in] sleepTime Structure describing sleep timeout value
  405. */
  406. void ( *SetRxDutyCycle ) ( uint32_t rxTime, uint32_t sleepTime );
  407. };
  408. /*!
  409. * \brief Gets the current Radio OperationMode variable
  410. *
  411. * \retval RadioOperatingModes_t last operating mode
  412. */
  413. RadioOperatingModes_t SX126xGetOperatingMode( void );
  414. /*!
  415. * \brief Sets/Updates the current Radio OperationMode variable.
  416. *
  417. * \remark WARNING: This function is only required to reflect the current radio
  418. * operating mode when processing interrupts.
  419. *
  420. * \param [in] mode New operating mode
  421. */
  422. void SX126xSetOperatingMode( RadioOperatingModes_t mode );
  423. /*!
  424. * \brief Initializes the TCXO power pin.
  425. */
  426. void SX126xIoTcxoInit( void );
  427. /*!
  428. * \brief Gets the Defines the time required for the TCXO to wakeup [ms].
  429. *
  430. * \retval time Board TCXO wakeup time in ms.
  431. */
  432. uint32_t SX126xGetBoardTcxoWakeupTime( void );
  433. /*!
  434. * \brief Initializes RF switch control pins.
  435. */
  436. void SX126xIoRfSwitchInit( void );
  437. /*!
  438. * \brief Initializes the RF Switch I/Os pins interface
  439. */
  440. void SX126xAntSwOn( void );
  441. /*!
  442. * \brief De-initializes the RF Switch I/Os pins interface
  443. *
  444. * \remark Needed to decrease the power consumption in MCU low power modes
  445. */
  446. void SX126xAntSwOff( void );
  447. /*!
  448. * \brief Gets the device ID
  449. *
  450. * \retval id Connected device ID
  451. */
  452. uint8_t SX126xGetDeviceId( void );
  453. /*!
  454. * \brief Sets the radio output power.
  455. *
  456. * \param [IN] power Sets the RF output power
  457. */
  458. void SX126xSetRfTxPower( int8_t power );
  459. /*!
  460. * \brief Write a single byte of data to the radio memory
  461. *
  462. * \param [in] address The address of the first byte to write in the radio
  463. * \param [in] value The data to be written in radio's memory
  464. */
  465. void SX126xWriteRegister( uint16_t address, uint8_t value );
  466. /*!
  467. * \brief Read a single byte of data from the radio memory
  468. *
  469. * \param [in] address The address of the first byte to write in the radio
  470. *
  471. * \retval value The value of the byte at the given address in radio's memory
  472. */
  473. uint8_t SX126xReadRegister( uint16_t address );
  474. /*!
  475. * \brief Wakes up the radio
  476. */
  477. void SX126xWakeup( void );
  478. /*!
  479. * \brief Send a command that write data to the radio
  480. *
  481. * \param [in] opcode Opcode of the command
  482. * \param [in] buffer Buffer to be send to the radio
  483. * \param [in] size Size of the buffer to send
  484. */
  485. void SX126xWriteCommand( RadioCommands_t opcode, uint8_t *buffer, uint16_t size );
  486. /*!
  487. * \brief Send a command that read data from the radio
  488. *
  489. * \param [in] opcode Opcode of the command
  490. * \param [out] buffer Buffer holding data from the radio
  491. * \param [in] size Size of the buffer
  492. *
  493. * \retval status Return command radio status
  494. */
  495. uint8_t SX126xReadCommand( RadioCommands_t opcode, uint8_t *buffer, uint16_t size );
  496. /*!
  497. * \brief Tx timeout timer callback
  498. */
  499. void RadioOnTxTimeoutIrq( void* context );
  500. /*!
  501. * \brief Rx timeout timer callback
  502. */
  503. void RadioOnRxTimeoutIrq( void* context );
  504. /*!
  505. * \brief Radio driver
  506. *
  507. * \remark This variable is defined and initialized in the specific radio
  508. * board implementation
  509. */
  510. extern const struct Radio_s Radio;
  511. uint32_t RadioTimeOnAir2( RadioModems_t modem, uint8_t pktLen );
  512. #ifdef __cplusplus
  513. }
  514. #endif
  515. #endif // __RADIO_H__