From 899b6726fa0eada0253bccb72cab7c3b61274076 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Fri, 26 Jul 2019 08:01:31 -0400 Subject: [PATCH] stm32f4: Improve serial baud rate calculation Signed-off-by: Kevin O'Connor --- src/stm32f4/serial.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/stm32f4/serial.c b/src/stm32f4/serial.c index 951a82979..525e44939 100644 --- a/src/stm32f4/serial.c +++ b/src/stm32f4/serial.c @@ -21,8 +21,9 @@ serial_init(void) enable_pclock(USART2_BASE); uint32_t pclk = get_pclock_frequency(USART2_BASE); - uint32_t div = pclk / (CONFIG_SERIAL_BAUD * 16); - USART2->BRR = div << USART_BRR_DIV_Mantissa_Pos; + uint32_t div = DIV_ROUND_CLOSEST(pclk, CONFIG_SERIAL_BAUD); + USART2->BRR = (((div / 16) << USART_BRR_DIV_Mantissa_Pos) + | ((div % 16) << USART_BRR_DIV_Fraction_Pos)); USART2->CR1 = CR1_FLAGS; NVIC_SetPriority(USART2_IRQn, 0); NVIC_EnableIRQ(USART2_IRQn);