cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX 4.16.1 deletes user code

xerxes931
Associate
Posted on October 07, 2016 at 22:51

Hi. I am writing my engineering thesis and I have to build a project based on STM32F746G-Discovery. So I started by creating a project in CubeMX, then writing some code in it in Keil uVision 5, wrote my own LCD libraries and so on. After that I wanted to add ADC functionality to my project, so I opened CubeMX and enabled ADC, but when I generated the code, all my ''user code'' and libraries I created just disappeared.

I made sure many times that my code is between ''user code begin'' and ''user code end'' comments, and that the checkbox ''keep user code when re-generating'' is checked.

Could you please help me? I'd like to finish this project as fast as possible, but it won't be possible if everytime I want to add another peripheral I have to write my code from scratch.

I am aware that I can write the peripheral definitions and initializations myself, but I didn't download CubeMX so that I have to do it by hand anyway.

Greetings,

Marcin
2 REPLIES 2
Posted on October 08, 2016 at 02:15

Suggest you always have CubeMX, or any other auto-gen tool, create into a sandbox, and then use merge tools like WinMerge or Araxis Merge to manage your code in/out of generated source. Archive your project frequently.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on October 09, 2016 at 05:38

Marcin,

From my article that will be going out next week:

''I don’t particularly care for ST’s code style, I find putting my code into their slots pretty restrictive, so I would replace line 25 with a function call to my mainline code in another file.

24  /* USER CODE BEGIN WHILE */

25  ThermostatMain();

26  {

27  /* USER CODE END WHILE */

Even if you rerun the code generation phase of Cube, line 25 will be preserved. Then in my code, probably in a file called thermostat.c in my

source

directory, I would have a function called ThermostatMain() which would contain my normal FOREVER loop structured code.''

Well, at least I thought it would be preserved.

So, my suggestion is to create your own source and include directories and put all of your code there instead of the Src and Inc directories that Cube creates.

In main, get out as soon as possible, replace the while (1) with a subroutine call and don't ever touch the Cube generated files again.

Frankly, and I've said this before here, the generated code from Cube should not create main(). Some RTOS' provide main and take care of a bunch of stuff before giving the processor over to your application. Cube should have generated stuff that my code calls. But, too late now. Let's work around it.

Andrei from The Great White North

(as seen on the embedded.fm podcast and blog)