Skip to main content
Visitor II
August 21, 2022
Solved

Can I use backup registers when RTC is disabled?

  • August 21, 2022
  • 2 replies
  • 3219 views

I need the RTC module to be enabled in order to write to & read from backup registers on STM32F401 using these functions:

void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)

However, the datasheet states that the lowest backup battery drain current (IDD_VBAT, typ. 0.1uA) can be achieved when RTC and LSE are off.

So is there a way to use the backup registers as a general memory while RTC is off?

    This topic has been closed for replies.
    Best answer by waclawek.jan

    Hi Kaouthar,

    > The BackUp Registers are part of the RTC peripheral so we will need to enable the RTC to be able to access them.

    I don't know what do you exactly mean by "enable the RTC" in terms of peripheral registers' content, but generally I disagree. Here's my take, please correct me if I am wrong:

    • RTC backup registers (RTC_BKPxR) in 'F401 are readable immediately after reset.
    • to be able write RTC_BKPxR, you need to set PWR_CR.DBP, nothing else.
    • RTC_BKPxR content is preserved during VDD down, if valid voltage is present on VBAT pin all the time. There's no need to enable anything in the peripheral registers for this.

    This all can be very easily checked, no program needed, just using the debugger. I don't have an 'F401 at hand and tested on 'F407, but I see nothing in the RM to indicate different behaviour.

    Enabling RTC clock by setting nonzero RCC_BDCR.RTCSEL and setting RCC_BDCR.RTCEN indeed increases current consumption on VBAT pin, and is not needed for using RTC_BKPxR alone.

    In some STM32, there is an extra bit, RCC_APBxENR.RTCAPBEN, which enables/disables APB clock for RTC. In 'F401, there is no such bit so it's of no concern here.

    JW

    @KDJEM.1​ 

    2 replies

    Technical Moderator
    August 31, 2022

    Hi @Community member​ and welcome to the Community :),

    The BackUp Registers are part of the RTC peripheral so we will need to enable the RTC to be able to access them.

    For that, I advise you to take a look at this article “How to use the STM32’s backup registers�?

    The main purpose VBAT is to be used as power supply for RTC, external clock 32 kHz oscillator and backup registers (through power switch) when VDD is not present.

    When your question is answered, please close this topic by choosing Select as Best. This will help other users find that answer faster.

    Kaouthar

    Super User
    August 31, 2022

    Hi Kaouthar,

    > The BackUp Registers are part of the RTC peripheral so we will need to enable the RTC to be able to access them.

    I don't know what do you exactly mean by "enable the RTC" in terms of peripheral registers' content, but generally I disagree. Here's my take, please correct me if I am wrong:

    • RTC backup registers (RTC_BKPxR) in 'F401 are readable immediately after reset.
    • to be able write RTC_BKPxR, you need to set PWR_CR.DBP, nothing else.
    • RTC_BKPxR content is preserved during VDD down, if valid voltage is present on VBAT pin all the time. There's no need to enable anything in the peripheral registers for this.

    This all can be very easily checked, no program needed, just using the debugger. I don't have an 'F401 at hand and tested on 'F407, but I see nothing in the RM to indicate different behaviour.

    Enabling RTC clock by setting nonzero RCC_BDCR.RTCSEL and setting RCC_BDCR.RTCEN indeed increases current consumption on VBAT pin, and is not needed for using RTC_BKPxR alone.

    In some STM32, there is an extra bit, RCC_APBxENR.RTCAPBEN, which enables/disables APB clock for RTC. In 'F401, there is no such bit so it's of no concern here.

    JW

    @KDJEM.1​ 

    MZadn.2Author
    Visitor II
    September 1, 2022

    Hi Jan & Kaouthar, thanks for your feedback,

    I tend to agree with Jan. Here's what I've made:

    • I temporarily enabled the RTC in STM32CubeIDE configurator, just like explained under point 5. in the link that Kaouthar pasted
    • I put some diagnostic output into the HAL_RTCEx_BKUPWrite function that gets included in the code when RTC is enabled, just to find out that the address of the 1st backup register on STM32F401 is 0x40002850
    • I disabled the RTC in IDE
    • Now I can simply read the backup registers as:
    #define ADDR_BKP_REG_0 0x40002850
    #define ADDR_BKP_REG_1 (ADDR_BKP_REG_0 + 4)
    #define ADDR_BKP_REG_2 (ADDR_BKP_REG_0 + 8)
    ...
    uint32_t reg0 = *(__IO uint32_t*) ADDR_BKP_REG_0;
    uint32_t reg1 = *(__IO uint32_t*) ADDR_BKP_REG_1;
    uint32_t reg2 = *(__IO uint32_t*) ADDR_BKP_REG_2;
    ...
    • And write to them:
    HAL_PWR_EnableBkUpAccess();
    *(__IO uint32_t*) ADDR_BKP_REG_0 = 0xCAFECAFE;
    *(__IO uint32_t*) ADDR_BKP_REG_1 = 0xBEEFBEEF;
    *(__IO uint32_t*) ADDR_BKP_REG_2 = 0x12345678;
    ...
    HAL_PWR_DisableBkUpAccess();

    This all works with the RTC being disabled.

    Technical Moderator
    September 2, 2022

    Hi,

    Based on some tests I made with Nucleo-STM32F410RB, I confirm that you can read and write the backup registers while the RTC is disabled.

    Kaouthar