radio.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #include<stdint.h>
  26. #include<stdbool.h>
  27. #define USE_MODEM_LORA
  28. // #define USE_MODEM_FSK
  29. /*!
  30. * Radio driver supported modems
  31. */
  32. typedef enum
  33. {
  34. MODEM_FSK = 0,
  35. MODEM_LORA,
  36. }RadioModems_t;
  37. /*!
  38. * Radio driver internal state machine states definition
  39. */
  40. typedef enum
  41. {
  42. RF_IDLE = 0, //!< The radio is idle
  43. RF_RX_RUNNING, //!< The radio is in reception state
  44. RF_TX_RUNNING, //!< The radio is in transmission state
  45. RF_CAD, //!< The radio is doing channel activity detection
  46. }RadioState_t;
  47. /*!
  48. * \brief Radio driver callback functions
  49. */
  50. typedef struct
  51. {
  52. /*!
  53. * \brief Tx Done callback prototype.
  54. */
  55. void ( *TxDone )( void );
  56. /*!
  57. * \brief Tx Timeout callback prototype.
  58. */
  59. void ( *TxTimeout )( void );
  60. /*!
  61. * \brief Rx Done callback prototype.
  62. *
  63. * \param [IN] payload Received buffer pointer
  64. * \param [IN] size Received buffer size
  65. * \param [IN] rssi RSSI value computed while receiving the frame [dBm]
  66. * \param [IN] snr Raw SNR value given by the radio hardware
  67. * FSK : N/A ( set to 0 )
  68. * LoRa: SNR value in dB
  69. */
  70. void ( *RxDone )( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
  71. /*!
  72. * \brief Rx Timeout callback prototype.
  73. */
  74. void ( *RxTimeout )( void );
  75. /*!
  76. * \brief Rx Error callback prototype.
  77. */
  78. void ( *RxError )( void );
  79. /*!
  80. * \brief FHSS Change Channel callback prototype.
  81. *
  82. * \param [IN] currentChannel Index number of the current channel
  83. */
  84. void ( *FhssChangeChannel )( uint8_t currentChannel );
  85. /*!
  86. * \brief CAD Done callback prototype.
  87. *
  88. * \param [IN] channelDetected Channel Activity detected during the CAD
  89. */
  90. void ( *CadDone ) ( bool channelActivityDetected );
  91. }RadioEvents_t;
  92. /*!
  93. * \brief Radio driver definition
  94. */
  95. struct Radio_s
  96. {
  97. /*!
  98. * \brief Initializes the radio
  99. *
  100. * \param [IN] events Structure containing the driver callback functions
  101. */
  102. void ( *Init )( RadioEvents_t *events );
  103. /*!
  104. * Return current radio status
  105. *
  106. * \param status Radio status.[RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
  107. */
  108. RadioState_t ( *GetStatus )( void );
  109. /*!
  110. * \brief Configures the radio with the given modem
  111. *
  112. * \param [IN] modem Modem to be used [0: FSK, 1: LoRa]
  113. */
  114. void ( *SetModem )( RadioModems_t modem );
  115. /*!
  116. * \brief Sets the channel frequency
  117. *
  118. * \param [IN] freq Channel RF frequency
  119. */
  120. void ( *SetChannel )( uint32_t freq );
  121. /*!
  122. * \brief Checks if the channel is free for the given time
  123. *
  124. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  125. * \param [IN] freq Channel RF frequency
  126. * \param [IN] rssiThresh RSSI threshold
  127. * \param [IN] maxCarrierSenseTime Max time while the RSSI is measured
  128. *
  129. * \retval isFree [true: Channel is free, false: Channel is not free]
  130. */
  131. bool ( *IsChannelFree )( RadioModems_t modem, uint32_t freq, int16_t rssiThresh, uint32_t maxCarrierSenseTime );
  132. /*!
  133. * \brief Generates a 32 bits random value based on the RSSI readings
  134. *
  135. * \remark This function sets the radio in LoRa modem mode and disables
  136. * all interrupts.
  137. * After calling this function either Radio.SetRxConfig or
  138. * Radio.SetTxConfig functions must be called.
  139. *
  140. * \retval randomValue 32 bits random value
  141. */
  142. uint32_t ( *Random )( void );
  143. /*!
  144. * \brief Sets the reception parameters
  145. *
  146. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  147. * \param [IN] bandwidth Sets the bandwidth
  148. * FSK : >= 2600 and <= 250000 Hz
  149. * LoRa: [0: 125 kHz, 1: 250 kHz,
  150. * 2: 500 kHz, 3: Reserved]
  151. * \param [IN] datarate Sets the Datarate
  152. * FSK : 600..300000 bits/s
  153. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  154. * 10: 1024, 11: 2048, 12: 4096 chips]
  155. * \param [IN] coderate Sets the coding rate (LoRa only)
  156. * FSK : N/A ( set to 0 )
  157. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  158. * \param [IN] bandwidthAfc Sets the AFC Bandwidth (FSK only)
  159. * FSK : >= 2600 and <= 250000 Hz
  160. * LoRa: N/A ( set to 0 )
  161. * \param [IN] preambleLen Sets the Preamble length
  162. * FSK : Number of bytes
  163. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  164. * \param [IN] symbTimeout Sets the RxSingle timeout value
  165. * FSK : timeout in number of bytes
  166. * LoRa: timeout in symbols
  167. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  168. * \param [IN] payloadLen Sets payload length when fixed length is used
  169. * \param [IN] crcOn Enables/Disables the CRC [0: OFF, 1: ON]
  170. * \param [IN] freqHopOn Enables disables the intra-packet frequency hopping
  171. * FSK : N/A ( set to 0 )
  172. * LoRa: [0: OFF, 1: ON]
  173. * \param [IN] hopPeriod Number of symbols between each hop
  174. * FSK : N/A ( set to 0 )
  175. * LoRa: Number of symbols
  176. * \param [IN] iqInverted Inverts IQ signals (LoRa only)
  177. * FSK : N/A ( set to 0 )
  178. * LoRa: [0: not inverted, 1: inverted]
  179. * \param [IN] rxContinuous Sets the reception in continuous mode
  180. * [false: single mode, true: continuous mode]
  181. */
  182. void ( *SetRxConfig )( RadioModems_t modem, uint32_t bandwidth,
  183. uint32_t datarate, uint8_t coderate,
  184. uint32_t bandwidthAfc, uint16_t preambleLen,
  185. uint16_t symbTimeout, bool fixLen,
  186. uint8_t payloadLen,
  187. bool crcOn, bool freqHopOn, uint8_t hopPeriod,
  188. bool iqInverted, bool rxContinuous );
  189. /*!
  190. * \brief Sets the transmission parameters
  191. *
  192. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  193. * \param [IN] power Sets the output power [dBm]
  194. * \param [IN] fdev Sets the frequency deviation (FSK only)
  195. * FSK : [Hz]
  196. * LoRa: 0
  197. * \param [IN] bandwidth Sets the bandwidth (LoRa only)
  198. * FSK : 0
  199. * LoRa: [0: 125 kHz, 1: 250 kHz,
  200. * 2: 500 kHz, 3: Reserved]
  201. * \param [IN] datarate Sets the Datarate
  202. * FSK : 600..300000 bits/s
  203. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  204. * 10: 1024, 11: 2048, 12: 4096 chips]
  205. * \param [IN] coderate Sets the coding rate (LoRa only)
  206. * FSK : N/A ( set to 0 )
  207. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  208. * \param [IN] preambleLen Sets the preamble length
  209. * FSK : Number of bytes
  210. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  211. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  212. * \param [IN] crcOn Enables disables the CRC [0: OFF, 1: ON]
  213. * \param [IN] freqHopOn Enables disables the intra-packet frequency hopping
  214. * FSK : N/A ( set to 0 )
  215. * LoRa: [0: OFF, 1: ON]
  216. * \param [IN] hopPeriod Number of symbols between each hop
  217. * FSK : N/A ( set to 0 )
  218. * LoRa: Number of symbols
  219. * \param [IN] iqInverted Inverts IQ signals (LoRa only)
  220. * FSK : N/A ( set to 0 )
  221. * LoRa: [0: not inverted, 1: inverted]
  222. * \param [IN] timeout Transmission timeout [ms]
  223. */
  224. void ( *SetTxConfig )( RadioModems_t modem, int8_t power, uint32_t fdev,
  225. uint32_t bandwidth, uint32_t datarate,
  226. uint8_t coderate, uint16_t preambleLen,
  227. bool fixLen, bool crcOn, bool freqHopOn,
  228. uint8_t hopPeriod, bool iqInverted, uint32_t timeout );
  229. /*!
  230. * \brief Checks if the given RF frequency is supported by the hardware
  231. *
  232. * \param [IN] frequency RF frequency to be checked
  233. * \retval isSupported [true: supported, false: unsupported]
  234. */
  235. bool ( *CheckRfFrequency )( uint32_t frequency );
  236. /*!
  237. * \brief Computes the packet time on air in ms for the given payload
  238. *
  239. * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
  240. *
  241. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  242. * \param [IN] pktLen Packet payload length
  243. *
  244. * \retval airTime Computed airTime (ms) for the given packet payload length
  245. */
  246. uint32_t ( *TimeOnAir )( RadioModems_t modem, uint8_t pktLen );
  247. /*!
  248. * \brief Sends the buffer of size. Prepares the packet to be sent and sets
  249. * the radio in transmission
  250. *
  251. * \param [IN]: buffer Buffer pointer
  252. * \param [IN]: size Buffer size
  253. */
  254. void ( *Send )( uint8_t *buffer, uint8_t size );
  255. /*!
  256. * \brief Sets the radio in sleep mode
  257. */
  258. void ( *Sleep )( void );
  259. /*!
  260. * \brief Sets the radio in standby mode
  261. */
  262. void ( *Standby )( void );
  263. /*!
  264. * \brief Sets the radio in reception mode for the given time
  265. * \param [IN] timeout Reception timeout [ms]
  266. * [0: continuous, others timeout]
  267. */
  268. void ( *Rx )( uint32_t timeout );
  269. /*!
  270. * \brief Start a Channel Activity Detection
  271. */
  272. void ( *StartCad )( void );
  273. /*!
  274. * \brief Sets the radio in continuous wave transmission mode
  275. *
  276. * \param [IN]: freq Channel RF frequency
  277. * \param [IN]: power Sets the output power [dBm]
  278. * \param [IN]: time Transmission mode timeout [s]
  279. */
  280. void ( *SetTxContinuousWave )( uint32_t freq, int8_t power, uint16_t time );
  281. /*!
  282. * \brief Reads the current RSSI value
  283. *
  284. * \retval rssiValue Current RSSI value in [dBm]
  285. */
  286. int16_t ( *Rssi )( RadioModems_t modem );
  287. /*!
  288. * \brief Writes the radio register at the specified address
  289. *
  290. * \param [IN]: addr Register address
  291. * \param [IN]: data New register value
  292. */
  293. void ( *Write )( uint16_t addr, uint8_t data );
  294. /*!
  295. * \brief Reads the radio register at the specified address
  296. *
  297. * \param [IN]: addr Register address
  298. * \retval data Register value
  299. */
  300. uint8_t ( *Read )( uint16_t addr );
  301. /*!
  302. * \brief Writes multiple radio registers starting at address
  303. *
  304. * \param [IN] addr First Radio register address
  305. * \param [IN] buffer Buffer containing the new register's values
  306. * \param [IN] size Number of registers to be written
  307. */
  308. void ( *WriteBuffer )( uint16_t addr, uint8_t *buffer, uint8_t size );
  309. /*!
  310. * \brief Reads multiple radio registers starting at address
  311. *
  312. * \param [IN] addr First Radio register address
  313. * \param [OUT] buffer Buffer where to copy the registers data
  314. * \param [IN] size Number of registers to be read
  315. */
  316. void ( *ReadBuffer )( uint16_t addr, uint8_t *buffer, uint8_t size );
  317. /*!
  318. * \brief Sets the maximum payload length.
  319. *
  320. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  321. * \param [IN] max Maximum payload length in bytes
  322. */
  323. void ( *SetMaxPayloadLength )( RadioModems_t modem, uint8_t max );
  324. /*!
  325. * \brief Sets the network to public or private. Updates the sync byte.
  326. *
  327. * \remark Applies to LoRa modem only
  328. *
  329. * \param [IN] enable if true, it enables a public network
  330. */
  331. void ( *SetPublicNetwork )( bool enable );
  332. /*!
  333. * \brief Gets the time required for the board plus radio to get out of sleep.[ms]
  334. *
  335. * \retval time Radio plus board wakeup time in ms.
  336. */
  337. uint32_t ( *GetWakeupTime )( void );
  338. /*!
  339. * \brief Process radio irq
  340. */
  341. void ( *IrqProcess )( void );
  342. /*
  343. * The next functions are available only on SX126x radios.
  344. */
  345. /*!
  346. * \brief Sets the radio in reception mode with Max LNA gain for the given time
  347. *
  348. * \remark Available on SX126x radios only.
  349. *
  350. * \param [IN] timeout Reception timeout [ms]
  351. * [0: continuous, others timeout]
  352. */
  353. void ( *RxBoosted )( uint32_t timeout );
  354. /*!
  355. * \brief Sets the Rx duty cycle management parameters
  356. *
  357. * \remark Available on SX126x radios only.
  358. *
  359. * \param [in] rxTime Structure describing reception timeout value
  360. * \param [in] sleepTime Structure describing sleep timeout value
  361. */
  362. void ( *SetRxDutyCycle ) ( uint32_t rxTime, uint32_t sleepTime );
  363. };
  364. /*!
  365. * \brief Radio driver
  366. *
  367. * \remark This variable is defined and initialized in the specific radio
  368. * board implementation
  369. */
  370. extern const struct Radio_s Radio;
  371. #endif // __RADIO_H__