cancel
Showing results for 
Search instead for 
Did you mean: 

I am running stm32cubeide 1.5.0. CubeMx appears to be 6.1.1-RC2. The project I have has months of development time, but recently when I made a change in CubeMX and generated the file, CubeMX deleted all of the code I have between the USER CODE sections.

MLync.1
Associate II

I have semi-recent backups, but this has created a lot of extra work.

Is this a problem specific to the versions I am using?

7 REPLIES 7
TDK
Guru

Ensure you have it set up to preserve user code.

Project Manager -> Code Generator -> Keep User Code when re-generating

If that's set, could be a bug. Could be you deleted some important comments. See if you can replicate if if you're interested.

If you feel a post has answered your question, please click "Accept as Solution".
MLync.1
Associate II

Thank you for your reply. I have copied files from a backup version into the current project. I then went to the MX.ioc file and modified an unused gpio pin and selected generate. Everything worked as it has in the past. I am not sure what happened in my previous attempt.

For now I can continue on, but will make very frequent backups.

MLync.1
Associate II

An update to my original question. I do have Keep User Code when re-generating checked. However when I re-generated the cubeMX configuration many of the User Code sections were eliminated. (Some were retained) This is the second time I have seen this happen, although I have re-generated the code multiple times. I cannot determine why, or predict when this happens. I think there is a bug in the CubeMX generation function.

MLync.1
Associate II

This only affects the main.c file as far as I have seen.

MLync.1
Associate II

After more investigating I have found that the User Code is removed when you select Generate Peripheral initialization as a pair of '.c/.h' files per peripheral.

I don't know why this selection is not compatible with Keep User Code when re-generating. I think it is a bug in CubeMX.

Thanks for your observation. This issue is explored further in this thread.

Embedded UI/UX Consulting: cadenza.design
Paul1
Lead

Suggestion:

  • create a main2.c (or main2.cpp) to hold your custom code
  • have main() immediately call main2() and never return. manually copy required code (init) from main.c to main2.c
  • then only a couple of lines in main.c can be lost, and easily repaired.
  • Consider putting your code files in a separate folder next to \Core, such as \ProjectCode (inc/src optional). Keeping it away from the Folders MX generates makes it safer.

Also:

  • programs like "Beyond Compare" allow easily comparing a project in backup folder to current working project, and copying desired code.
  • makes it extremely easy to catch changes and choose which to keep and which to revert.
  • also easy to copy your code style from a working project to a new project, such as customizations in init/interrupts/etc.

and:

  • get some version control software (SVN, GIT), free versions are available.
  • they make it very easy to detect changes, and commit or revert them.

Paul