本文转载自:http://blog.csdn.net/dosculler/article/details/7932315

一、jiffies定时器,HZ=100,精度只能达到10ms。

注:采用jiffies+msecs_to_jiffies(xx ms);可做到ms级,不过精度不够

#include <Linux/jiffies.h>//DO-->jiffies调用头文件
#include <linux/timer.h>  //DO-->timer_list结构体

static struct timer_list ms_timer;//DO-->定义timer_list结构体

static void ms_timer_handler(void)//DO-->定义定时器处理函数

{
    printk("DO_DEBUG----------->%s\n",__func__);
   // ms_timer.expires=jiffies+HZ;

ms_timer.expires=jiffies+msecs_to_jiffies(10);
    ms_timer.function=&ms_timer_handler;
    add_timer(&ms_timer);

}

static int32_t xxx_init(void)
{
// hrtimer_init_module();
    init_timer(&ms_timer);                          //DO-->初始化定时器
    ms_timer.expires=jiffies+msecs_to_jiffies(10);  //DO-->定义中断时间:10ms进入中断

//ms_timer.expires=jiffies+HZ;  
    //ms_timer.data=(unsigned long)ms_timer;//区分不同定时器,未验证
    ms_timer.function=&ms_timer_handler;            //DO-->定义定时器中断处理函数

add_timer(&ms_timer);                           //DO-->增加注册定时器,使定时器生效

二、hrtimer高精度定时器,可做到ns级,此处做到毫秒如下例:

注:实际是为纳秒级,由此处ktime_set(const long secs, const unsigned long nsecs)决定的,参数下此处参数即可实现纳秒级。

#include <linux/dma-mapping.h> //DO-->hrtimer包含以下三个头文件 /* DMA APIs             */   
#include <linux/hrtimer.h>

#include <linux/time.h>           /* struct timespec    */

#define KER_PRINT(fmt, ...) printk("<ker-driver>"fmt, ##__VA_ARGS__);   
static struct hrtimer vibe_timer;

static struct work_struct vibe_work;  
static int value = 2000;   /*注:以毫秒ms为单位 Time out setting,2 seconds */

static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)  //DO-->回调函数,中断时调用 
{

struct timespec uptime;

do_posix_clock_monotonic_gettime(&uptime);  
    KER_PRINT("Time:%lu.%02lu\n",

(unsigned long) uptime.tv_sec,  
            (uptime.tv_nsec / (NSEC_PER_SEC / 1000)));

KER_PRINT("vibrator_timer_func\n");   
    schedule_work(&vibe_work);  
    return HRTIMER_NORESTART;

}  
static void vibe_work_func(struct work_struct *work)  //DO-->工作队列函数
{

KER_PRINT("'vibe_work_func'-->work\n");  
   // msleep(50); /* CPU sleep */

vibe_timer.function = vibrator_timer_func;  
    hrtimer_start(&vibe_timer,

ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);  
}

static void ker_driver_init(void)                        //DO-->hrtimer高精度定时器初始化函数
{

struct timespec uptime;

KER_PRINT("ker_driver_init\n");  
    hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);  //DO-->hrtimer定时器初始化

vibe_timer.function = vibrator_timer_func;                     //DO-->hrtimer定时器回调函数
    hrtimer_start(&vibe_timer,

ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);  //DO-->hrtimer定时器时间初始化,其中ktime_set(秒,纳秒)

do_posix_clock_monotonic_gettime(&uptime);    //线程建立时间,用于比较看(定时器)此时时间
    KER_PRINT("Time:%lu.%02lu\n",

(unsigned long) uptime.tv_sec,  
            (uptime.tv_nsec / (NSEC_PER_SEC / 1000)));

INIT_WORK(&vibe_work, vibe_work_func);  /* Intialize the work queue */  //初始化工作队列

}

static int32_t xxxx_init(void)

{

ker_driver_init();

....

}

