Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1371400
  • 博文数量: 198
  • 博客积分: 1629
  • 博客等级: 上尉
  • 技术积分: 2743
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-01 15:41
文章分类
文章存档

2023年(6)

2022年(20)

2021年(8)

2020年(3)

2018年(17)

2017年(3)

2016年(3)

2015年(9)

2014年(13)

2013年(17)

2012年(77)

2011年(22)

分类: LINUX

2022-03-28 20:46:32

驱动:

点击(此处)折叠或打开

  1. /*
  2.  * gpio irq
  3.  */
  4. #include <linux/module.h>
  5. #include <linux/moduleparam.h>
  6. #include <linux/cdev.h>
  7. #include <linux/fs.h>
  8. #include <linux/wait.h>
  9. #include <linux/poll.h>
  10. #include <linux/sched.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/gpio.h>
  14. #include <linux/interrupt.h>
  15.  
  16. #define SUCCESS (0)
  17. #define ERROR (-1)
  18.  
  19. static struct fasync_struct *fasync_queue;
  20.  
  21. #define IRQ_PIN 115
  22. int flag = 0;
  23.  
  24. struct cdev *irq_dev;
  25. int major;
  26. static struct class *irq_class;
  27. //static unsigned int irq_num;
  28.  
  29. int dev_open(struct inode *inode, struct file *filp)
  30. {
  31.     return 0;
  32. }
  33.  
  34. ssize_t dev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_ops)
  35. {
  36.     return count;
  37. }
  38.  
  39. ssize_t dev_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_ops)
  40. {
  41.     return count;
  42. }
  43.  
  44.  
  45. static irqreturn_t irq_handler(int irq, void *dev)
  46. {
  47.     printk("driver irq work\n");
  48.     if(fasync_queue){
  49.         kill_fasync(&fasync_queue, SIGIO, POLL_IN);
  50.     }
  51.  
  52.     return IRQ_HANDLED;
  53. }
  54.  
  55. static int dev_fasync(int fd, struct file *filp, int on)
  56. {
  57.     int retval;
  58.     retval = fasync_helper(fd, filp, on, &fasync_queue);
  59.     if(retval < 0){
  60.         return retval;
  61.     }
  62.     return 0;
  63. }
  64.  
  65. int dev_release(struct inode *inode, struct file *filp)
  66. {
  67.     dev_fasync(-1, filp, 0);
  68.     return 0;
  69. }
  70.  
  71. static struct file_operations dev_fops = {
  72.     .owner = THIS_MODULE,
  73.     .open = dev_open,
  74.     .release = dev_release,
  75.     .read = dev_read,
  76.     .write = dev_write,
  77.     .fasync = dev_fasync,
  78. };
  79.  
  80. static int dev_init(void)
  81. {
  82.     int err;
  83.     major = register_chrdev(0, "irq_dev", &dev_fops);
  84.     if(major < 0){
  85.         printk("Unable to register character device %d\n", major);
  86.         return major;
  87.     }
  88.  
  89.     irq_class = class_create(THIS_MODULE, "irq_class");
  90.     device_create(irq_class, NULL, MKDEV(major, 0), NULL, "irq_dev");
  91.  
  92.  
  93.     printk(KERN_INFO "irq init\n");
  94.     err = gpio_request(IRQ_PIN, "power_down");
  95.     if(err){
  96.         return err;
  97.     }
  98.  
  99.     err = request_irq(gpio_to_irq(IRQ_PIN), irq_handler, IRQF_TRIGGER_FALLING, "power down", NULL);
  100.  
  101.     if(err < 0) {
  102.         printk("irq_request failed\n");
  103.         return err;
  104.     }
  105.     
  106.     printk("module init sucessful !!!\n");
  107.     flag = 1;
  108.     return 0;
  109. }
  110.  
  111.  
  112. static void dev_exit(void)
  113. {
  114.     printk(KERN_INFO "gpio irq exit\n");
  115.     unregister_chrdev(major, "irq_dev");
  116.     device_destroy(irq_class, MKDEV(major,0));
  117.     class_destroy(irq_class);
  118.  
  119.     if(flag){
  120.         free_irq(gpio_to_irq(IRQ_PIN), NULL);
  121.         gpio_free(IRQ_PIN);
  122.     }
  123.     printk("module exit successful !!!\n");
  124. }
  125.  
  126.  
  127. module_init(dev_init);
  128. module_exit(dev_exit);
  129.  
  130. MODULE_LICENSE("Dual BSD/GPL");
测试程序:

点击(此处)折叠或打开

  1. /*
  2.  * irq test
  3.  */
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. #include <signal.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10.  
  11. static int fd;
  12.  
  13. void sigterm_handler(int signo)
  14. {
  15.     int i = 0;
  16.     printf("app irq work !!!\n");
  17.     while(1)
  18.     {
  19.         printf("%d do something ...\r", i++);
  20.         fflush(stdout);
  21.         usleep(10000);
  22.     }
  23. }
  24.  
  25. int main(void)
  26. {
  27.     int i = 0;
  28.     int oflags;
  29.     fd = open("/dev/irq_dev", O_RDWR);
  30.     printf("open fd = %d\n", fd);
  31.     if(fd < 0)
  32.     {
  33.         perror("open");
  34.         return -1;
  35.     }
  36.  
  37.     signal(SIGIO, sigterm_handler);
  38.     fcntl(fd, F_SETOWN, getpid());
  39.  
  40.     oflags = fcntl(fd, F_GETFL);
  41.     fcntl(fd, F_SETFL, oflags | FASYNC);
  42.  
  43.     while(1)
  44.     {
  45.         printf("sleep %d\n", i++);
  46.         sleep(1);
  47.     }
  48.  
  49.     close(fd);
  50.     return 0;
  51. }

阅读(395) | 评论(0) | 转发(0) |
0

上一篇:启动计数

下一篇:获取usb设备VID&PID

给主人留下些什么吧!~~