字符设备驱动程序设计_第1页
字符设备驱动程序设计_第2页
字符设备驱动程序设计_第3页
字符设备驱动程序设计_第4页
字符设备驱动程序设计_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验七 字符设备驱动程序设计实验目的掌握字符设备驱动程序编写的框架;掌握设备驱动相关的知识;实验原理参考本周大课课件字符设备驱动程序.PPT。实验步骤建立一个工作目录,我们的驱动程序模块程序保存在这个目录下;#mkdir first_drv#cd first_drv1 自己编写一个字符设备驱动程序,程序格式参考下面;#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/type

2、s.h>#include <linux/uaccess.h>#include <linux/cdev.h>/(1)编写硬件底层操作函数实现open,release,write,read./(2)创建一个file_operations结构. static int _init xxx_init(void) /(3)申请设备号./(4)初始化cdev结构./(5)注册cdev结构.return 0; static void _exit XXX_exit(void) /注销cdev结构./注销设备号 module_init(.); module_exit(.); MODU

3、LE_LICENSE(.);注意:要求底层函数要实现open,release,write,read方法。2 编写Makefile文件内容格式,参考如下:obj-m+=模块程序文件名.oall:make -C 内核源码路径 M=pwd modules #这一行要以TAB键开头clean:make -C 内核源码路径 M=pwd modules clean #这一行要以TAB键开头3 编译模块,拷贝到根文件系统中编译内核模块,直接使用make命令就可以了;#make编译没有错误时,将模块拷贝到跟文件系统中;#cp xxx.ko/opt/rootfs/lib/modules/3.5.0-yyy/4

4、启动开发板,进入linux系统后,在开发板上加载和卸载模块加载:# insmod/lib/modules/3.5.0-yyy/xxxx.ko查看系统分配的设备号#cat /proc/devices手动添加设备文件(设备节点)# mknod /dev/first_drv c 主设备号 次设备号5 交叉编译应程序,打开设备文件进行读写操作实验成功后,叫老师查看实验结果,作为平时考察成绩;first_drv:#include <linux/module.h>#include <linux/init.h>#include <linux/fs.h>#include &

5、lt;linux/types.h>#include <linux/uaccess.h>#include <linux/kernel.h>#include <linux/cdev.h>#include <linux/device.h>#include <asm/io.h>static int first_drv_open(struct inode *in, struct file *fp)printk("first driver open called!n");return 0;static int first

6、_drv_release(struct inode *in, struct file *fp)printk("first driver release called!n");return 0;static int data=0;static ssize_t first_drv_write(struct file *fp, const char _user *buf, size_t len, loff_t *offset)copy_from_user(void *)&data,buf,sizeof(int);printk("first driver writ

7、e called!data is %dn",data);return sizeof(int);static ssize_t first_drv_read(struct file *fp, char _user *buf, size_t len, loff_t * offset)data=data+1;copy_to_user(buf,&data,sizeof(int);printk("first driver read called!n");return sizeof(int);static struct file_operations first_fop

8、s = .owner = THIS_MODULE,.open = first_drv_open,.release =first_drv_release,.write = first_drv_write,.read = first_drv_read, ;/驱动注册static int major = 0;static struct cdev *first_drv_cdev;#define FIRST_DRV_NAME "first_drv"static int _init first_drv_init(void)dev_t dev;alloc_chrdev_region(&a

9、mp;dev, 0, 1, FIRST_DRV_NAME);major = MAJOR(dev);first_drv_cdev = cdev_alloc();cdev_init(first_drv_cdev,&first_fops);first_drv_cdev->owner = THIS_MODULE;cdev_add(first_drv_cdev, dev, 1);printk("first driver module insert!n");return 0;static void _exit first_drv_exit(void)dev_t dev;d

10、ev = MKDEV(major,0);cdev_del(first_drv_cdev);unregister_chrdev_region(dev, 1);printk("first driver module remove!n");module_init(first_drv_init);module_exit(first_drv_exit);MODULE_LICENSE("GPL");Makefile:obj-m += first_drv.oall:make -C /home/sice/linux-4.4.19M=pwdmodulesclean:make -C /home/sice/linux-4.4.19M=pwdmodules cleantest:#include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<fcntl.h>#define FILE_NAME "/dev/abc"int main(void)int fd;fd=open(FILE_

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论