欢迎来到天天文库
浏览记录
ID:44209278
大小:81.77 KB
页数:4页
时间:2019-10-19
《linux简单的设备驱动程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、简单的设备驱动程序一、实验目的:动手实践一个简单的字符型设备驱动程序,学习驱动程序编写的基本步骤。二、实验内容:编写一个简单的字符型设备驱动程序。三、实验设备:PC机,REDHAT软件,配置好ARMLINUX的开发环境。四、实验步骤:1、编写简单的字符型设备驱动程序,学习编写驱动程序的过程。打开VI,编写以下代码://devicedriverprogram#include/*内核编译时的配置文件,文件里面指向一个makemenuconfig自动生成的文件autoconf
2、.h*/#include//模块驱动程序的头文件#include#include〃中断相关的头文件#include#include〃定时器相关的头文件#include#include#include#include3、h>//文件中定义了LINUX内核的所有的版本,用于版本检查#include//访问系统硬件用的头文件#include#include#include#defineSimpleD.MAJOR96//定义主设备号devfs_handle_tdev_handle;//系统DEVFS所用到的handleintSimpleD_temp_count=0;//定义系统变暈,防止文件被多重打开/4、*SimpledRead*/ssize_tSimpleD_read(structfile*file,char*buf,size_tcountJoffLt*fL()ps){printk("S3c2410:devicefile-Readoperation!");returncount;printk(HS3c2410:devicefile-writeoperation!n);returncount;printk(HS3c2410:devicefile-ioctloperation!H);Simp5、leddeviceopen*/ssize_tSimpleD_open(structinode*inode,structfile*file)return0;}/*Simpleddeviceclose-*/ssize_tSimpleD_release(structinode*inode,structfile*file){devfs_unregister_chrdev(SimpleD_MAJOR,"SimpleD");devfs_unregister(dev_handle);return0;}/^Simpl6、eD设备向系统注册用的OPS结构,里面是对应的操作*/structfile_operationsSimpleD_ops={open:SimpleD_open,read:SimpleD_read,write:SimpleD_write,ioctl:SimpleD_ioctl,release:SimpleD_release,};严初始化设备的函数*/staticint_initHW_SimpleD_init(void){intret=-ENODEV;intdelay;〃注册字符型设备ret=devfs_r7、egister_chrdev(SimpleD_MAJOR,,'SimpleD,',&SimpleD_ops);if(rel<0){printk("S3c2410:init_modulefailedwith%d",ret);returnret;}else}printk(KERN」NFO"S3c241osimpleDregistersuccess!!!");1dev_handle=devfs_register(NULL;,SimpleDH,DEVFS_FL_DEFAULT,96,0,S_IFCHR8、,&SipleD_ops,NULL)returnret;}〃系统初始化staticint_inits3c2410_SimpleD_init(void){intret=-ENODEV;ret=HW_SimpleDif(ret)returnret;return0;〃模块初始化函数intinit_module(){s3c_2410_SimpleD」nit();I//模块卸载函数voidcleanup_module(){printk(HModulecleanup
3、h>//文件中定义了LINUX内核的所有的版本,用于版本检查#include//访问系统硬件用的头文件#include#include#include#defineSimpleD.MAJOR96//定义主设备号devfs_handle_tdev_handle;//系统DEVFS所用到的handleintSimpleD_temp_count=0;//定义系统变暈,防止文件被多重打开/
4、*SimpledRead*/ssize_tSimpleD_read(structfile*file,char*buf,size_tcountJoffLt*fL()ps){printk("S3c2410:devicefile-Readoperation!");returncount;printk(HS3c2410:devicefile-writeoperation!n);returncount;printk(HS3c2410:devicefile-ioctloperation!H);Simp
5、leddeviceopen*/ssize_tSimpleD_open(structinode*inode,structfile*file)return0;}/*Simpleddeviceclose-*/ssize_tSimpleD_release(structinode*inode,structfile*file){devfs_unregister_chrdev(SimpleD_MAJOR,"SimpleD");devfs_unregister(dev_handle);return0;}/^Simpl
6、eD设备向系统注册用的OPS结构,里面是对应的操作*/structfile_operationsSimpleD_ops={open:SimpleD_open,read:SimpleD_read,write:SimpleD_write,ioctl:SimpleD_ioctl,release:SimpleD_release,};严初始化设备的函数*/staticint_initHW_SimpleD_init(void){intret=-ENODEV;intdelay;〃注册字符型设备ret=devfs_r
7、egister_chrdev(SimpleD_MAJOR,,'SimpleD,',&SimpleD_ops);if(rel<0){printk("S3c2410:init_modulefailedwith%d",ret);returnret;}else}printk(KERN」NFO"S3c241osimpleDregistersuccess!!!");1dev_handle=devfs_register(NULL;,SimpleDH,DEVFS_FL_DEFAULT,96,0,S_IFCHR
8、,&SipleD_ops,NULL)returnret;}〃系统初始化staticint_inits3c2410_SimpleD_init(void){intret=-ENODEV;ret=HW_SimpleDif(ret)returnret;return0;〃模块初始化函数intinit_module(){s3c_2410_SimpleD」nit();I//模块卸载函数voidcleanup_module(){printk(HModulecleanup
此文档下载收益归作者所有