CubeMX and CubeIDE interaction - User Code being overwritten
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-01-30 03:29 AM - last edited on ‎2025-01-30 03:46 AM by Andrew Neil
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 */
Solved! Go to Solution.
- Labels:
-
STM32CubeMX
-
STM32F1 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-01-30 03:38 AM - edited ‎2025-01-30 03:52 AM
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:
and by me - with link to previous examples:
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-01-30 03:38 AM - edited ‎2025-01-30 03:52 AM
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:
and by me - with link to previous examples:
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-01-30 03:56 AM
wou thanks it is working. :)
