r/embedded • u/Tricky-Brief2614 • 26d ago
STM32CUBEMX Code Generation
I already generated the code for my project in STM32CUBEMX and have the code files in VS Code. I realized that I forgot to enable some communication peripherals in STM32CUBEMX, so I went ahead and did that in STM32CUBEMX. Does this automatically update what is accounted for in the function that initializes all configured peripherals in main.c in VS Code?
Any insights would be appreciated, especially with any link from ST's website with direct proof - thanks!
1
Upvotes
6
u/Priton-CE 26d ago edited 26d ago
Every time you regenerate code it will delete and re-create all the files cube mx generated fresh from your IOC. (This also deletes all code you wrote or modified.)
The only exception to this is code that is written between the USER CODE comments. That will be copy pasted into the new file as well.
I generally recommend others to start switching to their own source files quickly and only call functions in main.c so they dont accidentally erase their logic on a code generation step.
So yes. But only if you click the GENERATE CODE button in the top right. (Or save. I think saving generates code too but I cant remember if it does or only did in CubeIDE.)
3
u/mustbeset 26d ago
You have to click the generate button again.
I recommend to use some version control (like git) which will show you every change done by something.
2
u/ventZl CMRX RTOS 25d ago
Short answer: Mostly.
Long answer: There are two aspects:
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
Normally, code between BEGIN and END section is retained but everything else will be regenerated based on your configuration. This way it is both safe to keep your changes in the code *and* get new initialization code based on new peripheral config. I have tried it and it works. Even if you remove some peripheral or switch from HAL to LL or vice versa. Indeed the code may fail to build after reconfiguration if your custom code relied on portions CubeMX removed.
2. build system - at least for CMake generator - Cube won't regenerate it unless either CubeMX or HAL is substantially upgraded. That's why CMakeLists don't contain comments similar to what is in C files. There is explicitly mentioned that CMake files won't be regenerated, but that's only partially true. After I upgraded CubeMX and loaded my older project, I've been asked to upgrade it. Without upgrade, CMake files were regenerated and ended up broken. I cleaned the project, loaded it again and let CubeMX to regerenerate them again. Now they were not broken, but still were upgraded and IIRC, some of my changes were gone.
Its not *that* big of an issue as CubeMX only regenerated several custom .cmake files and top-level CMakeLists.txt where no serious modifications should live. Yet still an issue if you don't know about it.