Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino HardwareSerial::flush() should not flush input ! #3095

Closed
bitli opened this issue Aug 16, 2019 · 2 comments
Closed

Arduino HardwareSerial::flush() should not flush input ! #3095

bitli opened this issue Aug 16, 2019 · 2 comments
Labels
Status: Stale Issue is stale stage (outdated/stuck)

Comments

@bitli
Copy link

bitli commented Aug 16, 2019

Hardware:

ESP32 using PlatformIO Espressif1.9 and Arduino framework

Description:

I have to toggle GPIO around UART rx and tx activities, porting code from AVR Arduino. This does not work due to a bug in the HardwareSerial.cpp of the ESP32 Arduino framework

On Arduino the Serial.flush() flushes ONLY the TX buffer (to flush the RX, you just read it to end). This how it is documented and implemented (this is also true of SoftSerial).

Code:

On ESP32, the code of HardwareSerial.cpp is

void HardwareSerial::flush()
{
    uartFlush(_uart);
}

and in esp32-hal-uart.c, the code of uartFlush() flushes the output AND ERRONEOUSLY THE INPUT. This cause data loss on the input when we flush output.

void uartFlush(uart_t* uart)
{
    if(uart == NULL) {
        return;
    }

    UART_MUTEX_LOCK();
    while(uart->dev->status.txfifo_cnt || uart->dev->status.st_utx_out);

    //Due to hardware issue, we can not use fifo_rst to reset uart fifo.
    //See description about UART_TXFIFO_RST and UART_RXFIFO_RST in <<esp32_technical_reference_manual>> v2.6 or later.

    // we read the data out and make `fifo_len == 0 && rd_addr == wr_addr`.
    while(uart->dev->status.rxfifo_cnt != 0 || (uart->dev->mem_rx_status.wr_addr != uart->dev->mem_rx_status.rd_addr)) {
        READ_PERI_REG(UART_FIFO_REG(uart->num));
    }

    UART_MUTEX_UNLOCK();
}
@stale
Copy link

stale bot commented Oct 15, 2019

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Oct 15, 2019
@stale
Copy link

stale bot commented Oct 29, 2019

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Oct 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck)
Projects
None yet
Development

No branches or pull requests

1 participant