From dd7297fb64eb94fe06a89fd9e8656f499cd8cc15 Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Thu, 16 Jun 2016 13:43:09 -0600 Subject: [PATCH] STM32L4: Add logic reset backup domain early in initialization --- arch/arm/src/stm32l4/stm32l4_rcc.c | 53 ++++++++++++++++++++++++++++- arch/arm/src/stm32l4/stm32l4_rtc.h | 20 +++++++++++ arch/arm/src/stm32l4/stm32l4_rtcc.c | 9 +---- 3 files changed, 73 insertions(+), 9 deletions(-) diff --git a/arch/arm/src/stm32l4/stm32l4_rcc.c b/arch/arm/src/stm32l4/stm32l4_rcc.c index 0d51d7afd1..202b90176f 100644 --- a/arch/arm/src/stm32l4/stm32l4_rcc.c +++ b/arch/arm/src/stm32l4/stm32l4_rcc.c @@ -89,7 +89,58 @@ ****************************************************************************/ /************************************************************************************ - * Name: stm32l4_clockconfig + * Name: rcc_resetbkp + * + * Description: + * The RTC needs to reset the Backup Domain to change RTCSEL and resetting the + * Backup Domain renders to disabling the LSE as consequence. In order to avoid + * resetting the Backup Domain when we already configured LSE we will reset the + * Backup Domain early (here). + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ************************************************************************************/ + +#if defined(CONFIG_STM32_PWR) && defined(CONFIG_RTC) +static inline rcc_resetbkp(void) +{ + bool init_stat; + + /* Check if the RTC is already configured */ + + init_stat = rtc_is_inits(); + if(!init_stat) + { + /* Enable write access to the backup domain (RTC registers, RTC + * backup data registers and backup SRAM). + */ + + (void)stm32l4_pwr_enablebkp(true); + + /* We might be changing RTCSEL - to ensure such changes work, we must + * reset the backup domain (having backed up the RTC_MAGIC token) + */ + + modifyreg32(STM32L4_RCC_BDCR, 0, RCC_BDCR_BDRST); + modifyreg32(STM32L4_RCC_BDCR, RCC_BDCR_BDRST, 0); + + (void)stm32l4_pwr_enablebkp(false); + } +} +#else +# define rcc_resetbkp() +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/************************************************************************************ + * Name: stm32_clockconfig * * Description: * Called to establish the clock settings based on the values in board.h. This diff --git a/arch/arm/src/stm32l4/stm32l4_rtc.h b/arch/arm/src/stm32l4/stm32l4_rtc.h index b155bef8b0..222f5bc7c0 100644 --- a/arch/arm/src/stm32l4/stm32l4_rtc.h +++ b/arch/arm/src/stm32l4/stm32l4_rtc.h @@ -105,6 +105,26 @@ extern "C" * Public Functions ****************************************************************************/ +/************************************************************************************ + * Name: rtc_is_inits + * + * Description: + * Returns 'true' if the RTC has been initialized (according to the RTC itself). + * It will be 'false' if the RTC has never been initialized since first time power + * up, and the counters are stopped until it is first initialized. + * + * Input Parameters: + * None + * + * Returned Value: + * bool -- true if the INITS flag is set in the ISR. + * + ************************************************************************************/ +#ifdef CONFIG_RTC_DRIVER +bool rtc_is_inits(void); +#endif + + /**************************************************************************** * Name: stm32l4_rtc_getdatetime_with_subseconds * diff --git a/arch/arm/src/stm32l4/stm32l4_rtcc.c b/arch/arm/src/stm32l4/stm32l4_rtcc.c index c894fb54d8..830425e1a7 100644 --- a/arch/arm/src/stm32l4/stm32l4_rtcc.c +++ b/arch/arm/src/stm32l4/stm32l4_rtcc.c @@ -250,7 +250,7 @@ static void rtc_dumptime(FAR const struct tm *tp, FAR const char *msg) * ************************************************************************************/ -static bool rtc_is_inits(void) +bool rtc_is_inits(void) { uint32_t regval; @@ -832,13 +832,6 @@ int up_rtc_initialize(void) (void)stm32l4_pwr_enablebkp(true); -#if 0 - /* Do not reset the backup domain; you will lose your clock setup done in *rcc.c */ - - modifyreg32(STM32L4_RCC_BDCR, 0, RCC_BDCR_BDRST); - modifyreg32(STM32L4_RCC_BDCR, RCC_BDCR_BDRST, 0); -#endif - #if defined(CONFIG_STM32L4_RTC_HSECLOCK) modifyreg32(STM32L4_RCC_BDCR, RCC_BDCR_RTCSEL_MASK, RCC_BDCR_RTCSEL_HSE); #elif defined(CONFIG_STM32L4_RTC_LSICLOCK)