//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. cocos2d-x plist在拍照

    if(!CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("scrollicon_bg.png" ...

  2. Yii Framework2.0开发教程(4)在yii中定义全局变量

    在yii中定义全局变量最好的地方是入口脚本处.也就是web目录中的index.php文件 比如我们在defined('YII_ENV') or define('YII_ENV', 'dev');后写上 ...

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

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

  4. 基于jsoup的Java服务端http(s)代理程序-代理服务器Demo

    亲爱的开发者朋友们,知道百度网址翻译么?他们为何能够翻译源网页呢,iframe可是不能跨域操作的哦,那么可以用代理实现.直接上代码: 本Demo基于MVC写的,灰常简单,copy过去,简单改改就可以用 ...

  5. 湘潭oj1203/邀请赛A称号 数论+java睑板

    乞讨 n%1+n%2+n%3+n%4+.........n%n=,n<=10^12次要. 一味的找规律之初.没有发现.后来,前辈执教后,人才平淡,所以,现在唯一明确的. 首先在地图上: 对于该题 ...

  6. 小议 js 下字符串比较大小

    原文:小议 js 下字符串比较大小 之前群里有人问如何比较两个时间大小,他的时间格式是 2014-08-08 而不是 2014-8-8.所以我给的方法是 直接比较,如: var a = "2 ...

  7. C语言对mysql数据库的操作

    原文:C语言对mysql数据库的操作 这已经是一相当老的话题.不过今天我才首次使用,把今天的一些体会写下来,也许能给一些新手带来一定的帮助,更重要的是供自己今后忘记的怎么使用而进行查阅的! 我们言归正 ...

  8. PE文件结构(五岁以下儿童)基地搬迁

    PE文件结构(五岁以下儿童) 參考 书:<加密与解密> 视频:小甲鱼 解密系列 视频 基址重定位 链接器生成一个PE文件时,它会如果程序被装入时使用的默认ImageBase基地址(VC默认 ...

  9. jquery+ligerform三级联动下拉框

    如下为ligerform里的三级联动下拉框: var formData=[ {display:,width:,space:,type:"select",group:"区域 ...

  10. 【从0开始Tornado网站】主页登录和显示的最新文章

    日志首页只能放置在它,这里的美,该<form>使用bootstrap的form-inline修改后的类,例如以下列方式: 前台代码例如以下: {%extends 'main.html'%} ...