低分辨率定時(shí)器是用jiffies來(lái)定時(shí)的,所以會(huì)受到HZ影響,如果HZ為200,代表每秒種產(chǎn)生200次中斷,那一個(gè)jiffies就需要5毫秒,所以精度為5毫秒。
如果精度需要達(dá)到納秒級(jí)別,則需要使用高精度定時(shí)器hrtimer。
使用示例
單次定時(shí)
加載驅(qū)動(dòng)一秒后輸出“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)定時(shí)
循環(huán)定時(shí)可以在回調(diào)函數(shù)中調(diào)用hrtimer_forward_now()重新設(shè)置定時(shí)時(shí)間,然后將返回值設(shè)置為HRTIMER_RESTART代表重啟定時(shí)器,就可以做到循環(huán)定時(shí)的效果。
每隔一秒輸出“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));//重新設(shè)置定時(shí)時(shí)間
return HRTIMER_RESTART;//重啟定時(shí)器
}
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)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀(guān)點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。
舉報(bào)投訴
-
Linux
+關(guān)注
關(guān)注
88文章
11763瀏覽量
219090 -
定時(shí)器
+關(guān)注
關(guān)注
23文章
3368瀏覽量
123711 -
函數(shù)
+關(guān)注
關(guān)注
3文章
4417瀏覽量
67545
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
熱點(diǎn)推薦
#硬聲創(chuàng)作季 #Linux 學(xué)Linux-2.18.1 高精度延時(shí)實(shí)驗(yàn)-GPT定時(shí)器原理-2
高精度Linux
水管工
發(fā)布于 :2022年11月10日 18:08:37
GPT高精度延時(shí)定時(shí)器簡(jiǎn)介
Linux NXP (I.MX6ULL) GPT高精度延時(shí)定時(shí)器0、GPT 定時(shí)器簡(jiǎn)介1、GPT 定時(shí)器特性如下:2、GPT
發(fā)表于 01-12 06:46
長(zhǎng)時(shí)間高精度定時(shí)器
長(zhǎng)時(shí)間高精度定時(shí)器:某些場(chǎng)合需要長(zhǎng)時(shí)間高精度的定時(shí)器,此圖定時(shí)電路定時(shí)時(shí)間較長(zhǎng),
發(fā)表于 12-14 08:05
?1273次閱讀
Linux時(shí)間子系統(tǒng)中的高精度定時(shí)器(HRTIMER)的原理和實(shí)現(xiàn)
雖然大部分時(shí)間里,時(shí)間輪可以實(shí)現(xiàn)O(1)時(shí)間復(fù)雜度,但是當(dāng)有進(jìn)位發(fā)生時(shí),不可預(yù)測(cè)的O(N)定時(shí)器級(jí)聯(lián)遷移時(shí)間,這對(duì)于低分辨率定時(shí)器來(lái)說(shuō)問(wèn)題不大,可是它大大地影響了定時(shí)器的精度;
發(fā)表于 05-10 14:11
?8170次閱讀
LINUX內(nèi)核定時(shí)器(高精度&低精度)
linux從內(nèi)核2.6.16開(kāi)始引入了高精度定時(shí)器,達(dá)到ns級(jí)別。自此,內(nèi)核擁有兩套并行計(jì)時(shí)器,低精度和
發(fā)表于 05-13 09:41
?4685次閱讀
詳解高精度定時(shí)器與高級(jí)控制定時(shí)器
在高精度定時(shí)器中,可以使用外部事件來(lái)對(duì) PWM 輸出進(jìn)行封鎖,并可自動(dòng)恢復(fù);在高級(jí)控制定時(shí)器中,可以使用 Break 或是 Clr_input 來(lái)對(duì) PWM 輸出進(jìn)行封鎖, 然后也可以自動(dòng)恢復(fù),其中 Break 必須結(jié)合 AOE
Linux驅(qū)動(dòng)開(kāi)發(fā)高精度定時(shí)器的精度測(cè)量評(píng)測(cè)
前言 今天我們來(lái)評(píng)測(cè)linux內(nèi)核的高精度定時(shí)器。順便利用通過(guò)Tektronix示波器 和 DS100 Mini 數(shù)字示波器進(jìn)行交叉測(cè)試。 因項(xiàng)目需要用到精準(zhǔn)的時(shí)間周期,所以要評(píng)估它的可行性,并驗(yàn)證
工程師筆記|高精度定時(shí)器的同步功能
關(guān)鍵詞:高精度定時(shí)器, 同步 目錄預(yù)覽 1.引言 2.定時(shí)器同步結(jié)構(gòu) 3.高精度定時(shí)器內(nèi)部同步 4.高精
高精度定時(shí)器與高級(jí)控制定時(shí)器 PWM 封波后再恢復(fù)的區(qū)別
高精度定時(shí)器與高級(jí)控制定時(shí)器 PWM 封波后再恢復(fù)的區(qū)別
Linux驅(qū)動(dòng)高精度定時(shí)器hrtimer
高分辨率定時(shí)器( hrtimer )以 ktime_t 來(lái)定義時(shí)間, 精度可以達(dá)到納秒級(jí)別 , ktime_t 定義如下: typedef s64 ktime_t ; 可以用 ktime_set 來(lái)
Linux驅(qū)動(dòng)定時(shí)器使用示例
定時(shí)器使用示例 使用步驟: 1、調(diào)用 init_timer 初始化一個(gè)定時(shí)器,給 struct timer_list 各成員賦值。 2、調(diào)用 add_timer 將定時(shí)器添加到內(nèi)核
LAT1173高精度定時(shí)器的同步功能應(yīng)用筆記
STM32G474 所含的高精度定時(shí)器(HRTIMER)其實(shí)包含了多個(gè)定時(shí)器,多個(gè)定時(shí)器之間可以單獨(dú)工作,也可以進(jìn)行同步,且
發(fā)表于 01-11 17:32
?0次下載
Linux高精度定時(shí)器hrtimer使用示例
評(píng)論