Skip to content

Commit d82b943

Browse files
authored
Merge pull request stm32duino#442 from fpistm/RTC_BackUp
[RTC] Avoid reset time after low power mode
2 parents 3a7996f + 65f4c1b commit d82b943

File tree

5 files changed

+162
-18
lines changed

5 files changed

+162
-18
lines changed

Diff for: cores/arduino/board.h

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
#include "interrupt.h"
88
#include "analog.h"
9+
#include "backup.h"
910
#include "clock.h"
1011
#include "core_callback.h"
1112
#include "digital_io.h"

Diff for: cores/arduino/stm32/backup.h

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
2+
******************************************************************************
3+
* @file backup.h
4+
* @author fpistm
5+
* @brief Header for backup domain driver
6+
******************************************************************************
7+
* @attention
8+
*
9+
* Copyright (c) 2017 STMicroelectronics.
10+
* All rights reserved.
11+
*
12+
* This software component is licensed by ST under BSD 3-Clause license,
13+
* the "License"; You may not use this file except in compliance with the
14+
* License. You may obtain a copy of the License at:
15+
* opensource.org/licenses/BSD-3-Clause
16+
*
17+
******************************************************************************
18+
*/
19+
20+
/* Define to prevent recursive inclusion -------------------------------------*/
21+
#ifndef __BACKUP_H
22+
#define __BACKUP_H
23+
24+
/* Includes ------------------------------------------------------------------*/
25+
#include "stm32_def.h"
26+
#include "stm32yyxx_ll_rtc.h"
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
/* Exported macro ------------------------------------------------------------*/
33+
#if (!defined(STM32F0xx) && !defined(STM32F3xx) && !defined(STM32L0xx) &&\
34+
!defined(STM32L1xx) && !defined(STM32L4xx)) || defined(RTC_BACKUP_SUPPORT)
35+
#if !defined(STM32L412xx) && !defined(STM32L422xx)
36+
#define ENABLE_BACKUP_SUPPORT
37+
#endif
38+
#endif
39+
40+
#if !defined(RTC_BKP_INDEX) && defined(ENABLE_BACKUP_SUPPORT)
41+
#define RTC_BKP_INDEX LL_RTC_BKP_DR1
42+
#else
43+
#define RTC_BKP_INDEX 0
44+
#endif
45+
#ifndef RTC_BKP_VALUE
46+
#define RTC_BKP_VALUE 0x32F2
47+
#endif
48+
49+
50+
/* Exported functions ------------------------------------------------------- */
51+
static inline void resetBackupRegister(void)
52+
{
53+
#ifdef HAL_PWR_MODULE_ENABLED
54+
/* Enable access to the RTC registers */
55+
HAL_PWR_EnableBkUpAccess();
56+
/**
57+
* Write twice the value to flush the APB-AHB bridge
58+
* This bit shall be written in the register before writing the next one
59+
*/
60+
HAL_PWR_EnableBkUpAccess();
61+
#endif
62+
__HAL_RCC_BACKUPRESET_FORCE();
63+
__HAL_RCC_BACKUPRESET_RELEASE();
64+
}
65+
66+
static inline void enableBackupRegister(void)
67+
{
68+
/* Enable Power Clock */
69+
#ifdef __HAL_RCC_PWR_IS_CLK_DISABLED
70+
if (__HAL_RCC_PWR_IS_CLK_DISABLED()) {
71+
__HAL_RCC_PWR_CLK_ENABLE();
72+
}
73+
#endif
74+
#ifdef HAL_PWR_MODULE_ENABLED
75+
/* Allow access to Backup domain */
76+
HAL_PWR_EnableBkUpAccess();
77+
#endif
78+
#ifdef __HAL_RCC_BKP_CLK_ENABLE
79+
/* Enable BKP CLK enable for backup registers */
80+
__HAL_RCC_BKP_CLK_ENABLE();
81+
#endif
82+
}
83+
84+
static inline void disableBackupRegister(void)
85+
{
86+
#ifdef HAL_PWR_MODULE_ENABLED
87+
/* Forbid access to Backup domain */
88+
HAL_PWR_DisableBkUpAccess();
89+
#endif
90+
#ifdef __HAL_RCC_BKP_CLK_DISABLE
91+
/* Disable BKP CLK enable for backup registers */
92+
__HAL_RCC_BKP_CLK_DISABLE();
93+
#endif
94+
/* Disable Power Clock */
95+
#ifdef __HAL_RCC_PWR_IS_CLK_DISABLED
96+
if (!__HAL_RCC_PWR_IS_CLK_DISABLED()) {
97+
__HAL_RCC_PWR_CLK_DISABLE();
98+
}
99+
#endif
100+
}
101+
102+
static inline void setBackupRegister(uint32_t index, uint32_t value)
103+
{
104+
#if defined(STM32F1xx)
105+
LL_RTC_BKP_SetRegister(BKP, index, value);
106+
#else
107+
#ifdef ENABLE_BACKUP_SUPPORT
108+
LL_RTC_BAK_SetRegister(RTC, index, value);
109+
#else
110+
UNUSED(index);
111+
UNUSED(value);
112+
#endif
113+
#endif
114+
}
115+
116+
static inline uint32_t getBackupRegister(uint32_t index)
117+
{
118+
#if defined(STM32F1xx)
119+
return LL_RTC_BKP_GetRegister(BKP, index);
120+
#else
121+
#ifdef ENABLE_BACKUP_SUPPORT
122+
return LL_RTC_BAK_GetRegister(RTC, index);
123+
#else
124+
UNUSED(index);
125+
return 0;
126+
#endif
127+
#endif
128+
}
129+
130+
#ifdef __cplusplus
131+
}
132+
#endif
133+
134+
#endif /* __BACKUP_H */
135+
136+
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Diff for: cores/arduino/stm32/clock.c

