ARM Linux 驱动Input子系统之按键驱动测试

上一篇已经谈过,在现内核的中引入设备树之后对于内核驱动的编写,主要集中在硬件接口的配置上了即xxxx.dts文件的编写。

在自己的开发板上移植按键驱动:

1、根据开发板的原理图

ARM Linux 驱动Input子系统之按键驱动测试

ARM Linux 驱动Input子系统之按键驱动测试

确定按键的硬件接口为:GPIO2_2、GPIO2_3、GPIO2_5、GPIO0_30。

修改dts文件使其与原理图的按键接口一致。

gpio_buttons: gpio_buttons@ {
compatible = "gpio-keys";
#address-cells = <>;
#size-cells = <>; switch@ {
label = "button0";
linux,code = <0x100>;
gpios = <&gpio2 GPIO_ACTIVE_HIGH>;
}; switch@ {
label = "button1";
linux,code = <0x101>;
gpios = <&gpio2 GPIO_ACTIVE_HIGH>;
}; switch@ {
label = "button2";
linux,code = <0x102>;
gpios = <&gpio0 GPIO_ACTIVE_HIGH>;
gpio-key,wakeup;
}; switch@ {
label = "button3";
linux,code = <0x103>;
gpios = <&gpio2 GPIO_ACTIVE_HIGH>;
};
};
gpio_keys_s0: gpio_keys_s0 {
pinctrl-single,pins = <
0x94 (PIN_INPUT_PULLDOWN | MUX_MODE7) /* gpmc_oen_ren.gpio2_3 */
0x90 (PIN_INPUT_PULLDOWN | MUX_MODE7) /* gpmc_advn_ale.gpio2_2 */
0x70 (PIN_INPUT_PULLDOWN | MUX_MODE7) /* gpmc_wait0.gpio0_30 */
0x9c (PIN_INPUT_PULLDOWN | MUX_MODE7) /* gpmc_ben0_cle.gpio2_5 */
>;
};

保存编译生成zyr-am335x-evmsk.dtb

在内核中确保xxx/linux3.14.65/drivers/input/keyboard/gpio_key.c被编译进去(可以通过各层的Makefile与Kconfig还有menuconfig来实现)

编译好的内核和dtb文件下载到开发板中查看按键的事件类型:

[root@zyr-am335x mnt]#cd
[root@zyr-am335x ]#cat /proc/bus/input/devices
I: Bus= Vendor= Product= Version=
N: Name="gpio_buttons.7"
P: Phys=gpio-keys/input0
S: Sysfs=/devices/gpio_buttons./input/input0
U: Uniq=
H: Handlers=event0
B: PROP=
B: EV=
B: KEY=f [root@zyr-am335x ]#

可以看到按键的事件或者是用户接口吧为event0.。知道了用户的按键接口就可以编写测试程序了。

测试程序目的:实现一个按键对应一个LED灯,按键按一次LED灯的状态变化一次(前提是led子系统对应的LED驱动已经加载入内核中):

#include <stdio.h>
#include <linux/input.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h> void keyboard_test()
{
int count_sw1 = ;
int count_sw2 = ;
int count_sw3 = ;
int count_sw4 = ;
int fd=open("/dev/input/event0",O_RDWR);
if( fd <= )
{
printf("Can not open keyboard input file\n");
} struct input_event *event;
char buf[] = {};
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(fd, &rfds); while()
{
int ret = select(fd + ,&rfds, NULL,NULL,NULL);
if(ret < )
continue;
if( FD_ISSET(fd, &rfds) )
{
int readn = read(fd, buf, sizeof(struct input_event));
if (readn <= )
{
printf("uart read error %d\n", readn);
continue;
} struct input_event *my_event=(struct input_event*)buf;
if(my_event->type == EV_KEY)
{
switch( my_event->code )
{
case :
printf("This is a button:%d %d\n", my_event->code,my_event->value);
count_sw1++;
if(count_sw1==)
{
system("echo 1 > /sys/class/leds/zyrD1:green:usr0/brightness");
}
else if(count_sw1==)
{
system("echo 0 > /sys/class/leds/zyrD1:green:usr0/brightness");
count_sw1=;
}
break;
case :
printf("This is a button:%d %d\n", my_event->code,my_event->value);
count_sw2++;
if(count_sw2==)
{
system("echo 1 > /sys/class/leds/zyrD2:green:usr1/brightness");
}
else if(count_sw2==)
{
system("echo 0 > /sys/class/leds/zyrD2:green:usr1/brightness");
count_sw2=;
}
break;
case :
printf("This is a button:%d %d\n", my_event->code,my_event->value);
count_sw3++;
if(count_sw3==)
{
system("echo 1 > /sys/class/leds/zyrD3:green:heartbeat/brightness");
}
else if(count_sw3==)
{
system("echo 0 > /sys/class/leds/zyrD3:green:heartbeat/brightness");
count_sw3=;
}
break;
case :
printf("This is a button:%d %d\n", my_event->code,my_event->value);
count_sw4++;
if(count_sw4==)
{
system("echo 1 > /sys/class/leds/zyrD4:green:mmc0/brightness");
}
else if(count_sw4==)
{
system("echo 0 > /sys/class/leds/zyrD4:green:mmc0/brightness");
count_sw4=;
}
break;
default:
break; }
} } }
} int main()
{
keyboard_test();
return ;
}

将生成的可执行文件copy到nfs的共享目录下运行:

[root@zyr-am335x ]#cd mnt/
[root@zyr-am335x mnt]#ls
LED_zixitong gpio_keys.ko leds-gpio.ko zyr-hello.ko
gpio_buttons_leds led_test zleds-gpio.ko
[root@zyr-am335x mnt]#./gpio_buttons_leds
This is a button:
This is a button:
This is a button:
This is a button:
This is a button:
This is a button:
This is a button:
This is a button:
This is a button:
This is a button:
This is a button:
This is a button:
This is a button:
This is a button:
This is a button:
This is a button: 0
其中的256,257,258,259分别对应dts文件中的linux,code = <0x101>;linux,code = <0x102>;linux,code = <0x103>;linux,code = <0x104>;

上一篇:maven配置(myeclipse版)


下一篇:VS 中關於附加到進程中調試 的問題。