2Linux驱动编写

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1Linux驱动编写张黎明leeming.cublog.cnleeming1203@gmail.com2Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.主要内容:ƒ1.Linux驱动简述及字符型驱动的框架ƒ2.基于Gpio的Linux字符型驱动设计ƒ3.Linux键盘驱动的设计31.Linux驱动简述及字符型驱动的框架4Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.ƒ设备驱动程序是操作系统内核和机器硬件之间的接口.设备驱动程序为应用程序屏蔽了硬件的细节,这样在应用程序看来,硬件设备只是一个设备文件,应用程序可以象操作普通文件一样对硬件设备进行操作.设备驱动程序是内核的一部分,它完成以下的功能:1.对设备初始化和释放.2.把数据从内核传送到硬件和从硬件读取数据.3.读取应用程序传送给设备文件的数据和回送应用程序请求的数据.4.检测和处理设备出现的错误.1.1什么是设备驱动5Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.应用程序硬件设备writereadioctlioctl驱动程序设备功能设备功能设备注册设备卸载Init_module()cleanup_module()内核模块设备,驱动,内核,应用程序之间的调用关系:6Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.7Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.应用程序VFS字符设备驱动文件系统(ext2,yaffs……)其他设备驱动字符设备其他设备系统调用file_operations设备,驱动,文件系统,应用程序之间的调用关系:8Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.ƒ⑴核心代码:设备驱动程序是核心的一部分,像核心中其他的代码一样,出错将导致系统的严重损伤。一个编写不当的设备驱动程序甚至能够使系统崩溃导致文件系统的破坏和数据的丢失;ƒ⑵标准接口:设备驱动程序必须为Linux核心或者其从属的子系统提供一个标准的接口;ƒ⑶核心机制:设备驱动程序可以使用标准的核心服务比如内存分配、中断发送和等待队列等;ƒ⑷动态可加载:多数的Linux设备驱动程序可以在核心模块发出加载请求时进行加载,同时在不使用设备时进行卸载,这样核心可以有效地利用系统的资源ƒ⑸可配置:Linux设备驱动属于核心的一部分,用户可以根据自己的需要进行配置来选择适合自己的驱动1.2设备驱动程序特点9Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.ƒ字符设备以字节为单位逐个进行I/O操作字符设备中的缓存是可有可无不支持随机访问,如串口设备ƒ块设备块设备的存取是通过buffer、cache来进行可以进行随机访问例如IDE硬盘设备可以支持可安装文件系统ƒ网络设备通过BSD套接口访问,网络接口–任何网络事务处理都是通过接口实现的,即,可以和其他宿主交换数据的设备。通常接口是一个硬件设备,但也可以象loopback(回路)接口一样是软件工具。由于不是面向流的设备,所以网络接口不能象/dev/tty1那样简单地映射到文件系统的节点上。Unix调用这些接口的方式是给它们分配一个独立的名字(如eth0)。这样的名字在文件系统中并没有对应项。内核和网络设备驱动程序之间的通信与字符设备驱动程序和块设备驱动程序与内核间的通信是完全不一样的。内核不再调用read,write,它调用与数据包传送相关的函数。1.3Linux设备的分类10Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.ƒLinux抽象了对硬件的处理,所有的硬件设备都可以作为普通文件一样来看待ƒ可以使用和操作文件相同的、标准的系统调用接口来完成打开、关闭、读写和I/O控制操作,对用户来说,设备文件与普通文件并无区别ƒ字符设备和块设备是通过文件节点访问的。在Linux的文件系统中,可以找到(或者使用mknod创建)设备对应的文件名,称这种文件为设备文件。ƒ主设备号:标识该设备的种类,也标识了该设备所使用的驱动程序Linux内核支持动态分配主设备号ƒ次设备号:标识使用同一设备驱动程序的不同硬件设备每次内核调用一个设备驱动程序时,它都告诉驱动程序它正在操作哪个设备。主设备号和从设备号合在一起构成一个数据类型并用来标别某个设备。MKDEV(ma,mi)((ma)8|(mi))1.4Linux设备文件的概念11Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.ƒLinux设备驱动程序的代码结构大致可以分为如下几个部分:1.5Linux字符型驱动程序框架12Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.staticintmy_open(structinode*inode,structfile*filp){设备打开时的操作…}staticintmy_release(structinode*inode,structfile*filp){设备关闭时的操作…}staticintmy_write(structfile*file,constchar*buffer,size_tcount,loff_t*ppos){设备写入时的操作…}staticintmy_read(structfile*file,constchar*buffer,size_tcount,loff_t*ppos){设备读取时的操作…}一个最简单字符驱动程序,由下面7个函数和1个结构体就可组成。Open(),Release,()Write(),Read()Ioctl()Init(),Exit()Structfile_operation13Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.staticint__initmy_init(void){初始化硬件,注册设备,创建设备节点…}staticvoid__exitmy_exit(void){删除设备节点,注销设备…}Staticintmy_ioctl(structinode*inode,structfile*filp,unsignedintcmd,unsignedlongarg){设备的控制操作……}staticstructfile_operationsmy_fops={对文件操作结构体成员定义初始值…}14Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.15Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.文件操作结构体初始化staticstructfile_operationsmy_fops={.owner=THIS_MODULE,.read=sep4020_key_read,.write=sep4020_key_write,.ioctl=sep4020_key_ioctl,.open=sep4020_key_open,.release=sep4020_key_release,};162.基于Gpio的Linux字符型驱动设计17Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.ƒ第一步:编写字符设备驱动ƒ第二步:加载ƒ第三步:编写应用程序测试设备驱动2.1流水灯Linux驱动步骤18Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.2.2第一步:编写流水灯Linux驱动ƒ内容如下:#defineKEY_MAJOR249/*主设备号*/#defineLED_ON1#defineLED_OFF2structled_dev{structcdevcdev;unsignedcharvalue;};structled_dev*leddev;在/linux-3.2/driver/char/sep4020_char/下面新建一个sep4020_flowled.c注意这个目录在后面会用到19Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.打开和关闭操作ƒopen和release函数会在设备打开和关闭时被调用,open的时候对设备进行初始化staticintsep4020_flowled_open(structinode*inode,structfile*filp){sep4020_flowled_setup();……}staticintsep4020_flowled_release(structinode*inode,structfile*filp……return0;}20Copyright2007ProchipElectronicsCo,ltd.AllRightsReserved.Nottobereproducedbyanymeanswithoutpriorwrittenconsent.写入和读出操作staticintsep4020_flowled_write(structfile*file,constchar*buffer,size_tcount,loff_t*ppos){return0;}staticintsep4020_flowled_read(structfile*

1 / 59
下载文档,编辑使用

©2015-2020 m.111doc.com 三一刀客.

备案号:赣ICP备18015867号-1 客服联系 QQ:2149211541

×
保存成功