Skip to content

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

Merged
fpistm merged 1 commit into
stm32duino:mainfrom
fpistm:uart-irq-blocked
Dec 2, 2026
Merged

fpistm merged 1 commit 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

@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).

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 merged commit c8f5da4 into stm32duino:main Dec 2, 2026
30 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in STM32 core based on ST HAL Dec 2, 2026
@fpistm
fpistm deleted the uart-irq-blocked branch December 2, 2026 09:28
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

Development

Successfully merging this pull request may close these issues.

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

2 participants