diff options
| author | Bernard Xiong <[email protected]> | 2026-01-31 17:44:27 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-01-31 17:44:27 +0800 |
| commit | 743b614875fc33257be47ab53b6c5e040ec27893 (patch) | |
| tree | e4b78b63c5c331e4a69a9d01ffcc9ed24389a9b8 /components/drivers/include | |
| parent | f2a646cff4947bef7554ddbf443765e63d888d96 (diff) | |
[components][clock_time] Refactor time subsystem around clock_time (#11111)
* [components][clock_time] Refactor time subsystem around clock_time
Introduce the clock_time core with clock source/event separation, high-resolution scheduling, and boot-time helpers, plus clock_timer adapters for timer peripherals.
Remove legacy ktime/cputime/hwtimer implementations and migrate arch and BSP time paths to the new subsystem while keeping POSIX time integration functional.
Update drivers, Kconfig/SConscript wiring, documentation, and tests; add clock_time overview docs and align naming to clock_boottime/clock_hrtimer/clock_timer.
* [components][clock_time] Use BSP-provided clock timer frequency on riscv64
* [risc-v] Use runtime clock timer frequency for tick and delays
* [bsp] Add clock timer frequency hooks for riscv64 boards
* [bsp] Update Renesas RA driver doc clock_timer link
* [bsp] Sync zynqmp-r5-axu4ev rtconfig after config refresh
* [bsp][rk3500] Update rk3500 clock configuration
* [bsp][hpmicro] Add rt_hw_us_delay hook and update board delays
* [bsp][stm32l496-st-nucleo] enable clock_time for hwtimer sample in ci
* [bsp][hpmicro] Fix rtconfig include scope for hpm6750evk
Move rtconfig.h include outside the ENET_MULTIPLE_PORT guard for hpm6750evk and hpm6750evk2 so configuration macros are available regardless of ENET settings.
* [bsp][raspi3] select clock time for systimer
* [bsp][hpm5300evk] Trim trailing blank line
* [bsp][hpm5301evklite] Trim trailing blank line
* [bsp][hpm5e00evk] Trim trailing blank line
* [bsp][hpm6200evk] Trim trailing blank line
* [bsp][hpm6300evk] Trim trailing blank line
* [bsp][hpm6750evk] Trim trailing blank line
* [bsp][hpm6750evk2] Trim trailing blank line
* [bsp][hpm6750evkmini] Trim trailing blank line
* [bsp][hpm6800evk] Trim trailing blank line
* [bsp][hpm6e00evk] Trim trailing blank line
* [bsp][nxp] switch lpc178x to gcc and remove mcx timer source
* [bsp][stm32] fix the CONFIG_RT_USING_CLOCK_TIME issue.
* [docs][clock_time] add clock time documentation
* [docs][clock_time] Update clock time subsystem documentation
- Update device driver index to use correct page reference
- Clarify upper layer responsibilities in architecture overview
- Update README to describe POSIX/libc, Soft RTC, and device driver usage
- Refine architecture diagram with improved layout and color scheme
- Remove obsolete clock_timer.md file
* [kernel][utest] Trim trailing space
* [clock_time] Fix hrtimer wrap handling
* [clock_time] fix the static rt_inline issue
* [clock_time] fix the rt_clock_hrtimer_control result issue
Diffstat (limited to 'components/drivers/include')
| -rw-r--r-- | components/drivers/include/drivers/clock_time.h | 184 | ||||
| -rw-r--r-- | components/drivers/include/drivers/core/master_id.h | 2 | ||||
| -rw-r--r-- | components/drivers/include/drivers/cputime.h | 38 | ||||
| -rw-r--r-- | components/drivers/include/drivers/cputimer.h | 48 | ||||
| -rw-r--r-- | components/drivers/include/drivers/hwtimer.h | 89 | ||||
| -rw-r--r-- | components/drivers/include/rtdevice.h | 10 |
6 files changed, 188 insertions, 183 deletions
diff --git a/components/drivers/include/drivers/clock_time.h b/components/drivers/include/drivers/clock_time.h new file mode 100644 index 0000000000..7374b97d00 --- /dev/null +++ b/components/drivers/include/drivers/clock_time.h @@ -0,0 +1,184 @@ +/* + * 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 subsystem + */ +#ifndef __CLOCK_TIME_H__ +#define __CLOCK_TIME_H__ + +#include <rtthread.h> +#include <rtdevice.h> +#include <sys/time.h> +#include <ipc/completion.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#define RT_CLOCK_TIME_RESMUL (1000000ULL) + +#define RT_CLOCK_TIME_CAP_SOURCE (1U << 0) +#define RT_CLOCK_TIME_CAP_EVENT (1U << 1) + +struct rt_clock_time_device; + +struct rt_clock_time_ops +{ + rt_uint64_t (*get_freq)(struct rt_clock_time_device *dev); + rt_uint64_t (*get_counter)(struct rt_clock_time_device *dev); + rt_err_t (*set_timeout)(struct rt_clock_time_device *dev, rt_uint64_t delta); +}; + +struct rt_clock_time_device +{ + struct rt_device parent; + const struct rt_clock_time_ops *ops; + rt_uint64_t res_scale; + rt_uint8_t caps; +}; + +/* Clock time timer device (replacement for legacy hardware timer device) */ +typedef enum +{ + CLOCK_TIMER_CTRL_FREQ_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x01, + CLOCK_TIMER_CTRL_STOP = RT_DEVICE_CTRL_BASE(Timer) + 0x02, + CLOCK_TIMER_CTRL_INFO_GET = RT_DEVICE_CTRL_BASE(Timer) + 0x03, + CLOCK_TIMER_CTRL_MODE_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x04 +} rt_clock_timer_ctrl_t; + +typedef enum +{ + CLOCK_TIMER_MODE_ONESHOT = 0x01, + CLOCK_TIMER_MODE_PERIOD +} rt_clock_timer_mode_t; + +typedef struct rt_clock_timerval +{ + rt_int32_t sec; + rt_int32_t usec; +} rt_clock_timerval_t; + +#define CLOCK_TIMER_CNTMODE_UP 0x01 +#define CLOCK_TIMER_CNTMODE_DW 0x02 + +struct rt_clock_timer_device; + +struct rt_clock_timer_ops +{ + void (*init)(struct rt_clock_timer_device *timer, rt_uint32_t state); + rt_err_t (*start)(struct rt_clock_timer_device *timer, rt_uint32_t cnt, rt_clock_timer_mode_t mode); + void (*stop)(struct rt_clock_timer_device *timer); + rt_uint32_t (*count_get)(struct rt_clock_timer_device *timer); + rt_err_t (*control)(struct rt_clock_timer_device *timer, rt_uint32_t cmd, void *args); +}; + +struct rt_clock_timer_info +{ + rt_int32_t maxfreq; + rt_int32_t minfreq; + rt_uint32_t maxcnt; + rt_uint8_t cntmode; +}; + +typedef struct rt_clock_timer_device +{ + struct rt_device parent; + const struct rt_clock_timer_ops *ops; + const struct rt_clock_timer_info *info; + + rt_int32_t freq; + rt_int32_t overflow; + float period_sec; + rt_int32_t cycles; + rt_int32_t reload; + rt_clock_timer_mode_t mode; + +} rt_clock_timer_t; + +rt_err_t rt_clock_timer_register(rt_clock_timer_t *timer, const char *name, void *user_data); +void rt_clock_timer_isr(rt_clock_timer_t *timer); + +#ifdef RT_USING_DM +extern void (*rt_clock_timer_us_delay)(rt_uint32_t us); +#endif + +struct rt_clock_hrtimer +{ + rt_uint8_t flag; /**< compatible to tick timer's flag */ + char name[RT_NAME_MAX]; + rt_list_t node; + void *parameter; + unsigned long delay_cnt; + unsigned long timeout_cnt; + rt_err_t error; + struct rt_completion completion; + void (*timeout_func)(void *parameter); +}; +typedef struct rt_clock_hrtimer *rt_clock_hrtimer_t; + +rt_err_t rt_clock_time_device_register(struct rt_clock_time_device *dev, const char *name, rt_uint8_t caps); + +void rt_clock_time_set_default_source(struct rt_clock_time_device *dev); +void rt_clock_time_set_default_event(struct rt_clock_time_device *dev); +struct rt_clock_time_device *rt_clock_time_get_default_source(void); +struct rt_clock_time_device *rt_clock_time_get_default_event(void); + +void rt_clock_time_source_init(void); + +rt_uint64_t rt_clock_time_get_freq(void); +rt_uint64_t rt_clock_time_get_counter(void); +rt_uint64_t rt_clock_time_get_res_scaled(void); +rt_uint64_t rt_clock_time_get_event_freq(void); +rt_uint64_t rt_clock_time_get_event_res_scaled(void); + +rt_uint64_t rt_clock_time_counter_to_ns(rt_uint64_t cnt); +rt_uint64_t rt_clock_time_ns_to_counter(rt_uint64_t ns); + +rt_err_t rt_clock_time_set_timeout(rt_uint64_t delta); +void rt_clock_time_event_isr(void); + +rt_err_t rt_clock_boottime_get_us(struct timeval *tv); +rt_err_t rt_clock_boottime_get_s(time_t *t); +rt_err_t rt_clock_boottime_get_ns(struct timespec *ts); + +rt_uint64_t rt_clock_hrtimer_getres(void); +unsigned long rt_clock_hrtimer_getfrq(void); +rt_err_t rt_clock_hrtimer_settimeout(unsigned long cnt); +void rt_clock_hrtimer_process(void); + +void rt_clock_hrtimer_init(rt_clock_hrtimer_t timer, + const char *name, + rt_uint8_t flag, + void (*timeout)(void *parameter), + void *parameter); +rt_err_t rt_clock_hrtimer_start(rt_clock_hrtimer_t timer, unsigned long cnt); +rt_err_t rt_clock_hrtimer_stop(rt_clock_hrtimer_t timer); +rt_err_t rt_clock_hrtimer_control(rt_clock_hrtimer_t timer, int cmd, void *arg); +rt_err_t rt_clock_hrtimer_detach(rt_clock_hrtimer_t timer); + +rt_inline void rt_clock_hrtimer_keep_errno(rt_clock_hrtimer_t timer, rt_err_t err) +{ + RT_ASSERT(timer != RT_NULL); + + timer->error = err; + rt_set_errno(-err); +} + +void rt_clock_hrtimer_delay_init(struct rt_clock_hrtimer *timer); +void rt_clock_hrtimer_delay_detach(struct rt_clock_hrtimer *timer); +void rt_clock_hrtimer_process(void); + +rt_err_t rt_clock_hrtimer_sleep(struct rt_clock_hrtimer *timer, unsigned long cnt); +rt_err_t rt_clock_hrtimer_ndelay(struct rt_clock_hrtimer *timer, unsigned long ns); +rt_err_t rt_clock_hrtimer_udelay(struct rt_clock_hrtimer *timer, unsigned long us); +rt_err_t rt_clock_hrtimer_mdelay(struct rt_clock_hrtimer *timer, unsigned long ms); + +#ifdef __cplusplus +} +#endif + +#endif /* __CLOCK_TIME_H__ */ diff --git a/components/drivers/include/drivers/core/master_id.h b/components/drivers/include/drivers/core/master_id.h index 5bf9e611cb..99b1b09856 100644 --- a/components/drivers/include/drivers/core/master_id.h +++ b/components/drivers/include/drivers/core/master_id.h @@ -26,7 +26,7 @@ #define MASTER_ID_SERIAL 13 /* Clock Timer */ -#define MASTER_ID_HWTIMER 21 +#define MASTER_ID_CLOCK_TIMER 21 #define MASTER_ID_PTP 22 #define MASTER_ID_RTC 23 diff --git a/components/drivers/include/drivers/cputime.h b/components/drivers/include/drivers/cputime.h deleted file mode 100644 index 478ccfd019..0000000000 --- a/components/drivers/include/drivers/cputime.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2006-2023, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2017-12-23 Bernard first version - */ - -#ifndef CPUTIME_H__ -#define CPUTIME_H__ - -#include <stdint.h> -#include "cputimer.h" - -struct rt_clock_cputime_ops -{ - uint64_t (*cputime_getres)(void); - uint64_t (*cputime_gettime)(void); - int (*cputime_settimeout)(uint64_t tick, void (*timeout)(void *param), void *param); -}; - -uint64_t clock_cpu_getres(void); -uint64_t clock_cpu_gettime(void); -int clock_cpu_settimeout(uint64_t tick, void (*timeout)(void *param), void *param); -int clock_cpu_issettimeout(void); - -uint64_t clock_cpu_microsecond(uint64_t cpu_tick); -uint64_t clock_cpu_millisecond(uint64_t cpu_tick); - -int clock_cpu_setops(const struct rt_clock_cputime_ops *ops); - -#ifdef RT_USING_CPUTIME_RISCV -int riscv_cputime_init(void); -#endif /* RT_USING_CPUTIME_RISCV */ - -#endif diff --git a/components/drivers/include/drivers/cputimer.h b/components/drivers/include/drivers/cputimer.h deleted file mode 100644 index 371992a41e..0000000000 --- a/components/drivers/include/drivers/cputimer.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2006-2023, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-02-13 zhkag first version - */ - -#ifndef CPUTIMER_H__ -#define CPUTIMER_H__ - -#include <rtthread.h> - -struct rt_cputimer -{ - struct rt_object parent; /**< inherit from rt_object */ - rt_list_t row; - void (*timeout_func)(void *parameter); - void *parameter; - rt_uint64_t init_tick; - rt_uint64_t timeout_tick; - struct rt_semaphore sem; -}; -typedef struct rt_cputimer *rt_cputimer_t; - -rt_err_t rt_cputimer_detach(rt_cputimer_t timer); - -#ifdef RT_USING_HEAP -void rt_cputimer_init(rt_cputimer_t timer, - const char *name, - void (*timeout)(void *parameter), - void *parameter, - rt_uint64_t tick, - rt_uint8_t flag); -rt_err_t rt_cputimer_delete(rt_cputimer_t timer); -#endif - -rt_err_t rt_cputimer_start(rt_cputimer_t timer); -rt_err_t rt_cputimer_stop(rt_cputimer_t timer); -rt_err_t rt_cputimer_control(rt_cputimer_t timer, int cmd, void *arg); -rt_err_t rt_cputime_sleep(rt_uint64_t tick); -rt_err_t rt_cputime_ndelay(rt_uint64_t ns); -rt_err_t rt_cputime_udelay(rt_uint64_t us); -rt_err_t rt_cputime_mdelay(rt_uint64_t ms); - -#endif diff --git a/components/drivers/include/drivers/hwtimer.h b/components/drivers/include/drivers/hwtimer.h deleted file mode 100644 index 6f11ff2c54..0000000000 --- a/components/drivers/include/drivers/hwtimer.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2006-2023, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - */ -#ifndef __HWTIMER_H__ -#define __HWTIMER_H__ - -#include <rtthread.h> - -#ifdef __cplusplus -extern "C" { -#endif - -/* Timer Control Command */ -typedef enum -{ - HWTIMER_CTRL_FREQ_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x01, /* set the count frequency */ - HWTIMER_CTRL_STOP = RT_DEVICE_CTRL_BASE(Timer) + 0x02, /* stop timer */ - HWTIMER_CTRL_INFO_GET = RT_DEVICE_CTRL_BASE(Timer) + 0x03, /* get a timer feature information */ - HWTIMER_CTRL_MODE_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x04 /* Setting the timing mode(oneshot/period) */ -} rt_hwtimer_ctrl_t; - -/* Timing Mode */ -typedef enum -{ - HWTIMER_MODE_ONESHOT = 0x01, - HWTIMER_MODE_PERIOD -} rt_hwtimer_mode_t; - -/* Time Value */ -typedef struct rt_hwtimerval -{ - rt_int32_t sec; /* second */ - rt_int32_t usec; /* microsecond */ -} rt_hwtimerval_t; - -#define HWTIMER_CNTMODE_UP 0x01 /* increment count mode */ -#define HWTIMER_CNTMODE_DW 0x02 /* decreasing count mode */ - -struct rt_hwtimer_device; - -struct rt_hwtimer_ops -{ - void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state); - rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode); - void (*stop)(struct rt_hwtimer_device *timer); - rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer); - rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args); -}; - -/* Timer Feature Information */ -struct rt_hwtimer_info -{ - rt_int32_t maxfreq; /* the maximum count frequency timer support */ - rt_int32_t minfreq; /* the minimum count frequency timer support */ - rt_uint32_t maxcnt; /* counter maximum value */ - rt_uint8_t cntmode; /* count mode (inc/dec) */ -}; - -typedef struct rt_hwtimer_device -{ - struct rt_device parent; - const struct rt_hwtimer_ops *ops; - const struct rt_hwtimer_info *info; - - rt_int32_t freq; /* counting frequency set by the user */ - rt_int32_t overflow; /* timer overflows */ - float period_sec; - rt_int32_t cycles; /* how many times will generate a timeout event after overflow */ - rt_int32_t reload; /* reload cycles(using in period mode) */ - rt_hwtimer_mode_t mode; /* timing mode(oneshot/period) */ -} rt_hwtimer_t; - -rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data); -void rt_device_hwtimer_isr(rt_hwtimer_t *timer); - -#ifdef RT_USING_DM -extern void (*rt_device_hwtimer_us_delay)(rt_uint32_t us); -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index 50a97beee5..c3a1277449 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -242,18 +242,14 @@ extern "C" { #include "drivers/dev_can.h" #endif /* RT_USING_CAN */ -#ifdef RT_USING_HWTIMER -#include "drivers/hwtimer.h" -#endif /* RT_USING_HWTIMER */ +#ifdef RT_USING_CLOCK_TIME +#include "drivers/clock_time.h" +#endif /* RT_USING_CLOCK_TIME */ #ifdef RT_USING_AUDIO #include "drivers/dev_audio.h" #endif /* RT_USING_AUDIO */ -#ifdef RT_USING_CPUTIME -#include "drivers/cputime.h" -#endif /* RT_USING_CPUTIME */ - #ifdef RT_USING_ADC #include "drivers/adc.h" #endif /* RT_USING_ADC */ |
