linux下jiffies定时器和hrtimer高精度定时器【转】
本文转载自: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高精度定时器【转】的更多相关文章
- Linux下的hrtimer高精度定时器【转】
转自:http://blog.csdn.net/waverider2012/article/details/38305785 hrtimer高精度定时器的interval由ktime_set(cons ...
- 使用linux内核hrtimer高精度定时器实现GPIO口模拟PWM,【原创】
关键词:Android linux hrtimer 蜂鸣器 等待队列 信号量 字符设备 平台信息:内核:linux3.4.39 系统:android/android5.1平台:S5P4418 作 ...
- linux下执行java类(运行java定时器)
假如有一个定时器TimerTest.java import java.io.IOException; import java.util.Timer; public class TimerTest { ...
- hrtimer高精度定时器的简单使用【学习笔记】
#include <linux/module.h> #include <linux/kernel.h> #include <linux/hrtimer.h> #in ...
- linux下的C语言开发(定时器)
定时器是我们需要经常处理的一种资源.那Linux下面的定时器又是怎么一回事呢?其实,在linux里面有一种进程中信息传递的方法,那就是信号.这里的定时器就相当于系统每隔一段时间给进程发一个定时信号,我 ...
- Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现
转自:http://blog.csdn.net/droidphone/article/details/8074892 上一篇文章,我介绍了传统的低分辨率定时器的实现原理.而随着内核的不断演进,大牛们已 ...
- Linux下的微秒级定时器: usleep, nanosleep, select, pselect
Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...
- Linux下的定时器:alarm()与setitimer()
Linux下的定时器有两种,以下分别介绍: 1.alarm 如果不要求很精确的话,用alarm()和signal()就够了 unsigned int alarm(unsigned int second ...
- windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器(用轮询的办法保持高精度)
windows 下,用CreateWaitableTimer SetWaitableTimer 创建定时器可以有 100 纳秒也就是 1/10 微秒, 1/10000 毫秒的精度. 呵呵. SetWa ...
随机推荐
- PHP include和require 区别
require 的使用方法如 require("MyRequireFile.php"); .这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require ...
- php-fpm.conf
[global]pid = /usr/local/php/var/run/php-fpm.piderror_log = /usr/local/php/var/log/php-fpm.loglog_le ...
- 洛谷P1771 方程的解_NOI导刊2010提高(01)
题目描述 佳佳碰到了一个难题,请你来帮忙解决. 对于不定方程a1+a2+…+ak-1+ak=g(x),其中k≥2且k∈N,x是正整数,g(x)=x^x mod 1000(即x^x除以1000的余数), ...
- hdu 4801模拟题
/* 模拟: 注意:实质上一次魔方的一半要变化 用c++超内存 用g++过了 */ #include<stdio.h> #include<string.h> #include& ...
- 16 个常用的yum 命令
1. yum [-y] install package_name2. yum remove package_name 卸载指定软件3. ...
- ***ps -ef |grep 输出的具体含义是什么?
Q: 比如:[root@localhost ~]# ps -ef | grep ApacheJetspeedroot 18887 18828 0 08:09 pts/0 00:00:00 grep A ...
- React学习及实例开发(一)——开始
本文基于React v16.4.1 初学react,有理解不对的地方,欢迎批评指正^_^ 一.构建一个新项目 1.命令行运行如下命令,构建一个新的react项目 npm install -g crea ...
- ZOJ 1232 【灵活运用FLOYD】 【图DP】
题意: copy自http://blog.csdn.net/monkey_little/article/details/6637805 有A个村子和B个城堡,村子标号是1~A,城堡标号是A+1~B.马 ...
- MongoDB学习day09--Mongoose数据校验
一.Mongoose检验参数 required : 表示这个数据必须传入max: 用于 Number 类型数据, 最大值 min: 用于 Number 类型数据, 最小值 enum:枚举类型, 要求数 ...
- linux otl oracle数据库连接例子
#include <string> #include <iostream> using namespace std; #define OTL_ORA10G //我连的是LI ...