一、kernel dts 配置修改:
diff --git a/sysdrv/source/kernel/arch/arm/boot/dts/rv1106g-evb1-v11.dts b/sysdrv/source/kernel/arch/arm/boot/dts/rv1106g-evb1-v11.dts
index d0d059a3b..584f3a00a 100755
--- a/sysdrv/source/kernel/arch/arm/boot/dts/rv1106g-evb1-v11.dts
+++ b/sysdrv/source/kernel/arch/arm/boot/dts/rv1106g-evb1-v11.dts
@@ -11,7 +11,19 @@
model = "Rockchip RV1106G EVB1 V11 Board";
compatible = "rockchip,rv1106g-evb1-v11", "rockchip,rv1106";
-
+ gpio-keys {
+ compatible = "gpio-keys";
+ pinctrl-names = "default";
+ pinctrl-0 = <&pinctrl_gpio_keys>;
+
+ button@1 {
+ label = "KEY1";
+ linux,code = <KEY_ENTER>;
+ gpios = <&gpio1 RK_PD3 GPIO_ACTIVE_LOW>;
+ };
+
+ };
+
leds {
compatible = "gpio-leds";
led_green {
@@ -52,6 +64,12 @@
&pinctrl {
+ pinctrl_gpio_keys {
+ pinctrl_gpio_keys: pinctrl-gpio-keys {
+ rockchip,pins = <1 RK_PD3 RK_FUNC_GPIO &pcfg_pull_up>;
+ };
+ };
+
led_green_gpio {
led_green_gpio: led-green-gpio {
rockchip,pins = <1 RK_PC4 RK_FUNC_GPIO &pcfg_pull_up>;
diff --git a/sysdrv/source/kernel/arch/arm/configs/rv1106-evb.config b/sysdrv/source/kernel/arch/arm/configs/rv1106-evb.config
index f7ba90247..dbb2ae82d 100755
--- a/sysdrv/source/kernel/arch/arm/configs/rv1106-evb.config
+++ b/sysdrv/source/kernel/arch/arm/configs/rv1106-evb.config
@@ -51,6 +51,7 @@ CONFIG_WIRELESS=y
CONFIG_WLAN=y
CONFIG_AIC_WLAN_SUPPORT=y
CONFIG_AIC8800_WLAN_SUPPORT=m
+CONFIG_KEYBOARD_GPIO=y
# CONFIG_6LOWPAN is not set
# CONFIG_AD2S1200 is not set
# CONFIG_AD2S1210 is not set
@@ -508,7 +509,6 @@ CONFIG_KEYBOARD_ADC=y
# CONFIG_KEYBOARD_BCM is not set
# CONFIG_KEYBOARD_CAP11XX is not set
# CONFIG_KEYBOARD_DLINK_DIR685 is not set
-# CONFIG_KEYBOARD_GPIO is not set
# CONFIG_KEYBOARD_GPIO_POLLED is not set
# CONFIG_KEYBOARD_LKKBD is not set
# CONFIG_KEYBOARD_LM8333 is not set
二、测试代码
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <linux/input.h>
#define DEVICE "/dev/input/event0" // 替换X为实际的设备编号
void handle_key_event(int fd) {
struct input_event ev;
if (read(fd, &ev, sizeof(struct input_event)) != sizeof(struct input_event)) {
perror("Error reading from input device");
return;
}
if (ev.type == EV_KEY) {
printf("Key code:%d has been %s\n", ev.code,ev.value == 1 ? "pressed" : "released");
}
}
int main() {
int fd, ret;
fd_set readfds;
struct timeval timeout;
// 打开输入设备
fd = open(DEVICE, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
perror("Cannot open input device");
return 1;
}
printf("Waiting for key events on %s (press Ctrl+C to exit) KEY_ENTER=%d\n", DEVICE,KEY_ENTER);
while (1) {
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
// 设置超时时间,这里设置为5秒
timeout.tv_sec = 5;
timeout.tv_usec = 0;
// 等待按键事件
ret = select(fd + 1, &readfds, NULL, NULL, &timeout);
if (ret == -1) {
perror("Select failed");
break;
} else if (ret == 0) {
printf("Select timed out!\n");
continue;
}
if (FD_ISSET(fd, &readfds)) {
handle_key_event(fd);
}
}
close(fd);
return 0;
}
三、查看/proc/bus/input/devices设备
四、运行效果
五、参考文章
GPIO-KEY的实现原理及使用方法-CSDN博客
MX6ULL学习笔记(十三)Linux 自带按键驱动程序_linux自带按键驱动程序-CSDN博客
Linux 输入设备 自定义键盘 input输入子系统 gpio-keys按键驱动_label = "key-ec11-CSDN博客