uart.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 mode selection
  43. */
  44. typedef enum {
  45. UART_MODE_UART = 0x00, /*!< mode: regular UART mode*/
  46. UART_MODE_RS485_HALF_DUPLEX = 0x01, /*!< mode: half duplex RS485 UART mode control by RTS pin */
  47. UART_MODE_IRDA = 0x02, /*!< mode: IRDA UART mode*/
  48. UART_MODE_RS485_COLLISION_DETECT = 0x03, /*!< mode: RS485 collision detection UART mode (used for test purposes)*/
  49. UART_MODE_RS485_APP_CTRL = 0x04, /*!< mode: application control RS485 UART mode (used for test purposes)*/
  50. } uart_mode_t;
  51. /**
  52. * @brief UART word length constants
  53. */
  54. typedef enum {
  55. UART_DATA_5_BITS = 0x0, /*!< word length: 5bits*/
  56. UART_DATA_6_BITS = 0x1, /*!< word length: 6bits*/
  57. UART_DATA_7_BITS = 0x2, /*!< word length: 7bits*/
  58. UART_DATA_8_BITS = 0x3, /*!< word length: 8bits*/
  59. UART_DATA_BITS_MAX = 0x4,
  60. } uart_word_length_t;
  61. /**
  62. * @brief UART stop bits number
  63. */
  64. typedef enum {
  65. UART_STOP_BITS_1 = 0x1, /*!< stop bit: 1bit*/
  66. UART_STOP_BITS_1_5 = 0x2, /*!< stop bit: 1.5bits*/
  67. UART_STOP_BITS_2 = 0x3, /*!< stop bit: 2bits*/
  68. UART_STOP_BITS_MAX = 0x4,
  69. } uart_stop_bits_t;
  70. /**
  71. * @brief UART peripheral number
  72. */
  73. typedef enum {
  74. UART_NUM_0 = 0x0, /*!< UART base address 0x3ff40000*/
  75. UART_NUM_1 = 0x1, /*!< UART base address 0x3ff50000*/
  76. UART_NUM_2 = 0x2, /*!< UART base address 0x3ff6e000*/
  77. UART_NUM_MAX,
  78. } uart_port_t;
  79. /**
  80. * @brief UART parity constants
  81. */
  82. typedef enum {
  83. UART_PARITY_DISABLE = 0x0, /*!< Disable UART parity*/
  84. UART_PARITY_EVEN = 0x2, /*!< Enable UART even parity*/
  85. UART_PARITY_ODD = 0x3 /*!< Enable UART odd parity*/
  86. } uart_parity_t;
  87. /**
  88. * @brief UART hardware flow control modes
  89. */
  90. typedef enum {
  91. UART_HW_FLOWCTRL_DISABLE = 0x0, /*!< disable hardware flow control*/
  92. UART_HW_FLOWCTRL_RTS = 0x1, /*!< enable RX hardware flow control (rts)*/
  93. UART_HW_FLOWCTRL_CTS = 0x2, /*!< enable TX hardware flow control (cts)*/
  94. UART_HW_FLOWCTRL_CTS_RTS = 0x3, /*!< enable hardware flow control*/
  95. UART_HW_FLOWCTRL_MAX = 0x4,
  96. } uart_hw_flowcontrol_t;
  97. /**
  98. * @brief UART configuration parameters for uart_param_config function
  99. */
  100. typedef struct {
  101. int baud_rate; /*!< UART baud rate*/
  102. uart_word_length_t data_bits; /*!< UART byte size*/
  103. uart_parity_t parity; /*!< UART parity mode*/
  104. uart_stop_bits_t stop_bits; /*!< UART stop bits*/
  105. uart_hw_flowcontrol_t flow_ctrl; /*!< UART HW flow control mode (cts/rts)*/
  106. uint8_t rx_flow_ctrl_thresh; /*!< UART HW RTS threshold*/
  107. bool use_ref_tick; /*!< Set to true if UART should be clocked from REF_TICK */
  108. } uart_config_t;
  109. /**
  110. * @brief UART interrupt configuration parameters for uart_intr_config function
  111. */
  112. typedef struct {
  113. 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*/
  114. uint8_t rx_timeout_thresh; /*!< UART timeout interrupt threshold (unit: time of sending one byte)*/
  115. uint8_t txfifo_empty_intr_thresh; /*!< UART TX empty interrupt threshold.*/
  116. uint8_t rxfifo_full_thresh; /*!< UART RX full interrupt threshold.*/
  117. } uart_intr_config_t;
  118. /**
  119. * @brief UART event types used in the ring buffer
  120. */
  121. typedef enum {
  122. UART_DATA, /*!< UART data event*/
  123. UART_BREAK, /*!< UART break event*/
  124. UART_BUFFER_FULL, /*!< UART RX buffer full event*/
  125. UART_FIFO_OVF, /*!< UART FIFO overflow event*/
  126. UART_FRAME_ERR, /*!< UART RX frame error event*/
  127. UART_PARITY_ERR, /*!< UART RX parity event*/
  128. UART_DATA_BREAK, /*!< UART TX data and break event*/
  129. UART_PATTERN_DET, /*!< UART pattern detected */
  130. UART_EVENT_MAX, /*!< UART event max index*/
  131. } uart_event_type_t;
  132. /**
  133. * @brief Event structure used in UART event queue
  134. */
  135. typedef struct {
  136. uart_event_type_t type; /*!< UART event type */
  137. size_t size; /*!< UART data size for UART_DATA event*/
  138. } uart_event_t;
  139. typedef intr_handle_t uart_isr_handle_t;
  140. /**
  141. * @brief Set UART data bits.
  142. *
  143. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  144. * @param data_bit UART data bits
  145. *
  146. * @return
  147. * - ESP_OK Success
  148. * - ESP_FAIL Parameter error
  149. */
  150. esp_err_t uart_set_word_length(uart_port_t uart_num, uart_word_length_t data_bit);
  151. /**
  152. * @brief Get UART data bits.
  153. *
  154. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  155. * @param data_bit Pointer to accept value of UART data bits.
  156. *
  157. * @return
  158. * - ESP_FAIL Parameter error
  159. * - ESP_OK Success, result will be put in (*data_bit)
  160. */
  161. esp_err_t uart_get_word_length(uart_port_t uart_num, uart_word_length_t* data_bit);
  162. /**
  163. * @brief Set UART stop bits.
  164. *
  165. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  166. * @param stop_bits UART stop bits
  167. *
  168. * @return
  169. * - ESP_OK Success
  170. * - ESP_FAIL Fail
  171. */
  172. esp_err_t uart_set_stop_bits(uart_port_t uart_num, uart_stop_bits_t stop_bits);
  173. /**
  174. * @brief Get UART stop bits.
  175. *
  176. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  177. * @param stop_bits Pointer to accept value of UART stop bits.
  178. *
  179. * @return
  180. * - ESP_FAIL Parameter error
  181. * - ESP_OK Success, result will be put in (*stop_bit)
  182. */
  183. esp_err_t uart_get_stop_bits(uart_port_t uart_num, uart_stop_bits_t* stop_bits);
  184. /**
  185. * @brief Set UART parity mode.
  186. *
  187. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  188. * @param parity_mode the enum of uart parity configuration
  189. *
  190. * @return
  191. * - ESP_FAIL Parameter error
  192. * - ESP_OK Success
  193. */
  194. esp_err_t uart_set_parity(uart_port_t uart_num, uart_parity_t parity_mode);
  195. /**
  196. * @brief Get UART parity mode.
  197. *
  198. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  199. * @param parity_mode Pointer to accept value of UART parity mode.
  200. *
  201. * @return
  202. * - ESP_FAIL Parameter error
  203. * - ESP_OK Success, result will be put in (*parity_mode)
  204. *
  205. */
  206. esp_err_t uart_get_parity(uart_port_t uart_num, uart_parity_t* parity_mode);
  207. /**
  208. * @brief Set UART baud rate.
  209. *
  210. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  211. * @param baudrate UART baud rate.
  212. *
  213. * @return
  214. * - ESP_FAIL Parameter error
  215. * - ESP_OK Success
  216. */
  217. esp_err_t uart_set_baudrate(uart_port_t uart_num, uint32_t baudrate);
  218. /**
  219. * @brief Get UART baud rate.
  220. *
  221. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  222. * @param baudrate Pointer to accept value of UART baud rate
  223. *
  224. * @return
  225. * - ESP_FAIL Parameter error
  226. * - ESP_OK Success, result will be put in (*baudrate)
  227. *
  228. */
  229. esp_err_t uart_get_baudrate(uart_port_t uart_num, uint32_t* baudrate);
  230. /**
  231. * @brief Set UART line inverse mode
  232. *
  233. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  234. * @param inverse_mask Choose the wires that need to be inverted.
  235. * Inverse_mask should be chosen from
  236. * UART_INVERSE_RXD / UART_INVERSE_TXD / UART_INVERSE_RTS / UART_INVERSE_CTS,
  237. * combined with OR operation.
  238. *
  239. * @return
  240. * - ESP_OK Success
  241. * - ESP_FAIL Parameter error
  242. */
  243. esp_err_t uart_set_line_inverse(uart_port_t uart_num, uint32_t inverse_mask);
  244. /**
  245. * @brief Set hardware flow control.
  246. *
  247. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  248. * @param flow_ctrl Hardware flow control mode
  249. * @param rx_thresh Threshold of Hardware RX flow control (0 ~ UART_FIFO_LEN).
  250. * Only when UART_HW_FLOWCTRL_RTS is set, will the rx_thresh value be set.
  251. *
  252. * @return
  253. * - ESP_OK Success
  254. * - ESP_FAIL Parameter error
  255. */
  256. esp_err_t uart_set_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t flow_ctrl, uint8_t rx_thresh);
  257. /**
  258. * @brief Set software flow control.
  259. *
  260. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  261. * @param enable switch on or off
  262. * @param rx_thresh_xon low water mark
  263. * @param rx_thresh_xoff high water mark
  264. *
  265. * @return
  266. * - ESP_OK Success
  267. * - ESP_FAIL Parameter error
  268. */
  269. 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);
  270. /**
  271. * @brief Get hardware flow control mode
  272. *
  273. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  274. * @param flow_ctrl Option for different flow control mode.
  275. *
  276. * @return
  277. * - ESP_FAIL Parameter error
  278. * - ESP_OK Success, result will be put in (*flow_ctrl)
  279. */
  280. esp_err_t uart_get_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t* flow_ctrl);
  281. /**
  282. * @brief Clear UART interrupt status
  283. *
  284. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  285. * @param clr_mask Bit mask of the interrupt status to be cleared.
  286. * The bit mask should be composed from the fields of register UART_INT_CLR_REG.
  287. *
  288. * @return
  289. * - ESP_OK Success
  290. * - ESP_FAIL Parameter error
  291. */
  292. esp_err_t uart_clear_intr_status(uart_port_t uart_num, uint32_t clr_mask);
  293. /**
  294. * @brief Set UART interrupt enable
  295. *
  296. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  297. * @param enable_mask Bit mask of the enable bits.
  298. * The bit mask should be composed from the fields of register UART_INT_ENA_REG.
  299. *
  300. * @return
  301. * - ESP_OK Success
  302. * - ESP_FAIL Parameter error
  303. */
  304. esp_err_t uart_enable_intr_mask(uart_port_t uart_num, uint32_t enable_mask);
  305. /**
  306. * @brief Clear UART interrupt enable bits
  307. *
  308. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  309. * @param disable_mask Bit mask of the disable bits.
  310. * The bit mask should be composed from the fields of register UART_INT_ENA_REG.
  311. *
  312. * @return
  313. * - ESP_OK Success
  314. * - ESP_FAIL Parameter error
  315. */
  316. esp_err_t uart_disable_intr_mask(uart_port_t uart_num, uint32_t disable_mask);
  317. /**
  318. * @brief Enable 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_enable_rx_intr(uart_port_t uart_num);
  327. /**
  328. * @brief Disable UART RX interrupt (RX_FULL & RX_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_rx_intr(uart_port_t uart_num);
  337. /**
  338. * @brief Disable UART TX interrupt (TX_FULL & TX_TIMEOUT INTERRUPT)
  339. *
  340. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  341. *
  342. * @return
  343. * - ESP_OK Success
  344. * - ESP_FAIL Parameter error
  345. */
  346. esp_err_t uart_disable_tx_intr(uart_port_t uart_num);
  347. /**
  348. * @brief Enable UART TX interrupt (TX_FULL & TX_TIMEOUT INTERRUPT)
  349. *
  350. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  351. * @param enable 1: enable; 0: disable
  352. * @param thresh Threshold of TX interrupt, 0 ~ UART_FIFO_LEN
  353. *
  354. * @return
  355. * - ESP_OK Success
  356. * - ESP_FAIL Parameter error
  357. */
  358. esp_err_t uart_enable_tx_intr(uart_port_t uart_num, int enable, int thresh);
  359. /**
  360. * @brief Register UART interrupt handler (ISR).
  361. *
  362. * @note UART ISR handler will be attached to the same CPU core that this function is running on.
  363. *
  364. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  365. * @param fn Interrupt handler function.
  366. * @param arg parameter for handler function
  367. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  368. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  369. * @param handle Pointer to return handle. If non-NULL, a handle for the interrupt will
  370. * be returned here.
  371. *
  372. * @return
  373. * - ESP_OK Success
  374. * - ESP_FAIL Parameter error
  375. */
  376. 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);
  377. /**
  378. * @brief Free UART interrupt handler registered by uart_isr_register. Must be called on the same core as
  379. * uart_isr_register was called.
  380. *
  381. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  382. *
  383. * @return
  384. * - ESP_OK Success
  385. * - ESP_FAIL Parameter error
  386. */
  387. esp_err_t uart_isr_free(uart_port_t uart_num);
  388. /**
  389. * @brief Set UART pin number
  390. *
  391. * @note Internal signal can be output to multiple GPIO pads.
  392. * Only one GPIO pad can connect with input signal.
  393. *
  394. * @note Instead of GPIO number a macro 'UART_PIN_NO_CHANGE' may be provided
  395. to keep the currently allocated pin.
  396. *
  397. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  398. * @param tx_io_num UART TX pin GPIO number.
  399. * @param rx_io_num UART RX pin GPIO number.
  400. * @param rts_io_num UART RTS pin GPIO number.
  401. * @param cts_io_num UART CTS pin GPIO number.
  402. *
  403. * @return
  404. * - ESP_OK Success
  405. * - ESP_FAIL Parameter error
  406. */
  407. 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);
  408. /**
  409. * @brief Manually set the UART RTS pin level.
  410. * @note UART must be configured with hardware flow control disabled.
  411. *
  412. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  413. * @param level 1: RTS output low (active); 0: RTS output high (block)
  414. *
  415. * @return
  416. * - ESP_OK Success
  417. * - ESP_FAIL Parameter error
  418. */
  419. esp_err_t uart_set_rts(uart_port_t uart_num, int level);
  420. /**
  421. * @brief Manually set the UART DTR pin level.
  422. *
  423. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  424. * @param level 1: DTR output low; 0: DTR output high
  425. *
  426. * @return
  427. * - ESP_OK Success
  428. * - ESP_FAIL Parameter error
  429. */
  430. esp_err_t uart_set_dtr(uart_port_t uart_num, int level);
  431. /**
  432. * @brief Set UART idle interval after tx FIFO is empty
  433. *
  434. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  435. * @param idle_num idle interval after tx FIFO is empty(unit: the time it takes to send one bit
  436. * under current baudrate)
  437. *
  438. * @return
  439. * - ESP_OK Success
  440. * - ESP_FAIL Parameter error
  441. */
  442. esp_err_t uart_set_tx_idle_num(uart_port_t uart_num, uint16_t idle_num);
  443. /**
  444. * @brief Set UART configuration parameters.
  445. *
  446. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  447. * @param uart_config UART parameter settings
  448. *
  449. * @return
  450. * - ESP_OK Success
  451. * - ESP_FAIL Parameter error
  452. */
  453. esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config);
  454. /**
  455. * @brief Configure UART interrupts.
  456. *
  457. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  458. * @param intr_conf UART interrupt settings
  459. *
  460. * @return
  461. * - ESP_OK Success
  462. * - ESP_FAIL Parameter error
  463. */
  464. esp_err_t uart_intr_config(uart_port_t uart_num, const uart_intr_config_t *intr_conf);
  465. /**
  466. * @brief Install UART driver.
  467. *
  468. * UART ISR handler will be attached to the same CPU core that this function is running on.
  469. *
  470. * @note Rx_buffer_size should be greater than UART_FIFO_LEN. Tx_buffer_size should be either zero or greater than UART_FIFO_LEN.
  471. *
  472. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  473. * @param rx_buffer_size UART RX ring buffer size.
  474. * @param tx_buffer_size UART TX ring buffer size.
  475. * If set to zero, driver will not use TX buffer, TX function will block task until all data have been sent out.
  476. * @param queue_size UART event queue size/depth.
  477. * @param uart_queue UART event queue handle (out param). On success, a new queue handle is written here to provide
  478. * access to UART events. If set to NULL, driver will not use an event queue.
  479. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  480. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info. Do not set ESP_INTR_FLAG_IRAM here
  481. * (the driver's ISR handler is not located in IRAM)
  482. *
  483. * @return
  484. * - ESP_OK Success
  485. * - ESP_FAIL Parameter error
  486. */
  487. 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);
  488. /**
  489. * @brief Uninstall UART driver.
  490. *
  491. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  492. *
  493. * @return
  494. * - ESP_OK Success
  495. * - ESP_FAIL Parameter error
  496. */
  497. esp_err_t uart_driver_delete(uart_port_t uart_num);
  498. /**
  499. * @brief Wait until UART TX FIFO is empty.
  500. *
  501. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  502. * @param ticks_to_wait Timeout, count in RTOS ticks
  503. *
  504. * @return
  505. * - ESP_OK Success
  506. * - ESP_FAIL Parameter error
  507. * - ESP_ERR_TIMEOUT Timeout
  508. */
  509. esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait);
  510. /**
  511. * @brief Send data to the UART port from a given buffer and length.
  512. *
  513. * 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.
  514. * @note This function should only be used when UART TX buffer is not enabled.
  515. *
  516. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  517. * @param buffer data buffer address
  518. * @param len data length to send
  519. *
  520. * @return
  521. * - (-1) Parameter error
  522. * - OTHERS (>=0) The number of bytes pushed to the TX FIFO
  523. */
  524. int uart_tx_chars(uart_port_t uart_num, const char* buffer, uint32_t len);
  525. /**
  526. * @brief Send data to the UART port from a given buffer and length,
  527. *
  528. * If the UART driver's parameter 'tx_buffer_size' is set to zero:
  529. * This function will not return until all the data have been sent out, or at least pushed into TX FIFO.
  530. *
  531. * Otherwise, if the 'tx_buffer_size' > 0, this function will return after copying all the data to tx ring buffer,
  532. * UART ISR will then move data from the ring buffer to TX FIFO gradually.
  533. *
  534. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  535. * @param src data buffer address
  536. * @param size data length to send
  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(uart_port_t uart_num, const char* src, size_t size);
  543. /**
  544. * @brief Send data to the UART port from a given buffer and length,
  545. *
  546. * If the UART driver's parameter 'tx_buffer_size' is set to zero:
  547. * This function will not return until all the data and the break signal have been sent out.
  548. * After all data is sent out, send a break signal.
  549. *
  550. * Otherwise, if the 'tx_buffer_size' > 0, this function will return after copying all the data to tx ring buffer,
  551. * UART ISR will then move data from the ring buffer to TX FIFO gradually.
  552. * After all data sent out, send a break signal.
  553. *
  554. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  555. * @param src data buffer address
  556. * @param size data length to send
  557. * @param brk_len break signal duration(unit: the time it takes to send one bit at current baudrate)
  558. *
  559. * @return
  560. * - (-1) Parameter error
  561. * - OTHERS (>=0) The number of bytes pushed to the TX FIFO
  562. */
  563. int uart_write_bytes_with_break(uart_port_t uart_num, const char* src, size_t size, int brk_len);
  564. /**
  565. * @brief UART read bytes from UART buffer
  566. *
  567. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  568. * @param buf pointer to the buffer.
  569. * @param length data length
  570. * @param ticks_to_wait sTimeout, count in RTOS ticks
  571. *
  572. * @return
  573. * - (-1) Error
  574. * - OTHERS (>=0) The number of bytes read from UART FIFO
  575. */
  576. int uart_read_bytes(uart_port_t uart_num, uint8_t* buf, uint32_t length, TickType_t ticks_to_wait);
  577. /**
  578. * @brief Alias of uart_flush_input.
  579. * UART ring buffer flush. This will discard all data in the UART RX buffer.
  580. * @note Instead of waiting the data sent out, this function will clear UART rx buffer.
  581. * In order to send all the data in tx FIFO, we can use uart_wait_tx_done function.
  582. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  583. *
  584. * @return
  585. * - ESP_OK Success
  586. * - ESP_FAIL Parameter error
  587. */
  588. esp_err_t uart_flush(uart_port_t uart_num);
  589. /**
  590. * @brief Clear input buffer, discard all the data is in the ring-buffer.
  591. * @note In order to send all the data in tx FIFO, we can use uart_wait_tx_done function.
  592. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  593. *
  594. * @return
  595. * - ESP_OK Success
  596. * - ESP_FAIL Parameter error
  597. */
  598. esp_err_t uart_flush_input(uart_port_t uart_num);
  599. /**
  600. * @brief UART get RX ring buffer cached data length
  601. *
  602. * @param uart_num UART port number.
  603. * @param size Pointer of size_t to accept cached data length
  604. *
  605. * @return
  606. * - ESP_OK Success
  607. * - ESP_FAIL Parameter error
  608. */
  609. esp_err_t uart_get_buffered_data_len(uart_port_t uart_num, size_t* size);
  610. /**
  611. * @brief UART disable pattern detect function.
  612. * Designed for applications like 'AT commands'.
  613. * When the hardware detects a series of one same character, the interrupt will be triggered.
  614. *
  615. * @param uart_num UART port number.
  616. *
  617. * @return
  618. * - ESP_OK Success
  619. * - ESP_FAIL Parameter error
  620. */
  621. esp_err_t uart_disable_pattern_det_intr(uart_port_t uart_num);
  622. /**
  623. * @brief UART enable pattern detect function.
  624. * Designed for applications like 'AT commands'.
  625. * When the hardware detect a series of one same character, the interrupt will be triggered.
  626. *
  627. * @param uart_num UART port number.
  628. * @param pattern_chr character of the pattern
  629. * @param chr_num number of the character, 8bit value.
  630. * @param chr_tout timeout of the interval between each pattern characters, 24bit value, unit is APB (80Mhz) clock cycle.
  631. * When the duration is less than this value, it will not take this data as at_cmd char
  632. * @param post_idle idle time after the last pattern character, 24bit value, unit is APB (80Mhz) clock cycle.
  633. * When the duration is less than this value, it will not take the previous data as the last at_cmd char
  634. * @param pre_idle idle time before the first pattern character, 24bit value, unit is APB (80Mhz) clock cycle.
  635. * When the duration is less than this value, it will not take this data as the first at_cmd char
  636. *
  637. * @return
  638. * - ESP_OK Success
  639. * - ESP_FAIL Parameter error
  640. */
  641. 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);
  642. /**
  643. * @brief Return the nearest detected pattern position in buffer.
  644. * The positions of the detected pattern are saved in a queue,
  645. * this function will dequeue the first pattern position and move the pointer to next pattern position.
  646. * @note If the RX buffer is full and flow control is not enabled,
  647. * the detected pattern may not be found in the rx buffer due to overflow.
  648. *
  649. * The following APIs will modify the pattern position info:
  650. * uart_flush_input, uart_read_bytes, uart_driver_delete, uart_pop_pattern_pos
  651. * It is the application's responsibility to ensure atomic access to the pattern queue and the rx data buffer
  652. * when using pattern detect feature.
  653. *
  654. * @param uart_num UART port number
  655. * @return
  656. * - (-1) No pattern found for current index or parameter error
  657. * - others the pattern position in rx buffer.
  658. */
  659. int uart_pattern_pop_pos(uart_port_t uart_num);
  660. /**
  661. * @brief Return the nearest detected pattern position in buffer.
  662. * The positions of the detected pattern are saved in a queue,
  663. * This function do nothing to the queue.
  664. * @note If the RX buffer is full and flow control is not enabled,
  665. * the detected pattern may not be found in the rx buffer due to overflow.
  666. *
  667. * The following APIs will modify the pattern position info:
  668. * uart_flush_input, uart_read_bytes, uart_driver_delete, uart_pop_pattern_pos
  669. * It is the application's responsibility to ensure atomic access to the pattern queue and the rx data buffer
  670. * when using pattern detect feature.
  671. *
  672. * @param uart_num UART port number
  673. * @return
  674. * - (-1) No pattern found for current index or parameter error
  675. * - others the pattern position in rx buffer.
  676. */
  677. int uart_pattern_get_pos(uart_port_t uart_num);
  678. /**
  679. * @brief Allocate a new memory with the given length to save record the detected pattern position in rx buffer.
  680. * @param uart_num UART port number
  681. * @param queue_length Max queue length for the detected pattern.
  682. * If the queue length is not large enough, some pattern positions might be lost.
  683. * Set this value to the maximum number of patterns that could be saved in data buffer at the same time.
  684. * @return
  685. * - ESP_ERR_NO_MEM No enough memory
  686. * - ESP_ERR_INVALID_STATE Driver not installed
  687. * - ESP_FAIL Parameter error
  688. * - ESP_OK Success
  689. */
  690. esp_err_t uart_pattern_queue_reset(uart_port_t uart_num, int queue_length);
  691. /**
  692. * @brief UART set communication mode
  693. * @note This function must be executed after uart_driver_install(), when the driver object is initialized.
  694. * @param uart_num Uart number to configure
  695. * @param mode UART UART mode to set
  696. *
  697. * @return
  698. * - ESP_OK Success
  699. * - ESP_ERR_INVALID_ARG Parameter error
  700. */
  701. esp_err_t uart_set_mode(uart_port_t uart_num, uart_mode_t mode);
  702. /**
  703. * @brief UART set threshold timeout for TOUT feature
  704. *
  705. * @param uart_num Uart number to configure
  706. * @param tout_thresh This parameter defines timeout threshold in uart symbol periods. The maximum value of threshold is 126.
  707. * tout_thresh = 1, defines TOUT interrupt timeout equal to transmission time of one symbol (~11 bit) on current baudrate.
  708. * If the time is expired the UART_RXFIFO_TOUT_INT interrupt is triggered. If tout_thresh == 0,
  709. * the TOUT feature is disabled.
  710. *
  711. * @return
  712. * - ESP_OK Success
  713. * - ESP_ERR_INVALID_ARG Parameter error
  714. * - ESP_ERR_INVALID_STATE Driver is not installed
  715. */
  716. esp_err_t uart_set_rx_timeout(uart_port_t uart_num, const uint8_t tout_thresh);
  717. /**
  718. * @brief Returns collision detection flag for RS485 mode
  719. * Function returns the collision detection flag into variable pointed by collision_flag.
  720. * *collision_flag = true, if collision detected else it is equal to false.
  721. * This function should be executed when actual transmission is completed (after uart_write_bytes()).
  722. *
  723. * @param uart_num Uart number to configure
  724. * @param collision_flag Pointer to variable of type bool to return collision flag.
  725. *
  726. * @return
  727. * - ESP_OK Success
  728. * - ESP_ERR_INVALID_ARG Parameter error
  729. */
  730. esp_err_t uart_get_collision_flag(uart_port_t uart_num, bool* collision_flag);
  731. #ifdef __cplusplus
  732. }
  733. #endif
  734. #endif /*_DRIVER_UART_H_*/