From 2ac09c2694f89b11544f4abf10413c64eeec2b15 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 13 Apr 2021 23:56:37 +1000 Subject: [PATCH] stm32/uart: Use LL_USART_GetBaudRate to compute baudrate. This function includes the UART prescaler in the calculation (if it has one, eg on H7 and WB MCUs). Signed-off-by: Damien George --- ports/stm32/uart.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index d40a883c52..36c59cee4a 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -711,16 +711,11 @@ uint32_t uart_get_source_freq(pyb_uart_obj_t *self) { } uint32_t uart_get_baudrate(pyb_uart_obj_t *self) { - // This formula assumes UART_OVERSAMPLING_16 - uint32_t source_freq = uart_get_source_freq(self); - #if defined(LPUART1) - if (self->uart_id == PYB_LPUART_1) { - return source_freq / (self->uartx->BRR >> 8); - } else - #endif - { - return source_freq / self->uartx->BRR; - } + return LL_USART_GetBaudRate(self->uartx, uart_get_source_freq(self), + #if defined(STM32H7) || defined(STM32WB) + self->uartx->PRESC, + #endif + LL_USART_OVERSAMPLING_16); } void uart_set_baudrate(pyb_uart_obj_t *self, uint32_t baudrate) {