cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX Work Flow

Brother Theo
Associate II
Posted on April 12, 2018 at 19:25

CubeMX is a great tool but there is something I am not understanding. I am using System Workbench to write my project. If I go back to CubeMX to make a change it tend to wipe out the code I've added to main.c.  Should I put all my code in a separate source file and leave main.c as unchanged as possible? What is the right way to do this?

#system-workbench #cubemx #workflow
6 REPLIES 6
David SIORPAES
ST Employee
Posted on April 12, 2018 at 20:04

Hello,

source code generated by CubeMX is scattered with ''USER CODE BEGIN/END'' comments like these ones:

0690X0000060Aa8QAE.png0690X0000060AVxQAM.png0690X0000060Aa3QAE.png

If you place your code between such comments you are guaranteed that CubeMX will preserve it across code re-generations.

Brother Theo
Associate II
Posted on April 12, 2018 at 21:05

Interesting. I re-generated code with CubeMX to add DMA and everything was fine. But then I re-generated again to remove DMA and the routines I added to main.c went away, like it replaced the entire file. I see there are user code tags right above the main routine declaration. Are you saying all of my code should go there?

David SIORPAES
ST Employee
Posted on April 12, 2018 at 22:58

Yes. As long as your code is sandwiched between any of those tags it's not going to be touched by the tool.

Anyway, as a good practice, I always version things before regenerating as it's easy to just overlook such rule.

Dave Jones
Associate III
Posted on April 13, 2018 at 00:11

They give you a lot of different areas that have the USER CODE BEGIN and END comments. As long as your code goes between them it is preserved.

For example, at the top of main.c is where they put their include files. If you add an include file to that list it gets deleted the next time CubeMX generates the file. But just below that are comments for /* USER CODE BEGIN Includes */ and /* USER CODE END Includes */ . If you put your includes between those, they are preserved.

Likewise they have areas near the top where they put their variables and function prototypes, each followed by a USER CODE section for you to put your own. Then there's the USER CODE 0 section where you can put any of your functions or additional code, above main().

Then there's several USER CODE sections within main() at the top, middle, and bottom, for you to put your code. And there's another USER CODE section after main() and all their initialization code for you to add other stuff to at the bottom of main.c

On the other hand, when I am writing lots of additional functions for specific purposes, I put those in their own files. They don't touch your own files that they didn't create.

Pavel A.
Evangelist III
Posted on April 13, 2018 at 01:47

Should I put all my code in a separate source file and leave main.c as unchanged as possible?

This is what I do. (Have very short experience with STM32, no older pre-Cube code to maintain.)

So far, have developed the following routine with the Cube:

 - Before updating the .ioc file, check everything into source control, or just copy aside

- After generating code, review changes and merge manually. Duh. Very annoying but keeping custom code in main.c and other files that Cube touches, minimal - it's acceptable for me.

- Especially, revert any changes made to the Keil (or whatever you use) project file, unless you understand them!

- Keep track of the CubeMX program version and it's 'repository' version.

- And not to forget, again, insert your code only between 

USER CODE BEGIN and END comments. 

Unless you have to change something *outside* these markers.... then restore your changes when merging.

My 2 (euro) cents...

-- pavel

Brother Theo
Associate II
Posted on April 13, 2018 at 19:37

Thanks everybody!  I get it now.

--TimR