radio.h 14 KB

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