aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2019-07-26 08:01:31 -0400
committerKevin O'Connor <kevin@koconnor.net>2019-07-26 08:01:31 -0400
commit899b6726fa0eada0253bccb72cab7c3b61274076 (patch)
tree4ef136d45f55f3ac0dc7eff36a2787c546ed6a56
parentf3d7287a282f205fd0914e35872b036b4c46ddf5 (diff)
downloadkutter-899b6726fa0eada0253bccb72cab7c3b61274076.tar.gz
kutter-899b6726fa0eada0253bccb72cab7c3b61274076.tar.xz
kutter-899b6726fa0eada0253bccb72cab7c3b61274076.zip
stm32f4: Improve serial baud rate calculation
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
-rw-r--r--src/stm32f4/serial.c5
1 files changed, 3 insertions, 2 deletions
diff --git a/src/stm32f4/serial.c b/src/stm32f4/serial.c
index 951a8297..525e4493 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);