diff --git a/cores/arduino/board.h b/cores/arduino/board.h index 0d79fcdfac..e759d7d474 100644 --- a/cores/arduino/board.h +++ b/cores/arduino/board.h @@ -6,6 +6,7 @@ */ #include "interrupt.h" #include "analog.h" +#include "backup.h" #include "clock.h" #include "core_callback.h" #include "digital_io.h" diff --git a/cores/arduino/stm32/backup.h b/cores/arduino/stm32/backup.h new file mode 100644 index 0000000000..06ba0b9339 --- /dev/null +++ b/cores/arduino/stm32/backup.h @@ -0,0 +1,136 @@ +/** + ****************************************************************************** + * @file backup.h + * @author fpistm + * @brief Header for backup domain driver + ****************************************************************************** + * @attention + * + * Copyright (c) 2017 STMicroelectronics. + * All rights reserved. + * + * This software component is licensed by ST under BSD 3-Clause license, + * the "License"; You may not use this file except in compliance with the + * License. You may obtain a copy of the License at: + * opensource.org/licenses/BSD-3-Clause + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __BACKUP_H +#define __BACKUP_H + +/* Includes ------------------------------------------------------------------*/ +#include "stm32_def.h" +#include "stm32yyxx_ll_rtc.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Exported macro ------------------------------------------------------------*/ +#if (!defined(STM32F0xx) && !defined(STM32F3xx) && !defined(STM32L0xx) &&\ + !defined(STM32L1xx) && !defined(STM32L4xx)) || defined(RTC_BACKUP_SUPPORT) +#if !defined(STM32L412xx) && !defined(STM32L422xx) +#define ENABLE_BACKUP_SUPPORT +#endif +#endif + +#if !defined(RTC_BKP_INDEX) && defined(ENABLE_BACKUP_SUPPORT) +#define RTC_BKP_INDEX LL_RTC_BKP_DR1 +#else +#define RTC_BKP_INDEX 0 +#endif +#ifndef RTC_BKP_VALUE +#define RTC_BKP_VALUE 0x32F2 +#endif + + +/* Exported functions ------------------------------------------------------- */ +static inline void resetBackupRegister(void) +{ +#ifdef HAL_PWR_MODULE_ENABLED + /* Enable access to the RTC registers */ + HAL_PWR_EnableBkUpAccess(); + /** + * Write twice the value to flush the APB-AHB bridge + * This bit shall be written in the register before writing the next one + */ + HAL_PWR_EnableBkUpAccess(); +#endif + __HAL_RCC_BACKUPRESET_FORCE(); + __HAL_RCC_BACKUPRESET_RELEASE(); +} + +static inline void enableBackupRegister(void) +{ + /* Enable Power Clock */ +#ifdef __HAL_RCC_PWR_IS_CLK_DISABLED + if (__HAL_RCC_PWR_IS_CLK_DISABLED()) { + __HAL_RCC_PWR_CLK_ENABLE(); + } +#endif +#ifdef HAL_PWR_MODULE_ENABLED + /* Allow access to Backup domain */ + HAL_PWR_EnableBkUpAccess(); +#endif +#ifdef __HAL_RCC_BKP_CLK_ENABLE + /* Enable BKP CLK enable for backup registers */ + __HAL_RCC_BKP_CLK_ENABLE(); +#endif +} + +static inline void disableBackupRegister(void) +{ +#ifdef HAL_PWR_MODULE_ENABLED + /* Forbid access to Backup domain */ + HAL_PWR_DisableBkUpAccess(); +#endif +#ifdef __HAL_RCC_BKP_CLK_DISABLE + /* Disable BKP CLK enable for backup registers */ + __HAL_RCC_BKP_CLK_DISABLE(); +#endif + /* Disable Power Clock */ +#ifdef __HAL_RCC_PWR_IS_CLK_DISABLED + if (!__HAL_RCC_PWR_IS_CLK_DISABLED()) { + __HAL_RCC_PWR_CLK_DISABLE(); + } +#endif +} + +static inline void setBackupRegister(uint32_t index, uint32_t value) +{ +#if defined(STM32F1xx) + LL_RTC_BKP_SetRegister(BKP, index, value); +#else +#ifdef ENABLE_BACKUP_SUPPORT + LL_RTC_BAK_SetRegister(RTC, index, value); +#else + UNUSED(index); + UNUSED(value); +#endif +#endif +} + +static inline uint32_t getBackupRegister(uint32_t index) +{ +#if defined(STM32F1xx) + return LL_RTC_BKP_GetRegister(BKP, index); +#else +#ifdef ENABLE_BACKUP_SUPPORT + return LL_RTC_BAK_GetRegister(RTC, index); +#else + UNUSED(index); + return 0; +#endif +#endif +} + +#ifdef __cplusplus +} +#endif + +#endif /* __BACKUP_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/cores/arduino/stm32/clock.c b/cores/arduino/stm32/clock.c index c88d535add..7dac1da133 100644 --- a/cores/arduino/stm32/clock.c +++ b/cores/arduino/stm32/clock.c @@ -35,6 +35,7 @@ * ****************************************************************************** */ +#include "backup.h" #include "clock.h" #include "stm32yyxx_ll_cortex.h" @@ -98,6 +99,8 @@ void enableClock(sourceClock_t source) RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; + enableBackupRegister(); + switch (source) { case LSI_CLOCK: #ifdef STM32WBxx @@ -118,16 +121,6 @@ void enableClock(sourceClock_t source) } break; case LSE_CLOCK: - /* Enable Power Clock */ -#if !defined(STM32H7xx) && !defined(STM32WBxx) - if (__HAL_RCC_PWR_IS_CLK_DISABLED()) { - __HAL_RCC_PWR_CLK_ENABLE(); - } -#endif -#ifdef HAL_PWR_MODULE_ENABLED - /* Allow access to Backup domain */ - HAL_PWR_EnableBkUpAccess(); -#endif if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) { #ifdef __HAL_RCC_LSEDRIVE_CONFIG __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); diff --git a/cores/arduino/stm32/rtc.c b/cores/arduino/stm32/rtc.c index e1e23be949..620920b6d3 100644 --- a/cores/arduino/stm32/rtc.c +++ b/cores/arduino/stm32/rtc.c @@ -292,10 +292,14 @@ static void RTC_computePrediv(int8_t *asynch, int16_t *synch) * @param format: enable the RTC in 12 or 24 hours mode * @retval None */ -void RTC_init(hourFormat_t format, sourceClock_t source) +void RTC_init(hourFormat_t format, sourceClock_t source, bool reset) { initFormat = format; + if (reset) { + resetBackupRegister(); + } + /* Init RTC clock */ RTC_initClock(source); @@ -323,12 +327,6 @@ void RTC_init(hourFormat_t format, sourceClock_t source) HAL_RTC_Init(&RtcHandle); - /*Sunday 1st January 2017*/ - RTC_SetDate(17, 1, 1, 7); - - /*at 0:0:0*/ - RTC_SetTime(0, 0, 0, 0, HOUR_AM); - #if !defined(STM32F1xx) && !defined(STM32F2xx) && !defined(STM32L1xx) || defined(STM32L1_ULPH) /* Enable Direct Read of the calendar registers (not through Shadow) */ HAL_RTCEx_EnableBypassShadow(&RtcHandle); @@ -336,6 +334,8 @@ void RTC_init(hourFormat_t format, sourceClock_t source) HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 2, 0); HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); + /* Ensure backup domain is enabled */ + enableBackupRegister(); } /** @@ -349,6 +349,15 @@ void RTC_DeInit(void) callbackUserData = NULL; } +/** + * @brief Check wether time is already set + * @retval True if set else false + */ +bool RTC_IsTimeSet(void) +{ + return (getBackupRegister(RTC_BKP_INDEX) == RTC_BKP_VALUE) ? true : false; +} + /** * @brief Set RTC time * @param hours: 0-12 or 0-23. Depends on the format used. @@ -392,6 +401,7 @@ void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSe #endif /* !STM32F1xx */ HAL_RTC_SetTime(&RtcHandle, &RTC_TimeStruct, RTC_FORMAT_BIN); + setBackupRegister(RTC_BKP_INDEX, RTC_BKP_VALUE); } } @@ -453,6 +463,7 @@ void RTC_SetDate(uint8_t year, uint8_t month, uint8_t day, uint8_t wday) RTC_DateStruct.Date = day; RTC_DateStruct.WeekDay = wday; HAL_RTC_SetDate(&RtcHandle, &RTC_DateStruct, RTC_FORMAT_BIN); + setBackupRegister(RTC_BKP_INDEX, RTC_BKP_VALUE); } } diff --git a/cores/arduino/stm32/rtc.h b/cores/arduino/stm32/rtc.h index cb90b71ebe..644d990b89 100644 --- a/cores/arduino/stm32/rtc.h +++ b/cores/arduino/stm32/rtc.h @@ -40,6 +40,8 @@ #define __RTC_H /* Includes ------------------------------------------------------------------*/ +#include +#include "backup.h" #include "clock.h" #ifdef HAL_RTC_MODULE_ENABLED @@ -135,8 +137,9 @@ void RTC_SetClockSource(sourceClock_t source); void RTC_getPrediv(int8_t *asynch, int16_t *synch); void RTC_setPrediv(int8_t asynch, int16_t synch); -void RTC_init(hourFormat_t format, sourceClock_t source); +void RTC_init(hourFormat_t format, sourceClock_t source, bool reset); void RTC_DeInit(void); +bool RTC_IsTimeSet(void); void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, hourAM_PM_t period); void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *subSeconds, hourAM_PM_t *period);