diff options
author | Gregory Nutt <gnutt@nuttx.org> | 2015-02-25 07:24:03 -0600 |
---|---|---|
committer | Gregory Nutt <gnutt@nuttx.org> | 2015-02-25 07:24:03 -0600 |
commit | 319b419c234f9974cc374a7b9b6cc0d1ec11f33a (patch) | |
tree | eb8499c04182b97b89c91de9faf4f8adec082958 | |
parent | 83bb4ede6652944d738d9fa865b68c2fc0acc470 (diff) | |
download | nuttx-319b419c234f9974cc374a7b9b6cc0d1ec11f33a.tar.gz nuttx-319b419c234f9974cc374a7b9b6cc0d1ec11f33a.tar.bz2 nuttx-319b419c234f9974cc374a7b9b6cc0d1ec11f33a.zip |
When an RTC is used, clock_systimespec() must subtract the basetime from the RTC time
-rw-r--r-- | nuttx/sched/clock/clock_gettime.c | 2 | ||||
-rw-r--r-- | nuttx/sched/clock/clock_systimespec.c | 46 |
2 files changed, 44 insertions, 4 deletions
diff --git a/nuttx/sched/clock/clock_gettime.c b/nuttx/sched/clock/clock_gettime.c index 0c8c9be80..429f28315 100644 --- a/nuttx/sched/clock/clock_gettime.c +++ b/nuttx/sched/clock/clock_gettime.c @@ -130,7 +130,7 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp) if (clock_id == CLOCK_REALTIME) { - /* Get the elapsedcd s time since the time-of-day was last set. + /* Get the elapsed time since the time-of-day was last set. * clock_systimespec() provides the time since power was applied; * the bias value corresponds to the time when the time-of-day was * last set. diff --git a/nuttx/sched/clock/clock_systimespec.c b/nuttx/sched/clock/clock_systimespec.c index 30a5ccbc1..37511f1d0 100644 --- a/nuttx/sched/clock/clock_systimespec.c +++ b/nuttx/sched/clock/clock_systimespec.c @@ -41,6 +41,7 @@ #include <stdint.h> #include <time.h> +#include <errno.h> #include <nuttx/arch.h> #include <nuttx/clock.h> @@ -64,7 +65,7 @@ * * Description: * Return the current value of the system timer counter as a struct - * timespec. + * timespec. The returned time is the elapsed time since power up. * * Parameters: * ts - Location to return the time @@ -83,9 +84,48 @@ int clock_systimespec(FAR struct timespec *ts) if (g_rtc_enabled) { - /* Get the hi-resolution time from the RTC */ + int ret; - return up_rtc_gettime(ts); + /* Get the hi-resolution time from the RTC. This will return the + * current time, not the time since power up. + */ + + ret = up_rtc_gettime(ts); + if (ret < 0) + { + return ret; + } + + /* Subtract the base time to this in order to convert this to the + * time since power up. + */ + + DEBUGASSERT(ts->tv_sec >= g_basetime.tv_sec); + if (ts->tv_sec < g_basetime.tv_sec) + { + /* Negative times are not supported */ + + return -ENOSYS; + } + + ts->tv_sec -= g_basetime.tv_sec; + if (ts->tv_nsec < g_basetime.tv_nsec) + { + /* Borrow */ + + if (ts->tv_sec < 1) + { + /* Negative times are not supported */ + + return -ENOSYS; + } + + ts->tv_sec--; + ts->tv_nsec += NSEC_PER_SEC; + } + + ts->tv_nsec -= g_basetime.tv_nsec; + return OK; } else #endif |