Skip to main content
Visitor II
March 24, 2025
Question

What's the bug in the code of IWDG with HAL_IWDG_RegisterCallback below? (STM32H563)

  • March 24, 2025
  • 2 replies
  • 734 views

I am trying to implement an IWDG in my program, which will save the value of the specified register to EEPROM before resetting.

The STM32 I'm using is STM32H563.

But I found that my program is not working (cannot save data to EEPROM before resetting).

I politely request your help or to provide a simple example of IWDG using HAL_SWDG_Registercallbacks.

Many thanks. :)

void IWDG_EarlyWakeupCallback(IWDG_HandleTypeDef *hiwdg)
{
 uint8_t arr[1] = {0xAA};
 // if (imageAbortedFlag)
 {
 if (getCurrentRunningImage() == 0x01)
 {
 pRAMbootctrl->image_A_tasks_aborted_count++;
 }
 else if (getCurrentRunningImage() == 0x10)
 {
 pRAMbootctrl->image_B_tasks_aborted_count++;
 }
 MemoryStoreData(EEPROM_MSATABLE_UP03_INDEX, UPE6_128, 1, arr);
 jump_to_IMAGE(BOOTLOADER_START_ADDRESS);
 }
}

void MX_IWDG_Init(void)
{
 hiwdg.Instance = IWDG;
 hiwdg.Init.Prescaler = IWDG_PRESCALER_8;
 hiwdg.Init.Window = IWDG_WINDOW_DISABLE;
 hiwdg.Init.Reload = 0x0FFF; // ?TBD
 hiwdg.Init.EWI = 0; // ?? 0x0FFE

 if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
 {
 // Error_Handler();
 }

 if (HAL_IWDG_RegisterCallback(&hiwdg, HAL_IWDG_EWI_CB_ID, IWDG_EarlyWakeupCallback) != HAL_OK)
 {
 // Error_Handler();
 }

 #if 0
 HAL_NVIC_SetPriority(IWDG_IRQn, NVICPreemptionPriority_IWDG, NVICSubPriority_IWDG);
 HAL_NVIC_EnableIRQ(IWDG_IRQn);
 #endif
}

 

    This topic has been closed for replies.

    2 replies

    Graduate
    March 24, 2025

    Does H563 have genuine EEPROM, or is it emulated in software by writing to FLASH?

    Be aware that writing to FLASH takes a long time (milliseconds) and in that time the entire FLASH (maybe just that bank of FLASH for devices that have two) is inaccessible.

    Now the system is pretty clever in that when the processor is made to wait if it tries to access that memory. But your watchdog interrupt will not get serviced until the wait is complete and by then the watchdog might have concluded that your system has crashed. And so the watchdog will reset your processor.

    Ways to avoid this?

    Only write to FLASH immediately after a watchdog interrupt 

    Execute from RAM during the emulated-EEPROM writing process - this includes the interrupt-service-routine for the Watchdog and the vector table

    Where your processor has two banks of FLASH, have all accesses to the bank you’re not writing to. 

    Don’t use IWDG for this. Set up a normal timer

    XeManAuthor
    Visitor II
    March 25, 2025

    OK. Thanks for your detailed explanation. I'll try to fix it.

    ST Employee
    March 26, 2025

    Hello @XeMan

    Indeed, the STM32H563 uses emulated EEPROM by writing to FLASH

    However, I want to highlight that in your code, the value 0 means that EWI is disabled: 

     hiwdg.Init.EWI = 0; 

     

    XeManAuthor
    Visitor II
    March 28, 2025

    Thanks Sarra. I've updated the variable of hiwdg.Init.EWI to 4000 but still failed to save the data.