uart.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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 "driver/periph_ctrl.h"
  22. #include "freertos/FreeRTOS.h"
  23. #include "freertos/semphr.h"
  24. #include "freertos/xtensa_api.h"
  25. #include "freertos/task.h"
  26. #include "freertos/queue.h"
  27. #include "freertos/ringbuf.h"
  28. #include <esp_types.h>
  29. #define UART_FIFO_LEN (128) /*!< Length of the hardware FIFO buffers */
  30. #define UART_INTR_MASK 0x1ff /*!< mask of all UART interrupts */
  31. #define UART_LINE_INV_MASK (0x3f << 19) /*!< TBD */
  32. #define UART_BITRATE_MAX 5000000 /*!< Max bit rate supported by UART */
  33. #define UART_PIN_NO_CHANGE (-1) /*!< Constant for uart_set_pin function which indicates that UART pin should not be changed */
  34. #define UART_INVERSE_DISABLE (0x0) /*!< Disable UART signal inverse*/
  35. #define UART_INVERSE_RXD (UART_RXD_INV_M) /*!< UART RXD input inverse*/
  36. #define UART_INVERSE_CTS (UART_CTS_INV_M) /*!< UART CTS input inverse*/
  37. #define UART_INVERSE_TXD (UART_TXD_INV_M) /*!< UART TXD output inverse*/
  38. #define UART_INVERSE_RTS (UART_RTS_INV_M) /*!< UART RTS output inverse*/
  39. /**
  40. * @brief UART word length constants
  41. */
  42. typedef enum {
  43. UART_DATA_5_BITS = 0x0, /*!< word length: 5bits*/
  44. UART_DATA_6_BITS = 0x1, /*!< word length: 6bits*/
  45. UART_DATA_7_BITS = 0x2, /*!< word length: 7bits*/
  46. UART_DATA_8_BITS = 0x3, /*!< word length: 8bits*/
  47. UART_DATA_BITS_MAX = 0X4,
  48. } uart_word_length_t;
  49. /**
  50. * @brief UART stop bits number
  51. */
  52. typedef enum {
  53. UART_STOP_BITS_1 = 0x1, /*!< stop bit: 1bit*/
  54. UART_STOP_BITS_1_5 = 0x2, /*!< stop bit: 1.5bits*/
  55. UART_STOP_BITS_2 = 0x3, /*!< stop bit: 2bits*/
  56. UART_STOP_BITS_MAX = 0x4,
  57. } uart_stop_bits_t;
  58. /**
  59. * @brief UART peripheral number
  60. */
  61. typedef enum {
  62. UART_NUM_0 = 0x0, /*!< UART base address 0x3ff40000*/
  63. UART_NUM_1 = 0x1, /*!< UART base address 0x3ff50000*/
  64. UART_NUM_2 = 0x2, /*!< UART base address 0x3ff6E000*/
  65. UART_NUM_MAX,
  66. } uart_port_t;
  67. /**
  68. * @brief UART parity constants
  69. */
  70. typedef enum {
  71. UART_PARITY_DISABLE = 0x0, /*!< Disable UART parity*/
  72. UART_PARITY_EVEN = 0x2, /*!< Enable UART even parity*/
  73. UART_PARITY_ODD = 0x3 /*!< Enable UART odd parity*/
  74. } uart_parity_t;
  75. /**
  76. * @brief UART hardware flow control modes
  77. */
  78. typedef enum {
  79. UART_HW_FLOWCTRL_DISABLE = 0x0, /*!< disable hardware flow control*/
  80. UART_HW_FLOWCTRL_RTS = 0x1, /*!< enable RX hardware flow control (rts)*/
  81. UART_HW_FLOWCTRL_CTS = 0x2, /*!< enable TX hardware flow control (cts)*/
  82. UART_HW_FLOWCTRL_CTS_RTS = 0x3, /*!< enable hardware flow control*/
  83. UART_HW_FLOWCTRL_MAX = 0x4,
  84. } uart_hw_flowcontrol_t;
  85. /**
  86. * @brief UART configuration parameters for uart_param_config function
  87. */
  88. typedef struct {
  89. int baud_rate; /*!< UART baudrate*/
  90. uart_word_length_t data_bits; /*!< UART byte size*/
  91. uart_parity_t parity; /*!< UART parity mode*/
  92. uart_stop_bits_t stop_bits; /*!< UART stop bits*/
  93. uart_hw_flowcontrol_t flow_ctrl; /*!< UART HW flow control mode(cts/rts)*/
  94. uint8_t rx_flow_ctrl_thresh ; /*!< UART HW RTS threshold*/
  95. } uart_config_t;
  96. /**
  97. * @brief UART interrupt configuration parameters for uart_intr_config function
  98. */
  99. typedef struct {
  100. 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*/
  101. uint8_t rx_timeout_thresh; /*!< UART timeout interrupt threshold(unit: time of sending one byte)*/
  102. uint8_t txfifo_empty_intr_thresh; /*!< UART TX empty interrupt threshold.*/
  103. uint8_t rxfifo_full_thresh; /*!< UART RX full interrupt threshold.*/
  104. } uart_intr_config_t;
  105. /**
  106. * @brief UART event types used in the ringbuffer
  107. */
  108. typedef enum {
  109. UART_DATA, /*!< UART data event*/
  110. UART_BREAK, /*!< UART break event*/
  111. UART_BUFFER_FULL, /*!< UART RX buffer full event*/
  112. UART_FIFO_OVF, /*!< UART FIFO overflow event*/
  113. UART_FRAME_ERR, /*!< UART RX frame error event*/
  114. UART_PARITY_ERR, /*!< UART RX parity event*/
  115. UART_DATA_BREAK, /*!< UART TX data and break event*/
  116. UART_EVENT_MAX, /*!< UART event max index*/
  117. } uart_event_type_t;
  118. /**
  119. * @brief Event structure used in UART event queue
  120. */
  121. typedef struct {
  122. uart_event_type_t type; /*!< UART event type */
  123. size_t size; /*!< UART data size for UART_DATA event*/
  124. } uart_event_t;
  125. /**
  126. * @brief Set UART data bits.
  127. *
  128. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  129. * @param data_bit UART data bits
  130. *
  131. * @return
  132. * - ESP_OK Success
  133. * - ESP_FAIL Parameter error
  134. */
  135. esp_err_t uart_set_word_length(uart_port_t uart_num, uart_word_length_t data_bit);
  136. /**
  137. * @brief Get UART data bits.
  138. *
  139. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  140. * @param data_bit Pointer to accept value of UART data bits.
  141. *
  142. * @return
  143. * - ESP_FAIL Parameter error
  144. * - ESP_OK Success, result will be put in (*data_bit)
  145. */
  146. esp_err_t uart_get_word_length(uart_port_t uart_num, uart_word_length_t* data_bit);
  147. /**
  148. * @brief Set UART stop bits.
  149. *
  150. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  151. * @param bit_num UART stop bits
  152. *
  153. * @return
  154. * - ESP_OK Success
  155. * - ESP_FAIL Fail
  156. */
  157. esp_err_t uart_set_stop_bits(uart_port_t uart_num, uart_stop_bits_t bit_num);
  158. /**
  159. * @brief Set UART stop bits.
  160. *
  161. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  162. * @param stop_bit Pointer to accept value of UART stop bits.
  163. *
  164. * @return
  165. * - ESP_FAIL Parameter error
  166. * - ESP_OK Success, result will be put in (*stop_bit)
  167. */
  168. esp_err_t uart_get_stop_bits(uart_port_t uart_num, uart_stop_bits_t* stop_bit);
  169. /**
  170. * @brief Set UART parity.
  171. *
  172. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  173. * @param parity_mode the enum of uart parity configuration
  174. *
  175. * @return
  176. * - ESP_FAIL Parameter error
  177. * - ESP_OK Success
  178. */
  179. esp_err_t uart_set_parity(uart_port_t uart_num, uart_parity_t parity_mode);
  180. /**
  181. * @brief Get UART parity mode.
  182. *
  183. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  184. * @param parity_mode Pointer to accept value of UART parity mode.
  185. *
  186. * @return
  187. * - ESP_FAIL Parameter error
  188. * - ESP_OK Success, result will be put in (*parity_mode)
  189. *
  190. */
  191. esp_err_t uart_get_parity(uart_port_t uart_num, uart_parity_t* parity_mode);
  192. /**
  193. * @brief Set UART baud rate.
  194. *
  195. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  196. * @param baud_rate UART baud-rate.
  197. *
  198. * @return
  199. * - ESP_FAIL Parameter error
  200. * - ESP_OK Success
  201. */
  202. esp_err_t uart_set_baudrate(uart_port_t uart_num, uint32_t baud_rate);
  203. /**
  204. * @brief Get UART bit-rate.
  205. *
  206. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  207. * @param baudrate Pointer to accept value of UART baud rate
  208. *
  209. * @return
  210. * - ESP_FAIL Parameter error
  211. * - ESP_OK Success, result will be put in (*baudrate)
  212. *
  213. */
  214. esp_err_t uart_get_baudrate(uart_port_t uart_num, uint32_t* baudrate);
  215. /**
  216. * @brief Set UART line inverse mode
  217. *
  218. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  219. * @param inverse_mask Choose the wires that need to be inversed.
  220. * Inverse_mask should be chosen from UART_INVERSE_RXD/UART_INVERSE_TXD/UART_INVERSE_RTS/UART_INVERSE_CTS, combine with OR operation.
  221. *
  222. * @return
  223. * - ESP_OK Success
  224. * - ESP_FAIL Parameter error
  225. */
  226. esp_err_t uart_set_line_inverse(uart_port_t uart_num, uint32_t inverse_mask);
  227. /**
  228. * @brief Set hardware flow control.
  229. *
  230. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  231. * @param flow_ctrl Hardware flow control mode
  232. * @param rx_thresh Threshold of Hardware RX flow control(0 ~ UART_FIFO_LEN).
  233. * Only when UART_HW_FLOWCTRL_RTS is set, will the rx_thresh value be set.
  234. *
  235. * @return
  236. * - ESP_OK Success
  237. * - ESP_FAIL Parameter error
  238. */
  239. esp_err_t uart_set_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t flow_ctrl, uint8_t rx_thresh);
  240. /**
  241. * @brief Get hardware flow control mode
  242. *
  243. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  244. * @param flow_ctrl Option for different flow control mode.
  245. *
  246. * @return
  247. * - ESP_FAIL Parameter error
  248. * - ESP_OK Success, result will be put in (*flow_ctrl)
  249. */
  250. esp_err_t uart_get_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t* flow_ctrl);
  251. /**
  252. * @brief Clear UART interrupt status
  253. *
  254. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  255. * @param clr_mask Bit mask of the status that to be cleared.
  256. * enable_mask should be chosen from the fields of register UART_INT_CLR_REG.
  257. *
  258. * @return
  259. * - ESP_OK Success
  260. * - ESP_FAIL Parameter error
  261. */
  262. esp_err_t uart_clear_intr_status(uart_port_t uart_num, uint32_t clr_mask);
  263. /**
  264. * @brief Set UART interrupt enable
  265. *
  266. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  267. * @param enable_mask Bit mask of the enable bits.
  268. * enable_mask should be chosen from the fields of register UART_INT_ENA_REG.
  269. *
  270. * @return
  271. * - ESP_OK Success
  272. * - ESP_FAIL Parameter error
  273. */
  274. esp_err_t uart_enable_intr_mask(uart_port_t uart_num, uint32_t enable_mask);
  275. /**
  276. * @brief Clear UART interrupt enable bits
  277. *
  278. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  279. * @param disable_mask Bit mask of the disable bits.
  280. * disable_mask should be chosen from the fields of register UART_INT_ENA_REG.
  281. *
  282. * @return
  283. * - ESP_OK Success
  284. * - ESP_FAIL Parameter error
  285. */
  286. esp_err_t uart_disable_intr_mask(uart_port_t uart_num, uint32_t disable_mask);
  287. /**
  288. * @brief Enable UART RX interrupt(RX_FULL & RX_TIMEOUT INTERRUPT)
  289. *
  290. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  291. *
  292. * @return
  293. * - ESP_OK Success
  294. * - ESP_FAIL Parameter error
  295. */
  296. esp_err_t uart_enable_rx_intr(uart_port_t uart_num);
  297. /**
  298. * @brief Disable UART RX interrupt(RX_FULL & RX_TIMEOUT INTERRUPT)
  299. *
  300. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  301. *
  302. * @return
  303. * - ESP_OK Success
  304. * - ESP_FAIL Parameter error
  305. */
  306. esp_err_t uart_disable_rx_intr(uart_port_t uart_num);
  307. /**
  308. * @brief Disable UART TX 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_disable_tx_intr(uart_port_t uart_num);
  317. /**
  318. * @brief Enable UART TX interrupt(RX_FULL & RX_TIMEOUT INTERRUPT)
  319. *
  320. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  321. * @param enable 1: enable; 0: disable
  322. * @param thresh Threshold of TX interrupt, 0 ~ UART_FIFO_LEN
  323. *
  324. * @return
  325. * - ESP_OK Success
  326. * - ESP_FAIL Parameter error
  327. */
  328. esp_err_t uart_enable_tx_intr(uart_port_t uart_num, int enable, int thresh);
  329. /**
  330. * @brief register UART interrupt handler(ISR).
  331. *
  332. * @note UART ISR handler will be attached to the same CPU core that this function is running on.
  333. * Users should know that which CPU is running and then pick a INUM that is not used by system.
  334. * We can find the information of INUM and interrupt level in soc.h.
  335. *
  336. * @attention The ISR handler function MUST be defined with attribution of "IRAM_ATTR" for now.
  337. *
  338. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  339. * @param uart_intr_num UART interrupt number,check the info in soc.h, and please refer to core-isa.h for more details
  340. * @param fn Interrupt handler function.
  341. * @param arg parameter for handler function
  342. *
  343. * @return
  344. * - ESP_OK Success
  345. * - ESP_FAIL Parameter error
  346. */
  347. esp_err_t uart_isr_register(uart_port_t uart_num, uint8_t uart_intr_num, void (*fn)(void*), void * arg);
  348. /**
  349. * @brief Set UART pin number
  350. *
  351. * @note Internal signal can be output to multiple GPIO pads.
  352. * Only one GPIO pad can connect with input signal.
  353. *
  354. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  355. * @param tx_io_num UART TX pin GPIO number, if set to UART_PIN_NO_CHANGE, use the current pin.
  356. * @param rx_io_num UART RX pin GPIO number, if set to UART_PIN_NO_CHANGE, use the current pin.
  357. * @param rts_io_num UART RTS pin GPIO number, if set to UART_PIN_NO_CHANGE, use the current pin.
  358. * @param cts_io_num UART CTS pin GPIO number, if set to UART_PIN_NO_CHANGE, use the current pin.
  359. *
  360. * @return
  361. * - ESP_OK Success
  362. * - ESP_FAIL Parameter error
  363. */
  364. 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);
  365. /**
  366. * @brief UART set RTS level (before inverse)
  367. * UART rx hardware flow control should not be set.
  368. *
  369. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  370. * @param level 1: RTS output low(active); 0: RTS output high(block)
  371. *
  372. * @return
  373. * - ESP_OK Success
  374. * - ESP_FAIL Parameter error
  375. */
  376. esp_err_t uart_set_rts(uart_port_t uart_num, int level);
  377. /**
  378. * @brief UART set DTR level (before inverse)
  379. *
  380. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  381. * @param level 1: DTR output low; 0: DTR output high
  382. *
  383. * @return
  384. * - ESP_OK Success
  385. * - ESP_FAIL Parameter error
  386. */
  387. esp_err_t uart_set_dtr(uart_port_t uart_num, int level);
  388. /**
  389. * @brief UART parameter configure
  390. *
  391. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  392. * @param uart_config UART parameter settings
  393. *
  394. * @return
  395. * - ESP_OK Success
  396. * - ESP_FAIL Parameter error
  397. */
  398. esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config);
  399. /**
  400. * @brief UART interrupt configure
  401. *
  402. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  403. * @param intr_conf UART interrupt settings
  404. *
  405. * @return
  406. * - ESP_OK Success
  407. * - ESP_FAIL Parameter error
  408. */
  409. esp_err_t uart_intr_config(uart_port_t uart_num, const uart_intr_config_t *intr_conf);
  410. /**
  411. * @brief Install UART driver.
  412. *
  413. * UART ISR handler will be attached to the same CPU core that this function is running on.
  414. * Users should know that which CPU is running and then pick a INUM that is not used by system.
  415. * We can find the information of INUM and interrupt level in soc.h.
  416. *
  417. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  418. * @param rx_buffer_size UART RX ring buffer size
  419. * @param tx_buffer_size UART TX ring buffer size.
  420. * If set to zero, driver will not use TX buffer, TX function will block task until all data have been sent out..
  421. * @param queue_size UART event queue size/depth.
  422. * @param uart_intr_num UART interrupt number,check the info in soc.h, and please refer to core-isa.h for more details
  423. * @param uart_queue UART event queue handle, if set NULL, driver will not use an event queue.
  424. *
  425. * @return
  426. * - ESP_OK Success
  427. * - ESP_FAIL Parameter error
  428. */
  429. esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int queue_size, int uart_intr_num, void* uart_queue);
  430. /**
  431. * @brief Uninstall UART driver.
  432. *
  433. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  434. *
  435. * @return
  436. * - ESP_OK Success
  437. * - ESP_FAIL Parameter error
  438. */
  439. esp_err_t uart_driver_delete(uart_port_t uart_num);
  440. /**
  441. * @brief Wait UART TX FIFO empty
  442. *
  443. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  444. * @param ticks_to_wait Timeout, count in RTOS ticks
  445. *
  446. * @return
  447. * - ESP_OK Success
  448. * - ESP_FAIL Parameter error
  449. * - ESP_ERR_TIMEOUT Timeout
  450. */
  451. esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait);
  452. /**
  453. * @brief Send data to the UART port from a given buffer and length.
  454. *
  455. * This function will not wait for the space in TX FIFO, just fill the TX FIFO and return when the FIFO is full.
  456. * @note This function should only be used when UART TX buffer is not enabled.
  457. *
  458. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  459. * @param buffer data buffer address
  460. * @param len data length to send
  461. *
  462. * @return
  463. * - (-1) Parameter error
  464. * - OTHERS(>=0) The number of data that pushed to the TX FIFO
  465. */
  466. int uart_tx_chars(uart_port_t uart_num, const char* buffer, uint32_t len);
  467. /**
  468. * @brief Send data to the UART port from a given buffer and length,
  469. *
  470. * If parameter tx_buffer_size is set to zero:
  471. * This function will not return until all the data have been sent out, or at least pushed into TX FIFO.
  472. *
  473. * Otherwise, if tx_buffer_size > 0, this function will return after copying all the data to tx ringbuffer,
  474. * then, UART ISR will move data from ring buffer to TX FIFO gradually.
  475. *
  476. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  477. * @param src data buffer address
  478. * @param size data length to send
  479. *
  480. * @return
  481. * - (-1) Parameter error
  482. * - OTHERS(>=0) The number of data that pushed to the TX FIFO
  483. */
  484. int uart_write_bytes(uart_port_t uart_num, const char* src, size_t size);
  485. /**
  486. * @brief Send data to the UART port from a given buffer and length,
  487. *
  488. * If parameter tx_buffer_size is set to zero:
  489. * This function will not return until all the data and the break signal have been sent out.
  490. * After all data send out, send a break signal.
  491. *
  492. * Otherwise, if tx_buffer_size > 0, this function will return after copying all the data to tx ringbuffer,
  493. * then, UART ISR will move data from ring buffer to TX FIFO gradually.
  494. * After all data send out, send a break signal.
  495. *
  496. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  497. * @param src data buffer address
  498. * @param size data length to send
  499. * @param brk_len break signal length (unit: time of one data bit at current_baudrate)
  500. *
  501. * @return
  502. * - (-1) Parameter error
  503. * - OTHERS(>=0) The number of data that pushed to the TX FIFO
  504. */
  505. int uart_write_bytes_with_break(uart_port_t uart_num, const char* src, size_t size, int brk_len);
  506. /**
  507. * @brief UART read bytes from UART buffer
  508. *
  509. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  510. * @param buf pointer to the buffer.
  511. * @param length data length
  512. * @param ticks_to_wait sTimeout, count in RTOS ticks
  513. *
  514. * @return
  515. * - (-1) Error
  516. * - Others return a char data from uart fifo.
  517. */
  518. int uart_read_bytes(uart_port_t uart_num, uint8_t* buf, uint32_t length, TickType_t ticks_to_wait);
  519. /**
  520. * @brief UART ring buffer flush
  521. *
  522. * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2
  523. *
  524. * @return
  525. * - ESP_OK Success
  526. * - ESP_FAIL Parameter error
  527. */
  528. esp_err_t uart_flush(uart_port_t uart_num);
  529. /***************************EXAMPLE**********************************
  530. *
  531. *
  532. * ----------------EXAMPLE OF UART SETTING ---------------------
  533. * @code{c}
  534. * //1. Setup UART
  535. * #include "freertos/queue.h"
  536. * #define UART_INTR_NUM 17 //choose one interrupt number from soc.h
  537. * //a. Set UART parameter
  538. * int uart_num = 0; //uart port number
  539. * uart_config_t uart_config = {
  540. * .baud_rate = UART_BITRATE_115200, //baudrate
  541. * .data_bits = UART_DATA_8_BITS, //data bit mode
  542. * .parity = UART_PARITY_DISABLE, //parity mode
  543. * .stop_bits = UART_STOP_BITS_1, //stop bit mode
  544. * .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, //hardware flow control(cts/rts)
  545. * .rx_flow_ctrl_thresh = 120, //flow control threshold
  546. * };
  547. * uart_param_config(uart_num, &uart_config);
  548. * //b1. Setup UART driver(with UART queue)
  549. * QueueHandle_t uart_queue;
  550. * //parameters here are just an example, tx buffer size is 2048
  551. * uart_driver_install(uart_num, 1024 * 2, 1024 * 2, 10, UART_INTR_NUM, &uart_queue);
  552. * //b2. Setup UART driver(without UART queue)
  553. * //parameters here are just an example, tx buffer size is 0
  554. * uart_driver_install(uart_num, 1024 * 2, 0, 10, UART_INTR_NUM, NULL);
  555. *@endcode
  556. *-----------------------------------------------------------------------------*
  557. * @code{c}
  558. * //2. Set UART pin
  559. * //set UART pin, not needed if use default pins.
  560. * uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, 15, 13);
  561. * @endcode
  562. *-----------------------------------------------------------------------------*
  563. * @code{c}
  564. * //3. Read data from UART.
  565. * uint8_t data[128];
  566. * int length = 0;
  567. * length = uart_read_bytes(uart_num, data, sizeof(data), 100);
  568. * @endcode
  569. *-----------------------------------------------------------------------------*
  570. * @code{c}
  571. * //4. Write data to UART.
  572. * char* test_str = "This is a test string.\n"
  573. * uart_write_bytes(uart_num, (const char*)test_str, strlen(test_str));
  574. * @endcode
  575. *-----------------------------------------------------------------------------*
  576. * @code{c}
  577. * //5. Write data to UART, end with a break signal.
  578. * uart_write_bytes_with_break(0, "test break\n",strlen("test break\n"), 100);
  579. * @endcode
  580. *-----------------------------------------------------------------------------*
  581. * @code{c}
  582. * //6. an example of echo test with hardware flow control on UART1
  583. * void uart_loop_back_test()
  584. * {
  585. * int uart_num = 1;
  586. * uart_config_t uart_config = {
  587. * .baud_rate = 115200,
  588. * .data_bits = UART_DATA_8_BITS,
  589. * .parity = UART_PARITY_DISABLE,
  590. * .stop_bits = UART_STOP_BITS_1,
  591. * .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
  592. * .rx_flow_ctrl_thresh = 122,
  593. * };
  594. * //Configure UART1 parameters
  595. * uart_param_config(uart_num, &uart_config);
  596. * //Set UART1 pins(TX: IO16, RX: IO17, RTS: IO18, CTS: IO19)
  597. * uart_set_pin(uart_num, 16, 17, 18, 19);
  598. * //Install UART driver( We don't need an event queue here)
  599. * uart_driver_install(uart_num, 1024 * 2, 1024*4, 10, 17, NULL, RINGBUF_TYPE_BYTEBUF);
  600. * uint8_t data[1000];
  601. * while(1) {
  602. * //Read data from UART
  603. * int len = uart_read_bytes(uart_num, data, sizeof(data), 10);
  604. * //Write data back to UART
  605. * uart_write_bytes(uart_num, (const char*)data, len);
  606. * }
  607. * }
  608. * @endcode
  609. *-----------------------------------------------------------------------------*
  610. * @code{c}
  611. * //7. An example of using UART event queue on UART0.
  612. * #include "freertos/queue.h"
  613. * //A queue to handle UART event.
  614. * QueueHandle_t uart0_queue;
  615. * static const char *TAG = "uart_example";
  616. * void uart_task(void *pvParameters)
  617. * {
  618. * int uart_num = (int)pvParameters;
  619. * uart_event_t event;
  620. * size_t size = 1024;
  621. * uint8_t* dtmp = (uint8_t*)malloc(size);
  622. * for(;;) {
  623. * //Waiting for UART event.
  624. * if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
  625. * ESP_LOGI(TAG, "uart[%d] event:", uart_num);
  626. * switch(event.type) {
  627. * memset(dtmp, 0, size);
  628. * //Event of UART receving data
  629. * case UART_DATA:
  630. * ESP_LOGI(TAG,"data, len: %d", event.size);
  631. * int len = uart_read_bytes(uart_num, dtmp, event.size, 10);
  632. * ESP_LOGI(TAG, "uart read: %d", len);
  633. uart_write_bytes(uart_num, (const char*)dtmp, len);
  634. * break;
  635. * //Event of HW FIFO overflow detected
  636. * case UART_FIFO_OVF:
  637. * ESP_LOGI(TAG, "hw fifo overflow\n");
  638. * break;
  639. * //Event of UART ring buffer full
  640. * case UART_BUFFER_FULL:
  641. * ESP_LOGI(TAG, "ring buffer full\n");
  642. * break;
  643. * //Event of UART RX break detected
  644. * case UART_BREAK:
  645. * ESP_LOGI(TAG, "uart rx break\n");
  646. * break;
  647. * //Event of UART parity check error
  648. * case UART_PARITY_ERR:
  649. * ESP_LOGI(TAG, "uart parity error\n");
  650. * break;
  651. * //Event of UART frame error
  652. * case UART_FRAME_ERR:
  653. * ESP_LOGI(TAG, "uart frame error\n");
  654. * break;
  655. * //Others
  656. * default:
  657. * ESP_LOGI(TAG, "uart event type: %d\n", event.type);
  658. * break;
  659. * }
  660. * }
  661. * }
  662. * free(dtmp);
  663. * dtmp = NULL;
  664. * vTaskDelete(NULL);
  665. * }
  666. *
  667. * void uart_queue_test()
  668. * {
  669. * int uart_num = 0;
  670. * uart_config_t uart_config = {
  671. * .baud_rate = 115200,
  672. * .data_bits = UART_DATA_8_BITS,
  673. * .parity = UART_PARITY_DISABLE,
  674. * .stop_bits = UART_STOP_BITS_1,
  675. * .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  676. * .rx_flow_ctrl_thresh = 122,
  677. * };
  678. * //Set UART parameters
  679. * uart_param_config(uart_num, &uart_config);
  680. * //Set UART pins,(-1: default pin, no change.)
  681. * uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
  682. * //Set UART log level
  683. * esp_log_level_set(TAG, ESP_LOG_INFO);
  684. * //Install UART driver, and get the queue.
  685. * uart_driver_install(uart_num, 1024 * 2, 1024*4, 10, 17, &uart0_queue);
  686. * //Create a task to handler UART event from ISR
  687. * xTaskCreate(uart_task, "uTask", 1024, (void*)uart_num, 10, NULL);
  688. * }
  689. * @endcode
  690. *
  691. ***************************END OF EXAMPLE**********************************/
  692. #ifdef __cplusplus
  693. }
  694. #endif
  695. #endif /*_DRIVER_UART_H_*/