summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nuttx/sched/clock/clock_gettime.c2
-rw-r--r--nuttx/sched/clock/clock_systimespec.c46
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