cancel
Showing results for 
Search instead for 
Did you mean: 

Customizing STM32 output

Sego2
Associate III

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 !

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

5 REPLIES 5
Piranha
Chief II

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
Principal III

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

> 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.

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

@KnarfB​ Nice tip, thank you!