Skip to main content
chriskuku
Senior II
November 8, 2022
Solved

STM32CubeIDE adding code to main.c - code disappears

  • November 8, 2022
  • 1 reply
  • 1105 views

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?

    This topic has been closed for replies.
    Best answer by Foued_KH

    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

    1 reply

    Foued_KH
    Foued_KHBest answer
    ST Employee
    November 8, 2022

    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.