2018-12-06 03:25 PM
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
Solved! Go to Solution.
2018-12-07 06:34 AM
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.
*/
2018-12-06 03:56 PM
>> 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()
2018-12-07 02:23 AM
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
2018-12-07 06:11 AM
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
2018-12-07 06:29 AM
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());
2018-12-07 06:34 AM
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.
*/
2024-10-14 04:53 AM
The comment on the chips in my IDE L4,U0,G0,U5 seems incorrect and states
2./ by calling HAL API function HAL_RCC_GetHCLKFreq()
This would also be incorrect as it does not or no longer updates the global today.
2) by calling HAL API function HAL_RCC_GetSysClockFreq()
Neither HAL_RCC_GetHCLKFreq (which simply returns the global SystemCoreClock) nor HAL_RCC_GetSysClockFreq update the global SystemCoreClock. GetSysClockFreq returns the system frequency without updating the global SystemCoreClock.