From 5e40781027ed29e253467ff3d33b870bd08d05fa Mon Sep 17 00:00:00 2001 From: brabo Date: Mon, 13 Mar 2017 23:04:44 +0100 Subject: [PATCH] (get/set)timeofday now using sys_clock_(get/set)time, insane-adding-machines/frosted#120 --- newlib/libc/sys/frosted/gettimeofday.c | 24 ++++++++++++++++++++++-- newlib/libc/sys/frosted/include/time.h | 3 ++- newlib/libc/sys/frosted/sleep.c | 19 ++++++------------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/newlib/libc/sys/frosted/gettimeofday.c b/newlib/libc/sys/frosted/gettimeofday.c index 1cddd8b35..b3bb26a1a 100644 --- a/newlib/libc/sys/frosted/gettimeofday.c +++ b/newlib/libc/sys/frosted/gettimeofday.c @@ -3,15 +3,35 @@ */ #include "sys/frosted.h" +#include "time.h" +#include #include struct timeval; struct timezone; -extern int sys_gettimeofday(struct timeval *tv); +extern int sys_clock_gettime(clockid_t clock_id, struct timespec *tp); +extern int sys_clock_settime(clockid_t clock_id, struct timespec *tp); int gettimeofday(struct timeval *tv, struct timezone *tz) { (void)tz; /* Timezone not implemented in frosted. */ - int ret = sys_gettimeofday(tv); + struct timespec tp; + int ret = sys_clock_gettime(CLOCK_REALTIME, &tp); + if (ret < 0) { + errno = 0 - ret; + ret = -1; + } + tv->tv_sec = tp.tv_sec; + tv->tv_usec = (tp.tv_nsec / 1000); + return ret; +} + +int settimeofday(struct timeval *tv, struct timezone *tz) +{ + (void)tz; /* Timezone not implemented in frosted. */ + struct timespec tp; + tp.tv_sec = tv->tv_sec; + tp.tv_nsec = (tv->tv_usec * 1000); + int ret = sys_clock_settime(CLOCK_REALTIME, &tp); if (ret < 0) { errno = 0 - ret; ret = -1; diff --git a/newlib/libc/sys/frosted/include/time.h b/newlib/libc/sys/frosted/include/time.h index c5d1af183..081743da2 100644 --- a/newlib/libc/sys/frosted/include/time.h +++ b/newlib/libc/sys/frosted/include/time.h @@ -196,7 +196,8 @@ int _EXFUN(nanosleep, (const struct timespec *rqtp, struct timespec *rmtp)); /* Manifest Constants, P1003.1b-1993, p. 262 */ -#define CLOCK_REALTIME (clockid_t)1 +#define CLOCK_REALTIME (clockid_t)0 +#define CLOCK_MONOTONIC (clockid_t)1 /* Flag indicating time is "absolute" with respect to the clock associated with a time. */ diff --git a/newlib/libc/sys/frosted/sleep.c b/newlib/libc/sys/frosted/sleep.c index bf67e9754..17ca917c8 100644 --- a/newlib/libc/sys/frosted/sleep.c +++ b/newlib/libc/sys/frosted/sleep.c @@ -3,7 +3,7 @@ */ #include "sys/frosted.h" -#include "sys/time.h" +#include "time.h" #include extern int sys_sleep(int ms, uint32_t *rem); @@ -57,7 +57,8 @@ int nanosleep(const struct timespec *req, struct timespec *rem) return ret; } -extern int sys_gettimeofday(struct timeval *tv); +extern int sys_clock_gettime(clockid_t clock_id, struct timespec *tp); +extern int sys_clock_settime(clockid_t clock_id, struct timespec *tp); int clock_getres(clockid_t clock_id, struct timespec *res) { @@ -72,26 +73,17 @@ int clock_getres(clockid_t clock_id, struct timespec *res) int clock_gettime(clockid_t clock_id, struct timespec *tp) { - struct timeval tv; - int ret = sys_gettimeofday(&tv); + int ret = sys_clock_gettime(clock_id, tp); if (ret < 0) { errno = 0 - ret; ret = -1; } - tp->tv_sec = tv.tv_sec; - tp->tv_nsec = tv.tv_usec * 1000; return ret; } -extern int sys_clock_settime(struct timeval *tv); - int clock_settime(clockid_t clock_id, const struct timespec *tp) { - struct timeval tv; - tv.tv_sec = tp->tv_sec; - tv.tv_usec = tp->tv_nsec * 1000; - - int ret = sys_clock_settime(&tv); + int ret = sys_clock_settime(clock_id, tp); if (ret < 0) { errno = 0 - ret; ret = -1; @@ -99,3 +91,4 @@ int clock_settime(clockid_t clock_id, const struct timespec *tp) return ret; } +