“手把手教你学linux驱动开发”OK6410系列之02---虚拟字符设备

来源:互联网 发布:群智能算法 编辑:程序博客网 时间:2024/04/28 14:27

上一篇文章我们介绍了如何在linux下编写一个模块,并加载到内核空间。通过实验我们了解了大体的流程以及模块的工作原理。本篇文章我们将介绍如何编写一个虚拟的字符设备驱动程序。

       之所以称之为虚拟字符设备驱动程序,主要原因是该驱动程序并没有真正操作外部设备,只是一个字符设备驱动程序框架,这为后面我们开发正是设备的驱动程序(LED、蜂鸣器等)奠定了基础。

       作者:沧海猎人   出处:http://blog.csdn.net/embedded_hunter  转载请注明出处   嵌入式技术交流QQ群:179012822

 

 一、实验环境 

开发机环境

          操作系统:ubuntu 9.10

          交叉编译环境:arm-linux-gcc 4.2.2 ,安装位置 /usr/local/arm/4.3.2/

          6410板子内核源码路径:/work/linux-2.6.36.2-v1.05/     

目标板环境:OK6410-A     linux2.6.36

 

二、实验原理

         在linux系统中,我们经常听到说“一切都是文件”。我们对设备操作就转换为对文件的操作。那么我们在用户空间对文件的操作包括open、read、write、close等。那么在驱动程序中如何响应用户发出来的文件操作请求呢?

        通过本实验先了解一下大体的过程,之后我会渐渐向大家介绍具体的细节。

 

三、实验步骤

1、编写驱动程序

driver_char.c

[plain] view plaincopy
  1. #include <linux/module.h>  
  2. #include <linux/kernel.h>  
  3. #include <linux/fs.h>  
  4. #include <asm/uaccess.h> /* copy_to_user,copy_from_user */  
  5. #define MY_MAJOR 240  
  6.   
  7. int my_open (struct inode *inode,struct file *filp)  
  8. {  
  9.     printk("#########open######\n");  
  10.     return 0;  
  11. }  
  12.   
  13. ssize_t my_read (struct file *filp, char __user *buf, size_t count,loff_t *f_pos)  
  14. {  
  15.     printk("#########read######\n");  
  16.     return count;  
  17. }  
  18.   
  19. ssize_t my_write (struct file *filp, const char __user *buf, size_t count,loff_t *f_pos)  
  20. {  
  21.     printk("#########write######\n");  
  22.     return count;  
  23. }  
  24.   
  25. int my_release (struct inode *inode, struct file *filp)  
  26. {  
  27.     printk("#########release######\n");  
  28.     return 0;  
  29. }  
  30.   
  31. struct file_operations my_fops ={  
  32.     .owner = THIS_MODULE,  
  33.     .open = my_open,  
  34.     .read = my_read,  
  35.     .write = my_write,  
  36.     .release = my_release,  
  37. };  
  38.   
  39. int __init my_init (void)  
  40. {  
  41.     int rc;  
  42.     printk ("Test char dev\n");  
  43.     rc = register_chrdev(MY_MAJOR,"my",&my_fops);  
  44.     if (rc <0)  
  45.     {  
  46.         printk ("register %s char dev error\n","my");  
  47.         return -1;  
  48.     }  
  49.   
  50.     printk ("ok!\n");  
  51.     return 0;  
  52. }  
  53.   
  54. void __exit my_exit (void)  
  55. {  
  56.     unregister_chrdev(MY_MAJOR,"my");  
  57.     printk ("module exit\n");  
  58. }  
  59.   
  60. MODULE_LICENSE("GPL");  
  61. module_init(my_init);  
  62. module_exit(my_exit);  

 

Makefile文件

[plain] view plaincopy
  1. obj-m := driver_char.o  
  2. KDIR :=/work/linux-2.6.36.2-v1.05/  
  3. all:  
  4.     make -C $(KDIR) M=$(shell pwd) modules  
  5. install:  
  6.     cp driver_char.ko /tftpboot/  
  7. clean:  
  8.     make -C $(KDIR) M=$(shell pwd) clean  

 

2、编写测试程序

test.c

[plain] view plaincopy
  1. #include <stdio.h>  
  2. #include <sys/types.h>  
  3. #include <sys/stat.h>  
  4. #include <fcntl.h>  
  5. int main (void)  
  6. {  
  7.     int fd;  
  8.     char buf[10]={0,1};  
  9.     char buf2[10];  
  10.     fd = open("/dev/my_char",O_RDWR);  
  11.     if (fd < 0)  
  12.     {  
  13.         printf ("Open /dev/my_char");  
  14.         return -1;  
  15.     }  
  16.     write(fd,buf,2);  
  17.     read(fd,buf2,2);  
  18.     close (fd);  
  19.     return 0;  
  20. }  

 

3、编译驱动程序与测试程序

      编译驱动程序

      #make

      将驱动程序放到tftp的工作目录 /tftpboot

      #make install

      编译测试程序

      #arm-linux-gcc  test.c  -o  test

      将测试程序放到tftp的工作目录 /tftpboot

       #cp  test  /tftpboot

 

4、将程序下载到开发板

       将开发板的IP地址修改,与主机在同一个网段。确保PC的tftp服务开启。

      下载程序到开发板

        SMDK6410#   tftp -l /lib/modules/2.6.36.2/driver_char.ko -r driver_char.ko  -g  192.168.1.111        192.168.1.111为服务器IP

        SMDK6410#   tftp -l test  -r test  -g  192.168.1.111        

 

5、测试

        加载驱动   #insmod  /lib/modules/2.6.36.2/driver_char.ko

        创建设备文件   #mknod  /dev/my_char  c   240  0

        测试  ./test

        [root@FORLINX6410]# ./test
        #########open######
        #########write######
        #########read######
        #########release######

   

       卸载驱动  #rmmod   driver_char

       从上面的结果我们可以看到,当用户调用相应的文件操作函数时,驱动程序中的相应的函数也会被调用。

        大家可以修改相应程序,测试一下其他的情况。

 

作者:沧海猎人   出处:http://blog.csdn.net/embedded_hunter  转载请注明出处   嵌入式技术交流QQ群:179012822

 

备注: 本驱动使用了最基本的内核函数,可能操作方法与你看过的其他驱动程序不太一样,尤其注册字符设备的函数。  没关系,我们会一步一步介绍相关的其他函数。

原创粉丝点击