CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 10147|回复: 4

关于_外部中断驱动异步通知应用程序,

[复制链接]
发表于 2014-9-2 22:45:40 | 显示全部楼层 |阅读模式
本帖最后由 jiangdou 于 2014-12-24 17:17 编辑

先上图片:

1,file 1,-> ent_test.c
  1. /*
  2. * file  ent_test.c
  3. * Copyright by jiangdou QQ:344283973
  4. *
  5. * RK3188 ENT test 20140902
  6. * how to compile: $ arm-linux-gnueabihf-gcc -o ent_test ENT_test.c  -static

  7. */


  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <poll.h>
  13. #include <signal.h>
  14. #include <sys/types.h>
  15. #include <unistd.h>
  16. #include <fcntl.h>



  17. int fd;

  18. //信号处理函数
  19. void my_signal_fun(int signum)
  20. {
  21.         
  22.         printf("by jiangdou............\n");
  23. }

  24. int main(void)
  25. {
  26.         unsigned char key_val;
  27.         int ret;
  28.         int Oflags;
  29.         signal(SIGIO, my_signal_fun);
  30.         fd = open("/dev/key", O_RDWR);
  31.         if (fd < 0)
  32.         {
  33.                 printf("can't open!\n");
  34.         }
  35.         ret = fcntl(fd, F_SETOWN, getpid());
  36.         if(ret < 0)
  37.         {
  38.                 printf("can't fcntl_1!\n");
  39.         }
  40.         
  41.         
  42.         Oflags = fcntl(fd, F_GETFL);
  43.         printf("Oflags is : %d\n", Oflags);//is 2
  44.         if(Oflags < 0)
  45.         {
  46.                 printf("can't fcntl_2!\n");
  47.         }
  48.         ret = fcntl(fd, F_SETFL, Oflags | FASYNC);
  49.         if(ret < 0)
  50.         {
  51.                 printf("can't fcntl_3!\n");
  52.         }
  53.         while (1)
  54.         {
  55.                 sleep(1000);
  56.         }
  57.         
  58.         return 0;
  59. }



复制代码
1,file 2,-> irq_.c
  1. /*
  2. * file  ent_test.c
  3. * Driver for irq on GPIO lines capable of generating interrupts.
  4. *
  5. * Copyright by jiangdou
  6. *
  7. *
  8. *
  9. */

  10. #include <linux/module.h>

  11. #include <linux/init.h>
  12. #include <linux/fs.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/sched.h>
  16. #include <linux/pm.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/delay.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/input.h>
  22. #include <linux/adc.h>

  23. #include <asm/gpio.h>
  24. #include <mach/board.h>
  25. #include <plat/key.h>
  26. #include <linux/poll.h>



  27. static struct class *keydrv_class;

  28. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

  29. static struct fasync_struct *button_async;


  30. //static volatile int ev_press = 0;




  31. #define ENT0 RK30_PIN1_PB7
  32. #define GPIO_IRQ  207


  33. static irqreturn_t irq_isr(int irq, void *dev_id)
  34. {
  35.         //printk("jiangdou....irq_isr...\n");
  36.         
  37.         ev_press = 1;

  38.     wake_up_interruptible(&button_waitq);  
  39.    
  40.         
  41.         kill_fasync(&button_async, SIGIO, POLL_IN);
  42.         
  43.         return IRQ_RETVAL(IRQ_HANDLED);
  44.         
  45.         //return 0;
  46. }

  47. static int irq_open(struct inode*inode, struct file *file)
  48. {
  49.         int error;
  50.         
  51.         printk("jiangdou....irq_open...\n");
  52.         if(gpio_request(ENT0,NULL) != 0){
  53.       gpio_free(ENT0);
  54.       printk("gpio_request error\n");
  55.       return -EIO;
  56.     }
  57.         error = request_irq(GPIO_IRQ, irq_isr, IRQF_TRIGGER_FALLING, "irq_dou", 1);
  58.         if(error){
  59.                 printk("failed to register irq_isr interrupt\n");
  60.         }
  61.         return error;
  62. }


  63. int irq_close(struct inode *inode,struct file *file)
  64. {
  65.         free_irq(GPIO_IRQ,1);
  66.         return 0;
  67. }


  68. static unsigned irq_poll(struct file *file, poll_table *wait)
  69. {
  70.         unsigned int mask = 0;
  71.         poll_wait(file, &button_waitq, wait); //

  72.         if (ev_press)
  73.                 mask |= POLLIN | POLLRDNORM;

  74.         return mask;
  75. }




  76. static int irq_fasync (int fd, struct file *filp, int on)
  77. {
  78.         printk("driver: fifth_drv_fasync\n");
  79.         
  80.         return fasync_helper(fd, filp, on, &button_async);
  81. }





  82. static struct file_operations irq_drv_fops= {

  83.         .owner        =        THIS_MODULE,   
  84.         .open        =        irq_open,            
  85.         .release        =        irq_close,
  86.         //.fasync         =  irq_fasync,
  87.         //.poll    =  irq_poll,

  88. };

  89. int major;

  90. static int  irq_init(void)
  91. {
  92.         printk("jiangdou....irq_init...\n");
  93.         major = register_chrdev(0, "irq_drv", &irq_drv_fops);
  94.         keydrv_class = class_create(THIS_MODULE,"irq_drv");
  95.         device_create(keydrv_class,NULL,MKDEV(major,0),NULL,"key");
  96.         

  97.         return 0;
  98. }
  99. static void irq_drv_exit(void)

  100. {
  101.         unregister_chrdev(major, "irq_drv");
  102.         device_destroy(keydrv_class,MKDEV(major,0));
  103.         class_destroy(keydrv_class);
  104.         

  105. }

  106. module_init(irq_init);

  107. module_exit(irq_drv_exit);

  108. MODULE_LICENSE("GPL");

复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

发表于 2014-9-3 08:49:10 | 显示全部楼层
不错,但是要拿A10搞一个
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-9-3 10:49:01 | 显示全部楼层
qiaoge 发表于 2014-9-3 08:49
不错,但是要拿A10搞一个

芯片是次要的,驱动框架更重要,A10,更简单
回复 支持 反对

使用道具 举报

发表于 2014-9-4 14:24:32 | 显示全部楼层
jiangdou又出好东西啦。
回复 支持 反对

使用道具 举报

发表于 2014-11-14 11:14:13 | 显示全部楼层
挺好的,,,
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-5-2 12:13 , Processed in 0.025787 second(s), 16 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部