




已阅读5页,还剩65页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
嵌入式系统程序设计 大连理工大学软件学院嵌入式系统工程系赖晓晨 Linux驱动程序实例 rh9驱动程序实例 2 4 ubuntu驱动程序实例 2 6 嵌入式设备linux驱动实例 2 4 一 rh9驱动程序实例 下面的驱动程序虚拟了一个字符设备驱动程序 运行于RedHatLinux9功能 在内核空间定义一个数据 然后通过测试程序调用驱动 来读取及修改这个数据 例5 21 程序文件driver cMakefiletest c linux字符设备驱动程序 运行于rh9linux 赖晓晨 include include include includeMODULE LICENSE GPL defineMAJOR NUM254 defineEFALUT1staticssize tmy read structfile filp char buf ssize tsize loff t off staticssize tmy write structfile flip constchar buf ssize tsize loff t off structfile operationsmy ops read my read write my write staticintglobal var 0 linux字符设备驱动程序 运行于rh9linux 赖晓晨 include include include includeMODULE LICENSE GPL defineMAJOR NUM254 defineEFALUT1staticssize tmy read structfile filp char buf ssize tsize loff t off staticssize tmy write structfile flip constchar buf ssize tsize loff t off structfile operationsmy ops read my read write my write staticintglobal var 0 必要的头文件 linux字符设备驱动程序 运行于rh9linux 赖晓晨 include include include includeMODULE LICENSE GPL defineMAJOR NUM254 defineEFALUT1staticssize tmy read structfile filp char buf ssize tsize loff t off staticssize tmy write structfile flip constchar buf ssize tsize loff t off structfile operationsmy ops read my read write my write staticintglobal var 0 遵守GPL协议 linux字符设备驱动程序 运行于rh9linux 赖晓晨 include include include includeMODULE LICENSE GPL defineMAJOR NUM254 defineEFALUT1staticssize tmy read structfile filp char buf ssize tsize loff t off staticssize tmy write structfile flip constchar buf ssize tsize loff t off structfile operationsmy ops read my read write my write staticintglobal var 0 主设备号 linux字符设备驱动程序 运行于rh9linux 赖晓晨 include include include includeMODULE LICENSE GPL defineMAJOR NUM254 defineEFALUT1staticssize tmy read structfile filp char buf ssize tsize loff t off staticssize tmy write structfile flip constchar buf ssize tsize loff t off structfile operationsmy ops read my read write my write staticintglobal var 0 定义宏表示错误 linux字符设备驱动程序 运行于rh9linux 赖晓晨 include include include includeMODULE LICENSE GPL defineMAJOR NUM254 defineEFALUT1staticssize tmy read structfile filp char buf ssize tsize loff t off staticssize tmy write structfile flip constchar buf ssize tsize loff t off structfile operationsmy ops read my read write my write staticintglobal var 0 读函数 从flip指定的文件中读size个字符到buf中 linux字符设备驱动程序 运行于rh9linux 赖晓晨 include include include includeMODULE LICENSE GPL defineMAJOR NUM254 defineEFALUT1staticssize tmy read structfile filp char buf ssize tsize loff t off staticssize tmy write structfile flip constchar buf ssize tsize loff t off structfile operationsmy ops read my read write my write staticintglobal var 0 写函数 把buf中的size个字符写到flip文件中 linux字符设备驱动程序 运行于rh9linux 赖晓晨 include include include includeMODULE LICENSE GPL defineMAJOR NUM254 defineEFALUT1staticssize tmy read structfile filp char buf ssize tsize loff t off staticssize tmy write structfile flip constchar buf ssize tsize loff t off structfile operationsmy ops read my read write my write staticintglobal var 0 函数指针设置 linux字符设备驱动程序 运行于rh9linux 赖晓晨 include include include includeMODULE LICENSE GPL defineMAJOR NUM254 defineEFALUT1staticssize tmy read structfile filp char buf ssize tsize loff t off staticssize tmy write structfile flip constchar buf ssize tsize loff t off structfile operationsmy ops read my read write my write staticintglobal var 0 驱动保存的内核数据 staticint initmy init void intret ret register chrdev MAJOR NUM mydev 模块初始化函数 staticint initmy init void intret ret register chrdev MAJOR NUM mydev 向系统注册驱动 参数分别是设备号 设备名 函数集合 staticint initmy init void intret ret register chrdev MAJOR NUM mydev 显示注册结果 staticint initmy init void intret ret register chrdev MAJOR NUM mydev 模块退出函数 staticint initmy init void intret ret register chrdev MAJOR NUM mydev 从系统卸载驱动 参数分别是设备号 设备名 staticint initmy init void intret ret register chrdev MAJOR NUM mydev 显示卸载结果 staticssize tmy read structfile filp char buf ssize tsize loff t off if copy to user buf 注册初始化和卸载函数 staticssize tmy read structfile filp char buf ssize tsize loff t off if copy to user buf 读函数定义 staticssize tmy read structfile filp char buf ssize tsize loff t off if copy to user buf 把数据从内核空间拷贝到用户空间 从global var读到buf staticssize tmy read structfile filp char buf ssize tsize loff t off if copy to user buf 写函数定义 staticssize tmy read structfile filp char buf ssize tsize loff t off if copy to user buf 把数据从用户空间拷贝到内核空间 从buf读到global var makefile INCLUDEDIR usr src linux 2 4 includeCC gccCFLAG D KERNEL DMODULE DLINUX I INCLUDEDIR all hello ohello o hello c CC CFLAG chello cclean rm f o makefile INCLUDEDIR usr src linux 2 4 includeCC gccCFLAG D KERNEL DMODULE DLINUX I INCLUDEDIR all hello ohello o hello c CC CFLAG chello cclean rm f o 内核运行 模块 测试 在命令执行make 用insmod插入模块 然后在 dev目录下建立一个设备文件 主设备号为254 此设备号为0 字母c代表字符设备 命令如下 mknod dev mydevc2540 编写测试程序 接下来编写测试程序 例5 21 include include include includevoidmain intfd num fd open dev mydev O RDWR S IRUSR S IWUSR if fd 1 read fd include include include includevoidmain intfd num fd open dev mydev O RDWR S IRUSR S IWUSR if fd 1 read fd 打开设备文件 include include include includevoidmain intfd num fd open dev mydev O RDWR S IRUSR S IWUSR if fd 1 read fd 如果打开成功 include include include includevoidmain intfd num fd open dev mydev O RDWR S IRUSR S IWUSR if fd 1 read fd 从fd文件读4字节到num include include include includevoidmain intfd num fd open dev mydev O RDWR S IRUSR S IWUSR if fd 1 read fd 输出显示 读取新数据 include include include includevoidmain intfd num fd open dev mydev O RDWR S IRUSR S IWUSR if fd 1 read fd 把新数据写入内核空间 include include include includevoidmain intfd num fd open dev mydev O RDWR S IRUSR S IWUSR if fd 1 read fd 从fd文件读4字节到num include include include includevoidmain intfd num fd open dev mydev O RDWR S IRUSR S IWUSR if fd 1 read fd 输出显示新数据 include include include includevoidmain intfd num fd open dev mydev O RDWR S IRUSR S IWUSR if fd 1 read fd 关闭设备文件 二 ubuntu驱动程序实例 本小节虚拟了一个2 6内核字符驱动程序 仍然通过内核信息访问来介绍驱动架构 可以运行于2 6啮合的ubuntu系统之中 例5 22 driver cMakefiletest c 例5 22 ch5 22driver c驱动程序 include include include include include defineDEV MAJOR200 defineDEV MINOR0MODULE LICENSE CasmSSDUT GPL char buf hello world n staticintdriver open structinode driver inode structfile driver file staticintdriver release structinode driver inode structfile driver file staticssize tdriver read structfile p file char u buffer size tcount loff t ppos dev tdriver dev structcdev driver cdev 例5 22 structfile operationsdriver fops owner THIS MODULE read driver read open driver open release driver release staticintdriver open structinode p inode structfile p file return 0 staticintdriver release structinode p inode structfile p file return 0 staticssize tdriver read structfile p file char u buffer size tcount loff t ppos intrc rc copy to user u buffer buf count return rc 例5 22 staticintdriver init void intrc driver dev MKDEV DEV MAJOR DEV MINOR rc register chrdev region driver dev 1 driver if rcowner THIS MODULE driver cdev ops 例5 22 staticvoiddriver exit void cdev del driver cdev unregister chrdev region driver dev 1 printk KERN ALERT ExitDeviceSuccessfully n module init driver init 以上内容为driver c 例5 22 staticvoiddriver exit void cdev del driver cdev unregister chrdev region driver dev 1 printk KERN ALERT ExitDeviceSuccessfully n module init driver init 以上内容为driver c下面介绍Makefile Makefile PWD shellpwd KERNEL SRC usr src linux headers 2 6 15 23 386 obj m driver oall MAKE C KERNEL SRC M PWD modulesclean rm korm o test c ch5 22test c驱动测试程序 include includeintmain charbuffer 64 intcount intfd fd open dev driver 0 read fd buffer 20 printf s buffer close fd return0 Makefile ch5 22test c驱动测试程序 include includeintmain charbuffer 64 intcount intfd fd open dev driver 0 read fd buffer 20 printf s buffer close fd return0 运行 make gcc otesttest c sudoinsmod scull ko sudomknod dev scullc2000 sudochmod777 dev scull 三 嵌入式设备linux驱动实例 本小节展示一个运行于EmbestEduKit 型教学实验平台 S3C44B0X uClinux系统的led驱动实例 实验原理图 共阳极 输出低电平点亮led 1 编写驱动程序 设计LED驱动程序 首先定义一个structunit的结构 structunit 定义一个结构类型 成员是整型指针 代表各个口寄存器 以及两个变量structsemaphorelock 定义信号量u32 PCONB PCONBregister u32 PDATB PDATBregister u32 PCONF PCONFregister u32 PDATF PDATFregister u32 PUPF PUPFregister u32b storeLED1andLED2value u32f storeLED3andLED4value 1 编写驱动程序 续 LED字符设备驱动程序实现的函数 staticstructfile operationsled ops owner THIS MODULE read led read write led write open led open release led release 例5 23 代码讲解 ch5 23 Makefile ch5 23驱动程序编译Makefile Makefileforthekernelleddriver O TARGET led oexport objs obj y ekii led oinclude TOPDIR Rules make 2 编译驱动程序至内核 1 在 uClinux dist linux 2 4 x drivers目录下新建名为led的文件夹 把led驱动程序和makefile复制到其中 2 修改 uClinux dist linux 2 4 x drivers Makefile文件 在Makefile中添加如下加粗代码 2 编译驱动程序至内核 续 MakefileforLEDdrivers mod subdirs diohilmtdsbusvideomacintoshusbinputtelephonyide message i2omessage fusionscsimdieee1394pnpisdnatm fc4net hamradioi2cacpibluetoothusb gadgetsubdir y parportcharblocknetsoundmiscmediacdromhotplugsubdir y ledsubdir m subdir y 2 编译驱动程序至内核 续 修改 uClinux dist linux 2 4 x Makefile文件 在 DRIVERS y 之后 添加如下加粗代码 这样在连接uClinux内核映像文件时 能把led o连接进去 2 编译驱动程序至内核 续 DRIVERS CONFIG ACPI BOOT drivers acpi acpi oDRIVERS CONFIG PARPORT drivers parport driver oDRIVERS y drivers char char o drivers block block o drivers misc misc o drivers net net oDRIVERS y drivers led led o 2 编译驱动程序至内核 续 修改 uClinux dist vendors Embest EduKit Makefile文件 在 DRIVERS 后 添加如下加粗代码 led0 c 60 0 其中led0为设备名称 c代表字符设备 60为主设备号 最后一个0是从设备号 主设备号与驱动程序注册时的主设备号要求一致 否则用户程序用设备文件名称请求打开设备时 内核无法根据主设备号找到对应的设备驱动程序 2 编译驱动程序至内核 续 DEVICES tty c 5 0console c 5 1cua0 c 5 64cua1 c 5 65 led0 c 60 0 2 编译驱动程序至内核 续 在 uClinux dist目录下 执行以下操作 makedepmakelib onlymakeuser onlymakeromfsmakeimage将生成的内核烧写到目标板flash的5 35扇区 2 编译驱动程序至内核 续 启动uClinux 使用cp命令 简单测试一下LED驱动程序 把任意一个文件复制到led设备上 检查是否能点亮其中某个LED 例如 cp bin init dev led0 3 编写测试程序 重启动目标板 打开超级终端用ls命令观察 dev下的设备名 寻找名为 led0 的设备 交叉编译文件 test led c 然后用tftp工具把可执行文件下载到实验板并运行 结合 test led c 源代码 观察超级终端的输出以及实验板上led灯的变化 测试程序 ch5 23LED驱动测试程序 include include include include defineLED NUM4intmain intargc char argv inti j wval rval fd printf TestLED n fd open dev led0 O RDWR 测试程序 续 for i 0 i 3 i for j 0 j LED NUM j wval 1 j write fd 测试程序 续 for i 0 i 6 i if i 2 wval 0 x00 write fd 测试程序 续 else wval 0 x0f write fd Makefile ch5 23测试程序编码Makefile makeDEBUG 1构造调试版本 make构造优化版本 makeclean清除构造生成的文
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年N1级执业医师护理士内科护理模拟习题(附答案)
- 水果景观工程创新创业项目商业计划书
- 2025年煤气考试题库附答案
- 大学生的恋爱观调查报告
- 2025年景观与园林设计师岗位职业技能资格考试题库含答案
- 2025年国家保安员资格考试试题(答案+解析)
- 2025年动画场景设计试题及答案
- 2025年保育员(幼儿教育法规与政策)考试试卷(附答案)
- Unit5StartingoutUnderstandingideas阅读课课件外研版七年级英语上册
- 第6课《散步》课件统编版语文七年级上册-3
- 表B.0.4-分包单位资格报审表(通用模板)
- 幼儿园假期安全教育内容知识
- 小学教育课件教案雪雕和冰雕的历史与传统
- 新版出口报关单模板
- 员工工资条模板
- 新教科版科学六年级上册知识点
- 初中历史小论文现状分析与写作探讨
- 新疆地方史课件
- 一粒种子旅行
- GB/T 9124-2010钢制管法兰技术条件
- GB 4287-1992纺织染整工业水污染物排放标准
评论
0/150
提交评论