Skip to content

[RTC] Avoid reset time after low power mode #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cores/arduino/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
#include "interrupt.h"
#include "analog.h"
#include "backup.h"
#include "clock.h"
#include "core_callback.h"
#include "digital_io.h"
Expand Down
136 changes: 136 additions & 0 deletions cores/arduino/stm32/backup.h
Original file line number Diff line number Diff line change
@@ -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****/
13 changes: 3 additions & 10 deletions cores/arduino/stm32/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*
******************************************************************************
*/
#include "backup.h"
#include "clock.h"
#include "stm32yyxx_ll_cortex.h"

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
25 changes: 18 additions & 7 deletions cores/arduino/stm32/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -323,19 +327,15 @@ 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);
#endif /* !STM32F1xx && !STM32F2xx */

HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
/* Ensure backup domain is enabled */
enableBackupRegister();
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
5 changes: 4 additions & 1 deletion cores/arduino/stm32/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#define __RTC_H

/* Includes ------------------------------------------------------------------*/
#include <stdbool.h>
#include "backup.h"
#include "clock.h"

#ifdef HAL_RTC_MODULE_ENABLED
Expand Down Expand Up @@ -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);
Expand Down