Skip to content

Commit 3c65f1b

Browse files
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 #14530
1 parent 115dec6 commit 3c65f1b

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

components/ulp/lp_core/lp_core/include/ulp_lp_core_uart.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -59,6 +59,16 @@ esp_err_t lp_core_uart_write_bytes(uart_port_t lp_uart_num, const void *src, siz
5959
*/
6060
int lp_core_uart_read_bytes(uart_port_t lp_uart_num, void *buf, size_t size, int32_t timeout);
6161

62+
/**
63+
* @brief Flush LP UART Tx FIFO
64+
*
65+
* This function is automatically called before the LP core powers down once the main() function returns.
66+
* It can also be called manually in the application to flush the Tx FIFO.
67+
*
68+
* @param lp_uart_num LP UART port number
69+
*/
70+
void lp_core_uart_tx_flush(uart_port_t lp_uart_num);
71+
6272
#ifdef __cplusplus
6373
}
6474
#endif

components/ulp/lp_core/lp_core/lp_core_uart.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -63,6 +63,24 @@ int lp_core_uart_tx_chars(uart_port_t lp_uart_num, const void *src, size_t size)
6363
return tx_len;
6464
}
6565

66+
void lp_core_uart_tx_flush(uart_port_t lp_uart_num)
67+
{
68+
(void)lp_uart_num;
69+
int loop_cnt = 0;
70+
71+
if (uart_ll_is_enabled(LP_UART_NUM_0) && !uart_hal_is_tx_idle(&hal)) {
72+
/* Wait for the Tx FIFO to be empty */
73+
while (!(uart_hal_get_intraw_mask(&hal) & (LP_UART_TX_INT_FLAG | LP_UART_ERR_INT_FLAG))) {
74+
loop_cnt++;
75+
if (loop_cnt > 10000) {
76+
/* Bail out */
77+
break;
78+
}
79+
}
80+
uart_hal_clr_intsts_mask(&hal, LP_UART_TX_INT_FLAG | LP_UART_ERR_INT_FLAG);
81+
}
82+
}
83+
6684
esp_err_t lp_core_uart_write_bytes(uart_port_t lp_uart_num, const void *src, size_t size, int32_t timeout)
6785
{
6886
(void)lp_uart_num;

examples/system/ulp/lp_core/lp_uart/lp_uart_print/main/lp_core/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdint.h>
88
#include "ulp_lp_core_print.h"
99
#include "ulp_lp_core_utils.h"
10+
#include "ulp_lp_core_uart.h"
1011

1112
int main (void)
1213
{
@@ -17,6 +18,7 @@ int main (void)
1718
lp_core_printf("This program has run %d times\r\n", ++iteration);
1819
lp_core_printf("%s", separator);
1920
lp_core_printf("\n");
21+
lp_core_uart_tx_flush(LP_UART_NUM_0);
2022

2123
return 0;
2224
}

0 commit comments

Comments
 (0)