您的位置:首页 > 移动开发 > 微信开发

LED驱动程序的测试

2017-04-15 10:29 253 查看
前面大概聊了一下本人对led驱动的学习情况以及一些总结,现在我们用一个小的测试程序来玩一下

先给出基本的Makefile:

1 obj-m := led.o
2 KERNEL_DIR := ~/kernel/linux-3.0.54/
3 PWD := $(shell pwd)
4 all:
5     make -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
6 clean:
7     rm *.o *.ko *.mod.c
8
9 .PHONY:clean


再给出测试程序test.c:

t.c
1 /*********************************************************************************
2  *      Copyright:  (C) 2017 minda
3  *                  All rights reserved.
4  *
5  *       Filename:  test.c
6  *    Description:  This file
7  *
8  *        Version:  1.0.0(03/29/2017)
9  *         Author:  tangyanjun <519656780@qq.com>
10  *      ChangeLog:  1, Release initial version on "03/29/2017 02:55:07 PM"
11  *
12  ********************************************************************************/
13 #include<stdio.h>
14 #include<stdlib.h>
15 #include<unistd.h>
16 #include<fcntl.h>
17 #include<errno.h>
18 #include<string.h>
19 #include<asm/ioctl.h>
20 #ifndef __KERNEL__
21 #include <sys/ioctl.h>
22 #endif
23
24 #define PLATDRV_MAGIC      0X60
25 #define LED_ON              _IO (PLATDRV_MAGIC, 0x19)
26 #define LED_OFF            _IO (PLATDRV_MAGIC, 0x18)
27 #define LEN_LED             4
28 #define LED_BUF            128
29
30 int main(int argc,char **argv)
31 {
32     int fd[LEN_LED];
33     char dev_name[LED_BUF];
34     int i;
35
36         for(i=0;i<LEN_LED;i++)
37         {
38             snprintf(dev_name,sizeof(dev_name),"/dev/led%d",i);
39             fd[i]=open(dev_name,O_RDWR,0755);
40                 if(fd[i]<0)
41                   {
42                     printf("open failure:%s",strerror(errno));
43                   }
44         }
45
46                 while(1)
47                {
48                   for(i=0;i<LEN_LED;i++)
49                   {
50                   //   ioctl(fd[i],LED_OFF);
51                    //  sleep(0.5);
52                      ioctl(fd[i],LED_ON);
53                      sleep(1);
54                      ioctl(fd[i],LED_OFF);
55                      sleep(0.5);
56                   }
57
58
59               }
60
61         for(i=0;i<LEN_LED;i++)
62        {
63              close(fd[i]);
64              return 0;
65        }                                                                                                                           }


测试小程序就是根据驱动代码的一些函数调用来写,这里要注意判断出错问题,可以用来检查程序。

那么怎么测试呢?

1.先将上述Makefile和上篇的led.c驱动放在同一目录下,make编译生成“.ko”文件。

2.因为我们要把测试程序放到开发板上去测试,所以我们应该使用交叉编译器arm-linux-gcc来编译上述test.c文件,生成可执行文件。

3.将上述生成的.ko文件和可执行文件烧到开发板中:



这里的地址为我们的服务器地址。

4.在开发板中用insmod安装我们的led模块:



此时lsmod看看我们安装的驱动模块:



驱动安装好以后我们就该创建设备节点了,因为要找次设备号嘛,主设备号都是统一的,我们的板子有4个led,亮哪个灭哪个由次设备号决定,我们先查一下我们的主设备号:


我们的led为251,所有用mknod建立设备节点:

mknod /dev/led0 c 251 0
mknod /dev/led1 c 251 1
mknod /dev/led2 c 251 2
mknod /dev/led3 c 251 3

最后给我们的test可执行文件可执行权限,然后执行:



我这里创建了四个节点,点亮了四个led,所以根据测试代码,我的led灯是流水灯,这里的延时可以根据需要自己设置。

当然我们如果要卸载驱动,既可以rmmod led,记得查看:



有了这个我们才可以卸载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息