Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

507 regels
17 KiB

  1. /*
  2. / _____) _ | |
  3. ( (____ _____ ____ _| |_ _____ ____| |__
  4. \____ \| ___ | (_ _) ___ |/ ___) _ \
  5. _____) ) ____| | | || |_| ____( (___| | | |
  6. (______/|_____)_|_|_| \__)_____)\____)_| |_|
  7. (C) 2014 Semtech
  8. Description: Actual implementation of a SX1276 radio, inherits Radio
  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 __SX1276_H__
  13. #define __SX1276_H__
  14. #include "radio.h"
  15. #include "./registers/sx1276Regs-Fsk.h"
  16. #include "./registers/sx1276Regs-LoRa.h"
  17. #include "./typedefs/typedefs.h"
  18. /*!
  19. * Radio wake-up time from sleep
  20. */
  21. #define RADIO_WAKEUP_TIME 1 // [ms]
  22. /*!
  23. * Sync word for Private LoRa networks
  24. */
  25. #define LORA_MAC_PRIVATE_SYNCWORD 0x12
  26. /*!
  27. * Sync word for Public LoRa networks
  28. */
  29. #define LORA_MAC_PUBLIC_SYNCWORD 0x34
  30. /*!
  31. * SX1276 definitions
  32. */
  33. #define XTAL_FREQ 32000000
  34. #define FREQ_STEP 61.03515625
  35. #define RX_BUFFER_SIZE 256
  36. /*!
  37. * Constant values need to compute the RSSI value
  38. */
  39. #define RSSI_OFFSET_LF -164.0
  40. #define RSSI_OFFSET_HF -157.0
  41. #define RF_MID_BAND_THRESH 525000000
  42. /*!
  43. * Actual implementation of a SX1276 radio, inherits Radio
  44. */
  45. class SX1276 : public Radio
  46. {
  47. protected:
  48. /*!
  49. * SPI Interface
  50. */
  51. SPI spi; // mosi, miso, sclk
  52. DigitalOut nss;
  53. /*!
  54. * SX1276 Reset pin
  55. */
  56. DigitalInOut reset;
  57. /*!
  58. * SX1276 DIO pins
  59. */
  60. InterruptIn dio0;
  61. InterruptIn dio1;
  62. InterruptIn dio2;
  63. InterruptIn dio3;
  64. InterruptIn dio4;
  65. DigitalIn dio5;
  66. bool isRadioActive;
  67. uint8_t boardConnected; //1 = SX1276MB1LAS; 0 = SX1276MB1MAS
  68. uint8_t *rxtxBuffer;
  69. /*!
  70. * Hardware DIO IRQ functions
  71. */
  72. DioIrqHandler *dioIrq;
  73. /*!
  74. * Tx and Rx timers
  75. */
  76. Timeout txTimeoutTimer;
  77. Timeout rxTimeoutTimer;
  78. Timeout rxTimeoutSyncWord;
  79. RadioSettings_t settings;
  80. static const FskBandwidth_t FskBandwidths[];
  81. protected:
  82. /*!
  83. * Performs the Rx chain calibration for LF and HF bands
  84. * \remark Must be called just after the reset so all registers are at their
  85. * default values
  86. */
  87. void RxChainCalibration( void );
  88. public:
  89. SX1276( RadioEvents_t *events,
  90. PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset,
  91. PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5 );
  92. SX1276( RadioEvents_t *events );
  93. virtual ~SX1276( );
  94. //-------------------------------------------------------------------------
  95. // Redefined Radio functions
  96. //-------------------------------------------------------------------------
  97. /*!
  98. * @brief Initializes the radio
  99. *
  100. * @param [IN] events Structure containing the driver callback functions
  101. */
  102. virtual void Init( RadioEvents_t *events );
  103. /*!
  104. * Return current radio status
  105. *
  106. * @param status Radio status. [RF_IDLE, RX_RUNNING, TX_RUNNING]
  107. */
  108. virtual RadioState GetStatus( void );
  109. /*!
  110. * @brief Configures the SX1276 with the given modem
  111. *
  112. * @param [IN] modem Modem to be used [0: FSK, 1: LoRa]
  113. */
  114. virtual void SetModem( RadioModems_t modem );
  115. /*!
  116. * @brief Sets the channel frequency
  117. *
  118. * @param [IN] freq Channel RF frequency
  119. */
  120. virtual void SetChannel( uint32_t freq );
  121. /*!
  122. * @brief Sets the channels configuration
  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. *
  128. * @retval isFree [true: Channel is free, false: Channel is not free]
  129. */
  130. virtual bool IsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh );
  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. virtual 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 ( LoRa only )
  161. * FSK : N/A ( set to 0 )
  162. * LoRa: Length in symbols ( the hardware adds 4 more symbols )
  163. * @param [IN] symbTimeout Sets the RxSingle timeout value
  164. * FSK : timeout 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 lenght 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 [0: OFF, 1: ON] (LoRa only)
  170. * @param [IN] hopPeriod Number of symbols bewteen each hop (LoRa only)
  171. * @param [IN] iqInverted Inverts IQ signals ( LoRa only )
  172. * FSK : N/A ( set to 0 )
  173. * LoRa: [0: not inverted, 1: inverted]
  174. * @param [IN] rxContinuous Sets the reception in continuous mode
  175. * [false: single mode, true: continuous mode]
  176. */
  177. virtual void SetRxConfig ( RadioModems_t modem, uint32_t bandwidth,
  178. uint32_t datarate, uint8_t coderate,
  179. uint32_t bandwidthAfc, uint16_t preambleLen,
  180. uint16_t symbTimeout, bool fixLen,
  181. uint8_t payloadLen,
  182. bool crcOn, bool freqHopOn, uint8_t hopPeriod,
  183. bool iqInverted, bool rxContinuous );
  184. /*!
  185. * @brief Sets the transmission parameters
  186. *
  187. * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  188. * @param [IN] power Sets the output power [dBm]
  189. * @param [IN] fdev Sets the frequency deviation ( FSK only )
  190. * FSK : [Hz]
  191. * LoRa: 0
  192. * @param [IN] bandwidth Sets the bandwidth ( LoRa only )
  193. * FSK : 0
  194. * LoRa: [0: 125 kHz, 1: 250 kHz,
  195. * 2: 500 kHz, 3: Reserved]
  196. * @param [IN] datarate Sets the Datarate
  197. * FSK : 600..300000 bits/s
  198. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  199. * 10: 1024, 11: 2048, 12: 4096 chips]
  200. * @param [IN] coderate Sets the coding rate ( LoRa only )
  201. * FSK : N/A ( set to 0 )
  202. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  203. * @param [IN] preambleLen Sets the preamble length
  204. * @param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  205. * @param [IN] crcOn Enables disables the CRC [0: OFF, 1: ON]
  206. * @param [IN] freqHopOn Enables disables the intra-packet frequency hopping [0: OFF, 1: ON] (LoRa only)
  207. * @param [IN] hopPeriod Number of symbols bewteen each hop (LoRa only)
  208. * @param [IN] iqInverted Inverts IQ signals ( LoRa only )
  209. * FSK : N/A ( set to 0 )
  210. * LoRa: [0: not inverted, 1: inverted]
  211. * @param [IN] timeout Transmission timeout [ms]
  212. */
  213. virtual void SetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev,
  214. uint32_t bandwidth, uint32_t datarate,
  215. uint8_t coderate, uint16_t preambleLen,
  216. bool fixLen, bool crcOn, bool freqHopOn,
  217. uint8_t hopPeriod, bool iqInverted, uint32_t timeout );
  218. /*!
  219. * @brief Checks if the given RF frequency is supported by the hardware
  220. *
  221. * @param [IN] frequency RF frequency to be checked
  222. * @retval isSupported [true: supported, false: unsupported]
  223. */
  224. virtual bool CheckRfFrequency( uint32_t frequency ) = 0;
  225. /*!
  226. * @brief Computes the packet time on air for the given payload
  227. *
  228. * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
  229. *
  230. * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  231. * @param [IN] pktLen Packet payload length
  232. *
  233. * @retval airTime Computed airTime for the given packet payload length
  234. */
  235. virtual uint32_t TimeOnAir ( RadioModems_t modem, uint8_t pktLen );
  236. /*!
  237. * @brief Sends the buffer of size. Prepares the packet to be sent and sets
  238. * the radio in transmission
  239. *
  240. * @param [IN]: buffer Buffer pointer
  241. * @param [IN]: size Buffer size
  242. */
  243. virtual void Send( uint8_t *buffer, uint8_t size );
  244. /*!
  245. * @brief Sets the radio in sleep mode
  246. */
  247. virtual void Sleep( void );
  248. /*!
  249. * @brief Sets the radio in standby mode
  250. */
  251. virtual void Standby( void );
  252. /*!
  253. * @brief Sets the radio in CAD mode
  254. */
  255. virtual void StartCad( void );
  256. /*!
  257. * @brief Sets the radio in reception mode for the given time
  258. * @param [IN] timeout Reception timeout [ms]
  259. * [0: continuous, others timeout]
  260. */
  261. virtual void Rx( uint32_t timeout );
  262. /*!
  263. * @brief Sets the radio in transmission mode for the given time
  264. * @param [IN] timeout Transmission timeout [ms]
  265. * [0: continuous, others timeout]
  266. */
  267. virtual void Tx( uint32_t timeout );
  268. /*!
  269. * @brief Sets the radio in continuous wave transmission mode
  270. *
  271. * @param [IN]: freq Channel RF frequency
  272. * @param [IN]: power Sets the output power [dBm]
  273. * @param [IN]: time Transmission mode timeout [s]
  274. */
  275. virtual void SetTxContinuousWave( uint32_t freq, int8_t power, uint16_t time );
  276. /*!
  277. * @brief Reads the current RSSI value
  278. *
  279. * @retval rssiValue Current RSSI value in [dBm]
  280. */
  281. virtual int16_t GetRssi ( RadioModems_t modem );
  282. /*!
  283. * @brief Writes the radio register at the specified address
  284. *
  285. * @param [IN]: addr Register address
  286. * @param [IN]: data New register value
  287. */
  288. virtual void Write ( uint8_t addr, uint8_t data ) = 0;
  289. /*!
  290. * @brief Reads the radio register at the specified address
  291. *
  292. * @param [IN]: addr Register address
  293. * @retval data Register value
  294. */
  295. virtual uint8_t Read ( uint8_t addr ) = 0;
  296. /*!
  297. * @brief Writes multiple radio registers starting at address
  298. *
  299. * @param [IN] addr First Radio register address
  300. * @param [IN] buffer Buffer containing the new register's values
  301. * @param [IN] size Number of registers to be written
  302. */
  303. virtual void Write( uint8_t addr, uint8_t *buffer, uint8_t size ) = 0;
  304. /*!
  305. * @brief Reads multiple radio registers starting at address
  306. *
  307. * @param [IN] addr First Radio register address
  308. * @param [OUT] buffer Buffer where to copy the registers data
  309. * @param [IN] size Number of registers to be read
  310. */
  311. virtual void Read ( uint8_t addr, uint8_t *buffer, uint8_t size ) = 0;
  312. /*!
  313. * @brief Writes the buffer contents to the SX1276 FIFO
  314. *
  315. * @param [IN] buffer Buffer containing data to be put on the FIFO.
  316. * @param [IN] size Number of bytes to be written to the FIFO
  317. */
  318. virtual void WriteFifo( uint8_t *buffer, uint8_t size ) = 0;
  319. /*!
  320. * @brief Reads the contents of the SX1276 FIFO
  321. *
  322. * @param [OUT] buffer Buffer where to copy the FIFO read data.
  323. * @param [IN] size Number of bytes to be read from the FIFO
  324. */
  325. virtual void ReadFifo( uint8_t *buffer, uint8_t size ) = 0;
  326. /*!
  327. * @brief Resets the SX1276
  328. */
  329. virtual void Reset( void ) = 0;
  330. /*!
  331. * @brief Sets the maximum payload length.
  332. *
  333. * @param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  334. * @param [IN] max Maximum payload length in bytes
  335. */
  336. virtual void SetMaxPayloadLength( RadioModems_t modem, uint8_t max );
  337. /*!
  338. * \brief Sets the network to public or private. Updates the sync byte.
  339. *
  340. * \remark Applies to LoRa modem only
  341. *
  342. * \param [IN] enable if true, it enables a public network
  343. */
  344. virtual void SetPublicNetwork( bool enable );
  345. //-------------------------------------------------------------------------
  346. // Board relative functions
  347. //-------------------------------------------------------------------------
  348. protected:
  349. /*!
  350. * @brief Initializes the radio I/Os pins interface
  351. */
  352. virtual void IoInit( void ) = 0;
  353. /*!
  354. * @brief Initializes the radio registers
  355. */
  356. virtual void RadioRegistersInit( ) = 0;
  357. /*!
  358. * @brief Initializes the radio SPI
  359. */
  360. virtual void SpiInit( void ) = 0;
  361. /*!
  362. * @brief Initializes DIO IRQ handlers
  363. *
  364. * @param [IN] irqHandlers Array containing the IRQ callback functions
  365. */
  366. virtual void IoIrqInit( DioIrqHandler *irqHandlers ) = 0;
  367. /*!
  368. * @brief De-initializes the radio I/Os pins interface.
  369. *
  370. * \remark Useful when going in MCU lowpower modes
  371. */
  372. virtual void IoDeInit( void ) = 0;
  373. /*!
  374. * @brief Sets the radio output power.
  375. *
  376. * @param [IN] power Sets the RF output power
  377. */
  378. virtual void SetRfTxPower( int8_t power ) = 0;
  379. /*!
  380. * @brief Gets the board PA selection configuration
  381. *
  382. * @param [IN] channel Channel frequency in Hz
  383. * @retval PaSelect RegPaConfig PaSelect value
  384. */
  385. virtual uint8_t GetPaSelect( uint32_t channel ) = 0;
  386. /*!
  387. * @brief Set the RF Switch I/Os pins in Low Power mode
  388. *
  389. * @param [IN] status enable or disable
  390. */
  391. virtual void SetAntSwLowPower( bool status ) = 0;
  392. /*!
  393. * @brief Initializes the RF Switch I/Os pins interface
  394. */
  395. virtual void AntSwInit( void ) = 0;
  396. /*!
  397. * @brief De-initializes the RF Switch I/Os pins interface
  398. *
  399. * \remark Needed to decrease the power consumption in MCU lowpower modes
  400. */
  401. virtual void AntSwDeInit( void ) = 0;
  402. /*!
  403. * @brief Controls the antenna switch if necessary.
  404. *
  405. * \remark see errata note
  406. *
  407. * @param [IN] opMode Current radio operating mode
  408. */
  409. virtual void SetAntSw( uint8_t opMode ) = 0;
  410. protected:
  411. /*!
  412. * @brief Sets the SX1276 operating mode
  413. *
  414. * @param [IN] opMode New operating mode
  415. */
  416. virtual void SetOpMode( uint8_t opMode );
  417. /*
  418. * SX1276 DIO IRQ callback functions prototype
  419. */
  420. /*!
  421. * @brief DIO 0 IRQ callback
  422. */
  423. virtual void OnDio0Irq( void );
  424. /*!
  425. * @brief DIO 1 IRQ callback
  426. */
  427. virtual void OnDio1Irq( void );
  428. /*!
  429. * @brief DIO 2 IRQ callback
  430. */
  431. virtual void OnDio2Irq( void );
  432. /*!
  433. * @brief DIO 3 IRQ callback
  434. */
  435. virtual void OnDio3Irq( void );
  436. /*!
  437. * @brief DIO 4 IRQ callback
  438. */
  439. virtual void OnDio4Irq( void );
  440. /*!
  441. * @brief DIO 5 IRQ callback
  442. */
  443. virtual void OnDio5Irq( void );
  444. /*!
  445. * @brief Tx & Rx timeout timer callback
  446. */
  447. virtual void OnTimeoutIrq( void );
  448. /*!
  449. * Returns the known FSK bandwidth registers value
  450. *
  451. * \param [IN] bandwidth Bandwidth value in Hz
  452. * \retval regValue Bandwidth register value.
  453. */
  454. static uint8_t GetFskBandwidthRegValue( uint32_t bandwidth );
  455. };
  456. #endif // __SX1276_H__