diff --git a/lib/stm32/common/usart_common_all.c b/lib/stm32/common/usart_common_all.c index 4865ef1f..2c7e9a6d 100644 --- a/lib/stm32/common/usart_common_all.c +++ b/lib/stm32/common/usart_common_all.c @@ -42,6 +42,9 @@ USART1/6) or the APB low-speed prescaler clock (for other USARTs). These values must be correctly set before calling this function (refer to the rcc_clock_setup-* functions in RCC). +Note: For LPUART, baudrates over 2**24 (~16.7 Mbaud) may overflow +the calculation and are therefore not supported by this function. + @param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base @param[in] baud unsigned 32 bit. Baud rate specified in Hz. @@ -70,7 +73,15 @@ void usart_set_baudrate(uint32_t usart, uint32_t baud) * Note: We round() the value rather than floor()ing it, for more * accurate divisor selection. */ - USART_BRR(usart) = ((2 * clock) + baud) / (2 * baud); +#ifdef LPUART1 + if (usart == LPUART1) { + USART_BRR(usart) = (clock / baud) * 256 + + ((clock % baud) * 256 + baud / 2) / baud; + return; + } +#endif + + USART_BRR(usart) = (clock + baud / 2) / baud; } /*---------------------------------------------------------------------------*/