@@ -82,9 +82,7 @@ void rtc_init(void)
82
82
if (HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct ) != HAL_OK ) {
83
83
error ("PeriphClkInitStruct RTC failed with LSE\n" );
84
84
}
85
-
86
85
#else /* !RTC_LSI */
87
-
88
86
__PWR_CLK_ENABLE ();
89
87
90
88
// Reset Backup domain
@@ -108,7 +106,6 @@ void rtc_init(void)
108
106
if (HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct ) != HAL_OK ) {
109
107
error ("PeriphClkInitStruct RTC failed with LSI\n" );
110
108
}
111
-
112
109
#endif /* !RTC_LSI */
113
110
114
111
// Enable RTC
@@ -173,11 +170,23 @@ void rtc_free(void)
173
170
}
174
171
175
172
/*
176
- RTC Registers
177
- RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday
178
- RTC_Month 1=january, 2=february, ..., 12=december
179
- RTC_Date day of the month 1-31
180
- RTC_Year year 0-99
173
+ ST RTC_DateTypeDef structure
174
+ WeekDay 1=monday, 2=tuesday, ..., 7=sunday
175
+ Month 0x1=january, 0x2=february, ..., 0x12=december
176
+ Date day of the month 1-31
177
+ Year year 0-99
178
+
179
+ ST RTC_TimeTypeDef structure
180
+ Hours 0-12 if the RTC_HourFormat_12 is selected during init
181
+ 0-23 if the RTC_HourFormat_24 is selected during init
182
+ Minutes 0-59
183
+ Seconds 0-59
184
+ TimeFormat RTC_HOURFORMAT12_AM/RTC_HOURFORMAT12_PM
185
+ SubSeconds time unit range between [0-1] Second with [1 Sec / SecondFraction +1] granularity
186
+ SecondFraction range or granularity of Sub Second register content corresponding to Synchronous pre-scaler factor value (PREDIV_S)
187
+ DayLightSaving RTC_DAYLIGHTSAVING_SUB1H/RTC_DAYLIGHTSAVING_ADD1H/RTC_DAYLIGHTSAVING_NONE
188
+ StoreOperation RTC_STOREOPERATION_RESET/RTC_STOREOPERATION_SET
189
+
181
190
struct tm
182
191
tm_sec seconds after the minute 0-61
183
192
tm_min minutes after the hour 0-59
@@ -189,6 +198,22 @@ void rtc_free(void)
189
198
tm_yday days since January 1 0-365
190
199
tm_isdst Daylight Saving Time flag
191
200
*/
201
+
202
+ /*
203
+ Information about STM32F0, STM32F2, STM32F3, STM32F4, STM32F7, STM32L0, STM32L1, STM32L4:
204
+ BCD format is used to store the date in the RTC. The year is store on 2 * 4 bits.
205
+ Because the first year is reserved to see if the RTC is init, the supposed range is 01-99.
206
+ 1st point is to cover the standard range from 1970 to 2038 (limited by the 32 bits of time_t).
207
+ 2nd point is to keep the year 1970 and the leap years synchronized.
208
+
209
+ So by moving it 68 years forward from 1970, it become 1969-2067 which include 1970-2038.
210
+ 68 is also a multiple of 4 so it let the leap year synchronized.
211
+
212
+ Information about STM32F1:
213
+ 32bit register is used (no BCD format) for the seconds and a software structure to store dates.
214
+ It is then not a problem to not use shifts.
215
+ */
216
+
192
217
time_t rtc_read (void )
193
218
{
194
219
RTC_DateTypeDef dateStruct ;
@@ -199,11 +224,11 @@ time_t rtc_read(void)
199
224
200
225
// Read actual date and time
201
226
// Warning: the time must be read first!
202
- HAL_RTC_GetTime (& RtcHandle , & timeStruct , FORMAT_BIN );
203
- HAL_RTC_GetDate (& RtcHandle , & dateStruct , FORMAT_BIN );
227
+ HAL_RTC_GetTime (& RtcHandle , & timeStruct , RTC_FORMAT_BIN );
228
+ HAL_RTC_GetDate (& RtcHandle , & dateStruct , RTC_FORMAT_BIN );
204
229
205
230
// Setup a tm structure based on the RTC
206
- timeinfo . tm_wday = dateStruct . WeekDay ;
231
+ /* tm_wday information is ignored by mktime */
207
232
timeinfo .tm_mon = dateStruct .Month - 1 ;
208
233
timeinfo .tm_mday = dateStruct .Date ;
209
234
timeinfo .tm_year = dateStruct .Year + 68 ;
@@ -230,7 +255,11 @@ void rtc_write(time_t t)
230
255
struct tm * timeinfo = localtime (& t );
231
256
232
257
// Fill RTC structures
233
- dateStruct .WeekDay = timeinfo -> tm_wday ;
258
+ if (timeinfo -> tm_wday == 0 ) {
259
+ dateStruct .WeekDay = 7 ;
260
+ } else {
261
+ dateStruct .WeekDay = timeinfo -> tm_wday ;
262
+ }
234
263
dateStruct .Month = timeinfo -> tm_mon + 1 ;
235
264
dateStruct .Date = timeinfo -> tm_mday ;
236
265
dateStruct .Year = timeinfo -> tm_year - 68 ;
@@ -245,21 +274,17 @@ void rtc_write(time_t t)
245
274
#endif /* TARGET_STM32F1 */
246
275
247
276
// Change the RTC current date/time
248
- HAL_RTC_SetDate (& RtcHandle , & dateStruct , FORMAT_BIN );
249
- HAL_RTC_SetTime (& RtcHandle , & timeStruct , FORMAT_BIN );
277
+ HAL_RTC_SetDate (& RtcHandle , & dateStruct , RTC_FORMAT_BIN );
278
+ HAL_RTC_SetTime (& RtcHandle , & timeStruct , RTC_FORMAT_BIN );
250
279
}
251
280
252
281
int rtc_isenabled (void )
253
282
{
254
- #if DEVICE_LOWPOWERTIMER
255
- if ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) {
256
- return 1 ;
257
- } else {
258
- return 0 ;
259
- }
260
- #else /* DEVICE_LOWPOWERTIMER */
261
- return 1 ;
262
- #endif /* DEVICE_LOWPOWERTIMER */
283
+ #if !(TARGET_STM32F1 )
284
+ return ( ((RTC -> ISR & RTC_ISR_INITS ) == RTC_ISR_INITS ) && ((RTC -> ISR & RTC_ISR_RSF ) == RTC_ISR_RSF ) );
285
+ #else /* TARGET_STM32F1 */
286
+ return ((RTC -> CRL & RTC_CRL_RSF ) == RTC_CRL_RSF );
287
+ #endif /* TARGET_STM32F1 */
263
288
}
264
289
265
290
#if DEVICE_LOWPOWERTIMER
@@ -308,5 +333,4 @@ void rtc_synchronize(void)
308
333
}
309
334
#endif /* DEVICE_LOWPOWERTIMER */
310
335
311
-
312
336
#endif /* DEVICE_RTC */
0 commit comments