1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author            Notes
8  * 2017-12-23     Bernard           first version
9  */
10 
11 #include <rtdevice.h>
12 #include <rtthread.h>
13 #include <sys/errno.h>
14 
15 static const struct rt_clock_cputime_ops *_cputime_ops  = RT_NULL;
16 
17 /**
18  * The clock_cpu_getres() function shall return the resolution of CPU time, the
19  * number of nanosecond per tick.
20  *
21  * @return the number of nanosecond per tick(x (1000UL * 1000))
22  */
clock_cpu_getres(void)23 uint64_t clock_cpu_getres(void)
24 {
25     if (_cputime_ops)
26         return _cputime_ops->cputime_getres();
27 
28     rt_set_errno(ENOSYS);
29     return 0;
30 }
31 
32 /**
33  * The clock_cpu_gettime() function shall return the current value of cpu time tick.
34  *
35  * @return the cpu tick
36  */
clock_cpu_gettime(void)37 uint64_t clock_cpu_gettime(void)
38 {
39     if (_cputime_ops)
40         return _cputime_ops->cputime_gettime();
41 
42     rt_set_errno(ENOSYS);
43     return 0;
44 }
45 
46 /**
47  * The clock_cpu_settimeout() fucntion set timeout time and timeout callback function
48  * The timeout callback function will be called when the timeout time is reached
49  *
50  * @param tick the Timeout tick
51  * @param timeout the Timeout function
52  * @param parameter the Parameters of timeout function
53  *
54  */
clock_cpu_settimeout(uint64_t tick,void (* timeout)(void * param),void * param)55 int clock_cpu_settimeout(uint64_t tick, void (*timeout)(void *param), void *param)
56 {
57     if (_cputime_ops)
58         return _cputime_ops->cputime_settimeout(tick, timeout, param);
59 
60     rt_set_errno(ENOSYS);
61     return 0;
62 }
63 
clock_cpu_issettimeout(void)64 int clock_cpu_issettimeout(void)
65 {
66     if (_cputime_ops)
67         return _cputime_ops->cputime_settimeout != RT_NULL;
68     return RT_FALSE;
69 }
70 
71 /**
72  * The clock_cpu_microsecond() fucntion shall return the microsecond according to
73  * cpu_tick parameter.
74  *
75  * @param cpu_tick the cpu tick
76  *
77  * @return the microsecond
78  */
clock_cpu_microsecond(uint64_t cpu_tick)79 uint64_t clock_cpu_microsecond(uint64_t cpu_tick)
80 {
81     uint64_t unit = clock_cpu_getres();
82 
83     return (uint64_t)(((cpu_tick * unit) / (1000UL * 1000)) / 1000);
84 }
85 
86 /**
87  * The clock_cpu_microsecond() fucntion shall return the millisecond according to
88  * cpu_tick parameter.
89  *
90  * @param cpu_tick the cpu tick
91  *
92  * @return the millisecond
93  */
clock_cpu_millisecond(uint64_t cpu_tick)94 uint64_t clock_cpu_millisecond(uint64_t cpu_tick)
95 {
96     uint64_t unit = clock_cpu_getres();
97 
98     return (uint64_t)(((cpu_tick * unit) / (1000UL * 1000)) / (1000UL * 1000));
99 }
100 
101 /**
102  * The clock_cpu_seops() function shall set the ops of cpu time.
103  *
104  * @return always return 0.
105  */
clock_cpu_setops(const struct rt_clock_cputime_ops * ops)106 int clock_cpu_setops(const struct rt_clock_cputime_ops *ops)
107 {
108     _cputime_ops = ops;
109     if (ops)
110     {
111         RT_ASSERT(ops->cputime_getres  != RT_NULL);
112         RT_ASSERT(ops->cputime_gettime != RT_NULL);
113     }
114 
115     return 0;
116 }
117