概述
采用Linux自带的按键驱动/drivers/input/keyboard/gpio_keys.c,实现按键功能。
gpio_keys.c实现了一个体系结构无关的GPIO按键驱动,使用此按键驱动,只需在相应的设备树定义相关的数据即可。
gpio-keys是基于input架构实现的一个通用GPIO按键驱动。该驱动基于platform_driver架构。
gpio-keys 功能已实现 ,设备中加入gpio按键功能主要修改两部分
1.修改设备树文件
打开/arch/arm/boot/dts/hi3516dv300.dtsi
增加gpio_key节点
gpio_keys {
compatible = "gpio-keys";
label = "gpio-keys";
recover{
label = "recover";
linux,code = <115>;
gpios = <&gpio_chip8 1 0>; /* GPIO8_1 */
debounce-interval = <10>;
};
};
以上属性的可以在\Documentation\devicetree\bindings\input\gpio-keys.txt中了解属性的含义
2. 修改config
打开/drivers/input/keyboard/Makefile可找到obj-$(CONFIG_KEYBOARD_GPIO) += gpio_keys.o
需要使能CONFIG_KEYBOARD_GPIO=y
编译下载后可以通过查看 /dev/input是否存在event节点
3. 应用层调用
#define KEY_CODE 115 /* dts 中 Linux, code 定义值*/
void read_key_event(void)
{
int fd = -1, ret = -1;
int len = 0
struct input_event input_ev;
fd = open("/dev/input/event0", O_RDONLY);
if (fd < 0)
{
printf("open failed.\n");
return;
}
len = sizeof(input_ev)
memset(&input_ev, 0, len);
ret = read(fd, &input_ev, len);
if (ret != len)
{
printf("read key event failed.\n");
close(fd);
}
if ((KEY_CODE == ev.code) && (0 == ev.value))
{
printf("key press.\n");
}
else
{
printf("key release.\n");
}
close(fd);
}