嵌入式系统开发之驱动篇002——完整的linux驱动 + 应用测试demo(神文)

http://blog.csdn.net/u010164190/article/details/52965530


   驱动代码

driverdemo.c

[cpp]  view plain  copy
 
  1. #include <linux/module.h>  
  2. #include <linux/init.h>  
  3. #include <linux/types.h>  
  4. #include <linux/fs.h>  
  5. #include <linux/mm.h>  
  6. #include <linux/cdev.h>  
  7. #include <linux/errno.h>  
  8. #include <linux/sched.h>  
  9. #include <asm/io.h>  
  10. #include <asm/system.h>  
  11. #include <asm/uaccess.h>  
  12. #define simple_MAJOR 200  
  13. static unsigned char simple_inc=0;  
  14. static char demoBuffer[256];  
  15. int simple_open(struct inode *inode,struct file *filp){  
  16.     if(simple_inc>0) return -ERESTARTSYS;  
  17.     simple_inc++;  
  18.     return 0;  
  19. }  
  20. int simple_release(struct inode *inode,struct file *filp){  
  21.     simple_inc--;  
  22.     return 0;  
  23. }  
  24. ssize_t simple_read(struct file *filp,char __user *buf,size_t count,loff_t *f_pos){  
  25.     if(copy_to_user(buf,demoBuffer,count)){  
  26.         count=-EFAULT;  
  27.     }  
  28.     return count;  
  29. }  
  30.   
  31. size_t simple_write(struct file *file,const char __user *buf,size_t count,loff_t *f_pos){  
  32.     if (copy_from_user(demoBuffer + *f_pos,buf,count)){  
  33.         count = -EFAULT;  
  34.     }  
  35.     return count;  
  36. }  
  37.   
  38. void simple_cleanup_module(void){  
  39.     unregister_chrdev(simple_MAJOR,"simple");  
  40.     printk("simple_cleanup_module!\n");  
  41.   
  42. }  
  43.   
  44. static const struct file_operations simple_fops={  
  45.     .owner=THIS_MODULE,  
  46.     .open=simple_open,  
  47.     .release=simple_release,  
  48.     .read=simple_read,  
  49.     .write=simple_write,      
  50. };  
  51.   
  52. int simple_init_module(void){  
  53.     int ret;  
  54.     ret=register_chrdev(simple_MAJOR,"simple",&simple_fops);  
  55.     if(ret<0){  
  56.         printk("Unable to register character device %d!/n",simple_MAJOR);  
  57.         return ret;  
  58.     }  
  59.     return 0;  
  60. }  
  61.   
  62. module_init(simple_init_module);  
  63. module_exit(simple_cleanup_module);  
  64.   
  65. MODULE_LICENSE("Dual BSD/GPL");  

Makefile 文件 
[plain]  view plain  copy
 
  1. ifeq ($(KERNELRELEASE),)  
  2.     KERNELDIR ?=/usr/src/linux-2.6.29.4  
  3.     INCLUDE ?=./  
  4.     PWD :=$(shell pwd)  
  5. modules:  
  6.     $(MAKE) -C $(KERNELDIR) -I $(INCLUDE) M=$(PWD) modules  
  7. modules_install:  
  8.     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install  
  9. clean:  
  10.     rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions   
  11. else  
  12.     obj-m :=driverdemo.o  
  13. endif  

生成安装驱动
[plain]  view plain  copy
 
  1. make //会生成driverdemo.ko文件   
  2. insmod driverdemo.ko //安装到系统内 需要在root 下运行  

创建设备文件
[cpp]  view plain  copy
 
  1. mknod /dev/fgj c 200 0  
这里我们只要关心200是什么, 200为设备号,也需要在root 下运行

我们在前面代码里

[cpp]  view plain  copy
 
  1. #define simple_MAJOR 200  
这就是设备号


应用程序code

test.c

[cpp]  view plain  copy
 
  1. #include <stdio.h>  
  2. #include <fcntl.h>  
  3. int main(void){  
  4.     int fd;  
  5.     int i;  
  6.     char data[256];  
  7.     int retval;  
  8.     fd=open("/dev/fgj",O_RDWR);  
  9.     if(fd==-1){  
  10.         perror("error open\n");  
  11.         exit(-1);  
  12.     }  
  13.     printf("open /dev/fgj successfully\n");  
  14.     //writing data  
  15.     retval=write(fd,"http://blog.csdn.net/ssihc0",26);  
  16.     if(retval==-1){  
  17.         perror("write error \n");  
  18.         exit(-1);  
  19.     }  
  20.       
  21.     retval=read(fd,data,26);  
  22.     if(retval==-1){  
  23.         perror("read error\n");  
  24.         exit(-1);  
  25.     }  
  26.     data[retval]=0;  
  27.     printf("read successfully:%s\n",data);  
  28.     close(fd);  
  29.     return 0;  
  30.   
  31. }  

生成
[plain]  view plain  copy
 
  1. gcc test.c  

运行./a.out

open /dev/fgj successfully
read successfully:http://blog.csdn.NET/ssihc0

下面是一些我们在驱动开发过程经常要用到的COMMAND

[cpp]  view plain  copy
 
  1. history  
  2. cat /var/log/messages | tail  //看一下我们的调试消息  
  3.   
  4. mknod /dev/fgj c 200 0    //建设备文件   
  5.   
  6. cat /proc/modules | grep d* //查看我们的驱动MODULE  
  7.   
  8. insmod driverdemo.ko   //安装 设备  
  9. rmmod 1-2module.ko   //删除设备  
  10.   
  11. ismod //查看设备信息  
  12.   
  13. dmesg  | tail  //看一下我们的调试消息  
  14.   
  15. insmod test.ko a=3 b=2 AddOrSub=2 //带参数加载驱动  


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值