Skip to content

arch/arm/src/common/stm32: disable the USART while changing line format in m33_v3 - #21427

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/stm32-m33-serial-setformat-ue
Nov 4, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/stm32-m33-serial-setformat-ue

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

In stm32serial_setformat() (arch/arm/src/common/stm32/stm32_serial_m33_v3.c), the baud rate register (BRR), CR2 STOP bits, and the parity/word length bits (PCE, PS, M0, M1) in CR1 were modified while the USART remained enabled (UE = 1).

Per ST Reference Manuals (e.g. RM0456 for STM32H5, RM0454 for STM32U5, RM0438 for STM32L5, and RM0481 for STM32C5), these registers and configuration bits can only be written while the USART is disabled (UE = 0). Consequently, run-time tcsetattr() calls attempting to change baud rate, parity, or word length returned success (0) while leaving hardware USART registers unchanged.

This patch clears USART_CR1_UE before updating the line configuration and restores it upon completion inside a critical section, aligning stm32_serial_m33_v3.c with the approved implementations in:

This unified fix resolves the hardware write-protection bug across all 5 modern STM32 Cortex-M33 chip families sharing this driver (STM32H5, STM32U5, STM32L5, STM32U3, STM32C5).

Impact

  • Impact on user: Run-time tcsetattr() calls to change serial line configuration (baud rate, parity, word length, stop bits) now reliably update the hardware registers instead of being ignored by the MCU.
  • Impact on build: None.
  • Impact on hardware: Covers all STM32 Cortex-M33 platforms utilizing CONFIG_STM32_HAVE_IP_USART_M33_V3 (STM32H5, STM32U5, STM32L5, STM32U3, STM32C5).

Testing

  • Style verification: Verified with nxstyle with 0 errors and 0 warnings.
  • Build host: Linux Docker / Windows 11

@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Size: S The size of the change in this PR is small labels Nov 3, 2026
In stm32serial_setformat(), BRR, CR2 STOP bits, and the PCE, PS,
and M0/M1 bits of CR1 were modified with the USART enabled. Per
the ST Reference Manuals (e.g., RM0456 for STM32H5, RM0454 for STM32U5,
RM0438 for STM32L5, and RM0481 for STM32C5), these registers and bits
can only be written while UE = 0. Modifying them while UE = 1 is ignored
by the hardware, so a run-time tcsetattr() call attempting to change
baud rate, parity, or word length returned success while leaving the
hardware USART registers unchanged.

Clear UE before updating the line configuration and restore it
afterwards inside a critical section, aligning stm32_serial_m33_v3.c with
the approved implementations in STM32N6 (commit 2b1b82c), STM32WL5
(PR apache#21336), STM32WB (PR apache#21334), and STM32L4 (commit 47180bf).

Signed-off-by: Swatantra Yadav <[email protected]>
Assisted-by: Google Gemini
@Swatantra-66
Swatantra-66 force-pushed the fix/stm32-m33-serial-setformat-ue branch from 41eaabb to f2a0978 Compare November 3, 2026 20:31
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@xiaoxiang781216
xiaoxiang781216 merged commit 706e098 into apache:master Nov 4, 2026
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants