Linux内核版本号:linux 2.6.39
交叉编译工具:arm-linux-gcc 4.5.1
Linux内核下载:www.kernel.org
开发板:友善之臂Tiny6410
LCD:友善之臂S70
一、移植LED驱动
打开arch/arm/mach-s3c64xx/mach-mini6410.c添加下列代码:
1 static struct gpio_led tiny6410_gpio_led[] = {
2 [0] = {
3 .name = 'led1', //设备名
4 .gpio = S3C64XX_GPK(4), //GPK4
5 .active_low = 1, //低电平点亮
6 .default_state = LEDS_GPIO_DEFSTATE_ON, //系统启动后默认为打开
7 },
8 [1] = {
9 .name = 'led2',
10 .gpio = S3C64XX_GPK(5),
11 .active_low = 1,
12 .default_state = LEDS_GPIO_DEFSTATE_OFF, //系统启动后默认关闭
13 },
14 [2] = {
15 .name = 'led3',
16 .gpio = S3C64XX_GPK(6),
17 .active_low = 1,
18 .default_state = LEDS_GPIO_DEFSTATE_ON,
19 },
20 [3] = {
21 .name = 'led4',
22 .gpio = S3C64XX_GPK(7),
23 .active_low = 1,
24 .default_state = LEDS_GPIO_DEFSTATE_OFF,
25 },
26 };
27
28 static struct gpio_led_platform_data tiny6410_leds_data = {
29 .num_leds = ARRAY_SIZE(tiny6410_gpio_led),
30 .leds = &tiny6410_gpio_led,
31 };
32
33 static struct platform_device tiny6410_device_leds = {
34 .name = 'leds-gpio',
35 .id = -1,
36 .dev = {
37 .platform_data = &tiny6410_leds_data,
38 },
39 };
在mini6410_devices中添加tiny6410_device_leds,系统启动时将自动注册LED平台设备:
1 static struct platform_device *mini6410_devices[] __initdata = {
2 ...
3 &tiny6410_device_leds,
4 };
执行make menuconfig修改内核配置,添加对LED设备的支持:
Device Drivers --->
│ │ [*] LED Support --->
│ │ [*] LED Class Support
│ │ *** LED drivers ***
│ │ <*> LED Support for GPIO connected LEDs
│ │ [*] Platform device bindings for GPIO LEDs
编译并烧写内核,启动开发板可以看到第一、第三个LED被点亮。
编写应用程序控制LED:
系统LED设备名为每个LED设备创建了一个节点文件夹,位于/sys/devices/platform/leds-gpio/leds/目录下,对设备文件夹里面的brightness 文件写0或写非0即可对LED进行操作。
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7
8
9 int main(int argc,char** argv)
10 {
11 int fd = 0;
12 char path[64] = '/sys/devices/platform/leds-gpio/leds/';
13
14 if(argc != 3)
15 {
16 printf('format error!n');
17 return -1;
18 }
19
20 strcat(path,argv[1]);
21 strcat(path,'/brightness');
22
23 printf('%sn',path);
24 fd = open(path,O_RDWR);
25 if(fd == -1)
26 {
27 printf('open file failure!n');
28 return -1;
29 }
30 if(atoi(argv[2]))
31 write(fd,'1',1);
32 else
33 write(fd,'0',1);
34
35 close(fd);
36 return 0;
37 }
二、按键驱动移植
在arch/arm/mach-s3c64xx/mach-mini6410.c添加下列代码:
1 static struct gpio_keys_button tiny6410_gpio_keys[] = {
2 [0] = {
3 .code = KEY_F1, //键值
4 .type = EV_KEY, //按键输入类型
5 .gpio = S3C64XX_GPN(0),
6 .active_low = 1, //低电平表示按下
7 .wakeup = 0,
8 .debounce_interval = 5, /* ms */ //延时消抖
9 .desc = 'Button 1',
10 },
11 [1] = {
12 .code = KEY_F2,
13 .type = EV_KEY,
14 .gpio = S3C64XX_GPN(1),
15 .active_low = 1,
16 .wakeup = 0,
17 .debounce_interval = 5, /* ms */
18 .desc = 'Button 2',
19 },
20 [2] = {
21 .code = KEY_F3,
22 .type = EV_KEY,
23 .gpio = S3C64XX_GPN(2),
24 .active_low = 1,
25 .wakeup = 0,
26 .debounce_interval = 5, /* ms */
27 .desc = 'Button 3',
28 },
29 [3] = {
30 .code = KEY_F4,
31 .type = EV_KEY,
32 .gpio = S3C64XX_GPN(3),
33 .active_low = 1,
34 .wakeup = 0,
35 .debounce_interval = 5, /* ms */
36 .desc = 'Button 4',
37 },
38
39 };
40
41 static struct gpio_keys_platform_data tiny6410_key_data = {
42 .buttons = &tiny6410_gpio_keys,
43 .nbuttons = ARRAY_SIZE(tiny6410_gpio_keys),
44 };
45
46 static struct platform_device tiny6410_device_keys = {
47 .name = 'gpio-keys',
48 .id = -1,
49 .dev = {
50 .platform_data = &tiny6410_key_data,
51 },
52 };
在mini6410_devices中添加tiny6410_device_keys:
1 static struct platform_device *mini6410_devices[] __initdata = {
2 ....
3 &tiny6410_device_leds,
4 &tiny6410_device_keys,
5 };
执行make menuconfig修改内核配置,添加对LED设备的支持:
Device Drivers --->
│ │ Input device support --->
│ │ [*] Keyboards --->
│ │ <*> GPIO Buttons
同时在Input device support里面添加event interface的支持,在/dev/下面就能生成一个event设备文件:
Device Drivers --->
│ │ Input device support --->
│ │ <*> Event interface
编译并烧写内核,启动开发板可以在/dev/目录下生成了event0设备文件,对按键驱动进行简单的测试:
执行hexdump /dev/event0
每次按下按键可以看到如下所示按键信息,表明按键是工作正常的。
1 /dev # hexdump event0
2 0000000 034d 0000 0e3b 000c 0001 003b 0001 0000
3 0000010 034d 0000 0e4c 000c 0000 0000 0000 0000
4 0000020 034d 0000 cd5f 000e 0001 003b 0000 0000
5 0000030 034d 0000 cd6b 000e 0000 0000 0000 0000
编写应用程序测试按键驱动:
按键驱动为输入子系统,应用程序中需要对event进行循环检测看系统有没有上报输入事件,按键的输入事件类型为EV_KEY,键值分别问KEY_F1、KEY_F2、KEY_F3、KEY_F4,数值为1表示按键按下为0表示按键释放。
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7
8 int main(void)
9 {
10 int fd = 0;
11 struct input_event event_key;
12 int count = 0;
13
14 fd = open('/dev/event0',O_RDONLY);
15 if(fd == -1)
16 {
17 printf('open file failedn');
18 return -1;
19 }
20
21 while(1)
22 {
23 count = read(fd,&event_key,sizeof(struct input_event));
24 if(count < 0)
25 {
26 printf('read failedn');
27 break;
28 }
29 if(event_key.type == EV_KEY)