uart.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _DRIVER_UART_H_
  14. #define _DRIVER_UART_H_
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "soc/uart_reg.h"
  19. #include "soc/uart_struct.h"
  20. #include "esp_err.h"
  21. #include "esp_intr_alloc.h"
  22. #include "driver/periph_ctrl.h"
  23. #include "freertos/FreeRTOS.h"
  24. #include "freertos/semphr.h"
  25. #include "freertos/xtensa_api.h"
  26. #include "freertos/task.h"
  27. #include "freertos/queue.h"
  28. #include "freertos/ringbuf.h"
  29. #include <esp_types.h>
  30. #include "soc/uart_channel.h"
  31. #define UART_FIFO_LEN (128) /*!< Length of the hardware FIFO buffers */
  32. #define UART_INTR_MASK 0x1ff /*!< Mask of all UART interrupts */
  33. #define UART_LINE_INV_MASK (0x3f << 19) /*!< TBD */
  34. #define UART_BITRATE_MAX 5000000 /*!< Max bit rate supported by UART */
  35. #define UART_PIN_NO_CHANGE (-1) /*!< Constant for uart_set_pin function which indicates that UART pin should not be changed */
  36. #define UART_INVERSE_DISABLE (0x0) /*!< Disable UART signal inverse*/
  37. #define UART_INVERSE_RXD (UART_RXD_INV_M) /*!< UART RXD input inverse*/
  38. #define UART_INVERSE_CTS (UART_CTS_INV_M) /*!< UART CTS input inverse*/
  39. #define UART_INVERSE_TXD (UART_TXD_INV_M) /*!< UART TXD output inverse*/
  40. #define UART_INVERSE_RTS (UART_RTS_INV_M) /*!< UART RTS output inverse*/
  41. /**
  42. * @brief UART word length constants
  43. */
  44. typedef enum {
  45. UART_DATA_5_BITS = 0x0, /*!< word length: 5bits*/
  46. UART_DATA_6_BITS = 0x1, /*!< word length: 6bits*/
  47. UART_DATA_7_BITS = 0x2, /*!< word length: 7bits*/
  48. UART_DATA_8_BITS = 0x3, /*!< word length: 8bits*/
  49. UART_DATA_BITS_MAX = 0X4,
  50. } uart_word_length_t;
  51. /**
  52. * @brief UART stop bits number
  53. */
  54. typedef enum {
  55. UART_STOP_BITS_1 = 0x1, /*!< stop bit: 1bit*/
  56. UART_STOP_BITS_1_5 = 0x2, /*!< stop bit: 1.5bits*/
  57. UART_STOP_BITS_2 = 0x3, /*!< stop bit: 2bits*/
  58. UART_STOP_BITS_MAX = 0x4,
  59. } uart_stop_bits_t;
  60. /**
  61. * @brief UART peripheral number
  62. */
  63. typedef enum {
  64. UART_NUM_0 = 0x0, /*!< UART base address 0x3ff40000*/
  65. UART_NUM_1 = 0x1, /*!< UART base address 0x3ff50000*/
  66. UART_NUM_2 = 0x2, /*!< UART base address 0x3ff6e000*/
  67. UART_NUM_MAX,
  68. } uart_port_t;
  69. /**
  70. * @brief UART parity constants
  71. */
  72. typedef enum {
  73. UART_PARITY_DISABLE = 0x0, /*!< Disable UART parity*/
  74. UART_PARITY_EVEN = 0x2, /*!< Enable UART even parity*/
  75. UART_PARITY_ODD = 0x3 /*!< Enable UART odd parity*/
  76. } uart_parity_t;
  77. /**
  78. * @brief UART hardware flow control modes
  79. */
  80. typedef enum {
  81. UART_HW_FLOWCTRL_DISABLE = 0x0, /*!< disable hardware flow control*/
  82. UART_HW_FLOWCTRL_RTS = 0x1, /*!< enable RX hardware flow control (rts)*/
  83. UART_HW_FLOWCTRL_CTS = 0x2, /*!< enable TX hardware flow control (cts)*/
  84. UART_HW_FLOWCTRL_CTS_RTS = 0x3, /*!< enable hardware flow control*/
  85. UART_HW_FLOWCTRL_MAX = 0x4,
  86. } uart_hw_flowcontrol_t;
  87. /**
  88. * @brief UART configuration parameters for uart_param_config function
  89. */
  90. typedef struct {
  91. int baud_rate; /*!< UART baud rate*/
  92. uart_word_length_t data_bits; /*!< UART byte size*/
  93. uart_parity_t parity; /*!< UART parity mode*/
  94. uart_stop_bits_t stop_bits; /*!< UART stop bits*/
  95. uart_hw_flowcontrol_t flow_ctrl; /*!< UART HW flow control mode (cts/rts)*/
  96. uint8_t rx_flow_ctrl_thresh; /*!< UART HW RTS threshold*/
  97. bool use_ref_tick; /*!< Set to true if UART should be clocked from REF_TICK */
  98. } uart_config_t;
  99. /**
  100. * @brief UART interrupt configuration parameters for uart_intr_config function
  101. */
  102. typedef struct {
  103. 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*/
  104. uint8_t rx_timeout_thresh; /*!< UART timeout interrupt threshold (unit: time of sending one byte)*/
  105. uint8_t txfifo_empty_intr_thresh; /*!< UART TX empty interrupt threshold.*/
  106. uint8_t rxfifo_full_thresh; /*!< UART RX full interrupt threshold.*/
  107. } uart_intr_config_t;
  108. /**
  109. * @brief UART event types used in the ring buffer
  110. */
  111. typedef enum {
  112. UART_DATA, /*!< UART data event*/
  113. UART_BREAK, /*!< UART break event*/
  114. UART_BUFFER_FULL, /*!< UART RX buffer full event*/
  115. UART_FIFO_OVF, /*!< UART FIFO overflow event*/
  116. UART_FRAME_ERR, /*!< UART RX frame error event*/
  117. UART_PARITY_ERR, /*!< UART RX parity event*/
  118. UART_DATA_BREAK, /*!< UART TX data and break event*/
  119. UART_PATTERN_DET, /*!< UART pattern detected */
  120. UART_EVENT_MAX, /*!< UART event max index*/
  121. } uart_event_type_t;
  122. /**
  123. * @brief Event structure used in UART event queue
  124. */
  125. typedef struct {
  126. uart_event_type_t type; /*!< UART event type */
  127. size_t size; /*!< UART data size for UART_DATA event*/
  128. } uart_event_t;
  129. typedef intr_handle_t uart_isr_handle_t;
  130. /**
  131. * @brief Set UART data bits.
  132. *
  133. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  134. * @param data_bit UART data bits
  135. *
  136. * @return
  137. * - ESP_OK Success
  138. * - ESP_FAIL Parameter error
  139. */
  140. esp_err_t uart_set_word_length(uart_port_t uart_num, uart_word_length_t data_bit);
  141. /**
  142. * @brief Get UART data bits.
  143. *
  144. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  145. * @param data_bit Pointer to accept value of UART data bits.
  146. *
  147. * @return
  148. * - ESP_FAIL Parameter error
  149. * - ESP_OK Success, result will be put in (*data_bit)
  150. */
  151. esp_err_t uart_get_word_length(uart_port_t uart_num, uart_word_length_t* data_bit);
  152. /**
  153. * @brief Set UART stop bits.
  154. *
  155. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  156. * @param stop_bits UART stop bits
  157. *
  158. * @return
  159. * - ESP_OK Success
  160. * - ESP_FAIL Fail
  161. */
  162. esp_err_t uart_set_stop_bits(uart_port_t uart_num, uart_stop_bits_t stop_bits);
  163. /**
  164. * @brief Get UART stop bits.
  165. *
  166. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  167. * @param stop_bits Pointer to accept value of UART stop bits.
  168. *
  169. * @return
  170. * - ESP_FAIL Parameter error
  171. * - ESP_OK Success, result will be put in (*stop_bit)
  172. */
  173. esp_err_t uart_get_stop_bits(uart_port_t uart_num, uart_stop_bits_t* stop_bits);
  174. /**
  175. * @brief Set UART parity mode.
  176. *
  177. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  178. * @param parity_mode the enum of uart parity configuration
  179. *
  180. * @return
  181. * - ESP_FAIL Parameter error
  182. * - ESP_OK Success
  183. */
  184. esp_err_t uart_set_parity(uart_port_t uart_num, uart_parity_t parity_mode);
  185. /**
  186. * @brief Get UART parity mode.
  187. *
  188. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  189. * @param parity_mode Pointer to accept value of UART parity mode.
  190. *
  191. * @return
  192. * - ESP_FAIL Parameter error
  193. * - ESP_OK Success, result will be put in (*parity_mode)
  194. *
  195. */
  196. esp_err_t uart_get_parity(uart_port_t uart_num, uart_parity_t* parity_mode);
  197. /**
  198. * @brief Set UART baud rate.
  199. *
  200. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  201. * @param baudrate UART baud rate.
  202. *
  203. * @return
  204. * - ESP_FAIL Parameter error
  205. * - ESP_OK Success
  206. */
  207. esp_err_t uart_set_baudrate(uart_port_t uart_num, uint32_t baudrate);
  208. /**
  209. * @brief Get UART baud rate.
  210. *
  211. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  212. * @param baudrate Pointer to accept value of UART baud rate
  213. *
  214. * @return
  215. * - ESP_FAIL Parameter error
  216. * - ESP_OK Success, result will be put in (*baudrate)
  217. *
  218. */
  219. esp_err_t uart_get_baudrate(uart_port_t uart_num, uint32_t* baudrate);
  220. /**
  221. * @brief Set UART line inverse mode
  222. *
  223. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  224. * @param inverse_mask Choose the wires that need to be inverted.
  225. * Inverse_mask should be chosen from
  226. UART_INVERSE_RXD / UART_INVERSE_TXD / UART_INVERSE_RTS / UART_INVERSE_CTS,
  227. combined with OR operation.
  228. *
  229. * @return
  230. * - ESP_OK Success
  231. * - ESP_FAIL Parameter error
  232. */
  233. esp_err_t uart_set_line_inverse(uart_port_t uart_num, uint32_t inverse_mask);
  234. /**
  235. * @brief Set hardware flow control.
  236. *
  237. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  238. * @param flow_ctrl Hardware flow control mode
  239. * @param rx_thresh Threshold of Hardware RX flow control (0 ~ UART_FIFO_LEN).
  240. * Only when UART_HW_FLOWCTRL_RTS is set, will the rx_thresh value be set.
  241. *
  242. * @return
  243. * - ESP_OK Success
  244. * - ESP_FAIL Parameter error
  245. */
  246. esp_err_t uart_set_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t flow_ctrl, uint8_t rx_thresh);
  247. /**
  248. * @brief Set software flow control.
  249. *
  250. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  251. * @param enable switch on or off
  252. * @param rx_thresh_xon low water mark
  253. * @param rx_thresh_xoff high water mark
  254. *
  255. * @return
  256. * - ESP_OK Success
  257. * - ESP_FAIL Parameter error
  258. */
  259. 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);
  260. /**
  261. * @brief Get hardware flow control mode
  262. *
  263. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  264. * @param flow_ctrl Option for different flow control mode.
  265. *
  266. * @return
  267. * - ESP_FAIL Parameter error
  268. * - ESP_OK Success, result will be put in (*flow_ctrl)
  269. */
  270. esp_err_t uart_get_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t* flow_ctrl);
  271. /**
  272. * @brief Clear UART interrupt status
  273. *
  274. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  275. * @param clr_mask Bit mask of the interrupt status to be cleared.
  276. * The bit mask should be composed from the fields of register UART_INT_CLR_REG.
  277. *
  278. * @return
  279. * - ESP_OK Success
  280. * - ESP_FAIL Parameter error
  281. */
  282. esp_err_t uart_clear_intr_status(uart_port_t uart_num, uint32_t clr_mask);
  283. /**
  284. * @brief Set UART interrupt enable
  285. *
  286. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  287. * @param enable_mask Bit mask of the enable bits.
  288. * The bit mask should be composed from the fields of register UART_INT_ENA_REG.
  289. *
  290. * @return
  291. * - ESP_OK Success
  292. * - ESP_FAIL Parameter error
  293. */
  294. esp_err_t uart_enable_intr_mask(uart_port_t uart_num, uint32_t enable_mask);
  295. /**
  296. * @brief Clear UART interrupt enable bits
  297. *
  298. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  299. * @param disable_mask Bit mask of the disable bits.
  300. * The bit mask should be composed from the fields of register UART_INT_ENA_REG.
  301. *
  302. * @return
  303. * - ESP_OK Success
  304. * - ESP_FAIL Parameter error
  305. */
  306. esp_err_t uart_disable_intr_mask(uart_port_t uart_num, uint32_t disable_mask);
  307. /**
  308. * @brief Enable UART RX interrupt (RX_FULL & RX_TIMEOUT INTERRUPT)
  309. *
  310. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  311. *
  312. * @return
  313. * - ESP_OK Success
  314. * - ESP_FAIL Parameter error
  315. */
  316. esp_err_t uart_enable_rx_intr(uart_port_t uart_num);
  317. /**
  318. * @brief Disable UART RX interrupt (RX_FULL & RX_TIMEOUT INTERRUPT)
  319. *
  320. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  321. *
  322. * @return
  323. * - ESP_OK Success
  324. * - ESP_FAIL Parameter error
  325. */
  326. esp_err_t uart_disable_rx_intr(uart_port_t uart_num);
  327. /**
  328. * @brief Disable UART TX interrupt (TX_FULL & TX_TIMEOUT INTERRUPT)
  329. *
  330. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  331. *
  332. * @return
  333. * - ESP_OK Success
  334. * - ESP_FAIL Parameter error
  335. */
  336. esp_err_t uart_disable_tx_intr(uart_port_t uart_num);
  337. /**
  338. * @brief Enable UART TX interrupt (TX_FULL & TX_TIMEOUT INTERRUPT)
  339. *
  340. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  341. * @param enable 1: enable; 0: disable
  342. * @param thresh Threshold of TX interrupt, 0 ~ UART_FIFO_LEN
  343. *
  344. * @return
  345. * - ESP_OK Success
  346. * - ESP_FAIL Parameter error
  347. */
  348. esp_err_t uart_enable_tx_intr(uart_port_t uart_num, int enable, int thresh);
  349. /**
  350. * @brief Register UART interrupt handler (ISR).
  351. *
  352. * @note UART ISR handler will be attached to the same CPU core that this function is running on.
  353. *
  354. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  355. * @param fn Interrupt handler function.
  356. * @param arg parameter for handler function
  357. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  358. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  359. * @param handle Pointer to return handle. If non-NULL, a handle for the interrupt will
  360. * be returned here.
  361. *
  362. * @return
  363. * - ESP_OK Success
  364. * - ESP_FAIL Parameter error
  365. */
  366. 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);
  367. /**
  368. * @brief Free UART interrupt handler registered by uart_isr_register. Must be called on the same core as
  369. * uart_isr_register was called.
  370. *
  371. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  372. *
  373. * @return
  374. * - ESP_OK Success
  375. * - ESP_FAIL Parameter error
  376. */
  377. esp_err_t uart_isr_free(uart_port_t uart_num);
  378. /**
  379. * @brief Set UART pin number
  380. *
  381. * @note Internal signal can be output to multiple GPIO pads.
  382. * Only one GPIO pad can connect with input signal.
  383. *
  384. * @note Instead of GPIO number a macro 'UART_PIN_NO_CHANGE' may be provided
  385. to keep the currently allocated pin.
  386. *
  387. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  388. * @param tx_io_num UART TX pin GPIO number.
  389. * @param rx_io_num UART RX pin GPIO number.
  390. * @param rts_io_num UART RTS pin GPIO number.
  391. * @param cts_io_num UART CTS pin GPIO number.
  392. *
  393. * @return
  394. * - ESP_OK Success
  395. * - ESP_FAIL Parameter error
  396. */
  397. 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);
  398. /**
  399. * @brief Manually set the UART RTS pin level.
  400. * @note UART must be configured with hardware flow control disabled.
  401. *
  402. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  403. * @param level 1: RTS output low (active); 0: RTS output high (block)
  404. *
  405. * @return
  406. * - ESP_OK Success
  407. * - ESP_FAIL Parameter error
  408. */
  409. esp_err_t uart_set_rts(uart_port_t uart_num, int level);
  410. /**
  411. * @brief Manually set the UART DTR pin level.
  412. *
  413. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  414. * @param level 1: DTR output low; 0: DTR output high
  415. *
  416. * @return
  417. * - ESP_OK Success
  418. * - ESP_FAIL Parameter error
  419. */
  420. esp_err_t uart_set_dtr(uart_port_t uart_num, int level);
  421. /**
  422. * @brief Set UART configuration parameters.
  423. *
  424. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  425. * @param uart_config UART parameter settings
  426. *
  427. * @return
  428. * - ESP_OK Success
  429. * - ESP_FAIL Parameter error
  430. */
  431. esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config);
  432. /**
  433. * @brief Configure UART interrupts.
  434. *
  435. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  436. * @param intr_conf UART interrupt settings
  437. *
  438. * @return
  439. * - ESP_OK Success
  440. * - ESP_FAIL Parameter error
  441. */
  442. esp_err_t uart_intr_config(uart_port_t uart_num, const uart_intr_config_t *intr_conf);
  443. /**
  444. * @brief Install UART driver.
  445. *
  446. * UART ISR handler will be attached to the same CPU core that this function is running on.
  447. *
  448. * @note Rx_buffer_size should be greater than UART_FIFO_LEN. Tx_buffer_size should be either zero or greater than UART_FIFO_LEN.
  449. *
  450. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  451. * @param rx_buffer_size UART RX ring buffer size.
  452. * @param tx_buffer_size UART TX ring buffer size.
  453. * If set to zero, driver will not use TX buffer, TX function will block task until all data have been sent out.
  454. * @param queue_size UART event queue size/depth.
  455. * @param uart_queue UART event queue handle (out param). On success, a new queue handle is written here to provide
  456. * access to UART events. If set to NULL, driver will not use an event queue.
  457. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  458. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info. Do not set ESP_INTR_FLAG_IRAM here
  459. * (the driver's ISR handler is not located in IRAM)
  460. *
  461. * @return
  462. * - ESP_OK Success
  463. * - ESP_FAIL Parameter error
  464. */
  465. 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);
  466. /**
  467. * @brief Uninstall UART driver.
  468. *
  469. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  470. *
  471. * @return
  472. * - ESP_OK Success
  473. * - ESP_FAIL Parameter error
  474. */
  475. esp_err_t uart_driver_delete(uart_port_t uart_num);
  476. /**
  477. * @brief Wait until UART TX FIFO is empty.
  478. *
  479. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  480. * @param ticks_to_wait Timeout, count in RTOS ticks
  481. *
  482. * @return
  483. * - ESP_OK Success
  484. * - ESP_FAIL Parameter error
  485. * - ESP_ERR_TIMEOUT Timeout
  486. */
  487. esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait);
  488. /**
  489. * @brief Send data to the UART port from a given buffer and length.
  490. *
  491. * 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.
  492. * @note This function should only be used when UART TX buffer is not enabled.
  493. *
  494. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  495. * @param buffer data buffer address
  496. * @param len data length to send
  497. *
  498. * @return
  499. * - (-1) Parameter error
  500. * - OTHERS (>=0) The number of bytes pushed to the TX FIFO
  501. */
  502. int uart_tx_chars(uart_port_t uart_num, const char* buffer, uint32_t len);
  503. /**
  504. * @brief Send data to the UART port from a given buffer and length,
  505. *
  506. * If the UART driver's parameter 'tx_buffer_size' is set to zero:
  507. * This function will not return until all the data have been sent out, or at least pushed into TX FIFO.
  508. *
  509. * Otherwise, if the 'tx_buffer_size' > 0, this function will return after copying all the data to tx ring buffer,
  510. * UART ISR will then move data from the ring buffer to TX FIFO gradually.
  511. *
  512. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  513. * @param src data buffer address
  514. * @param size data length to send
  515. *
  516. * @return
  517. * - (-1) Parameter error
  518. * - OTHERS (>=0) The number of bytes pushed to the TX FIFO
  519. */
  520. int uart_write_bytes(uart_port_t uart_num, const char* src, size_t size);
  521. /**
  522. * @brief Send data to the UART port from a given buffer and length.
  523. *
  524. * If the UART driver's parameter 'tx_buffer_size' is set to zero:
  525. * This function will not return until all the data and the break signal have been sent out.
  526. * After all data is sent out, send a break signal.
  527. *
  528. * Otherwise, if the 'tx_buffer_size' > 0, this function will return after copying all the data to tx ring buffer,
  529. * UART ISR will then move data from the ring buffer to TX FIFO gradually.
  530. * After all data sent out, send a break signal.
  531. *
  532. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  533. * @param src data buffer address
  534. * @param size data length to send
  535. * @param brk_len break signal length (unit: the time it takes to send a complete byte
  536. including start, stop and parity bits at current_baudrate)
  537. *
  538. * @return
  539. * - (-1) Parameter error
  540. * - OTHERS (>=0) The number of bytes pushed to the TX FIFO
  541. */
  542. int uart_write_bytes_with_break(uart_port_t uart_num, const char* src, size_t size, int brk_len);
  543. /**
  544. * @brief UART read bytes from UART buffer
  545. *
  546. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  547. * @param buf pointer to the buffer.
  548. * @param length data length
  549. * @param ticks_to_wait sTimeout, count in RTOS ticks
  550. *
  551. * @return
  552. * - (-1) Error
  553. * - OTHERS (>=0) The number of bytes read from UART FIFO
  554. */
  555. int uart_read_bytes(uart_port_t uart_num, uint8_t* buf, uint32_t length, TickType_t ticks_to_wait);
  556. /**
  557. * @brief UART ring buffer flush. This will discard all data in the UART RX buffer.
  558. *
  559. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  560. *
  561. * @return
  562. * - ESP_OK Success
  563. * - ESP_FAIL Parameter error
  564. */
  565. esp_err_t uart_flush(uart_port_t uart_num);
  566. /**
  567. * @brief UART get RX ring buffer cached data length
  568. *
  569. * @param uart_num UART port number.
  570. * @param size Pointer of size_t to accept cached data length
  571. *
  572. * @return
  573. * - ESP_OK Success
  574. * - ESP_FAIL Parameter error
  575. */
  576. esp_err_t uart_get_buffered_data_len(uart_port_t uart_num, size_t* size);
  577. /**
  578. * @brief UART disable pattern detect function.
  579. * Designed for applications like 'AT commands'.
  580. * When the hardware detects a series of one same character, the interrupt will be triggered.
  581. *
  582. * @param uart_num UART port number.
  583. *
  584. * @return
  585. * - ESP_OK Success
  586. * - ESP_FAIL Parameter error
  587. */
  588. esp_err_t uart_disable_pattern_det_intr(uart_port_t uart_num);
  589. /**
  590. * @brief UART enable pattern detect function.
  591. * Designed for applications like 'AT commands'.
  592. * When the hardware detect a series of one same character, the interrupt will be triggered.
  593. *
  594. * @param uart_num UART port number.
  595. * @param pattern_chr character of the pattern
  596. * @param chr_num number of the character, 8bit value.
  597. * @param chr_tout timeout of the interval between each pattern characters, 24bit value, unit is APB (80Mhz) clock cycle.
  598. * @param post_idle idle time after the last pattern character, 24bit value, unit is APB (80Mhz) clock cycle.
  599. * @param pre_idle idle time before the first pattern character, 24bit value, unit is APB (80Mhz) clock cycle.
  600. *
  601. * @return
  602. * - ESP_OK Success
  603. * - ESP_FAIL Parameter error
  604. */
  605. 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);
  606. #ifdef __cplusplus
  607. }
  608. #endif
  609. #endif /*_DRIVER_UART_H_*/