Chinaunix首页 | 论坛 | 博客
  • 博客访问: 466045
  • 博文数量: 100
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 955
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-21 09:30
文章分类

全部博文(100)

文章存档

2017年(1)

2016年(16)

2015年(83)

我的朋友

分类: 嵌入式

2015-07-22 18:01:00

button_drv.c:

点击(此处)折叠或打开

  1. /*
  2.  * linux/drivers/char/buttons.c
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License version 2 as
  6.  * published by the Free Software Foundation.
  7.  */

  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/fs.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/poll.h>
  14. #include <linux/irq.h>
  15. #include <asm/irq.h>
  16. #include <asm/io.h>
  17. #include <linux/interrupt.h>
  18. #include <asm/uaccess.h>
  19. #include <mach/hardware.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/cdev.h>
  22. #include <linux/miscdevice.h>

  23. #include <mach/map.h>
  24. #include <mach/gpio.h>
  25. #include <mach/regs-clock.h>
  26. #include <mach/regs-gpio.h>

  27. #include <linux/gpio.h>//gpio结构体的定义
  28. #include <linux/sched.h>//
  29. #include <linux/input.h>
  30. // #include <linux/wait.h>

  31. #define DEVICE_NAME        "buttons"

  32. struct button_desc {
  33.     int gpio;
  34.     int number;
  35.     char *name;    
  36.     unsigned int key_val;
  37.     struct timer_list timer;
  38. };

  39. static struct button_desc buttons[] = {
  40. /*
  41.     { S5PV210_GPH0(0), 0, "KEY0", .timer_list, KEY_A },
  42.     { S5PV210_GPH0(1), 1, "KEY1", .timer_list, KEY_B },
  43.     { S5PV210_GPH0(2), 2, "KEY2", .timer_list, KEY_C },
  44.     { S5PV210_GPH0(3), 3, "KEY3", .timer_list, KEY_D },
  45.     { S5PV210_GPH0(4), 4, "KEY4", .timer_list, KEY_F },
  46.     { S5PV210_GPH0(5), 5, "KEY5", .timer_list, KEY_G },
  47.     { S5PV210_GPH2(6), 6, "KEY6", .timer_list, KEY_H },
  48.     { S5PV210_GPH2(7), 7, "KEY7", .timer_list, KEY_I },
  49.     */
  50.     { S5PV210_GPH0(0), 0, "KEY0", KEY_A },
  51.     { S5PV210_GPH0(1), 1, "KEY1", KEY_B },
  52.     { S5PV210_GPH0(2), 2, "KEY2", KEY_C },
  53.     { S5PV210_GPH0(3), 3, "KEY3", KEY_D },
  54.     { S5PV210_GPH0(4), 4, "KEY4", KEY_E },
  55.     { S5PV210_GPH0(5), 5, "KEY5", KEY_F },
  56.     { S5PV210_GPH2(6), 6, "KEY6", KEY_G },
  57.     { S5PV210_GPH2(7), 7, "KEY7", KEY_H },
  58. };

  59. static volatile char key_values[] = {
  60.     '0', '0', '0', '0', '0', '0', '0', '0'
  61. };

  62. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

  63. static volatile int ev_press = 0;

  64. static struct input_dev *buttons_dev;

  65. //使用定时器消抖
  66. static void buttons_timer(unsigned long _data)
  67. {
  68.     struct button_desc *bdata = (struct button_desc *)_data;
  69.     int down;
  70.     int number;
  71.     unsigned tmp;

  72.     tmp = gpio_get_value(bdata->gpio);

  73.     /* active low */
  74.     down = !tmp;
  75.     printk("KEY %d: %01d\n", bdata->number, down);
  76.     
  77.     number = bdata->number;
  78.     if (down != (key_values[number] & 1)) {//down为1,按下,如果down!=键值&1,表示键值有变化,更新键值(置1);down为0,弹起,比较旧键值,不同则更新键值
  79.         //可起到进一步过滤无效按键数据的作用
  80.         key_values[number] = '0' + down;
  81.         if ( tmp)//这里判断条件不能弄错了,tmp为1表示松开。如果搞反了,会导致内核误认为是连击,一直上报事件。
  82.         {
  83.             /* 松开 : 最后一个参数: 0-松开, 1-按下 */
  84.             input_event(buttons_dev, EV_KEY, buttons->key_val, 0);
  85.             input_sync(buttons_dev);
  86.         }
  87.         else
  88.         {
  89.             /* 按下 */
  90.             input_event(buttons_dev, EV_KEY, buttons->key_val, 1);
  91.             input_sync(buttons_dev);
  92.         }
  93.     }
  94. }

  95. //按键中断
  96. static irqreturn_t button_interrupt(int irq, void *dev_id)
  97. {
  98.     struct button_desc *bdata = (struct button_desc *)dev_id;

  99.     mod_timer(&bdata->timer, jiffies + msecs_to_jiffies(40));

  100.     return IRQ_HANDLED; // return 1
  101. }

  102. static int __init button_dev_init(void)
  103. {
  104.     int irq;
  105.     int i;
  106.     int err = 0;
  107.     /* 1. 分配一个input_dev结构体 */
  108.     buttons_dev = input_allocate_device();;
  109.     buttons_dev->name = "button_input_device";
  110.     /* 2. 设置 */
  111.     /* 2.1 能产生哪类事件 */
  112.     set_bit(EV_KEY, buttons_dev->evbit);//按键
  113.     set_bit(EV_REP, buttons_dev->evbit);//可重复
  114.     
  115.     /* 2.2 能产生这类操作里的哪些事件: L,S,ENTER,LEFTSHIT */
  116.     set_bit(KEY_A, buttons_dev->keybit);
  117.     set_bit(KEY_B, buttons_dev->keybit);
  118.     set_bit(KEY_C, buttons_dev->keybit);
  119.     set_bit(KEY_D, buttons_dev->keybit);
  120.     set_bit(KEY_E, buttons_dev->keybit);
  121.     set_bit(KEY_F, buttons_dev->keybit);
  122.     set_bit(KEY_G, buttons_dev->keybit);
  123.     set_bit(KEY_H, buttons_dev->keybit);    
  124.     /* 3. 注册 */
  125.     err = input_register_device(buttons_dev);
  126.     if (err) {
  127.         printk(DEVICE_NAME"\t input_register_device err\n");
  128.         input_unregister_device(buttons_dev);
  129.         input_free_device(buttons_dev);        
  130.         return -EBUSY;
  131.     }    
  132.     //4. 硬件相关操作,注册8个中断和定时器
  133.     for (i = 0; i < ARRAY_SIZE(buttons); i++) {
  134.         if (!buttons[i].gpio)
  135.             continue;
  136.         //初始化定时器,setup_timer 内部调用了init_timer
  137.         setup_timer(&buttons[i].timer, buttons_timer,
  138.                 (unsigned long)&buttons[i]);
  139.         //从io获取中断信号线
  140.         irq = gpio_to_irq(buttons[i].gpio);
  141.         //申请按键中断, 边沿触发中断
  142.         err = request_irq(irq, button_interrupt, IRQ_TYPE_EDGE_BOTH,
  143.                 buttons[i].name, (void *)&buttons[i]);
  144.         if (err)
  145.             break;
  146.     }

  147.     if (err) {//失败则倒序注销资源
  148.         i--;
  149.         for (; i >= 0; i--) {
  150.             if (!buttons[i].gpio)
  151.                 continue;

  152.             irq = gpio_to_irq(buttons[i].gpio);
  153.             disable_irq(irq);
  154.             free_irq(irq, (void *)&buttons[i]);

  155.             del_timer_sync(&buttons[i].timer);
  156.         }

  157.         return -EBUSY;
  158.     }

  159.     printk(DEVICE_NAME"\tinitialized\n");

  160.     return err;
  161. }

  162. static void __exit button_dev_exit(void)
  163. {
  164.     int irq, i;

  165.     for (i = 0; i < ARRAY_SIZE(buttons); i++) {
  166.         if (!buttons[i].gpio)
  167.             continue;

  168.         irq = gpio_to_irq(buttons[i].gpio);
  169.         disable_irq(irq);
  170.         free_irq(irq, (void *)&buttons[i]);

  171.         del_timer_sync(&buttons[i].timer);
  172.     }
  173.     input_unregister_device(buttons_dev);
  174.     input_free_device(buttons_dev);
  175. }

  176. module_init(button_dev_init);
  177. module_exit(button_dev_exit);

  178. MODULE_LICENSE("GPL");
  179. MODULE_AUTHOR("GEC Inc.");
测试:
 hexdump /dev/event0  | tail -n +10


event0数据格式:
hexdump /dev/event1  (open(/dev/event1), read(), )
                   秒        微秒    类  code    value
004ceb0 0111 3901 26fa 0005 0000 0000 0001 0000
004cec0 0111 3901 afb9 0005 0001 001e 0002 0000
004ced0 0111 3901 afb9 0005 0000 0000 0001 0000
004cee0 0111 3901 386f 0006 0001 001e 0002 0000
004cef0 0111 3901 386f 0006 0000 0000 0001 0000
004cf00 0111 3901 c124 0006 0001 001e 0002 0000
004cf10 0111 3901 c124 0006 0000 0000 0001 0000





阅读(1278) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~