loragw_sx1302.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. / _____) _ | |
  3. ( (____ _____ ____ _| |_ _____ ____| |__
  4. \____ \| ___ | (_ _) ___ |/ ___) _ \
  5. _____) ) ____| | | || |_| ____( (___| | | |
  6. (______/|_____)_|_|_| \__)_____)\____)_| |_|
  7. (C)2019 Semtech
  8. Description:
  9. SX1302 Hardware Abstraction Layer entry functions.
  10. License: Revised BSD License, see LICENSE.TXT file include in the project
  11. */
  12. #ifndef _LORAGW_SX1302_H
  13. #define _LORAGW_SX1302_H
  14. /* -------------------------------------------------------------------------- */
  15. /* --- DEPENDANCIES --------------------------------------------------------- */
  16. #include <stdint.h> /* C99 types*/
  17. #include "config.h" /* library configuration options (dynamically generated) */
  18. /* -------------------------------------------------------------------------- */
  19. /* --- PUBLIC CONSTANTS ----------------------------------------------------- */
  20. /* Default values */
  21. #define SX1302_AGC_RADIO_GAIN_AUTO 0xFF
  22. #define TX_START_DELAY_DEFAULT 1500 /* Calibrated value for 500KHz BW */
  23. /* type of if_chain + modem */
  24. #define IF_UNDEFINED 0
  25. #define IF_LORA_STD 0x10 /* if + standard single-SF LoRa modem */
  26. #define IF_LORA_MULTI 0x11 /* if + LoRa receiver with multi-SF capability */
  27. #define IF_FSK_STD 0x20 /* if + standard FSK modem */
  28. /* -------------------------------------------------------------------------- */
  29. /* --- PUBLIC MACROS -------------------------------------------------------- */
  30. #define REG_SELECT(rf_chain, a, b) ((rf_chain == 0) ? a : b)
  31. #define SET_PPM_ON(bw,dr) (((bw == BW_125KHZ) && ((dr == DR_LORA_SF11) || (dr == DR_LORA_SF12))) || ((bw == BW_250KHZ) && (dr == DR_LORA_SF12)))
  32. /* -------------------------------------------------------------------------- */
  33. /* --- PUBLIC TYPES --------------------------------------------------------- */
  34. /**
  35. @enum sx1302_model_id_t
  36. @brief
  37. */
  38. typedef enum {
  39. CHIP_MODEL_ID_SX1302 = 0x02, /* SX1302 can be 0x00 or 0x02 */
  40. CHIP_MODEL_ID_SX1303 = 0x03,
  41. CHIP_MODEL_ID_UNKNOWN
  42. } sx1302_model_id_t;
  43. /**
  44. @enum sx1302_rx_frequency_tracking_t
  45. @brief Frequency Tracking mode
  46. */
  47. typedef enum {
  48. RX_FREQ_TRACK_OFF = 0x00,
  49. RX_FREQ_TRACK_ON = 0x01,
  50. RX_FREQ_TRACK_AUTO = 0x03
  51. } sx1302_rx_frequency_tracking_t;
  52. /**
  53. @enum sx1302_rx_fine_timing_mode_t
  54. @brief Fine Timing mode
  55. */
  56. typedef enum {
  57. RX_FINE_TIMING_MODE_ABS = 0x01,
  58. RX_FINE_TIMING_MODE_LINEAR = 0x02,
  59. RX_FINE_TIMING_MODE_AUTO = 0x03
  60. } sx1302_rx_fine_timing_mode_t;
  61. /**
  62. @enum sx1302_rx_dft_peak_mode_t
  63. @brief DFT peak mode
  64. */
  65. typedef enum {
  66. RX_DFT_PEAK_MODE_DISABLED = 0x00,
  67. RX_DFT_PEAK_MODE_FULL = 0x01,
  68. RX_DFT_PEAK_MODE_TRACK = 0x02,
  69. RX_DFT_PEAK_MODE_AUTO = 0x03
  70. } sx1302_rx_dft_peak_mode_t;
  71. /* -------------------------------------------------------------------------- */
  72. /* --- PUBLIC FUNCTIONS PROTOTYPES ------------------------------------------ */
  73. /**
  74. @brief Initialize sx1302 for operating, and needed internal structures (rx_buffer,....)
  75. @param conf a pointer to the fine timestamp configuration context
  76. @return LGW_REG_SUCCESS if no error, LGW_REG_ERROR otherwise
  77. */
  78. int sx1302_init(const struct lgw_conf_ftime_s *conf);
  79. /**
  80. @brief Get the SX1302 unique identifier
  81. @param eui pointerto the memory holding the concentrator EUI
  82. @return LGW_REG_SUCCESS if no error, LGW_REG_ERROR otherwise
  83. */
  84. int sx1302_get_eui(uint64_t * eui);
  85. /**
  86. @brief Get the SX1302/SX1303 Chip Model ID
  87. @param model_id pointer to the memory holding the Chip Model ID
  88. @return LGW_REG_SUCCESS if no error, LGW_REG_ERROR otherwise
  89. */
  90. int sx1302_get_model_id(sx1302_model_id_t * model_id);
  91. /**
  92. @brief Check AGC & ARB MCUs parity error, and update timestamp counter wraping status
  93. @brief This function needs to be called regularly (every few seconds) by the upper layer
  94. @param N/A
  95. @return LGW_REG_SUCCESS if no error, LGW_REG_ERROR otherwise
  96. */
  97. int sx1302_update(void);
  98. /**
  99. @brief Select the clock source radio
  100. @param rf_chain The RF chain index from which to get the clock source
  101. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  102. */
  103. int sx1302_radio_clock_select(uint8_t rf_chain);
  104. /**
  105. @brief Apply the radio reset sequence to the required RF chain index
  106. @param rf_chain The RF chain index of the radio to be reset
  107. @param type The type of radio to be reset
  108. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  109. */
  110. int sx1302_radio_reset(uint8_t rf_chain, lgw_radio_type_t type);
  111. /**
  112. @brief Configure the radio type for the given RF chain
  113. @param rf_chain The RF chain index to be configured
  114. @param type The type of radio to be set for the given RF chain
  115. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  116. */
  117. int sx1302_radio_set_mode(uint8_t rf_chain, lgw_radio_type_t type);
  118. /**
  119. @brief Give/Release control over the radios to/from the Host
  120. @param host_ctrl Set to true to give control to the host, false otherwise
  121. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  122. */
  123. int sx1302_radio_host_ctrl(bool host_ctrl);
  124. /**
  125. @brief Perform the radio calibration sequence and fill the TX gain LUT with calibration offsets
  126. @param context_rf_chain The RF chains array from which to get RF chains current configuration
  127. @param clksrc The RF chain index which provides the clock source
  128. @param txgain_lut A pointer to the TX gain LUT to be filled
  129. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  130. */
  131. int sx1302_radio_calibrate(struct lgw_conf_rxrf_s * context_rf_chain, uint8_t clksrc, struct lgw_tx_gain_lut_s * txgain_lut);
  132. /**
  133. @brief Configure the PA and LNA LUTs
  134. @param context_board A pointer to the current board configuration context
  135. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  136. */
  137. int sx1302_pa_lna_lut_configure(struct lgw_conf_board_s * context_board);
  138. /**
  139. @brief Configure the Radio Front-End stage of the SX1302
  140. @param N/A
  141. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  142. */
  143. int sx1302_radio_fe_configure(void);
  144. /**
  145. @brief Returns the type of the given modem index (LoRa MultiSF, LoRa SingleSF, FSK)
  146. @param if_chain the index if the IF chain
  147. @return The IF chain type
  148. */
  149. uint8_t sx1302_get_ifmod_config(uint8_t if_chain);
  150. /**
  151. @brief Configure the channelizer stage of the SX1302
  152. @param if_cfg A pointer to the channels configuration
  153. @param fix_gain Set to true to force the channelizer to a fixed gain, false to let the AGC controlling it
  154. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  155. */
  156. int sx1302_channelizer_configure(struct lgw_conf_rxif_s * if_cfg, bool fix_gain);
  157. /**
  158. @brief Configure the correlator stage of the SX1302 LoRa multi-SF modems
  159. @param if_cfg A pointer to the channels configuration
  160. @param demod_cfg A pointer to the demodulators configuration
  161. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  162. */
  163. int sx1302_lora_correlator_configure(struct lgw_conf_rxif_s * if_cfg, struct lgw_conf_demod_s * demod_cfg);
  164. /**
  165. @brief Configure the correlator stage of the SX1302 LoRa single-SF modem
  166. @param cfg A pointer to the channel configuration
  167. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  168. */
  169. int sx1302_lora_service_correlator_configure(struct lgw_conf_rxif_s * cfg);
  170. /**
  171. @brief Configure the syncword to be used by LoRa modems (public:0x34, private:0x12)
  172. @param public Set to true to use the "public" syncword, false to use the private one
  173. @param lora_service_sf The spreading factor configured for the single-SF LoRa modem
  174. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  175. */
  176. int sx1302_lora_syncword(bool public, uint8_t lora_service_sf);
  177. /**
  178. @brief Configure the LoRa multi-SF modems
  179. @param radio_freq_hz The center frequency of the RF chain (0 or 1)
  180. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  181. */
  182. int sx1302_lora_modem_configure(uint32_t radio_freq_hz);
  183. /**
  184. @brief Configure the LoRa single-SF modem
  185. @param cfg A pointer to the channel configuration
  186. @param radio_freq_hz The center frequency of the RF chain (0 or 1)
  187. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  188. */
  189. int sx1302_lora_service_modem_configure(struct lgw_conf_rxif_s * cfg, uint32_t radio_freq_hz);
  190. /**
  191. @brief Configure the FSK modem
  192. @param cfg A pointer to the channel configuration
  193. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  194. */
  195. int sx1302_fsk_configure(struct lgw_conf_rxif_s * cfg);
  196. /**
  197. @brief Enable the modems
  198. @param N/A
  199. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  200. */
  201. int sx1302_modem_enable(void);
  202. /**
  203. @brief Enable/Disable the GPS to allow PPS trigger and counter sampling
  204. @param enbale Set to true to enable, false otherwise
  205. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  206. */
  207. int sx1302_gps_enable(bool enable);
  208. /**
  209. @brief Get the current SX1302 internal counter value
  210. @param pps True for getting the counter value at last PPS
  211. @return the counter value in mciroseconds (32-bits)
  212. */
  213. uint32_t sx1302_timestamp_counter(bool pps);
  214. /**
  215. @brief Load firmware to AGC MCU memory
  216. @param firmware A pointer to the fw binary to be loaded
  217. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  218. */
  219. int sx1302_agc_load_firmware(const uint8_t *firmware);
  220. /**
  221. @brief Read the AGC status register for current status
  222. @param status A pointer to store the current status returned
  223. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  224. */
  225. int sx1302_agc_status(uint8_t* status);
  226. /**
  227. @brief TODO
  228. @param TODO
  229. @return TODO
  230. */
  231. int sx1302_agc_wait_status(uint8_t status);
  232. /**
  233. @brief TODO
  234. @param TODO
  235. @return TODO
  236. */
  237. int sx1302_agc_mailbox_read(uint8_t mailbox, uint8_t* value);
  238. /**
  239. @brief TODO
  240. @param TODO
  241. @return TODO
  242. */
  243. int sx1302_agc_mailbox_write(uint8_t mailbox, uint8_t value);
  244. /**
  245. @brief TODO
  246. @param TODO
  247. @return TODO
  248. */
  249. int sx1302_agc_start(uint8_t version, lgw_radio_type_t radio_type, uint8_t ana_gain, uint8_t dec_gain, bool full_duplex, bool lbt_enable);
  250. /**
  251. @brief TODO
  252. @param TODO
  253. @return TODO
  254. */
  255. int sx1302_arb_load_firmware(const uint8_t *firmware);
  256. /**
  257. @brief TODO
  258. @param TODO
  259. @return TODO
  260. */
  261. int sx1302_arb_status(uint8_t* status);
  262. /**
  263. @brief TODO
  264. @param TODO
  265. @return TODO
  266. */
  267. int sx1302_arb_wait_status(uint8_t status);
  268. /**
  269. @brief TODO
  270. @param TODO
  271. @return TODO
  272. */
  273. int sx1302_arb_debug_read(uint8_t reg_id, uint8_t* value);
  274. /**
  275. @brief TODO
  276. @param TODO
  277. @return TODO
  278. */
  279. int sx1302_arb_debug_write(uint8_t reg_id, uint8_t value);
  280. /**
  281. @brief TODO
  282. @param TODO
  283. @return TODO
  284. */
  285. int sx1302_arb_start(uint8_t version, const struct lgw_conf_ftime_s * ftime_context);
  286. /**
  287. @brief TODO
  288. @param TODO
  289. @return TODO
  290. */
  291. uint8_t sx1302_arb_get_debug_stats_detect(uint8_t channel);
  292. /**
  293. @brief TODO
  294. @param TODO
  295. @return TODO
  296. */
  297. uint8_t sx1302_arb_get_debug_stats_alloc(uint8_t channel);
  298. /**
  299. @brief TODO
  300. @param TODO
  301. @return TODO
  302. */
  303. void sx1302_arb_print_debug_stats(void);
  304. /**
  305. @brief TODO
  306. @param TODO
  307. @return TODO
  308. */
  309. uint16_t sx1302_lora_payload_crc(const uint8_t * data, uint8_t size);
  310. /**
  311. @brief Get the number of packets available in rx_buffer and fetch data from ...
  312. @brief ... the SX1302 if rx_buffer is empty.
  313. @param nb_pkt A pointer to allocated memory to hold the number of packet fetched
  314. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  315. */
  316. int sx1302_fetch(uint8_t * nb_pkt);
  317. /**
  318. @brief Parse and return the next packet available in rx_buffer.
  319. @param context Gateway configuration context
  320. @param p The structure to get the packet parsed
  321. @return LGW_REG_SUCCESS if a packet could be parsed, LGW_REG_ERROR otherwise
  322. */
  323. int sx1302_parse(lgw_context_t * context, struct lgw_pkt_rx_s * p);
  324. /**
  325. @brief Configure the delay to be applied by the SX1302 for TX to start
  326. @param rf_chain RF chain index to be configured
  327. @param radio_type Type of radio for this RF chain
  328. @param modulation Modulation used for the TX
  329. @param bandwidth Bandwidth used for the TX
  330. @param chirp_lowpass Chirp Low Pass filtering configuration
  331. @param delay TX start delay calculated and applied
  332. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  333. */
  334. int sx1302_tx_set_start_delay(uint8_t rf_chain, lgw_radio_type_t radio_type, uint8_t modulation, uint8_t bandwidth, uint8_t chirp_lowpass, uint16_t * delay);
  335. /**
  336. @brief Compute the offset to be applied on RSSI for temperature compensation
  337. @param context a pointer to the memory that holds the current temp comp context
  338. @param temperature the temperature for which to compute the offset to be applied
  339. @return the offset to be applied to RSSI
  340. */
  341. float sx1302_rssi_get_temperature_offset(struct lgw_rssi_tcomp_s * context, float temperature);
  342. /**
  343. @brief Get current TX status of the SX1302
  344. @param rf_chain the TX chain we want to get the status from
  345. @return current status
  346. */
  347. uint8_t sx1302_tx_status(uint8_t rf_chain);
  348. /**
  349. @brief Get current RX status of the SX1302
  350. @param rf_chain the RX chain we want to get the status from
  351. @return current status
  352. @note NOT IMPLEMENTED
  353. */
  354. uint8_t sx1302_rx_status(uint8_t rf_chain);
  355. /**
  356. @brief Abort current transmit
  357. @param rf_chain the TX chain on which we want to abort transmit
  358. @return LGW_REG_SUCCESS if success, LGW_REG_ERROR otherwise
  359. */
  360. int sx1302_tx_abort(uint8_t rf_chain);
  361. /**
  362. @brief TODO
  363. @param TODO
  364. @return TODO
  365. */
  366. int sx1302_tx_configure(lgw_radio_type_t radio_type);
  367. /**
  368. @brief TODO
  369. @param TODO
  370. @return TODO
  371. */
  372. int sx1302_send(lgw_radio_type_t radio_type, struct lgw_tx_gain_lut_s * tx_lut, bool lwan_public, struct lgw_conf_rxif_s * context_fsk, struct lgw_pkt_tx_s * pkt_data);
  373. /**
  374. @brief TODO
  375. @param TODO
  376. @return TODO
  377. */
  378. int sx1302_set_gpio(uint8_t gpio_reg_val);
  379. /**
  380. @brief TODO
  381. @param TODO
  382. @return TODO
  383. */
  384. double sx1302_dc_notch_delay(double if_freq_hz);
  385. #endif
  386. /* --- EOF ------------------------------------------------------------------ */