2025-03-24 1:30 PM - last edited on 2025-03-26 1:06 AM by Amel NASRI
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
}
2025-03-24 2:33 PM - edited 2025-03-24 2:36 PM
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
2025-03-25 11:15 AM
OK. Thanks for your detailed explanation. I'll try to fix it.
2025-03-26 2:17 AM
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;
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.