Skip to content

arch/arm/src/stm32wb: disable the USART while changing the line format - #21334

Merged
acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/stm32wb-serial-setformat-ue
Nov 1, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/stm32wb-serial-setformat-ue

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

In arch/arm/src/stm32wb/stm32wb_serial.c, stm32_serial_setformat() writes the baud rate register (BRR) and the parity/word length bits (PCE, PS, M0, M1) in CR1 while the USART is enabled (UE = 1).

Per the ST Reference Manual (RM0434), Section 37.8 (USART Control Register 1), bits M1, M0, PCE, and PS can only be written when the USART is disabled (UE = 0). Consequently, runtime tcsetattr() calls attempting to change the baud rate or parity/data bits return success while leaving the hardware USART registers completely unchanged.

This patch clears USART_CR1_UE before the update and restores it upon completion inside a critical section, bringing the STM32WB serial driver into alignment with the approved implementations in STM32L4 (commit 47180bf), STM32H7, and STM32F7.

Impact

  • Hardware: STM32WB microcontrollers (e.g. NUCLEO-WB55RG, Flipper Zero).
  • Functionality: Ensures runtime serial configuration changes via tcsetattr() take effect in hardware.

Testing

  • Verified against ST Reference Manual RM0434 (USART_CR1 bit write restrictions).
  • Logic aligns with merged commit 47180bf (arch/arm/src/stm32l4: disable the USART while changing the line format).

@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 1, 2026
xiaoxiang781216
xiaoxiang781216 previously approved these changes Nov 1, 2026
@github-actions

github-actions Bot commented Nov 1, 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.

stm32_serial_setformat() writes BRR and the PCE, PS, and M0/M1 bits of CR1
with the USART enabled.  They can only be written while UE = 0 per the ST
Reference Manual (RM0434), so a run-time tcsetattr() that changes the
baud rate, parity, or word length returned success but left the hardware
USART registers unchanged.

Clear UE around the update and restore it inside a critical section,
matching the approved STM32L4 (commit 47180bf), STM32H7, and F7
implementations.

Also fix pre-existing nxstyle alignment and declaration issues in
stm32_serial_ioctl, stm32_serial_detach, stm32_serial_txready, and
stm32_serial_dma_poll.

Signed-off-by: Swatantra Yadav <[email protected]>
Assisted-by: Google Gemini
@Swatantra-66
Swatantra-66 force-pushed the fix/stm32wb-serial-setformat-ue branch from cce6825 to 12b610c Compare November 1, 2026 16:50
@github-actions github-actions Bot added Size: L The size of the change in this PR is large and removed Size: S The size of the change in this PR is small labels Nov 1, 2026
@Swatantra-66

Copy link
Copy Markdown
Contributor Author

Fixed pre-existing nxstyle formatting in stm32wb_serial.c so checkpatch is now green

@acassis
acassis merged commit 0f22e12 into apache:master Nov 1, 2026
26 checks passed
xiaoxiang781216 pushed a commit that referenced this pull request Nov 2, 2026
stm32wl5serial_setformat() writes BRR, CR2 STOP bits, and the PCE, PS,
and M0/M1 bits of CR1 with the USART enabled.  They can only be written
while UE = 0 per the ST Reference Manual (RM0453), so a run-time
tcsetattr() that changes baud rate, parity, or word length returned
success but left the hardware USART registers unchanged.

