16 #define _SEC_IN_MINUTE 60L
17 #define _SEC_IN_HOUR 3600L
18 #define _SEC_IN_DAY 86400L
19 #define YEAR_BASE 1900
21 static const int _DAYS_BEFORE_MONTH[12] =
22 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
24 #define _ISLEAP(y) (((y) % 4) == 0 && (((y) % 100) != 0 || (((y)+1900) % 400) == 0))
25 #define _DAYS_IN_YEAR(year) (_ISLEAP(year) ? 366 : 365)
27 static time_t timegm(
struct tm *tim_p)
34 tim += tim_p->tm_sec + (tim_p->tm_min * _SEC_IN_MINUTE) +
35 (tim_p->tm_hour * _SEC_IN_HOUR);
38 days += tim_p->tm_mday - 1;
39 days += _DAYS_BEFORE_MONTH[tim_p->tm_mon];
40 if (tim_p->tm_mon > 1 && _DAYS_IN_YEAR (tim_p->tm_year) == 366)
44 tim_p->tm_yday = days;
46 if (tim_p->tm_year > 10000 || tim_p->tm_year < -10000)
50 if ((year = tim_p->tm_year) > 70)
52 for (year = 70; year < tim_p->tm_year; year++)
53 days += _DAYS_IN_YEAR (year);
57 for (year = 69; year > tim_p->tm_year; year--)
58 days -= _DAYS_IN_YEAR (year);
59 days -= _DAYS_IN_YEAR (year);
63 tim += (days * _SEC_IN_DAY);
66 tim_p->tm_isdst = isdst;
69 if ((tim_p->tm_wday = (days + 4) % 7) < 0)
75 int _gettimeofday_r(
struct _reent *ptr,
struct timeval *tv,
void *tz)
88 RTC_GetTime(RTC_Format_BIN,&time);
89 RTC_GetDate(RTC_Format_BIN,&date);
94 ptr->_errno = -EFAULT;
109 tv->tv_sec = timegm(&tm);
111 prediv_s = (RTC->PRER & RTC_PRER_PREDIV_S);
112 tv->tv_usec = 1000000*(prediv_s - subsecond)/(prediv_s + 1);
125 ErrorStatus stm32_status;
127 PWR_BackupAccessCmd(ENABLE);
128 RCC_LSEConfig(RCC_LSE_ON);
129 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
130 RCC_RTCCLKCmd(ENABLE);
132 RTC_WriteProtectionCmd(DISABLE);
135 stm32_status = RTC_EnterInitMode();
136 }
while (stm32_status == ERROR);
138 stm32_status = RTC_SetTime(RTC_Format_BIN,theTime);
139 stm32_status = RTC_SetDate(RTC_Format_BIN,theDate);
142 RTC_WriteProtectionCmd(ENABLE);
143 PWR_BackupAccessCmd(DISABLE);
146 int settimeofday(
const struct timeval *tv,
const struct timezone *tz)
154 gmtime_r(&tv->tv_sec, &tm);
162 date.
RTC_WeekDay = (tm.tm_wday == 0) ? 7 : tm.tm_wday;
164 RTC_Set(&time, &date);
170 static __tzinfo_type tzinfo =
174 {
'M', 3, 5, 0, 7200, (time_t)0, -3600 },
175 {
'M', 10, 5, 0, 10800, (time_t)0, -7200 }
179 __tzinfo_type *__gettzinfo(
void)
This file contains all the functions prototypes for the RCC firmware library.
This file contains all the functions prototypes for the RTC firmware library.
This file contains all the functions prototypes for the PWR firmware library.
RTC Date structure definition.
RTC Time structure definition.