summaryrefslogtreecommitdiff
path: root/components/drivers/clock_time/clock_boottime.c
blob: 3185a0496801e37eab0fcfb35a47c5684c89bdad (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-01-01     RT-Thread    Clock time boottime helpers
 */

#include <drivers/clock_time.h>

rt_err_t rt_clock_boottime_get_us(struct timeval *tv)
{
    rt_uint64_t cnt;
    rt_uint64_t res;
    rt_uint64_t ns;

    RT_ASSERT(tv != RT_NULL);

    cnt = rt_clock_time_get_counter();
    res = rt_clock_time_get_res_scaled();
    if (res == 0)
    {
        return -RT_ERROR;
    }

    ns = (cnt * res) / RT_CLOCK_TIME_RESMUL;

    tv->tv_sec  = ns / (1000ULL * 1000 * 1000);
    tv->tv_usec = (ns % (1000ULL * 1000 * 1000)) / 1000;

    return RT_EOK;
}

rt_err_t rt_clock_boottime_get_s(time_t *t)
{
    rt_uint64_t cnt;
    rt_uint64_t res;
    rt_uint64_t ns;

    RT_ASSERT(t != RT_NULL);

    cnt = rt_clock_time_get_counter();
    res = rt_clock_time_get_res_scaled();
    if (res == 0)
    {
        return -RT_ERROR;
    }

    ns = (cnt * res) / RT_CLOCK_TIME_RESMUL;
    *t = ns / (1000ULL * 1000 * 1000);

    return RT_EOK;
}

rt_err_t rt_clock_boottime_get_ns(struct timespec *ts)
{
    rt_uint64_t cnt;
    rt_uint64_t res;
    rt_uint64_t ns;

    RT_ASSERT(ts != RT_NULL);

    cnt = rt_clock_time_get_counter();
    res = rt_clock_time_get_res_scaled();
    if (res == 0)
    {
        return -RT_ERROR;
    }

    ns = (cnt * res) / RT_CLOCK_TIME_RESMUL;

    ts->tv_sec  = ns / (1000ULL * 1000 * 1000);
    ts->tv_nsec = ns % (1000ULL * 1000 * 1000);

    return RT_EOK;
}