From 27c48a383e4bd43aeac5fb260026e756aa459725 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 15 Aug 2014 17:48:07 -0600 Subject: [PATCH] Don't do 64-bit calculations if accuracy not achievable; Fix compile error in high res RTC mode --- sched/clock/clock_systimespec.c | 69 +++++++++++++++++---------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/sched/clock/clock_systimespec.c b/sched/clock/clock_systimespec.c index cacdf0ae9df..c24daa70c22 100644 --- a/sched/clock/clock_systimespec.c +++ b/sched/clock/clock_systimespec.c @@ -78,63 +78,64 @@ int clock_systimespec(FAR struct timespec *ts) { - #ifdef CONFIG_RTC_HIRES +#ifdef CONFIG_RTC_HIRES /* Do we have a high-resolution RTC that can provide us with the time? */ if (g_rtc_enabled) { /* Get the hi-resolution time from the RTC */ - ret = up_rtc_gettime(tp); + return up_rtc_gettime(tp); } else #endif - + { #if defined(CONFIG_SCHED_TICKLESS) - { - /* Let the platform time do the work */ + /* In tickless mode, all timing is controlled by platform-specific + * code. Let the platform timer do the work. + */ - return up_timer_gettime(ts); - } + return up_timer_gettime(ts); -#elif defined(CONFIG_HAVE_LONG_LONG) - { - uint64_t usecs; - uint64_t secs; - uint64_t nsecs; +#elif defined(CONFIG_HAVE_LONG_LONG) && (CONFIG_USEC_PER_TICK % 1000) != 0 + /* 64-bit microsecond calculations should improve our accuracy. */ - /* Get the time since power-on in seconds and milliseconds */ + uint64_t usecs; + uint64_t secs; + uint64_t nsecs; - usecs = TICK2MSEC(clock_systimer()); - secs = usecs / USEC_PER_SEC; + /* Get the time since power-on in seconds and milliseconds */ - /* Return the elapsed time in seconds and nanoseconds */ + usecs = TICK2MSEC(clock_systimer()); + secs = usecs / USEC_PER_SEC; - nsecs = (usecs - (secs * USEC_PER_SEC)) * NSEC_PER_USEC; + /* Return the elapsed time in seconds and nanoseconds */ - ts->tv_sec = (time_t)secs; - ts->tv_nsec = (long)nsecs; - return OK; - } + nsecs = (usecs - (secs * USEC_PER_SEC)) * NSEC_PER_USEC; + + ts->tv_sec = (time_t)secs; + ts->tv_nsec = (long)nsecs; + return OK; #else - { - uint32_t msecs; - uint32_t secs; - uint32_t nsecs; + /* 32-bit millisecond calculations should be just fine. */ - /* Get the time since power-on in seconds and milliseconds */ + uint32_t msecs; + uint32_t secs; + uint32_t nsecs; - msecs = TICK2MSEC(clock_systimer()); - secs = msecs / MSEC_PER_SEC; + /* Get the time since power-on in seconds and milliseconds */ - /* Return the elapsed time in seconds and nanoseconds */ + msecs = TICK2MSEC(clock_systimer()); + secs = msecs / MSEC_PER_SEC; - nsecs = (msecs - (secs * MSEC_PER_SEC)) * NSEC_PER_MSEC; + /* Return the elapsed time in seconds and nanoseconds */ - ts->tv_sec = (time_t)secs; - ts->tv_nsec = (long)nsecs; - return OK; - } + nsecs = (msecs - (secs * MSEC_PER_SEC)) * NSEC_PER_MSEC; + + ts->tv_sec = (time_t)secs; + ts->tv_nsec = (long)nsecs; + return OK; #endif + } }