From f4d33d27395e1ad9cc4a1979573cad3d9395c9a6 Mon Sep 17 00:00:00 2001 From: songruojing Date: Wed, 29 Jun 2022 17:56:38 +0800 Subject: [PATCH] uart: Add a new API to get the free space size of tx buffer Closes https://github.com/espressif/esp-idf/issues/8932 Closes https://github.com/espressif/esp-idf/issues/3078 (cherry picked from commit 9d73475e44b08db4f648957496426c3d4d6b02ef) --- components/driver/include/driver/uart.h | 12 ++++++++++++ components/driver/uart.c | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/components/driver/include/driver/uart.h b/components/driver/include/driver/uart.h index 4524516338..f0c2ae1013 100644 --- a/components/driver/include/driver/uart.h +++ b/components/driver/include/driver/uart.h @@ -594,6 +594,18 @@ esp_err_t uart_flush_input(uart_port_t uart_num); */ esp_err_t uart_get_buffered_data_len(uart_port_t uart_num, size_t* size); +/** + * @brief UART get TX ring buffer free space size + * + * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1). + * @param size Pointer of size_t to accept the free space size + * + * @return + * - ESP_OK Success + * - ESP_ERR_INVALID_ARG Parameter error + */ +esp_err_t uart_get_tx_buffer_free_size(uart_port_t uart_num, size_t *size); + /** * @brief UART disable pattern detect function. * Designed for applications like 'AT commands'. diff --git a/components/driver/uart.c b/components/driver/uart.c index 1ff366bdaa..2046cfa33b 100644 --- a/components/driver/uart.c +++ b/components/driver/uart.c @@ -1343,6 +1343,15 @@ esp_err_t uart_get_buffered_data_len(uart_port_t uart_num, size_t *size) return ESP_OK; } +esp_err_t uart_get_tx_buffer_free_size(uart_port_t uart_num, size_t *size) +{ + ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error"); + ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_ERR_INVALID_ARG, UART_TAG, "uart driver error"); + ESP_RETURN_ON_FALSE((size != NULL), ESP_ERR_INVALID_ARG, UART_TAG, "arg pointer is NULL"); + *size = p_uart_obj[uart_num]->tx_buf_size - p_uart_obj[uart_num]->tx_len_tot; + return ESP_OK; +} + esp_err_t uart_flush(uart_port_t uart_num) __attribute__((alias("uart_flush_input"))); esp_err_t uart_flush_input(uart_port_t uart_num)