linux下jiffies定时器和hrtimer高精度定时器【转】的更多相关文章

  1. Linux下的hrtimer高精度定时器【转】

    转自:http://blog.csdn.net/waverider2012/article/details/38305785 hrtimer高精度定时器的interval由ktime_set(cons ...

  2. 使用linux内核hrtimer高精度定时器实现GPIO口模拟PWM,【原创】

    关键词:Android  linux hrtimer 蜂鸣器  等待队列 信号量 字符设备 平台信息:内核:linux3.4.39 系统:android/android5.1平台:S5P4418  作 ...

  3. linux下执行java类(运行java定时器)

    假如有一个定时器TimerTest.java import java.io.IOException; import java.util.Timer; public class TimerTest { ...

  4. hrtimer高精度定时器的简单使用【学习笔记】

    #include <linux/module.h> #include <linux/kernel.h> #include <linux/hrtimer.h> #in ...

  5. linux下的C语言开发(定时器)

    定时器是我们需要经常处理的一种资源.那Linux下面的定时器又是怎么一回事呢?其实,在linux里面有一种进程中信息传递的方法,那就是信号.这里的定时器就相当于系统每隔一段时间给进程发一个定时信号,我 ...

  6. Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现

    转自:http://blog.csdn.net/droidphone/article/details/8074892 上一篇文章,我介绍了传统的低分辨率定时器的实现原理.而随着内核的不断演进,大牛们已 ...

  7. Linux下的微秒级定时器: usleep, nanosleep, select, pselect

    Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...

  8. Linux下的定时器:alarm()与setitimer()

    Linux下的定时器有两种,以下分别介绍: 1.alarm 如果不要求很精确的话,用alarm()和signal()就够了 unsigned int alarm(unsigned int second ...

  9. windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器(用轮询的办法保持高精度)

    windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器可以有 100 纳秒也就是 1/10 微秒, 1/10000 毫秒的精度. 呵呵. SetWa ...

随机推荐

  1. Java学习之接口概念

    Java语言只支持单重继承,不支持多继承,即一个类只能有一个父类.但是在实际应用中,又经常需要使用多继承来解决问题.为了解决该问题,Java语言提供接口来实现类的多继承问题. 接口(英文interfa ...

  2. unittest多线程执行用例

    前言 假设执行一条脚本(.py)用例一分钟,那么100个脚本需要100分钟,当你的用例达到一千条时需要1000分钟,也就是16个多小时... 那么如何并行运行多个.py的脚本,节省时间呢?这就用到多线 ...

  3. [HDU3065]病毒持续侵袭中(AC自动机)

    传送门 AC自动机的又一模板,统计每个字符串在文本中的次数. 所以就不需要vis数组了. ——代码 #include <cstdio> #include <cstring> # ...

  4. [codeVS1204] 单词背诵

    题目描述 灵梦有n个单词想要背,但她想通过一篇文章中的一段来记住这些单词. 文章由m个单词构成,她想在文章中找出连续的一段,其中包含最多的她想要背的单词(重复的只算一个).并且在背诵的单词量尽量多的情 ...

  5. python学习之 - XML

    xml模块定义:实现不同语言或程序之间进行数据交换的协议.格式如下:通过<>节点来区别数据结构如:<load-on-startup(这个是标签) test="value&q ...

  6. 转: ORACLE存储过程笔记3----流程控制

    流程控制 1.条件   if expression thenpl/sql or sqlend if;   if expression thenpl/sql or sqlelsif expression ...

  7. linux下查看哪个进程占用内存多

    1.用top命令 1.top top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器 可以直接使用top命令后,查看%MEM的内容.可以 ...

  8. 【C++基础 02】深拷贝和浅拷贝

    我的主题是.每天积累一点点. =========================================== 在类定义中,假设没有提供自己的拷贝构造函数,则C++提供一个默认拷贝构造函数. C ...

  9. Angular2.x

    Angular版本 Angular1和Angular4分别是Angular的两个版本,也就是Angular1.x和Angular2.x(除了Angular1以外,其余都属于Angular2.x). 1 ...

  10. SQL FULL OUTER JOIN 关键字

    SQL FULL OUTER JOIN 关键字 SQL FULL OUTER JOIN 关键字 FULL OUTER JOIN 关键字只要左表(table1)和右表(table2)其中一个表中存在匹配 ...