Electronic – Why does `RCC_GetClocksFreq` return different results than I expect

armclockstm32stm32f4

I have a STM32F427 MCU with a 8MHz HSE crystal. I am setting up the clock as follows:

#include <stm32f4xx.h>
#include <stm32f4xx_rcc.h>

void initClocks(void);

void initClocks(void) {
    /*
    HSE: 8 MHz
    PLL: HSE / 8 * 192 => 192 MHz
    SysCLK:  PLL / 2 => 96 MHz
    PrphCLK: PLL / 4 => 48 MHz
    */
    RCC_PLLConfig(RCC_PLLSource_HSE, 8, 192, 2, 4);
    RCC_PLLCmd(ENABLE);

    // Wait
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) continue;

    // Use PLL
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    while (RCC_GetSYSCLKSource() != 0x08) continue;

    RCC_HCLKConfig(RCC_SYSCLK_Div1);
    RCC_PCLK2Config(RCC_SYSCLK_Div1);
    RCC_PCLK1Config(RCC_SYSCLK_Div1);
}

int main(void) {
    RCC_ClocksTypeDef ClksFreq;

    initClocks();
    SystemCoreClockUpdate();

    RCC_GetClocksFreq(&ClksFreq);

    while(1) {}
}

HSE_VALUE is defined to 8000000

My debugger shows that SYSCLK in ClksFreq is 57.6MHz, though I would expect it to be 96Mhz.

What am I missing?

Best Answer

I managed to fix my problem by adding the following near the beginning:

RCC_HSEConfig(RCC_HSE_ON);

I had not started the HSE so most of the configuration was propably just ignored as the core still worked.

As a sidenote also the HCLK, PCLK1 and PCLK2 divisions were incorrect (over the frequency limit.) I had not spent so much attention on those as I tried to find out why SYSCLK was not getting set correctly.