Skip to content

Commit 232d3eb

Browse files
committed
drivers: serial: stm32: return error for bad baud
The uart_stm32 driver gives no way for a user to tell if setting a new baud rate was successful. Propagate error checks up to the API level. Signed-off-by: Eden Frosst <[email protected]>
1 parent a10f807 commit 232d3eb

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

drivers/serial/uart_stm32.c

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static void uart_stm32_pm_policy_state_lock_put(const struct device *dev)
140140
}
141141
#endif /* CONFIG_PM */
142142

143-
static inline void uart_stm32_set_baudrate(const struct device *dev, uint32_t baud_rate)
143+
static inline int uart_stm32_set_baudrate(const struct device *dev, uint32_t baud_rate)
144144
{
145145
const struct uart_stm32_config *config = dev->config;
146146
USART_TypeDef *usart = config->usart;
@@ -150,18 +150,18 @@ static inline void uart_stm32_set_baudrate(const struct device *dev, uint32_t ba
150150

151151
/* Get clock rate */
152152
if (IS_ENABLED(STM32_UART_DOMAIN_CLOCK_SUPPORT) && (config->pclk_len > 1)) {
153-
if (clock_control_get_rate(data->clock,
154-
(clock_control_subsys_t)&config->pclken[1],
155-
&clock_rate) < 0) {
153+
int ret = clock_control_get_rate(
154+
data->clock, (clock_control_subsys_t)&config->pclken[1], &clock_rate);
155+
if (ret < 0) {
156156
LOG_ERR("Failed call clock_control_get_rate(pclken[1])");
157-
return;
157+
return ret;
158158
}
159159
} else {
160-
if (clock_control_get_rate(data->clock,
161-
(clock_control_subsys_t)&config->pclken[0],
162-
&clock_rate) < 0) {
160+
int ret = clock_control_get_rate(
161+
data->clock, (clock_control_subsys_t)&config->pclken[0], &clock_rate);
162+
if (ret < 0) {
163163
LOG_ERR("Failed call clock_control_get_rate(pclken[0])");
164-
return;
164+
return ret;
165165
}
166166
}
167167

@@ -181,7 +181,7 @@ static inline void uart_stm32_set_baudrate(const struct device *dev, uint32_t ba
181181

182182
if (presc_idx == ARRAY_SIZE(LPUART_PRESCALER_TAB)) {
183183
LOG_ERR("Unable to set %s to %d", dev->name, baud_rate);
184-
return;
184+
return -EINVAL;
185185
}
186186

187187
presc_val = presc_idx << USART_PRESC_PRESCALER_Pos;
@@ -191,7 +191,7 @@ static inline void uart_stm32_set_baudrate(const struct device *dev, uint32_t ba
191191
lpuartdiv = lpuartdiv_calc(clock_rate, baud_rate);
192192
if (lpuartdiv < LPUART_BRR_MIN_VALUE || lpuartdiv > LPUART_BRR_MASK) {
193193
LOG_ERR("Unable to set %s to %d", dev->name, baud_rate);
194-
return;
194+
return -EINVAL;
195195
}
196196
#endif /* USART_PRESC_PRESCALER */
197197
LL_LPUART_SetBaudRate(usart,
@@ -229,6 +229,7 @@ static inline void uart_stm32_set_baudrate(const struct device *dev, uint32_t ba
229229
#if HAS_LPUART
230230
}
231231
#endif /* HAS_LPUART */
232+
return 0;
232233
}
233234

234235
static inline void uart_stm32_set_parity(const struct device *dev,
@@ -487,7 +488,7 @@ static inline enum uart_config_flow_control uart_stm32_ll2cfg_hwctrl(uint32_t fc
487488
return UART_CFG_FLOW_CTRL_NONE;
488489
}
489490

490-
static void uart_stm32_parameters_set(const struct device *dev,
491+
static int uart_stm32_parameters_set(const struct device *dev,
491492
const struct uart_config *cfg)
492493
{
493494
const struct uart_stm32_config *config = dev->config;
@@ -501,6 +502,7 @@ static void uart_stm32_parameters_set(const struct device *dev,
501502
#if HAS_DRIVER_ENABLE
502503
bool driver_enable = cfg->flow_ctrl == UART_CFG_FLOW_CTRL_RS485;
503504
#endif
505+
int ret = 0;
504506

505507
if (cfg == uart_cfg) {
506508
/* Called via (re-)init function, so the SoC either just booted,
@@ -512,7 +514,10 @@ static void uart_stm32_parameters_set(const struct device *dev,
512514
parity,
513515
stopbits);
514516
uart_stm32_set_hwctrl(dev, flowctrl);
515-
uart_stm32_set_baudrate(dev, cfg->baudrate);
517+
ret = uart_stm32_set_baudrate(dev, cfg->baudrate);
518+
if (ret < 0) {
519+
return ret;
520+
}
516521
} else {
517522
/* Called from application/subsys via uart_configure syscall */
518523
if (parity != uart_stm32_get_parity(dev)) {
@@ -538,10 +543,15 @@ static void uart_stm32_parameters_set(const struct device *dev,
538543
#endif
539544

540545
if (cfg->baudrate != uart_cfg->baudrate) {
541-
uart_stm32_set_baudrate(dev, cfg->baudrate);
546+
ret = uart_stm32_set_baudrate(dev, cfg->baudrate);
547+
if (ret < 0) {
548+
return ret;
549+
}
542550
uart_cfg->baudrate = cfg->baudrate;
543551
}
544552
}
553+
554+
return 0;
545555
}
546556

547557
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
@@ -598,10 +608,14 @@ static int uart_stm32_configure(const struct device *dev,
598608
LL_USART_Disable(usart);
599609

600610
/* Set basic parameters, such as data-/stop-bit, parity, and baudrate */
601-
uart_stm32_parameters_set(dev, cfg);
611+
int ret = uart_stm32_parameters_set(dev, cfg);
602612

603613
LL_USART_Enable(usart);
604614

615+
if (ret < 0) {
616+
return -ENOTSUP;
617+
}
618+
605619
/* Upon successful configuration, persist the syscall-passed
606620
* uart_config.
607621
* This allows restoring it, should the device return from a low-power
@@ -2090,7 +2104,9 @@ static int uart_stm32_registers_configure(const struct device *dev)
20902104
LL_USART_SetTransferDirection(usart, LL_USART_DIRECTION_TX_RX);
20912105

20922106
/* Set basic parameters, such as data-/stop-bit, parity, and baudrate */
2093-
uart_stm32_parameters_set(dev, uart_cfg);
2107+
if (uart_stm32_parameters_set(dev, uart_cfg) < 0) {
2108+
return -EINVAL;
2109+
}
20942110

20952111
/* Enable the single wire / half-duplex mode */
20962112
if (config->single_wire) {

0 commit comments

Comments
 (0)