lib: posix: clock: Add read of CLOCK_REALTIME
Add a private variable `rt_clock_base` that can be used to determine a real-time clock by using the `k_uptime_get` clock. Once `clock_settime` is added, this can allow us to have a meaningful real time clock. Signed-off-by: David Brown <david.brown@linaro.org>
This commit is contained in:
parent
e0dda1b4b0
commit
9921eb329d
1 changed files with 28 additions and 1 deletions
|
@ -8,6 +8,15 @@
|
|||
#include <posix/time.h>
|
||||
#include <posix/sys/types.h>
|
||||
|
||||
/*
|
||||
* `k_uptime_get` returns a timestamp based on an always increasing
|
||||
* value from the system start. To support the `CLOCK_REALTIME`
|
||||
* clock, this `rt_clock_base` records the time that the system was
|
||||
* started. This can either be set via 'clock_settime', or could be
|
||||
* set from a real time clock, if such hardware is present.
|
||||
*/
|
||||
static struct timespec rt_clock_base;
|
||||
|
||||
/**
|
||||
* @brief Get clock time specified by clock_id.
|
||||
*
|
||||
|
@ -16,8 +25,19 @@
|
|||
int clock_gettime(clockid_t clock_id, struct timespec *ts)
|
||||
{
|
||||
u64_t elapsed_msecs;
|
||||
struct timespec base;
|
||||
|
||||
if (clock_id != CLOCK_MONOTONIC) {
|
||||
switch (clock_id) {
|
||||
case CLOCK_MONOTONIC:
|
||||
base.tv_sec = 0;
|
||||
base.tv_nsec = 0;
|
||||
break;
|
||||
|
||||
case CLOCK_REALTIME:
|
||||
base = rt_clock_base;
|
||||
break;
|
||||
|
||||
default:
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
@ -27,6 +47,13 @@ int clock_gettime(clockid_t clock_id, struct timespec *ts)
|
|||
ts->tv_nsec = (s32_t) ((elapsed_msecs % MSEC_PER_SEC) *
|
||||
USEC_PER_MSEC * NSEC_PER_USEC);
|
||||
|
||||
ts->tv_sec += base.tv_sec;
|
||||
ts->tv_nsec += base.tv_nsec;
|
||||
if (ts->tv_nsec > NSEC_PER_SEC) {
|
||||
ts->tv_sec++;
|
||||
ts->tv_nsec -= NSEC_PER_SEC;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue