136 void rtc_lld_init(void){ 137 138 /* Required because access to PRL.*/ 139 rtc_lld_apb1_sync(); 140 141 /* 142 * Writes preload register only if its value is not equal to desired value. 143 * 144 * Ref CD00171190: RM0008 Reference manual Cls 18.4.3 The RTC->PRL registers 145 * are write only. We must store the value for the pre-scaler in BKP->DR1 146 * and BKP->DR1 so we know it has been set. 147 * The pre-scaler must not be set on every reset as RTC clock counts are 148 * lost when it is set. 149 */ 150 if ((STM32_RTCCLK - 1) != ((((uint32_t)BKP->DR1) << 16) | BKP->DR2)){ 151 rtc_lld_acquire(); 152 RTC->PRLH = (uint16_t)((STM32_RTCCLK - 1) >> 16) & 0x000F; 153 BKP->DR1 = (uint16_t)((STM32_RTCCLK - 1) >> 16) & 0x000F; 154 RTC->PRLL = (uint16_t)(((STM32_RTCCLK - 1)) & 0xFFFF); 155 BKP->DR2 = (uint16_t)(((STM32_RTCCLK - 1)) & 0xFFFF); 156 rtc_lld_release(); 157 } ...
Is it necessary to do so and may need to add the settings in 'halconf.h' file what registers to use for this or save this values elsewhere?
The reason is in the comment, and we must use a non volatile register for this. I am not sure if there is an alternate way to verify if the RTC has already been initialized.
Because no compiler or the programmer will not know that the cell is used and may try to use it again! For example I use BKP-DR1 in BOOT_LOADER and now have problems. We must do something about it. Since the value for the prescaler is a constant, then only one bit is need to determine the prescaler is reset and it will reset when BKP power off. Somewhere in BKP hardware must be a status bit for this, but I can not find it so quickly.
55 /** 56 * @brief Wait for synchronization of RTC registers with APB1 bus. 57 * @details This function must be invoked before trying to read RTC registers 58 * in the backup domain: DIV, CNT, ALR. CR registers can always 59 * be read. 60 * 61 * @notapi 62 */ 63 #define rtc_lld_apb1_sync() {while ((RTC->CRL & RTC_CRL_RSF) == 0);}
Because datasheet (Ref CD00171190: RM0008 Reference manual Cls 18.4.2) says:
Bit 3 RSF:Registers synchronized flag This bit is set by hardware at each time the RTC_CNT and RTC_DIV registers are updated and cleared by software. Before any read operation after an APB1 reset or an APB1 clock stop, this bit must be cleared by software, and the user application must wait until it is set to be sure that the RTC_CNT, RTC_ALR orRTC_PRL registers are synchronized. 0: Registers not yet synchronized. 1: Registers synchronized.
Giovanni The reason is in the comment, and we must use a non volatile register for this. I am not sure if there is an alternate way to verify if the RTC has already been initialized.
I think RCC_BDCR_RTCEN bit in RCC->BDCR register must be used to detect that RTC hasn't been initialized yet but it is analyzed in hal_lld_backup_domain_init(): file: hal_lld.c
If I remeber correct, the reasons to store whole value of preload register is to avoid initializing of this register every system startup.
alexblack wrote:Because no compiler or the programmer will not know that the cell is used and may try to use it again! For example I use BKP-DR1 in BOOT_LOADER and now have problems. We must do something about it.
Giovanni wrote:It can be a normal configuration option.
Good idea. Let it be in mcuconf.h near the RTC
alexblack wrote:I think code must be as: Code: Select all 63 #define rtc_lld_apb1_sync() {RTC->CRL |= RTC_CRL_RSF; while ((RTC->CRL & RTC_CRL_RSF) == 0);}
Syncing with RTC is slow process (some LSE clocks). This bit must be cleared only once after APB1 start/reset so it must be done in begining of rtc_lld_init().