Mercurial > public > ostc4
comparison 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 | 9b29995d6619 |
comparison
equal
deleted
inserted
replaced
231:f6961efb3794 | 232:f0069f002c55 |
---|---|
31 | 31 |
32 void RTC_SetTime(RTC_TimeTypeDef stimestructure) | 32 void RTC_SetTime(RTC_TimeTypeDef stimestructure) |
33 { | 33 { |
34 | 34 |
35 stimestructure.SubSeconds = 0; | 35 stimestructure.SubSeconds = 0; |
36 stimestructure.TimeFormat = RTC_HOURFORMAT12_AM; | 36 stimestructure.TimeFormat = RTC_HOURFORMAT_24; |
37 stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; | 37 stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; |
38 stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; | 38 stimestructure.StoreOperation = RTC_STOREOPERATION_RESET; |
39 | 39 |
40 if(HAL_RTC_SetTime(&RTCHandle, &stimestructure, FORMAT_BIN) != HAL_OK) | 40 if(HAL_RTC_SetTime(&RTCHandle, &stimestructure, FORMAT_BIN) != HAL_OK) |
41 { | 41 { |
102 */ | 102 */ |
103 | 103 |
104 | 104 |
105 void MX_RTC_init(void) | 105 void MX_RTC_init(void) |
106 { | 106 { |
107 | 107 /* Initialize RTC */ |
108 RTC_TimeTypeDef sTime; | |
109 RTC_DateTypeDef sDate; | |
110 // RTC_AlarmTypeDef sAlarm; | |
111 | |
112 /**Initialize RTC and set the Time and Date | |
113 */ | |
114 RTCHandle.Instance = RTC; | 108 RTCHandle.Instance = RTC; |
115 RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24; | 109 RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24; |
116 RTCHandle.Init.AsynchPrediv = 127; | 110 RTCHandle.Init.AsynchPrediv = 127; |
117 RTCHandle.Init.SynchPrediv = 255; | 111 RTCHandle.Init.SynchPrediv = 255; |
118 RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE; | 112 RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE; |
119 RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; | 113 RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
120 RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; | 114 RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
121 HAL_RTC_Init(&RTCHandle); | 115 HAL_RTC_Init(&RTCHandle); |
122 | |
123 sTime.Hours = 11; | |
124 sTime.Minutes = 0; | |
125 sTime.Seconds = 0; | |
126 sTime.SubSeconds = 0; | |
127 sTime.TimeFormat = RTC_HOURFORMAT12_AM; | |
128 sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; | |
129 sTime.StoreOperation = RTC_STOREOPERATION_RESET; | |
130 HAL_RTC_SetTime(&RTCHandle, &sTime, FORMAT_BCD); | |
131 | |
132 sDate.WeekDay = RTC_WEEKDAY_SUNDAY; | |
133 sDate.Month = RTC_MONTH_FEBRUARY; | |
134 sDate.Date = 15; | |
135 sDate.Year = 17; | |
136 HAL_RTC_SetDate(&RTCHandle, &sDate, FORMAT_BCD); | |
137 | |
138 | |
139 /* | |
140 RTCHandle.Instance = RTC; | |
141 RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24; | |
142 RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; | |
143 RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV; | |
144 RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE; | |
145 RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; | |
146 RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; | |
147 | |
148 HAL_RTC_Init(&RTCHandle); | |
149 */ | |
150 } | 116 } |
151 | 117 |
152 | 118 |
153 void RTC_StopMode_2seconds(void) | 119 void RTC_StopMode_2seconds(void) |
154 { | 120 { |