Hi3516DV300 linux驱动之按键输入

概述

采用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);

}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值