Linux的hrtimer例子

main.c

//似乎都是在驱动程序里使用的hrtimer
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/jiffies.h>


static struct hrtimer timer;
ktime_t kt;

static enum hrtimer_restart hrtimer_handler(struct hrtimer *timer)
{
 //kt = ktime_set(1, 10);
 printk(" ------ I am in hrtimer -----\n");
 hrtimer_forward(timer, timer->base->get_time(), kt);
 return HRTIMER_RESTART;
 }

static int __init test_init(void)
{

 pr_info("timer resolution: %lu\n", TICK_NSEC);
 kt = ktime_set(1, 10); /* 1 sec, 10 nsec */
 hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
 //hrtimer_set_expires(&timer, kt);
 hrtimer_start(&timer, kt, HRTIMER_MODE_REL);//中断触发周期为:1sec + 10 nsec
 timer.function = hrtimer_handler;

 printk("\n-------- test start ---------\n");
 return 0;
}

static void __exit test_exit(void)
{
 hrtimer_cancel(&timer);
 printk("-------- test over ----------\n");
 return;
}

MODULE_LICENSE("GPL");

module_init(test_init);
module_exit(test_exit);
Makefile:


KVERS = $(shell uname -r)
#Kernel modules
NAME = main.o
obj-m += $(NAME)

#specify flags for the module compilation.
#EXTRA_CFLAGS=-g -O0

build:kernel_modules

kernel_modules:
    make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules

clean:
    make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值