三,Makefile文件的编写:
obj-m:=Button6.o
CC=arm-linux-gcc
KERNELDIR=/usr/local/opt/EmbedSky/linux-2.6.30.4
PWD:=$(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
四,驱动测试程序源码Test_Button6.c :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
int main(void)
{
int i;
int buttons_fd;
int key_value[4];
int temp=0;
//打开键盘点亮LED设备文件
buttons_fd = open("/dev/button6", 0);//这里的open的实现在驱动的buttons_open
if (buttons_fd < 0)
{
perror("open device buttons");
exit(1);
}
for (;;) //一直循环来监听是否有按键按下
{
fd_set rds;
int ret;
FD_ZERO(&rds); //清除文件描述符集rds
FD_SET(buttons_fd, &rds); //将文件描述符buttons_fd添加到文件描述符集rds
//使用系统调用select检查是否能够从/dev/buttons设备读取数据
ret = select(buttons_fd + 1, &rds, NULL, NULL, NULL);
if (ret == -1) //读取出错或select被某个信号中断
{
printf("select error!\n");
exit(-1);
}
if (ret == 0) //经过了timeout等待后没有文件满足要求
{
printf("Timeout ! \n");
}
else if (FD_ISSET(buttons_fd, &rds)) //测试是否可读
{
//开始读取键盘驱动发出的数据,注意key_value和键盘驱动中定义为一致的类型
int ret = read(buttons_fd, key_value, sizeof key_value);//read也是在buttons_read中实现的
if (ret != sizeof key_value)
{
if (errno != EAGAIN)
perror("read buttons\n");
continue;
}
else
{
/*打印键值*/
for (i = 0; i < 4; i++)
if(key_value[i] != 0)
{
printf("K%d %s, key value = 0x%02x\n", i+1, (key_value[i] & 0x80) ? "released" : key_value[i] ? "pressed down" : "", key_value[i]);
if(temp==key_value[i])
ioctl(buttons_fd,0,i);
else
ioctl(buttons_fd,1,i); //判断按键值,点亮对应的LED
temp=key_value[i];
}
}
}
}
//关闭设备文件句柄
close(buttons_fd);
return 0;
}
源码分析:
本测试程序的核心实现在于select方法,select方法对应于在驱动程序中实现的设备poll方法,用于多路监控,当没有一个文件满足要求时,select将阻塞上层应用调用的进程。select方法的原型如下:
int select(int maxfd,fd_set*readfds,fd_set*writefds,fe_set*exceptfds,const struct timeval*timeout);
参数说明:
maxfd:文件描述符的范围,比待检测的最大文件描述符大1 。(说明:文件描述符就是我们使用open方法打开一个设备时,open函数的返回值)
readfds:被读监控的文件描述符集。
writefds:被写监控的文件描述符集。
exceptfds:被异常监控的文件描述符集。
timeout:定时器。
select系统调用的返回值:
正常情况下返回满足要求的文件描述符的个数。
经过了timeout等待后仍无文件满足要求,返回值为0 。
如果select被某个信号中断,它将返回 -1 并设置errno为EINTR。
如果出错,返回-1并设置相应的errno。
select系统调用的使用方法:
①将要监控的文件添加到文件描述符集。
②调用select开始监控。
③判断文件是否发生变化。
linux系统提供了4个宏对描述符集进行操作:
这些宏定义在select.h中
void FD_SET(int fd,fd_set*fdset); //将文件描述符fd添加到文件描述符集fdset中
void FD_CLR(int fd,fd_set*fdset); //从文件描述符集fdset中清除文件描述符fd
void FD_ZERO(fd_set*fdset); //清空文件描述符集fd_set
void FD_ISSET(int fd,fd_set *fdset); //在调用select后使用FD_ISSET来检测文件描述符集fdset中的文件fd发生了变化
五,驱动测试:
首先要特别注意的一个问题是开发板自带的按键驱动的存在会影响我们自己写的这个按键点亮LED灯驱动程序的测试,解决办法是重新编译开发板系统的内核,在配置内核的时候把device driver->Input device support-->Keyboards这个配置去掉,这个时候开发板系统自带的按键驱动程序就不会编译到内核了,从而达到去掉系统本身按键驱动的目的,至于编译内核的方法请参照我前面写的一篇文章:《针对TQ2440开发板上Linux2.6.30.4内核的交叉编译和驱动程序的移植》http://www.linuxidc.com/Linux/2014-08/106011.htm,然后重新烧写开发的linux系统,具体烧写方法请看你自己的开发的说明书,这里就不再赘述了。
1.在pc终端输入指令: sudo kermit -c ,连接开发板。
2.把编译好的驱动模块Button6.ko和编译好的测试程序Test_Button6.o使用sd卡拷贝至开发板系统的opt文件夹下。
3.在开发板系统的终端输入指令:insmod Button6.ko 将模块加载至系统内核,然后输入指令:./Test_Button6.o 进行驱动程序的测试。本人已经亲自通过测试。
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-10/107954.htm
