Skip to main content
FColl.1
Associate III
Oct 29, 2021 3:24 AM UTC
Solved

How to read clock frequencies from code? Even a #define for the hardcoded startup values would be useful.

CubeMX allows one to enter clock frequencies and have all the registers to be set back calculated. Great! This saves much fiddling. However... is there anyway for the code to determine, for example, what the frequency if a clock going to the ADC is without doing all those calculations that CubeMX saved us from?

This topic has been closed for replies.
TDK
TDKBest answer
Oct 29, 2021 3:29 AM UTC

There are functions like HAL_RCC_GetPCLK1Freq that you can call to calculate them. There are no hardcoded #defined values.

Per CMSIS standard, the SystemCoreClock variable reflects the current system core clock frequency and is updated when the clock changes.

"If you feel a post has answered your question, please click ""Accept as Solution""."

3 replies

TDK
TDKBest answer
Oct 29, 2021 3:29 AM UTC

There are functions like HAL_RCC_GetPCLK1Freq that you can call to calculate them. There are no hardcoded #defined values.

Per CMSIS standard, the SystemCoreClock variable reflects the current system core clock frequency and is updated when the clock changes.

"If you feel a post has answered your question, please click ""Accept as Solution""."
FColl.1
FColl.1Author
Associate III
Oct 31, 2021 10:09 PM UTC

Thanks! I found   HAL_RCCEx_GetPLL2ClockFreq(); Which gives me the ADC input freq. Very useful.

Do you know of a function that will calculate ADC sample rate? It would need to wrap up, conversion time, oversampling , hold time, prescaler etc.

TDK
Oct 31, 2021 10:15 PM UTC
There isn’t a function that does that. It would be difficult given all the different ways to set up the ADC.
"If you feel a post has answered your question, please click ""Accept as Solution""."