开发板:OKMX6UL-C/OKMX6ULL-S (Linux4.1.15)
矩阵键盘添加方法:
1、切换到root用户
whl@ubuntu:~/work/temp/linux-4.1.15$ sudo su root
2、设置环境变量
root@ubuntu:/home/whl/work/temp/linux-4.1.15# . /opt/fsl-imx-x11/4.1.15-2.0.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
3、打开配置菜单添加矩阵键盘配置
root@ubuntu:/home/whl/work/temp/linux-4.1.15# make menuconfig
->Device Drivers
->Input device support
->Keyboards
GPIO driven matrix keypad support
4、修改设备树,添加matrix-keypad节点(例如添加一个3x2的矩阵键盘)
matrix-keypad {
compatible = "gpio-matrix-keypad";
debounce-delay-ms = <5>;
col-scan-delay-us = <2>;
row-gpios = <&gpio3 23 0
&gpio3 24 0
&gpio3 25 0>;
col-gpios = <&gpio3 26 0
&gpio3 27 0>;
linux,keymap = <0x0000008B
0x0100009E
0x02000069
0x0001006A
0x0101001C
0x0201006C>;
};
其中linux,keymap属性中,每一组数据代表:行、列、键值
row << 24 | column << 16 | key-code
0x0000008B:代表0行,0列,键值139
0x0100009E:代表1行,0列,键值158
0x02000069:代表2行,0列,键值105
0x0001006A:代表0行,1列,键值106
0x0101001C:代表1行,1列,键值28
0x0201006C:代表2行,1列,键值108
5、重新编译内核和设备树,重新烧录
6、进入文件系统后,查看是否生成对应节点
从打印信息中可以看出有生成matrix-keypad设备和event0节点
7、测试
使用hexdump命令监控/dev/input/event0节点
/*按下时*/
hexdump序列号 秒 微妙 键盘事件 code=KEY_L value=1(按下)
0000010 6238 59c3 fe06 000e 0001 008b 0001 0000
/*抬起时*/
hexdump序列号 秒 微妙 键盘事件 code=KEY_L value=0(松开)
0000040 6238 59c3 41fe 0001 0001 008b 0000 0000
8、测试程序
#include <stdio.h>
#include <linux/input.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define DEV_PATH "/dev/input/event0"
int main()
{
int keys_fd;
struct input_event key_event;
keys_fd=open(DEV_PATH, O_RDONLY);
if(keys_fd <= 0)
{
printf("open /dev/input/event1 device error!\n");
return -1;
}
while(1)
{
if(read(keys_fd, &key_event, sizeof(key_event)) == sizeof(key_event))
{
if(key_event.type==EV_KEY)
if(key_event.value==0 || key_event.value==1)
{
printf("key %d %s \n",key_event.code,(key_event.value)?"Pressed":"Released");
}
}
}
close(keys_fd);
return 0;
}
input_event结构体,在input.h中有定义
struct input_event {
struct timeval time;
__u16 type;
__u16 code;
__s32 value;
};
运行结果
root@imx6ulevk:~# ./key_test
key 139 Pressed
key 139 Released
key 158 Pressed
key 158 Released
key 105 Pressed
key 105 Released
key 106 Pressed
key 106 Released
key 28 Pressed
key 28 Released
key 108 Pressed
key 108 Released