Mac OS: Fix clock

This commit is contained in:
Lorenz Meier 2015-11-07 09:30:13 +01:00
parent 4f795309fd
commit f170272669
1 changed files with 11 additions and 23 deletions

View File

@ -81,35 +81,23 @@ static void hrt_unlock(void)
px4_sem_post(&_hrt_lock);
}
#ifdef __PX4_DARWIN
#include <mach/mach_time.h>
#define MAC_NANO (+1.0E-9)
#define MAC_GIGA UINT64_C(1000000000)
#define CLOCK_MONOTONIC 1
#define HRT_LOCK_NAME "/hrt_lock"
static double px4_timebase = 0.0;
#if defined(__APPLE__) && defined(__MACH__)
#include <time.h>
#include <sys/time.h>
#define CLOCK_REALTIME 0
int px4_clock_gettime(clockid_t clk_id, struct timespec *tp)
{
if (clk_id != CLOCK_MONOTONIC) {
return 1;
struct timeval now;
int rv = gettimeofday(&now, NULL);
if(rv) {
return rv;
}
if (!px4_timestart) {
mach_timebase_info_data_t tb = {};
mach_timebase_info(&tb);
px4_timebase = tb.numer;
px4_timebase /= tb.denom;
// px4_timestart = mach_absolute_time();
}
tp->tv_sec = now.tv_sec;
tp->tv_nsec = now.tv_usec * 1000;
memset(tp, 0, sizeof(*tp));
double diff = mach_absolute_time() * px4_timebase;
tp->tv_sec = diff * MAC_NANO;
tp->tv_nsec = diff - (tp->tv_sec * MAC_GIGA);
return 0;
}