From 3c65f1b654aef4c5b89a8d715e360c3c5d19d25b Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Thu, 12 Sep 2024 10:44:18 +0200 Subject: [PATCH] fix(lp_uart): Added lp_uart flush feature This commit adds the lp_core_uart_flush() API to flush the LP UART Tx FIFO. This API is automatically called once the program returns from the main function(). Closes https://github.com/espressif/esp-idf/issues/14530 --- .../lp_core/include/ulp_lp_core_uart.h | 12 ++++++++++- components/ulp/lp_core/lp_core/lp_core_uart.c | 20 ++++++++++++++++++- .../lp_uart/lp_uart_print/main/lp_core/main.c | 2 ++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/components/ulp/lp_core/lp_core/include/ulp_lp_core_uart.h b/components/ulp/lp_core/lp_core/include/ulp_lp_core_uart.h index 3f97cafb864..5b125e5c0f4 100644 --- a/components/ulp/lp_core/lp_core/include/ulp_lp_core_uart.h +++ b/components/ulp/lp_core/lp_core/include/ulp_lp_core_uart.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -59,6 +59,16 @@ esp_err_t lp_core_uart_write_bytes(uart_port_t lp_uart_num, const void *src, siz */ int lp_core_uart_read_bytes(uart_port_t lp_uart_num, void *buf, size_t size, int32_t timeout); +/** + * @brief Flush LP UART Tx FIFO + * + * This function is automatically called before the LP core powers down once the main() function returns. + * It can also be called manually in the application to flush the Tx FIFO. + * + * @param lp_uart_num LP UART port number + */ +void lp_core_uart_tx_flush(uart_port_t lp_uart_num); + #ifdef __cplusplus } #endif diff --git a/components/ulp/lp_core/lp_core/lp_core_uart.c b/components/ulp/lp_core/lp_core/lp_core_uart.c index ef20026587e..f1a3bdae293 100644 --- a/components/ulp/lp_core/lp_core/lp_core_uart.c +++ b/components/ulp/lp_core/lp_core/lp_core_uart.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -63,6 +63,24 @@ int lp_core_uart_tx_chars(uart_port_t lp_uart_num, const void *src, size_t size) return tx_len; } +void lp_core_uart_tx_flush(uart_port_t lp_uart_num) +{ + (void)lp_uart_num; + int loop_cnt = 0; + + if (uart_ll_is_enabled(LP_UART_NUM_0) && !uart_hal_is_tx_idle(&hal)) { + /* Wait for the Tx FIFO to be empty */ + while (!(uart_hal_get_intraw_mask(&hal) & (LP_UART_TX_INT_FLAG | LP_UART_ERR_INT_FLAG))) { + loop_cnt++; + if (loop_cnt > 10000) { + /* Bail out */ + break; + } + } + uart_hal_clr_intsts_mask(&hal, LP_UART_TX_INT_FLAG | LP_UART_ERR_INT_FLAG); + } +} + esp_err_t lp_core_uart_write_bytes(uart_port_t lp_uart_num, const void *src, size_t size, int32_t timeout) { (void)lp_uart_num; diff --git a/examples/system/ulp/lp_core/lp_uart/lp_uart_print/main/lp_core/main.c b/examples/system/ulp/lp_core/lp_uart/lp_uart_print/main/lp_core/main.c index 4a47ccb3a23..3c4d48c9267 100644 --- a/examples/system/ulp/lp_core/lp_uart/lp_uart_print/main/lp_core/main.c +++ b/examples/system/ulp/lp_core/lp_uart/lp_uart_print/main/lp_core/main.c @@ -7,6 +7,7 @@ #include #include "ulp_lp_core_print.h" #include "ulp_lp_core_utils.h" +#include "ulp_lp_core_uart.h" int main (void) { @@ -17,6 +18,7 @@ int main (void) lp_core_printf("This program has run %d times\r\n", ++iteration); lp_core_printf("%s", separator); lp_core_printf("\n"); + lp_core_uart_tx_flush(LP_UART_NUM_0); return 0; }