From 8509c5f220c95a9a9fe22023220b4eff4316ab60 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Sun, 20 Sep 2015 12:53:33 +0200 Subject: [PATCH] Fix clock generation on Mac OS X --- src/platforms/posix/px4_layer/drv_hrt.c | 27 +++++++++++-------- .../posix/px4_layer/px4_posix_impl.cpp | 21 --------------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/platforms/posix/px4_layer/drv_hrt.c b/src/platforms/posix/px4_layer/drv_hrt.c index 74bfb9ee41..981857da45 100644 --- a/src/platforms/posix/px4_layer/drv_hrt.c +++ b/src/platforms/posix/px4_layer/drv_hrt.c @@ -37,6 +37,7 @@ * High-resolution timer with callouts and timekeeping. */ +#include #include #include #include @@ -85,14 +86,11 @@ static void hrt_unlock(void) #define MAC_NANO (+1.0E-9) #define MAC_GIGA UINT64_C(1000000000) #define CLOCK_MONOTONIC 1 -#define clockid_t int #define HRT_LOCK_NAME "/hrt_lock" static double px4_timebase = 0.0; -int clock_gettime(clockid_t clk_id, struct timespec *t); - -int clock_gettime(clockid_t clk_id, struct timespec *t) +int px4_clock_gettime(clockid_t clk_id, struct timespec *tp) { if (clk_id != CLOCK_MONOTONIC) { return 1; @@ -103,16 +101,23 @@ int clock_gettime(clockid_t clk_id, struct timespec *t) mach_timebase_info(&tb); px4_timebase = tb.numer; px4_timebase /= tb.denom; - px4_timestart = mach_absolute_time(); + // px4_timestart = mach_absolute_time(); } - memset(t, 0, sizeof(*t)); + memset(tp, 0, sizeof(*tp)); - double diff = (mach_absolute_time() - px4_timestart) * px4_timebase; - t->tv_sec = diff * MAC_NANO; - t->tv_nsec = diff - (t->tv_sec * MAC_GIGA); + double diff = mach_absolute_time() * px4_timebase; + tp->tv_sec = diff * MAC_NANO; + tp->tv_nsec = diff - (tp->tv_sec * MAC_GIGA); return 0; } + +int px4_clock_settime(clockid_t clk_id, struct timespec *tp) +{ + /* do nothing right now */ + return 0; +} + #endif /* @@ -123,11 +128,11 @@ hrt_abstime hrt_absolute_time(void) struct timespec ts; if (!px4_timestart) { - clock_gettime(CLOCK_MONOTONIC, &ts); + px4_clock_gettime(CLOCK_MONOTONIC, &ts); px4_timestart = ts_to_abstime(&ts); } - clock_gettime(CLOCK_MONOTONIC, &ts); + px4_clock_gettime(CLOCK_MONOTONIC, &ts); return ts_to_abstime(&ts) - px4_timestart; } diff --git a/src/platforms/posix/px4_layer/px4_posix_impl.cpp b/src/platforms/posix/px4_layer/px4_posix_impl.cpp index 5311ead749..19747556a9 100644 --- a/src/platforms/posix/px4_layer/px4_posix_impl.cpp +++ b/src/platforms/posix/px4_layer/px4_posix_impl.cpp @@ -56,27 +56,6 @@ __BEGIN_DECLS long PX4_TICKS_PER_SEC = sysconf(_SC_CLK_TCK); -#ifdef __PX4_DARWIN -extern void hrt_init(void); - -int px4_clock_gettime(clockid_t clk_id, struct timespec *tp) -{ - uint64_t currtime = hrt_absolute_time(); - - tp->tv_sec = currtime / (1000 * 1000); - tp->tv_nsec = (currtime - (currtime * 1000 * 1000)) * 1000; - - return 0; -} - -int px4_clock_settime(clockid_t clk_id, struct timespec *tp) -{ - /* do nothing right now */ - return 0; -} - -#endif - __END_DECLS namespace px4