已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验七 字符设备驱动程序设计实验目的掌握字符设备驱动程序编写的框架;掌握设备驱动相关的知识;实验原理参考本周大课课件字符设备驱动程序.PPT。实验步骤建立一个工作目录,我们的驱动程序模块程序保存在这个目录下;#mkdir first_drv#cd first_drv1 自己编写一个字符设备驱动程序,程序格式参考下面;#include #include #include #include #include #include #include /(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(.); MODULE_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 启动开发板,进入linux系统后,在开发板上加载和卸载模块加载:# insmod/lib/modules/3.5.0-yyy/xxxx.ko查看系统分配的设备号#cat /proc/devices手动添加设备文件(设备节点)# mknod /dev/first_drv c 主设备号 次设备号5 交叉编译应程序,打开设备文件进行读写操作实验成功后,叫老师查看实验结果,作为平时考察成绩;first_drv:#include #include #include #include #include #include #include #include #include static int first_drv_open(struct inode *in, struct file *fp)printk(first driver open called!n);return 0;static int first_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 write 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_fops = .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_drvstatic int _init first_drv_init(void)dev_t dev;alloc_chrdev_region(&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;dev = 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#include#include#include#include#define FILE_NAME /dev/abcint main(void)int fd;fd=open(FILE_NAME,O_R
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年一级注册建筑师之设计前期与场地设计题库综合试卷B卷附答案
- 胆管肿瘤的护理
- 雨课堂学堂在线学堂云病理学邢医专单元测试考核答案
- 高考化学“3+2”模拟练试卷含答案(一)
- 2025云南红河州弥勒市中医医院招聘备案制工作人员28人模拟试卷附答案解析
- 青岛市市北区教育和体育局所属公办学校招聘2026年应届高校毕业生(60人)历年真题汇编带答案解析
- 2025重庆两江航空航天产业投资集团有限公司招聘4人模拟试卷带答案解析
- 2026浙江嘉兴市第二医院招聘高层次人才44人历年真题汇编带答案解析
- 2025年滁州经济技术开发区消防救援大队政府专职消防员招聘8人笔试模拟试卷带答案解析
- 2026贵州册亨县“优师计划”毕业生专项招聘历年真题汇编带答案解析
- 四川成考试卷真题及答案
- 2025年法宣在线考试题库及答案
- 2025贵州毕节市市直事业单位面向基层公开考调工作人员考试笔试备考试题及答案解析
- 2025年高压电工作业(特种作业)考试题库(带答案)
- 交换机路由器课件
- DB31∕T 1545-2025 卫生健康数据分类分级要求
- GB/T 11344-2021无损检测超声测厚
- 自闭症儿童的结构化-12秋(课堂PPT)
- SPC培训讲义(共135页).ppt
- 艺术生高考(课堂PPT)
- 天虹商场门店明细
评论
0/150
提交评论