uart.h 32 KB

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