Skip to main content
Sego2
Associate II
Apr 22, 2022 2:13 PM UTC
Solved

Customizing STM32 output

Hello,

I have seen that it is possible to use templates to modify the output code generated by STM32CubeMX.

However, I'm having difficulty to get some examples.

Also, I was wondering if with the help of templates the two following points can be achieved:

1.- Peripherals like timers init function can accept arguments and not just be:

e.i. MX_TIM1_Init

2.- If main function can be renamed, in order for me to use the autogenerated code on several projects with others main function. Inother words only use STM32CubeMX to configure hardware.

Thank you,

Best regards !

This topic has been closed for replies.
KnarfB
KnarfBBest answer
Super User
Apr 24, 2022 11:50 AM UTC

Think it works, don't see a catch here.

a) click the gear icon close to a numerical parameter and set the "no check" option. Now enter whatever you want and it will be copied to the generated code, e.g. enter a new id BAUDRATE and define it elsewhere in your code. or

b) define a User Constant BAUDRATE and use it in the Parameter Settings. The definition goes to main.h. You can re-define it in user code as you like

hth

KnarfB

5 replies

Piranha
Principal III
Apr 23, 2022 9:53 AM UTC

CubeMX is already a template based generator of a (broken) C code and is practically unmanageable. Adding one more layer of regeneration, of course, will make the whole thing better and more manageable. But if it doesn't, just add another layer! ;)

KnarfB
Super User
Apr 23, 2022 10:40 AM UTC

Don't know about Init parameters, but you can define User Constants in CubeMX which are preprocessor defines, defined in main.h (or elsewhere in your setting) and used in *_Init() functions.

In the .ioc View > Project Mananger > Project tab, there is a checkbox "Do not generate the main()" which does what is says.

hth

KnarfB

Pavel A.
Apr 23, 2022 11:55 PM UTC

> you can define User Constants in CubeMX which are preprocessor defines

This feature looks tempting but proved sour.

People already complained about confusing substitution of GPIO pin identifiers.

I also thought of using defines for something like UART baudrate:

#define UART_BAUDRATE 9600

But I do not know how to make Cube to use this macro for UART init.

Not going to dig in the templates, which will be overwritten by next Cube update.

So - rewriting the Cube-generated code. After changes in the .ioc and re-generation, doing 3-way merge. Not easy, yes.

The User Constants in CubeMX are useful to define things such as board variant or schema version, for common source modules.

KnarfB
KnarfBBest answer
Super User
Apr 24, 2022 11:50 AM UTC

Think it works, don't see a catch here.

a) click the gear icon close to a numerical parameter and set the "no check" option. Now enter whatever you want and it will be copied to the generated code, e.g. enter a new id BAUDRATE and define it elsewhere in your code. or

b) define a User Constant BAUDRATE and use it in the Parameter Settings. The definition goes to main.h. You can re-define it in user code as you like

hth

KnarfB