Repository navigation
fix(uart): don't resend old data on flush() timeout - #3132
Merged
Merged
Conversation
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>
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 transferExplain the motivation for making this change. What existing problem does the pull request solve?
On timeout,
flush(uint32_t)discarded the pending data withtx_head = tx_tail. The HAL transfer oftx_sizebytes fromtx_tailcan't be cancelled and is still running. When it completes,_tx_complete_irq()movestx_tailbytx_size, pasttx_head. The buffer then looks almost full andSERIAL_TX_BUFFER_SIZE - tx_sizeold 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 inwrite()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_headis reset totx_tailas 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
xand send it, then write 32 bytes (ABC...) andflush(3). At 57600 baud the 32 bytes take about 5.5 ms, so the timeout expires during the transfer. Expected on the host: 64x, the 32 bytes, nothing else.Serialon LPUART1)xafter the 32 bytesSerialon USART2)xafter the 32 bytesThe 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.
Code formatting
Serial.cppunchanged).Closing issues
Fixes #3130
Assisted-by: GitHub Copilot: claude-sonnet-5.5