//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 高精度定时器例子的更多相关文章

  1. Linux 高精度定时器hrtimer 使用示例【转】

    本文转载自:http://blog.csdn.net/dean_gdp/article/details/25481225 hrtimer的基本操作 Linux的传统定时器通过时间轮算法实现(timer ...

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

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

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

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

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

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

  5. linux下jiffies定时器和hrtimer高精度定时器【转】

    本文转载自:http://blog.csdn.net/dosculler/article/details/7932315 一.jiffies定时器,HZ=100,精度只能达到10ms. 注:采用jif ...

  6. 高精度定时器实现 z

    1背景Permalink .NET Framework 提供了四种定时器,然而其精度都不高(一般情况下 15ms 左右),难以满足一些场景下的需求. 在进行媒体播放.绘制动画.性能分析以及和硬件交互时 ...

  7. 芯灵思Sinlinx A64开发板Linux内核定时器编程

    开发平台 芯灵思Sinlinx A64 内存: 1GB 存储: 4GB 开发板详细参数 https://m.tb.cn/h.3wMaSKm 开发板交流群 641395230 Linux 内核定时器是内 ...

  8. 全志A33开发板Linux内核定时器编程

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 Linux 内核定时器是内核 ...

  9. 芯灵思SinlinxA33开发板Linux内核定时器编程

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 Linux 内核定时器是内核 ...

随机推荐

  1. [Cocoa]深入浅出 Cocoa 之消息

    深入浅出 Cocoa 之消息    罗朝辉(http://blog.csdn.net/kesalin) 转载请注明出处 在入门级别的ObjC 教程中,我们常对从C++或Java 或其它面向对象语言转过 ...

  2. C语言库函数大全及应用实例六

    原文:C语言库函数大全及应用实例六                                              [编程资料]C语言库函数大全及应用实例六 函数名: getlinesett ...

  3. 超高性能的json序列化

    超高性能的json序列化之MVC中使用Json.Net 超高性能的json序列化之MVC中使用Json.Net 先不废话,直接上代码 Asp.net MVC自带Json序列化 1 /// <su ...

  4. NGUI ScrollView动态加入和删除对象。

    动态加入,基本思想是: 1.先把要加入的元素在编辑器中编辑好,制作成一个prefab. 2.在代码中,动态的生成一个新的对象增加到Grid对象的子对象中.这里利用到了Resources对象,这个对象的 ...

  5. JVM监控概述(图文)

    JVM内存分配概述 Jvm 内存分为:堆.非堆及直接内存三大块. 堆区分为年轻代和老年代,永生代属于非堆内存. 对象优先在Eden区分配 大对象直接进入老年代 长期存活的对象将进入老年代 class. ...

  6. CSS知识总结之浏览器(持续更新)

    web页面浏览器渲染过程 1.解析html文件,并构建DOM树: 在DOM树中,每一个html标签都有一个对应的节点,并且每一个文本也有一个对应 的节点(js的textNode),DOM树的根节点就是 ...

  7. Fedora21无法播放MP4[已解决]

    首先,安装安装rpmfusion源 http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-21.noarch.rpm ht ...

  8. SQL点滴20—T-SQL中的排名函数

    原文:SQL点滴20-T-SQL中的排名函数 提到排名函数我们首先可能想到的是order by,这个是排序,不是排名,排名需要在前面加个名次序号的,order by是没有这个功能的.还可能会想到ide ...

  9. hdu2899 Strange fuction

    在区间(0,100).在恒大二阶导数0.f(x)有极小值.用的最低要求的一阶导数值点: #include<math.h> #include<stdio.h> #include& ...

  10. JS复选框选中

    Web前端之复选框选中属性   熟悉web前端开发的人都知道,判断复选框是否选中是经常做的事情,判断的方法很多,但是开发过程中常常忽略了这些方法的兼容性,而是实现效果就好了.博主之前用户不少方法,经常 ...