HAL IWDG initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2 :

  • Open STM32CubeMX2 IWDG configuration panel.

  • Configure the SW layer HAL.

  • Configure the IWDG main features.

  • Configure the NVIC if the application requires the IWDG to be used in IT mode.

  • Generate the code.

HAL IWDG configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

HAL IWDG main features configuration

Configure the IWDG calculator.

IWDG main feature configuration

NVIC configuration

Configure the NVIC in case of an interrupt.

IWDG NVIC configuration

Generated IWDG initialization and de-initialization sequences

The IWDG initialisation sequence is generated in the mx_iwdg_init function in mx_iwdg.c file under $YOUR_PORJECT_NAME$/$YOUR_PORJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated : IWDG initialization sequence

Topic

HAL 1 Code Snippet

HAL 2 Code Snippet

Handle declaration

IWDG_HandleTypeDef hiwdg;
static hal_iwdg_handle_t hIWDG;

Initialization and configuration sequence

static void MX_IWDG_Init(void)
{
  hiwdg.Instance = IWDG;
  hiwdg.Init.Prescaler = IWDG_PRESCALER_32;
  hiwdg.Init.Window = IWDG_WINDOW;
  hiwdg.Init.Reload = IWDG_RELOAD;
  hiwdg.Init.EWI = 0;
  if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  {
    Error_Handler();
  }
}
hal_iwdg_handle_t *mx_iwdg_init(void)
{
  if (HAL_IWDG_Init(&hIWDG, HAL_IWDG1) != HAL_OK)
  {
    return NULL;
  }

  /* Enable the interruption for IWDG */
  HAL_CORTEX_NVIC_SetPriority(IWDG_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(IWDG_IRQn);

  return &hIWDG;
}

/*
  Start the Independent Watchdog (IWDG) with the following parameters:
  - Maximum time before watchdog reset   : 131072 milliseconds
  - Minimum time before refresh (window) : 1 milliseconds
  - Early Wakeup Interrupt (EWI) time    : 1 milliseconds
*/
hal_status_t mx_iwdg_start(void)
{
  return HAL_IWDG_Start(&hIWDG, 1UL, 131072UL, 1UL);
}