From 009dd8778d3ca181d6e6ec7f3b7e99337a8ea5c2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 27 Aug 2017 21:55:04 +1000 Subject: [PATCH] F4_HAL/rcc: Adjust computation of SYSCLK to retain precision. --- STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c b/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c index 3f19ced..8543455 100644 --- a/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c +++ b/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c @@ -891,7 +891,12 @@ __weak uint32_t HAL_RCC_GetSysClockFreq(void) if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI) { /* HSE used as PLL clock source */ - pllvco = ((HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN))); + //pllvco = ((HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN))); + // dpgeorge: Adjust the way the arithmetic is done so it retains + // precision for the case that pllm doesn't evenly divide HSE_VALUE. + // Must be sure not to overflow, so divide by 4 first. HSE_VALUE + // should be a multiple of 4 (being a multiple of 100 is enough). + pllvco = ((HSE_VALUE / 4) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN))) / pllm * 4; } else {