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>
|
Thank you for the quick fix! I tried it on a STeaMi V1c (STM32WB55, Each run echoes a random block twice, first with the echo throttled to 50 kB/s and then at full rate:
All the bytes now come back exactly once. As a side effect, the board no longer drops received bytes in the throttled run (17212 dropped before, 0 now). I am not sure why, maybe because the stale resends kept the TX busy. One unrelated thing I noticed while reading, which I have not tested: in |
|
Ok. Thanks for the test. You can open an other issue, I will check. |
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