我是靠谱客的博主 寂寞哑铃,最近开发中收集的这篇文章主要介绍Linux 内核定时器 timer_list hrtimer,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述



10.5内核定时器
10.5.1内核定时器编程


1.timer_list
struct timer_list my_timer;


2.初始化定时器
void init_timer(struct timer_list *timer);


3.增加定时器
void add_timer(struct timer_list *timer);


4.删除定时器
int del_timer(struct timer_list *timer);


5.修改定时器的expire
int mod_timer(struct timer_list *timer, unsigned long expires);


10.10 内核定时器使用模板
//xxx设备结构体
struct xxx_dev
{
struct cdev cdev;
.....
timer_list xxx_timer;//设备要使用的定时器
};
//xxx驱动中的某函数
xxx_funcl(...)
{
struct xxx_dev *dev = filp->private_data;
...
//初始化定时器
init_timer(&dev->xxx_timer);
dev->xxx_timer.function = &xxx_do_timer;
dev->xxx_timer.data = (unsigned long)dev;
//设备结构体指针作为定时器处理函数参数
dev->xxx_timer.expires = jiffies + delay;
//添加(注册)定时器
add_timer(&dev->xxx_timer);
}


//xxx驱动中的某函数
xxx_func2(...)
{
....
//删除定时器
del_timer(&dev->xxx_timer);
....
}


//定时器处理函数
static void xxx_do_timer(unsigned long arg)
{
struct xxx_device *dev = (struct xxx_device *)(arg);
...
//调度定时器再执行
dev->xxx_timer.expires = jiffies + delay;
add_timer(&dev->xxx_timer);
...
}




hrtimer(high resolution timer)表示高精度定时器:
==============================================================================
API
ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
void hrtimer_init_on_stack(struct hrtimer *timer, clockid_t clock_id,
  enum hrtimer_mode mode)
  
u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)   
int hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
unsigned long delta_ns, const enum hrtimer_mode mode)


int
hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
int hrtimer_try_to_cancel(struct hrtimer *timer)
int hrtimer_cancel(struct hrtimer *timer)
ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
void hrtimer_init(struct hrtimer *timer, clockid_t clock_id,
 enum hrtimer_mode mode)
int hrtimer_get_res(const clockid_t which_clock, struct timespec *tp)
int __sched schedule_hrtimeout(ktime_t *expires,
      const enum hrtimer_mode mode)






/*
 * Mode arguments of xxx_hrtimer functions:
 */
enum hrtimer_mode {
HRTIMER_MODE_ABS = 0x0, /* Time value is absolute */
HRTIMER_MODE_REL = 0x1, /* Time value is relative to now */
HRTIMER_MODE_PINNED = 0x02, /* Timer is bound to CPU */
HRTIMER_MODE_ABS_PINNED = 0x02,
HRTIMER_MODE_REL_PINNED = 0x03,
};




/*
 * Return values for the callback function
 */
enum hrtimer_restart {
HRTIMER_NORESTART, /* Timer is not restarted */
HRTIMER_RESTART, /* Timer must be restarted */
};       
       
#define HRTIMER_STATE_INACTIVE 0x00
#define HRTIMER_STATE_ENQUEUED 0x01
#define HRTIMER_STATE_CALLBACK 0x02
#define HRTIMER_STATE_MIGRATE 0x04


/* Parameters used to convert the timespec values: */
#define MSEC_PER_SEC 1000L
#define USEC_PER_MSEC 1000L
#define NSEC_PER_USEC 1000L
#define NSEC_PER_MSEC 1000000L
#define USEC_PER_SEC 1000000L
#define NSEC_PER_SEC 1000000000L
#define FSEC_PER_SEC 1000000000000000LL

hrtimer用法:


struct hrtimer acc_timer; //定义hrtimer
ktime_t acc_poll_delay; //定义ktimer_t 类型 时间变量。


//hrtimer 调用函数
static enum hrtimer_restart stk_acc_timer_func(struct hrtimer *timer)
{
struct stk831x_data *stk = container_of(timer, struct stk831x_data, acc_timer);
queue_work(stk->stk_acc_wq, &stk->stk_acc_work);
hrtimer_forward_now(&acc_timer, acc_poll_delay); //
//hrtimer_forward_now(&gw_hrtimer,ktime_set(0, 62500));//直接设置为62500ns
return HRTIMER_RESTART;
}


int __init xxx_init(void)
{
....
hrtimer_init(&acc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
acc_poll_delay = ns_to_ktime(2500 * USEC_PER_MSEC);
acc_timer.function = stk_acc_timer_func;
......
}


//启动
int xxx_probe(void)
{
hrtimer_start(&stk->acc_timer, stk->acc_poll_delay, HRTIMER_MODE_REL);
}


void xxx_suspend(void)
{
hrtimer_cancel(&acc_timer);
}


void __exit xxx_exit(void)
{
hrtimer_try_to_cancel(&acc_timer);
}

最后

以上就是寂寞哑铃为你收集整理的Linux 内核定时器 timer_list hrtimer的全部内容,希望文章能够帮你解决Linux 内核定时器 timer_list hrtimer所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(67)

评论列表共有 0 条评论

立即
投稿
返回
顶部