定時(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)注
3文章
1416瀏覽量
41405 -
驅(qū)動(dòng)
+關(guān)注
關(guān)注
12文章
1916瀏覽量
86889 -
Linux
+關(guān)注
關(guān)注
87文章
11509瀏覽量
213680 -
定時(shí)器
+關(guān)注
關(guān)注
23文章
3298瀏覽量
118813
發(fā)布評(píng)論請(qǐng)先 登錄
Linux驅(qū)動(dòng)開發(fā)-內(nèi)核定時(shí)器
Linux內(nèi)核定時(shí)器的相關(guān)資料分享
定時(shí)器、PWM、ICP范例(AVR 定時(shí)器使用范例)
STM32F1通用定時(shí)器示例詳解--TIM15_Compleme
如何利用單片機(jī)看門狗定時(shí)器使led閃爍

STM32通用定時(shí)器的單脈沖示例詳解

關(guān)于STM32定時(shí)器觸發(fā)SPI逐字收發(fā)之應(yīng)用示例
基于STM32定時(shí)器捕獲測(cè)量脈寬的應(yīng)用示例

詳細(xì)剖析Linux和RTOS(RT-Thread)的時(shí)鐘和定時(shí)器的使用

Linux內(nèi)核定時(shí)器

利用通用定時(shí)器輸出PWM(附示例驅(qū)動(dòng)直流電機(jī))

CKS32F4xx系列產(chǎn)品的定時(shí)器使用-基本特征和定時(shí)操作

評(píng)論