LINUX 产生PPM 驱动例子
APP:
- //author:DriverMonkey
- //phone:13410905075
- //mail:bookworepeng@Hotmail.com
- //qq:196568501
- #include<stdio.h>
- #include<string.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include<unistd.h>
- #define US (1000)
- #define PPM_CHANEL 8
- #define FIX_LOW_TIME 100*US
- #define FIX_SYNC_TIME 5000
- 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,FIX_SYNC_TIME*US, }; // 9
- int main(int argc,char *args[])
- {
- int fd;
- int channel = 0;
- long long value = 0;
- fd=open("/dev/ppm",O_WRONLY|O_CREAT,0640);
- if(fd < 0)
- return 0;
- if(argc > 3)
- return;
- channel = atol(args[1]);
- printf("input channle is: %d\n", channel);
- value = atol(args[2]);
- printf("input value is: %d\n", (int)value );
- printf("old value is:%d\n",(int)ppm_values[channel*2 + 1]);
- ppm_values[channel*2 + 1] = value*US;
- printf("new value is:%d\n",(int)ppm_values[channel*2 + 1]);
- write(fd,ppm_values,sizeof(ppm_values));
- sleep(20);
- close(fd);
- }
Driver:
- //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 != sizeof(ppm_values))
- 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 产生PPM 驱动例子的更多相关文章
- Linux FC/iSCSI存储设备管理系列(一):Linux系统设备驱动入门
Linux FC/iSCSI存储设备管理系列(一):Linux系统设备驱动入门 转载请在文首保留原文出处:EMC中文支持论坛 - https://community.emc.com/go/chines ...
- 【VS开发】【DSP开发】浅谈Linux PCI设备驱动(二)
我们在 浅谈Linux PCI设备驱动(一)中(以下简称 浅谈(一) )介绍了PCI的配置寄存器组,而Linux PCI初始化就是使用了这些寄存器来进行的.后面我们会举个例子来说明Linux PCI设 ...
- 【VS开发】【DSP开发】浅谈Linux PCI设备驱动(一)
要弄清楚Linux PCI设备驱动,首先要明白,所谓的Linux PCI设备驱动实际包括Linux PCI设备驱动和设备本身驱动两部分.不知道读者理不理解这句话,本人觉得这句话很重要,对于PCI.US ...
- (57)Linux驱动开发之三Linux字符设备驱动
1.一般情况下,对每一种设备驱动都会定义一个软件模块,这个工程模块包含.h和.c文件,前者定义该设备驱动的数据结构并声明外部函数,后者进行设备驱动的具体实现. 2.典型的无操作系统下的逻辑开发程序是: ...
- Linux Charger IC 驱动移植总结
Linux Charger IC 驱动移植总结 文章目录 Linux Charger IC 驱动移植总结 1 设备树的基本知识 设备树的概念 设备树的基本结构 compatible属性 举个栗子 2 ...
- linux块设备驱动之实例
1.注册:向内核注册个块设备驱动,其实就是用主设备号告诉内核这个代表块设备驱动 sbull_major = register_blkdev(sbull_major, "sbull&quo ...
- Linux 视频设备驱动V4L2最常用的控制命令
http://blog.csdn.net/shaolyh/article/details/6583226 Linux 视频设备驱动V4L2最常用的控制命令使用说明(1.02) 命令 功能 VIDIOC ...
- linux 2.6 驱动笔记(一)
本文作为linux 2.6 驱动笔记,记录环境搭建及linux基本内核模块编译加载. 环境搭建: 硬件:OK6410开发板 目标板操作系统:linux 2.6 交叉编译环境:windows 7 + v ...
- 深入理解Linux字符设备驱动
文章从上层应用访问字符设备驱动开始,一步步地深入分析Linux字符设备的软件层次.组成框架和交互.如何编写驱动.设备文件的创建和mdev原理,对Linux字符设备驱动有全面的讲解.本文整合之前发表的& ...
随机推荐
- 刨根问底U3D---从一个空类说起
这篇文章包含哪些内容 这篇文章从一个Empty的MonoBehaviour入手,首先讨论一下C#的修饰符internal,default,virtual,sealed 接着讨论一下MonoBehavi ...
- :“boost/serialization/string.hpp”: No such file or directory 错误
主要原因是没有安装和配置boost库. 解决:http://www.programlife.net/boost-compile-and-config.html
- 利用并查集求最大生成树和最小生成树(nlogn)
hdu1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 接口是否可继承接口? 抽像类是否可实现(implements)接口? 抽像类是否可继承实体类(concrete class)?
接口是否可继承接口? 抽像类是否可实现(implements)接口? 抽像类是否可继承实体类(concrete class)? 1. 接口可以继承接口. 2. 抽像类可以实现(implements)接 ...
- CCF真题之节日
201503-3 问题描述 有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形式定下来的,比如说母亲节就定为每年的五月的第二个星期日. 现在,给你a,b,c和y1, y2(1850 ≤ y ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You ...
- 20道C#练习题(二)11——20题
11.一个游戏,前20关是每一关自身的分数,1-30关每一关是10分,31-40关,每一关是20分,1-49关,每一关是30分,第50关是100分,输入你现在闯到的关卡数,求你现在拥有的分数.利用if ...
- UBoot讲解和实践-----------讲解(一)
Boot Loader之ARM Uboot移植 阶段一 理论篇 1.boot loader需要完成的任务: 1>设计程序的入口地址 2&g ...
- 161028、Nginx负载均衡实现tomcat集群方案简要小结
重点两部分:一.负载均衡二.tomcat集群 所谓tomcat集群,就是可以向外提供并行服务的多台机器,任何一台服务器宕机,其它服务器可以替代它向外提供服务,而不影响用户访问. Nginx是一个常用的 ...
- STM32模拟I2C
之前为了测试, 拿最小板做了一个I2C的主发跟主读, 一开始当然是尝试用硬件I2C, 结果弄了很久, 时间紧迫, 只好用了模拟, 结果发现, 哎, 真特么挺好用的, 现在1片儿顶过去5片儿. 硬件I2 ...