From ec65999d9b8536d3da954dce325d9b217cd667df Mon Sep 17 00:00:00 2001 From: barthess Date: Sat, 19 May 2012 15:39:24 +0000 Subject: [PATCH] chrtclib. Fixed overflows in mcrosecond functions. Fixed initializations of RTCTime structs in functions. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4217 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/various/chrtclib.c | 45 +++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/os/various/chrtclib.c b/os/various/chrtclib.c index f9e770ad1..f76f91d65 100644 --- a/os/various/chrtclib.c +++ b/os/various/chrtclib.c @@ -139,7 +139,11 @@ static void stm32_rtc_tm2bcd(struct tm *timp, RTCTime *timespec) { * @api */ void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp) { +#if STM32_RTC_HAS_SUBSECONDS RTCTime timespec = {0,0,FALSE,0}; +#else + RTCTime timespec = {0,0,FALSE}; +#endif rtcGetTime(rtcp, ×pec); stm32_rtc_bcd2tm(timp, ×pec); @@ -154,7 +158,11 @@ void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp) { * @api */ void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp) { +#if STM32_RTC_HAS_SUBSECONDS RTCTime timespec = {0,0,FALSE,0}; +#else + RTCTime timespec = {0,0,FALSE}; +#endif stm32_rtc_tm2bcd(timp, ×pec); rtcSetTime(rtcp, ×pec); @@ -169,7 +177,11 @@ void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp) { * @api */ time_t rtcGetTimeUnixSec(RTCDriver *rtcp) { +#if STM32_RTC_HAS_SUBSECONDS RTCTime timespec = {0,0,FALSE,0}; +#else + RTCTime timespec = {0,0,FALSE}; +#endif struct tm timp; rtcGetTime(rtcp, ×pec); @@ -187,11 +199,13 @@ time_t rtcGetTimeUnixSec(RTCDriver *rtcp) { * @api */ void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec) { +#if STM32_RTC_HAS_SUBSECONDS RTCTime timespec = {0,0,FALSE,0}; - struct tm *timp; +#else + RTCTime timespec = {0,0,FALSE}; +#endif - timp = localtime(&tv_sec); - stm32_rtc_tm2bcd(timp, ×pec); + stm32_rtc_tm2bcd(localtime(&tv_sec), ×pec); rtcSetTime(rtcp, ×pec); } @@ -204,20 +218,18 @@ void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec) { * @api */ uint64_t rtcGetTimeUnixUsec(RTCDriver *rtcp) { +#if STM32_RTC_HAS_SUBSECONDS uint64_t result = 0; - RTCTime timespec = {0,0,FALSE,0}; struct tm timp; rtcGetTime(rtcp, ×pec); stm32_rtc_bcd2tm(&timp, ×pec); - result = mktime(&timp) * 1000000; - -#if STM32_RTC_HAS_SUBSECONDS + result = (uint64_t)mktime(&timp) * 1000000; return result + timespec.tv_msec * 1000; #else - return result; + return (uint64_t)rtcGetTimeUnixSec(rtcp) * 1000000; #endif } @@ -231,7 +243,7 @@ uint64_t rtcGetTimeUnixUsec(RTCDriver *rtcp) { * @api */ void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp) { - RTCTime timespec = {0,0,FALSE,0}; + RTCTime timespec = {0,0}; rtcGetTime(rtcp, ×pec); if (timp != NULL) /* this comparison needed to avoid compiler warning */ @@ -247,7 +259,7 @@ void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp) { * @api */ void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp) { - RTCTime timespec = {0,0,FALSE,0}; + RTCTime timespec = {0,0}; timespec.tv_sec = mktime(timp); timespec.tv_msec = 0; @@ -263,7 +275,7 @@ void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp) { * @api */ time_t rtcGetTimeUnixSec(RTCDriver *rtcp) { - RTCTime timespec = {0,0,FALSE,0}; + RTCTime timespec = {0,0}; rtcGetTime(rtcp, ×pec); return timespec.tv_sec; @@ -278,7 +290,7 @@ time_t rtcGetTimeUnixSec(RTCDriver *rtcp) { * @api */ void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec) { - RTCTime timespec = {0,0,FALSE,0}; + RTCTime timespec = {0,0}; timespec.tv_sec = tv_sec; timespec.tv_msec = 0; @@ -294,16 +306,15 @@ void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec) { * @api */ uint64_t rtcGetTimeUnixUsec(RTCDriver *rtcp) { +#if STM32_RTC_HAS_SUBSECONDS uint64_t result = 0; - RTCTime timespec = {0,0,FALSE,0}; + RTCTime timespec = {0,0}; rtcGetTime(rtcp, ×pec); - result = timespec.tv_sec * 1000000; - -#if STM32_RTC_HAS_SUBSECONDS + result = (uint64_t)timespec.tv_sec * 1000000; return result + timespec.tv_msec * 1000; #else - return result; + return (uint64_t)rtcGetTimeUnixSec(rtcp) * 1000000; #endif } #endif /* STM32_RTC_IS_CALENDAR */