linux内核之高精度定时器

低精度定时器在一些特殊场合不能达到定时要求,故引出高精度定时器;

struct hrtimer结构体

高精度定时器在内核中是以struct hrtimer结构体对象表示:

头文件: linux/krtimer.h

struct hrtimer {
	struct timerqueue_node		node;
	ktime_t				_softexpires;
	enum hrtimer_restart		(*function)(struct hrtimer *);//计时器到期回调函数
	struct hrtimer_clock_base	*base;//指向计时器基数的指针;
	u8				state; //状态信息
	u8				is_rel;//若计时器已启用,则设置;
	u8				is_soft;//在软中断上下文中设置hrtimer是否过期;
};

ktime_t

高精度定时器是以ktime_t来定义时间的;

操作函数如下:

typedef s64	ktime_t;


//将秒和纳秒转ktime_t
ktime_t ktime_set(const s64 secs, const unsigned long nsecs);

//ktime转微秒
s64 ktime_to_us(const ktime_t kt);

//ktime转毫秒
s64 ktime_to_ms(const ktime_t kt);

//纳秒转ktime
ktime_t ns_to_ktime(u64 ns);

//毫秒转ktime
ktime_t ms_to_ktime(u64 ms);

其中最核心的就是function回调函数,定时到期后自动调用,需要用户实现;

常用函数

/* 作用:初始化高精度定时器
  参数1:需要初始化的高精度定时器
  参数2:要使用的时钟,种类如下:
    #define CLOCK_REALTIME			    0
    #define CLOCK_MONOTONIC			    1
    #define CLOCK_PROCESS_CPUTIME_ID	2
    #define CLOCK_THREAD_CPUTIME_ID		3
    #define CLOCK_MONOTONIC_RAW		    4
    #define CLOCK_REALTIME_COARSE		5
    #define CLOCK_MONOTONIC_COARSE		6
    #define CLOCK_BOOTTIME			    7
    #define CLOCK_REALTIME_ALARM		8
    #define CLOCK_BOOTTIME_ALARM		9
    其中CLOCK_MONOTONIC最常用,表示自系统开机以来的单调递增时间;
  参数3:时间模式;取值如下:
    enum hrtimer_mode {
	    HRTIMER_MODE_ABS	= 0x00,//绝对时间;
	    HRTIMER_MODE_REL	= 0x01,//相对时间;
	    HRTIMER_MODE_PINNED	= 0x02,//计时器绑定到CPU,仅在启动定时器时考虑;
	    HRTIMER_MODE_SOFT	= 0x04,//计时器回调将会在软中断上下文中执行;

	    HRTIMER_MODE_ABS_PINNED = HRTIMER_MODE_ABS | HRTIMER_MODE_PINNED,
	    HRTIMER_MODE_REL_PINNED = HRTIMER_MODE_REL | HRTIMER_MODE_PINNED,

	    HRTIMER_MODE_ABS_SOFT	= HRTIMER_MODE_ABS | HRTIMER_MODE_SOFT,
	    HRTIMER_MODE_REL_SOFT	= HRTIMER_MODE_REL | HRTIMER_MODE_SOFT,

	    HRTIMER_MODE_ABS_PINNED_SOFT = HRTIMER_MODE_ABS_PINNED | HRTIMER_MODE_SOFT,
	    HRTIMER_MODE_REL_PINNED_SOFT = HRTIMER_MODE_REL_PINNED | HRTIMER_MODE_SOFT,
    };			    
*/

void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, enum hrtimer_mode mode);



/*
    作用:启动定时器
    参数1:高精度定时器;
    参数2:设置的到期时间;
    参数3:参数3:时间模式,同hrtimer_init中;
*/

void hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode);



//修改到期时间为 从现在开始的interval时间
u64 hrtimer_forward_now(struct hrtimer *timer, ktime_t interval);


//取消定时器
int hrtimer_cancel(struct hrtimer *timer);


示例:

#include 
#include 
#include 

struct hrtimer timer;

enum hrtimer_restart hrtimer_func(struct hrtimer *t)
{
	printk("----hrtimer_func----\n");

    //实现循环定时
	hrtimer_forward_now(t, ktime_set(5,1000));

	return HRTIMER_RESTART;
}


static int __init test_drv_init(void)
{		
	hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	timer.function = hrtimer_func;

	hrtimer_start(&timer, ktime_set(1,1000), HRTIMER_MODE_REL);

	printk("----test_drv_init----\n");
	
	return 0;
}


static void __exit test_drv_exit(void)
{
	hrtimer_cancel(&timer);
	
	printk("----test_drv_exit----\n");
}

module_init(test_drv_init);
module_exit(test_drv_exit);


MODULE_LICENSE("GPL");

你可能感兴趣的:(#,kernel-timer,Linux驱动,hrtimer_init,hrtimer_start,hrtimer_cancel)