Skip to content

Commit 7db0837

Browse files
CopilotBernardXiong
andcommitted
Add unified clock_time subsystem core implementation
Co-authored-by: BernardXiong <[email protected]>
1 parent f868507 commit 7db0837

File tree

9 files changed

+1152
-0
lines changed

9 files changed

+1152
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
menuconfig RT_USING_CLOCK_TIME
2+
bool "Using unified clock_time subsystem"
3+
default n
4+
help
5+
Unified time subsystem that consolidates hwtimer, ktime, and cputime.
6+
Provides clock source, clock event, and high-resolution timer support.
7+
8+
if RT_USING_CLOCK_TIME
9+
10+
config RT_USING_CLOCK_HRTIMER
11+
bool "Enable high-resolution timer support"
12+
default y
13+
help
14+
Enable high-resolution software timers built on clock_time devices.
15+
16+
config RT_USING_CLOCK_CPUTIME
17+
bool "Enable CPU time APIs"
18+
default y
19+
help
20+
Enable CPU time measurement and delay APIs.
21+
22+
config RT_USING_CLOCK_BOOTTIME
23+
bool "Enable boottime APIs"
24+
default y
25+
help
26+
Enable system boottime (monotonic time since boot) APIs.
27+
28+
# Backward compatibility options
29+
config RT_USING_HWTIMER
30+
bool
31+
default y
32+
help
33+
Legacy option for backward compatibility with hwtimer.
34+
Automatically enabled when RT_USING_CLOCK_TIME is enabled.
35+
36+
config RT_USING_KTIME
37+
bool
38+
default y if RT_USING_CLOCK_HRTIMER
39+
help
40+
Legacy option for backward compatibility with ktime.
41+
Automatically enabled when RT_USING_CLOCK_HRTIMER is enabled.
42+
43+
config RT_USING_CPUTIME
44+
bool
45+
default y if RT_USING_CLOCK_CPUTIME
46+
help
47+
Legacy option for backward compatibility with cputime.
48+
Automatically enabled when RT_USING_CLOCK_CPUTIME is enabled.
49+
50+
endif
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from building import *
2+
3+
cwd = GetCurrentDir()
4+
src = []
5+
inc = [cwd + '/inc']
6+
7+
if GetDepend(['RT_USING_CLOCK_TIME']):
8+
# Core clock_time device implementation
9+
src += ['src/clock_time.c']
10+
11+
# High-resolution timer support
12+
if GetDepend(['RT_USING_CLOCK_HRTIMER']):
13+
src += ['src/hrtimer.c']
14+
15+
# CPU time APIs
16+
if GetDepend(['RT_USING_CLOCK_CPUTIME']):
17+
src += ['src/clock_time_cputime.c']
18+
19+
# Boottime APIs
20+
if GetDepend(['RT_USING_CLOCK_BOOTTIME']):
21+
src += ['src/clock_time_boottime.c']
22+
23+
# Tick-based fallback implementation
24+
src += ['src/clock_time_tick.c']
25+
26+
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = inc)
27+
28+
Return('group')
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) 2006-2025, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2025-01-01 RT-Thread Unified clock_time subsystem implementation
9+
*/
10+
11+
#include <rtthread.h>
12+
#include <rtdevice.h>
13+
#include <drivers/clock_time.h>
14+
15+
#define DBG_TAG "clock_time"
16+
#define DBG_LVL DBG_INFO
17+
#include <rtdbg.h>
18+
19+
/* Default system clock time device */
20+
static rt_clock_time_t _default_device = RT_NULL;
21+
22+
/**
23+
* @brief Register a clock time device
24+
*/
25+
rt_err_t rt_clock_time_device_register(struct rt_clock_time_device *dev,
26+
const char *name,
27+
rt_uint8_t caps)
28+
{
29+
rt_err_t result;
30+
31+
RT_ASSERT(dev != RT_NULL);
32+
RT_ASSERT(name != RT_NULL);
33+
RT_ASSERT(dev->ops != RT_NULL);
34+
35+
/* Initialize parent device structure */
36+
dev->parent.type = RT_Device_Class_Timer;
37+
dev->parent.rx_indicate = RT_NULL;
38+
dev->parent.tx_complete = RT_NULL;
39+
40+
dev->parent.init = RT_NULL;
41+
dev->parent.open = RT_NULL;
42+
dev->parent.close = RT_NULL;
43+
dev->parent.read = RT_NULL;
44+
dev->parent.write = RT_NULL;
45+
dev->parent.control = RT_NULL;
46+
47+
dev->caps = caps;
48+
49+
/* Calculate resolution scale factor */
50+
if (dev->ops->get_freq)
51+
{
52+
rt_uint64_t freq = dev->ops->get_freq();
53+
if (freq > 0)
54+
{
55+
/* res_scale = (1e9 * RT_CLOCK_TIME_RESMUL) / freq */
56+
dev->res_scale = ((1000000000ULL * RT_CLOCK_TIME_RESMUL) / freq);
57+
}
58+
else
59+
{
60+
dev->res_scale = RT_CLOCK_TIME_RESMUL;
61+
}
62+
}
63+
else
64+
{
65+
dev->res_scale = RT_CLOCK_TIME_RESMUL;
66+
}
67+
68+
/* Register device */
69+
result = rt_device_register(&dev->parent, name, RT_DEVICE_FLAG_RDWR);
70+
if (result != RT_EOK)
71+
{
72+
LOG_E("Failed to register clock_time device: %s", name);
73+
return result;
74+
}
75+
76+
/* Set as default if none exists */
77+
if (_default_device == RT_NULL)
78+
{
79+
_default_device = dev;
80+
LOG_D("Set %s as default clock_time device", name);
81+
}
82+
83+
LOG_I("Registered clock_time device: %s (caps: 0x%02x)", name, caps);
84+
85+
return RT_EOK;
86+
}
87+
88+
/**
89+
* @brief Get the default system clock time device
90+
*/
91+
rt_clock_time_t rt_clock_time_default(void)
92+
{
93+
return _default_device;
94+
}
95+
96+
/**
97+
* @brief Set the default system clock time device
98+
*/
99+
rt_err_t rt_clock_time_set_default(rt_clock_time_t dev)
100+
{
101+
RT_ASSERT(dev != RT_NULL);
102+
103+
_default_device = dev;
104+
LOG_D("Changed default clock_time device to: %s", dev->parent.parent.name);
105+
106+
return RT_EOK;
107+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2006-2025, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2025-01-01 RT-Thread Boottime APIs implementation
9+
*/
10+
11+
#include <rtthread.h>
12+
#include <rtdevice.h>
13+
#include <drivers/clock_time.h>
14+
#include <sys/time.h>
15+
16+
rt_weak rt_err_t rt_clock_boottime_get_us(struct timeval *tv)
17+
{
18+
RT_ASSERT(tv != RT_NULL);
19+
20+
rt_uint64_t ns = (rt_clock_cputimer_getcnt() * rt_clock_cputimer_getres()) / RT_CLOCK_TIME_RESMUL;
21+
22+
tv->tv_sec = ns / (1000ULL * 1000 * 1000);
23+
tv->tv_usec = (ns % (1000ULL * 1000 * 1000)) / 1000;
24+
25+
return RT_EOK;
26+
}
27+
28+
rt_weak rt_err_t rt_clock_boottime_get_s(time_t *t)
29+
{
30+
RT_ASSERT(t != RT_NULL);
31+
32+
rt_uint64_t ns = (rt_clock_cputimer_getcnt() * rt_clock_cputimer_getres()) / RT_CLOCK_TIME_RESMUL;
33+
34+
*t = ns / (1000ULL * 1000 * 1000);
35+
36+
return RT_EOK;
37+
}
38+
39+
rt_weak rt_err_t rt_clock_boottime_get_ns(struct timespec *ts)
40+
{
41+
RT_ASSERT(ts != RT_NULL);
42+
43+
rt_uint64_t ns = (rt_clock_cputimer_getcnt() * rt_clock_cputimer_getres()) / RT_CLOCK_TIME_RESMUL;
44+
45+
ts->tv_sec = ns / (1000ULL * 1000 * 1000);
46+
ts->tv_nsec = ns % (1000ULL * 1000 * 1000);
47+
48+
return RT_EOK;
49+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2006-2025, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2025-01-01 RT-Thread CPU time APIs (delegates to clock_time core)
9+
*/
10+
11+
#include <rtthread.h>
12+
#include <rtdevice.h>
13+
#include <drivers/clock_time.h>
14+
15+
/*
16+
* Additional CPU time utility functions
17+
* Core implementations are in clock_time_tick.c
18+
*/
19+
20+
/* These functions are implemented inline or use the core APIs */
21+
22+
/* Note: The main cputime APIs are now:
23+
* rt_clock_cputimer_getres()
24+
* rt_clock_cputimer_getfrq()
25+
* rt_clock_cputimer_getcnt()
26+
* rt_clock_cputimer_init()
27+
*
28+
* These are defined in clock_time_tick.c as weak functions
29+
* that can be overridden by BSP implementations.
30+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2006-2025, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2025-01-01 RT-Thread Tick-based fallback implementation for clock_time
9+
*/
10+
11+
#include <rtthread.h>
12+
#include <rtdevice.h>
13+
#include <drivers/clock_time.h>
14+
15+
/*
16+
* CPU Timer APIs - Default tick-based implementation
17+
* These are weak functions that can be overridden by BSP-specific implementations
18+
*/
19+
20+
rt_weak rt_uint64_t rt_clock_cputimer_getres(void)
21+
{
22+
/* Resolution in nanoseconds * RT_CLOCK_TIME_RESMUL */
23+
return ((1000ULL * 1000 * 1000) * RT_CLOCK_TIME_RESMUL) / RT_TICK_PER_SECOND;
24+
}
25+
26+
rt_weak unsigned long rt_clock_cputimer_getfrq(void)
27+
{
28+
return RT_TICK_PER_SECOND;
29+
}
30+
31+
rt_weak unsigned long rt_clock_cputimer_getcnt(void)
32+
{
33+
return rt_tick_get();
34+
}
35+
36+
rt_weak void rt_clock_cputimer_init(void)
37+
{
38+
/* Default: no initialization needed for tick-based implementation */
39+
return;
40+
}

0 commit comments

Comments
 (0)