CubieBoard中文论坛

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

按键唤醒去抖动的问题

[复制链接]
发表于 2015-7-6 19:02:34 | 显示全部楼层 |阅读模式
  1. #include<linux/module.h>
  2. #include<linux/kernel.h>
  3. #include<linux/cdev.h>
  4. #include<linux/irq.h>
  5. #include<linux/slab.h>
  6. #include <linux/init.h>
  7. #include <mach/gpio.h>
  8. #include<linux/gpio.h>
  9. #include <linux/input.h>
  10. #include <linux/timer.h>
  11. #define KEY_MAJOR 233
  12. #define DEVICE_NAME "gpio_key"
  13. struct input_dev *buttons_dev;
  14. struct timer_list buttons_timer;
  15. struct work_struct* work;
  16. int irq;
  17. static volatile int status;
  18. static volatile int key_status;
  19. static void gpio_keys_report_event(void)
  20. {

  21.         status=gpio_get_value(GPIOG(0));
  22.         if(status==1){
  23.                 input_event(buttons_dev,EV_KEY,KEY_WAKEUP,!!status);
  24.                 input_sync(buttons_dev);
  25.                 printk("<0>""the value of pin is %d\n",status);
  26.         }
  27.         else  if(status==0){
  28.                 input_event(buttons_dev,EV_KEY,KEY_WAKEUP,!!status);
  29.                 input_sync(buttons_dev);
  30.                 printk("<0>""the value of pin2 is %d\n",status);
  31.         }

  32. }
  33. static void work_func(struct work_struct *work)
  34. {

  35.         int ret=0;
  36.         ret=mod_timer(&buttons_timer, jiffies+(HZ/50));
  37.         if (ret){
  38.                 printk("<0>""the mod time is on job with %d\n",ret);
  39.         }

  40. }

  41. static void gpio_keys_timer(unsigned long _data)
  42. {
  43.         gpio_keys_report_event();       
  44. }
  45. static irqreturn_t gpio_key_isr(int irq, void *dev_id)
  46. {
  47.         //        disable_irq_nosync(irq);
  48.         schedule_work(work);
  49.         //        enable_irq(irq);
  50.         return IRQ_HANDLED;
  51. }
  52. static const struct file_operations key_ops={
  53.         .owner =THIS_MODULE,
  54. };
  55. static int __init gpio_keys_init(void)
  56. {

  57.         int ret = 0;
  58.         int error;
  59.         ret = register_chrdev(KEY_MAJOR, DEVICE_NAME, &key_ops);
  60.         if (ret < 0) {
  61.                 printk("can't register gpio_keys_number\n");
  62.                 return -1;
  63.         }
  64.         buttons_dev = input_allocate_device();

  65.         buttons_dev->name="keyboard";
  66.         set_bit(EV_KEY,buttons_dev->evbit);

  67.         set_bit(EV_REP,buttons_dev->evbit);

  68.         set_bit(KEY_WAKEUP,buttons_dev->keybit);
  69.         ret=gpio_is_valid(GPIOG(0));      //判断gpio是否合法
  70.         printk("<0>""the valid value is %d\n",ret);
  71.         if(gpio_request(GPIOG(0),NULL)){
  72.                 printk("<0>""request gpio error\n");
  73.         }
  74.         gpio_direction_input(GPIOG(0));    //把pg0设置为输入
  75.         irq=gpio_to_irq(GPIOG(0));        //gpio实现虚拟irq
  76.         printk("<0>""the irq value is %d\n",irq);
  77.         error=request_irq(irq,gpio_key_isr,IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING,"KEY_111",NULL); //上升沿下降沿触发
  78.         if(error){
  79.                 printk("<0>""request irq error\n");
  80.         }
  81.         printk("<0>""the value of error is %d\n",error);
  82.         work=kmalloc(sizeof(struct work_struct),GFP_KERNEL);
  83.         INIT_WORK(work, work_func);
  84.         init_timer(&buttons_timer);
  85.         buttons_timer.function=gpio_keys_timer;
  86.         add_timer(&buttons_timer);
  87.         //        key_status=KEYS_UP;

  88.         ret = input_register_device(buttons_dev);

  89.         if(ret) {

  90.                 printk("unable to register input device\n");

  91.                 return -1;
  92.         }
  93.         printk("gpio_keys init\n");
  94.         return 0;
  95. }
  96. static void __exit gpio_keys_exit(void)
  97. {
  98.         free_irq(irq,NULL);
  99.         gpio_free(GPIOG(0));
  100.         del_timer(&buttons_timer);
  101.         unregister_chrdev(KEY_MAJOR, DEVICE_NAME);

  102. }
  103. module_init(gpio_keys_init);
  104. module_exit(gpio_keys_exit);
  105. MODULE_LICENSE("GPL");

复制代码
我做了一个按键唤醒功能的驱动,用的是定时器,上升沿下降沿触发,但是发现还是消不了抖动,请大家帮我看看问题,还有我这个功能是按一下屏幕打开,再按一下屏幕关闭,怎么改成上升沿点亮,下降沿灭掉。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 20:47 , Processed in 0.017728 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部