sx1276.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. / _____) _ | |
  3. ( (____ _____ ____ _| |_ _____ ____| |__
  4. \____ \| ___ | (_ _) ___ |/ ___) _ \
  5. _____) ) ____| | | || |_| ____( (___| | | |
  6. (______/|_____)_|_|_| \__)_____)\____)_| |_|
  7. (C)2013 Semtech
  8. Description: Generic SX1276 driver implementation
  9. License: Revised BSD License, see LICENSE.TXT file include in the project
  10. Maintainer: Miguel Luis and Gregory Cristian
  11. */
  12. #ifndef __SX1276_H__
  13. #define __SX1276_H__
  14. #include "sx1276Regs-Fsk.h"
  15. #include "sx1276Regs-LoRa.h"
  16. #include "radio.h"
  17. /*!
  18. * Radio wakeup time from SLEEP mode
  19. */
  20. #define RADIO_OSC_STARTUP 1 // [ms]
  21. /*!
  22. * Radio PLL lock and Mode Ready delay which can vary with the temperature
  23. */
  24. #define RADIO_SLEEP_TO_RX 2 // [ms]
  25. /*!
  26. * Radio complete Wake-up Time with margin for temperature compensation
  27. */
  28. #define RADIO_WAKEUP_TIME ( RADIO_OSC_STARTUP + RADIO_SLEEP_TO_RX )
  29. /*!
  30. * Radio FSK modem parameters
  31. */
  32. typedef struct
  33. {
  34. int8_t Power;
  35. uint32_t Fdev;
  36. uint32_t Bandwidth;
  37. uint32_t BandwidthAfc;
  38. uint32_t Datarate;
  39. uint16_t PreambleLen;
  40. bool FixLen;
  41. uint8_t PayloadLen;
  42. bool CrcOn;
  43. bool IqInverted;
  44. bool RxContinuous;
  45. uint32_t TxTimeout;
  46. }RadioFskSettings_t;
  47. /*!
  48. * Radio FSK packet handler state
  49. */
  50. typedef struct
  51. {
  52. uint8_t PreambleDetected;
  53. uint8_t SyncWordDetected;
  54. int8_t RssiValue;
  55. int32_t AfcValue;
  56. uint8_t RxGain;
  57. uint16_t Size;
  58. uint16_t NbBytes;
  59. uint8_t FifoThresh;
  60. uint8_t ChunkSize;
  61. }RadioFskPacketHandler_t;
  62. /*!
  63. * Radio LoRa modem parameters
  64. */
  65. typedef struct
  66. {
  67. int8_t Power;
  68. uint32_t Bandwidth;
  69. uint32_t Datarate;
  70. bool LowDatarateOptimize;
  71. uint8_t Coderate;
  72. uint16_t PreambleLen;
  73. bool FixLen;
  74. uint8_t PayloadLen;
  75. bool CrcOn;
  76. bool FreqHopOn;
  77. uint8_t HopPeriod;
  78. bool IqInverted;
  79. bool RxContinuous;
  80. uint32_t TxTimeout;
  81. }RadioLoRaSettings_t;
  82. /*!
  83. * Radio LoRa packet handler state
  84. */
  85. typedef struct
  86. {
  87. int8_t SnrValue;
  88. int16_t RssiValue;
  89. uint8_t Size;
  90. }RadioLoRaPacketHandler_t;
  91. /*!
  92. * Radio Settings
  93. */
  94. typedef struct
  95. {
  96. RadioState_t State;
  97. RadioModems_t Modem;
  98. uint32_t Channel;
  99. RadioFskSettings_t Fsk;
  100. RadioFskPacketHandler_t FskPacketHandler;
  101. RadioLoRaSettings_t LoRa;
  102. RadioLoRaPacketHandler_t LoRaPacketHandler;
  103. }RadioSettings_t;
  104. /*!
  105. * Radio hardware and global parameters
  106. */
  107. typedef struct SX1276_s
  108. {
  109. /* GPIO和硬件相关,移植到 sx1276-board.c 中了
  110. Gpio_t Reset;
  111. Gpio_t DIO0;
  112. Gpio_t DIO1;
  113. Gpio_t DIO2;
  114. Gpio_t DIO3;
  115. Gpio_t DIO4;
  116. Gpio_t DIO5;
  117. Spi_t Spi;*/
  118. RadioSettings_t Settings;
  119. }SX1276_t;
  120. /*!
  121. * Hardware IO IRQ callback function definition
  122. */
  123. typedef void ( DioIrqHandler )( void );
  124. /*!
  125. * SX1276 definitions
  126. */
  127. #define XTAL_FREQ 32000000
  128. #define FREQ_STEP 61.03515625
  129. #define RX_BUFFER_SIZE 256
  130. /*!
  131. * ============================================================================
  132. * Public functions prototypes
  133. * ============================================================================
  134. */
  135. /*!
  136. * \brief Initializes the radio
  137. *
  138. * \param [IN] events Structure containing the driver callback functions
  139. */
  140. void SX1276Init( RadioEvents_t *events );
  141. /*!
  142. * Return current radio status
  143. *
  144. * \param status Radio status.[RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
  145. */
  146. RadioState_t SX1276GetStatus( void );
  147. /*!
  148. * \brief Configures the radio with the given modem
  149. *
  150. * \param [IN] modem Modem to be used [0: FSK, 1: LoRa]
  151. */
  152. void SX1276SetModem( RadioModems_t modem );
  153. /*!
  154. * \brief Sets the channels configuration
  155. *
  156. * \param [IN] freq Channel RF frequency
  157. */
  158. void SX1276SetChannel( uint32_t freq );
  159. /*!
  160. * \brief Sets the channels configuration
  161. *
  162. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  163. * \param [IN] freq Channel RF frequency
  164. * \param [IN] rssiThresh RSSI threshold
  165. *
  166. * \retval isFree [true: Channel is free, false: Channel is not free]
  167. */
  168. bool SX1276IsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh );
  169. /*!
  170. * \brief Generates a 32 bits random value based on the RSSI readings
  171. *
  172. * \remark This function sets the radio in LoRa modem mode and disables
  173. * all interrupts.
  174. * After calling this function either SX1276SetRxConfig or
  175. * SX1276SetTxConfig functions must be called.
  176. *
  177. * \retval randomValue 32 bits random value
  178. */
  179. uint32_t SX1276Random( void );
  180. /*!
  181. * \brief Sets the reception parameters
  182. *
  183. * \remark When using LoRa modem only bandwidths 125, 250 and 500 kHz are supported
  184. *
  185. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  186. * \param [IN] bandwidth Sets the bandwidth
  187. * FSK : >= 2600 and <= 250000 Hz
  188. * LoRa: [0: 125 kHz, 1: 250 kHz,
  189. * 2: 500 kHz, 3: Reserved]
  190. * \param [IN] datarate Sets the Datarate
  191. * FSK : 600..300000 bits/s
  192. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  193. * 10: 1024, 11: 2048, 12: 4096 chips]
  194. * \param [IN] coderate Sets the coding rate (LoRa only)
  195. * FSK : N/A ( set to 0 )
  196. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  197. * \param [IN] bandwidthAfc Sets the AFC Bandwidth (FSK only)
  198. * FSK : >= 2600 and <= 250000 Hz
  199. * LoRa: N/A ( set to 0 )
  200. * \param [IN] preambleLen Sets the Preamble length
  201. * FSK : Number of bytes
  202. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  203. * \param [IN] symbTimeout Sets the RxSingle timeout value (LoRa only)
  204. * FSK : N/A ( set to 0 )
  205. * LoRa: timeout in symbols
  206. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  207. * \param [IN] payloadLen Sets payload length when fixed lenght is used
  208. * \param [IN] crcOn Enables/Disables the CRC [0: OFF, 1: ON]
  209. * \param [IN] FreqHopOn Enables disables the intra-packet frequency hopping
  210. * FSK : N/A ( set to 0 )
  211. * LoRa: [0: OFF, 1: ON]
  212. * \param [IN] HopPeriod Number of symbols bewteen each hop
  213. * FSK : N/A ( set to 0 )
  214. * LoRa: Number of symbols
  215. * \param [IN] iqInverted Inverts IQ signals (LoRa only)
  216. * FSK : N/A ( set to 0 )
  217. * LoRa: [0: not inverted, 1: inverted]
  218. * \param [IN] rxContinuous Sets the reception in continuous mode
  219. * [false: single mode, true: continuous mode]
  220. */
  221. void SX1276SetRxConfig( RadioModems_t modem, uint32_t bandwidth,
  222. uint32_t datarate, uint8_t coderate,
  223. uint32_t bandwidthAfc, uint16_t preambleLen,
  224. uint16_t symbTimeout, bool fixLen,
  225. uint8_t payloadLen,
  226. bool crcOn, bool FreqHopOn, uint8_t HopPeriod,
  227. bool iqInverted, bool rxContinuous );
  228. /*!
  229. * \brief Sets the transmission parameters
  230. *
  231. * \remark When using LoRa modem only bandwidths 125, 250 and 500 kHz are supported
  232. *
  233. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  234. * \param [IN] power Sets the output power [dBm]
  235. * \param [IN] fdev Sets the frequency deviation (FSK only)
  236. * FSK : [Hz]
  237. * LoRa: 0
  238. * \param [IN] bandwidth Sets the bandwidth (LoRa only)
  239. * FSK : 0
  240. * LoRa: [0: 125 kHz, 1: 250 kHz,
  241. * 2: 500 kHz, 3: Reserved]
  242. * \param [IN] datarate Sets the Datarate
  243. * FSK : 600..300000 bits/s
  244. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  245. * 10: 1024, 11: 2048, 12: 4096 chips]
  246. * \param [IN] coderate Sets the coding rate (LoRa only)
  247. * FSK : N/A ( set to 0 )
  248. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  249. * \param [IN] preambleLen Sets the preamble length
  250. * FSK : Number of bytes
  251. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  252. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  253. * \param [IN] crcOn Enables disables the CRC [0: OFF, 1: ON]
  254. * \param [IN] FreqHopOn Enables disables the intra-packet frequency hopping
  255. * FSK : N/A ( set to 0 )
  256. * LoRa: [0: OFF, 1: ON]
  257. * \param [IN] HopPeriod Number of symbols bewteen each hop
  258. * FSK : N/A ( set to 0 )
  259. * LoRa: Number of symbols
  260. * \param [IN] iqInverted Inverts IQ signals (LoRa only)
  261. * FSK : N/A ( set to 0 )
  262. * LoRa: [0: not inverted, 1: inverted]
  263. * \param [IN] timeout Transmission timeout [ms]
  264. */
  265. void SX1276SetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev,
  266. uint32_t bandwidth, uint32_t datarate,
  267. uint8_t coderate, uint16_t preambleLen,
  268. bool fixLen, bool crcOn, bool FreqHopOn,
  269. uint8_t HopPeriod, bool iqInverted, uint32_t timeout );
  270. /*!
  271. * \brief Computes the packet time on air in us for the given payload
  272. *
  273. * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
  274. *
  275. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  276. * \param [IN] pktLen Packet payload length
  277. *
  278. * \retval airTime Computed airTime (us) for the given packet payload length
  279. */
  280. uint32_t SX1276GetTimeOnAir( RadioModems_t modem, uint8_t pktLen );
  281. /*!
  282. * \brief Sends the buffer of size. Prepares the packet to be sent and sets
  283. * the radio in transmission
  284. *
  285. * \param [IN]: buffer Buffer pointer
  286. * \param [IN]: size Buffer size
  287. */
  288. void SX1276Send( uint8_t *buffer, uint8_t size );
  289. /*!
  290. * \brief Sets the radio in sleep mode
  291. */
  292. void SX1276SetSleep( void );
  293. /*!
  294. * \brief Sets the radio in standby mode
  295. */
  296. void SX1276SetStby( void );
  297. /*!
  298. * \brief Sets the radio in reception mode for the given time
  299. * \param [IN] timeout Reception timeout [ms] [0: continuous, others timeout]
  300. */
  301. void SX1276SetRx( uint32_t timeout );
  302. /*!
  303. * \brief Start a Channel Activity Detection
  304. */
  305. void SX1276StartCad( void );
  306. /*!
  307. * \brief Reads the current RSSI value
  308. *
  309. * \retval rssiValue Current RSSI value in [dBm]
  310. */
  311. int16_t SX1276ReadRssi( RadioModems_t modem );
  312. /*!
  313. * \brief Writes the radio register at the specified address
  314. *
  315. * \param [IN]: addr Register address
  316. * \param [IN]: data New register value
  317. */
  318. void SX1276Write( uint8_t addr, uint8_t data );
  319. /*!
  320. * \brief Reads the radio register at the specified address
  321. *
  322. * \param [IN]: addr Register address
  323. * \retval data Register value
  324. */
  325. uint8_t SX1276Read( uint8_t addr );
  326. /*!
  327. * \brief Writes multiple radio registers starting at address
  328. *
  329. * \param [IN] addr First Radio register address
  330. * \param [IN] buffer Buffer containing the new register's values
  331. * \param [IN] size Number of registers to be written
  332. */
  333. void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size );
  334. /*!
  335. * \brief Reads multiple radio registers starting at address
  336. *
  337. * \param [IN] addr First Radio register address
  338. * \param [OUT] buffer Buffer where to copy the registers data
  339. * \param [IN] size Number of registers to be read
  340. */
  341. void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size );
  342. /*!
  343. * \brief Sets the maximum payload length.
  344. *
  345. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  346. * \param [IN] max Maximum payload length in bytes
  347. */
  348. void SX1276SetMaxPayloadLength( RadioModems_t modem, uint8_t max );
  349. /*!
  350. * \brief Set the RF Switch I/Os pins in Low Power mode
  351. *
  352. * \param [IN] status enable or disable
  353. */
  354. void SX1276SetAntSwLowPower( bool status );
  355. void SX1276OnTimeoutIrq( void );
  356. #endif // __SX1276_H__