欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標(biāo)題:
學(xué)習(xí)筆記 - Linux 內(nèi)核驅(qū)動(dòng)定時(shí)器
[打印本頁(yè)]
作者:
51hei小林
時(shí)間:
2016-9-24 22:32
標(biāo)題:
學(xué)習(xí)筆記 - Linux 內(nèi)核驅(qū)動(dòng)定時(shí)器
在編寫驅(qū)動(dòng)的過(guò)程中,可能會(huì)需要定期輪詢某個(gè)硬件的狀態(tài)或情況,此時(shí)可以使用延時(shí)工作隊(duì)列或定時(shí)器,為了方便日后工作,特地去了解定時(shí)器的使用方式。
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/timer.h> // timer
#include <asm/uaccess.h> // jiffies
#define __ISDEBUG__
#ifdef __ISDEBUG__
#define dprintk(format,...) printk("[debug][%s] "format" <--\n",strrchr(__FILE__,'/') ? strrchr(__FILE__,'/') : __FILE__ ,##__VA_ARGS__)
#else
#define dprintk(format,...)
#endif
#define iprintk(format,...) printk("[info][%s] "format" <--\n",strrchr(__FILE__,'/') ? strrchr(__FILE__,'/') : __FILE__ ,##__VA_ARGS__)
volatile struct timeval time1, time2;
/*
* jiffies的相互轉(zhuǎn)換函數(shù):
* unsigned int jiffies_to_msecs(unsigned long);
* unsigned int jiffies_to_usecs(unsigned long);
* unsigned long msecs_to_jiffies(unsigned int);
* unsigned long usecs_to_jiffies(unsigned int);
*/
/*
* 定時(shí)器相關(guān)的接口
* init_timer(struct timer_list*):定時(shí)器初始化函數(shù);
* add_timer(struct timer_list*):往系統(tǒng)添加定時(shí)器;
* mod_timer(struct timer_list *, unsigned long jiffier_timerout):修改定時(shí)器的超時(shí)時(shí)間為jiffies_timerout;
* timer_pending(struct timer_list *):定時(shí)器狀態(tài)查詢,如果在系統(tǒng)的定時(shí)器列表中則返回1,否則返回0;
* del_timer(struct timer_list*):刪除定時(shí)器。
*/
/*
* 函數(shù): timer_start
* 參數(shù): msecs:毫秒
function:回調(diào)函數(shù)(void timer_function(int para)) 若為空則刪除定時(shí)器
para:傳給回調(diào)的參數(shù)
* 返回: 無(wú)
* 說(shuō)明: 這種定時(shí)器方式并不是很精確,只生效一次
* 頭文件: #include <linux/timer.h> // timer #include <asm/uaccess.h> // jiffies
*/
void timer_start(int msecs, void *function, int para)
{
static struct timer_list timer;
if(NULL == function)
{
del_timer(&timer); // 刪除定時(shí)器
return ;
}
init_timer(&timer);
timer.data = para;
// timer.expires = jiffies + ((msecs * 1000) * HZ); // 另外一種方式
timer.expires = jiffies + msecs_to_jiffies(msecs); // 毫秒
timer.function = function;
add_timer(&timer);
return ;
}
void timer_function(int para)
{
do_gettimeofday(&time2);
printk("time.s:%ld time.u:%ld\n", time2.tv_sec - time1.tv_sec, time2.tv_usec - time1.tv_usec);
printk("time1.s:%ld time1.u:%ld\ntime2.s:%ld time2.u:%ld\n\n", time1.tv_sec, time1.tv_usec, time2.tv_sec, time2.tv_usec);
do_gettimeofday(&time1);
timer_start(5000, timer_function, para++);
}
static int __init zhc_drivers_init(void)
{
iprintk("__TIME__ : %s", __TIME__);
do_gettimeofday(&time1);
timer_start(5000, timer_function, 10);
return 0;
}
static void __exit zhc_drivers_exit(void)
{
iprintk();
timer_start(0, NULL, 0);
}
mo
dule_init
(
zhc_drivers_init
);
module_exit(zhc_drivers_exit);
MODULE_LICENSE("GPL v2");
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1