Skip to main content
Explorer II
August 3, 2021
Question

How to avoid reset RTC after wake up from standby mode

  • August 3, 2021
  • 4 replies
  • 2872 views

Hello, everyone. I try to use standby mode and use RTC to wake up but I found that after wake up everything was reset. How can I avoid RTC reset after wake up? I want it to keep running.

    This topic has been closed for replies.

    4 replies

    Super User
    August 3, 2021

    Don't use Cube/HAL's RTC init function.

    JW

    Super User
    August 3, 2021

    In MX_RTC_Init, in the first user code section, detect if the RTC is already enabled and, if so, return from the function rather than initializing it.

    Visitor II
    December 4, 2023

    static void MX_RTC_Init(void)

    {

    ...

    /* USER CODE BEGIN Check_RTC_BKUP */

    if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) == 0x5A5AA5A5)

    {

    return;

    }

    else

    {

    HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x5A5AA5A5);

    }

    /* USER CODE END Check_RTC_BKUP */

    ...

    }

    Visitor II
    January 25, 2024

    Hi, could you please elaborate on the values 0x5A5AA5A5? 
    This bit of code helped and now the rtc works and keeps time even after waking up from Standby Mode,
    Im just a bit confused why. 
    Thank you!

    Graduate II
    January 25, 2024

    Been here before, Done this already, Don't do it again, flagging value...

    Can be any non-zero value, something non-trivial the better. Use your pin number twice?

    Super User
    January 25, 2024

    > Hi, could you please elaborate on the values 0x5A5AA5A5?

    That's just some "magic number", used with a hope, that the backup register won't contain that value by "mistake".

    Btw. backup registers are zeroed upon backup domain reset; but so are other registers in RTC; so there's not much reason to use the backup registers for this purpose. In fact, RTC does have a dedicated bit exactly for this purpose - RTC_ISR.INITS - although in some cases it may be better to use RTC_BDCR.RTCEN.

    > I'm just a bit confused why.

    Apparently, ST has reasons why the Cube-generated initialization is deliberately written to reset RTC.

    I don't use Cube.

    JW