Repository navigation
arch/arm/src/stm32l4: disable the USART while changing the line format - #20914
Merged
michallenc merged 2 commits intoOct 23, 2026
Merged
Conversation
daniel-p-carvalho
force-pushed
the
fix/stm32l4-serial-setformat-ue
branch
from
October 23, 2026 04:01
c8ccff4 to
c1f71b1
Compare
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]>
xiaoxiang781216
previously approved these changes
Oct 23, 2026
daniel-p-carvalho
force-pushed
the
fix/stm32l4-serial-setformat-ue
branch
from
October 23, 2026 06:16
c1f71b1 to
73d99f2
Compare
xiaoxiang781216
approved these changes
Oct 23, 2026
|
jerpelea
approved these changes
Oct 23, 2026
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]>
This was referenced Nov 16, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.