Skip to main content
LVers.1
Associate III
Jan 31, 2022 4:02 PM UTC
Question

seems access to the backup domain is not granted when calling RTC_EnterInitMode() and RTC is clocked by LSI, at least on STM32WB55

Recently i experimented with the RTC modules and came accross an issue where HAL_RTC_Init() fails because RTC_EnterInitMode() fail with HAL_TIMEOUT

After debugging, it seems that HAL_PWR_EnableBkUpAccess() need to be called before HAL_RTC_Init()

It appear that HAL_PWR_EnableBkUpAccess() is called if the LSE is used, but not if LSI is used. See line 775 of stm32wbxx_hal_rcc_ex.c.

Question: when is HAL_PWR_EnableBkUpAccess() supposed to be called ?

This topic has been closed for replies.

3 replies

Remy ISSALYS
ST Technical Moderator
Mar 17, 2022 3:50 PM UTC

Hello,

After reset, the backup domain is protected against possible unwanted write accesses. To enable access to RCC_BDCR to select the RTC source clock and RTC registers use HAL_PWR_EnableBkUpAccess() function.

Before to perform RTC_EnterInitMode(), we need to select the RTC clock source inside RCC_BDCR using the HAL_RCCEx_PeriphCLKConfig() function and enable RTC source clock using the __HAL_RCC_RTCAPB_CLK_ENABLE() function.

To perform these actions, the driver provides the weak function HAL_RTC_MspInit() that can be overwritten by user function with same name. This function is call inside HAL_RTC_Init() function just before RTC_EnterInitMode(). This function configures the hardware resources need to access to RTC registers

If you look BLE_HeartRate project, you can see that HAL_PWR_EnableBkUpAccess() is called in SystemClock_Config() and HAL_RTC_Init() is called in MX_RTC_Init(). So HAL_PWR_EnableBkUpAccess() is called before HAL_RTC_Init() and need to be called in this order.

Best Regards

LVers.1
LVers.1Author
Associate III
Mar 18, 2022 9:31 AM UTC

Dear Remy,

Thanks a lot for taking the time to answer my question.

I agree that HAL_PWR_EnableBkUpAccess() must be called before HAL_RTC_Init(). That's clear to me.

My concern was more about why HAL_PWR_EnableBkUpAccess() is called during HAL_RCC_OscConfig() when the LSE is used. (see the call to HAL_PWR_EnableBkUpAccess() line 775 of stm32wbxx_hal_rcc_ex.c) and not when the LSI is used (LSI condfiguration is handled between lines 626 to 762 of the same stm32wbxx_hal_rcc_ex.c and there is no call to HAL_PWR_EnableBkUpAccess()).

So basically, when using LSE to clock the RTC the HAL do enable access to the backup domain, but when using the LSI to clock the RTC, the user must call HAL_PWR_EnableBkUpAccess() himself.

I checked the BLE_HeartRate project, and indeed i see the call to HAL_PWR_EnableBkUpAccess() from SystemClock_Config()  . However i don't think i can modify this SystemClock_Config() function as it is generated by stm32cubeMX.

Your suggestion to use HAL_RTC_MspInit() to call HAL_PWR_EnableBkUpAccess() is very valuable and would indeed provide an excellent solution. Thank you.

Thanks for the support

Remy ISSALYS
ST Technical Moderator
Mar 21, 2022 3:44 PM UTC

Hello,

As the LSE is in the Backup domain and write access is denied to this domain after reset, you have to enable write access using HAL_PWR_EnableBkUpAccess() function before to configure the LSE. And I suppose that LSI isn't in the Backup domain, so don't need to enable write access to this domain.

Best Regards