低分辨率定時器是用jiffies來定時的,所以會受到HZ影響,如果HZ為200,代表每秒種產(chǎn)生200次中斷,那一個jiffies就需要5毫秒,所以精度為5毫秒。
如果精度需要達到納秒級別,則需要使用高精度定時器hrtimer。
使用示例
單次定時
加載驅(qū)動一秒后輸出“hrtimer handler
”:
#include < linux/init.h >
#include < linux/kernel.h >
#include < linux/module.h >
#include < linux/ktime.h >
#include < linux/hrtimer.h >
static struct hrtimer timer;
static enum hrtimer_restart timer_handler(struct hrtimer *timer )
{
printk("hrtimer handlern");
return HRTIMER_NORESTART;
}
static int __init my_init(void)
{
ktime_t tim;
hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
timer.function = timer_handler;
tim = ktime_set(1,0); //1s
hrtimer_start(&timer,tim,HRTIMER_MODE_REL);
return 0;
}
static void __exit my_exit(void)
{
printk("%s entern", __func__);
hrtimer_cancel(&timer);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
循環(huán)定時
循環(huán)定時可以在回調(diào)函數(shù)中調(diào)用hrtimer_forward_now()
重新設置定時時間,然后將返回值設置為HRTIMER_RESTART
代表重啟定時器,就可以做到循環(huán)定時的效果。
每隔一秒輸出“hrtimer handler
”:
#include < linux/init.h >
#include < linux/kernel.h >
#include < linux/module.h >
#include < linux/ktime.h >
#include < linux/hrtimer.h >
static struct hrtimer timer;
static enum hrtimer_restart timer_handler(struct hrtimer *timer )
{
printk("hrtimer handlern");
hrtimer_forward_now(timer, ktime_set(1,0));//重新設置定時時間
return HRTIMER_RESTART;//重啟定時器
}
static int __init my_init(void)
{
ktime_t tim;
hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
timer.function = timer_handler;
tim = ktime_set(1,0); //1 s
hrtimer_start(&timer,tim,HRTIMER_MODE_REL);
return 0;
}
static void __exit my_exit(void)
{
printk("%s entern", __func__);
hrtimer_cancel(&timer);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。
舉報投訴
-
Linux
+關注
關注
87文章
11486瀏覽量
213145 -
定時器
+關注
關注
23文章
3297瀏覽量
117520 -
函數(shù)
+關注
關注
3文章
4375瀏覽量
64479
發(fā)布評論請先 登錄
相關推薦
熱點推薦

Linux時間子系統(tǒng)中的高精度定時器(HRTIMER)的原理和實現(xiàn)
雖然大部分時間里,時間輪可以實現(xiàn)O(1)時間復雜度,但是當有進位發(fā)生時,不可預測的O(N)定時器級聯(lián)遷移時間,這對于低分辨率定時器來說問題不大,可是它大大地影響了定時器的精度;
發(fā)表于 05-10 14:11
?7892次閱讀
LINUX內(nèi)核定時器(高精度&低精度)
linux從內(nèi)核2.6.16開始引入了高精度定時器,達到ns級別。自此,內(nèi)核擁有兩套并行計時器,低精度和
發(fā)表于 05-13 09:41
?4353次閱讀
詳解高精度定時器與高級控制定時器
在高精度定時器中,可以使用外部事件來對 PWM 輸出進行封鎖,并可自動恢復;在高級控制定時器中,可以使用 Break 或是 Clr_input 來對 PWM 輸出進行封鎖, 然后也可以自動恢復,其中 Break 必須結合 AOE
Linux驅(qū)動開發(fā)高精度定時器的精度測量評測
前言 今天我們來評測linux內(nèi)核的高精度定時器。順便利用通過Tektronix示波器 和 DS100 Mini 數(shù)字示波器進行交叉測試。 因項目需要用到精準的時間周期,所以要評估它的可行性,并驗證
工程師筆記|高精度定時器的同步功能
關鍵詞:高精度定時器, 同步 目錄預覽 1.引言 2.定時器同步結構 3.高精度定時器內(nèi)部同步 4.高精
Linux驅(qū)動高精度定時器hrtimer
高分辨率定時器( hrtimer )以 ktime_t 來定義時間, 精度可以達到納秒級別 , ktime_t 定義如下: typedef s64 ktime_t ; 可以用 ktime_set 來
評論