Skip to content

arch/arm/src/stm32l4: disable the USART while changing the line format - #20914

Merged
michallenc merged 2 commits into
apache:masterfrom
daniel-p-carvalho:fix/stm32l4-serial-setformat-ue
Oct 23, 2026
Merged

michallenc merged 2 commits into
apache:masterfrom
daniel-p-carvalho:fix/stm32l4-serial-setformat-ue

Conversation

@daniel-p-carvalho

@daniel-p-carvalho daniel-p-carvalho commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

stm32l4serial_setformat() writes CR1 (PCE, M0) with the USART enabled.
These bits can only be written while UE = 0 (RM0394), so a run-time
tcsetattr() that changes parity returns success and has no effect.

Clear UE around the update and restore it, as the H7, F7 and M0 v4
drivers already do. Found with nxmodbus asking for 8E1 on a
NUCLEO-L432KC. stm32wb, stm32wl5, stm32n6, m33_v3, m0_v3 and the F3
branch of m3m4 have the same pattern; not changed here, untested.

Impact

tcsetattr() applies parity and stop bits on STM32L4. Nothing else changes.

Testing

NUCLEO-L432KC, USART2 via ST-LINK VCP, nxmodbus server in 8E1, read with
OpenOCD. Before: driver writes CR1 = 0x142d, reads back 0x2d, master gets
no answer. After: FC03 answered in 8E1 and 8N2.

Re-indent the switch statements whose case labels were aligned with the
braces, add the missing braces and blank lines after declarations, and
fix the remaining alignment and line length findings.  Whitespace and
braces only; no functional change.

Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: Daniel P. Carvalho <[email protected]>
stm32l4serial_setformat() writes BRR and the PCE and M0 bits of CR1
with the USART enabled.  They can only be written while UE = 0
(RM0394), so a run-time tcsetattr() that changes the parity returned
success and left the USART unchanged.  Clear UE around the update and
restore it, as the STM32H7, F7 and M0 v4 drivers already do.

Tested on a NUCLEO-L432KC (USART2 through the ST-LINK VCP) with an
nxmodbus server in 8E1: before, the driver wrote CR1 = 0x142d and read
back 0x2d; now it keeps the value and FC03 is answered in 8E1 and 8N2.

Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: Daniel P. Carvalho <[email protected]>
@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 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 Oct 23, 2026
@github-actions

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.

@michallenc
michallenc merged commit 47180bf into apache:master Oct 23, 2026
38 checks passed
Sarthak-Pandey added a commit to Sarthak-Pandey/nuttx that referenced this pull request Oct 25, 2026
On USART v2/v3 IP peripherals, CR1 parity (PCE, PS) and word-length (M0, M1) bits can only be written while UE=0. Previously, several STM32 drivers wrote CR1 with UE=1, causing writes to be silently ignored by hardware.

This commit waits for any in-flight transmission to complete (TC=1) with a timeout before disabling UE in a critical section, applying the format configuration, and restoring the original UE state.

Fixes apache#20969.

Follows the pattern from apache#20914.

Signed-off-by: Sarthak Pandey <[email protected]>
Sarthak-Pandey added a commit to Sarthak-Pandey/nuttx that referenced this pull request Oct 26, 2026
On USART v2/v3 IP peripherals, CR1 parity (PCE, PS) and word-length (M0, M1) bits can only be written while UE=0. Previously, several STM32 drivers wrote CR1 with UE=1, causing writes to be silently ignored by hardware.

This commit waits for any in-flight transmission to complete (TC=1) with a timeout before disabling UE in a critical section, applying the format configuration, and restoring the original UE state.

Fixes apache#20969.

Follows the pattern from apache#20914.

Signed-off-by: Sarthak Pandey <[email protected]>
@daniel-p-carvalho
daniel-p-carvalho deleted the fix/stm32l4-serial-setformat-ue branch October 29, 2026 21:43
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.

4 participants