Generating Defines from CubeMX Settings
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-15 4:49 PM
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!
Solved! Go to Solution.
- Labels:
-
STM32CubeMX
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-15 4:57 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-15 4:57 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-15 5:49 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-08-15 10:20 PM
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
