个人能力有限,此博文只当记录实验过程,如有错误,请大家指正。
一、硬件连接
根据飞凌嵌入式的板子添加一个8位的旋转开关,需要8个gpio,所以去除SD3接口,改成GPIO。
需要修改的引脚为
SD3_DATA0
SD3_DATA1
SD3_DATA2
SD3_DATA3
SD3_DATA4
SD3_DATA5
SD3_CMD
SD3_CLK
查看电路图,可以知道这些都有外部上拉,去除上拉电阻,然后分别将各引脚拉到旋转开关上,旋转开关公共脚接地。
二、内核驱动
1、打开文件 imx6q-pinfunc.h
修改MX6QDL_PAD_SD3_DAT0__SD3_DATA0改成alt5,MX6QDL_PAD_SD3_DAT0__SD3_DATA1 改成alt5
#define MX6QDL_PAD_SD3_DAT0__SD3_DATA0 0x2c0 0x6a8 0x000 0x5 0x0
#define MX6QDL_PAD_SD3_DAT1__SD3_DATA1 0x2c4 0x6ac 0x000 0x5 0x0
2、打开文件 linux-4.1.15/arch/arm/boot/dts/imx6qdl-sabresd.dtsi
搜索 gpio-keys 添加 key-zero、key-one
gpio-keys {
compatible = "gpio-keys";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_gpio_keys>;
power {
label = "Power Button";
gpios = <&gpio3 29 GPIO_ACTIVE_LOW>;
gpio-key,wakeup;
linux,code =
};
volume-up {
label = "Volume Up";
gpios = <&gpio1 4 GPIO_ACTIVE_LOW>;
gpio-key,wakeup;
linux,code =
};
volume-down {
label = "Volume Down";
gpios = <&gpio1 5 GPIO_ACTIVE_LOW>;
gpio-key,wakeup;
linux,code =
};
key-zero {
label = "Key Zero";
gpios = <&gpio7 4 GPIO_ACTIVE_LOW>;
gpio-key,wakeup;
linux,code =
};
key-one {
label = "Key One";
gpios = <&gpio7 5 GPIO_ACTIVE_LOW>;
gpio-key,wakeup;
linux,code =
};
};
搜索pinctrl_gpio_keys,添加IOMUXC_SW_PAD_CTL_PAD_SD3_DATA0、 IOMUXC_SW_PAD_CTL_PAD_SD3_DATA1的寄存器配置
pinctrl_gpio_keys: gpio_keysgrp {
fsl,pins = <
MX6QDL_PAD_EIM_D29__GPIO3_IO29 0x1b0b0
MX6QDL_PAD_GPIO_4__GPIO1_IO04 0x1b0b0
MX6QDL_PAD_GPIO_5__GPIO1_IO05 0x1b0b0
MX6QDL_PAD_SD3_DAT0__SD3_DATA0 0x1b0b0
MX6QDL_PAD_SD3_DAT1__SD3_DATA1 0x1b0b0
>;
};
屏蔽原SD3作为SD卡应用的配置
//pinctrl_usdhc3: usdhc3grp {
// fsl,pins = <
// MX6QDL_PAD_SD3_CMD__SD3_CMD 0x17059
// MX6QDL_PAD_SD3_CLK__SD3_CLK 0x10059
// MX6QDL_PAD_SD3_DAT0__SD3_DATA0 0x17059
// MX6QDL_PAD_SD3_DAT1__SD3_DATA1 0x17059
// MX6QDL_PAD_SD3_DAT2__SD3_DATA2 0x17059
// MX6QDL_PAD_SD3_DAT3__SD3_DATA3 0x17059
// MX6QDL_PAD_SD3_DAT4__SD3_DATA4 0x17059
// MX6QDL_PAD_SD3_DAT5__SD3_DATA5 0x17059
// MX6QDL_PAD_SD3_DAT6__SD3_DATA6 0x17059
// MX6QDL_PAD_SD3_DAT7__SD3_DATA7 0x17059
// >;
//};
三、应用开发
进去系统打开节点
按键会有乱码出现,证明驱动已经生效。接下来就来写应用。
#include
#include
#include
#include
#include
#define NOKEY 0
int main()
{
int keys_fd;
char ret[2];
struct input_event t;
char *dev;
setvbuf(stdout, (char *)NULL, _IONBF, 0);//disable stdio out buffer;
// dev = getenv("KEYPAD_DEV");
keys_fd = open("/dev/input/event1", O_RDONLY);
if(keys_fd<=0)
{
printf("open %s device error!\n",dev);
return 0;
}
while(1)
{
if(read(keys_fd,&t,sizeof(t))==sizeof(t)) {
if(t.type==EV_KEY)
if(t.value==0 || t.value==1)
{
printf("%d \n", t. code);
switch(t.code)
{
case 158:
printf("key158 %s\n",(t.value)?"Presse":"Released");
break;
case 115:
printf("key115 %s\n",(t.value)?"Pressed":"Released");
break;
case 114:
printf("key114 %s\n",(t.value)?"pressed":"Released");
break;
case 102:
printf("key102 %s\n",(t.value)?"Pressed":"Released");
break;
case 116:
printf("key116 %s\n",(t.value)?"Released":"Pressed");
break;
//case 261:
// printf("key261 %s\n",(t.value)?"Released":"Pressed");
//break;
default:
break;
}
}
}
}
close(keys_fd);
return 0;
}
结果如下