1/**
2 * \file
3 * \brief System time, constants and convertors
4 *
5 */
6
7/*
8 * Copyright (c) 2016, ETH Zurich.
9 * All rights reserved.
10 *
11 * This file is distributed under the terms in the attached LICENSE file.
12 * If you do not find this file, copies can be found by writing to:
13 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
14 */
15
16#include <systime.h>
17
18/// Number of system ticks per one millisecond
19systime_t systime_frequency = 1;
20
21
22/// Convert nanoseconds to system ticks
23systime_t ns_to_systime(uint64_t nanoseconds)
24{
25    uint64_t q, r;
26
27    q = nanoseconds / 1000000000;
28    r = nanoseconds % 1000000000;
29
30    // Adding half a tick to round properly
31    return q * systime_frequency + (r * systime_frequency + 500000000) / 1000000000;
32}
33
34/// Convert system ticks to nanoseconds
35uint64_t systime_to_ns(systime_t time)
36{
37    systime_t q, r;
38
39    q = time / systime_frequency;
40    r = time % systime_frequency;
41
42    // Adding half a nanosecond to round properly
43    return q * 1000000000 + (r * 1000000000 + systime_frequency / 2) / systime_frequency;
44}
45