设计说明
源代码位于 bsp/artinchip/
:
-
bsp/artinchip/drv/hrtimer/drv_hrtimer.c,HRTimer Driver 层实现
-
bsp/artinchip/include/drv/drv_hrtimer.h,HRTimer Driver 层接口
-
bsp/artinchip/hal/pwmcs/hal_cap.c,PWMCS CAP 模块的 HAL 层实现
-
bsp/artinchip/include/hal/hal_cap.h,PWMCS CAP 模块的 HAL 层接口头文件
模块架构
HRTimer 驱动 Driver 层采用 RT-Thread 的 hwtimer 设备驱动框架,如果只使用 HAL 层也可以支持 baremetal 方式的应用场景。

关键流程设计
初始化流程
HRTimer 驱动的初始化接口通过 INIT_DEVICE_EXPORT(drv_hwtimer_init)
完成,主要是通过调用
hwtimer 子系统的接口 rt_device_hwtimer_register() 注册一个 hwtimer 设备。
CAP 控制器的配置过程,主要步骤有:
-
初始化 PWMCS 模块的 clk
-
使能 CAP 指定通道的 clk,并设置为 PWM 计数模式
-
设置 CAP 的 cnt 值
-
使能 CAP 的中断
-
启动 CAP 计数
数据结构设计
- struct aic_cap_arg:属于 HAL
层接口,记录每一个 CAP 通道的配置信息
struct aic_cap_arg { u16 available; u16 id; u32 freq; u32 prd; u32 irq_cnt; };
- struct hrtimer_info:属于 Driver
层接口,记录一个 HRTimer 设备的配置信息:
struct hrtimer_info { char name[12]; u32 id; rt_hwtimer_t hrtimer; };
Driver 层接口设计
以下接口是 hwtimer 设备驱动框架的标准接口。
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); };
函数原型 | void drv_hrtimer_init(rt_hwtimer_t *timer, rt_uint32_t state) |
---|---|
功能说明 | 初始化配置一路 Timer |
参数定义 | timer - 指向 rt_hwtimer_t 设备的指针 state - 1,表示打开;0,表示关闭 |
返回值 | 无 |
注意事项 | - |
函数原型 | rt_err_t drv_hrtimer_start(rt_hwtimer_t *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode) |
---|---|
功能说明 | 启动 Timer |
参数定义 | timer - 指向 rt_hwtimer_t 设备的指针 Timer 的超时计数,单位是:1/Freq 秒 Oneshot、或 Period 类型 |
返回值 | 0,成功;<0,失败 |
注意事项 | - |
函数原型 | void drv_hrtimer_stop(rt_hwtimer_t *timer) |
---|---|
功能说明 | 停止 Timer |
参数定义 | timer - 指向 rt_hwtimer_t 设备的指针 |
返回值 | 无 |
注意事项 | - |
函数原型 | rt_err_t drv_hrtimer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *args) |
---|---|
功能说明 | HRTimer 驱动的 ioctl 接口 |
参数定义 | timer - 指向 rt_hwtimer_t 设备的指针 ioctl 命令码 args - ioctl 命令相应的参数 |
返回值 | 0,成功;<0,失败 |
注意事项 | 目前仅支持设置 Timer 的 Freq 值 |
HAL 层接口设计
HAL 层的函数接口声明存放在 hal_cap.h 中,主要接口有:
void hal_cap_ch_init(u32 ch); void hal_cap_ch_deinit(u32 ch); void hal_cap_int_enable(u32 ch, int enable); u32 hal_cap_is_pending(u32 ch); int hal_cap_set_freq(u32 ch, u32 freq); int hal_cap_set_cnt(u32 ch, u32 cnt); int hal_cap_get(u32 ch); int hal_cap_enable(u32 ch); int hal_cap_disable(u32 ch); void hal_cap_cnt_start(u32 ch); void hal_cap_cnt_stop(u32 ch); int hal_cap_init(void); int hal_cap_deinit(void); void hal_cap_status_show(void);
Demo
本 Demo 是 test_hrtimer 的部分源码(bsp/examples/test-hrtimer/test_hrtimer.c):