2022-01-31 08:02 AM
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 ?
2022-03-17 08:50 AM
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
2022-03-18 02:31 AM
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
2022-03-21 08:44 AM
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