diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index f803469d649..d772203b458 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -5,7 +5,6 @@ rsource "ipc/Kconfig" rsource "serial/Kconfig" rsource "can/Kconfig" -rsource "cputime/Kconfig" rsource "i2c/Kconfig" rsource "phy/Kconfig" rsource "misc/Kconfig" @@ -46,9 +45,8 @@ rsource "pci/Kconfig" rsource "pic/Kconfig" rsource "pin/Kconfig" rsource "pinctrl/Kconfig" -rsource "ktime/Kconfig" +rsource "clock_time/Kconfig" rsource "clk/Kconfig" -rsource "hwtimer/Kconfig" rsource "usb/Kconfig" endmenu diff --git a/components/drivers/clock_time/Kconfig b/components/drivers/clock_time/Kconfig new file mode 100644 index 00000000000..1441cc3da94 --- /dev/null +++ b/components/drivers/clock_time/Kconfig @@ -0,0 +1,28 @@ +menuconfig RT_USING_CLOCK_TIME + bool "Using unified clock_time subsystem" + default n + help + Unified time subsystem that consolidates hwtimer, ktime, and cputime. + Provides clock source, clock event, and high-resolution timer support. + +if RT_USING_CLOCK_TIME + + config RT_USING_CLOCK_HRTIMER + bool "Enable high-resolution timer support" + default y + help + Enable high-resolution software timers built on clock_time devices. + + config RT_USING_CLOCK_CPUTIME + bool "Enable CPU time APIs" + default y + help + Enable CPU time measurement and delay APIs. + + config RT_USING_CLOCK_BOOTTIME + bool "Enable boottime APIs" + default y + help + Enable system boottime (monotonic time since boot) APIs. + +endif diff --git a/components/drivers/clock_time/README.md b/components/drivers/clock_time/README.md new file mode 100644 index 00000000000..5be7b2241f5 --- /dev/null +++ b/components/drivers/clock_time/README.md @@ -0,0 +1,638 @@ +# RT-Thread Clock Time Subsystem / RT-Thread 时钟时间子系统 + +English | [中文](#中文文档) + +## Overview + +The `clock_time` subsystem provides a unified interface for time-related hardware and software functionality in RT-Thread. It consolidates and replaces the previous separate subsystems: +- **hwtimer**: Hardware timer device drivers +- **ktime**: Kernel time and high-resolution timers +- **cputime**: CPU time measurement + +## Architecture + +The clock_time subsystem follows a C-OOP (C Object-Oriented Programming) design pattern with clear separation of concerns: + +``` +┌─────────────────────────────────────────────────────────┐ +│ Application Layer │ +│ (POSIX clock_gettime, nanosleep, etc.) │ +└────────────────┬────────────────────────────────────────┘ + │ +┌────────────────▼────────────────────────────────────────┐ +│ Clock Time High-Level APIs │ +│ • Boottime (monotonic time since boot) │ +│ • High-Resolution Timers (hrtimer) │ +│ • CPU Timer (counter access) │ +└────────────────┬────────────────────────────────────────┘ + │ +┌────────────────▼────────────────────────────────────────┐ +│ Clock Time Device Abstraction │ +│ struct rt_clock_time_device │ +│ struct rt_clock_time_ops │ +└────────────────┬────────────────────────────────────────┘ + │ +┌────────────────▼────────────────────────────────────────┐ +│ Hardware / BSP Implementation │ +│ • ARM Arch Timer, RISC-V RDTIME │ +│ • SysTick, System Timer │ +│ • Hardware Timer Peripherals │ +└─────────────────────────────────────────────────────────┘ +``` + +### Key Components + +1. **Device Abstraction** (`clock_time.c`) + - Unified device interface for timer hardware + - Device registration and management + - Capability flags (clocksource, clockevent) + +2. **High-Resolution Timers** (`hrtimer.c`) + - Software timer scheduling on hardware timers + - Linked-list or red-black tree management + - SMP-safe with spinlock protection + +3. **Boottime APIs** (`clock_time_boottime.c`) + - Monotonic time since system boot + - Nanosecond, microsecond, second precision + +4. **CPU Timer** (`clock_time_tick.c`, `clock_time_cputime.c`) + - Low-level counter access + - Frequency and resolution queries + - Tick-based fallback for simple systems + +## Configuration + +Enable clock_time in menuconfig: + +``` +Device Drivers ---> + [*] Using unified clock_time subsystem ---> + [*] Enable high-resolution timer support + [*] Enable CPU time APIs + [*] Enable boottime APIs +``` + +### Backward Compatibility + +When `RT_USING_CLOCK_TIME` is enabled, the old subsystems are automatically disabled and their APIs are redirected: + +- `RT_USING_HWTIMER` → Automatically set (compatibility mode) +- `RT_USING_KTIME` → Automatically set (compatibility mode) +- `RT_USING_CPUTIME` → Automatically set (compatibility mode) + +All `rt_ktime_*` APIs are mapped to `rt_clock_*` APIs via macros, ensuring existing code continues to work. + +## API Reference + +### Device Management + +```c +/* Register a clock time device */ +rt_err_t rt_clock_time_device_register(struct rt_clock_time_device *dev, + const char *name, + rt_uint8_t caps); + +/* Get/set default system clock device */ +rt_clock_time_t rt_clock_time_default(void); +rt_err_t rt_clock_time_set_default(rt_clock_time_t dev); +``` + +### Boottime APIs + +```c +/* Get system boottime (monotonic time since boot) */ +rt_err_t rt_clock_boottime_get_ns(struct timespec *ts); // Nanosecond precision +rt_err_t rt_clock_boottime_get_us(struct timeval *tv); // Microsecond precision +rt_err_t rt_clock_boottime_get_s(time_t *t); // Second precision +``` + +### CPU Timer APIs + +```c +/* Low-level counter access */ +rt_uint64_t rt_clock_cputimer_getres(void); // Get resolution +unsigned long rt_clock_cputimer_getfrq(void); // Get frequency in Hz +unsigned long rt_clock_cputimer_getcnt(void); // Get current counter value +void rt_clock_cputimer_init(void); // Initialize (called by system) +``` + +### High-Resolution Timer APIs + +```c +/* Initialize and manage high-resolution timers */ +void rt_clock_hrtimer_init(rt_clock_hrtimer_t timer, const char *name, + rt_uint8_t flag, void (*timeout)(void *), void *param); +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_detach(rt_clock_hrtimer_t timer); + +/* Delay functions (blocking) */ +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); +``` + +## BSP Porting Guide + +### Minimal Implementation (Tick-based) + +The tick-based fallback works out-of-the-box for simple systems: + +```c +// No additional code needed - uses RT_TICK_PER_SECOND +// Automatically provides: +// - rt_clock_cputimer_getcnt() → rt_tick_get() +// - rt_clock_cputimer_getfrq() → RT_TICK_PER_SECOND +// - Resolution based on tick rate +``` + +### Hardware Timer Implementation + +For better precision, implement hardware timer support: + +```c +#include + +/* Define hardware operations */ +static rt_uint64_t my_timer_get_freq(void) { + return 1000000; // 1 MHz +} + +static rt_uint64_t my_timer_get_counter(void) { + return READ_TIMER_COUNTER_REG(); +} + +static rt_err_t my_timer_set_timeout(rt_uint64_t delta) { + if (delta == 0) { + DISABLE_TIMER_INTERRUPT(); + return RT_EOK; + } + SET_TIMER_COMPARE_VALUE(delta); + ENABLE_TIMER_INTERRUPT(); + return RT_EOK; +} + +static const struct rt_clock_time_ops my_timer_ops = { + .get_freq = my_timer_get_freq, + .get_counter = my_timer_get_counter, + .set_timeout = my_timer_set_timeout, +}; + +/* Define device structure */ +static struct rt_clock_time_device my_timer_dev; + +/* Initialize and register */ +int my_timer_init(void) { + HARDWARE_TIMER_INIT(); + + my_timer_dev.ops = &my_timer_ops; + + return rt_clock_time_device_register(&my_timer_dev, "timer0", + RT_CLOCK_TIME_CAP_CLOCKSOURCE | RT_CLOCK_TIME_CAP_CLOCKEVENT); +} +INIT_BOARD_EXPORT(my_timer_init); + +/* Timer interrupt handler */ +void timer_irq_handler(void) { + CLEAR_TIMER_INTERRUPT(); + rt_clock_hrtimer_process(); // Process expired timers +} +``` + +### Overriding CPU Timer Functions + +For architecture-specific high-performance counters (e.g., ARM DWT, RISC-V RDTIME): + +```c +/* Override weak functions in your BSP */ +unsigned long rt_clock_cputimer_getcnt(void) { + return READ_CPU_CYCLE_COUNTER(); +} + +unsigned long rt_clock_cputimer_getfrq(void) { + return CPU_FREQUENCY_HZ; +} + +rt_uint64_t rt_clock_cputimer_getres(void) { + // Resolution in ns * RT_CLOCK_TIME_RESMUL + return ((1000000000ULL * RT_CLOCK_TIME_RESMUL) / CPU_FREQUENCY_HZ); +} +``` + +## Migration Guide + +### From ktime + +All `rt_ktime_*` APIs are automatically mapped. No code changes needed: + +```c +// Old code (still works) +#include +rt_ktime_boottime_get_ns(&ts); +rt_ktime_hrtimer_init(&timer, "mytimer", ...); + +// Equivalent new code (optional migration) +#include +rt_clock_boottime_get_ns(&ts); +rt_clock_hrtimer_init(&timer, "mytimer", ...); +``` + +### From hwtimer + +Hardware timer drivers should be updated to use the new device abstraction: + +```c +// Old: struct rt_hwtimer_device + rt_device_hwtimer_register() +// New: struct rt_clock_time_device + rt_clock_time_device_register() +``` + +### From cputime + +CPU time APIs have been unified: + +```c +// Old: clock_cpu_gettime() / clock_cpu_getres() +// New: rt_clock_cputimer_getcnt() / rt_clock_cputimer_getres() +// Note: Old APIs still work if using old cputime module +``` + +## Examples + +### Example 1: One-shot Timer + +```c +#include + +static void timer_callback(void *param) { + rt_kprintf("Timer expired!\n"); +} + +void example_oneshot_timer(void) { + struct rt_clock_hrtimer timer; + + rt_clock_hrtimer_init(&timer, "oneshot", RT_TIMER_FLAG_ONE_SHOT, + timer_callback, NULL); + + // Start timer for 1 second (using CPU timer frequency) + unsigned long freq = rt_clock_cputimer_getfrq(); + rt_clock_hrtimer_start(&timer, freq * 1); + + // Timer will fire callback after 1 second +} +``` + +### Example 2: Microsecond Delay + +```c +#include + +void example_delay(void) { + struct rt_clock_hrtimer timer; + + rt_clock_hrtimer_delay_init(&timer); + + // Delay for 500 microseconds + rt_clock_hrtimer_udelay(&timer, 500); + + rt_clock_hrtimer_delay_detach(&timer); +} +``` + +### Example 3: Boottime Measurement + +```c +#include + +void example_boottime(void) { + struct timespec ts; + + rt_clock_boottime_get_ns(&ts); + rt_kprintf("System has been running for %ld.%09ld seconds\n", + ts.tv_sec, ts.tv_nsec); +} +``` + +--- + +## 中文文档 + +## 概述 + +`clock_time` 子系统为 RT-Thread 中的时间相关硬件和软件功能提供统一接口。它整合并替代了以前的独立子系统: +- **hwtimer**:硬件定时器设备驱动 +- **ktime**:内核时间和高分辨率定时器 +- **cputime**:CPU 时间测量 + +## 架构 + +clock_time 子系统遵循 C-OOP(C 语言面向对象编程)设计模式,职责清晰分离: + +``` +┌─────────────────────────────────────────────────────────┐ +│ 应用层 │ +│ (POSIX clock_gettime, nanosleep 等) │ +└────────────────┬────────────────────────────────────────┘ + │ +┌────────────────▼────────────────────────────────────────┐ +│ 时钟时间高层 API │ +│ • Boottime (自启动以来的单调时间) │ +│ • 高分辨率定时器 (hrtimer) │ +│ • CPU 定时器 (计数器访问) │ +└────────────────┬────────────────────────────────────────┘ + │ +┌────────────────▼────────────────────────────────────────┐ +│ 时钟时间设备抽象 │ +│ struct rt_clock_time_device │ +│ struct rt_clock_time_ops │ +└────────────────┬────────────────────────────────────────┘ + │ +┌────────────────▼────────────────────────────────────────┐ +│ 硬件 / BSP 实现 │ +│ • ARM Arch Timer, RISC-V RDTIME │ +│ • SysTick, 系统定时器 │ +│ • 硬件定时器外设 │ +└─────────────────────────────────────────────────────────┘ +``` + +### 关键组件 + +1. **设备抽象** (`clock_time.c`) + - 定时器硬件的统一设备接口 + - 设备注册和管理 + - 能力标志(时钟源、时钟事件) + +2. **高分辨率定时器** (`hrtimer.c`) + - 在硬件定时器上的软件定时器调度 + - 链表或红黑树管理 + - SMP 安全,带自旋锁保护 + +3. **Boottime API** (`clock_time_boottime.c`) + - 自系统启动以来的单调时间 + - 纳秒、微秒、秒精度 + +4. **CPU 定时器** (`clock_time_tick.c`, `clock_time_cputime.c`) + - 底层计数器访问 + - 频率和分辨率查询 + - 简单系统的基于 tick 的回退 + +## 配置 + +在 menuconfig 中启用 clock_time: + +``` +Device Drivers ---> + [*] Using unified clock_time subsystem ---> + [*] Enable high-resolution timer support + [*] Enable CPU time APIs + [*] Enable boottime APIs +``` + +### 向后兼容 + +当启用 `RT_USING_CLOCK_TIME` 时,旧子系统自动禁用,其 API 被重定向: + +- `RT_USING_HWTIMER` → 自动设置(兼容模式) +- `RT_USING_KTIME` → 自动设置(兼容模式) +- `RT_USING_CPUTIME` → 自动设置(兼容模式) + +所有 `rt_ktime_*` API 通过宏映射到 `rt_clock_*` API,确保现有代码继续工作。 + +## API 参考 + +### 设备管理 + +```c +/* 注册时钟时间设备 */ +rt_err_t rt_clock_time_device_register(struct rt_clock_time_device *dev, + const char *name, + rt_uint8_t caps); + +/* 获取/设置默认系统时钟设备 */ +rt_clock_time_t rt_clock_time_default(void); +rt_err_t rt_clock_time_set_default(rt_clock_time_t dev); +``` + +### Boottime API + +```c +/* 获取系统启动时间(自启动以来的单调时间) */ +rt_err_t rt_clock_boottime_get_ns(struct timespec *ts); // 纳秒精度 +rt_err_t rt_clock_boottime_get_us(struct timeval *tv); // 微秒精度 +rt_err_t rt_clock_boottime_get_s(time_t *t); // 秒精度 +``` + +### CPU 定时器 API + +```c +/* 底层计数器访问 */ +rt_uint64_t rt_clock_cputimer_getres(void); // 获取分辨率 +unsigned long rt_clock_cputimer_getfrq(void); // 获取频率(Hz) +unsigned long rt_clock_cputimer_getcnt(void); // 获取当前计数器值 +void rt_clock_cputimer_init(void); // 初始化(由系统调用) +``` + +### 高分辨率定时器 API + +```c +/* 初始化和管理高分辨率定时器 */ +void rt_clock_hrtimer_init(rt_clock_hrtimer_t timer, const char *name, + rt_uint8_t flag, void (*timeout)(void *), void *param); +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_detach(rt_clock_hrtimer_t timer); + +/* 延时函数(阻塞) */ +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); +``` + +## BSP 移植指南 + +### 最小实现(基于 Tick) + +基于 tick 的回退对简单系统开箱即用: + +```c +// 无需额外代码 - 使用 RT_TICK_PER_SECOND +// 自动提供: +// - rt_clock_cputimer_getcnt() → rt_tick_get() +// - rt_clock_cputimer_getfrq() → RT_TICK_PER_SECOND +// - 基于 tick 速率的分辨率 +``` + +### 硬件定时器实现 + +为了更好的精度,实现硬件定时器支持: + +```c +#include + +/* 定义硬件操作 */ +static rt_uint64_t my_timer_get_freq(void) { + return 1000000; // 1 MHz +} + +static rt_uint64_t my_timer_get_counter(void) { + return READ_TIMER_COUNTER_REG(); +} + +static rt_err_t my_timer_set_timeout(rt_uint64_t delta) { + if (delta == 0) { + DISABLE_TIMER_INTERRUPT(); + return RT_EOK; + } + SET_TIMER_COMPARE_VALUE(delta); + ENABLE_TIMER_INTERRUPT(); + return RT_EOK; +} + +static const struct rt_clock_time_ops my_timer_ops = { + .get_freq = my_timer_get_freq, + .get_counter = my_timer_get_counter, + .set_timeout = my_timer_set_timeout, +}; + +/* 定义设备结构 */ +static struct rt_clock_time_device my_timer_dev; + +/* 初始化和注册 */ +int my_timer_init(void) { + HARDWARE_TIMER_INIT(); + + my_timer_dev.ops = &my_timer_ops; + + return rt_clock_time_device_register(&my_timer_dev, "timer0", + RT_CLOCK_TIME_CAP_CLOCKSOURCE | RT_CLOCK_TIME_CAP_CLOCKEVENT); +} +INIT_BOARD_EXPORT(my_timer_init); + +/* 定时器中断处理程序 */ +void timer_irq_handler(void) { + CLEAR_TIMER_INTERRUPT(); + rt_clock_hrtimer_process(); // 处理过期定时器 +} +``` + +### 覆盖 CPU 定时器函数 + +对于架构特定的高性能计数器(例如 ARM DWT、RISC-V RDTIME): + +```c +/* 在 BSP 中覆盖弱函数 */ +unsigned long rt_clock_cputimer_getcnt(void) { + return READ_CPU_CYCLE_COUNTER(); +} + +unsigned long rt_clock_cputimer_getfrq(void) { + return CPU_FREQUENCY_HZ; +} + +rt_uint64_t rt_clock_cputimer_getres(void) { + // 分辨率(纳秒 * RT_CLOCK_TIME_RESMUL) + return ((1000000000ULL * RT_CLOCK_TIME_RESMUL) / CPU_FREQUENCY_HZ); +} +``` + +## 迁移指南 + +### 从 ktime 迁移 + +所有 `rt_ktime_*` API 自动映射。无需更改代码: + +```c +// 旧代码(仍然有效) +#include +rt_ktime_boottime_get_ns(&ts); +rt_ktime_hrtimer_init(&timer, "mytimer", ...); + +// 等效的新代码(可选迁移) +#include +rt_clock_boottime_get_ns(&ts); +rt_clock_hrtimer_init(&timer, "mytimer", ...); +``` + +### 从 hwtimer 迁移 + +硬件定时器驱动应更新为使用新的设备抽象: + +```c +// 旧:struct rt_hwtimer_device + rt_device_hwtimer_register() +// 新:struct rt_clock_time_device + rt_clock_time_device_register() +``` + +### 从 cputime 迁移 + +CPU 时间 API 已统一: + +```c +// 旧:clock_cpu_gettime() / clock_cpu_getres() +// 新:rt_clock_cputimer_getcnt() / rt_clock_cputimer_getres() +// 注意:如果使用旧 cputime 模块,旧 API 仍然有效 +``` + +## 示例 + +### 示例 1:一次性定时器 + +```c +#include + +static void timer_callback(void *param) { + rt_kprintf("定时器过期!\n"); +} + +void example_oneshot_timer(void) { + struct rt_clock_hrtimer timer; + + rt_clock_hrtimer_init(&timer, "oneshot", RT_TIMER_FLAG_ONE_SHOT, + timer_callback, NULL); + + // 启动定时器 1 秒(使用 CPU 定时器频率) + unsigned long freq = rt_clock_cputimer_getfrq(); + rt_clock_hrtimer_start(&timer, freq * 1); + + // 定时器将在 1 秒后触发回调 +} +``` + +### 示例 2:微秒延时 + +```c +#include + +void example_delay(void) { + struct rt_clock_hrtimer timer; + + rt_clock_hrtimer_delay_init(&timer); + + // 延时 500 微秒 + rt_clock_hrtimer_udelay(&timer, 500); + + rt_clock_hrtimer_delay_detach(&timer); +} +``` + +### 示例 3:启动时间测量 + +```c +#include + +void example_boottime(void) { + struct timespec ts; + + rt_clock_boottime_get_ns(&ts); + rt_kprintf("系统已运行 %ld.%09ld 秒\n", + ts.tv_sec, ts.tv_nsec); +} +``` + +## License + +Apache-2.0 + +## References + +- RT-Thread Programming Guide: https://www.rt-thread.io/document/site/ +- RT-Thread Coding Style: https://github.com/RT-Thread/rt-thread/blob/master/documentation/coding_style_cn.md diff --git a/components/drivers/clock_time/SConscript b/components/drivers/clock_time/SConscript new file mode 100644 index 00000000000..bd3346cd9c9 --- /dev/null +++ b/components/drivers/clock_time/SConscript @@ -0,0 +1,28 @@ +from building import * + +cwd = GetCurrentDir() +src = [] +inc = [cwd + '/inc'] + +if GetDepend(['RT_USING_CLOCK_TIME']): + # Core clock_time device implementation + src += ['src/clock_time.c'] + + # High-resolution timer support + if GetDepend(['RT_USING_CLOCK_HRTIMER']): + src += ['src/hrtimer.c'] + + # CPU time APIs + if GetDepend(['RT_USING_CLOCK_CPUTIME']): + src += ['src/clock_time_cputime.c'] + + # Boottime APIs + if GetDepend(['RT_USING_CLOCK_BOOTTIME']): + src += ['src/clock_time_boottime.c'] + + # Tick-based fallback implementation + src += ['src/clock_time_tick.c'] + +group = DefineGroup('Drivers', src, depend = [''], CPPPATH = inc) + +Return('group') diff --git a/components/drivers/clock_time/src/clock_time.c b/components/drivers/clock_time/src/clock_time.c new file mode 100644 index 00000000000..82a8f14ff10 --- /dev/null +++ b/components/drivers/clock_time/src/clock_time.c @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-01-01 RT-Thread Unified clock_time subsystem implementation + */ + +#include +#include +#include + +#define DBG_TAG "clock_time" +#define DBG_LVL DBG_INFO +#include + +/* Default system clock time device */ +static rt_clock_time_t _default_device = RT_NULL; + +/** + * @brief Register a clock time device + */ +rt_err_t rt_clock_time_device_register(struct rt_clock_time_device *dev, + const char *name, + rt_uint8_t caps) +{ + rt_err_t result; + + RT_ASSERT(dev != RT_NULL); + RT_ASSERT(name != RT_NULL); + RT_ASSERT(dev->ops != RT_NULL); + + /* Initialize parent device structure */ + dev->parent.type = RT_Device_Class_Timer; + dev->parent.rx_indicate = RT_NULL; + dev->parent.tx_complete = RT_NULL; + + dev->parent.init = RT_NULL; + dev->parent.open = RT_NULL; + dev->parent.close = RT_NULL; + dev->parent.read = RT_NULL; + dev->parent.write = RT_NULL; + dev->parent.control = RT_NULL; + + dev->caps = caps; + + /* Calculate resolution scale factor */ + if (dev->ops->get_freq) + { + rt_uint64_t freq = dev->ops->get_freq(); + if (freq > 0) + { + /* res_scale = (1e9 * RT_CLOCK_TIME_RESMUL) / freq + * To avoid overflow, we check if freq is very small. + * For freq >= 1000, this calculation is safe on 64-bit. + * For very small frequencies, limit the scale factor. + */ + if (freq >= 1000) + { + dev->res_scale = ((1000000000ULL * RT_CLOCK_TIME_RESMUL) / freq); + } + else + { + /* For very low frequencies, calculate more carefully to avoid precision loss */ + dev->res_scale = ((1000000ULL * RT_CLOCK_TIME_RESMUL * 1000) / freq); + } + } + else + { + dev->res_scale = RT_CLOCK_TIME_RESMUL; + } + } + else + { + dev->res_scale = RT_CLOCK_TIME_RESMUL; + } + + /* Register device */ + result = rt_device_register(&dev->parent, name, RT_DEVICE_FLAG_RDWR); + if (result != RT_EOK) + { + LOG_E("Failed to register clock_time device: %s", name); + return result; + } + + /* Set as default if none exists */ + if (_default_device == RT_NULL) + { + _default_device = dev; + LOG_D("Set %s as default clock_time device", name); + } + + LOG_I("Registered clock_time device: %s (caps: 0x%02x)", name, caps); + + return RT_EOK; +} + +/** + * @brief Get the default system clock time device + */ +rt_clock_time_t rt_clock_time_default(void) +{ + return _default_device; +} + +/** + * @brief Set the default system clock time device + */ +rt_err_t rt_clock_time_set_default(rt_clock_time_t dev) +{ + RT_ASSERT(dev != RT_NULL); + + _default_device = dev; + LOG_D("Changed default clock_time device to: %s", dev->parent.parent.name); + + return RT_EOK; +} diff --git a/components/drivers/clock_time/src/clock_time_boottime.c b/components/drivers/clock_time/src/clock_time_boottime.c new file mode 100644 index 00000000000..0df1c5831c5 --- /dev/null +++ b/components/drivers/clock_time/src/clock_time_boottime.c @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-01-01 RT-Thread Boottime APIs implementation + */ + +#include +#include +#include +#include + +rt_weak rt_err_t rt_clock_boottime_get_us(struct timeval *tv) +{ + RT_ASSERT(tv != RT_NULL); + + /* Use 64-bit intermediate values to prevent overflow */ + rt_uint64_t cnt = rt_clock_cputimer_getcnt(); + rt_uint64_t res = rt_clock_cputimer_getres(); + rt_uint64_t ns = (cnt * res) / RT_CLOCK_TIME_RESMUL; + + tv->tv_sec = (long)(ns / (1000ULL * 1000 * 1000)); + tv->tv_usec = (long)((ns % (1000ULL * 1000 * 1000)) / 1000); + + return RT_EOK; +} + +rt_weak rt_err_t rt_clock_boottime_get_s(time_t *t) +{ + RT_ASSERT(t != RT_NULL); + + /* Use 64-bit intermediate values to prevent overflow */ + rt_uint64_t cnt = rt_clock_cputimer_getcnt(); + rt_uint64_t res = rt_clock_cputimer_getres(); + rt_uint64_t ns = (cnt * res) / RT_CLOCK_TIME_RESMUL; + + *t = (time_t)(ns / (1000ULL * 1000 * 1000)); + + return RT_EOK; +} + +rt_weak rt_err_t rt_clock_boottime_get_ns(struct timespec *ts) +{ + RT_ASSERT(ts != RT_NULL); + + /* Use 64-bit intermediate values to prevent overflow */ + rt_uint64_t cnt = rt_clock_cputimer_getcnt(); + rt_uint64_t res = rt_clock_cputimer_getres(); + rt_uint64_t ns = (cnt * res) / RT_CLOCK_TIME_RESMUL; + + ts->tv_sec = (time_t)(ns / (1000ULL * 1000 * 1000)); + ts->tv_nsec = (long)(ns % (1000ULL * 1000 * 1000)); + + return RT_EOK; +} + diff --git a/components/drivers/clock_time/src/clock_time_cputime.c b/components/drivers/clock_time/src/clock_time_cputime.c new file mode 100644 index 00000000000..5917365cfbc --- /dev/null +++ b/components/drivers/clock_time/src/clock_time_cputime.c @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-01-01 RT-Thread CPU time APIs (delegates to clock_time core) + */ + +#include +#include +#include + +/* + * Additional CPU time utility functions + * Core implementations are in clock_time_tick.c + */ + +/* These functions are implemented inline or use the core APIs */ + +/* Note: The main cputime APIs are now: + * rt_clock_cputimer_getres() + * rt_clock_cputimer_getfrq() + * rt_clock_cputimer_getcnt() + * rt_clock_cputimer_init() + * + * These are defined in clock_time_tick.c as weak functions + * that can be overridden by BSP implementations. + */ diff --git a/components/drivers/clock_time/src/clock_time_tick.c b/components/drivers/clock_time/src/clock_time_tick.c new file mode 100644 index 00000000000..ebf3ff9c6e5 --- /dev/null +++ b/components/drivers/clock_time/src/clock_time_tick.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-01-01 RT-Thread Tick-based fallback implementation for clock_time + */ + +#include +#include +#include + +/* + * CPU Timer APIs - Default tick-based implementation + * These are weak functions that can be overridden by BSP-specific implementations + */ + +rt_weak rt_uint64_t rt_clock_cputimer_getres(void) +{ + /* Resolution in nanoseconds * RT_CLOCK_TIME_RESMUL */ + return ((1000ULL * 1000 * 1000) * RT_CLOCK_TIME_RESMUL) / RT_TICK_PER_SECOND; +} + +rt_weak unsigned long rt_clock_cputimer_getfrq(void) +{ + return RT_TICK_PER_SECOND; +} + +rt_weak unsigned long rt_clock_cputimer_getcnt(void) +{ + return rt_tick_get(); +} + +rt_weak void rt_clock_cputimer_init(void) +{ + /* Default: no initialization needed for tick-based implementation */ + return; +} diff --git a/components/drivers/ktime/src/hrtimer.c b/components/drivers/clock_time/src/hrtimer.c similarity index 66% rename from components/drivers/ktime/src/hrtimer.c rename to components/drivers/clock_time/src/hrtimer.c index ca579a5e988..7045b85158f 100644 --- a/components/drivers/ktime/src/hrtimer.c +++ b/components/drivers/clock_time/src/hrtimer.c @@ -1,24 +1,24 @@ /* - * Copyright (c) 2006-2023, RT-Thread Development Team + * 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. + * 2023-07-10 xqyjlj The first version (ktime) * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable + * 2025-01-01 RT-Thread Migrated to unified clock_time subsystem */ #include #include #include +#include -#define DBG_SECTION_NAME "drv.ktime" +#define DBG_SECTION_NAME "clock.hrtimer" #define DBG_LEVEL DBG_INFO #include -#include "ktime.h" - #ifdef ARCH_CPU_64BIT #define _HRTIMER_MAX_CNT UINT64_MAX #else @@ -28,22 +28,22 @@ static rt_list_t _timer_list = RT_LIST_OBJECT_INIT(_timer_list); static RT_DEFINE_SPINLOCK(_spinlock); -rt_inline rt_ktime_hrtimer_t _first_hrtimer(void) +rt_inline rt_clock_hrtimer_t _first_hrtimer(void) { - return rt_list_isempty(&_timer_list) ? RT_NULL : rt_list_first_entry(&_timer_list, struct rt_ktime_hrtimer, node); + return rt_list_isempty(&_timer_list) ? RT_NULL : rt_list_first_entry(&_timer_list, struct rt_clock_hrtimer, node); } -rt_weak rt_uint64_t rt_ktime_hrtimer_getres(void) +rt_weak rt_uint64_t rt_clock_hrtimer_getres(void) { - return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / RT_TICK_PER_SECOND; + return ((1000ULL * 1000 * 1000) * RT_CLOCK_TIME_RESMUL) / RT_TICK_PER_SECOND; } -rt_weak unsigned long rt_ktime_hrtimer_getfrq(void) +rt_weak unsigned long rt_clock_hrtimer_getfrq(void) { return RT_TICK_PER_SECOND; } -rt_weak rt_err_t rt_ktime_hrtimer_settimeout(unsigned long cnt) +rt_weak rt_err_t rt_clock_hrtimer_settimeout(unsigned long cnt) { static rt_timer_t timer = RT_NULL; static struct rt_timer _sh_rtimer; @@ -53,7 +53,7 @@ rt_weak rt_err_t rt_ktime_hrtimer_settimeout(unsigned long cnt) if (timer == RT_NULL) { timer = &_sh_rtimer; - rt_timer_init(timer, "shrtimer", (void (*)(void *))rt_ktime_hrtimer_process, RT_NULL, cnt, RT_TIMER_FLAG_ONE_SHOT); + rt_timer_init(timer, "shrtimer", (void (*)(void *))rt_clock_hrtimer_process, RT_NULL, cnt, RT_TIMER_FLAG_ONE_SHOT); } else { @@ -74,29 +74,54 @@ rt_weak rt_err_t rt_ktime_hrtimer_settimeout(unsigned long cnt) /** * @brief convert cnt from cputimer cnt to hrtimer cnt * - * @param cnt - * @return unsigned long + * @param cnt Target count value + * @return Converted count for hrtimer */ static unsigned long _cnt_convert(unsigned long cnt) { unsigned long rtn = 0; - unsigned long count = cnt - rt_ktime_cputimer_getcnt(); + unsigned long current_cnt = rt_clock_cputimer_getcnt(); + + /* + * Check if target count is in the past. + * For unsigned counters, if cnt <= current_cnt, it means the target + * has already passed (or is exactly now). + */ + if (cnt <= current_cnt) + { + return 0; + } + + unsigned long count = cnt - current_cnt; + + /* + * Sanity check for wrap-around detection. + * If the difference exceeds half the maximum counter value, it's likely + * a wrap-around or invalid value. This handles both: + * - Forward overflow: count is unreasonably large + * - Backward wrap: cnt was in the past but appeared larger due to overflow + */ if (count > (_HRTIMER_MAX_CNT / 2)) return 0; - rtn = (count * rt_ktime_cputimer_getres()) / rt_ktime_hrtimer_getres(); + /* Use 64-bit intermediate to prevent overflow in multiplication */ + rt_uint64_t count_64 = (rt_uint64_t)count; + rt_uint64_t res_cpu = rt_clock_cputimer_getres(); + rt_uint64_t res_hr = rt_clock_hrtimer_getres(); + + rtn = (unsigned long)((count_64 * res_cpu) / res_hr); return rtn == 0 ? 1 : rtn; /* at least 1 */ } static void _sleep_timeout(void *parameter) { - struct rt_ktime_hrtimer *timer = parameter; + struct rt_clock_hrtimer *timer = parameter; rt_completion_done(&timer->completion); } -static void _insert_timer_to_list_locked(rt_ktime_hrtimer_t timer) +static void _insert_timer_to_list_locked(rt_clock_hrtimer_t timer) { - rt_ktime_hrtimer_t iter; + rt_clock_hrtimer_t iter; rt_list_for_each_entry(iter, &_timer_list, node) { @@ -112,17 +137,17 @@ static void _insert_timer_to_list_locked(rt_ktime_hrtimer_t timer) static void _hrtimer_process_locked(void) { - rt_ktime_hrtimer_t timer; + rt_clock_hrtimer_t timer; for (timer = _first_hrtimer(); - (timer != RT_NULL) && (timer->timeout_cnt <= rt_ktime_cputimer_getcnt()); + (timer != RT_NULL) && (timer->timeout_cnt <= rt_clock_cputimer_getcnt()); timer = _first_hrtimer()) { rt_list_remove(&(timer->node)); if (timer->flag & RT_TIMER_FLAG_PERIODIC) { - timer->timeout_cnt = timer->delay_cnt + rt_ktime_cputimer_getcnt(); + timer->timeout_cnt = timer->delay_cnt + rt_clock_cputimer_getcnt(); _insert_timer_to_list_locked(timer); } else @@ -139,7 +164,7 @@ static void _hrtimer_process_locked(void) static void _set_next_timeout_locked(void) { - rt_ktime_hrtimer_t timer; + rt_clock_hrtimer_t timer; rt_ubase_t next_timeout_hrtimer_cnt; rt_bool_t find_next; @@ -151,7 +176,7 @@ static void _set_next_timeout_locked(void) next_timeout_hrtimer_cnt = _cnt_convert(timer->timeout_cnt); if (next_timeout_hrtimer_cnt > 0) { - rt_ktime_hrtimer_settimeout(next_timeout_hrtimer_cnt); + rt_clock_hrtimer_settimeout(next_timeout_hrtimer_cnt); } else { @@ -163,7 +188,7 @@ static void _set_next_timeout_locked(void) while (find_next); } -void rt_ktime_hrtimer_process(void) +void rt_clock_hrtimer_process(void) { rt_base_t level = rt_spin_lock_irqsave(&_spinlock); @@ -173,7 +198,7 @@ void rt_ktime_hrtimer_process(void) rt_spin_unlock_irqrestore(&_spinlock, level); } -void rt_ktime_hrtimer_init(rt_ktime_hrtimer_t timer, +void rt_clock_hrtimer_init(rt_clock_hrtimer_t timer, const char *name, rt_uint8_t flag, void (*timeout)(void *parameter), @@ -183,7 +208,7 @@ void rt_ktime_hrtimer_init(rt_ktime_hrtimer_t timer, RT_ASSERT(timer != RT_NULL); RT_ASSERT(timeout != RT_NULL); - rt_memset(timer, 0, sizeof(struct rt_ktime_hrtimer)); + rt_memset(timer, 0, sizeof(struct rt_clock_hrtimer)); timer->flag = flag & ~RT_TIMER_FLAG_ACTIVATED; timer->timeout_func = timeout; @@ -193,7 +218,7 @@ void rt_ktime_hrtimer_init(rt_ktime_hrtimer_t timer, rt_completion_init(&timer->completion); } -rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer, unsigned long delay_cnt) +rt_err_t rt_clock_hrtimer_start(rt_clock_hrtimer_t timer, unsigned long delay_cnt) { rt_base_t level; @@ -202,7 +227,7 @@ rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer, unsigned long delay_cn RT_ASSERT(delay_cnt < (_HRTIMER_MAX_CNT / 2)); timer->delay_cnt = delay_cnt; - timer->timeout_cnt = timer->delay_cnt + rt_ktime_cputimer_getcnt(); + timer->timeout_cnt = timer->delay_cnt + rt_clock_cputimer_getcnt(); level = rt_spin_lock_irqsave(&_spinlock); @@ -220,7 +245,7 @@ rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer, unsigned long delay_cn return RT_EOK; } -rt_err_t rt_ktime_hrtimer_stop(rt_ktime_hrtimer_t timer) +rt_err_t rt_clock_hrtimer_stop(rt_clock_hrtimer_t timer) { rt_base_t level; @@ -243,7 +268,7 @@ rt_err_t rt_ktime_hrtimer_stop(rt_ktime_hrtimer_t timer) return RT_EOK; } -rt_err_t rt_ktime_hrtimer_control(rt_ktime_hrtimer_t timer, int cmd, void *arg) +rt_err_t rt_clock_hrtimer_control(rt_clock_hrtimer_t timer, int cmd, void *arg) { rt_base_t level; @@ -261,7 +286,7 @@ rt_err_t rt_ktime_hrtimer_control(rt_ktime_hrtimer_t timer, int cmd, void *arg) case RT_TIMER_CTRL_SET_TIME: RT_ASSERT((*(unsigned long *)arg) < (_HRTIMER_MAX_CNT / 2)); timer->delay_cnt = *(unsigned long *)arg; - timer->timeout_cnt = *(unsigned long *)arg + rt_ktime_cputimer_getcnt(); + timer->timeout_cnt = *(unsigned long *)arg + rt_clock_cputimer_getcnt(); break; case RT_TIMER_CTRL_SET_ONESHOT: @@ -289,7 +314,7 @@ rt_err_t rt_ktime_hrtimer_control(rt_ktime_hrtimer_t timer, int cmd, void *arg) *(unsigned long *)arg = timer->timeout_cnt; break; case RT_TIMER_CTRL_GET_FUNC: - arg = (void *)timer->timeout_func; + *(void **)arg = (void *)timer->timeout_func; break; case RT_TIMER_CTRL_SET_FUNC: @@ -312,7 +337,7 @@ rt_err_t rt_ktime_hrtimer_control(rt_ktime_hrtimer_t timer, int cmd, void *arg) return RT_EOK; } -rt_err_t rt_ktime_hrtimer_detach(rt_ktime_hrtimer_t timer) +rt_err_t rt_clock_hrtimer_detach(rt_clock_hrtimer_t timer) { rt_base_t level; @@ -340,47 +365,47 @@ rt_err_t rt_ktime_hrtimer_detach(rt_ktime_hrtimer_t timer) /************************** delay ***************************/ -void rt_ktime_hrtimer_delay_init(struct rt_ktime_hrtimer *timer) +void rt_clock_hrtimer_delay_init(struct rt_clock_hrtimer *timer) { - rt_ktime_hrtimer_init(timer, "hrtimer_sleep", RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER, + rt_clock_hrtimer_init(timer, "hrtimer_sleep", RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_HARD_TIMER, _sleep_timeout, timer); } -void rt_ktime_hrtimer_delay_detach(struct rt_ktime_hrtimer *timer) +void rt_clock_hrtimer_delay_detach(struct rt_clock_hrtimer *timer) { - rt_ktime_hrtimer_detach(timer); + rt_clock_hrtimer_detach(timer); } -rt_err_t rt_ktime_hrtimer_sleep(struct rt_ktime_hrtimer *timer, unsigned long cnt) +rt_err_t rt_clock_hrtimer_sleep(struct rt_clock_hrtimer *timer, unsigned long cnt) { rt_err_t err; if (cnt == 0) return -RT_EINVAL; - err = rt_ktime_hrtimer_start(timer, cnt); + err = rt_clock_hrtimer_start(timer, cnt); if (err) return err; err = rt_completion_wait_flags(&(timer->completion), RT_WAITING_FOREVER, RT_INTERRUPTIBLE); - rt_ktime_hrtimer_keep_errno(timer, err); + rt_clock_hrtimer_keep_errno(timer, err); return err; } -rt_err_t rt_ktime_hrtimer_ndelay(struct rt_ktime_hrtimer *timer, unsigned long ns) +rt_err_t rt_clock_hrtimer_ndelay(struct rt_clock_hrtimer *timer, unsigned long ns) { - rt_uint64_t res = rt_ktime_cputimer_getres(); - return rt_ktime_hrtimer_sleep(timer, (ns * RT_KTIME_RESMUL) / res); + rt_uint64_t res = rt_clock_cputimer_getres(); + return rt_clock_hrtimer_sleep(timer, (ns * RT_CLOCK_TIME_RESMUL) / res); } -rt_err_t rt_ktime_hrtimer_udelay(struct rt_ktime_hrtimer *timer, unsigned long us) +rt_err_t rt_clock_hrtimer_udelay(struct rt_clock_hrtimer *timer, unsigned long us) { - return rt_ktime_hrtimer_ndelay(timer, us * 1000); + return rt_clock_hrtimer_ndelay(timer, us * 1000); } -rt_err_t rt_ktime_hrtimer_mdelay(struct rt_ktime_hrtimer *timer, unsigned long ms) +rt_err_t rt_clock_hrtimer_mdelay(struct rt_clock_hrtimer *timer, unsigned long ms) { - return rt_ktime_hrtimer_ndelay(timer, ms * 1000000); + return rt_clock_hrtimer_ndelay(timer, ms * 1000000); } diff --git a/components/drivers/cputime/Kconfig b/components/drivers/cputime/Kconfig deleted file mode 100644 index 97c2c462593..00000000000 --- a/components/drivers/cputime/Kconfig +++ /dev/null @@ -1,34 +0,0 @@ -config RT_USING_CPUTIME - bool "Enable CPU time for high resolution clock counter" - default n - help - When enable this option, the BSP should provide a rt_clock_cputime_ops - for CPU time by: - const static struct rt_clock_cputime_ops _ops = {...}; - clock_cpu_setops(&_ops); - - Then user can use high resolution clock counter with: - - ts1 = clock_cpu_gettime(); - ts2 = clock_cpu_gettime(); - - /* and get the ms of delta tick with API: */ - ms_tick = clock_cpu_millisecond(t2 - t1); - us_tick = clock_cpu_microsecond(t2 - t1); - -if RT_USING_CPUTIME - config RT_USING_CPUTIME_CORTEXM - bool "Support Cortex-M CPU" - default y - depends on ARCH_ARM_CORTEX_M0 || ARCH_ARM_CORTEX_M3 || ARCH_ARM_CORTEX_M4 || ARCH_ARM_CORTEX_M7 - select PKG_USING_PERF_COUNTER - config RT_USING_CPUTIME_RISCV - bool "Use rdtime instructions for CPU time" - default y - depends on ARCH_RISCV64 - help - Some RISCV64 MCU Use rdtime instructions read CPU time. - config CPUTIME_TIMER_FREQ - int "CPUTIME timer freq" - default 0 -endif diff --git a/components/drivers/cputime/SConscript b/components/drivers/cputime/SConscript deleted file mode 100644 index 9fec4641e54..00000000000 --- a/components/drivers/cputime/SConscript +++ /dev/null @@ -1,18 +0,0 @@ -from building import * - -cwd = GetCurrentDir() -CPPPATH = [cwd + '/../include'] -src = Split(''' -cputime.c -cputimer.c -''') - -if GetDepend('RT_USING_CPUTIME_CORTEXM'): - src += ['cputime_cortexm.c'] - -if GetDepend('RT_USING_CPUTIME_RISCV'): - src += ['cputime_riscv.c'] - -group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_CPUTIME'], CPPPATH = CPPPATH) - -Return('group') diff --git a/components/drivers/cputime/cputime.c b/components/drivers/cputime/cputime.c deleted file mode 100644 index 42298ea98c1..00000000000 --- a/components/drivers/cputime/cputime.c +++ /dev/null @@ -1,116 +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 - */ - -#include -#include -#include - -static const struct rt_clock_cputime_ops *_cputime_ops = RT_NULL; - -/** - * The clock_cpu_getres() function shall return the resolution of CPU time, the - * number of nanosecond per tick. - * - * @return the number of nanosecond per tick(x (1000UL * 1000)) - */ -uint64_t clock_cpu_getres(void) -{ - if (_cputime_ops) - return _cputime_ops->cputime_getres(); - - rt_set_errno(ENOSYS); - return 0; -} - -/** - * The clock_cpu_gettime() function shall return the current value of cpu time tick. - * - * @return the cpu tick - */ -uint64_t clock_cpu_gettime(void) -{ - if (_cputime_ops) - return _cputime_ops->cputime_gettime(); - - rt_set_errno(ENOSYS); - return 0; -} - -/** - * The clock_cpu_settimeout() fucntion set timeout time and timeout callback function - * The timeout callback function will be called when the timeout time is reached - * - * @param tick the Timeout tick - * @param timeout the Timeout function - * @param parameter the Parameters of timeout function - * - */ -int clock_cpu_settimeout(uint64_t tick, void (*timeout)(void *param), void *param) -{ - if (_cputime_ops) - return _cputime_ops->cputime_settimeout(tick, timeout, param); - - rt_set_errno(ENOSYS); - return 0; -} - -int clock_cpu_issettimeout(void) -{ - if (_cputime_ops) - return _cputime_ops->cputime_settimeout != RT_NULL; - return RT_FALSE; -} - -/** - * The clock_cpu_microsecond() fucntion shall return the microsecond according to - * cpu_tick parameter. - * - * @param cpu_tick the cpu tick - * - * @return the microsecond - */ -uint64_t clock_cpu_microsecond(uint64_t cpu_tick) -{ - uint64_t unit = clock_cpu_getres(); - - return (uint64_t)(((cpu_tick * unit) / (1000UL * 1000)) / 1000); -} - -/** - * The clock_cpu_microsecond() fucntion shall return the millisecond according to - * cpu_tick parameter. - * - * @param cpu_tick the cpu tick - * - * @return the millisecond - */ -uint64_t clock_cpu_millisecond(uint64_t cpu_tick) -{ - uint64_t unit = clock_cpu_getres(); - - return (uint64_t)(((cpu_tick * unit) / (1000UL * 1000)) / (1000UL * 1000)); -} - -/** - * The clock_cpu_seops() function shall set the ops of cpu time. - * - * @return always return 0. - */ -int clock_cpu_setops(const struct rt_clock_cputime_ops *ops) -{ - _cputime_ops = ops; - if (ops) - { - RT_ASSERT(ops->cputime_getres != RT_NULL); - RT_ASSERT(ops->cputime_gettime != RT_NULL); - } - - return 0; -} diff --git a/components/drivers/cputime/cputime_cortexm.c b/components/drivers/cputime/cputime_cortexm.c deleted file mode 100644 index 100910a9f9f..00000000000 --- a/components/drivers/cputime/cputime_cortexm.c +++ /dev/null @@ -1,69 +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 - * 2022-06-14 Meco Man suuport pref_counter - */ - -#include -#include -#include - -#include -#ifdef PKG_USING_PERF_COUNTER -#include -#endif - -/* Use Cycle counter of Data Watchpoint and Trace Register for CPU time */ -static uint64_t cortexm_cputime_getres(void) -{ - uint64_t ret = 1000UL * 1000 * 1000; - - ret = (ret * (1000UL * 1000)) / SystemCoreClock; - return ret; -} - -static uint64_t cortexm_cputime_gettime(void) -{ -#ifdef PKG_USING_PERF_COUNTER - return get_system_ticks(); -#else - return DWT->CYCCNT; -#endif -} - -const static struct rt_clock_cputime_ops _cortexm_ops = -{ - cortexm_cputime_getres, - cortexm_cputime_gettime -}; - - -int cortexm_cputime_init(void) -{ -#ifdef PKG_USING_PERF_COUNTER - clock_cpu_setops(&_cortexm_ops); -#else - /* check support bit */ - if ((DWT->CTRL & (1UL << DWT_CTRL_NOCYCCNT_Pos)) == 0) - { - /* enable trace*/ - CoreDebug->DEMCR |= (1UL << CoreDebug_DEMCR_TRCENA_Pos); - - /* whether cycle counter not enabled */ - if ((DWT->CTRL & (1UL << DWT_CTRL_CYCCNTENA_Pos)) == 0) - { - /* enable cycle counter */ - DWT->CTRL |= (1UL << DWT_CTRL_CYCCNTENA_Pos); - } - - clock_cpu_setops(&_cortexm_ops); - } -#endif /* PKG_USING_PERF_COUNTER */ - return 0; -} -INIT_BOARD_EXPORT(cortexm_cputime_init); diff --git a/components/drivers/cputime/cputime_riscv.c b/components/drivers/cputime/cputime_riscv.c deleted file mode 100644 index 597157c226e..00000000000 --- a/components/drivers/cputime/cputime_riscv.c +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include - -#include - -/* Use Cycle counter of Data Watchpoint and Trace Register for CPU time */ - -static uint64_t riscv_cputime_getres(void) -{ - uint64_t ret = 1000UL * 1000 * 1000; - - ret = (ret * (1000UL * 1000)) / CPUTIME_TIMER_FREQ; - return ret; -} - -static uint64_t riscv_cputime_gettime(void) -{ - uint64_t time_elapsed; - __asm__ __volatile__( - "rdtime %0" - : "=r"(time_elapsed)); - return time_elapsed; -} - -const static struct rt_clock_cputime_ops _riscv_ops = -{ - riscv_cputime_getres, - riscv_cputime_gettime -}; - -int riscv_cputime_init(void) -{ - clock_cpu_setops(&_riscv_ops); - return 0; -} -INIT_BOARD_EXPORT(riscv_cputime_init); diff --git a/components/drivers/cputime/cputimer.c b/components/drivers/cputime/cputimer.c deleted file mode 100644 index 04318336407..00000000000 --- a/components/drivers/cputime/cputimer.c +++ /dev/null @@ -1,339 +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 - * 2023-04-03 xqyjlj fix cputimer in multithreading - */ - -#include -#include -#include - -static rt_list_t _cputimer_list = RT_LIST_OBJECT_INIT(_cputimer_list); -static struct rt_cputimer *_cputimer_nowtimer = RT_NULL; - -static void _cputime_sleep_timeout(void *parameter) -{ - struct rt_semaphore *sem; - sem = (struct rt_semaphore *)parameter; - rt_sem_release(sem); -} - -static void _cputime_timeout_callback(void *parameter) -{ - struct rt_cputimer *timer; - timer = (struct rt_cputimer *)parameter; - rt_base_t level; - level = rt_hw_interrupt_disable(); - _cputimer_nowtimer = RT_NULL; - rt_list_remove(&(timer->row)); - rt_hw_interrupt_enable(level); - timer->timeout_func(timer->parameter); - - if (&_cputimer_list != _cputimer_list.prev) - { - struct rt_cputimer *t; - t = rt_list_entry(_cputimer_list.next, struct rt_cputimer, row); - clock_cpu_settimeout(t->timeout_tick, _cputime_timeout_callback, t); - } - else - { - clock_cpu_settimeout(RT_NULL, RT_NULL, RT_NULL); - } -} - -static void _set_next_timeout() -{ - struct rt_cputimer *t; - - if (&_cputimer_list != _cputimer_list.prev) - { - t = rt_list_entry((&_cputimer_list)->next, struct rt_cputimer, row); - if (_cputimer_nowtimer != RT_NULL) - { - if (t != _cputimer_nowtimer && t->timeout_tick < _cputimer_nowtimer->timeout_tick) - { - _cputimer_nowtimer = t; - clock_cpu_settimeout(t->timeout_tick, _cputime_timeout_callback, t); - } - } - else - { - _cputimer_nowtimer = t; - clock_cpu_settimeout(t->timeout_tick, _cputime_timeout_callback, t); - } - } - else - { - _cputimer_nowtimer = RT_NULL; - clock_cpu_settimeout(RT_NULL, RT_NULL, RT_NULL); - } -} - -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) -{ - /* parameter check */ - RT_ASSERT(timer != RT_NULL); - RT_ASSERT(timeout != RT_NULL); - RT_ASSERT(clock_cpu_issettimeout() != RT_FALSE); - - /* set flag */ - timer->parent.flag = flag; - - /* set deactivated */ - timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED; - timer->timeout_func = timeout; - timer->parameter = parameter; - timer->timeout_tick = tick + clock_cpu_gettime(); - timer->init_tick = tick; - - rt_list_init(&(timer->row)); - rt_sem_init(&(timer->sem), "cputime", 0, RT_IPC_FLAG_PRIO); -} - -rt_err_t rt_cputimer_delete(rt_cputimer_t timer) -{ - rt_base_t level; - - /* parameter check */ - RT_ASSERT(timer != RT_NULL); - RT_ASSERT(clock_cpu_issettimeout() != RT_FALSE); - - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - rt_list_remove(&timer->row); - /* stop timer */ - timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED; - - /* enable interrupt */ - rt_hw_interrupt_enable(level); - - _set_next_timeout(); - - return RT_EOK; -} - -rt_err_t rt_cputimer_start(rt_cputimer_t timer) -{ - rt_list_t *timer_list; - rt_base_t level; - - /* parameter check */ - RT_ASSERT(timer != RT_NULL); - RT_ASSERT(clock_cpu_issettimeout() != RT_FALSE); - - /* stop timer firstly */ - level = rt_hw_interrupt_disable(); - /* remove timer from list */ - - rt_list_remove(&timer->row); - /* change status of timer */ - timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED; - - timer_list = &_cputimer_list; - - for (; timer_list != _cputimer_list.prev; - timer_list = timer_list->next) - { - struct rt_cputimer *t; - rt_list_t *p = timer_list->next; - - t = rt_list_entry(p, struct rt_cputimer, row); - - if ((t->timeout_tick - timer->timeout_tick) == 0) - { - continue; - } - else if ((t->timeout_tick - timer->timeout_tick) < 0x7fffffffffffffff) - { - break; - } - } - - rt_list_insert_after(timer_list, &(timer->row)); - - timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED; - - _set_next_timeout(); - /* enable interrupt */ - rt_hw_interrupt_enable(level); - - return RT_EOK; -} - -rt_err_t rt_cputimer_stop(rt_cputimer_t timer) -{ - rt_base_t level; - - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - /* timer check */ - RT_ASSERT(timer != RT_NULL); - RT_ASSERT(clock_cpu_issettimeout() != RT_FALSE); - - if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)) - { - rt_hw_interrupt_enable(level); - return -RT_ERROR; - } - - rt_list_remove(&timer->row); - /* change status */ - timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED; - - _set_next_timeout(); - /* enable interrupt */ - rt_hw_interrupt_enable(level); - - return RT_EOK; -} - -rt_err_t rt_cputimer_control(rt_cputimer_t timer, int cmd, void *arg) -{ - rt_base_t level; - - /* parameter check */ - RT_ASSERT(timer != RT_NULL); - RT_ASSERT(clock_cpu_issettimeout() != RT_FALSE); - - level = rt_hw_interrupt_disable(); - switch (cmd) - { - case RT_TIMER_CTRL_GET_TIME: - *(rt_uint64_t *)arg = timer->init_tick; - break; - - case RT_TIMER_CTRL_SET_TIME: - RT_ASSERT((*(rt_uint64_t *)arg) < 0x7fffffffffffffff); - timer->init_tick = *(rt_uint64_t *)arg; - timer->timeout_tick = *(rt_uint64_t *)arg + clock_cpu_gettime(); - break; - - case RT_TIMER_CTRL_SET_ONESHOT: - timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC; - break; - - case RT_TIMER_CTRL_SET_PERIODIC: - timer->parent.flag |= RT_TIMER_FLAG_PERIODIC; - break; - - case RT_TIMER_CTRL_GET_STATE: - if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) - { - /*timer is start and run*/ - *(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED; - } - else - { - /*timer is stop*/ - *(rt_uint32_t *)arg = RT_TIMER_FLAG_DEACTIVATED; - } - break; - - case RT_TIMER_CTRL_GET_REMAIN_TIME: - *(rt_uint64_t *)arg = timer->timeout_tick; - break; - case RT_TIMER_CTRL_GET_FUNC: - arg = (void *)timer->timeout_func; - break; - - case RT_TIMER_CTRL_SET_FUNC: - timer->timeout_func = (void (*)(void *))arg; - break; - - case RT_TIMER_CTRL_GET_PARM: - *(void **)arg = timer->parameter; - break; - - case RT_TIMER_CTRL_SET_PARM: - timer->parameter = arg; - break; - - default: - break; - } - rt_hw_interrupt_enable(level); - - return RT_EOK; -} - -rt_err_t rt_cputimer_detach(rt_cputimer_t timer) -{ - rt_base_t level; - - /* parameter check */ - RT_ASSERT(timer != RT_NULL); - RT_ASSERT(clock_cpu_issettimeout() != RT_FALSE); - - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - rt_list_remove(&timer->row); - /* stop timer */ - timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED; - - _set_next_timeout(); - /* enable interrupt */ - rt_hw_interrupt_enable(level); - - rt_sem_detach(&(timer->sem)); - - return RT_EOK; -} - -rt_err_t rt_cputime_sleep(rt_uint64_t tick) -{ - rt_base_t level; - struct rt_cputimer cputimer; - - if (!clock_cpu_issettimeout()) - { - rt_int32_t ms = clock_cpu_millisecond(tick); - return rt_thread_delay(rt_tick_from_millisecond(ms)); - } - - if (tick == 0) - { - return -RT_EINVAL; - } - - rt_cputimer_init(&cputimer, "cputime_sleep", _cputime_sleep_timeout, &(cputimer.sem), tick, - RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER); - - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - rt_cputimer_start(&cputimer); /* reset the timeout of thread timer and start it */ - rt_hw_interrupt_enable(level); - rt_sem_take_interruptible(&(cputimer.sem), RT_WAITING_FOREVER); - - rt_cputimer_detach(&cputimer); - return RT_EOK; -} - -rt_err_t rt_cputime_ndelay(rt_uint64_t ns) -{ - uint64_t unit = clock_cpu_getres(); - return rt_cputime_sleep(ns * (1000UL * 1000) / unit); -} - -rt_err_t rt_cputime_udelay(rt_uint64_t us) -{ - return rt_cputime_ndelay(us * 1000); -} - -rt_err_t rt_cputime_mdelay(rt_uint64_t ms) -{ - return rt_cputime_ndelay(ms * 1000000); -} diff --git a/components/drivers/hwtimer/Kconfig b/components/drivers/hwtimer/Kconfig deleted file mode 100644 index 0fd1974b899..00000000000 --- a/components/drivers/hwtimer/Kconfig +++ /dev/null @@ -1,14 +0,0 @@ -menuconfig RT_USING_HWTIMER - bool "Using Hardware Timer device drivers" - default n - -config RT_HWTIMER_ARM_ARCH - bool "ARM ARCH Timer" - depends on RT_USING_DM - depends on RT_USING_HWTIMER - depends on ARCH_ARM_CORTEX_A || ARCH_ARMV8 - default n - -if RT_USING_DM && RT_USING_HWTIMER - osource "$(SOC_DM_HWTIMER_DIR)/Kconfig" -endif diff --git a/components/drivers/hwtimer/SConscript b/components/drivers/hwtimer/SConscript deleted file mode 100644 index b6ffc580ed2..00000000000 --- a/components/drivers/hwtimer/SConscript +++ /dev/null @@ -1,18 +0,0 @@ -from building import * - -group = [] - -if not GetDepend(['RT_USING_HWTIMER']): - Return('group') - -cwd = GetCurrentDir() -CPPPATH = [cwd + '/../include'] - -src = ['hwtimer.c'] - -if GetDepend(['RT_HWTIMER_ARM_ARCH']): - src += ['hwtimer-arm_arch.c'] - -group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH) - -Return('group') diff --git a/components/drivers/hwtimer/hwtimer-arm_arch.c b/components/drivers/hwtimer/hwtimer-arm_arch.c deleted file mode 100644 index 5d6b0eafcfa..00000000000 --- a/components/drivers/hwtimer/hwtimer-arm_arch.c +++ /dev/null @@ -1,387 +0,0 @@ -/* - * Copyright (c) 2006-2022, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2021-12-20 GuEe-GUI first version - * 2022-08-24 GuEe-GUI Add OFW support - */ - -#include -#include -#include - -/* support registers access and timer registers in libcpu */ -#include -#include - -typedef void (*timer_ctrl_handle)(rt_bool_t enable); -typedef rt_uint64_t (*timer_value_handle)(rt_uint64_t val); - -static volatile rt_uint64_t timer_step; - -static int arm_arch_timer_irq = -1; -static timer_ctrl_handle arm_arch_timer_ctrl_handle = RT_NULL; -static timer_value_handle arm_arch_timer_value_handle = RT_NULL; - -/* CTL */ -static void mon_ptimer_ctrl(rt_bool_t enable) -{ - rt_hw_sysreg_write(CNTPS_CTL, !!enable); -} - -static void hyp_s_ptimer_ctrl(rt_bool_t enable) -{ -#if ARCH_ARMV8_EXTENSIONS > 1 - rt_hw_sysreg_write(CNTHPS_CTL, !!enable); -#endif -} - -static void hyp_ns_ptimer_ctrl(rt_bool_t enable) -{ - rt_hw_sysreg_write(CNTHP_CTL, !!enable); -} - -static void hyp_s_vtimer_ctrl(rt_bool_t enable) -{ -#if ARCH_ARMV8_EXTENSIONS > 1 - rt_hw_sysreg_write(CNTHVS_CTL, !!enable); -#endif -} - -static void hyp_ns_vtimer_ctrl(rt_bool_t enable) -{ -#if ARCH_ARMV8_EXTENSIONS > 1 - rt_hw_sysreg_write(CNTHV_CTL, !!enable); -#endif -} - -static void os_ptimer_ctrl(rt_bool_t enable) -{ - rt_hw_sysreg_write(CNTP_CTL, !!enable); -} - -static void os_vtimer_ctrl(rt_bool_t enable) -{ - rt_hw_sysreg_write(CNTV_CTL, !!enable); -} - -/* TVAL */ -static rt_uint64_t mon_ptimer_value(rt_uint64_t val) -{ - if (val) - { - rt_hw_sysreg_write(CNTPS_TVAL, val); - } - else - { - rt_hw_sysreg_read(CNTPS_TVAL, val); - } - - return val; -} - -static rt_uint64_t hyp_s_ptimer_value(rt_uint64_t val) -{ -#if ARCH_ARMV8_EXTENSIONS > 1 - if (val) - { - rt_hw_sysreg_write(CNTHPS_TVAL, val); - } - else - { - rt_hw_sysreg_read(CNTHPS_TVAL, val); - } - - return val; -#else - return 0; -#endif -} - -static rt_uint64_t hyp_ns_ptimer_value(rt_uint64_t val) -{ - if (val) - { - rt_hw_sysreg_write(CNTHP_TVAL, val); - } - else - { - rt_hw_sysreg_read(CNTHP_TVAL, val); - } - - return val; -} - -static rt_uint64_t hyp_s_vtimer_value(rt_uint64_t val) -{ -#if ARCH_ARMV8_EXTENSIONS > 1 - if (val) - { - rt_hw_sysreg_write(CNTHVS_TVAL, val); - } - else - { - rt_hw_sysreg_read(CNTHVS_TVAL, val); - } - - return val; -#else - return 0; -#endif -} - -static rt_uint64_t hyp_ns_vtimer_value(rt_uint64_t val) -{ -#if ARCH_ARMV8_EXTENSIONS > 1 - if (val) - { - rt_hw_sysreg_write(CNTHV_TVAL, val); - } - else - { - rt_hw_sysreg_read(CNTHV_TVAL, val); - } - - return val; -#else - return 0; -#endif -} - -static rt_uint64_t os_ptimer_value(rt_uint64_t val) -{ - if (val) - { - rt_hw_sysreg_write(CNTP_TVAL, val); - } - else - { - rt_hw_sysreg_read(CNTP_TVAL, val); - } - - return val; -} - -static rt_uint64_t os_vtimer_value(rt_uint64_t val) -{ - if (val) - { - rt_hw_sysreg_write(CNTV_TVAL, val); - } - else - { - rt_hw_sysreg_read(CNTV_TVAL, val); - } - - return val; -} - -static timer_ctrl_handle ctrl_handle[] = -{ - mon_ptimer_ctrl, - hyp_s_ptimer_ctrl, - hyp_ns_ptimer_ctrl, - hyp_s_vtimer_ctrl, - hyp_ns_vtimer_ctrl, - os_ptimer_ctrl, - os_vtimer_ctrl, -}; - -static timer_value_handle value_handle[] = -{ - mon_ptimer_value, - hyp_s_ptimer_value, - hyp_ns_ptimer_value, - hyp_s_vtimer_value, - hyp_ns_vtimer_value, - os_ptimer_value, - os_vtimer_value, -}; - -rt_err_t arm_arch_timer_local_enable(void) -{ - rt_err_t ret = RT_EOK; - - if (arm_arch_timer_irq >= 0) - { - arm_arch_timer_ctrl_handle(RT_FALSE); - arm_arch_timer_value_handle(timer_step); - - rt_hw_interrupt_umask(arm_arch_timer_irq); - - arm_arch_timer_ctrl_handle(RT_TRUE); - } - else - { - ret = -RT_ENOSYS; - } - - return ret; -} - -rt_err_t arm_arch_timer_local_disable(void) -{ - rt_err_t ret = RT_EOK; - - if (arm_arch_timer_ctrl_handle) - { - arm_arch_timer_ctrl_handle(RT_FALSE); - rt_hw_interrupt_mask(arm_arch_timer_irq); - } - else - { - ret = -RT_ENOSYS; - } - - return ret; -} - -rt_err_t arm_arch_timer_set_frequency(rt_uint64_t frq) -{ - rt_err_t ret = RT_EOK; - -#ifdef ARCH_SUPPORT_TEE - rt_hw_isb(); - rt_hw_sysreg_write(CNTFRQ, frq); - rt_hw_dsb(); -#else - ret = -RT_ENOSYS; -#endif - - return ret; -} - -rt_uint64_t arm_arch_timer_get_frequency(void) -{ - rt_uint64_t frq; - - rt_hw_isb(); - rt_hw_sysreg_read(CNTFRQ, frq); - rt_hw_isb(); - - return frq; -} - -rt_err_t arm_arch_timer_set_value(rt_uint64_t val) -{ - rt_err_t ret = RT_EOK; - - if (arm_arch_timer_value_handle) - { - val = arm_arch_timer_value_handle(val); - } - else - { - ret = -RT_ENOSYS; - } - - return ret; -} - -rt_uint64_t arm_arch_timer_get_value(void) -{ - rt_uint64_t val = 0; - - if (arm_arch_timer_value_handle) - { - val = arm_arch_timer_value_handle(0); - } - - return val; -} - -rt_uint64_t arm_arch_timer_get_count(void) -{ - rt_uint64_t cntpct; - - rt_hw_sysreg_read(CNTPCT, cntpct); - - return cntpct; -} - -static void arm_arch_timer_isr(int vector, void *param) -{ - arm_arch_timer_set_value(timer_step); - - rt_tick_increase(); -} - -static int arm_arch_timer_post_init(void) -{ - arm_arch_timer_local_enable(); - - return 0; -} -INIT_SECONDARY_CPU_EXPORT(arm_arch_timer_post_init); - -static rt_err_t arm_arch_timer_probe(struct rt_platform_device *pdev) -{ - int mode_idx, irq_idx; - const char *irq_name[] = - { - "phys", /* Secure Phys IRQ */ - "virt", /* Non-secure Phys IRQ */ - "hyp-phys", /* Virt IRQ */ - "hyp-virt", /* Hyp IRQ */ - }; - -#if defined(ARCH_SUPPORT_TEE) - mode_idx = 0; - irq_idx = 0; -#elif defined(ARCH_SUPPORT_HYP) - mode_idx = 2; - irq_idx = 3; -#else - mode_idx = 5; - irq_idx = 1; -#endif - - arm_arch_timer_irq = rt_dm_dev_get_irq_by_name(&pdev->parent, irq_name[irq_idx]); - - if (arm_arch_timer_irq < 0) - { - arm_arch_timer_irq = rt_dm_dev_get_irq(&pdev->parent, irq_idx); - } - - if (arm_arch_timer_irq < 0) - { - return -RT_EEMPTY; - } - - arm_arch_timer_ctrl_handle = ctrl_handle[mode_idx]; - arm_arch_timer_value_handle = value_handle[mode_idx]; - - rt_hw_interrupt_install(arm_arch_timer_irq, arm_arch_timer_isr, RT_NULL, "tick-arm-timer"); - - timer_step = arm_arch_timer_get_frequency() / RT_TICK_PER_SECOND; - - arm_arch_timer_local_enable(); - - return RT_EOK; -} - -static const struct rt_ofw_node_id arm_arch_timer_ofw_ids[] = -{ - { .compatible = "arm,armv7-timer", }, - { .compatible = "arm,armv8-timer", }, - { /* sentinel */ } -}; - -static struct rt_platform_driver arm_arch_timer_driver = -{ - .name = "arm-arch-timer", - .ids = arm_arch_timer_ofw_ids, - - .probe = arm_arch_timer_probe, -}; - -static int arm_arch_timer_drv_register(void) -{ - rt_platform_driver_register(&arm_arch_timer_driver); - - return 0; -} -INIT_SUBSYS_EXPORT(arm_arch_timer_drv_register); diff --git a/components/drivers/hwtimer/hwtimer.c b/components/drivers/hwtimer/hwtimer.c deleted file mode 100644 index 1b2792558d1..00000000000 --- a/components/drivers/hwtimer/hwtimer.c +++ /dev/null @@ -1,417 +0,0 @@ -/* - * Copyright (c) 2006-2024 RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2015-08-31 heyuanjie87 first version - */ - -#include -#include - -#define DBG_TAG "hwtimer" -#define DBG_LVL DBG_INFO -#include - -#ifdef RT_USING_DM -void (*rt_device_hwtimer_us_delay)(rt_uint32_t us) = RT_NULL; - -void rt_hw_us_delay(rt_uint32_t us) -{ - if (rt_device_hwtimer_us_delay) - { - rt_device_hwtimer_us_delay(us); - } - else - { - LOG_E("Implemented at least in the libcpu"); - - RT_ASSERT(0); - } -} -#endif /* RT_USING_DM */ - -rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv) -{ - float overflow; - float timeout; - rt_uint32_t counter; - int i, index = 0; - float tv_sec; - float devi_min = 1; - float devi; - - /* changed to second */ - overflow = timer->info->maxcnt/(float)timer->freq; - tv_sec = tv->sec + tv->usec/(float)1000000; - - if (tv_sec < (1/(float)timer->freq)) - { - /* little timeout */ - i = 0; - timeout = 1/(float)timer->freq; - } - else - { - for (i = 1; i > 0; i ++) - { - timeout = tv_sec/i; - - if (timeout <= overflow) - { - counter = (rt_uint32_t)(timeout * timer->freq); - devi = tv_sec - (counter / (float)timer->freq) * i; - /* Minimum calculation error */ - if (devi > devi_min) - { - i = index; - timeout = tv_sec/i; - break; - } - else if (devi == 0) - { - break; - } - else if (devi < devi_min) - { - devi_min = devi; - index = i; - } - } - } - } - - timer->cycles = i; - timer->reload = i; - timer->period_sec = timeout; - counter = (rt_uint32_t)(timeout * timer->freq); - - return counter; -} - -static rt_err_t rt_hwtimer_init(struct rt_device *dev) -{ - rt_err_t result = RT_EOK; - rt_hwtimer_t *timer; - - timer = (rt_hwtimer_t *)dev; - /* try to change to 1MHz */ - if ((1000000 <= timer->info->maxfreq) && (1000000 >= timer->info->minfreq)) - { - timer->freq = 1000000; - } - else - { - timer->freq = timer->info->minfreq; - } - timer->mode = HWTIMER_MODE_ONESHOT; - timer->cycles = 0; - timer->overflow = 0; - - if (timer->ops->init) - { - timer->ops->init(timer, 1); - } - else - { - result = -RT_ENOSYS; - } - - return result; -} - -static rt_err_t rt_hwtimer_open(struct rt_device *dev, rt_uint16_t oflag) -{ - rt_err_t result = RT_EOK; - rt_hwtimer_t *timer; - - timer = (rt_hwtimer_t *)dev; - if (timer->ops->control != RT_NULL) - { - timer->ops->control(timer, HWTIMER_CTRL_FREQ_SET, &timer->freq); - } - else - { - result = -RT_ENOSYS; - } - - return result; -} - -static rt_err_t rt_hwtimer_close(struct rt_device *dev) -{ - rt_err_t result = RT_EOK; - rt_hwtimer_t *timer; - - timer = (rt_hwtimer_t*)dev; - if (timer->ops->init != RT_NULL) - { - timer->ops->init(timer, 0); - } - else - { - result = -RT_ENOSYS; - } - - dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED; - dev->rx_indicate = RT_NULL; - - return result; -} - -static rt_ssize_t rt_hwtimer_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size) -{ - rt_hwtimer_t *timer; - rt_hwtimerval_t tv; - rt_uint32_t cnt; - rt_base_t level; - rt_int32_t overflow; - float t; - - timer = (rt_hwtimer_t *)dev; - if (timer->ops->count_get == RT_NULL) - return 0; - - level = rt_hw_interrupt_disable(); - cnt = timer->ops->count_get(timer); - overflow = timer->overflow; - rt_hw_interrupt_enable(level); - - if (timer->info->cntmode == HWTIMER_CNTMODE_DW) - { - cnt = (rt_uint32_t)(timer->freq * timer->period_sec) - cnt; - } - if (timer->mode == HWTIMER_MODE_ONESHOT) - { - overflow = 0; - } - - t = overflow * timer->period_sec + cnt/(float)timer->freq; - tv.sec = (rt_int32_t)t; - tv.usec = (rt_int32_t)((t - tv.sec) * 1000000); - size = size > sizeof(tv)? sizeof(tv) : size; - rt_memcpy(buffer, &tv, size); - - return size; -} - -static rt_ssize_t rt_hwtimer_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size) -{ - rt_base_t level; - rt_uint32_t t; - rt_hwtimer_mode_t opm = HWTIMER_MODE_PERIOD; - rt_hwtimer_t *timer; - - timer = (rt_hwtimer_t *)dev; - if ((timer->ops->start == RT_NULL) || (timer->ops->stop == RT_NULL)) - return 0; - - if (size != sizeof(rt_hwtimerval_t)) - return 0; - - timer->ops->stop(timer); - - level = rt_hw_interrupt_disable(); - timer->overflow = 0; - rt_hw_interrupt_enable(level); - - t = timeout_calc(timer, (rt_hwtimerval_t*)buffer); - if ((timer->cycles <= 1) && (timer->mode == HWTIMER_MODE_ONESHOT)) - { - opm = HWTIMER_MODE_ONESHOT; - } - - if (timer->ops->start(timer, t, opm) != RT_EOK) - size = 0; - - return size; -} - -static rt_err_t rt_hwtimer_control(struct rt_device *dev, int cmd, void *args) -{ - rt_base_t level; - rt_err_t result = RT_EOK; - rt_hwtimer_t *timer; - - timer = (rt_hwtimer_t *)dev; - - switch (cmd) - { - case HWTIMER_CTRL_STOP: - { - if (timer->ops->stop != RT_NULL) - { - timer->ops->stop(timer); - } - else - { - result = -RT_ENOSYS; - } - } - break; - case HWTIMER_CTRL_FREQ_SET: - { - rt_int32_t *f; - - if (args == RT_NULL) - { - result = -RT_EEMPTY; - break; - } - - f = (rt_int32_t*)args; - if ((*f > timer->info->maxfreq) || (*f < timer->info->minfreq)) - { - LOG_W("frequency setting out of range! It will maintain at %d Hz", timer->freq); - result = -RT_EINVAL; - break; - } - - if (timer->ops->control != RT_NULL) - { - result = timer->ops->control(timer, cmd, args); - if (result == RT_EOK) - { - level = rt_hw_interrupt_disable(); - timer->freq = *f; - rt_hw_interrupt_enable(level); - } - } - else - { - result = -RT_ENOSYS; - } - } - break; - case HWTIMER_CTRL_INFO_GET: - { - if (args == RT_NULL) - { - result = -RT_EEMPTY; - break; - } - - *((struct rt_hwtimer_info*)args) = *timer->info; - } - break; - case HWTIMER_CTRL_MODE_SET: - { - rt_hwtimer_mode_t *m; - - if (args == RT_NULL) - { - result = -RT_EEMPTY; - break; - } - - m = (rt_hwtimer_mode_t*)args; - - if ((*m != HWTIMER_MODE_ONESHOT) && (*m != HWTIMER_MODE_PERIOD)) - { - result = -RT_ERROR; - break; - } - level = rt_hw_interrupt_disable(); - timer->mode = *m; - rt_hw_interrupt_enable(level); - } - break; - default: - { - if (timer->ops->control != RT_NULL) - { - result = timer->ops->control(timer, cmd, args); - } - else - { - result = -RT_ENOSYS; - } - } - break; - } - - return result; -} - -void rt_device_hwtimer_isr(rt_hwtimer_t *timer) -{ - rt_base_t level; - - RT_ASSERT(timer != RT_NULL); - - level = rt_hw_interrupt_disable(); - - timer->overflow ++; - - if (timer->cycles != 0) - { - timer->cycles --; - } - - if (timer->cycles == 0) - { - timer->cycles = timer->reload; - - rt_hw_interrupt_enable(level); - - if (timer->mode == HWTIMER_MODE_ONESHOT) - { - if (timer->ops->stop != RT_NULL) - { - timer->ops->stop(timer); - } - } - - if (timer->parent.rx_indicate != RT_NULL) - { - timer->parent.rx_indicate(&timer->parent, sizeof(struct rt_hwtimerval)); - } - } - else - { - rt_hw_interrupt_enable(level); - } -} - -#ifdef RT_USING_DEVICE_OPS -const static struct rt_device_ops hwtimer_ops = -{ - rt_hwtimer_init, - rt_hwtimer_open, - rt_hwtimer_close, - rt_hwtimer_read, - rt_hwtimer_write, - rt_hwtimer_control -}; -#endif - -rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data) -{ - struct rt_device *device; - - RT_ASSERT(timer != RT_NULL); - RT_ASSERT(timer->ops != RT_NULL); - RT_ASSERT(timer->info != RT_NULL); - - device = &(timer->parent); - - device->type = RT_Device_Class_Timer; - device->rx_indicate = RT_NULL; - device->tx_complete = RT_NULL; - -#ifdef RT_USING_DEVICE_OPS - device->ops = &hwtimer_ops; -#else - device->init = rt_hwtimer_init; - device->open = rt_hwtimer_open; - device->close = rt_hwtimer_close; - device->read = rt_hwtimer_read; - device->write = rt_hwtimer_write; - device->control = rt_hwtimer_control; -#endif - device->user_data = user_data; - - return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); -} diff --git a/components/drivers/include/drivers/clock_time.h b/components/drivers/include/drivers/clock_time.h new file mode 100644 index 00000000000..69c288a4c95 --- /dev/null +++ b/components/drivers/include/drivers/clock_time.h @@ -0,0 +1,353 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-01-01 RT-Thread Unified clock_time subsystem (replaces hwtimer/ktime/cputime) + */ + +#ifndef __CLOCK_TIME_H__ +#define __CLOCK_TIME_H__ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Resolution multiplier for time calculations */ +#define RT_CLOCK_TIME_RESMUL (1000000ULL) + +/* Clock time device capabilities */ +#define RT_CLOCK_TIME_CAP_CLOCKSOURCE (1 << 0) /* Device provides time source */ +#define RT_CLOCK_TIME_CAP_CLOCKEVENT (1 << 1) /* Device provides event generation */ + +/** + * @brief Clock time device operations structure + * + * This structure defines the hardware interface for clock/timer devices. + * BSPs should implement these operations for their specific hardware. + */ +struct rt_clock_time_ops +{ + /** + * Get the counting frequency in Hz + * @return Frequency in Hz + */ + rt_uint64_t (*get_freq)(void); + + /** + * Get the current free-running counter value + * @return Current counter value + */ + rt_uint64_t (*get_counter)(void); + + /** + * Set a timeout relative to current counter value + * @param delta Timeout in counter ticks (0 to cancel) + * @return RT_EOK on success, error code otherwise + */ + rt_err_t (*set_timeout)(rt_uint64_t delta); +}; + +/** + * @brief Clock time device structure + * + * Unified device abstraction for time sources and event generators. + * This replaces the separate hwtimer, ktime, and cputime devices. + */ +struct rt_clock_time_device +{ + struct rt_device parent; /* Standard device interface */ + const struct rt_clock_time_ops *ops; /* Hardware operations */ + rt_uint64_t res_scale; /* Resolution scale factor */ + rt_uint8_t caps; /* Device capabilities (RT_CLOCK_TIME_CAP_*) */ +}; +typedef struct rt_clock_time_device *rt_clock_time_t; + +/** + * @brief High-resolution timer structure + * + * Software timer built on top of clock_time device. + * Compatible with rt_ktime_hrtimer interface. + */ +struct rt_clock_hrtimer +{ + rt_uint8_t flag; /* Timer flags (compatible with rt_timer) */ + char name[RT_NAME_MAX]; /* Timer name */ + rt_list_t node; /* List node for timer management */ + void *parameter; /* User parameter */ + unsigned long delay_cnt; /* Delay count */ + unsigned long timeout_cnt; /* Absolute timeout count */ + rt_err_t error; /* Last error code */ + struct rt_completion completion; /* For synchronous waiting */ + void (*timeout_func)(void *parameter); /* Timeout callback */ +}; +typedef struct rt_clock_hrtimer *rt_clock_hrtimer_t; + +/* + * ============================================================================ + * Device Management APIs + * ============================================================================ + */ + +/** + * @brief Register a clock time device + * + * @param dev Clock time device to register + * @param name Device name (e.g., "cputimer", "hwtimer0") + * @param caps Device capabilities (RT_CLOCK_TIME_CAP_*) + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_time_device_register(struct rt_clock_time_device *dev, + const char *name, + rt_uint8_t caps); + +/** + * @brief Get the default system clock time device + * @return Pointer to default device, or RT_NULL if none + */ +rt_clock_time_t rt_clock_time_default(void); + +/** + * @brief Set the default system clock time device + * @param dev Device to set as default + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_time_set_default(rt_clock_time_t dev); + +/* + * ============================================================================ + * Clock Source APIs (Boottime) + * ============================================================================ + */ + +/** + * @brief Get boottime with microsecond precision + * @param tv Output timeval structure + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_boottime_get_us(struct timeval *tv); + +/** + * @brief Get boottime with second precision + * @param t Output time_t value + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_boottime_get_s(time_t *t); + +/** + * @brief Get boottime with nanosecond precision + * @param ts Output timespec structure + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_boottime_get_ns(struct timespec *ts); + +/* + * ============================================================================ + * CPU Timer APIs (Clock Source) + * ============================================================================ + */ + +/** + * @brief Get CPU timer resolution (resolution * RT_CLOCK_TIME_RESMUL) + * @return Resolution value + */ +rt_uint64_t rt_clock_cputimer_getres(void); + +/** + * @brief Get CPU timer frequency in Hz + * @return Frequency in Hz + */ +unsigned long rt_clock_cputimer_getfrq(void); + +/** + * @brief Get current CPU timer counter value + * @return Counter value + */ +unsigned long rt_clock_cputimer_getcnt(void); + +/** + * @brief Initialize CPU timer subsystem + */ +void rt_clock_cputimer_init(void); + +/* + * ============================================================================ + * High-Resolution Timer APIs + * ============================================================================ + */ + +/** + * @brief Get hrtimer resolution (resolution * RT_CLOCK_TIME_RESMUL) + * @return Resolution value + */ +rt_uint64_t rt_clock_hrtimer_getres(void); + +/** + * @brief Get hrtimer frequency in Hz + * @return Frequency in Hz + */ +unsigned long rt_clock_hrtimer_getfrq(void); + +/** + * @brief Set hrtimer interrupt timeout (BSP should implement) + * @param cnt Timeout in counter ticks + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_hrtimer_settimeout(unsigned long cnt); + +/** + * @brief Process expired hrtimers (call from ISR) + */ +void rt_clock_hrtimer_process(void); + +/** + * @brief Initialize a high-resolution timer + * + * @param timer Timer structure to initialize + * @param name Timer name + * @param flag Timer flags (RT_TIMER_FLAG_*) + * @param timeout Timeout callback function + * @param parameter User parameter for callback + */ +void rt_clock_hrtimer_init(rt_clock_hrtimer_t timer, + const char *name, + rt_uint8_t flag, + void (*timeout)(void *parameter), + void *parameter); + +/** + * @brief Start a high-resolution timer + * @param timer Timer to start + * @param cnt Timeout in counter ticks + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_hrtimer_start(rt_clock_hrtimer_t timer, unsigned long cnt); + +/** + * @brief Stop a high-resolution timer + * @param timer Timer to stop + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_hrtimer_stop(rt_clock_hrtimer_t timer); + +/** + * @brief Control a high-resolution timer + * @param timer Timer to control + * @param cmd Control command + * @param arg Command argument + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_hrtimer_control(rt_clock_hrtimer_t timer, int cmd, void *arg); + +/** + * @brief Detach a high-resolution timer + * @param timer Timer to detach + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_hrtimer_detach(rt_clock_hrtimer_t timer); + +/** + * @brief Keep errno in timer structure + * @param timer Timer structure + * @param err Error code to keep + * + * Note: This function negates err when setting errno to convert RT-Thread + * error codes to POSIX-style errno values. This maintains compatibility + * with the original ktime implementation. + */ +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); +} + +/** + * @brief Initialize timer for delay operations + * @param timer Timer structure + */ +void rt_clock_hrtimer_delay_init(struct rt_clock_hrtimer *timer); + +/** + * @brief Detach timer after delay operations + * @param timer Timer structure + */ +void rt_clock_hrtimer_delay_detach(struct rt_clock_hrtimer *timer); + +/** + * @brief Sleep for specified counter ticks + * @param timer Timer structure to use + * @param cnt Number of counter ticks to sleep + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_hrtimer_sleep(struct rt_clock_hrtimer *timer, unsigned long cnt); + +/** + * @brief Delay for specified nanoseconds + * @param timer Timer structure to use + * @param ns Nanoseconds to delay + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_hrtimer_ndelay(struct rt_clock_hrtimer *timer, unsigned long ns); + +/** + * @brief Delay for specified microseconds + * @param timer Timer structure to use + * @param us Microseconds to delay + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_hrtimer_udelay(struct rt_clock_hrtimer *timer, unsigned long us); + +/** + * @brief Delay for specified milliseconds + * @param timer Timer structure to use + * @param ms Milliseconds to delay + * @return RT_EOK on success, error code otherwise + */ +rt_err_t rt_clock_hrtimer_mdelay(struct rt_clock_hrtimer *timer, unsigned long ms); + +/* + * ============================================================================ + * Legacy Compatibility Macros (Will be removed in future versions) + * ============================================================================ + */ + +/* Compatibility with rt_ktime_* APIs */ +#define RT_KTIME_RESMUL RT_CLOCK_TIME_RESMUL +#define rt_ktime_hrtimer rt_clock_hrtimer +#define rt_ktime_hrtimer_t rt_clock_hrtimer_t +#define rt_ktime_boottime_get_us rt_clock_boottime_get_us +#define rt_ktime_boottime_get_s rt_clock_boottime_get_s +#define rt_ktime_boottime_get_ns rt_clock_boottime_get_ns +#define rt_ktime_cputimer_getres rt_clock_cputimer_getres +#define rt_ktime_cputimer_getfrq rt_clock_cputimer_getfrq +#define rt_ktime_cputimer_getcnt rt_clock_cputimer_getcnt +#define rt_ktime_cputimer_init rt_clock_cputimer_init +#define rt_ktime_hrtimer_getres rt_clock_hrtimer_getres +#define rt_ktime_hrtimer_getfrq rt_clock_hrtimer_getfrq +#define rt_ktime_hrtimer_settimeout rt_clock_hrtimer_settimeout +#define rt_ktime_hrtimer_process rt_clock_hrtimer_process +#define rt_ktime_hrtimer_init rt_clock_hrtimer_init +#define rt_ktime_hrtimer_start rt_clock_hrtimer_start +#define rt_ktime_hrtimer_stop rt_clock_hrtimer_stop +#define rt_ktime_hrtimer_control rt_clock_hrtimer_control +#define rt_ktime_hrtimer_detach rt_clock_hrtimer_detach +#define rt_ktime_hrtimer_keep_errno rt_clock_hrtimer_keep_errno +#define rt_ktime_hrtimer_delay_init rt_clock_hrtimer_delay_init +#define rt_ktime_hrtimer_delay_detach rt_clock_hrtimer_delay_detach +#define rt_ktime_hrtimer_sleep rt_clock_hrtimer_sleep +#define rt_ktime_hrtimer_ndelay rt_clock_hrtimer_ndelay +#define rt_ktime_hrtimer_udelay rt_clock_hrtimer_udelay +#define rt_ktime_hrtimer_mdelay rt_clock_hrtimer_mdelay + +#ifdef __cplusplus +} +#endif + +#endif /* __CLOCK_TIME_H__ */ diff --git a/components/drivers/include/drivers/cputime.h b/components/drivers/include/drivers/cputime.h index 478ccfd0199..dd5e958d9b3 100644 --- a/components/drivers/include/drivers/cputime.h +++ b/components/drivers/include/drivers/cputime.h @@ -1,38 +1,111 @@ /* - * Copyright (c) 2006-2023, RT-Thread Development Team + * Copyright (c) 2006-2025, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: - * Date Author Notes - * 2017-12-23 Bernard first version + * Date Author Notes + * 2025-01-01 RT-Thread Compatibility layer for legacy cputime API + * + * COMPATIBILITY HEADER: + * This header provides backward compatibility for code using the old cputime API. + * The old cputime subsystem has been removed and replaced with the unified + * clock_time subsystem. */ -#ifndef CPUTIME_H__ -#define CPUTIME_H__ +#ifndef __DRIVERS_CPUTIME_H__ +#define __DRIVERS_CPUTIME_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef RT_USING_CLOCK_TIME +/* When clock_time is enabled, use the new APIs */ +#include + +/* Map old cputime APIs to new clock_time APIs */ +#define clock_cpu_getres() rt_clock_cputimer_getres() +#define clock_cpu_gettime() rt_clock_cputimer_getcnt() +#define clock_cpu_microsecond(tick) ((tick) * 1000000ULL / rt_clock_cputimer_getfrq()) +#define clock_cpu_millisecond(tick) ((tick) * 1000ULL / rt_clock_cputimer_getfrq()) + +/* Delay functions for BSP compatibility */ +rt_inline void clock_cpu_delay_us(rt_uint32_t us) +{ + rt_uint64_t start = rt_clock_cputimer_getcnt(); + rt_uint64_t freq = rt_clock_cputimer_getfrq(); + rt_uint64_t delta = (rt_uint64_t)us * freq / 1000000ULL; + while ((rt_clock_cputimer_getcnt() - start) < delta); +} + +rt_inline void clock_cpu_delay_ms(rt_uint32_t ms) +{ + rt_uint64_t start = rt_clock_cputimer_getcnt(); + rt_uint64_t freq = rt_clock_cputimer_getfrq(); + rt_uint64_t delta = (rt_uint64_t)ms * freq / 1000ULL; + while ((rt_clock_cputimer_getcnt() - start) < delta); +} + +/* Stub for riscv_cputime_init - now handled by clock_time */ +rt_inline int riscv_cputime_init(void) +{ + /* Initialization is now handled by clock_time subsystem */ + return 0; +} -#include -#include "cputimer.h" +#else +/* When clock_time is not enabled, provide stub implementations */ -struct rt_clock_cputime_ops +/* These are stub implementations for backward compatibility */ +rt_inline rt_uint64_t clock_cpu_getres(void) { - uint64_t (*cputime_getres)(void); - uint64_t (*cputime_gettime)(void); - int (*cputime_settimeout)(uint64_t tick, void (*timeout)(void *param), void *param); -}; + return ((1000ULL * 1000 * 1000) * 1000000ULL) / RT_TICK_PER_SECOND; +} -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); +rt_inline rt_uint64_t clock_cpu_gettime(void) +{ + return rt_tick_get(); +} -uint64_t clock_cpu_microsecond(uint64_t cpu_tick); -uint64_t clock_cpu_millisecond(uint64_t cpu_tick); +rt_inline rt_uint64_t clock_cpu_microsecond(rt_uint64_t cpu_tick) +{ + return (cpu_tick * 1000000ULL) / RT_TICK_PER_SECOND; +} -int clock_cpu_setops(const struct rt_clock_cputime_ops *ops); +rt_inline rt_uint64_t clock_cpu_millisecond(rt_uint64_t cpu_tick) +{ + return (cpu_tick * 1000ULL) / RT_TICK_PER_SECOND; +} -#ifdef RT_USING_CPUTIME_RISCV -int riscv_cputime_init(void); -#endif /* RT_USING_CPUTIME_RISCV */ +/* Tick-based delay functions */ +rt_inline void clock_cpu_delay_us(rt_uint32_t us) +{ + rt_uint32_t start = rt_tick_get(); + rt_uint32_t delta = (us * RT_TICK_PER_SECOND + 999999) / 1000000; + if (delta == 0) delta = 1; + while ((rt_tick_get() - start) < delta); +} +rt_inline void clock_cpu_delay_ms(rt_uint32_t ms) +{ + rt_uint32_t start = rt_tick_get(); + rt_uint32_t delta = (ms * RT_TICK_PER_SECOND + 999) / 1000; + if (delta == 0) delta = 1; + while ((rt_tick_get() - start) < delta); +} + +rt_inline int riscv_cputime_init(void) +{ + return 0; +} + +#endif /* RT_USING_CLOCK_TIME */ + +#ifdef __cplusplus +} #endif + +#endif /* __DRIVERS_CPUTIME_H__ */ diff --git a/components/drivers/include/drivers/cputimer.h b/components/drivers/include/drivers/cputimer.h deleted file mode 100644 index 371992a41e1..00000000000 --- 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 - -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 6f11ff2c545..00000000000 --- 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 - -#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/ktime.h b/components/drivers/include/ktime.h new file mode 100644 index 00000000000..91e79832b83 --- /dev/null +++ b/components/drivers/include/ktime.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2006-2025, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-01-01 RT-Thread Compatibility layer for legacy ktime API + * + * COMPATIBILITY HEADER: + * This header provides backward compatibility for code using the old ktime API. + * All rt_ktime_* APIs are now redirected to rt_clock_* APIs. + * + * The old ktime subsystem has been removed and replaced with the unified + * clock_time subsystem. Include for new code. + */ + +#ifndef __KTIME_H__ +#define __KTIME_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef RT_USING_CLOCK_TIME +/* Include the unified clock_time header which provides all APIs */ +#include + +/* All rt_ktime_* APIs are already defined as macros in clock_time.h */ + +#else +/* When clock_time is not enabled, provide stub implementations for backward compatibility */ + +/* These are minimal stub implementations to maintain compilation compatibility */ +rt_inline rt_err_t rt_ktime_boottime_get_ns(struct timespec *ts) +{ + rt_uint64_t tick = rt_tick_get(); + rt_uint64_t ns = tick * (1000000000ULL / RT_TICK_PER_SECOND); + ts->tv_sec = ns / 1000000000ULL; + ts->tv_nsec = ns % 1000000000ULL; + return RT_EOK; +} + +rt_inline rt_err_t rt_ktime_boottime_get_us(struct timeval *tv) +{ + rt_uint64_t tick = rt_tick_get(); + rt_uint64_t us = tick * (1000000ULL / RT_TICK_PER_SECOND); + tv->tv_sec = us / 1000000ULL; + tv->tv_usec = us % 1000000ULL; + return RT_EOK; +} + +rt_inline void rt_ktime_cputimer_init(void) +{ + /* Stub implementation */ +} + +#endif /* RT_USING_CLOCK_TIME */ + +#ifdef __cplusplus +} +#endif + +#endif /* __KTIME_H__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index 50a97beee54..c3a1277449c 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 */ diff --git a/components/drivers/ktime/Kconfig b/components/drivers/ktime/Kconfig deleted file mode 100644 index 170271c222c..00000000000 --- a/components/drivers/ktime/Kconfig +++ /dev/null @@ -1,3 +0,0 @@ -menuconfig RT_USING_KTIME - bool "Ktime: kernel time" - default n diff --git a/components/drivers/ktime/README.md b/components/drivers/ktime/README.md deleted file mode 100644 index b878f93d4c9..00000000000 --- a/components/drivers/ktime/README.md +++ /dev/null @@ -1,63 +0,0 @@ -# ktime - -## 1、介绍 - -ktime 为 kernel time,为内核时间子系统,实现了内核启动时间以及芯片内核 cputimer 时间管理以及一个 ns 精度的高精度定时器, - -## 2、如何打开 ktime - -使用 ktime 需要在 RT-Thread 的 menuconfig 中选择它,具体路径如下: - -``` -RT-Thread Components - [*] Ktime: kernel time -``` - -## 3、使用 ktime - -> 函数的功能以及参数类型已经写在头文件的注释之中,本文不再赘述 - -### 3.1、boottime - -boottime 为系统启动时间,即为系统从上电开始到现在运行的时间,默认的时间基准为芯片内核的 cputimer 的 cnt 值,已经适配了 aarch64 与 riscv64 平台,例如 stm32 等平台需要在自己的 bsp 里面进行适配(boottime 里面函数都为 weak function),需要注意 tick 从中断到设置中间的时延 - -**此值应当为 Readonly** - -### 3.2、cputimer - -cputimer 为芯片内核的 cputimer,也可以认为是 os tick 来源的那个定时器,cputimer 主要是提供了一个统一的接口去获得其分辨率,频率,cnt 值 - -**此值应当为 Readonly** - -### 3.3、hrtimer - -> TODO: hrtimer 目前还是使用优先级链表的方式进行管理,在遇到任务的大规模并发时还是存在部分性能问题,待内核有一个统一的红黑树组件后,再进行优化 - -hrtimer 为高精度定时器,需要重写其 weak 函数(需要对接到硬件定时器,否则默认走的是软件定时器,分辨率只有 os tick 的值)才能正常使用,其主要使用方法: - -#### 3.3.1、延时 - -hrtimer 的延时并不是 while(1)式死等,它会将一个线程挂起,睡眠多少时间后通过硬件定时器将其唤醒(注:延时 ns 并不是真的能准确的延时这么多,而是在保证性能的情况下尽可能的延时) - -- rt_ktime_hrtimer_sleep:单位为 cputimer 的 tick 值 -- rt_ktime_hrtimer_ndelay:单位为 ns -- rt_ktime_hrtimer_udelay:单位为 us -- rt_ktime_hrtimer_mdelay:单位为 ms - -#### 3.3.1、定时器 - -hrtimer 还提供了一套 rt_timer 风格的 api - -- rt_ktime_hrtimer_init -- rt_ktime_hrtimer_delete -- rt_ktime_hrtimer_start -- rt_ktime_hrtimer_stop -- rt_ktime_hrtimer_control -- rt_ktime_hrtimer_detach - -需要注意,此定时器回调函数依旧处于中断之中,不能做一些耗时的任务 - -## 5、联系方式 - -- 维护:xqyjlj -- 主页:https://github.com/xqyjlj diff --git a/components/drivers/ktime/SConscript b/components/drivers/ktime/SConscript deleted file mode 100644 index 20a02957191..00000000000 --- a/components/drivers/ktime/SConscript +++ /dev/null @@ -1,24 +0,0 @@ -import os -from building import * - -Import('rtconfig') - -cwd = GetCurrentDir() - -src = Glob('src/*.c') -list = os.listdir(cwd + "/src") -if rtconfig.ARCH in list: - if os.path.exists(cwd + "/src/" + rtconfig.ARCH + "/" + rtconfig.CPU): - src += Glob("src/" + rtconfig.ARCH + "/" + rtconfig.CPU + "/*.c") - else: - src += Glob("src/" + rtconfig.ARCH + "/*.c") -CPPPATH = [cwd, cwd + "/inc"] -LOCAL_CCFLAGS = '' -if rtconfig.PLATFORM in ['gcc', 'armclang']: - LOCAL_CCFLAGS += ' -std=gnu99' -elif rtconfig.PLATFORM in ['armcc']: - LOCAL_CCFLAGS += ' --c99 --gnu' - -group = DefineGroup('DeviceDrivers', src, depend=['RT_USING_KTIME'], CPPPATH=CPPPATH, LOCAL_CCFLAGS = LOCAL_CCFLAGS) - -Return('group') diff --git a/components/drivers/ktime/inc/ktime.h b/components/drivers/ktime/inc/ktime.h deleted file mode 100644 index a430f85a175..00000000000 --- a/components/drivers/ktime/inc/ktime.h +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (c) 2006-2023, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-07-10 xqyjlj The first version. - * 2024-04-26 Shell Improve ipc performance - */ - -#ifndef __KTIME_H__ -#define __KTIME_H__ - -#include -#include -#include - -#include "rtthread.h" - -#define RT_KTIME_RESMUL (1000000ULL) - -struct rt_ktime_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_ktime_hrtimer *rt_ktime_hrtimer_t; - -/** - * @brief Get boottime with us precision - * - * @param tv: timeval - * @return rt_err_t - */ -rt_err_t rt_ktime_boottime_get_us(struct timeval *tv); - -/** - * @brief Get boottime with s precision - * - * @param t: time_t - * @return rt_err_t - */ -rt_err_t rt_ktime_boottime_get_s(time_t *t); - -/** - * @brief Get boottime with ns precision - * - * @param ts: timespec - * @return rt_err_t - */ -rt_err_t rt_ktime_boottime_get_ns(struct timespec *ts); - -/** - * @brief Get cputimer resolution - * - * @return (resolution * RT_KTIME_RESMUL) - */ -rt_uint64_t rt_ktime_cputimer_getres(void); - -/** - * @brief Get cputimer frequency - * - * @return frequency - */ -unsigned long rt_ktime_cputimer_getfrq(void); - -/** - * @brief Get cputimer the value of the cnt counter - * - * @return cnt - */ -unsigned long rt_ktime_cputimer_getcnt(void); - -/** - * @brief Init cputimer - * - */ -void rt_ktime_cputimer_init(void); - -/** - * @brief Get hrtimer resolution - * - * @return (resolution * RT_KTIME_RESMUL) - */ -rt_uint64_t rt_ktime_hrtimer_getres(void); - -/** - * @brief Get hrtimer frequency - * - * @return frequency - */ -unsigned long rt_ktime_hrtimer_getfrq(void); - -/** - * @brief set hrtimer interrupt timeout count (cnt), you should re-implemented it in hrtimer device driver - * - * @param cnt: hrtimer requires a timing cnt value - * @return rt_err_t - */ -rt_err_t rt_ktime_hrtimer_settimeout(unsigned long cnt); - -/** - * @brief called in hrtimer device driver isr routinue, it will process the timeouts - */ -void rt_ktime_hrtimer_process(void); - -void rt_ktime_hrtimer_init(rt_ktime_hrtimer_t timer, - const char *name, - rt_uint8_t flag, - void (*timeout)(void *parameter), - void *parameter); -rt_err_t rt_ktime_hrtimer_start(rt_ktime_hrtimer_t timer, unsigned long cnt); -rt_err_t rt_ktime_hrtimer_stop(rt_ktime_hrtimer_t timer); -rt_err_t rt_ktime_hrtimer_control(rt_ktime_hrtimer_t timer, int cmd, void *arg); -rt_err_t rt_ktime_hrtimer_detach(rt_ktime_hrtimer_t timer); - -rt_inline void rt_ktime_hrtimer_keep_errno(rt_ktime_hrtimer_t timer, rt_err_t err) -{ - RT_ASSERT(timer != RT_NULL); - - timer->error = err; - rt_set_errno(-err); -} - -void rt_ktime_hrtimer_delay_init(struct rt_ktime_hrtimer *timer); -void rt_ktime_hrtimer_delay_detach(struct rt_ktime_hrtimer *timer); -void rt_ktime_hrtimer_process(void); - -/** - * @brief sleep by the cputimer cnt value - * - * @param cnt: the cputimer cnt value - * @return rt_err_t - */ -rt_err_t rt_ktime_hrtimer_sleep(struct rt_ktime_hrtimer *timer, unsigned long cnt); - -/** - * @brief sleep by ns - * - * @param ns: ns - * @return rt_err_t - */ -rt_err_t rt_ktime_hrtimer_ndelay(struct rt_ktime_hrtimer *timer, unsigned long ns); - -/** - * @brief sleep by us - * - * @param us: us - * @return rt_err_t - */ -rt_err_t rt_ktime_hrtimer_udelay(struct rt_ktime_hrtimer *timer, unsigned long us); - -/** - * @brief sleep by ms - * - * @param ms: ms - * @return rt_err_t - */ -rt_err_t rt_ktime_hrtimer_mdelay(struct rt_ktime_hrtimer *timer, unsigned long ms); - -#endif diff --git a/components/drivers/ktime/src/aarch64/cputimer.c b/components/drivers/ktime/src/aarch64/cputimer.c deleted file mode 100644 index 005848eccdc..00000000000 --- a/components/drivers/ktime/src/aarch64/cputimer.c +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2006-2023, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-07-10 xqyjlj The first version. - */ - -#include "gtimer.h" -#include "ktime.h" - -static volatile unsigned long _init_cnt = 0; - -rt_uint64_t rt_ktime_cputimer_getres(void) -{ - return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / rt_hw_get_gtimer_frq(); -} - -unsigned long rt_ktime_cputimer_getfrq(void) -{ - return rt_hw_get_gtimer_frq(); -} - -unsigned long rt_ktime_cputimer_getcnt(void) -{ - return rt_hw_get_cntpct_val() - _init_cnt; -} - -void rt_ktime_cputimer_init(void) -{ - _init_cnt = rt_hw_get_cntpct_val(); -} diff --git a/components/drivers/ktime/src/boottime.c b/components/drivers/ktime/src/boottime.c deleted file mode 100644 index 8e69141b68d..00000000000 --- a/components/drivers/ktime/src/boottime.c +++ /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-07-10 xqyjlj The first version. - */ - -#include "ktime.h" - -#define __KTIME_MUL ((1000ULL * 1000 * 1000) / RT_TICK_PER_SECOND) - -rt_weak rt_err_t rt_ktime_boottime_get_us(struct timeval *tv) -{ - RT_ASSERT(tv != RT_NULL); - - rt_uint64_t ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL; - - tv->tv_sec = ns / (1000ULL * 1000 * 1000); - tv->tv_usec = (ns % (1000ULL * 1000 * 1000)) / 1000; - - return RT_EOK; -} - -rt_weak rt_err_t rt_ktime_boottime_get_s(time_t *t) -{ - RT_ASSERT(t != RT_NULL); - - rt_uint64_t ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL; - - *t = ns / (1000ULL * 1000 * 1000); - - return RT_EOK; -} - -rt_weak rt_err_t rt_ktime_boottime_get_ns(struct timespec *ts) -{ - RT_ASSERT(ts != RT_NULL); - - rt_uint64_t ns = (rt_ktime_cputimer_getcnt() * rt_ktime_cputimer_getres()) / RT_KTIME_RESMUL; - - ts->tv_sec = ns / (1000ULL * 1000 * 1000); - ts->tv_nsec = ns % (1000ULL * 1000 * 1000); - - return RT_EOK; -} diff --git a/components/drivers/ktime/src/cputimer.c b/components/drivers/ktime/src/cputimer.c deleted file mode 100644 index ee19b236dc6..00000000000 --- a/components/drivers/ktime/src/cputimer.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2006-2023, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-07-10 xqyjlj The first version. - */ - -#include "ktime.h" - -rt_weak rt_uint64_t rt_ktime_cputimer_getres(void) -{ - return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / RT_TICK_PER_SECOND; -} - -rt_weak unsigned long rt_ktime_cputimer_getfrq(void) -{ - return RT_TICK_PER_SECOND; -} - -rt_weak unsigned long rt_ktime_cputimer_getcnt(void) -{ - return rt_tick_get(); -} - -rt_weak void rt_ktime_cputimer_init(void) -{ - return; -} diff --git a/components/drivers/ktime/src/risc-v/virt64/cputimer.c b/components/drivers/ktime/src/risc-v/virt64/cputimer.c deleted file mode 100644 index 70c133aa2e7..00000000000 --- a/components/drivers/ktime/src/risc-v/virt64/cputimer.c +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2006-2023, RT-Thread Development Team - * - * SPDX-License-Identifier: Apache-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-07-10 xqyjlj The first version. - */ - -#include "ktime.h" - -static volatile unsigned long _init_cnt = 0; - -rt_uint64_t rt_ktime_cputimer_getres(void) -{ - return ((1000ULL * 1000 * 1000) * RT_KTIME_RESMUL) / CPUTIME_TIMER_FREQ; -} - -unsigned long rt_ktime_cputimer_getfrq(void) -{ - return CPUTIME_TIMER_FREQ; -} - -unsigned long rt_ktime_cputimer_getcnt(void) -{ - unsigned long time_elapsed; - __asm__ __volatile__("rdtime %0" : "=r"(time_elapsed)); - return time_elapsed - _init_cnt; -} - -void rt_ktime_cputimer_init(void) -{ - __asm__ __volatile__("rdtime %0" : "=r"(_init_cnt)); -}