本文转载自: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. 在Ubuntu 16.04 LTS上用g++和gcc编译C/C++代码错误提示“.../x86_64-linux-gnu/crt1.o: ELF section name out of range”

    (有一些图片我是直接从个人的CSDN博客上复制来的) 最近一个多月来,我曾经多次尝试在Ubuntu 16.04 LTS上使用g++和gcc(这俩好像合起来叫MinGW?)来编译C/C++代码,但是在解 ...

  2. 数据结构实验2:C++实现单链表类

    太简单了,直接贴题目然后上代码. 题目: 实验2 2.1 实验目的 熟练掌握线性表的链式存储结构. 熟练掌握单链表的有关算法设计. 根据具体问题的需要,设计出合理的表示数据的链式存储结构,并设计相关算 ...

  3. PS学习笔记(01)

    [1]PS,文件-脚本-删除所有的空图层.   [2]设计师与美工的区别? 设计在于有思路了再去找素材, 美工在于有素材后再去设计 (思路是在大量的设计上,才累计出来的.)   [3]如何知道一张图片 ...

  4. python007 Python3 数字(Number)

    var1 = 1 var2 = 10 您也可以使用del语句删除一些数字对象的引用.del语句的语法是: del var1[,var2[,var3[....,varN]]]] 您可以通过使用del语句 ...

  5. 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]-最小内积(第八届北京师范大学程序设计竞赛决赛)

    H. 最小内积                                                                   Time Limit: 1000ms Memory ...

  6. [luoguP1198][JSOI2008] 最大数(线段树 || 单调栈)

    题目传送门 1.线段树 线段树可以搞. 不过慢的要死1300+ms #include <cstdio> #include <iostream> using namespace ...

  7. POJ3621 Sightseeing Cows【最短路】

    题目大意:在一个无向图里找一个环,是的点权和除以边权和最大 思路:UVA11090姊妹题 事实上当这题点权和都为1时就是上一题TUT #include <stdio.h> #include ...

  8. [NOIP2002] 提高组 洛谷P1034 矩形覆盖

    题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4(0,7),见图一. 这 ...

  9. [NOIP1999] 提高组 洛谷P1016 旅行家的预算

    题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2.出发点每升汽油价格P和沿 ...

  10. web移动端小tip,box-flex

    1,移动端页面 最重要的标签: <meta content="width=device-width,initial-scale=1.0,minimum-scale=1,maximum- ...