cancel
Showing results for 
Search instead for 
Did you mean: 

I lost all my code after regeneration using CubeMX!

NSemrHomeInit
Senior

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,

1 ACCEPTED SOLUTION

Accepted Solutions
Imen.D
ST Employee

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".
Thanks
Imen

View solution in original post

12 REPLIES 12
Imen.D
ST Employee

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".
Thanks
Imen
NSemrHomeInit
Senior

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.

Imen.D
ST Employee

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".
Thanks
Imen

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 Venmo
Up vote any posts that you find helpful, it shows what's working..

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

KMoha.11
Associate

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.

0693W000007Z7XoQAK.jpg

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi @KMoha.1​,

I hear your frustration and I tag the CubeMx team to take actions/explanations if necessary.

Hi @Nawres GHARBI​ , @Khouloud OTHMAN​ , @Khouloud ZEMMELI​,

Can you please review the feedback from @KMoha.1​ .

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
JayeCui
Associate

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