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

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

commit 47180bf5ea00a94b41cf2083b8fed123199e5ce9
Author: Daniel P. Carvalho <[email protected]>
AuthorDate: Fri Oct 23 00:54:01 2026 -0300

    arch/arm/src/stm32l4: disable the USART while changing the line format
    
    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]>
---
 arch/arm/src/stm32l4/stm32l4_serial.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/arch/arm/src/stm32l4/stm32l4_serial.c 
b/arch/arm/src/stm32l4/stm32l4_serial.c
index b57e3e9835b..a063574b28b 100644
--- a/arch/arm/src/stm32l4/stm32l4_serial.c
+++ b/arch/arm/src/stm32l4/stm32l4_serial.c
@@ -1118,6 +1118,19 @@ static void stm32l4serial_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;
+
+  flags = enter_critical_section();
+
+  /* Disable UE: the baud rate, parity and word length bits can not be
+   * written while UE = 1.
+   */
+
+  regval = stm32l4serial_getreg(priv, STM32_USART_CR1_OFFSET);
+  cr1_ue = regval & USART_CR1_UE;
+  stm32l4serial_putreg(priv, STM32_USART_CR1_OFFSET,
+                       regval & ~USART_CR1_UE);
 
   /* Set baud rate */
 
@@ -1206,6 +1219,13 @@ static void stm32l4serial_setformat(struct uart_dev_s 
*dev)
 #endif
 
   stm32l4serial_putreg(priv, STM32_USART_CR3_OFFSET, regval);
+
+  /* Restore UE */
+
+  regval = stm32l4serial_getreg(priv, STM32_USART_CR1_OFFSET);
+  stm32l4serial_putreg(priv, STM32_USART_CR1_OFFSET, regval | cr1_ue);
+
+  leave_critical_section(flags);
 }
 #endif /* CONFIG_SUPPRESS_UART_CONFIG */
 

Reply via email to