APP:

  1. //author:DriverMonkey
  2. //phone:13410905075
  3. //mail:bookworepeng@Hotmail.com
  4. //qq:196568501
  5. #include<stdio.h>
  6. #include<string.h>
  7. #include<sys/types.h>
  8. #include<sys/stat.h>
  9. #include<fcntl.h>
  10. #include<unistd.h>
  11. #define US (1000)
  12. #define PPM_CHANEL 8
  13. #define FIX_LOW_TIME 100*US
  14. #define FIX_SYNC_TIME 5000
  15. static long long ppm_values[(PPM_CHANEL + 1)*2] =
  16. {FIX_LOW_TIME,1000*US,   // 1
  17. FIX_LOW_TIME,1000*US,    // 2
  18. FIX_LOW_TIME,1000*US,    // 3
  19. FIX_LOW_TIME,1000*US,    // 4
  20. FIX_LOW_TIME,1000*US,    // 5
  21. FIX_LOW_TIME,1000*US,    // 6
  22. FIX_LOW_TIME,1000*US,    // 7
  23. FIX_LOW_TIME,1000*US,    // 8
  24. FIX_LOW_TIME,FIX_SYNC_TIME*US, };    // 9
  25. int main(int argc,char *args[])
  26. {
  27. int fd;
  28. int channel = 0;
  29. long long value = 0;
  30. fd=open("/dev/ppm",O_WRONLY|O_CREAT,0640);
  31. if(fd < 0)
  32. return 0;
  33. if(argc > 3)
  34. return;
  35. channel = atol(args[1]);
  36. printf("input channle is: %d\n", channel);
  37. value  = atol(args[2]);
  38. printf("input value is: %d\n", (int)value );
  39. printf("old value is:%d\n",(int)ppm_values[channel*2 + 1]);
  40. ppm_values[channel*2 + 1] = value*US;
  41. printf("new value is:%d\n",(int)ppm_values[channel*2 + 1]);
  42. write(fd,ppm_values,sizeof(ppm_values));
  43. sleep(20);
  44. close(fd);
  45. }

