按键驱动程序抖动问题会造成多次中断发生,实则可能是一次按下或释放的操作。本驱动程序就是在按键驱动程序(中断方式) 的基础之上,用定时器来去抖动。
当一次按键按下的时候,可能产生多个脉冲,我们可以等到最后一个脉冲平稳时再真正地做按下或释放的处理。所以,在中断中我们可以不断修改定时器的值,当最后稳定下来,没有中断产生了,就会调用超时函数,再在超时函数里面判断按键状态,返回值给用户空间等。
定时器的操作:
1.定义timer_list结构体。
2.定义超时函数。
3.在初始化函数绑定定时器结构体和超时函数,初始化定时器结构体等操作。用到
#define setup_timer(timer, fn, data)     \
 do {        \
  static struct lock_class_key __key;   \
  setup_timer_key((timer), #timer, &__key, (fn), (data));\
 } while (0)
这个是在2.6.32内核里面进行的初始化设置,在低版本中可能包括init和add等操作。
4.在中断函数里修改定时器。用到
int mod_timer(struct timer_list *timer, unsigned long expires)
详细驱动程序如下:

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/delay.h>
  6. #include <linux/irq.h>
  7. #include <asm/uaccess.h>
  8. #include <linux/cdev.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/device.h>
  11. #include <linux/sched.h>
  12. #include <linux/gpio.h>

  13. #define SEVENTHDEV MKDEV(250, 0)
  14. struct cdev seventhdrv_cdev;

  15. static struct class *seventhdrv_class;

  16. struct button {
  17.     int irq;
  18.     char *name;
  19.     int pin;
  20.     int val;
  21. };

  22. static volatile int pressed = 0;
  23. static unsigned char key_val;

  24. struct fasync_struct *button_async;
  25. static struct timer_list seventh_timer;
  26. static struct button *buttonp = NULL;
  27. DECLARE_WAIT_QUEUE_HEAD(button_wqh);

  28. /* 六个按键的相关定义整合到结构体 */
  29. static struct button buttons[6] = {
  30.         {IRQ_EINT8, "K1", S3C2410_GPG(0), 0x1},
  31.         {IRQ_EINT11, "K2", S3C2410_GPG(3), 0x2},
  32.         {IRQ_EINT13, "K3", S3C2410_GPG(5), 0x3},
  33.         {IRQ_EINT14, "K4", S3C2410_GPG(6), 0x4},
  34.         {IRQ_EINT15, "K5", S3C2410_GPG(7), 0x5},
  35.         {IRQ_EINT19, "K6", S3C2410_GPG(11),0x6},
  36. };


  37. static DECLARE_MUTEX(button_lock);

  38. /* 中断处理函数 */
  39. static irqreturn_t seventhdrv_intr(int irq, void *data)
  40. {
  41.     buttonp = (struct button*)data;
  42.     mod_timer(&seventh_timer, jiffies+HZ/100);
  43.     return IRQ_RETVAL(IRQ_HANDLED);
  44. }

  45. static int seventhdrv_open(struct inode * inode, struct file * file)
  46. {
  47.     int i=6;
  48.      if (file->f_flags & O_NONBLOCK) {
  49.         if(down_trylock(&button_lock))
  50.             return -EBUSY;
  51.         } else {
  52.            down(&button_lock);
  53.         }
  54.     while(i--){
  55.         request_irq(buttons[i].irq, &seventhdrv_intr, IRQ_TYPE_EDGE_BOTH,
  56.                     buttons[i].name, &buttons[i]);
  57.     }
  58.     return 0;
  59.     
  60. }

  61. static ssize_t seventhdrv_read(struct file *file, char __user *user, size_t size,loff_t*o)
  62. {
  63.     int sz = sizeof(key_val) ;
  64.     
  65.     if (sz != size) {
  66.         return -EINVAL;
  67.     }
  68.     
  69.     if (file->f_flags & O_NONBLOCK)
  70.     {
  71.         if (!pressed)
  72.             return -EAGAIN;
  73.     }
  74.     else
  75.     {
  76.         /* 如果没有按键动作, 休眠 */
  77.         wait_event_interruptible(button_wqh, pressed);
  78.     }
  79.     copy_to_user(user, &key_val, sz);

  80.     /* 重新清除按下标志 */
  81.     pressed = 0;

  82.     return sz;
  83. }

  84. static int seventhdrv_close(struct inode *inode, struct file *file)
  85. {
  86.     int i=6;
  87.     
  88.     while(i--) {
  89.         free_irq(buttons[i].irq, &buttons[i]);
  90.     }
  91.     up(&button_lock);
  92.     return 0;
  93. }

  94. static struct file_operations seventhdrv_ops = {
  95.     .owner = THIS_MODULE,
  96.     .open = seventhdrv_open,
  97.     .read = seventhdrv_read,
  98.     .release = seventhdrv_close,
  99. };

  100. static void button_timer(unsigned long data)
  101. {
  102.     int val;
  103.     if (buttonp == NULL) {
  104.         printk(KERN_INFO "No button pressed!\n");
  105.         return ;
  106.     }
  107.     
  108.     val = s3c2410_gpio_getpin(buttonp->pin);
  109.     
  110.     if (!val) {/* 按下按键*/
  111.         key_val = buttonp->val;    
  112.     } else { /* 释放按键*/
  113.         key_val = buttonp->val | 0x10; //将第4位置1,做标记
  114.     }

  115.     pressed = 1; //此处改变按下标志,以使队列不继续睡眠
  116.     wake_up_interruptible(&button_wqh);

  117.     buttonp = NULL;
  118. }

  119. static int seventhdrv_init(void)
  120. {
  121.     int ret;
  122.     int devt = SEVENTHDEV;
  123.     
  124.     ret = register_chrdev_region(devt, 1, "seventhdrv");
  125.     if (ret) {
  126.         printk(KERN_ERR "Unable to register minors for seventhdrv\n");
  127.         goto fail;
  128.     }
  129.     seventhdrv_class = class_create(THIS_MODULE, "seventhdrv_class");
  130.     if (IS_ERR(seventhdrv_class)) {
  131.         printk(KERN_ERR "can't register device class\n");
  132.         return PTR_ERR(seventhdrv_class);
  133.     }
  134.     device_create(seventhdrv_class, NULL, devt, NULL, "buttons");
  135.     cdev_init(&seventhdrv_cdev, &seventhdrv_ops);
  136.     ret = cdev_add(&seventhdrv_cdev, devt, 1);
  137.     if (ret < 0)
  138.         goto fail_cdev;
  139.     setup_timer(&seventh_timer, button_timer, (unsigned long)&buttons);
  140.     return 0;
  141.     
  142. fail_cdev:
  143.     class_unregister(seventhdrv_class);
  144.     device_destroy(seventhdrv_class, devt);
  145.     cdev_del(&seventhdrv_cdev);
  146. fail:
  147.     unregister_chrdev_region(devt, 1);
  148.     
  149.     return 0;
  150. }

  151. static void seventhdrv_exit(void)
  152. {
  153.     class_unregister(seventhdrv_class);
  154.     device_destroy(seventhdrv_class, SEVENTHDEV);
  155.     cdev_del(&seventhdrv_cdev);
  156.     unregister_chrdev_region(SEVENTHDEV, 1);
  157. }

  158. module_init(seventhdrv_init);
  159. module_exit(seventhdrv_exit);
  160. MODULE_LICENSE("GPL");
应用程序为:

点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <signal.h>

  6. int main(int argc, char *argv[])
  7. {
  8.     int fd, cnt=1;
  9.     unsigned char val;
  10.     
  11.     fd = open("/dev/buttons", O_RDWR);
  12.     
  13.     if (fd < 0)
  14.     {
  15.         printf("Can not open device\n");
  16.         return -1;
  17.     }

  18.     while(1)
  19.     {
  20.         read(fd, &val, sizeof(unsigned char));
  21.         printf("cnt:%d, val:0x%x\n", cnt++, val);
  22.     }
  23.     
  24.     return 0;
  25. }

Logo

更多推荐