Skip to content

arch/arm/stm32: fix UE handling in stm32_serial_m3m4 setformat for F3/G4 - #21894

Merged
GUIDINGLI merged 1 commit into
apache:masterfrom
Wooohaaa:fix/stm32f3g4-setformat-ue
Nov 24, 2026
Merged

GUIDINGLI merged 1 commit into
apache:masterfrom
Wooohaaa:fix/stm32f3g4-setformat-ue

Conversation

@Wooohaaa

Copy link
Copy Markdown
Contributor

Summary

up_set_format() 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.

Change

  • arch/arm/src/common/stm32/stm32_serial_m3m4_v1v2v3v4.c:
    • Extend the UE clearing condition from G4-only to F3+G4.
    • Save UE before clearing (instead of clearing UE|TE|RE without
      saving).
    • Clear only UE (instead of UE|TE|RE), matching the L4 (arch/arm/src/stm32l4: disable the USART while changing the line format #20914)
      pattern.
    • Restore UE from the saved value (instead of unconditionally
      OR-ing UE|TE|RE).
    • Add a critical section around the UE toggle.

Test plan

  • Built nucleo-f334r8:nsh (F3) and nucleo-g431rb:nsh (G4) with
    arm-none-eabi-gcc; the serial driver compiles cleanly for both.
  • Not tested on hardware (no F3 or G4 board available).
  • The change is gated on the F3/G4 #if conditions, so F1/F2/F4/L1
    boards are unaffected.

Fixes #20969

@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 16, 2026
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@darrylring

Copy link
Copy Markdown
Contributor

Please see #21903

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]>
@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 23, 2026
@GUIDINGLI
GUIDINGLI merged commit a60e7a3 into apache:master Nov 24, 2026
35 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: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

arch/arm/stm32: serial setformat writes CR1 parity/word-length bits with UE=1 in several drivers

4 participants