Skip to content

Commit 55a75c2

Browse files
committed
[docs][clock_time] add clock time documentation
1 parent 852f74e commit 55a75c2

11 files changed

Lines changed: 2252 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
@page page_device_clock_time Clock Time Subsystem
2+
3+
# Overview
4+
5+
The clock_time subsystem provides a unified, high-resolution time base and
6+
programmable event scheduling for RT-Thread. It decouples a monotonic counter
7+
(clock source) from deadline delivery (clock event), so platforms can mix
8+
hardware counters and timers while the kernel and libc see consistent behavior.
9+
10+
# Architecture
11+
12+
![Clock time architecture](figures/clock_time_arch.svg)
13+
14+
## Layering and Responsibilities
15+
16+
- Upper layers: kernel services (timeouts, delays), POSIX/libc time APIs
17+
(clock_gettime, nanosleep), and RTC/soft-RTC consume the monotonic time base
18+
and timer events exposed by clock_time.
19+
- clock_time subsystem: core APIs, clock source/event devices, the hrtimer
20+
scheduler, boottime helpers, and the clock_timer adapter.
21+
- Lower layers: BSP drivers provide hardware counters and timers, which are
22+
wrapped as clock_time devices or clock_timer devices.
23+
24+
## Internal Components
25+
26+
- Core API (clock_time_core.c)
27+
- Registers clock_time devices, manages default source/event selection, and
28+
provides counter <-> nanosecond conversion with fixed-point scaling.
29+
- Clock source device (rt_clock_time_device + RT_CLOCK_TIME_CAP_SOURCE)
30+
- Supplies a free-running counter and frequency for monotonic time reads.
31+
- Clock event device (rt_clock_time_device + RT_CLOCK_TIME_CAP_EVENT)
32+
- Programs the next deadline and calls rt_clock_time_event_isr() on expiry.
33+
- Clock hrtimer (clock_hrtimer.c)
34+
- Schedules high-resolution timeouts, programs the next event, and dispatches
35+
callbacks. Falls back to software timer when no hardware event is available.
36+
- Boottime helpers (clock_boottime.c)
37+
- Converts the monotonic counter into timeval/timespec/seconds for upper
38+
layers.
39+
- Clock timer adapter (clock_timer.c)
40+
- Exposes a unified hardware timer device (rt_clock_timer) and can register
41+
itself as a clock_time event device.
42+
- Architecture sources (arch/* and clock_time_arm_arch.c)
43+
- Provide fast CPU counters or architectural timers and register them as the
44+
default clock source when available.
45+
46+
## Data Flow
47+
48+
- Read path
49+
- Clock source counter -> scaled resolution -> nanoseconds -> boottime or
50+
clock_gettime.
51+
- Timeout path
52+
- HRTimer queue -> next expiry -> set_timeout on event device -> event ISR ->
53+
hrtimer processing -> callbacks.
54+
55+
# Configuration
56+
57+
Enable the subsystem in menuconfig:
58+
59+
```
60+
RT-Thread Components ->
61+
Device Drivers ->
62+
[*] Clock time subsystem (RT_USING_CLOCK_TIME)
63+
```
64+
65+
Optional settings:
66+
67+
- CLOCK_TIMER_FREQ (RISC-V): base counter frequency used by the clock source.
68+
- RT_CLOCK_TIME_ARM_ARCH: enable ARM architected timer integration (DM/OFW).
69+
70+
# BSP Integration Checklist
71+
72+
- Provide a clock source:
73+
- Register a rt_clock_time_device with CAP_SOURCE, or use the provided
74+
architecture source (AArch64/RISC-V) via rt_clock_time_source_init().
75+
- Provide a clock event:
76+
- Register a rt_clock_time_device with CAP_EVENT and call
77+
rt_clock_time_event_isr() in its interrupt handler.
78+
- Or register a rt_clock_timer device; it can become the default event
79+
device automatically.
80+
- Keep event ISRs short; heavy work should run in thread context if needed.
81+
82+
# Detailed Documents
83+
84+
- @subpage page_device_clock_time_core
85+
- @subpage page_device_clock_hrtimer
86+
- @subpage page_device_clock_boottime
87+
- @subpage page_device_clock_timer
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Clock Time 子系统概述
2+
3+
clock_time 子系统为 RT-Thread 提供统一的高精度时间基准与事件调度能力。
4+
它将单调计数(时钟源)与超时事件(时钟事件)解耦,使平台可以组合不同的
5+
硬件计数器与定时器,同时为内核与 libc 提供一致的时间行为。
6+
7+
# 软件架构
8+
9+
![Clock time architecture](figures/clock_time_arch.svg)
10+
11+
## 分层关系与职责
12+
13+
- 上层:内核超时/延时、POSIX/libc 时间接口(clock_gettime、nanosleep),
14+
以及 RTC/软 RTC,直接使用 clock_time 提供的时间与事件能力。
15+
- clock_time 子系统:核心 API、时钟源/事件设备、高精度定时器调度器、
16+
boottime 辅助函数、clock_timer 适配层。
17+
- 下层:BSP 驱动提供硬件计数器与定时器,并封装为 clock_time 设备或
18+
clock_timer 设备。
19+
20+
## 内部组成
21+
22+
- Core API(clock_time_core.c)
23+
- 负责设备注册、默认源/事件选择,以及计数 <-> 纳秒的缩放换算。
24+
- 时钟源设备(rt_clock_time_device + RT_CLOCK_TIME_CAP_SOURCE)
25+
- 提供自由运行计数器与频率,作为单调时间基准。
26+
- 时钟事件设备(rt_clock_time_device + RT_CLOCK_TIME_CAP_EVENT)
27+
- 负责下一次超时编程,并在到期时调用 rt_clock_time_event_isr()。
28+
- 高精度定时器(clock_hrtimer.c)
29+
- 维护超时队列、设置下一次事件并分发回调。没有硬件事件时退化为软件定时器。
30+
- Boottime 辅助(clock_boottime.c)
31+
- 将单调计数转换为 timeval/timespec/秒,供上层使用。
32+
- Clock timer 适配层(clock_timer.c)
33+
- 提供统一的硬件定时器设备(rt_clock_timer),并可注册为事件设备。
34+
- 架构源(arch/* 与 clock_time_arm_arch.c)
35+
- 提供 CPU 计数器或架构定时器,并在可用时设置为默认时钟源。
36+
37+
## 数据流
38+
39+
- 读取路径
40+
- 时钟源计数 -> 缩放分辨率 -> 纳秒 -> boottime 或 clock_gettime。
41+
- 超时路径
42+
- hrtimer 队列 -> 下一到期 -> 事件设备 set_timeout -> 事件中断 ->
43+
hrtimer 处理 -> 回调分发。
44+
45+
# 配置选项
46+
47+
在 menuconfig 中启用:
48+
49+
```
50+
RT-Thread Components ->
51+
Device Drivers ->
52+
[*] Clock time subsystem (RT_USING_CLOCK_TIME)
53+
```
54+
55+
可选配置:
56+
57+
- CLOCK_TIMER_FREQ(RISC-V):时钟源使用的基础计数频率。
58+
- RT_CLOCK_TIME_ARM_ARCH:启用 ARM 架构定时器集成(DM/OFW)。
59+
60+
# BSP 集成要点
61+
62+
- 提供时钟源:
63+
- 注册带 CAP_SOURCE 的 rt_clock_time_device,或使用
64+
rt_clock_time_source_init() 提供的架构源。
65+
- 提供时钟事件:
66+
- 注册带 CAP_EVENT 的 rt_clock_time_device,并在中断中调用
67+
rt_clock_time_event_isr()。
68+
- 或直接注册 rt_clock_timer 设备,自动成为默认事件设备。
69+
- 事件中断应保持简短,复杂处理建议转到线程上下文。
70+
71+
# 详细文档
72+
73+
- clock_time_core.md
74+
- clock_hrtimer.md
75+
- clock_boottime.md
76+
- clock_timer.md
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
@page page_device_clock_boottime Clock Boottime Helpers
2+
3+
# Overview
4+
5+
Boottime helpers convert the clock_time monotonic counter into standard time
6+
formats. The resulting values represent time since boot and do not depend on
7+
RTC or wall-clock settings.
8+
9+
# API
10+
11+
```c
12+
rt_err_t rt_clock_boottime_get_us(struct timeval *tv);
13+
rt_err_t rt_clock_boottime_get_s(time_t *t);
14+
rt_err_t rt_clock_boottime_get_ns(struct timespec *ts);
15+
```
16+
17+
All functions return RT_EOK on success or -RT_ERROR if the clock source is
18+
unavailable. The returned values are monotonic and suitable for measuring
19+
elapsed time.
20+
21+
## rt_clock_boottime_get_us
22+
23+
```c
24+
rt_err_t rt_clock_boottime_get_us(struct timeval *tv);
25+
```
26+
27+
- Purpose: get time since boot as seconds + microseconds.
28+
- Parameters: `tv` must be a valid pointer to `struct timeval`.
29+
- Return values:
30+
- RT_EOK: data written to `tv`.
31+
- -RT_ERROR: no valid clock_time source or conversion failed.
32+
- Notes:
33+
- `tv_usec` is derived from the clock_time resolution and may not be exact
34+
microseconds if the underlying counter does not align to 1 us.
35+
36+
## rt_clock_boottime_get_s
37+
38+
```c
39+
rt_err_t rt_clock_boottime_get_s(time_t *t);
40+
```
41+
42+
- Purpose: get time since boot in whole seconds.
43+
- Parameters: `t` must be a valid pointer to `time_t`.
44+
- Return values:
45+
- RT_EOK: `*t` updated.
46+
- -RT_ERROR: no valid clock_time source or conversion failed.
47+
- Notes:
48+
- Sub-second precision is discarded; use `rt_clock_boottime_get_us()` or
49+
`rt_clock_boottime_get_ns()` if needed.
50+
51+
## rt_clock_boottime_get_ns
52+
53+
```c
54+
rt_err_t rt_clock_boottime_get_ns(struct timespec *ts);
55+
```
56+
57+
- Purpose: get time since boot as seconds + nanoseconds.
58+
- Parameters: `ts` must be a valid pointer to `struct timespec`.
59+
- Return values:
60+
- RT_EOK: data written to `ts`.
61+
- -RT_ERROR: no valid clock_time source or conversion failed.
62+
- Notes:
63+
- `tv_nsec` reflects the clock_time resolution; it may not be 1 ns granularity
64+
if the counter frequency is lower.
65+
66+
# Example
67+
68+
```c
69+
#include <drivers/clock_time.h>
70+
71+
static void demo_boottime(void)
72+
{
73+
struct timespec ts;
74+
75+
if (rt_clock_boottime_get_ns(&ts) == RT_EOK)
76+
{
77+
rt_kprintf("boottime: %ld.%09ld\n", (long)ts.tv_sec, ts.tv_nsec);
78+
}
79+
}
80+
```
81+
82+
# Notes
83+
84+
- The boottime helpers are used by the soft RTC implementation to build a
85+
stable base time.
86+
- If no clock_time source is registered, the subsystem falls back to the tick
87+
counter and the resolution matches RT_TICK_PER_SECOND.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Clock Boottime 辅助
2+
3+
boottime 辅助函数将 clock_time 的单调计数转换为常见时间格式,返回值表示
4+
“系统启动以来的时间”,不受 RTC 或墙上时间设置影响。
5+
6+
# API
7+
8+
```c
9+
rt_err_t rt_clock_boottime_get_us(struct timeval *tv);
10+
rt_err_t rt_clock_boottime_get_s(time_t *t);
11+
rt_err_t rt_clock_boottime_get_ns(struct timespec *ts);
12+
```
13+
14+
成功返回 RT_EOK;若时钟源不可用则返回 -RT_ERROR。返回值单调递增,适合用于
15+
测量耗时。
16+
17+
## rt_clock_boottime_get_us
18+
19+
```c
20+
rt_err_t rt_clock_boottime_get_us(struct timeval *tv);
21+
```
22+
23+
- 作用:获取启动以来的时间,格式为秒 + 微秒。
24+
- 参数:`tv` 需为有效的 `struct timeval` 指针。
25+
- 返回值:
26+
- RT_EOK:成功写入 `tv`
27+
- -RT_ERROR:无有效时钟源或换算失败。
28+
- 说明:
29+
- `tv_usec` 由 clock_time 分辨率换算而来,不一定严格为 1 us 精度。
30+
31+
## rt_clock_boottime_get_s
32+
33+
```c
34+
rt_err_t rt_clock_boottime_get_s(time_t *t);
35+
```
36+
37+
- 作用:获取启动以来的整秒数。
38+
- 参数:`t` 需为有效的 `time_t` 指针。
39+
- 返回值:
40+
- RT_EOK:成功写入 `*t`。
41+
- -RT_ERROR:无有效时钟源或换算失败。
42+
- 说明:
43+
- 该接口仅返回秒,若需亚秒精度请使用其它接口。
44+
45+
## rt_clock_boottime_get_ns
46+
47+
```c
48+
rt_err_t rt_clock_boottime_get_ns(struct timespec *ts);
49+
```
50+
51+
- 作用:获取启动以来的时间,格式为秒 + 纳秒。
52+
- 参数:`ts` 需为有效的 `struct timespec` 指针。
53+
- 返回值:
54+
- RT_EOK:成功写入 `ts`
55+
- -RT_ERROR:无有效时钟源或换算失败。
56+
- 说明:
57+
- `tv_nsec` 的精度取决于计数频率,不一定达到 1 ns。
58+
59+
# 示例
60+
61+
```c
62+
#include <drivers/clock_time.h>
63+
64+
static void demo_boottime(void)
65+
{
66+
struct timespec ts;
67+
68+
if (rt_clock_boottime_get_ns(&ts) == RT_EOK)
69+
{
70+
rt_kprintf("boottime: %ld.%09ld\n", (long)ts.tv_sec, ts.tv_nsec);
71+
}
72+
}
73+
```
74+
75+
# 注意事项
76+
77+
- 软 RTC 会使用 boottime 作为稳定基准。
78+
- 未注册时钟源时,系统退化为 tick 计数,分辨率由 RT_TICK_PER_SECOND 决定。

0 commit comments

Comments
 (0)