Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2969912
  • 博文数量: 685
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5303
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-19 14:17
个人简介

文章分类

全部博文(685)

文章存档

2015年(116)

2014年(569)

分类: 嵌入式

2014-10-22 09:08:23

原文地址:


内核下的 drivers/input/keyboard/gpio_keys.c实现了一个体系结构无关的GPIO按键驱动,使用此按键驱动,只需在arch/arm/mach-s3c2410/mach-smdk2410.c中定义相关的数据即可。驱动的实现非常简单,但是较适合于实现独立式按键驱动,且按键所接端口为中断引脚,如本书硬件平台矩阵按键对应的EINT0,EINT2,EINT11,EINT19中断对应的引脚。如果是矩阵按键,相应代码的改动较大,不宜提倡。

假设在S3C2410开发平台上, 使用GPG3,GPG11,GPF0,GPF2实现了DOWN、ENTER、HOME、POWER四个按键,该法实现驱动程序首先在头文件位置键入以下头文件:


01.#include
02.#include
03.#include //S3C2410各个端口定义
04.
05.然后在mach-smdk2410.c中键入按键的定义信息:
06.
07.static struct gpio_keys_button s3c2410_buttons [ ] = {
08.{
09..gpio = S3C2410_GPG3,
10..code = KEY_DOWN,
11..desc = "Down" ,
12..active_low = 1,
13.},
14.
15.{
16..gpio = S3C2410_GPG11,
17..code = KEY_ENTER,
18..desc = "Enter " ,
19..active_low = 1,
20.},
21.
22.{
23..gpio = S3C2410_GPF0,
24..code = KEY_HOME,
25..desc = "Home" ,
26..active_low = 1,
27.},
28.
29.{
30..gpio = S3C2410_GPF2,
31..code = KEY_POWER,
32..desc = "Power " ,
33..active_low = 1,
34.},
35.};
36.
37.static struct gpio_keys_platform_data s3c2410_button_data = {
38..buttons = s3c2410_buttons,
39..nbuttons = ARRAY_SIZE(s3c2410_buttons) ,
40.};
41.
42.static struct platform_device s3c2410_device_button = {
43. .name = "gpio-keys",
44. .id =-1,
45. .dev = {
46. .platform_data = &s3c2410_button_data,
47.}
48.};

其中
n gpio是连接按键的IO管脚。
n code是这个按键上报的键值, 在input.h中定义。
n desc是按键的name。
n active_low为1是表示低电平触发。
将“&s3c2410_device_button,”语句填入 struct platform_device *s3c2410_devices[]数组,作为该数组的一个成员。


static struct platform_device *smdk2410_devices[] __initdata = {
&s3c_device_usb,
&s3c_device_lcd,
&s3c_device_wdt,
&s3c_device_i2c,
&s3c_device_iis,
&s3c_device_rtc,
&s3c_device_ts,
&s3c2410_device_button,};

编译内核时选择:

Device Drivers >

Input device support >

[*] Keyboards

<*> GPIO Buttons

如果要修改按键对应的GPIO和键值,只需要简单的修改s3c2410_buttons[]数组中的内容。

这样在内核的启动过程中,会发现如下的提示:

input: gpio-keys as /class/input/input0

同时在文件系统dev目录下有event0设备节点,event1是触摸屏节点,对gpio-keys按键的访问可以通过event0来完成。


01.#include
02.#include
03.#include
04.#include
05.#include
06.#include
07.#include
08.int main(int argc, char **argv)
09.{
10. int key_state;
11. int fd;
12. int ret;
13. int code;
14. struct input_event buf;
15. int repeat_param[2];
16. fd = open("/dev/input/event0", O_RDONLY);
17. if (fd < 0)
18. {
19. printf("Open gpio-keys failed.\n");
20. return -1;
21. }
22. else
23. {
24. printf("Open gpio-keys success.\n");
25. }
26. repeat_param[0]=500;//ms重复按键第一次间隔
27. repeat_param[1]=66;//ms重复按键后续间隔
28. ret = ioctl(fd,EVIOCSREP,(int *)repeat_param);//设置重复按键参数
29. if(ret != 0)
30. {
31. printf("set repeat_param fail!\n");
32. }
33. else
34. {
35. printf("set repeat_param ok.\n");
36. }
37.
38. while(1)
39. {
40. ret = read(fd,&buf,sizeof(struct input_event));
41. if(ret <= 0)
42. {
43. printf("read fail!\n");
44. return -1;
45. }
46.
47. code = buf.code;
48. key_state = buf.value;
49. switch(code)
50. {
51. case KEY_DOWN:
52. code = '1';
53. break;
54. case KEY_ENTER:
55. code = '2';
56. break;
57. case KEY_HOME:
58. code = '3';
59. break;
60. case KEY_POWER:
61. code = '4';
62. break;
63. default:
64. code = 0;
65. break;
66. }
67.
68. if(code!=0)
69. {
70. printf("Key_%c state= %d.\n",code,key_state);
71. }
72. }
73. close(fd);
74. printf("Key test finished.\n");
75. return 0;
76.}

上述按键驱动涉及到内核的platform设备驱动模型相关知识,读者可自行参考相关内容。
阅读(1956) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~