linux字符設備按鍵驅動之防抖動

按鍵驅動程序抖動問題會造成多次中斷髮生,實則可能是一次按下或釋放的操作。本驅動程序就是在按鍵驅動程序(中斷方式)的基礎之上,用定時器來去抖動。
當一次按鍵按下的時候,可能產生多個脈衝,我們可以等到最後一個脈衝平穩時再真正地做按下或釋放的處理。所以,在中斷中我們可以不斷修改定時器的值,當最後穩定下來,沒有中斷產生了,就會調用超時函數,再在超時函數裏面判斷按鍵狀態,返回值給用戶空間等。
定時器的操作:
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. }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章