《TQ2440的简单内存数据读写驱动程序》
Linux下的设备驱动程序被组织为一组完成不同任务的函数的集合,通过这些函数使得设备操作犹如文件一般。在应用程序看来,硬件设备只 是一个设备文件,应用程序可以象操作普通文件一样对硬件设备进行操作,如open ()、close ()、read ()、write () 等
该程序主要是在TQ2440开发板上完成对一段内存的读写,驱动程序中实现了简单的read、write、open、release等功能。
以下是fs.c的驱动程序代码
该程序主要是在TQ2440开发板上完成对一段内存的读写,驱动程序代码如下
THIS_MODULE,它的定义如下是#define THIS_MODULE (&__this_module),__this_module是一个struct module变量,代表当前模块
错误列举: error: implicit declaration of function class_device_create error: implicit declaration of function class_device_destroy 原因: class_device_create 和 class_device_destroy 是最期版本的API 现在已经改成device_create 和 device_destroy表当前模块,
编译错误:error: expected =, ,, ;, asm or __attribute__ before...
原因:有可能是你头文件的包含的顺序需要调换一下,具体参考:http://blog..net/xuyunzhang/article/details/6286873
Makefile程序
KERNELDIR=/opt/EmbedSky/linux-2.6.30.4/ PWD:=$(shell pwd) INSTALLDIR=$(PWD) CC=arm-linux-gcc obj-m:=fs.o modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules clean: rm -rf *.o *.ko *.mod.c *.markers *.order *.symvers .PHONY:modules clean
testing.c测试程序
#include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<sys/types.h> #include<unistd.h> #include<fcntl.h> #include<linux/ioctl.h> int main() { int fd,i,nwrite,nread; char *buf="hello "; char read_buf[6]={0}; fd=open("/dev/FS",O_RDWR); if(fd<0) { perror("open"); exit(1); } else { printf("open success "); } nwrite=write(fd,buf,strlen(buf)); printf("the num write_to_kernel is: %d ",nwrite); if(nwrite<0) { perror("write"); exit(1); } nread=read(fd,read_buf,6); printf("the num read_from_kernel is: %d ",nread); if(nread<0) { perror("read"); exit(1); } else { printf("read is %s ",read_buf); } close(fd); exit(0); }
make后将会由fs.c 生成fs.ko文件
在用过arm-linux-gcc testint.c -o testing 生成testing可执行文件,注意这里要将testing的权限修改成777
然后通过tftp或者其他方式正这两个文件传输至开发板上
在执行insmod fs.ko 后,将会在/dev/ 目录下看到 设备文件FS ,这说明设备驱动已经加载好了,并且分配了设备号
执行./testing 就可以看到内存读写结果啦。
以下是我参考的一些资料链接,感谢他们的无私分享
http://blog.163.com/njut_wangjian/blog/static/16579642520121022112940349/
http://blog..net/xuyunzhang/article/details/6286873
http://blog..net/wzws45/article/details/5948308
http://www.embedu.org/Column/Column476.htm