linux 高精度定时器例子
//author:DriverMonkey
//phone:13410905075
//mail:bookworepeng@Hotmail.com
//qq:196568501 #include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/syscalls.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/of_platform.h>
#include <linux/uaccess.h>
#include <linux/string.h> #include <mach/gpio.h>
#include <mach/irqs.h> #define GPIO_TO_PIN(bank, gpio) (32 * (bank) + (gpio)) #define US (1000) #define PPM_CHANEL 8
#define FIX_LOW_TIME 100*US struct ppm_dev
{
struct cdev cdev;
dev_t devno;
struct class *ppm_class;
int message_cdev_open;
}; struct ppm_dev ppm_dev; static long long ppm_values[(PPM_CHANEL + 1)*2] =
{FIX_LOW_TIME,1000*US, // 1
FIX_LOW_TIME,1000*US, // 2
FIX_LOW_TIME,1000*US, // 3
FIX_LOW_TIME,1000*US, // 4
FIX_LOW_TIME,1000*US, // 5
FIX_LOW_TIME,1000*US, // 6
FIX_LOW_TIME,1000*US, // 7
FIX_LOW_TIME,1000*US, // 8
FIX_LOW_TIME,5000*US, }; // 9 ktime_t ktime;
static struct hrtimer hr_timer; static enum hrtimer_restart hrtimer_callback(struct hrtimer *timer)
{
static int index = 0;
static ktime_t ktime;
if(index == ((PPM_CHANEL + 1)*2))
index = 0;
ktime.tv64 = ppm_values[index];
hrtimer_forward(timer, timer->base->get_time(), ktime);
index++;
if(ktime.tv64 == FIX_LOW_TIME)
gpio_direction_output(GPIO_TO_PIN(0,27), 0);
else
gpio_direction_output(GPIO_TO_PIN(0,27), 1); //printk("%d\n",(int)ktime.tv64); return HRTIMER_RESTART;
} static int ppm_open(struct inode *node, struct file *fd)
{
int ret = 0; printk("ppm_open()++\n"); ktime = ktime_set( 0, 200*1000); // 200us
hrtimer_init( &hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );
hr_timer.function = &hrtimer_callback;
hrtimer_start( &hr_timer, ktime, HRTIMER_MODE_REL ); printk("ppm_open()--\n"); return ret;
} ssize_t ppm_write(struct file *pfile,
const char __user *buffer,
size_t size,
loff_t *pnull) { printk("ppm_write()++\n"); if(size != (PPM_CHANEL * sizeof(long long)))
return 0; //copy_from_user(ppm_values, buffer, size); printk("ppm_write()--\n"); return size;
}
static int ppm_fasync(int fd, struct file *filp, int mode)
{ printk("ppm_fasync()++\n"); printk("ppm_fasync()--\n"); return 0;
} static int ppm_release(struct inode *node, struct file *fd)
{
printk("ppm_release()++\n"); hrtimer_cancel(&hr_timer);
printk("ppm_release()--\n");
return 0;
} struct file_operations meassage_operatons =
{
.owner = THIS_MODULE,
.open = ppm_open, .write = ppm_write,
.fasync = ppm_fasync,
.release = ppm_release,
}; static int __init ppm_init(void)
{
struct ppm_dev * dev;
int ret = 0; dev = &ppm_dev; alloc_chrdev_region(&dev->devno, 0, 1, "out_ppm"); dev->ppm_class = class_create(THIS_MODULE, "ppm_class");
if(IS_ERR(dev->ppm_class)) {
printk(KERN_ERR"Err: failed in creating class./n");
goto fail1;
}
device_create(dev->ppm_class, NULL, dev->devno, NULL, "ppm"); //init irq
ret = gpio_request(GPIO_TO_PIN(0,27), "ppm_inter");
if(ret){
printk(KERN_ERR"gpio_request() failed !\n");
goto fail1;
}
ret = gpio_direction_output(GPIO_TO_PIN(0,27), 1);
if(ret){
printk(KERN_ERR"gpio_direction_input() failed !\n");
goto fail2;
} cdev_init(&dev->cdev, &meassage_operatons);
cdev_add(&dev->cdev, dev->devno, 1); if(ret){
printk(KERN_ERR"request_irq() failed ! %d\n", ret);
goto fail2;
} printk("ppm_to_app_init(void)--\n");
return 0; fail2:
gpio_free(GPIO_TO_PIN(0,27));
fail1:
device_destroy(dev->ppm_class, dev->devno);
class_destroy(dev->ppm_class);
cdev_del(&dev->cdev);
unregister_chrdev_region(dev->devno, 1); return ret;
}
static void __exit ppm_exit(void)
{
struct ppm_dev *dev = &ppm_dev; // printk("ppm_to_app_exit(void)++\n");
gpio_free(GPIO_TO_PIN(0,27)); device_destroy(dev->ppm_class, dev->devno);
class_destroy(dev->ppm_class);
cdev_del(&dev->cdev);
unregister_chrdev_region(dev->devno, 1); // printk("ppm_to_app_exit(void)--\n");
}
module_init(ppm_init);
module_exit(ppm_exit); MODULE_LICENSE("GPL");
MODULE_AUTHOR("Driver Monkey");
MODULE_DESCRIPTION("Test ppm");
linux 高精度定时器例子的更多相关文章
- Linux 高精度定时器hrtimer 使用示例【转】
本文转载自:http://blog.csdn.net/dean_gdp/article/details/25481225 hrtimer的基本操作 Linux的传统定时器通过时间轮算法实现(timer ...
- Linux下的hrtimer高精度定时器【转】
转自:http://blog.csdn.net/waverider2012/article/details/38305785 hrtimer高精度定时器的interval由ktime_set(cons ...
- Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现
转自:http://blog.csdn.net/droidphone/article/details/8074892 上一篇文章,我介绍了传统的低分辨率定时器的实现原理.而随着内核的不断演进,大牛们已 ...
- 使用linux内核hrtimer高精度定时器实现GPIO口模拟PWM,【原创】
关键词:Android linux hrtimer 蜂鸣器 等待队列 信号量 字符设备 平台信息:内核:linux3.4.39 系统:android/android5.1平台:S5P4418 作 ...
- linux下jiffies定时器和hrtimer高精度定时器【转】
本文转载自:http://blog.csdn.net/dosculler/article/details/7932315 一.jiffies定时器,HZ=100,精度只能达到10ms. 注:采用jif ...
- 高精度定时器实现 z
1背景Permalink .NET Framework 提供了四种定时器,然而其精度都不高(一般情况下 15ms 左右),难以满足一些场景下的需求. 在进行媒体播放.绘制动画.性能分析以及和硬件交互时 ...
- 芯灵思Sinlinx A64开发板Linux内核定时器编程
开发平台 芯灵思Sinlinx A64 内存: 1GB 存储: 4GB 开发板详细参数 https://m.tb.cn/h.3wMaSKm 开发板交流群 641395230 Linux 内核定时器是内 ...
- 全志A33开发板Linux内核定时器编程
开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 Linux 内核定时器是内核 ...
- 芯灵思SinlinxA33开发板Linux内核定时器编程
开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 Linux 内核定时器是内核 ...
随机推荐
- s性能优化方面的小知识
总结的js性能优化方面的小知识 前言 一直在学习javascript,也有看过<犀利开发Jquery内核详解与实践>,对这本书的评价只有两个字犀利,可能是对javascript理解的还不够 ...
- Java对象比较器对泛型List进行排序-Demo
针对形如:字段1 字段2 字段3 字段n 1 hello 26 7891 world 89 5562 what 55 4562 the 85 452 fuck 55 995 haha 98 455 以 ...
- Swift 制作一个新闻通知中心插件1
使用 Swift 制作一个新闻通知中心插件(1) 随着 iOS 8 的发布,苹果为开发者们开放了很多新的 API,而在这些开放的接口中 通知中心插件 无疑是最显眼的一个.通知中心就不用过多介绍了,相信 ...
- Appium Android Bootstrap源码分析之简介
在上一个系列中我们分析了UiAutomator的核心源码,对UiAutomator是怎么运行的原理有了根本的了解.今天我们会开始另外一个在安卓平台上基于UiAutomator的新起之秀--Appium ...
- UC编程:通过fwrite()和write()比较标准库函数和系统调用的速度
fwrte是C标准库中提供的函数,是对write函数的扩展与封装,write则是Unix系统提供的函数.按照常理来讲,系统调用肯定比使用库快的多,但是事实正好相反 Why?原因就在于缓冲的问题,fwi ...
- 深入浅出SQL Server 2008 分区函数和分区表
原文:深入浅出SQL Server 2008 分区函数和分区表 当我们数据量比较大的时候,我们需要将大型表拆分为多个较小的表,则只访问部门数据的查询就可以更快的运行,基本原理就是,因为要扫描的数据变的 ...
- Unity3D开发必备神器(Visual Studio Tools for Unity)
Unity3D开发必备神器(Visual Studio Tools for Unity) 开发Unity3D程序你用的什么IDE呢? 1.MonoDevelop 2.VS 可能你的回答是这样的,我用的 ...
- js关闭当前页面不弹出提示的方法
js关闭当前页面不弹出提示的方法 js关闭当前页面不弹出提示的方法 "window.opener=null;window.open('','_self','');window.close() ...
- 通过js实现在页面中添加音乐
代码如下!兼容IE // JavaScript Document function autoPlay(){//自动播放 var myAuto = document.getElementById('my ...
- springmvc和servlet在上传和下载文件(保持文件夹和存储数据库Blob两种方式)
参与该项目的文件上传和下载.一旦struts2下完成,今天springmvc再来一遍.发现springmvc特别好包,基本上不具备的几行代码即可完成,下面的代码贴: FileUpAndDown.jsp ...