radio.h 19 KB

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