目录
目录
前言
DTS配置的参考
GPIO按键中断的DTS配置参考
GPIO按键轮询的DTS配置参考
内核配置的参考
GPIO按键中断的内核配置参考
GPIO按键轮询的内核配置参考
验证测试的参考
GPIO按键中断的验证测试参考
GPIO按键轮询的验证测试参考
总结
前言
GPIO-KEYS主要有两种实现方式:GPIO按键中断和GPIO按键轮询。
其中Linux内核下的`linux-x.xx/drivers/input/keyboard/gpio_keys.c`已经实现了与体系结构无关的GPIO按键中断驱动,Linux内核下的`linux-x.xx/drivers/input/keyboard/gpio_keys_polled.c`已经实现了与体系结构无关的GPIO按键轮询驱动。
使用GPIO-KEYS按键驱动,只需在设备树gpio-keys或gpio-key-polled节点添加需要的按键子节点即可,但主要适合于独立式按键。
注意:Linux内核原生的GPIO按键中断驱动只适用于支持外部中断的GPIO,而GPIO按键轮询驱动适用于支持输入功能的GPIO。
DTS配置的参考
GPIO按键中断的DTS配置参考
... #include "../../../../../../include/uapi/linux/input-event-codes.h" /* 键值定义 */ ... gpio-keys { compatible = "gpio-keys"; /* 该属性定义了设备的兼容性 */ pinctrl-names = "default"; pinctrl-0 = <&pinctrl_gpio_key_up>, <&pinctrl_gpio_key_down>, <&pinctrl_gpio_key_left>, <&pinctrl_gpio_key_right>, <&pinctrl_gpio_key_esc>, <&pinctrl_gpio_key_enter>; autorepeat; /* 若需要支持连按,可以添加autorepeat */ key_up { gpios = <&gpiod 8 GPIO_ACTIVE_LOW>; /* gpios表示GPIO管脚属性。 */ lable = "GPIO Key Up"; /* key的描述性名称。 */ linux,input-type = <1>; /* input-type表示输入事件类型,1表示键盘或按键事件。 */ linux,code = <KEY_UP>; /* code表示键值。 */ debounce-interval = <100>; /* key或button的消抖间隔时间,单位:ms。 */ }; key_down { gpios = <&gpiod 9 GPIO_ACTIVE_LOW>; lable = "GPIO Key Down"; linux,input-type = <1>; linux,code = <KEY_DOWN>; debounce-interval = <100>; }; key_left { gpios = <&gpiod 10 GPIO_ACTIVE_LOW>; lable = "GPIO Key Left"; linux,input-type = <1>; linux,code = <KEY_LEFT>; debounce-interval = <100>; }; key_right { gpios = <&gpiod 11 GPIO_ACTIVE_LOW>; lable = "GPIO Key Right"; linux,input-type = <1>; linux,code = <KEY_RIGHT>; debounce-interval = <100>; }; key_esc { gpios = <&gpiod 12 GPIO_ACTIVE_LOW>; lable = "GPIO Key Esc"; linux,input-type = <1>; linux,code = <KEY_ESC>; debounce-interval = <100>; }; key_enter { gpios = <&gpiod 13 GPIO_ACTIVE_LOW>; lable = "GPIO Key Enter"; linux,input-type = <1>; linux,code = <KEY_ENTER>; debounce-interval = <100>; }; }; pinctrl: pinctrl@xxxxxxxx { pinctrl_gpio_key_up: key_up_grp { xxx,pins = "gpiod-8"; xxx,pin-pud = <ENABLE_PULL_UP>; }; pinctrl_gpio_key_down: key_down_grp { xxx,pins = "gpiod-9"; xxx,pin-pud = <ENABLE_PULL_UP>; }; pinctrl_gpio_key_left: key_left_grp { xxx,pins = "gpiod-10"; xxx,pin-pud = <ENABLE_PULL_UP>; }; pinctrl_gpio_key_right: key_right_grp { xxx,pins = "gpd-11"; xxx,pin-pud = <ENABLE_PULL_UP>; }; pinctrl_gpio_key_esc: key_esc_grp { xxx,pins = "gpiod-12"; xxx,pin-pud = <ENABLE_PULL_UP>; }; pinctrl_gpio_key_enter: key_enter_grp { xxx,pins = "gpiod-13"; xxx,pin-pud = <ENABLE_PULL_UP>; }; };GPIO按键轮询的DTS配置参考
... #include "../../../../../../include/uapi/linux/input-event-codes.h" /* 键值定义 */ ... gpio-keys-polled { compatible = "gpio-keys-polled"; /* 该属性定义了设备的兼容性 */ poll-interval = <100>; /* 轮询周期,单位ms。 */ autorepeat; /* 若需要支持连按,可以添加autorepeat。 */ status = "okay"; /* 状态属性。 */ pol_key { label = "GPIO Key POL"; /* key的描述性名称。 */ linux,code = <KEY_ENTER>; /* code表示键值。 */ gpios = <&pio 0 GPIO_ACTIVE_LOW>; /* gpios表示GPIO管脚属性。 */ }; };内核配置的参考
GPIO按键中断的内核配置参考
CONFIG_INPUT_KEYBOARD=y CONFIG_KEYBOARD_GPIO=yGPIO按键轮询的内核配置参考
CONFIG_INPUT_KEYBOARD=y CONFIG_KEYBOARD_GPIO_POLLED=y验证测试的参考
GPIO按键中断的验证测试参考
1. 在系统内核启动和驱动加载后,查看内核中已经注册的INPUT设备。例如:
# cat /proc/bus/input/devices I: Bus=0019 Vendor=0001 Product=0001 Version=0100 N: Name="gpio-keys" P: Phys=gpio-keys/input0 S: Sysfs=/devices/platform/gpio-keys/input/input4 U: Uniq= H: Handlers=event4 (可以看出是设备节点为/dev/input/event4) B: PROP=0 B: EV=100003 B: KEY=1680 0 0 100000022. 查看GPIO按键的input设备节点的键值上报情况。例如:
# hexdump -d /dev/input/event4 (“-d”:以双字节十进制的形式显示。) 0000000 03304 25737 26302 00005 00001 00103 00001 00000 (KEY_UP按键按下) 0000010 03304 25737 26302 00005 00000 00000 00000 00000 0000020 03304 25737 45263 00007 00001 00103 00000 00000 (KEY_UP按键弹起) 0000030 03304 25737 45263 00007 00000 00000 00000 00000 ... 0000180 03435 25737 35243 00007 00001 00108 00001 00000 (KEY_DOWN按键按下) 0000190 03435 25737 35243 00007 00000 00000 00000 00000 00001a0 03435 25737 23097 00011 00001 00108 00000 00000 (KEY_DOWN按键弹起) 00001b0 03435 25737 23097 00011 00000 00000 00000 00000 ... 0000700 03534 25737 26306 00005 00001 00105 00001 00000 (KEY_LEFT按键按下) 0000710 03534 25737 26306 00005 00000 00000 00000 00000 0000720 03534 25737 59704 00008 00001 00105 00000 00000 (KEY_LEFT按键弹起) 0000730 03534 25737 59704 00008 00000 00000 00000 00000 ... 00008a0 03593 25737 37561 00012 00001 00106 00001 00000 (KEY_RIGHT按键按下) 00008b0 03593 25737 37561 00012 00000 00000 00000 00000 00008c0 03594 25737 08459 00001 00001 00106 00000 00000 (KEY_RIGHT按键弹起) 00008d0 03594 25737 08459 00001 00000 00000 00000 00000 ... 0000a20 03726 25737 07377 00003 00001 00001 00001 00000 (KEY_ESC按键按下) 0000a30 03726 25737 07377 00003 00000 00000 00000 00000 0000a40 03726 25737 40777 00006 00001 00001 00000 00000 (KEY_ESC按键弹起) 0000a50 03726 25737 40777 00006 00000 00000 00000 00000 ... 0000a60 03770 25737 37531 00012 00001 00028 00001 00000 (KEY_ENTER按键按下) 0000a70 03770 25737 37531 00012 00000 00000 00000 00000 0000a80 03771 25737 33986 00000 00001 00028 00000 00000 (KEY_ENTER按键弹起) 0000a90 03771 25737 33986 00000 00000 00000 00000 00000GPIO按键轮询的验证测试参考
1. 在系统内核启动和驱动加载后,查看内核中已经注册的INPUT设备。例如:
# cat /proc/bus/input/devices ... I: Bus=0019 Vendor=0001 Product=0001 Version=0100 N: Name="gpio-keys-polled" P: Phys=gpio-keys-polled/input0 S: Sysfs=/devices/platform/gpio-keys-polled/input/input1 U: Uniq= H: Handlers=event1 B: PROP=0 B: EV=100003 B: KEY=10000000 ...2. 查看GPIO按键轮询的input设备节点的键值上报情况。例如:
# hexdump -d /dev/input/event1 (“-d”:以双字节十进制的形式显示。) 0000000 01853 25737 55244 00011 00001 00028 00001 00000 (KEY_ENTER按键按下) 0000010 01853 25737 55244 00011 00000 00000 00000 00000 0000020 01853 25737 13100 00015 00001 00028 00000 00000 (KEY_ENTER按键弹起) 0000030 01853 25737 13100 00015 00000 00000 00000 00000总结
GPIO按键是嵌入式系统中常见的输入设备,用于实现人机交互。按键通过GPIO(通用输入输出)引脚检测状态变化,但机械按键在按下或释放时会产生电平抖动,需要通过软硬件方法消除抖动以确保信号稳定。