Repository navigation
Conversation
Uart::write() updated tx_head and then checked whether a HAL transfer was ongoing, without protection. If write() was preempted between the two steps for longer than the new data takes to send, the TX complete IRQ sent the data and left the UART idle with tx_tail == tx_head. write() then started a transfer from the empty buffer, and at its end tx_tail went past tx_head, so the last SERIAL_TX_BUFFER_SIZE bytes were sent a second time. Update tx_head and start the transfer in a critical section, restoring PRIMASK afterwards as write() may be called with interrupts disabled. Compute the transfer size from tx_head and tx_tail inside the critical section, and only start a transfer if data are pending. Fixes: stm32duino#3125 Assisted-by: GitHub Copilot: claude-sonnet-5.5 Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
When the TX buffer was full, Uart::write() waited for the TX interrupt to free space but kept the free space value read before the wait, so it was 0. The split loop then recursed with a zero-length write before the real one. Read the free space again in the wait loop so the data are queued directly. Tested on NUCLEO_C562RE at 921600 baud with an echo using single byte and 200 bytes write() calls, with and without interrupt load. Assisted-by: GitHub Copilot: claude-sonnet-5.5 Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
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
This PR fixes the following bugs
Uart::write()can restart a TX transfer from an empty bufferand send the last
SERIAL_TX_BUFFER_SIZEbytes twiceUart::write()queued the data, then started a HAL transfer if none wasongoing, in two non-atomic steps. If
write()was preempted between thetx_headupdate and theserial_tx_active()check for longer than the newdata takes to send, the TX complete IRQ sent the data and left the UART idle
with
tx_tail == tx_head.write()then started a transfer from the emptybuffer (a stale byte), and at its end
tx_tailwent pasttx_head, so thelast
SERIAL_TX_BUFFER_SIZEbytes were sent again.tx_headupdate and the transfer start are now a critical section.PRIMASK is saved and restored, as
write()can be called with interruptsdisabled.
tx_head/tx_tailinside the criticalsection and a transfer is only started if data are pending.
removes a useless zero-length recursive
write().Validation
Tested on NUCLEO_C562RE at 921600 baud with an echo sketch and a host script
checking that no byte is lost, repeated or corrupted. A timer interrupt of lower
priority than the UART one (30 us at 5 kHz) holds the main loop to trigger the
race, with
-DSERIAL_RX_BUFFER_SIZE=1024to avoid unrelated RX overflow.main: FAIL 6 loops out of 10 (corrupted bytes, stale data sent)write()calls(20 loops) and with a text file
Code formatting
Closing issues
Fixes #3125