summaryrefslogtreecommitdiff
path: root/components/drivers/clock_time/arch/risc-v/cputimer.c
blob: 8941830f77042072795007224c26345b0a9a7564 (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
/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-07-10     xqyjlj       The first version.
 * 2025-04-20     GuEe-GUI     Port to common RISC-V timer.
 */

#include <drivers/clock_time.h>
#include <riscv_timer.h>

static volatile rt_uint64_t _init_cnt = 0;
static rt_bool_t _riscv_clock_registered = RT_FALSE;
static struct rt_clock_time_device _riscv_clock_dev;

static rt_uint64_t _riscv_clock_get_freq(struct rt_clock_time_device *dev)
{
    RT_UNUSED(dev);

    return riscv_timer_get_frequency();
}

static rt_uint64_t _riscv_clock_get_counter(struct rt_clock_time_device *dev)
{
    RT_UNUSED(dev);

    return riscv_timer_rdtime() - _init_cnt;
}

static rt_err_t _riscv_clock_set_timeout(struct rt_clock_time_device *dev, rt_uint64_t delta)
{
    RT_UNUSED(dev);
    RT_UNUSED(delta);

    return -RT_ENOSYS;
}

static const struct rt_clock_time_ops _riscv_clock_ops =
{
    _riscv_clock_get_freq,
    _riscv_clock_get_counter,
    _riscv_clock_set_timeout,
};

void rt_clock_time_source_init(void)
{
    _init_cnt = riscv_timer_rdtime();

    if (!_riscv_clock_registered)
    {
        rt_uint8_t caps = RT_CLOCK_TIME_CAP_SOURCE;

        _riscv_clock_dev.ops = &_riscv_clock_ops;
        _riscv_clock_dev.res_scale = RT_CLOCK_TIME_RESMUL;
        _riscv_clock_dev.caps = caps;
        rt_clock_time_device_register(&_riscv_clock_dev, "clock_time_rdtime", caps);
        rt_clock_time_set_default_source(&_riscv_clock_dev);
        _riscv_clock_registered = RT_TRUE;
    }
}