# Setting stm32f4 SystemCoreClock to 100MHz but can't get it (only get 57.6MHz)

I am triying to change the stm32f407 systeme core clock to be 100MHz. To do this I need to set the source of the PLL to be HSE, and configure the PLL coefficient so as to get the right value of SYSCLK.

Here a screenshot of the stmcube that chows the right values of pll M, N, P, Q.

Here is my function

    void rcc_clk_config() // function to configure the rc clk for running on HSE : sysclk at 100MHZ
{
RCC_PLLConfig(RCC_PLLSource_HSE, 4, 200, 4, 4);//sysclck to 100mhz , systick to 12.5 mhz

RCC_HCLKConfig(RCC_SYSCLK_Div1);

RCC_PCLK1Config(RCC_HCLK_Div4);

RCC_PCLK2Config(RCC_HCLK_Div2);

do{
RCC_HSEConfig(RCC_HSE_ON);;

}

while(RCC_GetFlagStatus(RCC_FLAG_HSERDY)!=SET);

do{
RCC_PLLCmd(ENABLE);

}

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)!=SET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
SystemCoreClockUpdate();

}


In the debug session I get SystemCoreClock = 57.600.000.

Edit: it works now, here is the new code

{
RCC_DeInit();
do{
RCC_HSEConfig(RCC_HSE_ON);
}
while (!RCC_WaitForHSEStartUp());
RCC_PLLConfig(RCC_PLLSource_HSE, 4, 200, 4, 4);//100MHZ

do{
RCC_PLLCmd(ENABLE);
}

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)!=SET);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div4);
RCC_PCLK2Config(RCC_HCLK_Div2);

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
SystemCoreClockUpdate();
}

• What external oscillators are on the board? Which one is connected to HSE? – Brian Drummond Dec 13 '15 at 13:11
• @BrianDrummond 8Mhz – werber bang Dec 13 '15 at 15:18
• Why don't you compare with the built code from MxCube? – Marko Buršič Dec 13 '15 at 16:59
• As Marko Buršič said: in top menu click "Project" -> "Generate code". Choose directory and there you will have a ready-to-use function which sets system clocks. If you want to write it yourself, then compare generated one with yours. – zupazt3 Dec 13 '15 at 19:42
• You should post your solution as an answer rather than as an edit. And don't just post the working code, describe the key change in words. – Chris Stratton Dec 17 '15 at 13:11

it works now, here is the new code

{
RCC_DeInit();//a must
do{
RCC_HSEConfig(RCC_HSE_ON);
}
while (!RCC_WaitForHSEStartUp());
RCC_PLLConfig(RCC_PLLSource_HSE, 4, 200, 4, 4);//100MHZ

do{
RCC_PLLCmd(ENABLE);
}

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)!=SET);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div4);
RCC_PCLK2Config(RCC_HCLK_Div2);

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
SystemCoreClockUpdate();
}


with some experimentation and some more time reading the stm32f4xx_rcc.c , i discovered there was infact a function : RCC_WaitForHSEStartUp() ,which role is to wait for the HSE oscillator to start , i used it as follows

do{
RCC_HSEConfig(RCC_HSE_ON);
}
while (!RCC_WaitForHSEStartUp());


to replace in the previous version

do{
RCC_HSEConfig(RCC_HSE_ON);;

}

while(RCC_GetFlagStatus(RCC_FLAG_HSERDY)!=SET);


i also "had" to start the code by initialising the clock system using the function

 RCC_DeInit();