2024-05-01 10:07 PM - last edited on 2024-05-01 11:31 PM by STTwo-32
Hi Team,
I am developing a project in STM32CUBEIDE, when I configure GPIOs, connectivity, middleware's and clock configuration, from the .ioc file it generates the code and produce the main.c and other optional and header files based on my configuration. Now the problem is if any changes done in .ioc configuration, the main.c will update automatically with deleting pervious code. I am using build option to rectify this one, but same occurs again. Is there any other option to do the update in .ioc configuration without any deletion done in the previous main.c program, provide the suitable guidelines to rectify the problem.
Thank You
regards
S.Ramachandran
Solved! Go to Solution.
2024-05-03 02:05 AM
Well, you have changed the default structure of the CODE BEGIN/END blocks, which CubeMX cannot handle. In its raw state, the while loop looks like this:
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
You have turned it into:
/* USER CODE BEGIN WHILE */
while (1)
{
ethernetif_input(&gnetif);
sys_check_timeouts();
}
/* USER CODE END 3 */
...so the end marker of the block WHILE and the start marker of the block 3 are deleted. How should CubeMX then sort its own code?
There are deliberately two areas in the WHILE loop so that you can insert your own code at the beginning or at the end, so that you can build it up as shown below. Among other things, CubeMX keeps the option open to place automatically generated code in the WHILE later, which would not be feasible if the entire WHILE were user code:
/* USER CODE BEGIN WHILE */
while (1)
{
// user code at the beginning of the while loop
HAL_GPIO_WritePin(GPIO_Port, Pin, GPIO_PIN_SET); // dummy code for illustration
HAL_Delay(20); // dummy code for illustration
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// user code at the end of the while loop
HAL_GPIO_WritePin(GPIO_Port, Pin, GPIO_PIN_RESET); // dummy code for illustration
HAL_Delay(20); // dummy code for illustration
}
/* USER CODE END 3 */
I would therefore recommend inserting your code only in the first block, i.e. directly before USER CODE END WHILE.
Regards
/Peter
2024-05-02 04:23 AM
CubeMX (stand-alone or as part of CubeIDE) always overwrites everything outside the USER CODE BEGIN ... USER CODE END areas when regenerating. Exception: if you remove a peripheral module from the IOC, for example, for which your own code was previously written in such USER CODE areas, this code is deleted without further enquiry, precisely because the module is then no longer relevant - so please be careful!
Otherwise, simply place your code within the above-mentioned areas and nothing will be lost, apart from the exception also mentioned.
Hope that helps?
Regards
/Peter
2024-05-02 08:33 PM
Hi peter,
Thank you for your quick reply and valuable information. As you said above in the last point, "exception are mentioned", will you provide the any document/links that mentioned the exceptions.
than you
regards
Ramachandran.S
2024-05-02 09:07 PM - edited 2024-05-02 09:09 PM
Hi peter,
once again thank you for your quick reply, the below screenshot shows my example code before change in .ioc file.
The second screenshot shows my example code after changes in the .ioc file.
1. As you mentioned the code not within the "USER CODE BEGIN" deleted without further enquiry. That was done in function "udpServer_Init();".
2. But in the portion of
/* Infinite loop */
/* USER CODE BEGIN WHILE */
my own written code was also deleted. this was written under the "USER CODE BEGIN" portion.
let correct me, where I had done the mistake. It happens every time. I suffer a lot from this.
thank you.
regards
Ramachandran.S
2024-05-03 02:05 AM
Well, you have changed the default structure of the CODE BEGIN/END blocks, which CubeMX cannot handle. In its raw state, the while loop looks like this:
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
You have turned it into:
/* USER CODE BEGIN WHILE */
while (1)
{
ethernetif_input(&gnetif);
sys_check_timeouts();
}
/* USER CODE END 3 */
...so the end marker of the block WHILE and the start marker of the block 3 are deleted. How should CubeMX then sort its own code?
There are deliberately two areas in the WHILE loop so that you can insert your own code at the beginning or at the end, so that you can build it up as shown below. Among other things, CubeMX keeps the option open to place automatically generated code in the WHILE later, which would not be feasible if the entire WHILE were user code:
/* USER CODE BEGIN WHILE */
while (1)
{
// user code at the beginning of the while loop
HAL_GPIO_WritePin(GPIO_Port, Pin, GPIO_PIN_SET); // dummy code for illustration
HAL_Delay(20); // dummy code for illustration
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// user code at the end of the while loop
HAL_GPIO_WritePin(GPIO_Port, Pin, GPIO_PIN_RESET); // dummy code for illustration
HAL_Delay(20); // dummy code for illustration
}
/* USER CODE END 3 */
I would therefore recommend inserting your code only in the first block, i.e. directly before USER CODE END WHILE.
Regards
/Peter
2024-05-03 03:07 AM
Hi peter,
Thank you for your quick reply. I aware this mistake after sending this error message to you. After knowing this mistake, I tried my own "USER CODE BEGIN and END", it works fine.
In the above screenshot, the sub-program and "USER CODE BEGIN" & "USER CODE END" written me, this also work fine. Thank you for great guide.
regards
Ramachandran.S
2024-05-08 08:58 AM
Please use this button to properly post source code - rather than screenshots:
2024-05-08 08:51 PM
Hi andrew,
Hope you are doing well. Thanks for your suggestion and valuable information, hereafter you I follow your suggestion for further quires.
Thank you
Ramachandran.S