Skip to content

Commit 3a83fc8

Browse files
committed
STM32 RTC api minor update
Add more detailed information in comments Issue with sunday corrected Issue with wrong rtc_isenabled status corrected
1 parent 7fc73e4 commit 3a83fc8

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

targets/TARGET_STM/rtc_api.c

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ void rtc_init(void)
8282
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
8383
error("PeriphClkInitStruct RTC failed with LSE\n");
8484
}
85-
8685
#else /* !RTC_LSI */
87-
8886
__PWR_CLK_ENABLE();
8987

9088
// Reset Backup domain
@@ -108,7 +106,6 @@ void rtc_init(void)
108106
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
109107
error("PeriphClkInitStruct RTC failed with LSI\n");
110108
}
111-
112109
#endif /* !RTC_LSI */
113110

114111
// Enable RTC
@@ -173,11 +170,23 @@ void rtc_free(void)
173170
}
174171

175172
/*
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+
181190
struct tm
182191
tm_sec seconds after the minute 0-61
183192
tm_min minutes after the hour 0-59
@@ -189,6 +198,22 @@ void rtc_free(void)
189198
tm_yday days since January 1 0-365
190199
tm_isdst Daylight Saving Time flag
191200
*/
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+
192217
time_t rtc_read(void)
193218
{
194219
RTC_DateTypeDef dateStruct;
@@ -199,11 +224,11 @@ time_t rtc_read(void)
199224

200225
// Read actual date and time
201226
// 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);
204229

205230
// Setup a tm structure based on the RTC
206-
timeinfo.tm_wday = dateStruct.WeekDay;
231+
/* tm_wday information is ignored by mktime */
207232
timeinfo.tm_mon = dateStruct.Month - 1;
208233
timeinfo.tm_mday = dateStruct.Date;
209234
timeinfo.tm_year = dateStruct.Year + 68;
@@ -230,7 +255,11 @@ void rtc_write(time_t t)
230255
struct tm *timeinfo = localtime(&t);
231256

232257
// 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+
}
234263
dateStruct.Month = timeinfo->tm_mon + 1;
235264
dateStruct.Date = timeinfo->tm_mday;
236265
dateStruct.Year = timeinfo->tm_year - 68;
@@ -245,21 +274,17 @@ void rtc_write(time_t t)
245274
#endif /* TARGET_STM32F1 */
246275

247276
// 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);
250279
}
251280

252281
int rtc_isenabled(void)
253282
{
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 */
263288
}
264289

265290
#if DEVICE_LOWPOWERTIMER
@@ -308,5 +333,4 @@ void rtc_synchronize(void)
308333
}
309334
#endif /* DEVICE_LOWPOWERTIMER */
310335

311-
312336
#endif /* DEVICE_RTC */

0 commit comments

Comments
 (0)