This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 880dbc6380f arch/arm/stm32: disable UE in stm32_serial_m0_v3 setformat
880dbc6380f is described below

commit 880dbc6380fb65f3614069cb4debaf4fd80547cf
Author: Wooohaaa <[email protected]>
AuthorDate: Mon Nov 16 23:46:27 2026 +0800

    arch/arm/stm32: disable UE in stm32_serial_m0_v3 setformat
    
    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
---
 arch/arm/src/common/stm32/stm32_serial_m0_v3.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/arch/arm/src/common/stm32/stm32_serial_m0_v3.c 
b/arch/arm/src/common/stm32/stm32_serial_m0_v3.c
index c9bb8c8a37e..a40ffd8b447 100644
--- a/arch/arm/src/common/stm32/stm32_serial_m0_v3.c
+++ b/arch/arm/src/common/stm32/stm32_serial_m0_v3.c
@@ -892,6 +892,8 @@ static void stm32serial_setformat(struct uart_dev_s *dev)
 {
   struct stm32_serial_s *priv = (struct stm32_serial_s *)dev->priv;
   uint32_t regval;
+  uint32_t cr1_ue;
+  irqstate_t flags;
 
   /* This first implementation is for U[S]ARTs that support oversampling
    * by 8 in additional to the standard oversampling by 16.
@@ -901,6 +903,16 @@ static void stm32serial_setformat(struct uart_dev_s *dev)
   uint32_t cr1;
   uint32_t brr;
 
+  flags = enter_critical_section();
+
+  /* Disable UE: the baud rate, parity and word length bits can not be
+   * written while UE = 1.
+   */
+
+  cr1 = stm32serial_getreg(priv, STM32_USART_CR1_OFFSET);
+  cr1_ue = cr1 & USART_CR1_UE;
+  cr1 &= ~USART_CR1_UE;
+
   /* In case of oversampling by 8, the equation is:
    *
    *   baud      = 2 * fCK / usartdiv8
@@ -919,7 +931,6 @@ static void stm32serial_setformat(struct uart_dev_s *dev)
 
   /* Use oversamply by 8 only if the divisor is small.  But what is small? */
 
-  cr1 = stm32serial_getreg(priv, STM32_USART_CR1_OFFSET);
   if (usartdiv8 > 100)
     {
       /* Use usartdiv16 */
@@ -1020,6 +1031,13 @@ static void stm32serial_setformat(struct uart_dev_s *dev)
 #endif
 
   stm32serial_putreg(priv, STM32_USART_CR3_OFFSET, regval);
+
+  /* Restore UE */
+
+  regval = stm32serial_getreg(priv, STM32_USART_CR1_OFFSET);
+  stm32serial_putreg(priv, STM32_USART_CR1_OFFSET, regval | cr1_ue);
+
+  leave_critical_section(flags);
 }
 #endif /* CONFIG_SUPPRESS_UART_CONFIG */
 

Reply via email to