cancel
Showing results for 
Search instead for 
Did you mean: 

Get the System Clock Frequency (SystemCoreClock)

mmed
Associate III

Hi everyone,

Help please,

Im using CubeMX 5.0 with STM32F407 Disco board,

I had activated the HSE clock to be the clock system with 168 MHz (HCLK on the clock tree of CubeMX).

Im tryning then to get the CPU clock, ( to introduce it in the Basic systik manipulation)

But when generating the project, I noticed that the following function;

uint32_t HAL_RCC_GetHCLKFreq(void)

{

 return SystemCoreClock;

}

with uint32_t SystemCoreClock = 16000000;

there is something wrong in this value ( it would be HCLK of the clock tree of CubeMX) or im wrong in my analyzing? Then how can i get the system clock? there is already an implemented function that return directly HCLK?

Many thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Grep the SystemCoreClock variable within stm32f4xx_hal_rcc.c

See also HAL_RCC_GetSysClockFreq() and SystemCoreClockUpdate()

 /* The SystemCoreClock variable is updated in three ways:

     1) by calling CMSIS function SystemCoreClockUpdate()

     2) by calling HAL API function HAL_RCC_GetSysClockFreq()

     3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency

        Note: If you use this function to configure the system clock; then there

              is no need to call the 2 first functions listed above, since SystemCoreClock

              variable is updated automatically.

 */

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..

View solution in original post

5 REPLIES 5

>> there is already an implemented function that return directly HCLK?

No, it has to be computed by decomposing the RCC register settings, and determining the source clocks. a speed for the HSE is provided by HSE_VALUE.

If it returns 16 MHz it would probably be because you are still running from the HSI clock the processor started with.

The value in SystemCoreClock gets recomputed when you run functions like HAL_RCC_OscConfig() or HAL_RCC_ClockConfig()

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
mmed
Associate III

Hi CLive,

Thanks for your support,

In fact, im using HSE as the clock system with 8 Mhz in input (HSE=8 Mhz). Im confused about:

uint32_t SystemCoreClock = 16000000;

I didn't know where she did come from or its significance.

So, i understand that there is no way to retrieve the CPU clock ====> there is no variable in the code gen of CubeMX that hold the CPU's clock frequency???

In fact, i want to get the CPU clock to introduce it into the Basic systik manipulation: I want to ask if the CPU clock is the same as HCLK value??? or CPU clock correspond to another parameter

My goal is to get 1us using the Basic system timer:

=======> SysTick_Config(SystemCoreClock/x); 

so in this case, what would be the value of SystemCoreClock (is that HCLK or another value )

Many thanks,

Best regards

Jack Peacock_2
Senior III

As you discovered the HSE clock value is just a hard coded number. There is no hardware feature that determines the crystal frequency directly. You can determine the actual HSE frequency if you have another reference frequency, preferably LSE but LSI can give you an approximate answer.

Assuming you have an STM32 that can use LSE or LSI as a trigger or capture source, set up a timer clocked from HSE (you may have to adjust some calculations if the PCLK for the timer source is scaled). Start the timer, with a capture channel triggered by LSE or LSI.

When the capture event occurs read the timer count. The interval between two captures provides the information you need to derive HSE, assuming you know the LSE or LSI frequency. Take several samples and average the count.

If LSE = 32.768KHz, your interval is 1/32768 or 30.518usec. For an 8MHz HSE, and no PCLK scaling, each timer tick is 125ns. So if your timer interval is in the neighborhood of 244 tick counts (30518/125) you know the HSE is close to 8MHz.

You do have to allow for tolerances in the crystals so don't rely on exact counts.

Jack Peacock

If you use the ST prescribed methods for configuring the clocks the SystemCoreClock variable will be updated with a value reflective of the current situation. The processor starts running from the 16 MHz HSI, until you change it.

You also have these methods to recompute the clocks

 printf("HCLK=%d\n", HAL_RCC_GetHCLKFreq());

 printf("APB1=%d\n", HAL_RCC_GetPCLK1Freq());

 printf("APB2=%d\n", HAL_RCC_GetPCLK2Freq());

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..

Grep the SystemCoreClock variable within stm32f4xx_hal_rcc.c

See also HAL_RCC_GetSysClockFreq() and SystemCoreClockUpdate()

 /* The SystemCoreClock variable is updated in three ways:

     1) by calling CMSIS function SystemCoreClockUpdate()

     2) by calling HAL API function HAL_RCC_GetSysClockFreq()

     3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency

        Note: If you use this function to configure the system clock; then there

              is no need to call the 2 first functions listed above, since SystemCoreClock

              variable is updated automatically.

 */

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..