Page 1 of 1

Possible to check UART0 Tx buffer empty before light sleep?

Posted: Tue Oct 31, 2023 12:39 am
by dalbert
A call to esp_light_sleep_start(), immediately stops UART0 output, this can have a number of undesirable consequences. Is there any way to check whether the Tx buffer empty status so you can delay sleep entry until UART output is finished? I didn't see anything in the HardwareSerial class. If not, is there a recommended workaround? Thanks!

Re: Possible to check UART0 Tx buffer empty before light sleep?

Posted: Tue Oct 31, 2023 12:48 am
by lbernstone
HardwareSerial::flush() will block until the uart queue is cleared.

Re: Possible to check UART0 Tx buffer empty before light sleep?

Posted: Sat Nov 04, 2023 11:20 pm
by dalbert
Thanks @lbernstone, that is indeed a good workaround in many cases. I was (and still am) hoping to find a non-blocking solution; the flush code eventually gets to this:

Code: Select all

    UART_MUTEX_LOCK();
    while(!uart_ll_is_tx_idle(UART_LL_GET_HW(uart->num)));

    if ( !txOnly ) {
        ESP_ERROR_CHECK(uart_flush_input(uart->num));
    }
    UART_MUTEX_UNLOCK();
I'll check to see if it's feasible to grab the mutex lock and call uart_ll_is_tx_idle() externally.