From 7b958032184273957c73c395b3701dd1cf230f10 Mon Sep 17 00:00:00 2001 From: Francois Ramu Date: Thu, 8 Sep 2022 17:26:29 +0200 Subject: [PATCH] drivers: serial: stm32 uart check baudrate register value The baudrate register (BRR) of a stm32 USART cannot be lower than 16. The baudrate register (BRR) of a stm32 LPUART cannot be lower than 0x300 and greater than 0xFFFFF. Add assertion to check the range. This could be the case when configuring a baudrate of 9600 on usart clocked by LSE (32768Hz). Signed-off-by: Francois Ramu --- drivers/serial/uart_stm32.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/serial/uart_stm32.c b/drivers/serial/uart_stm32.c index 6a3e5b3fc48..344f7d74161 100644 --- a/drivers/serial/uart_stm32.c +++ b/drivers/serial/uart_stm32.c @@ -159,6 +159,13 @@ static inline void uart_stm32_set_baudrate(const struct device *dev, uint32_t ba presc_val, #endif baud_rate); + /* Check BRR is greater than or equal to 0x300 */ + __ASSERT(LL_LPUART_ReadReg(config->usart, BRR) >= 0x300U, + "BaudRateReg >= 0x300"); + + /* Check BRR is lower than or equal to 0xFFFFF */ + __ASSERT(LL_LPUART_ReadReg(config->usart, BRR) < 0x000FFFFFU, + "BaudRateReg < 0xFFFF"); } else { #endif /* HAS_LPUART_1 */ #ifdef USART_CR1_OVER8 @@ -174,6 +181,9 @@ static inline void uart_stm32_set_baudrate(const struct device *dev, uint32_t ba LL_USART_OVERSAMPLING_16, #endif baud_rate); + /* Check BRR is greater than or equal to 16d */ + __ASSERT(LL_USART_ReadReg(config->usart, BRR) > 16, + "BaudRateReg >= 16"); #if HAS_LPUART_1 }