cancel
Showing results for 
Search instead for 
Did you mean: 

Generating Defines from CubeMX Settings

PSquared
Associate

Is it possible to generate #define from project parameters in CubeMX, such that they are produced in a header file my user code can reference?

One example (of many possible ones) would be the HCLK frequency. I see MX generates the proper code to configure the RCC to generate the proper frequency, and at runtime I suppose I could look at how it's configured and do the math. but It'd be quite convenient, and a lot simpler, to be able to reference this frequency.

Something like "#define HCLK_FREQ_Hz 216000000" pushed to main.h would be useful, so user does not need to maintain information both in MX and separately in their source code.

Is there any way to get information that MX knows out into user code? Some sort of wildcard or symbol I can plug into the value of a user define to reference an internal variable, or something of the like?

Any help or guidance here would be appreciated. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

It's just not set up to do this.

You can call HAL_RCC_GetHCLKFreq to get HCLK frequency. There are similar calls for other clocks. Since they read the actual registers, it will always be correct (provided your HSE_VALUE is accurate), unlike a define which will only be correct after the clock is configured.

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

View solution in original post

3 REPLIES 3
TDK
Guru

It's just not set up to do this.

You can call HAL_RCC_GetHCLKFreq to get HCLK frequency. There are similar calls for other clocks. Since they read the actual registers, it will always be correct (provided your HSE_VALUE is accurate), unlike a define which will only be correct after the clock is configured.

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

Thanks. I suspected this would be the case, but figured I'd ask the hive mind here anyway. There are some conveniences in having a preprocessor definition in cases where you need to optimize runtime, but you are 100% right in that reading the configuration of the part will be more universally applicable, and correct in many cases.

Thanks again for your quick reply and your help. 

Hello

Note that you can manually add definitions to cubeMX 'user constant' page of each module and those are then defined in main.h. Might be useful in some cases.

Br J.T