2018-04-12 10:25 AM
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 #workflow2018-04-12 11:04 AM
Hello,
source code generated by CubeMX is scattered with ''USER CODE BEGIN/END'' comments like these ones:
If you place your code between such comments you are guaranteed that CubeMX will preserve it across code re-generations.
2018-04-12 12:05 PM
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?
2018-04-12 01:58 PM
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.
2018-04-12 03:11 PM
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.
2018-04-12 04:47 PM
>
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
2018-04-13 10:37 AM
Thanks everybody! I get it now.
--TimR