Driver:

  1. //author:DriverMonkey
  2. //phone:13410905075
  3. //mail:bookworepeng@Hotmail.com
  4. //qq:196568501
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/cdev.h>
  8. #include <linux/fs.h>
  9. #include <linux/device.h>
  10. #include <linux/syscalls.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/gpio.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/string.h>
  17. #include <mach/gpio.h>
  18. #include <mach/irqs.h>
  19. #define GPIO_TO_PIN(bank, gpio) (32 * (bank) + (gpio))
  20. #define US (1000)
  21. #define PPM_CHANEL 8
  22. #define FIX_LOW_TIME 100*US
  23. struct ppm_dev
  24. {
  25. struct cdev cdev;
  26. dev_t devno;
  27. struct class *ppm_class;
  28. int message_cdev_open;
  29. };
  30. struct ppm_dev ppm_dev;
  31. static long long ppm_values[(PPM_CHANEL + 1)*2] =
  32. {FIX_LOW_TIME,1000*US,   // 1
  33. FIX_LOW_TIME,1000*US,    // 2
  34. FIX_LOW_TIME,1000*US,    // 3
  35. FIX_LOW_TIME,1000*US,    // 4
  36. FIX_LOW_TIME,1000*US,    // 5
  37. FIX_LOW_TIME,1000*US,    // 6
  38. FIX_LOW_TIME,1000*US,    // 7
  39. FIX_LOW_TIME,1000*US,    // 8
  40. FIX_LOW_TIME,5000*US, }; // 9
  41. ktime_t ktime;
  42. static struct hrtimer hr_timer;
  43. static enum hrtimer_restart hrtimer_callback(struct hrtimer *timer)
  44. {
  45. static int index = 0;
  46. static ktime_t ktime;
  47. if(index == ((PPM_CHANEL + 1)*2))
  48. index = 0;
  49. ktime.tv64 = ppm_values[index];
  50. hrtimer_forward(timer, timer->base->get_time(), ktime);
  51. index++;
  52. if(ktime.tv64 == FIX_LOW_TIME)
  53. gpio_direction_output(GPIO_TO_PIN(0,27), 0);
  54. else
  55. gpio_direction_output(GPIO_TO_PIN(0,27), 1);
  56. //printk("%d\n",(int)ktime.tv64);
  57. return HRTIMER_RESTART;
  58. }
  59. static int ppm_open(struct inode *node, struct file *fd)
  60. {
  61. int ret = 0;
  62. printk("ppm_open()++\n");
  63. ktime = ktime_set( 0, 200*1000);                   // 200us
  64. hrtimer_init( &hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL );
  65. hr_timer.function = &hrtimer_callback;
  66. hrtimer_start( &hr_timer, ktime, HRTIMER_MODE_REL );
  67. printk("ppm_open()--\n");
  68. return ret;
  69. }
  70. ssize_t ppm_write(struct file *pfile,
  71. const char __user *buffer,
  72. size_t size,
  73. loff_t *pnull)
  74. {
  75. printk("ppm_write()++\n");
  76. if(size != sizeof(ppm_values))
  77. return 0;
  78. copy_from_user(ppm_values, buffer, size);
  79. printk("ppm_write()--\n");
  80. return size;
  81. }
  82. static int ppm_fasync(int fd, struct file *filp, int mode)
  83. {
  84. printk("ppm_fasync()++\n");
  85. printk("ppm_fasync()--\n");
  86. return 0;
  87. }
  88. static int ppm_release(struct inode *node, struct file *fd)
  89. {
  90. printk("ppm_release()++\n");
  91. hrtimer_cancel(&hr_timer);
  92. printk("ppm_release()--\n");
  93. return 0;
  94. }
  95. struct file_operations meassage_operatons =
  96. {
  97. .owner = THIS_MODULE,
  98. .open = ppm_open,
  99. .write = ppm_write,
  100. .fasync = ppm_fasync,
  101. .release = ppm_release,
  102. };
  103. static int __init ppm_init(void)
  104. {
  105. struct ppm_dev * dev;
  106. int ret = 0;
  107. dev = &ppm_dev;
  108. alloc_chrdev_region(&dev->devno, 0, 1, "out_ppm");
  109. dev->ppm_class = class_create(THIS_MODULE, "ppm_class");
  110. if(IS_ERR(dev->ppm_class)) {
  111. printk(KERN_ERR"Err: failed in creating class./n");
  112. goto fail1;
  113. }
  114. device_create(dev->ppm_class, NULL, dev->devno, NULL, "ppm");
  115. //init irq
  116. ret = gpio_request(GPIO_TO_PIN(0,27), "ppm_inter");
  117. if(ret){
  118. printk(KERN_ERR"gpio_request() failed !\n");
  119. goto fail1;
  120. }
  121. ret = gpio_direction_output(GPIO_TO_PIN(0,27), 1);
  122. if(ret){
  123. printk(KERN_ERR"gpio_direction_input() failed !\n");
  124. goto fail2;
  125. }
  126. cdev_init(&dev->cdev, &meassage_operatons);
  127. cdev_add(&dev->cdev, dev->devno, 1);
  128. if(ret){
  129. printk(KERN_ERR"request_irq() failed ! %d\n", ret);
  130. goto fail2;
  131. }
  132. printk("ppm_to_app_init(void)--\n");
  133. return 0;
  134. fail2:
  135. gpio_free(GPIO_TO_PIN(0,27));
  136. fail1:
  137. device_destroy(dev->ppm_class, dev->devno);
  138. class_destroy(dev->ppm_class);
  139. cdev_del(&dev->cdev);
  140. unregister_chrdev_region(dev->devno, 1);
  141. return ret;
  142. }
  143. static void __exit ppm_exit(void)
  144. {
  145. struct ppm_dev *dev = &ppm_dev;
  146. // printk("ppm_to_app_exit(void)++\n");
  147. gpio_free(GPIO_TO_PIN(0,27));
  148. device_destroy(dev->ppm_class, dev->devno);
  149. class_destroy(dev->ppm_class);
  150. cdev_del(&dev->cdev);
  151. unregister_chrdev_region(dev->devno, 1);
  152. // printk("ppm_to_app_exit(void)--\n");
  153. }
  154. module_init(ppm_init);
  155. module_exit(ppm_exit);
  156. MODULE_LICENSE("GPL");
  157. MODULE_AUTHOR("Driver Monkey");
  158. MODULE_DESCRIPTION("Test ppm");

