cancel
Showing results for 
Search instead for 
Did you mean: 

Clearing the RTC WUTF (saving your sanity by sharing my hard lesson learned :-)

Scott Löhr
Senior II

I did RTFM, and the STM32WB55xx/35xx ref man was very clear:

29.7.4 RTC initialization and status register (RTC_ISR)

 

Bit 10 WUTF: Wakeup timer flag

This flag is set by hardware when the wakeup auto-reload counter reaches 0.

This flag is cleared by software by writing 0.

This flag must be cleared by software at least 1.5 RTCCLK periods before WUTF is set to 1 again.

 

But I obviously glossed over it - after all, there is a lot to learn!

 

So time goes on, and I get a LOT of run-time on my application on the STM32WB Nucleo dev board using the RTC as my SYSTICK. But there is this very, very, very rare (but obviously very disturbing) hang when the system stops waking from STOP2 on a software timer or OSAL SYSTICK. The time finally came when I had to track down the insidiously rare hang in a very complex FreeRTOS system. And I searched, and hunted, and pulled out hair searching for the source and finally found it when re-RTFM:

This flag must be cleared by software at least 1.5 RTCCLK periods before WUTF is set to 1 again.

 

Crap - I saw that the first time, but the ramifications of the insidious (dare I say hideous) race condition between software and the (documented) hardware behavior just didn't hit me (thus the days I spent flogging myself while finding this race).

 

So this is not enough:

LL_RTC_ClearFlag_WUT(RTC);

 

Instead, this is what is required to resolve the (hideous, albeit documented) race with hardware (and what I think that ST should implement as the LL_RTC_ClearFlag_WUT() behavior):

#define RTC_ClearFlag_WUT() do { LL_RTC_ClearFlag_WUT(RTC); __ISB(); } while (LL_RTC_IsActiveFlag_WUT(RTC));

 

So there you have it - a little gold nugget of sanity for any of you using the RTC ;)

 

 

 

 

5 REPLIES 5

Thanks for sharing this.

I don't quite understand your fix. The single ISB() hardly lasts 1.5 RTCCLK period, does it?

JW

I am flattered to receive that attention of an Einstein level contributor, @Community member​ ! I am glad that you asked, because I had already been thinking that the subtlety of the fix might escape a reader who only has time to glance at this post (as I obviously only "glanced" at the clearly documented "failure to clear" behavior in the ref man). So I was thinking to add a follow-up post to this anyway, but your question gives me that opportunity to do so now.

Don't focus on the ISB(), that just flushes the flag clear operation before checking the flag - the "fix" is to loop checking that the flag actually gets cleared. When it comes to operations on the h/w, it is generally very inefficient to have to check that every single thing that you ask for is acted upon, thus I generally don't write s/w that has every h/w operation in a loop checking that it was done. My case in point is clearly demonstrated by the ST LL software team - they did not even implement the LL_RTC_ClearFlag_WUT() operation as a loop (but in this post, I am recommending that they should).

The RTC timer is auto-reloading, so it is just banging away, resetting the WUTF (especially when it is used for the SYSTICK, with a 2-msec period).

In order to be woken from low power the next time that WUTF is set, WUTF must be cleared by software before entering low power.

If software "clears" WUTF within 1.5 RTCCLK period of it being set again and enters low power, then the WUTF never really got "cleared" before entering low power and thus it never gets set "again" and thus the processor never awakens from low power ... so hung system, but ever so very sporadically.

Hi Scott,

I understand, thanks.

I am not very familiar with the low-power features of STM32, as I almost never worked on low power design; and honestly, I am quite afraid of the moment when I will be asked to implement such. So I'm trying to gather relevant info... While you are using the 'WB line, I believe this is a generic behaviour of WUTF in most of STM32 RTC - I see the same description in the 'F4 RTC chapter, for example.

Thanks for sharing this.

Jan

Scott Löhr
Senior II

You are very welcome, Jan!

ALipo.2
Associate

Hi Scott,

Thanks a lot you saved me a lot of time.

I use LPTIM as a systick and I there is also extensively used STOP2 (Zephyr RTOS).

I lost more than an hour trying to clear WUTF (before I found your post).

Regards,

Artur