@@ -459,9 +459,8 @@ static inline enum uart_config_flow_control uart_stm32_ll2cfg_hwctrl(uint32_t fc
459459 return UART_CFG_FLOW_CTRL_NONE ;
460460}
461461
462- #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
463- static int uart_stm32_configure (const struct device * dev ,
464- const struct uart_config * cfg )
462+ static void uart_stm32_parameters_set (const struct device * dev ,
463+ const struct uart_config * cfg )
465464{
466465 const struct uart_stm32_config * config = dev -> config ;
467466 struct uart_stm32_data * data = dev -> data ;
@@ -475,6 +474,60 @@ static int uart_stm32_configure(const struct device *dev,
475474 bool driver_enable = cfg -> flow_ctrl == UART_CFG_FLOW_CTRL_RS485 ;
476475#endif
477476
477+ if (cfg == uart_cfg ) {
478+ /* Called via (re-)init function, so the SoC either just booted,
479+ * or is returning from a low-power state where it lost register
480+ * contents
481+ */
482+ LL_USART_ConfigCharacter (config -> usart ,
483+ databits ,
484+ parity ,
485+ stopbits );
486+ uart_stm32_set_hwctrl (dev , flowctrl );
487+ uart_stm32_set_baudrate (dev , cfg -> baudrate );
488+ } else {
489+ /* Called from application/subsys via uart_configure syscall */
490+ if (parity != uart_stm32_get_parity (dev )) {
491+ uart_stm32_set_parity (dev , parity );
492+ }
493+
494+ if (stopbits != uart_stm32_get_stopbits (dev )) {
495+ uart_stm32_set_stopbits (dev , stopbits );
496+ }
497+
498+ if (databits != uart_stm32_get_databits (dev )) {
499+ uart_stm32_set_databits (dev , databits );
500+ }
501+
502+ if (flowctrl != uart_stm32_get_hwctrl (dev )) {
503+ uart_stm32_set_hwctrl (dev , flowctrl );
504+ }
505+
506+ #if HAS_DRIVER_ENABLE
507+ if (driver_enable != uart_stm32_get_driver_enable (dev )) {
508+ uart_stm32_set_driver_enable (dev , driver_enable );
509+ }
510+ #endif
511+
512+ if (cfg -> baudrate != uart_cfg -> baudrate ) {
513+ uart_stm32_set_baudrate (dev , cfg -> baudrate );
514+ uart_cfg -> baudrate = cfg -> baudrate ;
515+ }
516+ }
517+ }
518+
519+ #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
520+ static int uart_stm32_configure (const struct device * dev ,
521+ const struct uart_config * cfg )
522+ {
523+ const struct uart_stm32_config * config = dev -> config ;
524+ struct uart_stm32_data * data = dev -> data ;
525+ struct uart_config * uart_cfg = data -> uart_cfg ;
526+ const uint32_t parity = uart_stm32_cfg2ll_parity (cfg -> parity );
527+ const uint32_t stopbits = uart_stm32_cfg2ll_stopbits (config , cfg -> stop_bits );
528+ const uint32_t databits = uart_stm32_cfg2ll_databits (cfg -> data_bits ,
529+ cfg -> parity );
530+
478531 /* Hardware doesn't support mark or space parity */
479532 if ((cfg -> parity == UART_CFG_PARITY_MARK ) ||
480533 (cfg -> parity == UART_CFG_PARITY_SPACE )) {
@@ -515,34 +568,18 @@ static int uart_stm32_configure(const struct device *dev,
515568
516569 LL_USART_Disable (config -> usart );
517570
518- if (parity != uart_stm32_get_parity (dev )) {
519- uart_stm32_set_parity (dev , parity );
520- }
571+ /* Set basic parmeters, such as data-/stop-bit, parity, and baudrate */
572+ uart_stm32_parameters_set (dev , cfg );
521573
522- if (stopbits != uart_stm32_get_stopbits (dev )) {
523- uart_stm32_set_stopbits (dev , stopbits );
524- }
525-
526- if (databits != uart_stm32_get_databits (dev )) {
527- uart_stm32_set_databits (dev , databits );
528- }
529-
530- if (flowctrl != uart_stm32_get_hwctrl (dev )) {
531- uart_stm32_set_hwctrl (dev , flowctrl );
532- }
533-
534- #if HAS_DRIVER_ENABLE
535- if (driver_enable != uart_stm32_get_driver_enable (dev )) {
536- uart_stm32_set_driver_enable (dev , driver_enable );
537- }
538- #endif
574+ LL_USART_Enable (config -> usart );
539575
540- if (cfg -> baudrate != uart_cfg -> baudrate ) {
541- uart_stm32_set_baudrate (dev , cfg -> baudrate );
542- uart_cfg -> baudrate = cfg -> baudrate ;
543- }
576+ /* Upon successful configuration, persist the syscall-passed
577+ * uart_config.
578+ * This allows restoring it, should the device return from a low-power
579+ * mode in which register contents are lost.
580+ */
581+ * uart_cfg = * cfg ;
544582
545- LL_USART_Enable (config -> usart );
546583 return 0 ;
547584};
548585
@@ -1843,8 +1880,6 @@ static int uart_stm32_registers_configure(const struct device *dev)
18431880 const struct uart_stm32_config * config = dev -> config ;
18441881 struct uart_stm32_data * data = dev -> data ;
18451882 struct uart_config * uart_cfg = data -> uart_cfg ;
1846- uint32_t ll_parity ;
1847- uint32_t ll_datawidth ;
18481883
18491884 LL_USART_Disable (config -> usart );
18501885
@@ -1860,39 +1895,8 @@ static int uart_stm32_registers_configure(const struct device *dev)
18601895 LL_USART_SetTransferDirection (config -> usart ,
18611896 LL_USART_DIRECTION_TX_RX );
18621897
1863- /* Determine the datawidth and parity. If we use other parity than
1864- * 'none' we must use datawidth = 9 (to get 8 databit + 1 parity bit).
1865- */
1866- if (uart_cfg -> parity == 2 ) {
1867- /* 8 databit, 1 parity bit, parity even */
1868- ll_parity = LL_USART_PARITY_EVEN ;
1869- ll_datawidth = LL_USART_DATAWIDTH_9B ;
1870- } else if (uart_cfg -> parity == 1 ) {
1871- /* 8 databit, 1 parity bit, parity odd */
1872- ll_parity = LL_USART_PARITY_ODD ;
1873- ll_datawidth = LL_USART_DATAWIDTH_9B ;
1874- } else { /* Default to 8N0, but show warning if invalid value */
1875- if (uart_cfg -> parity != 0 ) {
1876- LOG_WRN ("Invalid parity setting '%d'."
1877- "Defaulting to 'none'." , uart_cfg -> parity );
1878- }
1879- /* 8 databit, parity none */
1880- ll_parity = LL_USART_PARITY_NONE ;
1881- ll_datawidth = LL_USART_DATAWIDTH_8B ;
1882- }
1883-
1884- /* Set datawidth and parity, 1 start bit, 1 stop bit */
1885- LL_USART_ConfigCharacter (config -> usart ,
1886- ll_datawidth ,
1887- ll_parity ,
1888- LL_USART_STOPBITS_1 );
1889-
1890- if (uart_cfg -> flow_ctrl ) {
1891- uart_stm32_set_hwctrl (dev , LL_USART_HWCONTROL_RTS_CTS );
1892- }
1893-
1894- /* Set the default baudrate */
1895- uart_stm32_set_baudrate (dev , uart_cfg -> baudrate );
1898+ /* Set basic parmeters, such as data-/stop-bit, parity, and baudrate */
1899+ uart_stm32_parameters_set (dev , uart_cfg );
18961900
18971901 /* Enable the single wire / half-duplex mode */
18981902 if (config -> single_wire ) {
0 commit comments