Electrical – Stm32 RTC wakeup from standby

rtcstm32

I am using an STM32L053C8 and I am trying to set up an RTC wakeup event, and while I am successful, I can only do it to a max time which is a couple of seconds. I need to put it in standby and wake up days later. At the moment I am using the low speed internal 40KHz as the RTC clock, eventually i will get a 32.768 crystal. enter image description here

According to Carmine Noviello-Mastering STM32 i should be able to set the wakeup delay for a pretty long time. And while I do know it says using an external clock, using the internal one of a few Khz should still be able to provide a fairly decent delay for testing purposes. I just cannot figure it out. When I chose the 1Hz setting for the wake up clock, that only means it will use the RTC clock (40Khz)/(Asynch_prediv +1 x Synch_prediv+1) = new wake up clock frequency.It calls it 1Hz because it assumes you have set up the calendar to have a 1Hz frequency. Its too bad I can use the calendar as a wake up event.

enter image description here

Best Answer

The assumption about CK_SPRE being 1 Hz is caused by the fact that this is essential for correct timekeeping.

What you are missing are the wakeup counters (below wakeup clock in your screen). That is - the RTC counts the clock pulses (say coming from CK_SPRE) and only wakes you up when the count reaches the value set in wakeup counter. Assuming CK_SPRE is at 1 Hz this gives you a maximum wakeup period of 36 hours (as stated in the reference manual).

Now, where did those 48 days come from? Either the author somehow mistakenly multiplied the period by 2 ** 5 or he assumed that CK_SPRE is 1/32 Hz (which wrecks timekeeping).

There are two workarounds to this:

  • software counter
  • alarms

Software counters - simply use a variable in your code and do something like this in your main:

int i = 0;
while(true) {
    sleep(WFE); // forgot exact code
    ++i;
    if(i % 10 != 0)
        continue;

    // your stuff goes here
}

Use RTC alarms (which are just before wakeup counters in your book):

  • wake up (same WFE)
  • get current time from RTC
  • add N days to this time
  • set next alarm
  • do your stuff.