diff options
Diffstat (limited to 'os/hal/src')
-rw-r--r-- | os/hal/src/rtc.c | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/os/hal/src/rtc.c b/os/hal/src/rtc.c index e67abb746..63a9a8b3e 100644 --- a/os/hal/src/rtc.c +++ b/os/hal/src/rtc.c @@ -185,6 +185,106 @@ void rtcSetCallback(RTCDriver *rtcp, rtccb_t callback) { }
#endif /* RTC_SUPPORTS_CALLBACKS */
+/**
+ * @brief Convert @p RTCDateTime to broken-down time structure.
+ *
+ * @param[in] timespec pointer to a @p RTCDateTime structure
+ * @param[out] timp pointer to a broken-down time structure
+ *
+ * @api
+ */
+void rtcConvertDateTimeToStructTm(const RTCDateTime *timespec,
+ struct tm *timp) {
+ uint32_t tmp;
+
+ timp->tm_year = timespec->year + (1980 - 1900);
+ timp->tm_mon = timespec->month - 1;
+ timp->tm_mday = timespec->day;
+ timp->tm_isdst = timespec->dstflag;
+
+ tmp = timespec->millisecond / 1000;
+ timp->tm_sec = tmp % 60;
+ tmp -= timp->tm_sec;
+ timp->tm_min = tmp % 3600;
+ tmp -= timp->tm_min * 60;
+ timp->tm_hour = tmp / 3600;
+}
+
+/**
+ * @brief Convert broken-down time structure to @p RTCDateTime.
+ *
+ * @param[in] timp pointer to a broken-down time structure
+ * @param[in] tv_msec milliseconds value
+ * @param[out] timespec pointer to a @p RTCDateTime structure
+ *
+ * @api
+ */
+void rtcConvertStructTmToDateTime(const struct tm *timp,
+ uint32_t tv_msec, RTCDateTime *timespec) {
+
+ timespec->year = timp->tm_year - (1980 - 1900);
+ timespec->month = timp->tm_mon + 1;
+ timespec->day = timp->tm_mday;
+ timespec->dayofweek = timp->tm_wday + 1;
+ if (-1 == timp->tm_isdst)
+ timespec->dstflag = 0; /* set zero if dst is unknown */
+ else
+ timespec->dstflag = timp->tm_isdst;
+ timespec->millisecond = tv_msec +
+ (timp->tm_hour * 3600 + timp->tm_min * 60 + timp->tm_sec) * 1000;
+}
+
+/*
+ * Lookup table with months' length
+ */
+static const uint8_t month_len[12] = {
+ 31, 30, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
+};
+
+/**
+ * @brief Get current time in format suitable for usage in FAT file system.
+ * @note The information about day of week and DST is lost in DOS
+ * format, the second field loses its least significant bit.
+ *
+ * @param[out] timespec pointer to a @p RTCDateTime structure
+ * @return FAT date/time value.
+ *
+ * @api
+ */
+uint32_t rtcConvertDateTimeToFAT(const RTCDateTime *timespec) {
+ uint32_t fattime;
+ uint32_t sec, min, hour, day, month, tmp;
+
+ tmp = timespec->millisecond / 1000;
+ sec = tmp % 60;
+ min = (tmp - sec) % 3600;
+ hour = (tmp - sec - min * 60) / 3600;
+ day = timespec->day;
+ month = timespec->month;
+
+ /* handle DST flag */
+ if (1 == timespec->dstflag) {
+ hour += 1;
+ if (hour == 24) {
+ hour = 0;
+ day += 1;
+ if (day > month_len[month - 1]) {
+ day = 1;
+ month += 1;
+ }
+ }
+ }
+
+ fattime = sec >> 1;
+ fattime |= min << 5;
+ fattime |= hour << 11;
+ fattime |= day << 16;
+ fattime |= month << 21;
+ fattime |= timespec->year << 25;
+
+ return fattime;
+}
+
#endif /* HAL_USE_RTC */
/** @} */
|