linux驱动笔记(四):内核定时器——基于4.X内核


int jiffies_to_msecs(const unsigned long j) 将 jiffies 类型的参数 j 分别转换为对
int jiffies_to_usecs(const unsigned long j) 应的毫秒、 微秒、 纳秒。
u64 jiffies_to_nsecs(const unsigned long j)

将毫秒、 微秒、 纳秒转换为 jiffies 类型。
long msecs_to_jiffies(const unsigned int m)
long usecs_to_jiffies(const unsigned int u)
unsigned long nsecs_to_jiffies(u64 n)

void init_timer(struct timer_list *timer) 初始化定时器
void add_timer(struct timer_list *timer) 向 Linux 内核注册定时器
int del_timer(struct timer_list * timer) 删除一个定时器
int del_timer_sync(struct timer_list
*timer)
等待其他处理器使用完定时器再删除
int mod_timer(struct timer_list *timer,
unsigned long expires)
修改定时值, 如果定时器还没有激活的
话, mod_timer 函数会激活定时器

模板
1 struct timer_list timer; /* 定义定时器 /
2 3
/
定时器回调函数 /
4 void function(unsigned long arg)
5 {
6 /

7 * 定时器处理代码
8 /
9 1
0 /
如果需要定时器周期性运行的话就使用 mod_timer
11 * 函数重新设置超时值并且启动定时器。
12 /
13 mod_timer(&dev->timertest, jiffies + msecs_to_jiffies(2000));
14 }北京迅为电子有限公司 i.MX6ULL 终结者-开发板使用手册
日期:2020-03-17 1072 www.topeetboard.com
15
16 /
初始化函数 /
17 void init(void)
18 {
19 init_timer(&timer); /
初始化定时器 /
20
21 timer.function = function; /
设置定时处理函数 /
22 timer.expires=jffies + msecs_to_jiffies(2000);/
超时时间 2 秒 /
23 timer.data = (unsigned long)&dev; /
将设备结构体作为参数 /
24
25 add_timer(&timer); /
启动定时器 /
26 }
27
28 /
退出函数 /
29 void exit(void)
30 {
31 del_timer(&timer); /
删除定时器 /
32 /
或者使用 */
33 del_timer_sync(&timer);
34 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小薛1988

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值