cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeIDE adding code to main.c - code disappears

chriskuku
Senior II

Is there a rule where to put code into main.c so that it stays there permanently? It happended to me now a couple of times that I added an interrupt callback function which suddenly disappeared. Are there areas in main.c where to safely put those?

1 ACCEPTED SOLUTION

Accepted Solutions
Foued_KH
ST Employee

Hello @chriskuku​,

You should add the functions in the User Code section:

/* USER CODE BEGIN  */
(..)
/* USER CODE END  */

Ensure that following option is checked: "Keep User Code when re-generating". In this case, the user sections defined by STM32CubeMX are preserved.

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.

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

1 REPLY 1
Foued_KH
ST Employee

Hello @chriskuku​,

You should add the functions in the User Code section:

/* USER CODE BEGIN  */
(..)
/* USER CODE END  */

Ensure that following option is checked: "Keep User Code when re-generating". In this case, the user sections defined by STM32CubeMX are preserved.

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.

Foued

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.