radio.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. / _____) _ | |
  3. ( (____ _____ ____ _| |_ _____ ____| |__
  4. \____ \| ___ | (_ _) ___ |/ ___) _ \
  5. _____) ) ____| | | || |_| ____( (___| | | |
  6. (______/|_____)_|_|_| \__)_____)\____)_| |_|
  7. (C)2013 Semtech
  8. Description: Generic radio driver definition
  9. License: Revised BSD License, see LICENSE.TXT file include in the project
  10. Maintainer: Miguel Luis and Gregory Cristian
  11. */
  12. #ifndef __RADIO_H__
  13. #define __RADIO_H__
  14. #include "stdbool.h" //bool¶¨Òå
  15. #include "stdint.h" //uint8_tµÈÊý¾ÝÀàÐͶ¨Òå
  16. /*!
  17. * Radio driver supported modems
  18. */
  19. typedef enum
  20. {
  21. MODEM_FSK = 0,
  22. MODEM_LORA,
  23. }RadioModems_t;
  24. /*!
  25. * Radio driver internal state machine states definition
  26. */
  27. typedef enum
  28. {
  29. RF_IDLE = 0,
  30. RF_RX_RUNNING,
  31. RF_TX_RUNNING,
  32. RF_CAD,
  33. }RadioState_t;
  34. /*!
  35. * \brief Radio driver callback functions
  36. */
  37. typedef struct
  38. {
  39. /*!
  40. * \brief Tx Done callback prototype.
  41. */
  42. void ( *TxDone )( void );
  43. /*!
  44. * \brief Tx Timeout callback prototype.
  45. */
  46. void ( *TxTimeout )( void );
  47. /*!
  48. * \brief Rx Done callback prototype.
  49. *
  50. * \param [IN] payload Received buffer pointer
  51. * \param [IN] size Received buffer size
  52. * \param [IN] rssi RSSI value computed while receiving the frame [dBm]
  53. * \param [IN] snr Raw SNR value given by the radio hardware
  54. * FSK : N/A ( set to 0 )
  55. * LoRa: SNR value in dB
  56. */
  57. void ( *RxDone )( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
  58. /*!
  59. * \brief Rx Timeout callback prototype.
  60. */
  61. void ( *RxTimeout )( void );
  62. /*!
  63. * \brief Rx Error callback prototype.
  64. */
  65. void ( *RxError )( void );
  66. /*!
  67. * \brief FHSS Change Channel callback prototype.
  68. *
  69. * \param [IN] currentChannel Index number of the current channel
  70. */
  71. void ( *FhssChangeChannel )( uint8_t currentChannel );
  72. /*!
  73. * \brief CAD Done callback prototype.
  74. *
  75. * \param [IN] channelDetected Channel Activity detected during the CAD
  76. */
  77. void ( *CadDone ) ( bool channelActivityDetected );
  78. }RadioEvents_t;
  79. /*!
  80. * \brief Radio driver definition
  81. */
  82. struct Radio_s
  83. {
  84. /*!
  85. * \brief Initializes the radio
  86. *
  87. * \param [IN] events Structure containing the driver callback functions
  88. */
  89. void ( *Init )( RadioEvents_t *events );
  90. /*!
  91. * Return current radio status
  92. *
  93. * \param status Radio status.[RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
  94. */
  95. RadioState_t ( *GetStatus )( void );
  96. /*!
  97. * \brief Configures the radio with the given modem
  98. *
  99. * \param [IN] modem Modem to be used [0: FSK, 1: LoRa]
  100. */
  101. void ( *SetModem )( RadioModems_t modem );
  102. /*!
  103. * \brief Sets the channel frequency
  104. *
  105. * \param [IN] freq Channel RF frequency
  106. */
  107. void ( *SetChannel )( uint32_t freq );
  108. /*!
  109. * \brief Sets the channels configuration
  110. *
  111. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  112. * \param [IN] freq Channel RF frequency
  113. * \param [IN] rssiThresh RSSI threshold
  114. *
  115. * \retval isFree [true: Channel is free, false: Channel is not free]
  116. */
  117. bool ( *IsChannelFree )( RadioModems_t modem, uint32_t freq, int16_t rssiThresh );
  118. /*!
  119. * \brief Generates a 32 bits random value based on the RSSI readings
  120. *
  121. * \remark This function sets the radio in LoRa modem mode and disables
  122. * all interrupts.
  123. * After calling this function either Radio.SetRxConfig or
  124. * Radio.SetTxConfig functions must be called.
  125. *
  126. * \retval randomValue 32 bits random value
  127. */
  128. uint32_t ( *Random )( void );
  129. /*!
  130. * \brief Sets the reception parameters
  131. *
  132. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  133. * \param [IN] bandwidth Sets the bandwidth
  134. * FSK : >= 2600 and <= 250000 Hz
  135. * LoRa: [0: 125 kHz, 1: 250 kHz,
  136. * 2: 500 kHz, 3: Reserved]
  137. * \param [IN] datarate Sets the Datarate
  138. * FSK : 600..300000 bits/s
  139. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  140. * 10: 1024, 11: 2048, 12: 4096 chips]
  141. * \param [IN] coderate Sets the coding rate (LoRa only)
  142. * FSK : N/A ( set to 0 )
  143. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  144. * \param [IN] bandwidthAfc Sets the AFC Bandwidth (FSK only)
  145. * FSK : >= 2600 and <= 250000 Hz
  146. * LoRa: N/A ( set to 0 )
  147. * \param [IN] preambleLen Sets the Preamble length
  148. * FSK : Number of bytes
  149. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  150. * \param [IN] symbTimeout Sets the RxSingle timeout value (LoRa only)
  151. * FSK : N/A ( set to 0 )
  152. * LoRa: timeout in symbols
  153. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  154. * \param [IN] payloadLen Sets payload length when fixed lenght is used
  155. * \param [IN] crcOn Enables/Disables the CRC [0: OFF, 1: ON]
  156. * \param [IN] FreqHopOn Enables disables the intra-packet frequency hopping
  157. * FSK : N/A ( set to 0 )
  158. * LoRa: [0: OFF, 1: ON]
  159. * \param [IN] HopPeriod Number of symbols bewteen each hop
  160. * FSK : N/A ( set to 0 )
  161. * LoRa: Number of symbols
  162. * \param [IN] iqInverted Inverts IQ signals (LoRa only)
  163. * FSK : N/A ( set to 0 )
  164. * LoRa: [0: not inverted, 1: inverted]
  165. * \param [IN] rxContinuous Sets the reception in continuous mode
  166. * [false: single mode, true: continuous mode]
  167. */
  168. void ( *SetRxConfig )( RadioModems_t modem, uint32_t bandwidth,
  169. uint32_t datarate, uint8_t coderate,
  170. uint32_t bandwidthAfc, uint16_t preambleLen,
  171. uint16_t symbTimeout, bool fixLen,
  172. uint8_t payloadLen,
  173. bool crcOn, bool FreqHopOn, uint8_t HopPeriod,
  174. bool iqInverted, bool rxContinuous );
  175. /*!
  176. * \brief Sets the transmission parameters
  177. *
  178. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  179. * \param [IN] power Sets the output power [dBm]
  180. * \param [IN] fdev Sets the frequency deviation (FSK only)
  181. * FSK : [Hz]
  182. * LoRa: 0
  183. * \param [IN] bandwidth Sets the bandwidth (LoRa only)
  184. * FSK : 0
  185. * LoRa: [0: 125 kHz, 1: 250 kHz,
  186. * 2: 500 kHz, 3: Reserved]
  187. * \param [IN] datarate Sets the Datarate
  188. * FSK : 600..300000 bits/s
  189. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  190. * 10: 1024, 11: 2048, 12: 4096 chips]
  191. * \param [IN] coderate Sets the coding rate (LoRa only)
  192. * FSK : N/A ( set to 0 )
  193. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  194. * \param [IN] preambleLen Sets the preamble length
  195. * FSK : Number of bytes
  196. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  197. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  198. * \param [IN] crcOn Enables disables the CRC [0: OFF, 1: ON]
  199. * \param [IN] FreqHopOn Enables disables the intra-packet frequency hopping
  200. * FSK : N/A ( set to 0 )
  201. * LoRa: [0: OFF, 1: ON]
  202. * \param [IN] HopPeriod Number of symbols bewteen each hop
  203. * FSK : N/A ( set to 0 )
  204. * LoRa: Number of symbols
  205. * \param [IN] iqInverted Inverts IQ signals (LoRa only)
  206. * FSK : N/A ( set to 0 )
  207. * LoRa: [0: not inverted, 1: inverted]
  208. * \param [IN] timeout Transmission timeout [ms]
  209. */
  210. void ( *SetTxConfig )( RadioModems_t modem, int8_t power, uint32_t fdev,
  211. uint32_t bandwidth, uint32_t datarate,
  212. uint8_t coderate, uint16_t preambleLen,
  213. bool fixLen, bool crcOn, bool FreqHopOn,
  214. uint8_t HopPeriod, bool iqInverted, uint32_t timeout );
  215. /*!
  216. * \brief Checks if the given RF frequency is supported by the hardware
  217. *
  218. * \param [IN] frequency RF frequency to be checked
  219. * \retval isSupported [true: supported, false: unsupported]
  220. */
  221. bool ( *CheckRfFrequency )( uint32_t frequency );
  222. /*!
  223. * \brief Computes the packet time on air in us for the given payload
  224. *
  225. * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
  226. *
  227. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  228. * \param [IN] pktLen Packet payload length
  229. *
  230. * \retval airTime Computed airTime (us) for the given packet payload length
  231. */
  232. uint32_t ( *TimeOnAir )( RadioModems_t modem, uint8_t pktLen );
  233. /*!
  234. * \brief Sends the buffer of size. Prepares the packet to be sent and sets
  235. * the radio in transmission
  236. *
  237. * \param [IN]: buffer Buffer pointer
  238. * \param [IN]: size Buffer size
  239. */
  240. void ( *Send )( uint8_t *buffer, uint8_t size );
  241. /*!
  242. * \brief Sets the radio in sleep mode
  243. */
  244. void ( *Sleep )( void );
  245. /*!
  246. * \brief Sets the radio in standby mode
  247. */
  248. void ( *Standby )( void );
  249. /*!
  250. * \brief Sets the radio in reception mode for the given time
  251. * \param [IN] timeout Reception timeout [ms]
  252. * [0: continuous, others timeout]
  253. */
  254. void ( *Rx )( uint32_t timeout );
  255. /*!
  256. * \brief Start a Channel Activity Detection
  257. */
  258. void ( *StartCad )( void );
  259. /*!
  260. * \brief Reads the current RSSI value
  261. *
  262. * \retval rssiValue Current RSSI value in [dBm]
  263. */
  264. int16_t ( *Rssi )( RadioModems_t modem );
  265. /*!
  266. * \brief Writes the radio register at the specified address
  267. *
  268. * \param [IN]: addr Register address
  269. * \param [IN]: data New register value
  270. */
  271. void ( *Write )( uint8_t addr, uint8_t data );
  272. /*!
  273. * \brief Reads the radio register at the specified address
  274. *
  275. * \param [IN]: addr Register address
  276. * \retval data Register value
  277. */
  278. uint8_t ( *Read )( uint8_t addr );
  279. /*!
  280. * \brief Writes multiple radio registers starting at address
  281. *
  282. * \param [IN] addr First Radio register address
  283. * \param [IN] buffer Buffer containing the new register's values
  284. * \param [IN] size Number of registers to be written
  285. */
  286. void ( *WriteBuffer )( uint8_t addr, uint8_t *buffer, uint8_t size );
  287. /*!
  288. * \brief Reads multiple radio registers starting at address
  289. *
  290. * \param [IN] addr First Radio register address
  291. * \param [OUT] buffer Buffer where to copy the registers data
  292. * \param [IN] size Number of registers to be read
  293. */
  294. void ( *ReadBuffer )( uint8_t addr, uint8_t *buffer, uint8_t size );
  295. /*!
  296. * \brief Sets the maximum payload length.
  297. *
  298. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  299. * \param [IN] max Maximum payload length in bytes
  300. */
  301. void ( *SetMaxPayloadLength )( RadioModems_t modem, uint8_t max );
  302. };
  303. /*!
  304. * \brief Radio driver
  305. *
  306. * \remark This variable is defined and initialized in the specific radio
  307. * board implementation
  308. */
  309. extern const struct Radio_s Radio;
  310. #endif // __RADIO_H__