Skip to main content
Visitor II
September 14, 2024
Question

Code Generation would clear my code in `main.c`!

  • September 14, 2024
  • 6 replies
  • 2101 views

Hi,

When I updating the pin assignment information and doing code generation, I find my code, for example, USER CODE part is clear. Why? Is any setup wrong here?

 

 

 

while (1)

{

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

}

/* U

 

STM32CubeIDE

Version: 1.16.0

Build: 21983_20240628_1741 (UTC)

 MacOS: 14.2.1

    This topic has been closed for replies.

    6 replies

    STTwo-32
    ST Technical Moderator
    September 14, 2024

    Hello @Fei and welcome to the ST Community :smiling_face_with_smiling_eyes:.

    Could you please attach you .ioc file and a screenshot of the issue if possible.

    Best Regards.

    STTwo-32 

    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.
    LCE
    Principal II
    September 16, 2024

    My advice:

    As soon as you start coding, create another CubeMX dummy project to copy new stuff from there.

    Then you are safe from Cube overwriting stuff - and from unwanted HAL/LL library updates.

    Andrew Neil
    Super User
    September 16, 2024

    Please see the posting tips for how to properly post source code:

    https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

     

    Where are you putting your user code?

     

    while (1)
    {
    /* USER CODE END WHILE */
     /*
     <<< Any code here is OUTSIDE the 'User' block,
     <<< so WILL get overwritten by code generation
     */
    /* USER CODE BEGIN 3 */
    }
    /* U

     

     

    See also: https://community.st.com/t5/stm32-mcus-products/cubemx-and-cubeide-interaction/m-p/767567/highlight/true#M272020

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Andrew Neil
    Super User
    September 17, 2024

    @Fei  - Example of correct placement of user code (see the 2nd code block):

    https://community.st.com/t5/stm32cubeide-mcus/how-to-covert-the-code-according-to-stm32cubeide/m-p/720546#M30694

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Andrew Neil
    Super User
    September 27, 2024

    @Fei if you prefer video, see this for correct placement of user code:

    https://youtu.be/hyZS2p1tW-g?t=573

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    unsigned_char_array
    Lead III
    September 16, 2024
    int main(void)
    {
     /* USER CODE BEGIN WHILE */
     // user code
     /* USER CODE END WHILE */
    
     // NOT user code. Will be deleted!
    
     /* USER CODE BEGIN 3 */
     // more user code
     /* USER CODE END 3 */
    }
    "Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."