Hello everybody,
I’m also trying to use the IWDG. Looking into the firmware I found this file “stm32f2xx_iwdg.h”
Using it in a simple example:
- after 30 sec config and enable WD
- with while(1){IWDG_ReloadCounter();} no reset
- without while(1){IWDG_ReloadCounter();} reset after ~1040ms
- using IWDG_Prescaler_256 or IWDG_Prescaler_4 does not change the reset period so I’m probably doing something wrong. I wanted to go for the maximum period between resets (~=32 sec) I also added the delays but do not know if it is needed?
#include "stm32f2xx_iwdg.h"
SYSTEM_THREAD(ENABLED);
void setup()
{
Serial.begin(115200);
}
static unsigned long lastMillis = 0;
static unsigned long milPrev = 0;
static uint8_t t = 0;
static uint8_t once = 1;
void loop()
{
if(!once)
{
unsigned long mil = millis() - lastMillis;
if((mil-milPrev) > 1 ) // count 2ms untill WD reset
{
Serial.printlnf(" %lu",millis() - lastMillis);
milPrev = mil;
}
}
if(((millis() - lastMillis) > 1000) && once)
{
t++;
Serial.printlnf("t=%u",t);
if(once && t==30)
{
once = 0;
Serial.println("WD config");
// 1. Enable write access to IWDG_PR and IWDG_RLR registers using
// IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable) function*/
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
// 2. Configure the IWDG prescaler using IWDG_SetPrescaler() function
delay(500);
IWDG_SetPrescaler(IWDG_Prescaler_256);
delay(500);
// 3. Configure the IWDG counter value using IWDG_SetReload() function.
// This value will be loaded in the IWDG counter each time the counter
// is reloaded, then the IWDG will start counting down from this value.
delay(500);
IWDG_SetReload(0xFFF);
// 4. Start the IWDG using IWDG_Enable() function, when the IWDG is used
// in software mode (no need to enable the LSI, it will be enabled by hardware)
delay(500);
uint8_t pvu = IWDG_GetFlagStatus(IWDG_FLAG_PVU) == SET;
Serial.printlnf("pvu %u", pvu);
uint8_t rvu = IWDG_GetFlagStatus(IWDG_FLAG_RVU) == SET;
Serial.printlnf("rvu %u", rvu);
delay(500);
Serial.println("Reload");
IWDG_ReloadCounter();
delay(500);
Serial.println("WD enable");
IWDG_Enable();
// 5. Then the application program must reload the IWDG counter at regular
// intervals during normal operation to prevent an MCU reset, using
// IWDG_ReloadCounter() function.
//while(1){IWDG_ReloadCounter();}
}
lastMillis = millis();
}
}
So it seems that the IWDG is working?
Why is it officially not working? What am I missing?
I do not plan to use Sleep in my application. Should I be careful with other things (OTA?)