0
\$\begingroup\$

I am trying to change the STM32F407 system core clock to be 100 MHz. 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 is a screenshot of the STMCube that shows the right values of PLL M, N, P, Q:

STMCube clock configuration

Here is my function

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

    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.

\$\endgroup\$
6
  • \$\begingroup\$ What external oscillators are on the board? Which one is connected to HSE? \$\endgroup\$
    – user16324
    Commented Dec 13, 2015 at 13:11
  • \$\begingroup\$ @BrianDrummond 8Mhz \$\endgroup\$ Commented Dec 13, 2015 at 15:18
  • 1
    \$\begingroup\$ Why don't you compare with the built code from MxCube? \$\endgroup\$ Commented Dec 13, 2015 at 16:59
  • \$\begingroup\$ 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. \$\endgroup\$
    – zupazt3
    Commented Dec 13, 2015 at 19:42
  • \$\begingroup\$ @zupazt3 I'am working with standard peripheral library and not with the HAL library !! \$\endgroup\$ Commented Dec 14, 2015 at 0:17

1 Answer 1

1
\$\begingroup\$

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 in fact 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 initializing the clock system using the function:

 RCC_DeInit();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.