Linux下设备驱动程序的开发(led流水灯的驱动)

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

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

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

资源描述

设备驱动程序的开发流程进行嵌入式Linux系统的开发,很大的工作量是为各种设备编写驱动程序。在ARM平台上开发嵌入式Linux的设备驱动程序与在其他平台上开发是一样的。总的来说,实现一个嵌入式Linux设备驱动的大致流程如下:(1)查看原理图,理解设备的工作原理(2)定义主设备号(3)在驱动程序中实现驱动的初始化。如果驱动程序采用模块的方式,则要实现模块初始化。(4)设计所要实现的文件操作,定义file_operations结构。(5)实现中断服务(中断并不是每个设备驱动所必须的)(6)编译该驱动程序到内核中,或者用insmod命令加载(7)测试该设备3.2linux下字符设备的驱动开发实例----LED驱动(可参考FS2410P实验指导手册v2.1.2.pdf,302-313)(1)实验内容:4个LED灯轮流闪烁本节要求实现在一个字符设备驱动里面实现对GPIO端口的操作。●在模块加载的时候跑马灯运行起来●模块卸载的时候,跑马灯停止。FS2410P上的4个LED指示灯由4个I/O口控制,它们分别是:GPF4~GPF7,输出低电平时候,相应的LED指示灯亮。(2)LED的原理图FS2410P带有4个用户可编程I/O方式LED,下表为LED对应的I/O口。表1用户指示灯占用CPU资源列表序号名字CPU端口资源1LED1GPF42LED2GPF53LED3GPF64LED4GPF7图1LED原理图(3)LED驱动源代码及说明◆在/s3c2410下新建一个目录:gpiodrv●#mkdir/s3c2410/gpiodrv●#cd/s3c2410/gpiodrv◆在/s3c2410/gpiodrv目录下用vi编辑器编写符合上面功能的驱动源程序gpiodrv.c●#cd/s3c2410/gpiodrv●#vigpiodrv.c#includelinux/config.h#includelinux/module.h#includelinux/fs.h#includelinux/kernel.h#includelinux/init.h#includelinux/iobuf.h#includelinux/major.h#includeasm/uaccess.h#includeasm/hardware.h#includeasm/arch/cpu_s3c2410.h#includeasm/io.h#includelinux/vmalloc.h#includelinux/delay.h#defineIOPORT_MAJOR220intmagic_leds_open(structinode*inode,structfile*filp);intmagic_leds_ioctl(structinode*inode,structfile*filp,unsignedintcmd,unsignedlongarg);intmagic_leds_release(structinode*inode,structfile*filp);staticstructfile_operationsmagic_leds_fops={ioctl:magic_leds_ioctl,open:magic_leds_open,release:magic_leds_release,};#defineLED1_ON()(GPFDAT&=~0x10)#defineLED2_ON()(GPFDAT&=~0x20)#defineLED3_ON()(GPFDAT&=~0x40)#defineLED4_ON()(GPFDAT&=~0x80)#defineLED1_OFF()(GPFDAT|=0x10)#defineLED2_OFF()(GPFDAT|=0x20)#defineLED3_OFF()(GPFDAT|=0x40)#defineLED4_OFF()(GPFDAT|=0x80)staticintledStatus;voidLedSet(intled){ledStatus=led;if(ledStatus&1)LED1_ON();elseLED1_OFF();if(ledStatus&2)LED2_ON();elseLED2_OFF();if(ledStatus&4)LED3_ON();elseLED3_OFF();if(ledStatus&8)LED4_ON();elseLED4_OFF();}voidLedDisy(void){LedSet(0x08);udelay(0x500000);LedSet(0x04);udelay(0x500000);LedSet(0x02);udelay(0x500000);LedSet(0x01);udelay(0x500000);LedSet(0x02);udelay(0x500000);LedSet(0x04);udelay(0x500000);LedSet(0x08);udelay(0x500000);}staticint__initmagic_leds_init(void){intresult=0;printk(magic_leds_initn);result=register_chrdev(IOPORT_MAJOR,gpio,&magic_leds_fops);if(result0){printk(Failedtoregistermajor.n);returnresult;}printk(successtoregistern);return0;}intmagic_leds_open(structinode*inode,structfile*filp){GPFCON=0x5500;GPFUP=0xff;printk(opengpiodevicesn);return0;}void__exitmagic_leds_exit(void){unregister_chrdev(IOPORT_MAJOR,gpio);}int__exitmagic_leds_release(structinode*inode,structfile*filp){printk(releasethisdevicen);return0;}intmagic_leds_ioctl(structinode*inode,structfile*filp,unsignedintcmd,unsignedlongarg){interr=0;if(cmd==1){while(arg--){LedDisy();printk(.....);}printk(n);return0;}returnerr;}module_init(magic_leds_init);module_exit(magic_leds_exit);(3)编译安装LED驱动◆同样,在/s3c2410/gpiodrv目录下用vi编辑器编写该驱动程序的Makefile文件:(Makefile的编写可参考Makefile中文教程.pdf)●#viMakefile输入以下内容:CROSS=arm-linux-gccCFLAGS=-D__KERNEL__CFLAGS+=-DMODULECFLAGS+=-I/s3c2410/2.4.18-rmk7/includeCFLAGS+=-I/s3c2410/2.4.18-rmk7/include/linuxCFLAGS+=-I/usr/local/arm/2.95.3/includeCFLAGS+=-Wall-Wstrict-prototypes-Wno-trigraphs-Os-mapcsCFLAGS+=-fno-strict-aliasing-fno-common-fno-common-pipe-mapcs-32CFLAGS+=-march=armv4-mtune=arm9tdmi-mshort-load-bytes-msoft-floatCFLAGS+=-DKBUILD_BASENAME=gpiodrvall:gpiodrv.ogpiodrv.o:gpiodrv.c$(CROSS)$(CFLAGS)-ogpiodrv.o-cgpiodrv.cclean:-rm-f$(EXEC)*.o*~core将gpiodrv.c和Makefile这个放置在同一个新建目录下gpiodrv下,进入这个目录,输入make后,编绎成功后将在这个目录下生成一个gpiodrv.o文件。●#cd/s3c2410/gpiodrv●#make3.3linux下字符设备的驱动开发实例—测试LED(1)在/s3c2410/gpiodrv目录下用vi编辑器编写led驱动程序相应的测试程序gpio_test.c●#vigpio_test.c#includestdio.h#includestdlib.h#includeunistd.h#includefcnt1.h#includeerrno.h#includelinux/delay.h#includesys/ioctl.hintmain(intargc,char**argv){inti;intfd;fd=open(/dev/gpio,0);if(fd0){perror(Failedtoopendevice);exit(1);}while(1)printf(pleaseselectnumbertorunprogramn);printf(1:ledonn2:quit);scanf(%d,&val);if(val==1)ioct1(fd,1,10);elseif(val==2){close(fd);}return0;}编译gpio_test.c,得到可执行文件gpio_test。即用下面的命令:#arm-linux-gcc–ogpio_testgpio_test.c3.4linux下字符设备的驱动开发实例—实验步骤(也可参考FS2410P实验指导手册v2.1.2.pdf,311-313上的方法)(1)PC机进入LINUX系统,配置好minicom,连接好串口线,让FS2410P教学实验平台进入LINUX环境,利用minicom来显示。(2)将编绎生成的gpiodrv.o和gpio_test用NFSmount到/tmp目录下。(方法参考实验三---通过NFS进入映射)#mount192.168.3.111:/s3c2410/tmp#cd/tmp#cd/gpiodrv(3)加载设备驱动gpiodrv.o模块:insmodgpiodrv.o如果加载成功,可以通过cat/proc/devices命令查看该设备的相关信息。卸载该设备驱动模块的命令是:rmmodgpiodrv(4)建立gpio设备节点:mknod/dev/gpioc2200/dev/gpio为该设备驱动程序的设备名,C表明该设备为字符设备,220为该设备的主设备好,0为从设备号。(5)执行gpio_test程序:./gpio_test(6)在minicon终端选择1,回车,可以看到4个LED灯轮流闪烁。选择2,则退出程序的运行。(7)将应用程序添加根文件系统,并烧写到开发板。◆将FS2410XP_camare_demo.cramfs拷贝到/s3c2410目录下。◆在该目录下建立两个文件:#cd/s3c2410/#mkdirchang#mkdirguo◆将FS2410XP_camare_demo.cramfs挂接到chang目录。#mount-oloopFS2410XP_camare_demo.cramfschang◆将chang目录下的内容压缩。#cdchang#tar-cvf/s3c2410/1.tar./这时,将在chang的目录产生一个1.tar的包。#cd..#mv1.targuo#cdguo#tar-xvf1.tar#rm1.tarrm:是否删除一般文件“1.tar”?y◆将自已的gpiodrv.o和gpio_test拷贝到相应的目录下。将gpio

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

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功