Skip to main content
NSemrHomeInit
Associate III
October 16, 2020
Solved

I lost all my code after regeneration using CubeMX!

  • October 16, 2020
  • 7 replies
  • 6929 views

Dear ST Hello,

what is the best way to use CubeMX without any deleting functions in CubeIDE?

For code added to the main file, it's clear to use /**User */ comment guidelines but, for functions, should i create another file or module for my functions?

Thank you in advance,

S.Tairk,

Best answer by Imen.D

You should add the functions in the User Code section:

/* USER CODE BEGIN 0 */
(..)
/* USER CODE END 0 */

Ensure that following option is checked : "Keep User Code when re-generating". In this case, the user sections defined by STM32CubeMX are preserved.

I recommend you to have a look at the UM1718 to show how to add the user code in different sections:

  • B.1 STM32CubeMX generated C code and user sections
  • 14.7 Updating the project with the user application code"

Note that when upgrading to a new version of STM32CubeMX, make sure to always backup your projects before loading the new project (especially when the project includes user code).

I hope that this brings some help to you.

Imen

7 replies

Technical Moderator
October 16, 2020

Hello @NSemrHomeInit​ ,

You should set the Backup previously generated files when re-generating option :

  • Backup previously generated files in a backup directory: The .bak extension is added to previously generated .c/.h files.
  • enabled in the Code Generator tab the following option : "Keep User Code when re-generating" in the Project Manager view > Code Generator tab. User Code is kept during generation the C code, when this option is enabled. It's applies only to user sections within STM32CubeMX generated files. It does not apply to the user files that might have been added manually or generated via ftl templates. 

Hope this helps you.

0693W000004K45QQAS.jpg 

Please select my post as Best is answered your question, so that thread is marked as solved.

Imen

"When your question is answered, please close this topic by clicking ""Accept as Solution"".ThanksImen"
NSemrHomeInit
Associate III
October 16, 2020

Yes this help me thank you for taking into consideration my question.

One more question please, may be i was not clear in the first post.

if i creat a function in the main.c file, how i could tell cubeMX to no delete it?

Thank you Imen.

Tesla DeLorean
Guru
October 16, 2020

You might be able to dig the earlier version you lost via windows "previous version" right-click of the file.

I might suggest putting stuff in your own .C file and then #include "mystuff.c" into the auto-generated file. Make sure to back things up all the same, as these tools have a habit of blowing things up. Ideally, put your working build in its own directory, regenerate in a sandbox, and merge to your live one

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
NSemrHomeInit
Associate III
October 16, 2020

Work like a professional, two sandboxes, Thank you :).

Imen.DBest answer
Technical Moderator
October 16, 2020

You should add the functions in the User Code section:

/* USER CODE BEGIN 0 */
(..)
/* USER CODE END 0 */

Ensure that following option is checked : "Keep User Code when re-generating". In this case, the user sections defined by STM32CubeMX are preserved.

I recommend you to have a look at the UM1718 to show how to add the user code in different sections:

  • B.1 STM32CubeMX generated C code and user sections
  • 14.7 Updating the project with the user application code"

Note that when upgrading to a new version of STM32CubeMX, make sure to always backup your projects before loading the new project (especially when the project includes user code).

I hope that this brings some help to you.

Imen

"When your question is answered, please close this topic by clicking ""Accept as Solution"".ThanksImen"
Explorer
November 24, 2024

Can you not just make sure to create a backup of the user code files before overwriting it?

KMoha.11
Visitor II
April 23, 2021

As someone who just lost all his code due to the popup to generate code by the CubeMX, I would like the STM team to realize that the way this is done is not a feature at the moment, but a bug. This could be a feature, however, it is not a feature in its current state. This code generation should be handled in a different way, maybe not by prompting with the default being 'Yes' because some people are in a hurry (a temporary solution). The 'User code' comments to "resolve" this issue is like using duct tape on a broken leg. STM can do better.

Tesla DeLorean
Guru
April 23, 2021

0693W000007Z7XoQAK.jpg

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Visitor II
May 29, 2024

Hello NSemrHomeInit,

Maybe you can set the property of your own source file and folder to read only. Then use generate function of CubeMX dose not delete those files. Just try

Karl Yamashita
Principal
May 29, 2024

I wouldn't rely on using the USER CODE section. 

I had spent a couple of hours adding code to the USER CODE section and STM32CubeIDE (Cube MX built in) crashed during code generation. The main.c file was corrupted and STM32CubeIDE could not open the file nor could any text editor. Lesson learned, so I never use the USER CODE section anymore.

 

So now I create a PollingRoutine.c/h files. In it i have two functions, PollingInit() and PollingRoutine().

#include "main.h"


void PollingInit(void)
{

}

void PollingRoutine(void)
{

}

 

In main.c I just write those two calls, one before the while loop and one within like so. 

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 PollingInit();
 while (1)
 {
	 PollingRoutine();
 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */
 }
 /* USER CODE END 3 */
}

 

So anything i want to initialize, i write in PollingInt() and my while loop is now PollingRoutine(). Then you can write other functions within PollingRoutine.c without having to find a USER CODE section to write new functions like you would in main.c. Or you can create other files pertaining to certain peripherals like UART_Handler.c or I2C_Handler.c, etc. 

 

If the main.c ever gets corrupted again, the only thing i lost was writing the two function calls.

Another advantage is that main.c already has a lot of generated code so instead of scrolling through all that mess to find your added functions. you can scroll through PollingRoutine.c that has only your functions.

 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Associate
August 23, 2024

I just experience this nonsense. Kept my code under main but deleted my code under the while(1) loop. Like seriouly WTF?