Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

338 строки
14 KiB

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