cancel
Showing results for 
Search instead for 
Did you mean: 

Can I use backup registers when RTC is disabled?

MZadn.2
Associate II

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?

1 ACCEPTED SOLUTION

Accepted Solutions

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​ 

View solution in original post

6 REPLIES 6
KDJEM.1
ST Employee

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

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

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​ 

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.

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

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Why don't you use normal defined register accesses?

RTC->BKP0R = 0xCAFECAFE;

Thanks for the tip, this is indeed a more elegant way. I was erroneously supposing that the definition of BKP0R won't be included in the code generated by STM32CubeIDE when RTC is disabled.