uart.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include "esp_err.h"
  11. #include "esp_intr_alloc.h"
  12. #include "soc/soc_caps.h"
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/semphr.h"
  15. #include "freertos/task.h"
  16. #include "freertos/queue.h"
  17. #include "freertos/ringbuf.h"
  18. #include "hal/uart_types.h"
  19. // Valid UART port number
  20. #define UART_NUM_0 (0) /*!< UART port 0 */
  21. #define UART_NUM_1 (1) /*!< UART port 1 */
  22. #if SOC_UART_NUM > 2
  23. #define UART_NUM_2 (2) /*!< UART port 2 */
  24. #endif
  25. #define UART_NUM_MAX (SOC_UART_NUM) /*!< UART port max */
  26. /* @brief When calling `uart_set_pin`, instead of GPIO number, `UART_PIN_NO_CHANGE`
  27. * can be provided to keep the currently allocated pin.
  28. */
  29. #define UART_PIN_NO_CHANGE (-1)
  30. #define UART_FIFO_LEN SOC_UART_FIFO_LEN ///< Length of the UART HW FIFO
  31. #define UART_BITRATE_MAX SOC_UART_BITRATE_MAX ///< Maximum configurable bitrate
  32. /**
  33. * @brief UART interrupt configuration parameters for uart_intr_config function
  34. */
  35. typedef struct {
  36. uint32_t intr_enable_mask; /*!< UART interrupt enable mask, choose from UART_XXXX_INT_ENA_M under UART_INT_ENA_REG(i), connect with bit-or operator*/
  37. uint8_t rx_timeout_thresh; /*!< UART timeout interrupt threshold (unit: time of sending one byte)*/
  38. uint8_t txfifo_empty_intr_thresh; /*!< UART TX empty interrupt threshold.*/
  39. uint8_t rxfifo_full_thresh; /*!< UART RX full interrupt threshold.*/
  40. } uart_intr_config_t;
  41. /**
  42. * @brief UART event types used in the ring buffer
  43. */
  44. typedef enum {
  45. UART_DATA, /*!< UART data event*/
  46. UART_BREAK, /*!< UART break event*/
  47. UART_BUFFER_FULL, /*!< UART RX buffer full event*/
  48. UART_FIFO_OVF, /*!< UART FIFO overflow event*/
  49. UART_FRAME_ERR, /*!< UART RX frame error event*/
  50. UART_PARITY_ERR, /*!< UART RX parity event*/
  51. UART_DATA_BREAK, /*!< UART TX data and break event*/
  52. UART_PATTERN_DET, /*!< UART pattern detected */
  53. #if SOC_UART_SUPPORT_WAKEUP_INT
  54. UART_WAKEUP, /*!< UART wakeup event */
  55. #endif
  56. UART_EVENT_MAX, /*!< UART event max index*/
  57. } uart_event_type_t;
  58. /**
  59. * @brief Event structure used in UART event queue
  60. */
  61. typedef struct {
  62. uart_event_type_t type; /*!< UART event type */
  63. size_t size; /*!< UART data size for UART_DATA event*/
  64. bool timeout_flag; /*!< UART data read timeout flag for UART_DATA event (no new data received during configured RX TOUT)*/
  65. /*!< If the event is caused by FIFO-full interrupt, then there will be no event with the timeout flag before the next byte coming.*/
  66. } uart_event_t;
  67. typedef intr_handle_t uart_isr_handle_t;
  68. /**
  69. * @brief Install UART driver and set the UART to the default configuration.
  70. *
  71. * UART ISR handler will be attached to the same CPU core that this function is running on.
  72. *
  73. * @note Rx_buffer_size should be greater than UART_FIFO_LEN. Tx_buffer_size should be either zero or greater than UART_FIFO_LEN.
  74. *
  75. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  76. * @param rx_buffer_size UART RX ring buffer size.
  77. * @param tx_buffer_size UART TX ring buffer size.
  78. * If set to zero, driver will not use TX buffer, TX function will block task until all data have been sent out.
  79. * @param queue_size UART event queue size/depth.
  80. * @param uart_queue UART event queue handle (out param). On success, a new queue handle is written here to provide
  81. * access to UART events. If set to NULL, driver will not use an event queue.
  82. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  83. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info. Do not set ESP_INTR_FLAG_IRAM here
  84. * (the driver's ISR handler is not located in IRAM)
  85. *
  86. * @return
  87. * - ESP_OK Success
  88. * - ESP_FAIL Parameter error
  89. */
  90. esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int queue_size, QueueHandle_t* uart_queue, int intr_alloc_flags);
  91. /**
  92. * @brief Uninstall UART driver.
  93. *
  94. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  95. *
  96. * @return
  97. * - ESP_OK Success
  98. * - ESP_FAIL Parameter error
  99. */
  100. esp_err_t uart_driver_delete(uart_port_t uart_num);
  101. /**
  102. * @brief Checks whether the driver is installed or not
  103. *
  104. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  105. *
  106. * @return
  107. * - true driver is installed
  108. * - false driver is not installed
  109. */
  110. bool uart_is_driver_installed(uart_port_t uart_num);
  111. /**
  112. * @brief Set UART data bits.
  113. *
  114. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  115. * @param data_bit UART data bits
  116. *
  117. * @return
  118. * - ESP_OK Success
  119. * - ESP_FAIL Parameter error
  120. */
  121. esp_err_t uart_set_word_length(uart_port_t uart_num, uart_word_length_t data_bit);
  122. /**
  123. * @brief Get the UART data bit configuration.
  124. *
  125. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  126. * @param data_bit Pointer to accept value of UART data bits.
  127. *
  128. * @return
  129. * - ESP_FAIL Parameter error
  130. * - ESP_OK Success, result will be put in (*data_bit)
  131. */
  132. esp_err_t uart_get_word_length(uart_port_t uart_num, uart_word_length_t* data_bit);
  133. /**
  134. * @brief Set UART stop bits.
  135. *
  136. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  137. * @param stop_bits UART stop bits
  138. *
  139. * @return
  140. * - ESP_OK Success
  141. * - ESP_FAIL Fail
  142. */
  143. esp_err_t uart_set_stop_bits(uart_port_t uart_num, uart_stop_bits_t stop_bits);
  144. /**
  145. * @brief Get the UART stop bit configuration.
  146. *
  147. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  148. * @param stop_bits Pointer to accept value of UART stop bits.
  149. *
  150. * @return
  151. * - ESP_FAIL Parameter error
  152. * - ESP_OK Success, result will be put in (*stop_bit)
  153. */
  154. esp_err_t uart_get_stop_bits(uart_port_t uart_num, uart_stop_bits_t* stop_bits);
  155. /**
  156. * @brief Set UART parity mode.
  157. *
  158. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  159. * @param parity_mode the enum of uart parity configuration
  160. *
  161. * @return
  162. * - ESP_FAIL Parameter error
  163. * - ESP_OK Success
  164. */
  165. esp_err_t uart_set_parity(uart_port_t uart_num, uart_parity_t parity_mode);
  166. /**
  167. * @brief Get the UART parity mode configuration.
  168. *
  169. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  170. * @param parity_mode Pointer to accept value of UART parity mode.
  171. *
  172. * @return
  173. * - ESP_FAIL Parameter error
  174. * - ESP_OK Success, result will be put in (*parity_mode)
  175. *
  176. */
  177. esp_err_t uart_get_parity(uart_port_t uart_num, uart_parity_t* parity_mode);
  178. /**
  179. * @brief Get the frequency of a clock source for the UART
  180. *
  181. * @param sclk Clock source
  182. * @param[out] out_freq_hz Output of frequency, in Hz
  183. *
  184. * @return
  185. * - ESP_ERR_INVALID_ARG: if the clock source is not supported
  186. * - otherwise ESP_OK
  187. */
  188. esp_err_t uart_get_sclk_freq(uart_sclk_t sclk, uint32_t* out_freq_hz);
  189. /**
  190. * @brief Set UART baud rate.
  191. *
  192. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  193. * @param baudrate UART baud rate.
  194. *
  195. * @return
  196. * - ESP_FAIL Parameter error
  197. * - ESP_OK Success
  198. */
  199. esp_err_t uart_set_baudrate(uart_port_t uart_num, uint32_t baudrate);
  200. /**
  201. * @brief Get the UART baud rate configuration.
  202. *
  203. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  204. * @param baudrate Pointer to accept value of UART baud rate
  205. *
  206. * @return
  207. * - ESP_FAIL Parameter error
  208. * - ESP_OK Success, result will be put in (*baudrate)
  209. *
  210. */
  211. esp_err_t uart_get_baudrate(uart_port_t uart_num, uint32_t* baudrate);
  212. /**
  213. * @brief Set UART line inverse mode
  214. *
  215. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  216. * @param inverse_mask Choose the wires that need to be inverted. Using the ORred mask of `uart_signal_inv_t`
  217. *
  218. * @return
  219. * - ESP_OK Success
  220. * - ESP_FAIL Parameter error
  221. */
  222. esp_err_t uart_set_line_inverse(uart_port_t uart_num, uint32_t inverse_mask);
  223. /**
  224. * @brief Set hardware flow control.
  225. *
  226. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  227. * @param flow_ctrl Hardware flow control mode
  228. * @param rx_thresh Threshold of Hardware RX flow control (0 ~ UART_FIFO_LEN).
  229. * Only when UART_HW_FLOWCTRL_RTS is set, will the rx_thresh value be set.
  230. *
  231. * @return
  232. * - ESP_OK Success
  233. * - ESP_FAIL Parameter error
  234. */
  235. esp_err_t uart_set_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t flow_ctrl, uint8_t rx_thresh);
  236. /**
  237. * @brief Set software flow control.
  238. *
  239. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  240. * @param enable switch on or off
  241. * @param rx_thresh_xon low water mark
  242. * @param rx_thresh_xoff high water mark
  243. *
  244. * @return
  245. * - ESP_OK Success
  246. * - ESP_FAIL Parameter error
  247. */
  248. esp_err_t uart_set_sw_flow_ctrl(uart_port_t uart_num, bool enable, uint8_t rx_thresh_xon, uint8_t rx_thresh_xoff);
  249. /**
  250. * @brief Get the UART hardware flow control configuration.
  251. *
  252. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  253. * @param flow_ctrl Option for different flow control mode.
  254. *
  255. * @return
  256. * - ESP_FAIL Parameter error
  257. * - ESP_OK Success, result will be put in (*flow_ctrl)
  258. */
  259. esp_err_t uart_get_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t* flow_ctrl);
  260. /**
  261. * @brief Clear UART interrupt status
  262. *
  263. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  264. * @param clr_mask Bit mask of the interrupt status to be cleared.
  265. *
  266. * @return
  267. * - ESP_OK Success
  268. * - ESP_FAIL Parameter error
  269. */
  270. esp_err_t uart_clear_intr_status(uart_port_t uart_num, uint32_t clr_mask);
  271. /**
  272. * @brief Set UART interrupt enable
  273. *
  274. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  275. * @param enable_mask Bit mask of the enable bits.
  276. *
  277. * @return
  278. * - ESP_OK Success
  279. * - ESP_FAIL Parameter error
  280. */
  281. esp_err_t uart_enable_intr_mask(uart_port_t uart_num, uint32_t enable_mask);
  282. /**
  283. * @brief Clear UART interrupt enable bits
  284. *
  285. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  286. * @param disable_mask Bit mask of the disable bits.
  287. *
  288. * @return
  289. * - ESP_OK Success
  290. * - ESP_FAIL Parameter error
  291. */
  292. esp_err_t uart_disable_intr_mask(uart_port_t uart_num, uint32_t disable_mask);
  293. /**
  294. * @brief Enable UART RX interrupt (RX_FULL & RX_TIMEOUT INTERRUPT)
  295. *
  296. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  297. *
  298. * @return
  299. * - ESP_OK Success
  300. * - ESP_FAIL Parameter error
  301. */
  302. esp_err_t uart_enable_rx_intr(uart_port_t uart_num);
  303. /**
  304. * @brief Disable UART RX interrupt (RX_FULL & RX_TIMEOUT INTERRUPT)
  305. *
  306. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  307. *
  308. * @return
  309. * - ESP_OK Success
  310. * - ESP_FAIL Parameter error
  311. */
  312. esp_err_t uart_disable_rx_intr(uart_port_t uart_num);
  313. /**
  314. * @brief Disable UART TX interrupt (TX_FULL & TX_TIMEOUT INTERRUPT)
  315. *
  316. * @param uart_num UART port number
  317. *
  318. * @return
  319. * - ESP_OK Success
  320. * - ESP_FAIL Parameter error
  321. */
  322. esp_err_t uart_disable_tx_intr(uart_port_t uart_num);
  323. /**
  324. * @brief Enable UART TX interrupt (TX_FULL & TX_TIMEOUT INTERRUPT)
  325. *
  326. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  327. * @param enable 1: enable; 0: disable
  328. * @param thresh Threshold of TX interrupt, 0 ~ UART_FIFO_LEN
  329. *
  330. * @return
  331. * - ESP_OK Success
  332. * - ESP_FAIL Parameter error
  333. */
  334. esp_err_t uart_enable_tx_intr(uart_port_t uart_num, int enable, int thresh);
  335. /**
  336. * @brief Assign signals of a UART peripheral to GPIO pins
  337. *
  338. * @note If the GPIO number configured for a UART signal matches one of the
  339. * IOMUX signals for that GPIO, the signal will be connected directly
  340. * via the IOMUX. Otherwise the GPIO and signal will be connected via
  341. * the GPIO Matrix. For example, if on an ESP32 the call
  342. * `uart_set_pin(0, 1, 3, -1, -1)` is performed, as GPIO1 is UART0's
  343. * default TX pin and GPIO3 is UART0's default RX pin, both will be
  344. * connected to respectively U0TXD and U0RXD through the IOMUX, totally
  345. * bypassing the GPIO matrix.
  346. * The check is performed on a per-pin basis. Thus, it is possible to have
  347. * RX pin binded to a GPIO through the GPIO matrix, whereas TX is binded
  348. * to its GPIO through the IOMUX.
  349. *
  350. * @note Internal signal can be output to multiple GPIO pads.
  351. * Only one GPIO pad can connect with input signal.
  352. *
  353. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  354. * @param tx_io_num UART TX pin GPIO number.
  355. * @param rx_io_num UART RX pin GPIO number.
  356. * @param rts_io_num UART RTS pin GPIO number.
  357. * @param cts_io_num UART CTS pin GPIO number.
  358. *
  359. * @return
  360. * - ESP_OK Success
  361. * - ESP_FAIL Parameter error
  362. */
  363. esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num);
  364. /**
  365. * @brief Manually set the UART RTS pin level.
  366. * @note UART must be configured with hardware flow control disabled.
  367. *
  368. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  369. * @param level 1: RTS output low (active); 0: RTS output high (block)
  370. *
  371. * @return
  372. * - ESP_OK Success
  373. * - ESP_FAIL Parameter error
  374. */
  375. esp_err_t uart_set_rts(uart_port_t uart_num, int level);
  376. /**
  377. * @brief Manually set the UART DTR pin level.
  378. *
  379. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  380. * @param level 1: DTR output low; 0: DTR output high
  381. *
  382. * @return
  383. * - ESP_OK Success
  384. * - ESP_FAIL Parameter error
  385. */
  386. esp_err_t uart_set_dtr(uart_port_t uart_num, int level);
  387. /**
  388. * @brief Set UART idle interval after tx FIFO is empty
  389. *
  390. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  391. * @param idle_num idle interval after tx FIFO is empty(unit: the time it takes to send one bit
  392. * under current baudrate)
  393. *
  394. * @return
  395. * - ESP_OK Success
  396. * - ESP_FAIL Parameter error
  397. */
  398. esp_err_t uart_set_tx_idle_num(uart_port_t uart_num, uint16_t idle_num);
  399. /**
  400. * @brief Set UART configuration parameters.
  401. *
  402. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  403. * @param uart_config UART parameter settings
  404. *
  405. * @return
  406. * - ESP_OK Success
  407. * - ESP_FAIL Parameter error
  408. */
  409. esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config);
  410. /**
  411. * @brief Configure UART interrupts.
  412. *
  413. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  414. * @param intr_conf UART interrupt settings
  415. *
  416. * @return
  417. * - ESP_OK Success
  418. * - ESP_FAIL Parameter error
  419. */
  420. esp_err_t uart_intr_config(uart_port_t uart_num, const uart_intr_config_t *intr_conf);
  421. /**
  422. * @brief Wait until UART TX FIFO is empty.
  423. *
  424. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  425. * @param ticks_to_wait Timeout, count in RTOS ticks
  426. *
  427. * @return
  428. * - ESP_OK Success
  429. * - ESP_FAIL Parameter error
  430. * - ESP_ERR_TIMEOUT Timeout
  431. */
  432. esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait);
  433. /**
  434. * @brief Send data to the UART port from a given buffer and length.
  435. *
  436. * This function will not wait for enough space in TX FIFO. It will just fill the available TX FIFO and return when the FIFO is full.
  437. * @note This function should only be used when UART TX buffer is not enabled.
  438. *
  439. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  440. * @param buffer data buffer address
  441. * @param len data length to send
  442. *
  443. * @return
  444. * - (-1) Parameter error
  445. * - OTHERS (>=0) The number of bytes pushed to the TX FIFO
  446. */
  447. int uart_tx_chars(uart_port_t uart_num, const char* buffer, uint32_t len);
  448. /**
  449. * @brief Send data to the UART port from a given buffer and length,
  450. *
  451. * If the UART driver's parameter 'tx_buffer_size' is set to zero:
  452. * This function will not return until all the data have been sent out, or at least pushed into TX FIFO.
  453. *
  454. * Otherwise, if the 'tx_buffer_size' > 0, this function will return after copying all the data to tx ring buffer,
  455. * UART ISR will then move data from the ring buffer to TX FIFO gradually.
  456. *
  457. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  458. * @param src data buffer address
  459. * @param size data length to send
  460. *
  461. * @return
  462. * - (-1) Parameter error
  463. * - OTHERS (>=0) The number of bytes pushed to the TX FIFO
  464. */
  465. int uart_write_bytes(uart_port_t uart_num, const void* src, size_t size);
  466. /**
  467. * @brief Send data to the UART port from a given buffer and length,
  468. *
  469. * If the UART driver's parameter 'tx_buffer_size' is set to zero:
  470. * This function will not return until all the data and the break signal have been sent out.
  471. * After all data is sent out, send a break signal.
  472. *
  473. * Otherwise, if the 'tx_buffer_size' > 0, this function will return after copying all the data to tx ring buffer,
  474. * UART ISR will then move data from the ring buffer to TX FIFO gradually.
  475. * After all data sent out, send a break signal.
  476. *
  477. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  478. * @param src data buffer address
  479. * @param size data length to send
  480. * @param brk_len break signal duration(unit: the time it takes to send one bit at current baudrate)
  481. *
  482. * @return
  483. * - (-1) Parameter error
  484. * - OTHERS (>=0) The number of bytes pushed to the TX FIFO
  485. */
  486. int uart_write_bytes_with_break(uart_port_t uart_num, const void* src, size_t size, int brk_len);
  487. /**
  488. * @brief UART read bytes from UART buffer
  489. *
  490. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  491. * @param buf pointer to the buffer.
  492. * @param length data length
  493. * @param ticks_to_wait sTimeout, count in RTOS ticks
  494. *
  495. * @return
  496. * - (-1) Error
  497. * - OTHERS (>=0) The number of bytes read from UART FIFO
  498. */
  499. int uart_read_bytes(uart_port_t uart_num, void* buf, uint32_t length, TickType_t ticks_to_wait);
  500. /**
  501. * @brief Alias of uart_flush_input.
  502. * UART ring buffer flush. This will discard all data in the UART RX buffer.
  503. * @note Instead of waiting the data sent out, this function will clear UART rx buffer.
  504. * In order to send all the data in tx FIFO, we can use uart_wait_tx_done function.
  505. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  506. *
  507. * @return
  508. * - ESP_OK Success
  509. * - ESP_FAIL Parameter error
  510. */
  511. esp_err_t uart_flush(uart_port_t uart_num);
  512. /**
  513. * @brief Clear input buffer, discard all the data is in the ring-buffer.
  514. * @note In order to send all the data in tx FIFO, we can use uart_wait_tx_done function.
  515. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  516. *
  517. * @return
  518. * - ESP_OK Success
  519. * - ESP_FAIL Parameter error
  520. */
  521. esp_err_t uart_flush_input(uart_port_t uart_num);
  522. /**
  523. * @brief UART get RX ring buffer cached data length
  524. *
  525. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  526. * @param size Pointer of size_t to accept cached data length
  527. *
  528. * @return
  529. * - ESP_OK Success
  530. * - ESP_FAIL Parameter error
  531. */
  532. esp_err_t uart_get_buffered_data_len(uart_port_t uart_num, size_t* size);
  533. /**
  534. * @brief UART get TX ring buffer free space size
  535. *
  536. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  537. * @param size Pointer of size_t to accept the free space size
  538. *
  539. * @return
  540. * - ESP_OK Success
  541. * - ESP_ERR_INVALID_ARG Parameter error
  542. */
  543. esp_err_t uart_get_tx_buffer_free_size(uart_port_t uart_num, size_t *size);
  544. /**
  545. * @brief UART disable pattern detect function.
  546. * Designed for applications like 'AT commands'.
  547. * When the hardware detects a series of one same character, the interrupt will be triggered.
  548. *
  549. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  550. *
  551. * @return
  552. * - ESP_OK Success
  553. * - ESP_FAIL Parameter error
  554. */
  555. esp_err_t uart_disable_pattern_det_intr(uart_port_t uart_num);
  556. /**
  557. * @brief UART enable pattern detect function.
  558. * Designed for applications like 'AT commands'.
  559. * When the hardware detect a series of one same character, the interrupt will be triggered.
  560. *
  561. * @param uart_num UART port number.
  562. * @param pattern_chr character of the pattern.
  563. * @param chr_num number of the character, 8bit value.
  564. * @param chr_tout timeout of the interval between each pattern characters, 16bit value, unit is the baud-rate cycle you configured.
  565. * When the duration is more than this value, it will not take this data as at_cmd char.
  566. * @param post_idle idle time after the last pattern character, 16bit value, unit is the baud-rate cycle you configured.
  567. * When the duration is less than this value, it will not take the previous data as the last at_cmd char
  568. * @param pre_idle idle time before the first pattern character, 16bit value, unit is the baud-rate cycle you configured.
  569. * When the duration is less than this value, it will not take this data as the first at_cmd char.
  570. *
  571. * @return
  572. * - ESP_OK Success
  573. * - ESP_FAIL Parameter error
  574. */
  575. esp_err_t uart_enable_pattern_det_baud_intr(uart_port_t uart_num, char pattern_chr, uint8_t chr_num, int chr_tout, int post_idle, int pre_idle);
  576. /**
  577. * @brief Return the nearest detected pattern position in buffer.
  578. * The positions of the detected pattern are saved in a queue,
  579. * this function will dequeue the first pattern position and move the pointer to next pattern position.
  580. * @note If the RX buffer is full and flow control is not enabled,
  581. * the detected pattern may not be found in the rx buffer due to overflow.
  582. *
  583. * The following APIs will modify the pattern position info:
  584. * uart_flush_input, uart_read_bytes, uart_driver_delete, uart_pop_pattern_pos
  585. * It is the application's responsibility to ensure atomic access to the pattern queue and the rx data buffer
  586. * when using pattern detect feature.
  587. *
  588. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  589. * @return
  590. * - (-1) No pattern found for current index or parameter error
  591. * - others the pattern position in rx buffer.
  592. */
  593. int uart_pattern_pop_pos(uart_port_t uart_num);
  594. /**
  595. * @brief Return the nearest detected pattern position in buffer.
  596. * The positions of the detected pattern are saved in a queue,
  597. * This function do nothing to the queue.
  598. * @note If the RX buffer is full and flow control is not enabled,
  599. * the detected pattern may not be found in the rx buffer due to overflow.
  600. *
  601. * The following APIs will modify the pattern position info:
  602. * uart_flush_input, uart_read_bytes, uart_driver_delete, uart_pop_pattern_pos
  603. * It is the application's responsibility to ensure atomic access to the pattern queue and the rx data buffer
  604. * when using pattern detect feature.
  605. *
  606. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  607. * @return
  608. * - (-1) No pattern found for current index or parameter error
  609. * - others the pattern position in rx buffer.
  610. */
  611. int uart_pattern_get_pos(uart_port_t uart_num);
  612. /**
  613. * @brief Allocate a new memory with the given length to save record the detected pattern position in rx buffer.
  614. *
  615. * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
  616. * @param queue_length Max queue length for the detected pattern.
  617. * If the queue length is not large enough, some pattern positions might be lost.
  618. * Set this value to the maximum number of patterns that could be saved in data buffer at the same time.
  619. * @return
  620. * - ESP_ERR_NO_MEM No enough memory
  621. * - ESP_ERR_INVALID_STATE Driver not installed
  622. * - ESP_FAIL Parameter error
  623. * - ESP_OK Success
  624. */
  625. esp_err_t uart_pattern_queue_reset(uart_port_t uart_num, int queue_length);
  626. /**
  627. * @brief UART set communication mode
  628. *
  629. * @note This function must be executed after uart_driver_install(), when the driver object is initialized.
  630. * @param uart_num Uart number to configure, the max port number is (UART_NUM_MAX -1).
  631. * @param mode UART UART mode to set
  632. *
  633. * @return
  634. * - ESP_OK Success
  635. * - ESP_ERR_INVALID_ARG Parameter error
  636. */
  637. esp_err_t uart_set_mode(uart_port_t uart_num, uart_mode_t mode);
  638. /**
  639. * @brief Set uart threshold value for RX fifo full
  640. * @note If application is using higher baudrate and it is observed that bytes
  641. * in hardware RX fifo are overwritten then this threshold can be reduced
  642. *
  643. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  644. * @param threshold Threshold value above which RX fifo full interrupt is generated
  645. *
  646. * @return
  647. * - ESP_OK Success
  648. * - ESP_ERR_INVALID_ARG Parameter error
  649. * - ESP_ERR_INVALID_STATE Driver is not installed
  650. */
  651. esp_err_t uart_set_rx_full_threshold(uart_port_t uart_num, int threshold);
  652. /**
  653. * @brief Set uart threshold values for TX fifo empty
  654. *
  655. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  656. * @param threshold Threshold value below which TX fifo empty interrupt is generated
  657. *
  658. * @return
  659. * - ESP_OK Success
  660. * - ESP_ERR_INVALID_ARG Parameter error
  661. * - ESP_ERR_INVALID_STATE Driver is not installed
  662. */
  663. esp_err_t uart_set_tx_empty_threshold(uart_port_t uart_num, int threshold);
  664. /**
  665. * @brief UART set threshold timeout for TOUT feature
  666. *
  667. * @param uart_num Uart number to configure, the max port number is (UART_NUM_MAX -1).
  668. * @param tout_thresh This parameter defines timeout threshold in uart symbol periods. The maximum value of threshold is 126.
  669. * tout_thresh = 1, defines TOUT interrupt timeout equal to transmission time of one symbol (~11 bit) on current baudrate.
  670. * If the time is expired the UART_RXFIFO_TOUT_INT interrupt is triggered. If tout_thresh == 0,
  671. * the TOUT feature is disabled.
  672. *
  673. * @return
  674. * - ESP_OK Success
  675. * - ESP_ERR_INVALID_ARG Parameter error
  676. * - ESP_ERR_INVALID_STATE Driver is not installed
  677. */
  678. esp_err_t uart_set_rx_timeout(uart_port_t uart_num, const uint8_t tout_thresh);
  679. /**
  680. * @brief Returns collision detection flag for RS485 mode
  681. * Function returns the collision detection flag into variable pointed by collision_flag.
  682. * *collision_flag = true, if collision detected else it is equal to false.
  683. * This function should be executed when actual transmission is completed (after uart_write_bytes()).
  684. *
  685. * @param uart_num Uart number to configure the max port number is (UART_NUM_MAX -1).
  686. * @param collision_flag Pointer to variable of type bool to return collision flag.
  687. *
  688. * @return
  689. * - ESP_OK Success
  690. * - ESP_ERR_INVALID_ARG Parameter error
  691. */
  692. esp_err_t uart_get_collision_flag(uart_port_t uart_num, bool* collision_flag);
  693. /**
  694. * @brief Set the number of RX pin signal edges for light sleep wakeup
  695. *
  696. * UART can be used to wake up the system from light sleep. This feature works
  697. * by counting the number of positive edges on RX pin and comparing the count to
  698. * the threshold. When the count exceeds the threshold, system is woken up from
  699. * light sleep. This function allows setting the threshold value.
  700. *
  701. * Stop bit and parity bits (if enabled) also contribute to the number of edges.
  702. * For example, letter 'a' with ASCII code 97 is encoded as 0100001101 on the wire
  703. * (with 8n1 configuration), start and stop bits included. This sequence has 3
  704. * positive edges (transitions from 0 to 1). Therefore, to wake up the system
  705. * when 'a' is sent, set wakeup_threshold=3.
  706. *
  707. * The character that triggers wakeup is not received by UART (i.e. it can not
  708. * be obtained from UART FIFO). Depending on the baud rate, a few characters
  709. * after that will also not be received. Note that when the chip enters and exits
  710. * light sleep mode, APB frequency will be changing. To make sure that UART has
  711. * correct baud rate all the time, select UART_SCLK_REF_TICK or UART_SCLK_XTAL as UART clock source in uart_config_t::source_clk.
  712. *
  713. * @note in ESP32, the wakeup signal can only be input via IO_MUX (i.e.
  714. * GPIO3 should be configured as function_1 to wake up UART0,
  715. * GPIO9 should be configured as function_5 to wake up UART1), UART2
  716. * does not support light sleep wakeup feature.
  717. *
  718. * @param uart_num UART number, the max port number is (UART_NUM_MAX -1).
  719. * @param wakeup_threshold number of RX edges for light sleep wakeup, value is 3 .. 0x3ff.
  720. * @return
  721. * - ESP_OK on success
  722. * - ESP_ERR_INVALID_ARG if uart_num is incorrect or wakeup_threshold is
  723. * outside of [3, 0x3ff] range.
  724. */
  725. esp_err_t uart_set_wakeup_threshold(uart_port_t uart_num, int wakeup_threshold);
  726. /**
  727. * @brief Get the number of RX pin signal edges for light sleep wakeup.
  728. *
  729. * See description of uart_set_wakeup_threshold for the explanation of UART
  730. * wakeup feature.
  731. *
  732. * @param uart_num UART number, the max port number is (UART_NUM_MAX -1).
  733. * @param[out] out_wakeup_threshold output, set to the current value of wakeup
  734. * threshold for the given UART.
  735. * @return
  736. * - ESP_OK on success
  737. * - ESP_ERR_INVALID_ARG if out_wakeup_threshold is NULL
  738. */
  739. esp_err_t uart_get_wakeup_threshold(uart_port_t uart_num, int* out_wakeup_threshold);
  740. /**
  741. * @brief Wait until UART tx memory empty and the last char send ok (polling mode).
  742. *
  743. * @param uart_num UART number
  744. *
  745. * * @return
  746. * - ESP_OK on success
  747. * - ESP_ERR_INVALID_ARG Parameter error
  748. * - ESP_FAIL Driver not installed
  749. */
  750. esp_err_t uart_wait_tx_idle_polling(uart_port_t uart_num);
  751. /**
  752. * @brief Configure TX signal loop back to RX module, just for the test usage.
  753. *
  754. * @param uart_num UART number
  755. * @param loop_back_en Set ture to enable the loop back function, else set it false.
  756. *
  757. * * @return
  758. * - ESP_OK on success
  759. * - ESP_ERR_INVALID_ARG Parameter error
  760. * - ESP_FAIL Driver not installed
  761. */
  762. esp_err_t uart_set_loop_back(uart_port_t uart_num, bool loop_back_en);
  763. /**
  764. * @brief Configure behavior of UART RX timeout interrupt.
  765. *
  766. * When always_rx_timeout is true, timeout interrupt is triggered even if FIFO is full.
  767. * This function can cause extra timeout interrupts triggered only to send the timeout event.
  768. * Call this function only if you want to ensure timeout interrupt will always happen after a byte stream.
  769. *
  770. * @param uart_num UART number
  771. * @param always_rx_timeout_en Set to false enable the default behavior of timeout interrupt,
  772. * set it to true to always trigger timeout interrupt.
  773. *
  774. */
  775. void uart_set_always_rx_timeout(uart_port_t uart_num, bool always_rx_timeout_en);
  776. #ifdef __cplusplus
  777. }
  778. #endif