radio.h 15 KB

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