stm32/rtc: Fix RTC init to use LSI if LSI is already selected on boot.

Upon boot the RTC early-init function should detect if LSE or LSI is
already selected/running and, if so, use it.  When the LSI has previously
(in the previous reset cycle) been selected as the clock source the only
way to reliably tell is if the RTCSEL bits of the RCC_BDCR are set to the
correct LSI value.  In particular the RCC_CSR bits for LSI control do not
indicate if the LSI is ready even if it is selected.

This patch removes the check on the RCC_CSR bits for the LSI being on and
ready and only uses the check on the RCC_BDCR to see if the LSI should be
used straightaway.  This was tested on a PYBLITEv1.0 and with the patch the
LSI persists correctly as the RTC source as long as the backup domain
remains powered.
This commit is contained in:
Damien George 2018-02-05 15:12:22 +11:00
parent 5a62f0faa6
commit 011d1555cb
1 changed files with 4 additions and 3 deletions

View File

@ -132,13 +132,14 @@ void rtc_init_start(bool force_init) {
// provide some status information
rtc_info |= 0x40000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8;
return;
} else if (((RCC->BDCR & RCC_BDCR_RTCSEL) == RCC_BDCR_RTCSEL_1) && ((RCC->CSR & 3) == 3)) {
// LSI configured & enabled & ready --> no need to (re-)init RTC
} else if ((RCC->BDCR & RCC_BDCR_RTCSEL) == RCC_BDCR_RTCSEL_1) {
// LSI configured as the RTC clock source --> no need to (re-)init RTC
// remove Backup Domain write protection
HAL_PWR_EnableBkUpAccess();
// Clear source Reset Flag
__HAL_RCC_CLEAR_RESET_FLAGS();
RCC->CSR |= 1;
// Turn the LSI on (it may need this even if the RTC is running)
RCC->CSR |= RCC_CSR_LSION;
// provide some status information
rtc_info |= 0x80000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8;
return;