cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX and CubeIDE interaction - User Code being overwritten

JerryMC22
Associate

Hello,

when I change configuraion of my procejt STM32F103 in CubeMX  e.g.

PIN&Configuration / SYS / Debug    item is changed  from "4 pins" to "5 pins" and I push "GENERATE buton" the code inside While cycle in main.c function in CubeIDE is changed at the same time. It is terrible. I thought that If I insert my user code inside the /* USER CODE END WHILE */  clause all such code is protected against changing when configuraion of my project is done in CubeMX. But it is NOT true. How can I solve this problem ?

 

 /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

	    // LED ON
	    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
	    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET);
	    HAL_Delay(500);
	    // LED OFF
	    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
	    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_SET);
	    HAL_Delay(500);


    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

 

1 ACCEPTED SOLUTION

Accepted Solutions

You need to put your code between the USER CODE BEGIN xxx marker and the USER CODE END xxx marker;

In other words, your code must come after the USER CODE BEGIN xxx marker and before the USER CODE END xxx marker.

What you posted has your code after the USER CODE END xxx marker - so it is outside the protection of the User Block:

 

 /* Infinite loop */
  /* USER CODE BEGIN WHILE */     <=== User Block STARTS here
  while (1)
  {
    /* USER CODE END WHILE */     <=== User Block ENDS here

	    // This code is OUTSIDE the User Blocks!!

	    // LED ON
	    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
	    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET);
	    HAL_Delay(500);
	    // LED OFF
	    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
	    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_SET);
	    HAL_Delay(500);


    /* USER CODE BEGIN 3 */     <=== User Block STARTS here
  }
  /* USER CODE END 3 */         <=== User Block ENDS here
 

 

PS:

This was already explained by @SOgal.1 in your previous thread:

https://community.st.com/t5/stm32-mcus-boards-and-hardware/code-debugging-using-stlinkv2/m-p/767142/highlight/true#M23748

and by me - with link to previous examples:

https://community.st.com/t5/stm32-mcus-boards-and-hardware/code-debugging-using-stlinkv2/m-p/767156/highlight/true#M23752 

 

PPS:

If you find the positioning of those markers odd, see:

https://community.st.com/t5/stm32cubemx-mcus/stm32cubeide-while-1-bug-linux-versions/m-p/751755

View solution in original post

2 REPLIES 2

You need to put your code between the USER CODE BEGIN xxx marker and the USER CODE END xxx marker;

In other words, your code must come after the USER CODE BEGIN xxx marker and before the USER CODE END xxx marker.

What you posted has your code after the USER CODE END xxx marker - so it is outside the protection of the User Block:

 

 /* Infinite loop */
  /* USER CODE BEGIN WHILE */     <=== User Block STARTS here
  while (1)
  {
    /* USER CODE END WHILE */     <=== User Block ENDS here

	    // This code is OUTSIDE the User Blocks!!

	    // LED ON
	    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
	    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET);
	    HAL_Delay(500);
	    // LED OFF
	    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
	    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_SET);
	    HAL_Delay(500);


    /* USER CODE BEGIN 3 */     <=== User Block STARTS here
  }
  /* USER CODE END 3 */         <=== User Block ENDS here
 

 

PS:

This was already explained by @SOgal.1 in your previous thread:

https://community.st.com/t5/stm32-mcus-boards-and-hardware/code-debugging-using-stlinkv2/m-p/767142/highlight/true#M23748

and by me - with link to previous examples:

https://community.st.com/t5/stm32-mcus-boards-and-hardware/code-debugging-using-stlinkv2/m-p/767156/highlight/true#M23752 

 

PPS:

If you find the positioning of those markers odd, see:

https://community.st.com/t5/stm32cubemx-mcus/stm32cubeide-while-1-bug-linux-versions/m-p/751755

wou thanks it is working. :)