定時(shí)器使用示例
使用步驟:
1、調(diào)用init_timer初始化一個(gè)定時(shí)器,給struct timer_list各成員賦值。
2、調(diào)用add_timer將定時(shí)器添加到內(nèi)核定時(shí)器鏈表,時(shí)間到后回調(diào)函數(shù)自動(dòng)調(diào)用,用mod_timer修改expires的值可實(shí)現(xiàn)循環(huán)定時(shí)。
3、不需要定時(shí)器時(shí),調(diào)用del_timer刪除。
單次定時(shí)
加載驅(qū)動(dòng)一秒鐘后,打印出“timer handler, data:520”:
#include < linux/init.h >
#include < linux/kernel.h >
#include < linux/module.h >
#include < linux/sched.h >//jiffies在此頭文件中定義
#include < linux/timer.h >//struct timer_list
struct timer_list timer;
static void timer_handler (unsigned long arg)
{
printk("timer handler, data:%dn", arg);
}
static int __init my_init(void)
{
printk("%s entern", __func__);
init_timer(&timer);
timer.expires = get_jiffies_64() + msecs_to_jiffies(1000);//定時(shí)1秒
timer.function = timer_handler;
timer.data = 520;
add_timer(&timer);
return 0;
}
static void __exit my_exit(void)
{
printk("%s entern", __func__);
del_timer(&timer);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
循環(huán)定時(shí)
實(shí)現(xiàn)循環(huán)定時(shí)就是在定時(shí)時(shí)間到了之后, 調(diào)用mod_timer函數(shù)再次修改定時(shí)時(shí)間 。
每隔一秒鐘打印“timer handler, data:520”
#include < linux/init.h >
#include < linux/kernel.h >
#include < linux/module.h >
#include < linux/sched.h >//jiffies在此頭文件中定義
#include < linux/timer.h >//struct timer_list
struct timer_list timer;
static void timer_handler (unsigned long arg)
{
printk("timer handler, data:%dn", arg);
mod_timer(&timer, get_jiffies_64() + msecs_to_jiffies (1000));
}
static int __init my_init(void)
{
init_timer(&timer);
timer.expires = get_jiffies_64() + msecs_to_jiffies (1000);//定時(shí)1秒
timer.function = timer_handler;
timer.data = 520;
add_timer(&timer);
return 0;
}
static void __exit my_exit(void)
{
del_timer(&timer);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
-
內(nèi)核
+關(guān)注
關(guān)注
4文章
1432瀏覽量
42323 -
驅(qū)動(dòng)
+關(guān)注
關(guān)注
12文章
1925瀏覽量
87985 -
Linux
+關(guān)注
關(guān)注
88文章
11581瀏覽量
217129 -
定時(shí)器
+關(guān)注
關(guān)注
23文章
3350瀏覽量
121238
發(fā)布評論請先 登錄
Linux驅(qū)動(dòng)開發(fā)-內(nèi)核定時(shí)器
Linux內(nèi)核定時(shí)器的相關(guān)資料分享
定時(shí)器、PWM、ICP范例(AVR 定時(shí)器使用范例)
如何利用單片機(jī)看門狗定時(shí)器使led閃爍
STM32通用定時(shí)器的單脈沖示例詳解
關(guān)于STM32定時(shí)器觸發(fā)SPI逐字收發(fā)之應(yīng)用示例
基于STM32定時(shí)器捕獲測量脈寬的應(yīng)用示例
詳細(xì)剖析Linux和RTOS(RT-Thread)的時(shí)鐘和定時(shí)器的使用
Linux內(nèi)核定時(shí)器
利用通用定時(shí)器輸出PWM(附示例驅(qū)動(dòng)直流電機(jī))
CKS32F4xx系列產(chǎn)品的定時(shí)器使用-基本特征和定時(shí)操作

Linux驅(qū)動(dòng)定時(shí)器使用示例
評論