Skip to content

fix(uart): don't resend old data on flush() timeout - #3132

Merged
fpistm merged 1 commit into
stm32duino:mainfrom
fpistm:fix/uart-flush-timeout
Dec 3, 2026
Merged

fpistm merged 1 commit into
stm32duino:mainfrom
fpistm:fix/uart-flush-timeout

Conversation

@fpistm

@fpistm fpistm commented Dec 2, 2026

Copy link
Copy Markdown
Member

Summary

Don't resend old data when Uart::flush(timeout) expires during a transfer.

This PR fixes the following bugs

  • flush(timeout) sends up to a whole TX buffer of stale bytes after a timeout in the middle of a transfer

Explain the motivation for making this change. What existing problem does the pull request solve?

On timeout, flush(uint32_t) discarded the pending data with tx_head = tx_tail. The HAL transfer of tx_size bytes from tx_tail can't be cancelled and is still running. When it completes, _tx_complete_irq() moves tx_tail by tx_size, past tx_head. The buffer then looks almost full and SERIAL_TX_BUFFER_SIZE - tx_size old bytes are sent again.

On timeout, only the data which is not in the transfer in progress is now discarded (tx_head = tx_tail + tx_size). This is done in a critical section (PRIMASK saved/restored, like in write() since #3128), as the TX complete IRQ may end the transfer and start the next one at any time. If no transfer is active, tx_head is reset to tx_tail as before.

Note: the bytes of the transfer already started are still sent, they can't be cancelled.

Validation

Sketch: fill the whole TX ring (64 bytes) with x and send it, then write 32 bytes (ABC...) and flush(3). At 57600 baud the 32 bytes take about 5.5 ms, so the timeout expires during the transfer. Expected on the host: 64 x, the 32 bytes, nothing else.

void setup() {
  Serial.begin(57600);
  for (int i = 0; i < SERIAL_TX_BUFFER_SIZE; i++) {
    Serial.write('x');
  }
  Serial.flush();
  uint8_t payload[SERIAL_TX_BUFFER_SIZE / 2];
  for (size_t i = 0; i < sizeof(payload); i++) {
    payload[i] = 'A' + (i % 26);
  }
  Serial.write(payload, sizeof(payload));
  Serial.flush(3);
}

void loop() {}
Board Without the fix With the fix
NUCLEO_G431RB (Serial on LPUART1) 5/5 runs: 32 stale x after the 32 bytes 20/20 runs OK
NUCLEO_L053R8 (Serial on USART2) 5/5 runs: 32 stale x after the 32 bytes 20/20 runs OK

The issue's 9600 baud example can't be run on the NUCLEO_G431RB: LPUART1 clocked at 170 MHz can't go below about 41.5 kbaud.

  • Ensure CI build is passed.

Code formatting

  • AStyle checked locally (Serial.cpp unchanged).

Closing issues

Fixes #3130

Assisted-by: GitHub Copilot: claude-sonnet-5.5

When Uart::flush(timeout) expired during a HAL transfer, it set
tx_head = tx_tail to discard the pending data. But the transfer in
progress can't be cancelled: at its completion _tx_complete_irq()
moves tx_tail by tx_size, past tx_head. The buffer then looks almost
full and up to SERIAL_TX_BUFFER_SIZE - tx_size old bytes are sent
again.

On timeout, only discard the data which is not in the transfer in
progress: tx_head = tx_tail + tx_size. This is done in a critical
section, as the TX complete IRQ may end the transfer and start the next
one at any time. If no transfer is active, reset tx_head to tx_tail as
before.

Tested on NUCLEO_G431RB (Serial on LPUART1) and NUCLEO_L053R8 (Serial
on USART2): with a TX buffer filled and sent, then 32 bytes written and
flush(3) at 57600 baud, 32 stale bytes were received after the 32 bytes
without this change, and none with it (20 runs).

Fixes: stm32duino#3130
Assisted-by: GitHub Copilot: claude-sonnet-5.5
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
@nedseb

nedseb commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Tested on a STeaMi board with a looped version of the sketch above, read at 57600 baud through the board's DAPLink USB-UART:

base c8f5da4: 0/21 cycles correct, each one ends with 32 stale x after the 32 letters;
This PR (a9061d2): no stale bytes anymore.

@fpistm
fpistm merged commit 492c370 into stm32duino:main Dec 3, 2026
30 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in STM32 core based on ST HAL Dec 3, 2026
@fpistm
fpistm deleted the fix/uart-flush-timeout branch December 3, 2026 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix 🩹 Bug fix

Projects

Development

Successfully merging this pull request may close these issues.

HardwareSerial: flush(timeout) can resend old bytes when the timeout expires during a transfer

2 participants