cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX after regenerating the project the user code dissipated

An Ha
Associate II
Posted on December 06, 2017 at 13:59

I add some code to the files generated by CubeMX. When I regenerate the project, my added code will be deleted.

In the main.c is /* USER CODE BEGIN */ and /* USER CODE END*/ but in other files, there is no area where one can add user code.

any idea how can I solve this issue?
7 REPLIES 7
Doug Kehn
Senior
Posted on December 06, 2017 at 14:53

I suggest putting the source under source/version control and then look at the diffs after regenerating with CubeMX. It may also be helpful to add your own markers to the code you modified. This helps to visually show where your modifications are and may be particularly helpful after a CubeMX and/or Firmware Library update that result in a messy diff.

Posted on December 06, 2017 at 16:45

At the very least sandbox the code and merge in from the CubeMX generated tree, there have been several cases where user code and files have been destroyed. Not having some backup or protection strategy courts failure and loss.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Alan Chambers
Associate II
Posted on December 06, 2017 at 18:25

I prefer never to mix generated code with hand-written code in the same files. I don't know how easy this is with CubeMX/HAL. I generated a CubeMX project just now for STM32F4: there were /* USER CODE xxx */ sections all over the place. Are you sure you have only one?

Imen.D
ST Employee
Posted on December 07, 2017 at 16:39

Hello

a.haloui

,

Which CubeMx version are you using ?

Check the 'Code Generator' Tab in Project Settings and ensure that you pick 'Keep user code when re-generating', in that way you will note lose it in the next generation.

Best Regards

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Posted on December 08, 2017 at 14:13

Hello Imen D,

I am using STM32CubeMX V1.0 Version 4.23.0. The 'Keep user code when re-generating' is set.

There is /* USER CODE xxx */ sections only in the 'main.c' , 'main.h' and 'stm32f7xx_it.c'. In this files the user code will not be deleted.

In other files 'Drivers folder' for example 'stm32f7xx_hal_uart.c/.h' the added code will be deleted because they havn't /* USER CODE xxx */ sections.

The result is, I have to create for each module my one module. This cause too many new files and repetitions This is not good for the overview in a big project.

Thank you for your support.

Best regards

Posted on December 08, 2017 at 14:16

There are many /* USER CODE xxx */ sections but only in 'main.c' , 'main.h' and 'stm32f7xx_it.c'. In this files the user code will not be deleted.

Posted on December 08, 2017 at 14:46

Ah, only files with USER CODE sections are 'intended' to be modified. This will be files in the Inc/ and Src/ directories. Files in other directories (e.g. Drivers) are not intended to be modified. As Alan Chambers points out, it's not a good idea to just add code to theses files. However, to my original point, sometimes it's necessary to modify these files; to fix a bug for example. This is where I use a combination of source/revision control and my own custom code markers. Clive One suggestion of sandboxing also works well.

If you generated code in individual c/h files, you should have Src/usart.c and Inc/usart.h. Custom UART code can be added to these files in the desired USER CODE sections. Alternatively, you can add your own files to the project with your custom code.