From da626f7780edd07d4cc0249f18fbfd4efde7620f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Wed, 24 Mar 2021 00:23:47 +0100 Subject: [PATCH] application: use LSI clock for RTC --- application.c | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/application.c b/application.c index 816e777..da914d3 100644 --- a/application.c +++ b/application.c @@ -414,20 +414,13 @@ void main(void) // setup RTC puts("setup RTC: "); rcc_periph_clock_enable(RCC_RTC); // enable clock for RTC peripheral + rcc_osc_on(RCC_LSI); // enable LSI clock + while (!rcc_is_osc_ready(RCC_LSI)); // wait until clock is ready if (!(RCC_BDCR && RCC_BDCR_RTCEN)) { // the RTC has not been configured yet pwr_disable_backup_domain_write_protect(); // disable backup protection so we can set the RTC clock source rtc_unlock(); // enable writing RTC registers -#if defined(MINIF401) - rcc_osc_on(RCC_LSE); // enable LSE clock - while (!rcc_is_osc_ready(RCC_LSE)); // wait until clock is ready - rtc_set_prescaler(256, 128); // set clock prescaler to 32768 - RCC_BDCR = (RCC_BDCR & ~(RCC_BDCR_RTCSEL_MASK << RCC_BDCR_RTCSEL_SHIFT)) | (RCC_BDCR_RTCSEL_LSE << RCC_BDCR_RTCSEL_SHIFT); // select LSE as RTC clock source -#else - rcc_osc_on(RCC_LSI); // enable LSI clock - while (!rcc_is_osc_ready(RCC_LSI)); // wait until clock is ready rtc_set_prescaler(250, 128); // set clock prescaler to 32000 RCC_BDCR = (RCC_BDCR & ~(RCC_BDCR_RTCSEL_MASK << RCC_BDCR_RTCSEL_SHIFT)) | (RCC_BDCR_RTCSEL_LSI << RCC_BDCR_RTCSEL_SHIFT); // select LSI as RTC clock source -#endif RCC_BDCR |= RCC_BDCR_RTCEN; // enable RTC rtc_lock(); // protect RTC register against writing pwr_enable_backup_domain_write_protect(); // re-enable protection now that we configured the RTC clock @@ -441,11 +434,7 @@ void main(void) pwr_disable_backup_domain_write_protect(); // disable backup protection so we can write to the RTC registers rtc_unlock(); // enable writing RTC registers rtc_clear_wakeup_flag(); // clear flag for fresh start -#if defined(MINIF401) - rtc_set_wakeup_time((32768 / 2) / WAKEUP_FREQ - 1, RTC_CR_WUCLKSEL_RTC_DIV2); // set wakeup time based on LSE (keep highest precision, also enables the wakeup timer) -#else rtc_set_wakeup_time((32000 / 2) / WAKEUP_FREQ - 1, RTC_CR_WUCLKSEL_RTC_DIV2); // set wakeup time based on LSI (keep highest precision, also enables the wakeup timer) -#endif rtc_enable_wakeup_timer_interrupt(); // enable interrupt rtc_lock(); // disable writing RTC registers // important: do not re-enable backup_domain_write_protect, since this will prevent clearing flags (but RTC registers do not need to be unlocked)