uart_hal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*******************************************************************************
  7. * NOTICE
  8. * The hal is not public api, don't use in application code.
  9. * See readme.md in hal/include/hal/readme.md
  10. ******************************************************************************/
  11. // The HAL layer for UART.
  12. // There is no parameter check in the hal layer, so the caller must ensure the correctness of the parameters.
  13. #pragma once
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #include "hal/uart_ll.h"
  18. #include "hal/uart_types.h"
  19. /**
  20. * Context that should be maintained by both the driver and the HAL
  21. */
  22. typedef struct {
  23. uart_dev_t *dev;
  24. } uart_hal_context_t;
  25. /**
  26. * @brief Clear the UART interrupt status
  27. *
  28. * @param hal Context of the HAL layer
  29. * @param mask The interrupt status mask to be cleared. Using the ORred mask of `UART_INTR_RXFIFO_FULL ... UART_INTR_CMD_CHAR_DET`
  30. *
  31. * @return None
  32. */
  33. #define uart_hal_clr_intsts_mask(hal, mask) uart_ll_clr_intsts_mask((hal)->dev, mask)
  34. /**
  35. * @brief Disable the UART interrupt
  36. *
  37. * @param hal Context of the HAL layer
  38. * @param mask The interrupt mask to be disabled. Using the ORred mask of `UART_INTR_RXFIFO_FULL ... UART_INTR_CMD_CHAR_DET`
  39. *
  40. * @return None
  41. */
  42. #define uart_hal_disable_intr_mask(hal, mask) uart_ll_disable_intr_mask((hal)->dev, mask)
  43. /**
  44. * @brief Enable the UART interrupt
  45. *
  46. * @param hal Context of the HAL layer
  47. * @param mask The UART interrupt mask to be enabled. Using the ORred mask of `UART_INTR_RXFIFO_FULL ... UART_INTR_CMD_CHAR_DET`
  48. *
  49. * @return None
  50. */
  51. #define uart_hal_ena_intr_mask(hal, mask) uart_ll_ena_intr_mask((hal)->dev, mask)
  52. /**
  53. * @brief Get the UART raw interrupt status
  54. *
  55. * @param hal Context of the HAL layer
  56. *
  57. * @return UART raw interrupt status
  58. */
  59. #define uart_hal_get_intraw_mask(hal) uart_ll_get_intraw_mask((hal)->dev)
  60. /**
  61. * @brief Get the UART interrupt status
  62. *
  63. * @param hal Context of the HAL layer
  64. *
  65. * @return UART interrupt status
  66. */
  67. #define uart_hal_get_intsts_mask(hal) uart_ll_get_intsts_mask((hal)->dev)
  68. /**
  69. * @brief Get status of enabled interrupt
  70. *
  71. * @param hal Context of the HAL layer
  72. *
  73. * @return UART Interrupt enabled value
  74. */
  75. #define uart_hal_get_intr_ena_status(hal) uart_ll_get_intr_ena_status((hal)->dev)
  76. /**
  77. * @brief Get the UART pattern char configuration
  78. *
  79. * @param hal Context of the HAL layer
  80. * @param cmd_char Pointer to accept UART AT cmd char
  81. * @param char_num Pointer to accept the `UART_CHAR_NUM` configuration
  82. *
  83. * @return None
  84. */
  85. #define uart_hal_get_at_cmd_char(hal, cmd_char, char_num) uart_ll_get_at_cmd_char((hal)->dev, cmd_char, char_num)
  86. /**
  87. * @brief Set the UART rst signal active level
  88. *
  89. * @param hal Context of the HAL layer
  90. * @param active_level The rts active level. The active level is low if set to 0. The active level is high if set to 1
  91. *
  92. * @return None
  93. */
  94. #define uart_hal_set_rts(hal, active_level) uart_ll_set_rts_active_level((hal)->dev, active_level)
  95. /**
  96. * @brief Get the txfifo writeable length(in byte)
  97. *
  98. * @param hal Context of the HAL layer
  99. *
  100. * @return UART txfifo writeable length
  101. */
  102. #define uart_hal_get_txfifo_len(hal) uart_ll_get_txfifo_len((hal)->dev)
  103. /**
  104. * @brief Check if the UART sending state machine is in the IDLE state.
  105. *
  106. * @param hal Context of the HAL layer
  107. *
  108. * @return True if the state machine is in the IDLE state, otherwise false will be returned.
  109. */
  110. #define uart_hal_is_tx_idle(hal) uart_ll_is_tx_idle((hal)->dev)
  111. /**
  112. * @brief Configure the UART core reset
  113. *
  114. * @param hal Context of the HAL layer
  115. * @param core_rst_en true to enable the core reset, otherwise set it false
  116. *
  117. * @return None
  118. */
  119. #define uart_hal_set_reset_core(hal, core_rst_en) uart_ll_set_reset_core((hal)->dev, core_rst_en)
  120. /**
  121. * @brief Read data from the UART rxfifo
  122. *
  123. * @param[in] hal Context of the HAL layer
  124. * @param[in] buf Pointer to the buffer used to store the read data. The buffer size should be large than 128 byte
  125. * @param[inout] inout_rd_len As input, the size of output buffer to read (set to 0 to read all available data).
  126. * As output, returns the actual size written into the output buffer.
  127. *
  128. * @return None
  129. */
  130. void uart_hal_read_rxfifo(uart_hal_context_t *hal, uint8_t *buf, int *inout_rd_len);
  131. /**
  132. * @brief Write data into the UART txfifo
  133. *
  134. * @param hal Context of the HAL layer
  135. * @param buf Pointer of the data buffer need to be written to txfifo
  136. * @param data_size The data size(in byte) need to be written
  137. * @param write_size The size has been written
  138. *
  139. * @return None
  140. */
  141. void uart_hal_write_txfifo(uart_hal_context_t *hal, const uint8_t *buf, uint32_t data_size, uint32_t *write_size);
  142. /**
  143. * @brief Reset the UART txfifo
  144. * @note On ESP32, this function is reserved for UART1 and UART2.
  145. *
  146. * @param hal Context of the HAL layer
  147. *
  148. * @return None
  149. */
  150. void uart_hal_txfifo_rst(uart_hal_context_t *hal);
  151. /**
  152. * @brief Reset the UART rxfifo
  153. *
  154. * @param hal Context of the HAL layer
  155. *
  156. * @return None
  157. */
  158. void uart_hal_rxfifo_rst(uart_hal_context_t *hal);
  159. /**
  160. * @brief Init the UART hal and set the UART to the default configuration.
  161. *
  162. * @param hal Context of the HAL layer
  163. * @param uart_num The uart port number, the max port number is (UART_NUM_MAX -1)
  164. *
  165. * @return None
  166. */
  167. void uart_hal_init(uart_hal_context_t *hal, uart_port_t uart_num);
  168. /**
  169. * @brief Set the UART source clock type
  170. * @param hal Context of the HAL layer
  171. * @param sclk The UART source clock type.
  172. *
  173. * @return None
  174. */
  175. void uart_hal_set_sclk(uart_hal_context_t *hal, uart_sclk_t sclk);
  176. /**
  177. * @brief Get the UART source clock type
  178. *
  179. * @param hal Context of the HAL layer
  180. * @param sclk The poiter to accept the UART source clock type
  181. *
  182. * @return None
  183. */
  184. void uart_hal_get_sclk(uart_hal_context_t *hal, uart_sclk_t *sclk);
  185. /**
  186. * @brief Configure the UART baud-rate and select the source clock
  187. *
  188. * @param hal Context of the HAL layer
  189. * @param baud_rate The baud-rate to be set
  190. * @param sclk_freq Frequency of the clock source of UART, in Hz.
  191. *
  192. * @return None
  193. */
  194. void uart_hal_set_baudrate(uart_hal_context_t *hal, uint32_t baud_rate, uint32_t sclk_freq);
  195. /**
  196. * @brief Configure the UART stop bit
  197. *
  198. * @param hal Context of the HAL layer
  199. * @param stop_bit The stop bit to be set
  200. *
  201. * @return None
  202. */
  203. void uart_hal_set_stop_bits(uart_hal_context_t *hal, uart_stop_bits_t stop_bit);
  204. /**
  205. * @brief Configure the UART data bit
  206. *
  207. * @param hal Context of the HAL layer
  208. * @param data_bit The data bit to be set
  209. *
  210. * @return None
  211. */
  212. void uart_hal_set_data_bit_num(uart_hal_context_t *hal, uart_word_length_t data_bit);
  213. /**
  214. * @brief Configure the UART parity mode
  215. *
  216. * @param hal Context of the HAL layer
  217. * @param parity_mode The UART parity mode to be set
  218. *
  219. * @return None
  220. */
  221. void uart_hal_set_parity(uart_hal_context_t *hal, uart_parity_t parity_mode);
  222. /**
  223. * @brief Configure the UART hardware flow control
  224. *
  225. * @param hal Context of the HAL layer
  226. * @param flow_ctrl The flow control mode to be set
  227. * @param rx_thresh The rts flow control signal will be active if the data length in rxfifo is large than this value
  228. *
  229. * @return None
  230. */
  231. void uart_hal_set_hw_flow_ctrl(uart_hal_context_t *hal, uart_hw_flowcontrol_t flow_ctrl, uint8_t rx_thresh);
  232. /**
  233. * @brief Configure the UART AT cmd char detect function. When the receiver receives a continuous AT cmd char, it will produce a interrupt
  234. *
  235. * @param hal Context of the HAL layer
  236. * @param at_cmd The AT cmd char detect configuration
  237. *
  238. * @return None.
  239. */
  240. void uart_hal_set_at_cmd_char(uart_hal_context_t *hal, uart_at_cmd_t *at_cmd);
  241. /**
  242. * @brief Set the timeout value of the UART receiver
  243. *
  244. * @param hal Context of the HAL layer
  245. * @param tout The timeout value for receiver to receive a data
  246. *
  247. * @return None
  248. */
  249. void uart_hal_set_rx_timeout(uart_hal_context_t *hal, const uint8_t tout);
  250. /**
  251. * @brief Set the UART dtr signal active level
  252. *
  253. * @param hal Context of the HAL layer
  254. * @param active_level The dtr active level. The active level is low if set to 0. The active level is high if set to 1
  255. *
  256. * @return None
  257. */
  258. void uart_hal_set_dtr(uart_hal_context_t *hal, int active_level);
  259. /**
  260. * @brief Set the UART software flow control
  261. *
  262. * @param hal Context of the HAL layer
  263. * @param flow_ctrl The software flow control configuration
  264. * @param sw_flow_ctrl_en Set true to enable the software flow control, otherwise set it false
  265. *
  266. * @return None
  267. */
  268. void uart_hal_set_sw_flow_ctrl(uart_hal_context_t *hal, uart_sw_flowctrl_t *flow_ctrl, bool sw_flow_ctrl_en);
  269. /**
  270. * @brief Set the UART tx idle number
  271. *
  272. * @param hal Context of the HAL layer
  273. * @param idle_num The cycle number betwin the two transmission
  274. *
  275. * @return None
  276. */
  277. void uart_hal_set_tx_idle_num(uart_hal_context_t *hal, uint16_t idle_num);
  278. /**
  279. * @brief Set the UART rxfifo full threshold
  280. *
  281. * @param hal Context of the HAL layer
  282. * @param full_thrhd The rxfifo full threshold. If the `UART_RXFIFO_FULL` interrupt is enabled and
  283. * the data length in rxfifo is more than this value, it will generate `UART_RXFIFO_FULL` interrupt
  284. *
  285. * @return None
  286. */
  287. void uart_hal_set_rxfifo_full_thr(uart_hal_context_t *hal, uint32_t full_thrhd);
  288. /**
  289. * @brief Set the UART txfifo empty threshold
  290. *
  291. * @param hal Context of the HAL layer
  292. * @param empty_thrhd The txfifo empty threshold to be set. If the `UART_TXFIFO_EMPTY` interrupt is enabled and
  293. * the data length in txfifo is less than this value, it will generate `UART_TXFIFO_EMPTY` interrupt
  294. *
  295. * @return None
  296. */
  297. void uart_hal_set_txfifo_empty_thr(uart_hal_context_t *hal, uint32_t empty_thrhd);
  298. /**
  299. * @brief Configure the UART to send a number of break(NULL) chars
  300. *
  301. * @param hal Context of the HAL layer
  302. * @param break_num The number of the break char need to be send
  303. *
  304. * @return None
  305. */
  306. void uart_hal_tx_break(uart_hal_context_t *hal, uint32_t break_num);
  307. /**
  308. * @brief Configure the UART wake up function.
  309. * Note that RXD cannot be input through GPIO Matrix but only through IO_MUX when use this function
  310. *
  311. * @param hal Context of the HAL layer
  312. * @param wakeup_thrd The wake up threshold to be set. The system will be woken up from light-sleep when the input RXD edge changes more times than `wakeup_thrd+2`
  313. *
  314. * @return None
  315. */
  316. void uart_hal_set_wakeup_thrd(uart_hal_context_t *hal, uint32_t wakeup_thrd);
  317. /**
  318. * @brief Configure the UART mode
  319. *
  320. * @param hal Context of the HAL layer
  321. * @param mode The UART mode to be set
  322. *
  323. * @return None
  324. */
  325. void uart_hal_set_mode(uart_hal_context_t *hal, uart_mode_t mode);
  326. /**
  327. * @brief Configure the UART hardware to inverse the signals
  328. *
  329. * @param hal Context of the HAL layer
  330. * @param inv_mask The sigal mask needs to be inversed. Use the ORred mask of type `uart_signal_inv_t`
  331. *
  332. * @return None
  333. */
  334. void uart_hal_inverse_signal(uart_hal_context_t *hal, uint32_t inv_mask);
  335. /**
  336. * @brief Get the UART wakeup threshold configuration
  337. *
  338. * @param hal Context of the HAL layer
  339. * @param wakeup_thrd Pointer to accept the value of UART wakeup threshold configuration
  340. *
  341. * @return None
  342. */
  343. void uart_hal_get_wakeup_thrd(uart_hal_context_t *hal, uint32_t *wakeup_thrd);
  344. /**
  345. * @brief Get the UART data bit configuration
  346. *
  347. * @param hal Context of the HAL layer
  348. * @param data_bit Pointer to accept the value of UART data bit configuration
  349. *
  350. * @return None
  351. */
  352. void uart_hal_get_data_bit_num(uart_hal_context_t *hal, uart_word_length_t *data_bit);
  353. /**
  354. * @brief Get the UART stop bit configuration
  355. *
  356. * @param hal Context of the HAL layer
  357. * @param stop_bit Pointer to accept the value of UART stop bit configuration
  358. *
  359. * @return None
  360. */
  361. void uart_hal_get_stop_bits(uart_hal_context_t *hal, uart_stop_bits_t *stop_bit);
  362. /**
  363. * @brief Get the UART parity mode configuration
  364. *
  365. * @param hal Context of the HAL layer
  366. * @param parity_mode Pointer to accept the UART parity mode configuration
  367. *
  368. * @return None
  369. */
  370. void uart_hal_get_parity(uart_hal_context_t *hal, uart_parity_t *parity_mode);
  371. /**
  372. * @brief Get the UART baud-rate configuration
  373. *
  374. * @param hal Context of the HAL layer
  375. * @param baud_rate Pointer to accept the current baud-rate
  376. * @param sclk_freq Frequency of the clock source of UART, in Hz.
  377. *
  378. * @return None
  379. */
  380. void uart_hal_get_baudrate(uart_hal_context_t *hal, uint32_t *baud_rate, uint32_t sclk_freq);
  381. /**
  382. * @brief Get the hw flow control configuration
  383. *
  384. * @param hal Context of the HAL layer
  385. * @param flow_ctrl Pointer to accept the UART flow control configuration
  386. *
  387. * @return None
  388. */
  389. void uart_hal_get_hw_flow_ctrl(uart_hal_context_t *hal, uart_hw_flowcontrol_t *flow_ctrl);
  390. /**
  391. * @brief Check if the UART rts flow control is enabled
  392. *
  393. * @param hal Context of the HAL layer
  394. *
  395. * @return True if rts flow control is enabled, otherwise false will be returned
  396. */
  397. bool uart_hal_is_hw_rts_en(uart_hal_context_t *hal);
  398. /**
  399. * @brief Configure TX signal loop back to RX module, just for the testing purposes
  400. *
  401. * @param hal Context of the HAL layer
  402. * @param loop_back_en Set ture to enable the loop back function, else set it false.
  403. *
  404. * @return None
  405. */
  406. void uart_hal_set_loop_back(uart_hal_context_t *hal, bool loop_back_en);
  407. /**
  408. * @brief Calculate uart symbol bit length, as defined in configuration.
  409. *
  410. * @param hw Beginning address of the peripheral registers.
  411. *
  412. * @return number of bits per UART symbol.
  413. */
  414. uint8_t uart_hal_get_symb_len(uart_hal_context_t *hal);
  415. /**
  416. * @brief Get UART maximum timeout threshold.
  417. *
  418. * @param hw Beginning address of the peripheral registers.
  419. *
  420. * @return maximum timeout threshold value for target.
  421. */
  422. uint16_t uart_hal_get_max_rx_timeout_thrd(uart_hal_context_t *hal);
  423. /**
  424. * @brief Get the timeout threshold value set for receiver.
  425. *
  426. * @param hw Beginning address of the peripheral registers.
  427. *
  428. * @return tout_thr The timeout value. If timeout is disabled then returns 0.
  429. */
  430. #define uart_hal_get_rx_tout_thr(hal) uart_ll_get_rx_tout_thr((hal)->dev)
  431. /**
  432. * @brief Get the length of readable data in UART rxfifo.
  433. *
  434. * @param hw Beginning address of the peripheral registers.
  435. *
  436. * @return The readable data length in rxfifo.
  437. */
  438. #define uart_hal_get_rxfifo_len(hal) uart_ll_get_rxfifo_len((hal)->dev)
  439. #ifdef __cplusplus
  440. }
  441. #endif