annotate Small_CPU/Src/rtc.c @ 232:f0069f002c55 div-fixes-4-1

Bugfix: make date/time setting work over reboots Setting the time/date over the UART interface or by the menu, seems to work, but a reboot of the RTE brings back strange, seemingly random, time. The reason for this is rather simple. In the settings, a time is stored, based on some flawed logic, and that time was restored on reboot. There is no reason to store any time, when the moment of restoring it is unrelated in time. So, the fix is simple: do not set time (in the RTC) based on some time from the past. The whole idea of a RTC is that it does preserve the time for you, as long its powered. Any attempt to do things better using stored time data is futile (and nonsense). And while working on his, also kick out some useless code from the RTE. There is no reason to initialize the time on the RTC to some random time/date in the past. A zero data/time is as good and any random date. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Wed, 03 Apr 2019 21:11:56 +0200
parents 5f11787b4f42
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3 * @file rtc.c
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
4 * @author heinrichs weikamp gmbh
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
5 * @version V0.0.1
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
6 * @date 10-Oct-2014
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
7 * @brief Source code for rtc control
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
8 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
9 @verbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
10 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
11 ##### How to use #####
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
12 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
13 @endverbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
14 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
15 * @attention
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
16 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
17 * <h2><center>&copy; COPYRIGHT(c) 2015 heinrichs weikamp</center></h2>
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
18 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
19 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
20 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
21 /* Includes ------------------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
22 #include "rtc.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
23 #include "stm32f4xx_hal.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
24 #include "stm32f4xx_hal_conf.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
25 #include "baseCPU2.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 RTC_HandleTypeDef RTCHandle;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29 static void RTC_Error_Handler(void);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
30
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32 void RTC_SetTime(RTC_TimeTypeDef stimestructure)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35 stimestructure.SubSeconds = 0;
232
f0069f002c55 Bugfix: make date/time setting work over reboots
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
36 stimestructure.TimeFormat = RTC_HOURFORMAT_24;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 if(HAL_RTC_SetTime(&RTCHandle, &stimestructure, FORMAT_BIN) != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 RTC_Error_Handler();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 void RTC_SetDate(RTC_DateTypeDef sdatestructure)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 if(HAL_RTC_SetDate(&RTCHandle, &sdatestructure, FORMAT_BIN) != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51 RTC_Error_Handler();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 /*
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 static void RTC_CalendarConfig(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59 RTC_DateTypeDef sdatestructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 RTC_TimeTypeDef stimestructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 //##-1- Configure the Date #################################################
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63 // Set Date: Monday April 14th 2014
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 sdatestructure.Year = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 sdatestructure.Month = RTC_MONTH_JANUARY;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 sdatestructure.Date = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67 sdatestructure.WeekDay = RTC_WEEKDAY_MONDAY;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 if(HAL_RTC_SetDate(&RTCHandle,&sdatestructure,FORMAT_BCD) != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 RTC_Error_Handler();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74 //##-2- Configure the Time #################################################
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75 // Set Time: 02:00:00
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 stimestructure.Hours = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 stimestructure.Minutes = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78 stimestructure.Seconds = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79 stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82 if(HAL_RTC_SetTime(&RTCHandle,&stimestructure,FORMAT_BCD) != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 RTC_Error_Handler();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87 //##-3- Writes a data in a RTC Backup data Register0 #######################
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88 // HAL_RTCEx_BKUPWrite(&RTCHandle,RTC_BKP_DR0,0x32F2);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 /* ##-1- Configure the RTC peripheral #######################################
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 Configure RTC prescaler and RTC data registers
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 RTC configured as follow:
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 - Hour Format = Format 24
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 - Asynch Prediv = Value according to source clock
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 - Synch Prediv = Value according to source clock
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 - OutPut = Output Disable
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100 - OutPutPolarity = High Polarity
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 - OutPutType = Open Drain
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 void MX_RTC_init(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106 {
232
f0069f002c55 Bugfix: make date/time setting work over reboots
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
107 /* Initialize RTC */
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 RTCHandle.Instance = RTC;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 RTCHandle.Init.AsynchPrediv = 127;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 RTCHandle.Init.SynchPrediv = 255;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114 RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 HAL_RTC_Init(&RTCHandle);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 void RTC_StopMode_2seconds(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 /* Enable Power Control clock */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122 __HAL_RCC_PWR_CLK_ENABLE();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124 /* Disable Wake-up timer */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125 HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 /* Enable Wake-up timer */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, (0x1000-1), RTC_WAKEUPCLOCK_RTCCLK_DIV16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 /* FLASH Deep Power Down Mode enabled */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 HAL_PWREx_EnableFlashPowerDown();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133 /*## Enter Stop Mode #######################################################*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
135
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136 /* Configures system clock after wake-up from STOP: enable HSI, PLL and select
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
137 PLL as system clock source (HSI and PLL are disabled in STOP mode) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
138 SYSCLKConfig_STOP();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
139
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
142
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
143
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144 void RTC_Stop_11ms(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
146 /* Disable Wake-up timer */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
147 HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
148
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
149 /* Enable Wake-up timer */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, (0x18-1), RTC_WAKEUPCLOCK_RTCCLK_DIV16);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 /* FLASH Deep Power Down Mode enabled */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153 HAL_PWREx_DisableFlashPowerDown();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155 /*## Enter Stop Mode #######################################################*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156 HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
157
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158 /* Configures system clock after wake-up from STOP: enable HSI, PLL and select
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159 PLL as system clock source (HSI and PLL are disabled in STOP mode) */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
160 SYSCLKConfig_STOP();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
161
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
162 HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
163 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
164
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
165
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
166 static void RTC_Error_Handler(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
167 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
168 while(1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
169 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
170
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
171
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
172 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/