3
votes

I am running a STM32F072ZB into standbye mode. The MCU sleeps most of the time (waking up every 1hr to read sensors). As I have the watchdog running, I am using RTC to generate an alarm every 25s (watchdog period is ~28s). So when I am sleeping for 1hr, I am setting the alarm in time now + 25s, then go to standby, woken up by the alarm kick the watchdog and set the alarm to the next 25s etc...

It is working almost fine but for some reason, sometimes the MCU resets because of the watchdog not kicked. That means I have missed the alarm for some reason. I have been doing some tests and over 24 hours it happened 6 times.

As anyone ever experienced this kind of issue?

The code goes here:

// Set RTC_Alarm
HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
util_msDelay(10);

/*To configure the wake up timer to 25s the WakeUpCounter is set to 0xC738:
 Wakeup Time Base = 16 /(32.768 kHz RC) = ~0.49 ms
 Wakeup Time = ~0.49 ms  * WakeUpCounter
 Therefore, with wake-up counter =  0xC738  = 51.000
 Wakeup Time =  0.49 ms *  51,000 = ~ 25 sec. */

HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0xC738, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
util_msDelay(10);

if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET) {
    /* Clear Standby flag */
    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
}

/* Clear Wake-up timer flag if it is set    */
/* Flag will set after exiting from Standby */
if (LL_RTC_IsActiveFlag_WUT(RTC) == 1) {
    LL_RTC_ClearFlag_WUT(RTC);
}

/* Clear all related wakeup flags */
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
util_msDelay(10);
1
Any periodicity observed with this issue ? BTW what's your brownout reset level in sleep mode ? - GabrielT

1 Answers

4
votes

The STM32 IWDG runs from the LSI oscillator which is nominally 40KHz, giving a maximum nominal timeout of ~26.2 seconds however the RC oscillator is neither accurate nor stable and can vary from 30KHz to 50KHz giving timing variation from 19.65 to 32.75 seconds.

If the RTC is also driven from the LSI then that is not a problem but if you are driving it from the LSE you cannot guarantee your watchdog will not go off before the RTC alarm, and you should then set the RTC alarm to less than 19 seconds.

That said, if you are running the RTC from the LSI then your calculation for 25 seconds is incorrect at nominally 40KHz, 51000 would be just 20.4 second. It is ambiguous because the comment suggests you are using the LSE at 32768Hz but it also says it is an RC oscillator where I'd expect a crystal or ceramic oscillator.