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 3f97cafb86..5b125e5c0f 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 ef20026587..f1a3bdae29 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 4a47ccb3a2..3c4d48c926 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; }