Skip to content

fix(uart): don't lock up when the UART IRQ can't run - #3129

Open
fpistm wants to merge 3 commits into
stm32duino:mainfrom
fpistm:uart-irq-blocked
Open

fpistm wants to merge 3 commits into
stm32duino:mainfrom
fpistm:uart-irq-blocked

Conversation

@fpistm

@fpistm fpistm commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Summary

This PR fixes the following bugs

  • Bug 1: Uart::write(), Uart::flush() and uart_debug_write() lock up
    when the U(S)ART interrupt can't be executed

Note: this PR is based on #3128. Until it is merged, the first two
commits belong to it, only the last commit
(fix(uart): don't lock up when the UART IRQ can't run) is new
.
I will rebase once #3128 is merged.

write() on a full TX buffer, flush() and uart_debug_write() wait for the
U(S)ART interrupt to make the transfer progress. If this interrupt can't be
executed they wait for ever:

  • interrupts disabled (PRIMASK, BASEPRI),
  • 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.

  • New uart_irq_poll() detects this situation, then processes the pending
    events 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.
  • If the handler can't be called safely (called from the U(S)ART interrupt
    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.
  • Nothing is sent until the interrupt can run, except through the polling:
    a flush() is needed to get the data out of a fault handler.

Cost

  • Code size: +212 bytes on G071RB, +240 on F429ZI and C562RE (for the whole
    series +256 / +284 / +288 bytes with fix(uart): make Uart::write() TX start atomic #3128), RAM unchanged.
  • Speed: not measurable on the normal write() path (below the boot to boot
    variation of about 1% of a write()). The polling only runs in the wait
    loops.

Validation

Test sketch: write() of 300 bytes then flush() in each context, with the
data checked on the host.

Case before after
ISR with lower priority than UART (control) PASS PASS
interrupts disabled (PRIMASK) lock up PASS
ISR with higher priority than UART lock up PASS
ISR with the same priority as UART lock up PASS
BASEPRI masking the UART lock up PASS
HardFault handler not run PASS
  • NUCLEO_F429ZI (HAL v1, M4): all cases pass, 20 repeats of the non
    locking ones, 100 of 100.
  • NUCLEO_G071RB (HAL v1, M0+, no BASEPRI): all cases pass.
  • NUCLEO_C562RE (HAL v2, M33): all cases pass (run before the final
    bool/single-return cleanup, which was only built for this board).
  • Echo test at 921600 baud under interrupt load is unchanged (F429ZI: 50 of 50
    loops and 30 of 30 with 200 bytes writes).

Code formatting

  • AStyle to be checked by CI

Closing issues

Fixes #1834

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>
@fpistm fpistm added the enhancement New feature or request label Dec 1, 2026
@fpistm fpistm added this to the 3.2.0 milestone Dec 1, 2026
@nedseb

nedseb commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

I tried it on the same STeaMi V1c (STM32WB55, Cortex-M4, Serial on LPUART1, core at 64 MHz). This time I used the whole PR head (9e016e9, so main + #3128 + this PR), with main (5bdb3b9) as the control.

Each case does a 300-byte write(), then flush(), and the host checks every byte:

Case main this PR
loop (control) PASS PASS
timer ISR, lower priority than the UART PASS PASS
interrupts disabled (PRIMASK) lock up PASS
timer ISR, higher priority than the UART lock up PASS
timer ISR, same priority as the UART lock up PASS
BASEPRI masking the UART lock up PASS
HardFault handler (udf) lock up PASS

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 main, no byte of the block came out in the lock-up cases.

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 main + #3128 only (43.3 to 43.9 kB/s, against 41.8 kB/s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

HardwareSerial/Uart write and flush lock up when interrupts are disabled #472

2 participants