LINUX 产生PPM 驱动例子的更多相关文章

  1. Linux FC/iSCSI存储设备管理系列(一):Linux系统设备驱动入门

    Linux FC/iSCSI存储设备管理系列(一):Linux系统设备驱动入门 转载请在文首保留原文出处:EMC中文支持论坛 - https://community.emc.com/go/chines ...

  2. 【VS开发】【DSP开发】浅谈Linux PCI设备驱动(二)

    我们在 浅谈Linux PCI设备驱动(一)中(以下简称 浅谈(一) )介绍了PCI的配置寄存器组,而Linux PCI初始化就是使用了这些寄存器来进行的.后面我们会举个例子来说明Linux PCI设 ...

  3. 【VS开发】【DSP开发】浅谈Linux PCI设备驱动(一)

    要弄清楚Linux PCI设备驱动,首先要明白,所谓的Linux PCI设备驱动实际包括Linux PCI设备驱动和设备本身驱动两部分.不知道读者理不理解这句话,本人觉得这句话很重要,对于PCI.US ...

  4. (57)Linux驱动开发之三Linux字符设备驱动

    1.一般情况下,对每一种设备驱动都会定义一个软件模块,这个工程模块包含.h和.c文件,前者定义该设备驱动的数据结构并声明外部函数,后者进行设备驱动的具体实现. 2.典型的无操作系统下的逻辑开发程序是: ...

  5. Linux Charger IC 驱动移植总结

    Linux Charger IC 驱动移植总结 文章目录 Linux Charger IC 驱动移植总结 1 设备树的基本知识 设备树的概念 设备树的基本结构 compatible属性 举个栗子 2 ...

  6. linux块设备驱动之实例

    1.注册:向内核注册个块设备驱动,其实就是用主设备号告诉内核这个代表块设备驱动 sbull_major  =  register_blkdev(sbull_major, "sbull&quo ...

  7. Linux 视频设备驱动V4L2最常用的控制命令

    http://blog.csdn.net/shaolyh/article/details/6583226 Linux 视频设备驱动V4L2最常用的控制命令使用说明(1.02) 命令 功能 VIDIOC ...

  8. linux 2.6 驱动笔记(一)

    本文作为linux 2.6 驱动笔记,记录环境搭建及linux基本内核模块编译加载. 环境搭建: 硬件:OK6410开发板 目标板操作系统:linux 2.6 交叉编译环境:windows 7 + v ...

  9. 深入理解Linux字符设备驱动

    文章从上层应用访问字符设备驱动开始,一步步地深入分析Linux字符设备的软件层次.组成框架和交互.如何编写驱动.设备文件的创建和mdev原理,对Linux字符设备驱动有全面的讲解.本文整合之前发表的& ...

随机推荐

  1. 刨根问底U3D---从一个空类说起

    这篇文章包含哪些内容 这篇文章从一个Empty的MonoBehaviour入手,首先讨论一下C#的修饰符internal,default,virtual,sealed 接着讨论一下MonoBehavi ...

  2. :“boost/serialization/string.hpp”: No such file or directory 错误

    主要原因是没有安装和配置boost库. 解决:http://www.programlife.net/boost-compile-and-config.html

  3. 利用并查集求最大生成树和最小生成树(nlogn)

    hdu1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  4. 接口是否可继承接口? 抽像类是否可实现(implements)接口? 抽像类是否可继承实体类(concrete class)?

    接口是否可继承接口? 抽像类是否可实现(implements)接口? 抽像类是否可继承实体类(concrete class)? 1. 接口可以继承接口. 2. 抽像类可以实现(implements)接 ...

  5. CCF真题之节日

    201503-3 问题描述 有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形式定下来的,比如说母亲节就定为每年的五月的第二个星期日. 现在,给你a,b,c和y1, y2(1850 ≤ y ...

  6. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

    Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You ...

  7. 20道C#练习题(二)11——20题

    11.一个游戏,前20关是每一关自身的分数,1-30关每一关是10分,31-40关,每一关是20分,1-49关,每一关是30分,第50关是100分,输入你现在闯到的关卡数,求你现在拥有的分数.利用if ...

  8. UBoot讲解和实践-----------讲解(一)

    Boot Loader之ARM Uboot移植 阶段一 理论篇 1.boot loader需要完成的任务:               1>设计程序的入口地址               2&g ...

  9. 161028、Nginx负载均衡实现tomcat集群方案简要小结

    重点两部分:一.负载均衡二.tomcat集群 所谓tomcat集群,就是可以向外提供并行服务的多台机器,任何一台服务器宕机,其它服务器可以替代它向外提供服务,而不影响用户访问. Nginx是一个常用的 ...

  10. STM32模拟I2C

    之前为了测试, 拿最小板做了一个I2C的主发跟主读, 一开始当然是尝试用硬件I2C, 结果弄了很久, 时间紧迫, 只好用了模拟, 结果发现, 哎, 真特么挺好用的, 现在1片儿顶过去5片儿. 硬件I2 ...