From e001f37593ed0201b5ebe1e542aaed7f3d5162f8 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 21 Jan 2016 13:31:00 -0600 Subject: [PATCH] sched/clock/clock_timespec.c: Fix an error in the time conversion --- ChangeLog | 6 +++++ sched/clock/clock_abstime2ticks.c | 18 +------------ sched/clock/clock_gettime.c | 30 +-------------------- sched/clock/clock_systimespec.c | 43 +++++++++++++++++++------------ 4 files changed, 35 insertions(+), 62 deletions(-) diff --git a/ChangeLog b/ChangeLog index d9764a3b2e..3bbd4e2c0d 100755 --- a/ChangeLog +++ b/ChangeLog @@ -11332,4 +11332,10 @@ read-only passwd file (2016-01-20). * sim/nsh: Uses the custom start up script, includes hoks for an MOTD message. (2016-01-20). + * include/clock.h and lots of other files: If the 64-bit timer is + selected, then use it whenever clock_systimer() is called rather + then chopping the 64-bit timer down to 32-bits. Add a new type, + systime_t to represent the 32- or 64-bit system timer (2016-01-21). + * sched/clock/clock_systimespec.c: Fix an error in a time + conversion (2016-01-21). diff --git a/sched/clock/clock_abstime2ticks.c b/sched/clock/clock_abstime2ticks.c index 30027d7617..24213dd07b 100644 --- a/sched/clock/clock_abstime2ticks.c +++ b/sched/clock/clock_abstime2ticks.c @@ -1,7 +1,7 @@ /******************************************************************************** * sched/clock/clock_abstime2ticks.c * - * Copyright (C) 2007, 2008, 2013-2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2008, 2013-2014, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -44,22 +44,6 @@ #include #include "clock/clock.h" -/******************************************************************************** - * Pre-processor Definitions - ********************************************************************************/ - -/******************************************************************************** - * Private Type Declarations - ********************************************************************************/ - -/******************************************************************************** - * Public Data - ********************************************************************************/ - -/******************************************************************************** - * Private Variables - ********************************************************************************/ - /******************************************************************************** * Private Functions ********************************************************************************/ diff --git a/sched/clock/clock_gettime.c b/sched/clock/clock_gettime.c index 519785d9ea..50e3c6e7c1 100644 --- a/sched/clock/clock_gettime.c +++ b/sched/clock/clock_gettime.c @@ -1,7 +1,7 @@ /**************************************************************************** * sched/clock/clock_gettime.c * - * Copyright (C) 2007, 2009, 2011, 2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009, 2011, 2014, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -50,34 +50,6 @@ #include "clock/clock.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Type Declarations - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Public Constant Data - ****************************************************************************/ - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -/**************************************************************************** - * Private Variables - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - /**************************************************************************** * Public Functions ****************************************************************************/ diff --git a/sched/clock/clock_systimespec.c b/sched/clock/clock_systimespec.c index 37511f1d0a..030f3f7e0c 100644 --- a/sched/clock/clock_systimespec.c +++ b/sched/clock/clock_systimespec.c @@ -1,7 +1,7 @@ /**************************************************************************** * sched/clock/clock_systimespec.c * - * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2014, 2016 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -48,14 +48,6 @@ #include "clock/clock.h" -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -138,15 +130,17 @@ int clock_systimespec(FAR struct timespec *ts) return up_timer_gettime(ts); #elif defined(CONFIG_HAVE_LONG_LONG) && (CONFIG_USEC_PER_TICK % 1000) != 0 - /* 64-bit microsecond calculations should improve our accuracy. */ + /* 64-bit microsecond calculations should improve our accuracy + * when the clock period is in units of microseconds. + */ uint64_t usecs; uint64_t secs; uint64_t nsecs; - /* Get the time since power-on in seconds and milliseconds */ + /* Get the time since power-on in seconds and microsecon */ - usecs = TICK2MSEC(clock_systimer()); + usecs = (uint64_t)TICK2USEC(clock_systimer()); secs = usecs / USEC_PER_SEC; /* Return the elapsed time in seconds and nanoseconds */ @@ -158,11 +152,28 @@ int clock_systimespec(FAR struct timespec *ts) return OK; #else - /* 32-bit millisecond calculations should be just fine. */ + /* We know that the clock rate is in units of milliseconds + * show we should be able to do the calculations with less + * chance of overflow. + * + * 32-bit millisecond calculations should be just fine in + * most cases. For a 32-bit system timer and a clock period + * of 10 milliseconds, the msecs value will overflow at about + * 49.7 days. + * + * So, we will still use 64-bit calculations if we have them + * in order to avoid that limitation. + */ - uint32_t msecs; - uint32_t secs; - uint32_t nsecs; +#ifdef CONFIG_HAVE_LONG_LONG + uint64_t msecs; + uint64_t secs; + uint64_t nsecs; +#else + systime_t msecs; + systime_t secs; + systime_t nsecs; +#endif /* Get the time since power-on in seconds and milliseconds */