Clear UE around the update and restore it inside a critical section,
matching the approved STM32L4 (commit 47180bf), STM32WB (PR #21334),
STM32H7, and F7 implementations.

Also fix pre-existing nxstyle alignment, declaration, and brace issues
in stm32wl5serial_ioctl, stm32wl5serial_setapbclock, stm32wl5serial_detach,
stm32wl5serial_pm_suspend, stm32wl5serial_pm_prepare, and
stm32_serial_dma_poll.

Signed-off-by: Swatantra Yadav <[email protected]>
Assisted-by: Google Gemini
Swatantra-66 added a commit to Swatantra-66/nuttx that referenced this pull request Nov 2, 2026
stm32serial_setformat() writes BRR, CR2 STOP bits, and the PCE, PS,
and M0/M1 bits of CR1 with the USART enabled. They can only be written
while UE = 0 per the ST Reference Manual (RM0486), so a run-time
tcsetattr() that changes baud rate, parity, or word length returned
success but left the hardware USART registers unchanged.

Clear UE around the update and restore it inside a critical section,
matching the approved STM32L4 (commit 47180bf), STM32WB (PR apache#21334),
STM32WL5 (PR apache#21336), STM32H7, and F7 implementations.

Also fix pre-existing nxstyle alignment, declaration, and brace issues
in stm32serial_pm_setsuspend, stm32serial_setapbclock, stm32serial_detach,
stm32serial_interrupt, stm32serial_ioctl, and stm32serial_pmprepare.

Fixes apache#21377

Signed-off-by: Swatantra Yadav <[email protected]>
Assisted-by: Google Gemini
xiaoxiang781216 pushed a commit that referenced this pull request Nov 3, 2026
stm32serial_setformat() writes BRR, CR2 STOP bits, and the PCE, PS,
and M0/M1 bits of CR1 with the USART enabled. They can only be written
while UE = 0 per the ST Reference Manual (RM0486), so a run-time
tcsetattr() that changes baud rate, parity, or word length returned
success but left the hardware USART registers unchanged.

Clear UE around the update and restore it inside a critical section,
matching the approved STM32L4 (commit 47180bf), STM32WB (PR #21334),
STM32WL5 (PR #21336), STM32H7, and F7 implementations.

Also fix pre-existing nxstyle alignment, declaration, and brace issues
in stm32serial_pm_setsuspend, stm32serial_setapbclock, stm32serial_detach,
stm32serial_interrupt, stm32serial_ioctl, and stm32serial_pmprepare.

Fixes #21377

Signed-off-by: Swatantra Yadav <[email protected]>
Assisted-by: Google Gemini
Swatantra-66 added a commit to Swatantra-66/nuttx that referenced this pull request 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
xiaoxiang781216 pushed a commit that referenced this pull request Nov 4, 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 #21336), STM32WB (PR #21334), and STM32L4 (commit 47180bf).

Signed-off-by: Swatantra Yadav <[email protected]>
Assisted-by: Google Gemini
xiaoxiang781216 pushed a commit that referenced this pull request Nov 17, 2026
On USART v2 and later, the parity and word-length bits in CR1 (PCE, PS,
M0/M1) can only be written while UE = 0. stm32serial_setformat() in
stm32_serial_m0_v3.c (used by F0 boards such as nucleo-f072rb) wrote
CR1 with UE = 1, so tcsetattr(8E1) returned 0 but the parity and word
length did not change.

Follow the pattern already used by the L4 (#20914) and WB (#21334)
drivers: enter a critical section, save and clear UE, configure the
format bits, then restore UE.

Built nucleo-f072rb:nsh with arm-none-eabi-gcc; the serial driver
compiles cleanly. Not tested on hardware.

Assisted-by: OpenCode:DeepSeek-V4.1-Flash

Fixes #20969
Wooohaaa added a commit to Wooohaaa/nuttx that referenced this pull request Nov 23, 2026
On USART v2 and later, the parity and word-length bits in CR1 (PCE, PS,
M0/M1) can only be written while UE = 0. The up_set_format() function
in stm32_serial_m3m4_v1v2v3v4.c had two problems for the F3 and G4
families:

1. F3 (F30XX/F33XX/F37XX): UE was never cleared, so tcsetattr(8E1)
   returned 0 but the parity and word length did not change.

2. G4 (STM32G4XXX): UE|TE|RE were cleared but the original values were
   not saved. At the end, UE|TE|RE were unconditionally OR-ed back,
   which would incorrectly set them to 1 even if they were originally 0.

Follow the pattern from the L4 (apache#20914) and WB (apache#21334) drivers: enter a
critical section, save and clear UE, configure the format bits, then
restore UE. Only UE is saved/cleared/restored; TE|RE are left unchanged
(the L4 fix does the same).

Built nucleo-f334r8:nsh and nucleo-g431rb:nsh with arm-none-eabi-gcc;
the serial driver compiles cleanly for both. Not tested on hardware.

Assisted-by: OpenCode:DeepSeek-V4.1-Flash

Fixes apache#20969

Signed-off-by: Wooohaaa <[email protected]>
GUIDINGLI pushed a commit that referenced this pull request Nov 24, 2026
On USART v2 and later, the parity and word-length bits in CR1 (PCE, PS,
M0/M1) can only be written while UE = 0. The up_set_format() function
in stm32_serial_m3m4_v1v2v3v4.c had two problems for the F3 and G4
families:

1. F3 (F30XX/F33XX/F37XX): UE was never cleared, so tcsetattr(8E1)
   returned 0 but the parity and word length did not change.

2. G4 (STM32G4XXX): UE|TE|RE were cleared but the original values were
   not saved. At the end, UE|TE|RE were unconditionally OR-ed back,
   which would incorrectly set them to 1 even if they were originally 0.

Follow the pattern from the L4 (#20914) and WB (#21334) drivers: enter a
critical section, save and clear UE, configure the format bits, then
restore UE. Only UE is saved/cleared/restored; TE|RE are left unchanged
(the L4 fix does the same).

Built nucleo-f334r8:nsh and nucleo-g431rb:nsh with arm-none-eabi-gcc;
the serial driver compiles cleanly for both. Not tested on hardware.

Assisted-by: OpenCode:DeepSeek-V4.1-Flash

Fixes #20969

Signed-off-by: Wooohaaa <[email protected]>
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: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants