2、inux/types.h>#include#include//(1)編寫硬件底層操作函數(shù)實(shí)現(xiàn)open,release,write,read......//(2)創(chuàng)建一個(gè)file_operations結(jié)構(gòu)......staticint__initxxx_init(void){//(3)申請(qǐng)?jiān)O(shè)備號(hào)........//(4)初始化cdev結(jié)構(gòu)........//(5)注冊(cè)cdev結(jié)構(gòu)........return0;}staticvoid__exitXXX
3、_exit(void){//注銷cdev結(jié)構(gòu)........//注銷設(shè)備號(hào)}module_init(.......);module_exit(.......);MODULE_LICENSE(......);注意:要求底層函數(shù)要實(shí)現(xiàn)open,release,write,read方法。2編寫Makefile文件內(nèi)容格式,參考如下:obj-m+=模塊程序文件名.oall:make-C內(nèi)核源碼路徑M=`pwd`modules#這一行要以TAB鍵開頭clean:make-C內(nèi)核源碼路徑M=`pwd`modulescle
4、an#這一行要以TAB鍵開頭3編譯模塊,拷貝到根文件系統(tǒng)中編譯內(nèi)核模塊,直接使用make命令就可以了;#make編譯沒有錯(cuò)誤時(shí),將模塊拷貝到跟文件系統(tǒng)中;#cpxxx.ko/opt/rootfs/lib/modules/3.5.0-yyy/4啟動(dòng)開發(fā)板,進(jìn)入linux系統(tǒng)后,在開發(fā)板上加載和卸載模塊加載:#insmod/lib/modules/3.5.0-yyy/xxxx.ko查看系統(tǒng)分配的設(shè)備號(hào)#cat/proc/devices手動(dòng)添加設(shè)備文件(設(shè)備節(jié)點(diǎn))#mknod/dev/first_drvc主設(shè)備號(hào)次
5、設(shè)備號(hào)5交叉編譯應(yīng)程序,打開設(shè)備文件進(jìn)行讀寫操作實(shí)驗(yàn)成功后,叫老師查看實(shí)驗(yàn)結(jié)果,作為平時(shí)考察成績(jī);first_drv:#include#include#include#include#include#include#include#include#include6、o.h>staticintfirst_drv_open(structinode*in,structfile*fp){printk("firstdriveropencalled!");return0;}staticintfirst_drv_release(structinode*in,structfile*fp){printk("firstdriverreleasecalled!");return0;}staticintdata=0;staticssize_tfirst_drv_write(struct
7、file*fp,constchar__user*buf,size_tlen,loff_t*offset){copy_from_user((void*)&data,buf,sizeof(int));printk("firstdriverwritecalled!datais%d",data);returnsizeof(int);}staticssize_tfirst_drv_read(structfile*fp,char__user*buf,size_tlen,loff_t*offset){data=data
8、+1;copy_to_user(buf,&data,sizeof(int));printk("firstdriverreadcalled!");returnsizeof(int);}staticstructfile_operationsfirst_fops={.owner=THIS_MODULE,.open=first_drv_open,.release=first_drv_release,.w