cancel
Showing results for 
Search instead for 
Did you mean: 

code generation after updating .ioc file in CUBEIDX

Ramachandran1992
Associate II

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 

1 ACCEPTED SOLUTION

Accepted Solutions
Peter BENSCH
ST Employee

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 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

In order 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.

View solution in original post

7 REPLIES 7
Peter BENSCH
ST Employee

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

In order 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.
Ramachandran1992
Associate II

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

Ramachandran1992
Associate II

Hi peter,

once again thank you for your quick reply, the below screenshot shows my example code before change in .ioc file.

Ramachandran1992_0-1714707660846.png

The second screenshot shows my example code after changes in the .ioc file.

Ramachandran1992_1-1714708416498.png

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

Peter BENSCH
ST Employee

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 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

In order 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.
Ramachandran1992
Associate II

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. 

Ramachandran1992_0-1714730238240.png

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

 

Please use this button to properly post source code - rather than screenshots:

AndrewNeil_0-1715183872181.png

 

 

Ramachandran1992
Associate II

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