From b4bbe354fd956dc35a39ff2a9d53301b0f7d7ff4 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 11 Aug 2014 11:14:40 -0600 Subject: [PATCH] Fix inaccurate time conversion. Remove MSEC_PER_TICK and convert uint32_t to uin64_t. --- sched/sched/sched_timerexpiration.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/sched/sched/sched_timerexpiration.c b/sched/sched/sched_timerexpiration.c index 2bc737ecbe..0378775c16 100644 --- a/sched/sched/sched_timerexpiration.c +++ b/sched/sched/sched_timerexpiration.c @@ -290,8 +290,13 @@ static unsigned int sched_timer_process(unsigned int ticks, bool noswitches) static void sched_timer_start(unsigned int ticks) { - uint32_t msecs; - uint32_t secs; +#ifdef CONFIG_HAVE_LONG_LONG + uint64_t usecs; + uint64_t secs; +#else + uint64_t usecs; + uint64_t secs; +#endif uint32_t nsecs; int ret; @@ -308,11 +313,18 @@ static void sched_timer_start(unsigned int ticks) /* Convert ticks to a struct timespec that up_timer_start() can * understand. + * + * REVISIT: Calculations may not have acceptable ragne if uint64_t + * is not supported(?) */ - msecs = TICK2MSEC(ticks); - secs = msecs / MSEC_PER_SEC; - nsecs = (msecs - (secs * MSEC_PER_SEC)) * NSEC_PER_MSEC; +#ifdef CONFIG_HAVE_LONG_LONG + usecs = TICK2USEC((uint64_t)ticks); +#else + usecs = TICK2USEC((uint64_t)ticks); +#endif + secs = usecs / USEC_PER_SEC; + nsecs = (usecs - (secs * USEC_PER_SEC)) * NSEC_PER_MSEC; ts.tv_sec = (time_t)secs; ts.tv_nsec = (long)nsecs;