+3-10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*
3636
******************************************************************************
3737
*/
38+
#include "backup.h"
3839
#include "clock.h"
3940
#include "stm32yyxx_ll_cortex.h"
4041

@@ -98,6 +99,8 @@ void enableClock(sourceClock_t source)
9899
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
99100
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
100101

102+
enableBackupRegister();
103+
101104
switch (source) {
102105
case LSI_CLOCK:
103106
#ifdef STM32WBxx
@@ -118,16 +121,6 @@ void enableClock(sourceClock_t source)
118121
}
119122
break;
120123
case LSE_CLOCK:
121-
/* Enable Power Clock */
122-
#if !defined(STM32H7xx) && !defined(STM32WBxx)
123-
if (__HAL_RCC_PWR_IS_CLK_DISABLED()) {
124-
__HAL_RCC_PWR_CLK_ENABLE();
125-
}
126-
#endif
127-
#ifdef HAL_PWR_MODULE_ENABLED
128-
/* Allow access to Backup domain */
129-
HAL_PWR_EnableBkUpAccess();
130-
#endif
131124
if (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) {
132125
#ifdef __HAL_RCC_LSEDRIVE_CONFIG
133126
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);

Diff for: cores/arduino/stm32/rtc.c

+18-7
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,14 @@ static void RTC_computePrediv(int8_t *asynch, int16_t *synch)
292292
* @param format: enable the RTC in 12 or 24 hours mode
293293
* @retval None
294294
*/
295-
void RTC_init(hourFormat_t format, sourceClock_t source)
295+
void RTC_init(hourFormat_t format, sourceClock_t source, bool reset)
296296
{
297297
initFormat = format;
298298

299+
if (reset) {
300+
resetBackupRegister();
301+
}
302+
299303
/* Init RTC clock */
300304
RTC_initClock(source);
301305

@@ -323,19 +327,15 @@ void RTC_init(hourFormat_t format, sourceClock_t source)
323327

324328
HAL_RTC_Init(&RtcHandle);
325329

326-
/*Sunday 1st January 2017*/
327-
RTC_SetDate(17, 1, 1, 7);
328-
329-
/*at 0:0:0*/
330-
RTC_SetTime(0, 0, 0, 0, HOUR_AM);
331-
332330
#if !defined(STM32F1xx) && !defined(STM32F2xx) && !defined(STM32L1xx) || defined(STM32L1_ULPH)
333331
/* Enable Direct Read of the calendar registers (not through Shadow) */
334332
HAL_RTCEx_EnableBypassShadow(&RtcHandle);
335333
#endif /* !STM32F1xx && !STM32F2xx */
336334

337335
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 2, 0);
338336
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
337+
/* Ensure backup domain is enabled */
338+
enableBackupRegister();
339339
}
340340

341341
/**
@@ -349,6 +349,15 @@ void RTC_DeInit(void)
349349
callbackUserData = NULL;
350350
}
351351

352+
/**
353+
* @brief Check wether time is already set
354+
* @retval True if set else false
355+
*/
356+
bool RTC_IsTimeSet(void)
357+
{
358+
return (getBackupRegister(RTC_BKP_INDEX) == RTC_BKP_VALUE) ? true : false;
359+
}
360+
352361
/**
353362
* @brief Set RTC time
354363
* @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
392401
#endif /* !STM32F1xx */
393402

394403
HAL_RTC_SetTime(&RtcHandle, &RTC_TimeStruct, RTC_FORMAT_BIN);
404+
setBackupRegister(RTC_BKP_INDEX, RTC_BKP_VALUE);
395405
}
396406
}
397407

@@ -453,6 +463,7 @@ void RTC_SetDate(uint8_t year, uint8_t month, uint8_t day, uint8_t wday)
453463
RTC_DateStruct.Date = day;
454464
RTC_DateStruct.WeekDay = wday;
455465
HAL_RTC_SetDate(&RtcHandle, &RTC_DateStruct, RTC_FORMAT_BIN);
466+
setBackupRegister(RTC_BKP_INDEX, RTC_BKP_VALUE);
456467
}
457468
}
458469

Diff for: cores/arduino/stm32/rtc.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#define __RTC_H
4141

4242
/* Includes ------------------------------------------------------------------*/
43+
#include <stdbool.h>
44+
#include "backup.h"
4345
#include "clock.h"
4446

4547
#ifdef HAL_RTC_MODULE_ENABLED
@@ -135,8 +137,9 @@ void RTC_SetClockSource(sourceClock_t source);
135137
void RTC_getPrediv(int8_t *asynch, int16_t *synch);
136138
void RTC_setPrediv(int8_t asynch, int16_t synch);
137139

138-
void RTC_init(hourFormat_t format, sourceClock_t source);
140+
void RTC_init(hourFormat_t format, sourceClock_t source, bool reset);
139141
void RTC_DeInit(void);
142+
bool RTC_IsTimeSet(void);
140143

141144
void RTC_SetTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, hourAM_PM_t period);
142145
void RTC_GetTime(uint8_t *hours, uint8_t *minutes, uint8_t *seconds, uint32_t *subSeconds, hourAM_PM_t *period);

0 commit comments

Comments
 (0)