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>
Uart::write() on a full TX buffer, Uart::flush() and uart_debug_write() wait for the U(S)ART interrupt to make the transfer progress. When this interrupt can't be executed they wait for ever: interrupts disabled (PRIMASK, BASEPRI), or called from an exception with an equal or higher priority than the U(S)ART one (SysTick, HardFault, ...). The timeouts don't help as they rely on the SysTick interrupt. Add uart_irq_poll() which detects this situation and then processes the pending events by calling the HAL IRQ handler, as the interrupt would have done. Use it in the wait loops. If the handler can't be called safely (called from the U(S)ART interrupt itself, or while it is preempted), write() drops the remaining data and flush() returns, instead of locking up. write() returns the number of bytes queued. Tested on NUCLEO_C562RE (HAL v2, Cortex-M33): write() of 300 bytes then flush() with PRIMASK set, with BASEPRI masking the UART, from timer ISRs of a higher, equal and lower priority than the UART, and from a HardFault handler. All locked up before, except the lower priority one, and now output the data. Echo test at 921600 baud with interrupt load unchanged. Built for F4 (HAL v1, Cortex-M4) and G0 (Cortex-M0+). Fixes: stm32duino#1834 Assisted-by: GitHub Copilot: claude-sonnet-5.5 Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
Contributor
|
I tried it on the same STeaMi V1c (STM32WB55, Cortex-M4, Each case does a 300-byte
With this PR, I ran the first six cases 10 times in a row on one upload, then the HardFault case, and all 61 passed. On The echo test from #3125 is still clean with the whole series: nothing repeated or lost on 64 KB and 256 KB blocks, throttled and at full rate. The full-rate echo speed was about the same as with |
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(),Uart::flush()anduart_debug_write()lock upwhen the U(S)ART interrupt can't be executed
write()on a full TX buffer,flush()anduart_debug_write()wait for theU(S)ART interrupt to make the transfer progress. If this interrupt can't be
executed they wait for ever:
PRIMASK,BASEPRI),one (SysTick, HardFault, ...).
The timeouts don't help as they rely on the SysTick interrupt.
uart_irq_poll()detects this situation, then processes the pendingevents by calling the HAL IRQ handler, as the interrupt would have done (the
same approach as AVR
HardwareSerial). It is used in the wait loops.itself, or while it is preempted, detected on Cortex-M3 and later),
write()drops the remaining data and
flush()returns instead of locking up.write()returns the number of bytes queued.a
flush()is needed to get the data out of a fault handler.Cost
series +256 / +284 / +288 bytes with fix(uart): make Uart::write() TX start atomic #3128), RAM unchanged.
write()path (below the boot to bootvariation of about 1% of a
write()). The polling only runs in the waitloops.
Validation
Test sketch:
write()of 300 bytes thenflush()in each context, with thedata checked on the host.
PRIMASK)BASEPRImasking the UARTlocking ones, 100 of 100.
bool/single-return cleanup, which was only built for this board).loops and 30 of 30 with 200 bytes writes).
Code formatting
Closing issues
Fixes #1834