Skip to main content
RAltm
Senior
July 1, 2026
Solved

USBX user code sections are overwritten

  • July 1, 2026
  • 3 replies
  • 97 views

Hello,

I’m using CubeMX 6.18.0. I’m trying to implement a USBX standalone (bare-metal, no RTOS) CDC application. Code within user code sections is overwritten when re-generating the project. As far as I can see this is not a new problem, there are already some older topics about it. @ST: will this ever be fixed?

Regards

Best answer by RAltm

Hello ​@Souhaib MAZHOUD 

I think I figured out what happened: as I mentioned there’s no out-of-the-box working CDC non-RTOS example for my device. I used source code snippets found on the web, including code for functions generated by CubeMX.

I replaced the whole functions instead of adding/modifying only the user code sections. The sources on the web have been created with older versions of CubeMX and/or the device Cube package and contained some faulty user code begin/end tags. So CubeMX wasn’t able to keep the sections because copying the whole functions also copied the faulty tags.

I deleted the generated source files, regenerated them and added code only in the user code sections. From what I can see until now it seems that no user code is removed unintentionally. I keep an eye on it, but currently it looks like it’s working as intended.

Regards

3 replies

ST Technical Moderator
July 1, 2026

Hello ​@RAltm 

Can you ensure that the option “Keep User Code when re-generating” is checked in Code Generator section:

Otherwise, could you please provide your IOC file in order to investigate the issue?

BR, Souhaib

To give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question.
RAltm
RAltmAuthor
Senior
July 1, 2026

Hello  Souhaib,

the option to keep user code is enabled in my .ioc file. Currently I’m trying to setup a minimal working example. Unfortunately I’m not able to reproduce the issue, neither on the MWE nor on my project.

Since there’s no out-of-the-box working CDC non-RTOS example for my device (STM32U083) I’ve to implement it piece by piece based on snippets found on the web. So there’s a chance that I accidentally put a snippet before a USER CODE BEGIN statement and then regenerated the source code.

The only other thing which comes to my mind right now is that my changes to the .ioc file triggered a full replacement of the files.

I hope I can reproduce the issue, but I’ve to investigate first.

Regards

RAltm
RAltmAuthorBest answer
Senior
July 4, 2026

Hello ​@Souhaib MAZHOUD 

I think I figured out what happened: as I mentioned there’s no out-of-the-box working CDC non-RTOS example for my device. I used source code snippets found on the web, including code for functions generated by CubeMX.

I replaced the whole functions instead of adding/modifying only the user code sections. The sources on the web have been created with older versions of CubeMX and/or the device Cube package and contained some faulty user code begin/end tags. So CubeMX wasn’t able to keep the sections because copying the whole functions also copied the faulty tags.

I deleted the generated source files, regenerated them and added code only in the user code sections. From what I can see until now it seems that no user code is removed unintentionally. I keep an eye on it, but currently it looks like it’s working as intended.

Regards