forked from intel/zephyr.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzjs_linux_time.c
48 lines (36 loc) · 994 Bytes
/
zjs_linux_time.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (c) 2016-2017, Intel Corporation.
// C includes
#include <time.h>
// ZJS includes
#include "zjs_linux_port.h"
#define ZEPHYR_TICKS_PER_SEC
void zjs_port_timer_start(zjs_port_timer_t *timer, u32_t interval, u32_t repeat)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
timer->sec = now.tv_sec;
timer->milli = now.tv_nsec / 1000000;
timer->interval = interval;
}
void zjs_port_timer_stop(zjs_port_timer_t *timer)
{
timer->interval = 0;
}
u8_t zjs_port_timer_test(zjs_port_timer_t *timer)
{
u32_t elapsed;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
elapsed = (1000 * (now.tv_sec - timer->sec)) +
((now.tv_nsec / 1000000) - timer->milli);
if (elapsed >= timer->interval) {
return elapsed;
}
return 0;
}
u32_t zjs_port_timer_get_uptime(void)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (1000 * now.tv_sec) + (now.tv_nsec / 1000000);
}