cancel
Showing results for 
Search instead for 
Did you mean: 

How to prevent modified auto-generated code from regenerating?

is45
Associate III
/**
  * @brief IWDG Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_IWDG_Init(void)
{

    /* USER CODE BEGIN IWDG_Init 0 */

    /* USER CODE END IWDG_Init 0 */

    /* USER CODE BEGIN IWDG_Init 1 */

    /* USER CODE END IWDG_Init 1 */
    hiwdg.Instance = IWDG;
    hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
    hiwdg.Init.Window = 4095;
    hiwdg.Init.Reload = 4095;
    if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN IWDG_Init 2 */

    /* USER CODE END IWDG_Init 2 */

}

Hello, apologies if the title sounds confusing, but I wasn't sure how to accurately describe my question.

So, I have the following auto-generated code for a watchdog:

/**
  * @brief IWDG Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_IWDG_Init(void)
{

    /* USER CODE BEGIN IWDG_Init 0 */

    /* USER CODE END IWDG_Init 0 */

    /* USER CODE BEGIN IWDG_Init 1 */

    /* USER CODE END IWDG_Init 1 */
    hiwdg.Instance = IWDG;
    hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
    hiwdg.Init.Window = 4095;
    hiwdg.Init.Reload = 4095;
    if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN IWDG_Init 2 */

    /* USER CODE END IWDG_Init 2 */

}

However, I want to edit line 19 as seen below:

/**
  * @brief IWDG Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_IWDG_Init(void)
{

    /* USER CODE BEGIN IWDG_Init 0 */

    /* USER CODE END IWDG_Init 0 */

    /* USER CODE BEGIN IWDG_Init 1 */

    /* USER CODE END IWDG_Init 1 */
    hiwdg.Instance = IWDG;
    hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
    hiwdg.Init.Window = 4095;
    hiwdg.Init.Reload = (customTimeout * 32000) / 256 / 1000 - 1;
    if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN IWDG_Init 2 */

    /* USER CODE END IWDG_Init 2 */

}

 

customTimeout is a user-defined variable, which is why I don't want it to be erased whenever code is regenerated.

I am aware of the USER CODE blocks, but I don't see anyway in which I can use them. If I put line 19 in in the first 2 USER CODE blocks, they just get overwritten if I regenerate the code as there will be another "hiwdg.Init.Reload = 4095;" in the original line 19. I can't put my modified line in the third user code block, as the the watchdog would already be initialized in line 20.

Would anyone have any idea how I can overcome this issue? Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
is45
Associate III

Hi @RobK1 

Thanks for that, I didn't end up using that, but it helped me realise that there's this blue button here which I can click and change to "No check" so that I can input any variables or calculations inside.

is45_0-1750932765999.png

Nonetheless, thank you!

View solution in original post

4 REPLIES 4
RobK1
Associate III

CubeMX allows you to define User Constants

RobK1_0-1750930633478.png

 

RobK1_2-1750930696418.png

 

is45
Associate III

Hi @RobK1 

Thanks for that, I didn't end up using that, but it helped me realise that there's this blue button here which I can click and change to "No check" so that I can input any variables or calculations inside.

is45_0-1750932765999.png

Nonetheless, thank you!

RobK1
Associate III

And that keeps permanent? Sweet, then I learned something new as well, thanks.

is45
Associate III

Yep! I was quite surprised since I didn't expect it to allow entries like that lol.