From d37f6cb1f3cac35b1f7f7373062fddb638707570 Mon Sep 17 00:00:00 2001 From: barthess Date: Sat, 1 Nov 2014 15:44:30 +0000 Subject: RTCv1. Time converstion functions moved to rtc.c(h). Chrtclib deleted. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@7443 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/include/rtc.h | 19 ++++++- os/hal/ports/STM32/LLD/RTCv1/rtc_lld.c | 2 - os/hal/ports/STM32/LLD/RTCv1/rtc_lld.h | 2 - os/hal/src/rtc.c | 100 +++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 5 deletions(-) (limited to 'os/hal') diff --git a/os/hal/include/rtc.h b/os/hal/include/rtc.h index a775ec570..a0f02b502 100644 --- a/os/hal/include/rtc.h +++ b/os/hal/include/rtc.h @@ -87,8 +87,20 @@ */ typedef struct RTCDriver RTCDriver; -#include "chrtclib.h" +/** + * @brief Type of a structure representing an RTC date/time stamp. + */ +typedef struct { + uint32_t year: 8; /**< @brief Years since 1980. */ + uint32_t month: 4; /**< @brief Months 1..12. */ + uint32_t dstflag: 1; /**< @brief DST correction flag. */ + uint32_t dayofweek: 3; /**< @brief Day of week 1..7. */ + uint32_t day: 5; /**< @brief Day of the month 1..31. */ + uint32_t millisecond: 27; /**< @brief Milliseconds since midnight.*/ +} RTCDateTime; + #include "rtc_lld.h" +#include /*===========================================================================*/ /* Driver macros. */ @@ -113,6 +125,11 @@ extern "C" { #if RTC_SUPPORTS_CALLBACKS void rtcSetCallback(RTCDriver *rtcp, rtccb_t callback); #endif + void rtcConvertDateTimeToStructTm(const RTCDateTime *timespec, + struct tm *timp); + void rtcConvertStructTmToDateTime(const struct tm *timp, + uint32_t tv_msec, RTCDateTime *timespec); + uint32_t rtcConvertDateTimeToFAT(const RTCDateTime *timespec); #ifdef __cplusplus } #endif diff --git a/os/hal/ports/STM32/LLD/RTCv1/rtc_lld.c b/os/hal/ports/STM32/LLD/RTCv1/rtc_lld.c index f17a553b4..c8ccd41bd 100644 --- a/os/hal/ports/STM32/LLD/RTCv1/rtc_lld.c +++ b/os/hal/ports/STM32/LLD/RTCv1/rtc_lld.c @@ -30,8 +30,6 @@ #if HAL_USE_RTC || defined(__DOXYGEN__) -#include "chrtclib.h" - /*===========================================================================*/ /* Driver local definitions. */ /*===========================================================================*/ diff --git a/os/hal/ports/STM32/LLD/RTCv1/rtc_lld.h b/os/hal/ports/STM32/LLD/RTCv1/rtc_lld.h index 31d2b576b..0685ef6bc 100644 --- a/os/hal/ports/STM32/LLD/RTCv1/rtc_lld.h +++ b/os/hal/ports/STM32/LLD/RTCv1/rtc_lld.h @@ -31,8 +31,6 @@ #if HAL_USE_RTC || defined(__DOXYGEN__) -#include "chrtclib.h" - /*===========================================================================*/ /* Driver constants. */ /*===========================================================================*/ 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 */ /** @} */ -- cgit v1.2.3