在Linux驅動之LED驅動編寫已經詳細介紹了一個驅動的編寫過程,接着來寫一個按鍵驅動程序,主要是在file_operations結構中添加了一個read函數。還是分以下幾步說明
5、編寫Makefile,編譯驅動代碼與測試代碼,在開發板上運行
打開原理圖,確定需要控制的IO端口為GPF0、GPF2、GPG3、GPG11

2、查看芯片手冊,確定IO端口的寄存器地址,可以看到因為用了兩組GPIO端口,所以它的基地址分別為0x56000050、0x56000060


1)、編寫出口、入口函數。代碼如下,具體說明參考Linux驅動之LED驅動編寫
static int second_drv_init(void) { Secondmajor = register_chrdev(0, "buttons", &second_drv_ops);//注冊驅動程序 if(Secondmajor < 0) printk("failes 1 buttons_drv register\n"); second_drv_class = class_create(THIS_MODULE, "buttons");//創建類 if(second_drv_class < 0) printk("failes 2 buttons_drv register\n"); second_drv_class_dev = class_device_create(second_drv_class, NULL, MKDEV(Secondmajor,0), NULL,"buttons");//創建設備節點 if(second_drv_class_dev < 0) printk("failes 3 buttons_drv register\n"); gpfcon = ioremap(0x56000050, 16);//重映射 gpfdat = gpfcon + 1; gpgcon = ioremap(0x56000060, 16);//重映射 gpgdat = gpgcon + 1; printk("register buttons_drv\n"); return 0; } static void second_drv_exit(void) { unregister_chrdev(Secondmajor,"buttons"); class_device_unregister(second_drv_class_dev); class_destroy(second_drv_class); iounmap(gpfcon); iounmap(gpgcon); printk("unregister buttons_drv\n"); } module_init(second_drv_init); module_exit(second_drv_exit);
2)、添加file_operations 結構體,這個是字符設備驅動的核心結構,所有的應用層調用的函數最終都會調用這個結構下面定義的函數
static struct file_operations second_drv_ops = { .owner = THIS_MODULE, .open = second_drv_open, .read = second_drv_read, };
3)、分別編寫file_operations 結構體下的open、read函數。其中open函數主要將相應的IO端口配置成輸入功能,read函數主要是讀出IO端口的高低電平,然后傳給應用程序處理。函數為copy_to_user,第一個參數為目標地址(即傳到應用層的地址),第二個參數位源地址(即在內核里的地址),第三個參數為傳的個數。
static int second_drv_open (struct inode * inode, struct file * file) { /*配置gpf0、gpf2 io端口為輸入*/ *gpfcon &= ~((3<<(0*2)) | (3<<(2*2))); /*配置gpg3、gpg11 io端口為輸入*/ *gpgcon &= ~((3<<(3*2)) | (3<<(11*2))); return 0; } static ssize_t second_drv_read(struct file * file, char __user * userbuf, size_t count, loff_t * off) { unsigned char key_values[4]; unsigned long key_value; int ret; if(count != sizeof(key_values)) { printk("read error\n"); return -1; } /*讀取gpf0、gpf2 io端口*/ key_value = *gpfdat; key_values[0] =( (key_value>>0)&0X01) ? 1:0; key_values[1] =( (key_value>>2)&0X01) ? 1:0; /*讀取gpg3、gpg11 io端口*/ key_value = *gpgdat; key_values[2] =( (key_value>>3)&0X01) ? 1:0; key_values[3] =( (key_value>>11)&0X01) ? 1:0; ret = copy_to_user(userbuf, key_values, sizeof(key_values)); if(ret) { printk("copy error\n"); return -1; } return sizeof(key_values); }
4)、整體代碼
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <asm/io.h> //含有iomap函數iounmap函數 #include <asm/uaccess.h>//含有copy_from_user函數 #include <linux/device.h>//含有類相關的處理函數 static struct class *second_drv_class;//類 static struct class_device *second_drv_class_dev;//類下面的設備 static int Secondmajor; static unsigned long *gpfcon = NULL; static unsigned long *gpfdat = NULL; static unsigned long *gpgcon = NULL; static unsigned long *gpgdat = NULL; static int second_drv_open (struct inode * inode, struct file * file) { /*配置gpf0、gpf2 io端口為輸入*/ *gpfcon &= ~((3<<(0*2)) | (3<<(2*2))); /*配置gpg3、gpg11 io端口為輸入*/ *gpgcon &= ~((3<<(3*2)) | (3<<(11*2))); return 0; } static ssize_t second_drv_read(struct file * file, char __user * userbuf, size_t count, loff_t * off) { unsigned char key_values[4]; unsigned long key_value; int ret; if(count != sizeof(key_values)) { printk("read error\n"); return -1; } /*讀取gpf0、gpf2 io端口*/ key_value = *gpfdat; key_values[0] =( (key_value>>0)&0X01) ? 1:0; key_values[1] =( (key_value>>2)&0X01) ? 1:0; /*讀取gpg3、gpg11 io端口*/ key_value = *gpgdat; key_values[2] =( (key_value>>3)&0X01) ? 1:0; key_values[3] =( (key_value>>11)&0X01) ? 1:0; ret = copy_to_user(userbuf, key_values, sizeof(key_values)); if(ret) { printk("copy error\n"); return -1; } return sizeof(key_values); } static struct file_operations second_drv_ops = { .owner = THIS_MODULE, .open = second_drv_open, .read = second_drv_read, }; static int second_drv_init(void) { Secondmajor = register_chrdev(0, "buttons", &second_drv_ops);//注冊驅動程序 if(Secondmajor < 0) printk("failes 1 buttons_drv register\n"); second_drv_class = class_create(THIS_MODULE, "buttons");//創建類 if(second_drv_class < 0) printk("failes 2 buttons_drv register\n"); second_drv_class_dev = class_device_create(second_drv_class, NULL, MKDEV(Secondmajor,0), NULL,"buttons");//創建設備節點 if(second_drv_class_dev < 0) printk("failes 3 buttons_drv register\n"); gpfcon = ioremap(0x56000050, 16);//重映射 gpfdat = gpfcon + 1; gpgcon = ioremap(0x56000060, 16);//重映射 gpgdat = gpgcon + 1; printk("register buttons_drv\n"); return 0; } static void second_drv_exit(void) { unregister_chrdev(Secondmajor,"buttons"); class_device_unregister(second_drv_class_dev); class_destroy(second_drv_class); iounmap(gpfcon); iounmap(gpgcon); printk("unregister buttons_drv\n"); } module_init(second_drv_init); module_exit(second_drv_exit); MODULE_LICENSE("GPL");
測試程序實現四個按鍵中有一個按鍵按下時,打印出四個按鍵的按鍵值。./sencond_test。直接看代碼
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> /* *usage ./buttonstest */ int main(int argc, char **argv) { int fd; char* filename="dev/buttons"; unsigned char key_val[4]; unsigned long cnt=0; fd = open(filename, O_RDWR);//打開dev/firstdrv設備文件 if (fd < 0)//小於0說明沒有成功 { printf("error, can't open %s\n", filename); return 0; } if(argc !=1) { printf("Usage : %s ",argv[0]); return 0; } while(1) { read(fd, key_val, sizeof(key_val)); if(!key_val[0] || !key_val[1] || !key_val[2] || !key_val[3]) printf("%d key pressed %d %d %d %d\n",cnt++,key_val[0],key_val[1],key_val[2],key_val[3]); } return 0; }
5、編寫Makefile,編譯驅動代碼與測試代碼,在開發板上運行
Makefile源碼如下:
KERN_DIR = /work/system/linux-2.6.22.6 all: make -C $(KERN_DIR) M=`pwd` modules //M='pwd'表示當前目錄。這句話的意思是利用內核目錄下的Makefile規則來編譯當前目錄下的模塊 clean: make -C $(KERN_DIR) M=`pwd` modules clean rm -rf modules.order obj-m +=sencond_drv.o//調用內核目錄下Makefile編譯時需要用到這個參數
1)、然后在當前目錄下make后編譯出second_drv.ko文件
2)、arm-linux-gcc -o second_test second_test.c編譯出second_test測試程序
3)、cp second_drv.ko second_test /work/nfs_root將編譯出來的文件拷貝到開發板掛接的網絡文件系統上
4)、執行insmod second_drv.ko加載驅動。
5)、./second_test測試程序,按下按鍵,成功打印按鍵值,用top命令查看應用程序發現second_test程序占用了99%的CPU資源,這個驅動程序還需要完善。

