HAL_RCC_GetSysClockFrequency shows different frequency to STM32Cube clock config tool
Using the STM32Cube with a STM32F4, I set the SYSCLK to 96Mhz
Which generates this code
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 96;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
However, if I call HAL_RCC_GetSysClockFreq directly after SystemClock_Config() e.g.
SystemClock_Config();
clkF = HAL_RCC_GetSysClockFreq();
clkF is 72000000 i.e 72Mhz
I've checked that HSE_VALUE is correctly defined as 8000000
So I made a copy of HAL_RCC_GetSysClockFreq() and added some debugging, which produced these values
{
uint32_t pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
uint32_t plln = (RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> RCC_PLLCFGR_PLLN_Pos;
uint32_t pllvco = ((int64_t)HSE_VALUE * (int64_t)plln / (int64_t)pllm);
uint32_t pllp = ((((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> RCC_PLLCFGR_PLLP_Pos) + 1U) *2U);
uint32_t sysclockfreq = pllvco/pllp;
USB_DEBUG_printf("HSE:%d pllm:%d plln:%d pllvco:%d pllp:%d SYSCLK:%d\n",HSE_VALUE,pllm,plln,pllvco,pllp,sysclockfreq);
}
HSE:8000000 pllm:8 plln:288 pllvco:288000000 pllp:4 SYSCLK:72000000
So PLLM is being read from the hardware as a different value to what has been requested, and PLLN value is something totally different.
With the result that the SYSCLK is either wrong, or being wrongly calculated by HAL_RCC_GetSysClockFreq
I've looked at the frequency of some of the PWM outputs that I'm using, and it does appear that the processor is running at 72Mhz not 96Mhz
Yet the Timer 14 millisecond tick interrupt does seem to be the correct frequency
Hence I'm confused about whats going on :(
