2025-06-26 2:31 AM
/**
  * @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!
Solved! Go to Solution.
2025-06-26 3:13 AM
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.
Nonetheless, thank you!
2025-06-26 2:38 AM
CubeMX allows you to define User Constants
2025-06-26 3:13 AM
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.
Nonetheless, thank you!
2025-06-26 6:06 AM
And that keeps permanent? Sweet, then I learned something new as well, thanks.
2025-06-26 8:54 AM
Yep! I was quite surprised since I didn't expect it to allow entries like that lol.
