uart.h 31 KB

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