简介
Hrtimer,一种高精度时间计时器,主要实现技术:红黑树;其实现依赖于timekeeper(时间维护者)和clock_event_device(定时器引擎);hrtimer系统需要通过timekeeper获取当前的时间,计算与到期时间的差值,并根据该差值,设定该cpu的tick_device(clock_event_device)的下一次的到期时间,时间一到,在clock_event_device的事件回调函数中处理到期的hrtimer。
关键知识:Linux时间子系统
参考文件
kernel/include/soc/qcom/event_timer.h
kernel/drivers/soc/qcom/event_timer.c
kernel/include/linux/hrtimer.h
kernel/include/linux/timerqueue.h
函数使用介绍
重要的结构体
struct **event_timer_info** {
struct timerqueue_node node; //timerqueue节点,用来跟踪按照时间排序的事件定时器结构体
void (*function)(void *); //时间定时器的回调函数,
void *data; //给回调函数提供的数据
};
**struct hrtimer** {
struct timerqueue_node node; //
ktime_t _softexpires;
enum hrtimer_restart (*function)(struct hrtimer *);
struct hrtimer_clock_base *base;
unsigned long state;};
static struct hrtimer event_hrtimer; //一种高精度定时器,
使用方法
1, struct event_timer_info add_event_timer(void (*function)(void ), void *data)
@function 事件定时器超时时执行的回调函数
@data 用户提供的回调函数的操作数据
函数作用:添加一个唤醒事件,只会被用户调用一次,返回一个用来处理交互的一个操作句柄
2, static bool is_event_next(struct event_timer_info *event)
@event:被检查的事件event
函数作用:辅助函数用以检查此事件是否是下一个即将到期的事件
3, void activate_event_timer(struct event_timer_info *event, ktime_t event_time)
@event:到期操作函数
@event_time:绝对时间
函数作用:设置的到期事件为绝对时间,一旦设置就一定会执行,用户需要再调用用来设置另一个事件的到期事件
4 void timerqueue_init(struct timerqueue_node *node)
代码案例
LCD驱动实例
代码路径
kernel/drivers/video/msm/mdss/mdss_mdp_overlay.c
//1,添加事件计时器 add_event_timer
int mdss_mdp_overlay_init(struct msm_fb_data_type *mfd)
{
……..
mdp5_data->cpu_pm_hdl = add_event_timer(NULL, (void *)mdp5_data); //添加事件计时器初始化
……..
}
//2,设置计时器触发时间相关
static void mdss_mdp_overlay_update_pm(struct mdss_overlay_private *mdp5_data)
{
ktime_t wakeup_time;
if (!mdp5_data->cpu_pm_hdl)
return;
if (mdss_mdp_display_wakeup_time(mdp5_data->ctl, &wakeup_time)) //初始化时间
return;
activate_event_timer(mdp5_data->cpu_pm_hdl, wakeup_time); //给计时器设置触发时间,绝对时间
}