Linux驱动中断与时间篇——高精度定时器hrtimer

简介: Linux驱动中断与时间篇——高精度定时器hrtimer

前言

低分辨率定时器是用jiffies来定时的,所以会受到HZ影响,如果HZ200,代表每秒种产生200次中断,那一个jiffies就需要5毫秒,所以精度为5毫秒。

如果精度需要达到纳秒级别,则需要使用高精度定时器hrtimer

相关接口

高分辨率定时器(hrtimer)以ktime_t来定义时间,精度可以达到纳秒级别ktime_t定义如下:

typedef s64 ktime_t;

可以用ktime_set来初始化一个ktime对象,常用方法如下:

ktime_t t = ktime_set(secs, nsecs);

高分辨率hrtimer结构体定义如下:

struct hrtimer {  
    struct timerqueue_node      node;  
    ktime_t             _softexpires;  
    enum hrtimer_restart        (*function)(struct hrtimer *);  
    struct hrtimer_clock_base   *base;  
    unsigned long           state;  
        ......  
};  
enum hrtimer_restart {  
    HRTIMER_NORESTART,  /* Timer is not restarted */  
    HRTIMER_RESTART,    /* Timer must be restarted */  
};

struct hrtimer结构体中最主要的成员就是回调函数function,回调函数的返回值可以为HRTIMER_NORESTARTHRTIMER_RESTARTHRTIMER_NORESTART代表不需要重启定时器,HRTIMER_RESTART代表需要重启定时器。

最常用的接口如下:

hrtimer_init(struct hrtimer *timer, clockid_t clock_id , enum hrtimer_mode mode)
hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
hrtimer_forward_now(struct hrtimer *timer,ktime_t interval)
hrtimer_cancel(struct hrtimer *timer)

hrtimer_init:初始化 struct hrtimer 结构对象。 clockid_t 是时钟的类型, 种类很多,常见的有四种:

  • CLOCK_REALTIME:系统实时时间。
  • CLOCK_MONOTONIC:从系统启动时开始计时,自系统开机以来的单调递增时间
  • CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间,包含该进程下的所有线程。
  • CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间。

mode 是时间的模式,可以是 HRTIMER_MODE_ABS, 表示绝对时间, 也可以是 HRTIMER_MODE_REL, 表 示相对时间。hrtimer_start:启动定时器。 tim 是设定的到期时间, modehrtimer_init中的 mode参数含义相同。hrtimer_forward_now: 修改到期时间为从现在开始之后的 interval 时间。hrtimer_cancel:取消定时器。

使用示例

单次定时

加载驱动一秒后输出“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 handler\n");
    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 enter\n", __func__);
 hrtimer_cancel(&timer);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");

循环定时

循环定时可以在回调函数中调用hrtimer_forward_now()重新设置定时时间,然后将返回值设置为HRTIMER_RESTART代表重启定时器,就可以做到循环定时的效果。

每隔一秒输出“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 handler\n");
    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 enter\n", __func__);
    hrtimer_cancel(&timer);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");

end

往期推荐

入职Linux驱动工程师后,我才知道的真相......

机遇:我是如何走向Linux驱动的...

当我用几道题考了一遍做Linux驱动的同事......

“不是所有的驱动岗,都值得你去”

Linux驱动面试高频考点

相关文章
|
2月前
|
Java Linux API
Linux设备驱动开发详解2
Linux设备驱动开发详解
33 6
|
2月前
|
消息中间件 算法 Unix
Linux设备驱动开发详解1
Linux设备驱动开发详解
33 5
|
2月前
|
Ubuntu NoSQL Linux
Linux内核和驱动
Linux内核和驱动
19 2
|
2月前
|
数据采集 Linux
Linux源码阅读笔记20-PCI设备驱动详解
Linux源码阅读笔记20-PCI设备驱动详解
|
1月前
|
Linux API
Linux里的高精度时间计时器(HPET)驱动 【ChatGPT】
Linux里的高精度时间计时器(HPET)驱动 【ChatGPT】
|
2月前
|
Linux
【linux】【驱动】<specifier>-map-pass-thru讲解
【linux】【驱动】<specifier>-map-pass-thru讲解
17 0
|
2月前
|
Linux
【linux】【驱动】phy接口类型
【linux】【驱动】phy接口类型
15 0
|
5月前
|
存储 负载均衡 网络协议
X86 linux异常处理与Ipipe接管中断/异常
本文讲述了X86平台上Xenomai的ipipe如何接管中断处理。首先回顾了X86中断处理机制,包括IDT(中断描述符表)的工作原理和中断处理流程。接着详细介绍了Linux中中断门的初始化,包括门描述符的结构、中断门的定义和填充,以及IDT的加载。在异常处理部分,文章讲解了早期异常处理和start_kernel阶段的异常向量初始化。最后,讨论了APIC和SMP中断在IDT中的填充,以及剩余中断的统一处理。文章指出,ipipe通过在中断入口处插入`__ipipe_handle_irq()`函数,实现了对中断的拦截和优先处理,确保了实时性。
127 0
X86 linux异常处理与Ipipe接管中断/异常
|
存储 负载均衡 算法
Linux内核17-硬件如何处理中断和异常
Linux内核17-硬件如何处理中断和异常