linux下led小灯的驱动.doc_第1页
linux下led小灯的驱动.doc_第2页
linux下led小灯的驱动.doc_第3页
linux下led小灯的驱动.doc_第4页
linux下led小灯的驱动.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

我第一个字符设备驱动程序的痛苦经历2009-12-26 21:07写驱动程序是个痛苦成长的过程。对驱动的编译,先要有支持的内核源码树,所以先得编译内核。由于过了两天才来整理,写的东西有点杂乱了。1.按照那个入门设备驱动程序,来编译的时候老是错误,网络上有个参考的(/sabalol/archive/2008/02/01/2076610.aspx),但是他有一个严重的错误:编译linux源码的路径不对。他的错误如下:liuliu-desktop hellomod#make oldconfigliuliu-desktop hellomod#make prepare应该是:rootliu-desktop linux26#make oldconfigrootliu-desktop linux26#make prepare2.我下了linux内核后,直接make出现如下错误:rootlocalhost my_drivers# makemake -C /home/xinghun/linux- M=/home/xinghun/my_drivers modulesmake1: Entering directory /home/xinghun/linux-ERROR: Kernel configuration is invalid. include/linux/autoconf.h or include/config/auto.conf are missing. Run make oldconfig & make prepare on kernel src to fix it.make1: * include/config/auto.conf Error 1make1: Leaving directory /home/xinghun/linux-make: * default Error 2解决办法:红色部分(在linux内核目录下执行红色部分代码)rootlocalhost linux-# make preparescripts/kconfig/conf -s arch/arm/KconfigCHK include/linux/version.hUPD include/linux/version.hGenerating include/asm-arm/mach-types.hCHK include/linux/utsrelease.hUPD include/linux/utsrelease.hSYMLINK include/asm - include/asm-armCC kernel/bounds.sGEN include/linux/bounds.hCC arch/arm/kernel/asm-offsets.sGEN include/asm/asm-offsets.hCALL scripts/checksyscalls.sh:1097:2: warning: #warning syscall fadvise64 not implemented:1265:2: warning: #warning syscall migrate_pages not implemented:1321:2: warning: #warning syscall pselect6 not implemented:1325:2: warning: #warning syscall ppoll not implemented:1365:2: warning: #warning syscall epoll_pwait not implementedrootlocalhost linux-#看见几个warning很不爽,解决办法:make下内核就不会出现了。3.出现MODPOST 1 modules /bin/sh: scripts/mod/modpost: not found错误的解决办法:rootlocalhost linux-# make scriptsCC scripts/mod/empty.oHOSTCC scripts/mod/mk_elfconfigMKELF scripts/mod/elfconfig.hHOSTCC scripts/mod/file2alias.oHOSTCC scripts/mod/modpost.oHOSTCC scripts/mod/sumversion.oHOSTLD scripts/mod/modpostHOSTCC scripts/kallsymsHOSTCC scripts/conmakehashHOSTCC scripts/bin2crootlocalhost linux-#4.接下来可以编译自己的驱动了由于我在编译驱动程序之前没有对内核进行编译,所以我的编译结果如下:rootlocalhost my_drivers# makemake -C /home/xinghun/linux- M=/home/xinghun/my_drivers modulesmake1: Entering directory /home/xinghun/linux-WARNING: Symbol version dump /home/xinghun/linux-/Module.symvers is missing; modules will have no dependencies and modversions.Building modules, stage 2.MODPOST 1 modulesCC /home/xinghun/my_drivers/gpio_led.mod.oLD M /home/xinghun/my_drivers/gpio_led.komake1: Leaving directory /home/xinghun/linux-rootlocalhost my_drivers# lsgpio_led.c gpio_led.ko gpio_led.mod.o Makefile Module.symversgpio_led.h gpio_led.mod.c gpio_led.o modules.orderrootlocalhost my_drivers#解决办法:cd到内核目录下:sudo make zImage,这样就ok了。接下来重新编译驱动程序。5.接下来编译测试程序,不过我出现了如下错误:rootlocalhost gpio_led_test# make/opt/EmbedSky/4.3.3/bin/arm-linux-gcc -o gpio_led_test gpio_led_test.o/opt/EmbedSky/4.3.3/bin/./lib/gcc/arm-none-linux-gnueabi/4.3.3/././././arm-none-linux-gnueabi/bin/ld: gpio_led_test.o: Relocations in generic ELF (EM: 3)/opt/EmbedSky/4.3.3/bin/./lib/gcc/arm-none-linux-gnueabi/4.3.3/././././arm-none-linux-gnueabi/bin/ld: gpio_led_test.o: Relocations in generic ELF (EM: 3)/opt/EmbedSky/4.3.3/bin/./lib/gcc/arm-none-linux-gnueabi/4.3.3/././././arm-none-linux-gnueabi/bin/ld: gpio_led_test.o: Relocations in generic ELF (EM: 3)/opt/EmbedSky/4.3.3/bin/./lib/gcc/arm-none-linux-gnueabi/4.3.3/././././arm-none-linux-gnueabi/bin/ld: gpio_led_test.o: Relocations in generic ELF (EM: 3)/opt/EmbedSky/4.3.3/bin/./lib/gcc/arm-none-linux-gnueabi/4.3.3/././././arm-none-linux-gnueabi/bin/ld: gpio_led_test.o: Relocations in generic ELF (EM: 3)/opt/EmbedSky/4.3.3/bin/./lib/gcc/arm-none-linux-gnueabi/4.3.3/././././arm-none-linux-gnueabi/bin/ld: gpio_led_test.o: Relocations in generic ELF (EM: 3)gpio_led_test.o: could not read symbols: File in wrong formatcollect2: ld returned 1 exit statusmake: * gpio_led_test Error 1rootlocalhost gpio_led_test# 出现如上错误的原因是我链接库路径没设好,具体来说是我设置链接路径(系统默认为/usr目录里的lib),我设置了链接库路径后(LD = $(CROSS_COMPILE)ld,具体程序见后文),在重新编译就ok了。6.安装驱动,出现如下错误rootEmbedSky gpio_led_driver# insmod gpio_led.kogpio_led: version magic mod_unload ARMv4 should be -EmbedSinsmod: cannot insert gpio_led.ko: invalid module formatrootEmbedSky gpio_led_driver# 那是因为天嵌公司在linux版本中加了自己的标志,这要在menuconfig中改。解决办法如下:在配置单中添加如下信息General setup - Prompt for development and/or incomplete code/drivers(-EmbedSky) Local version - append to kernel release内核版本的差异导致的。7.在基于tq2440开发板gpio_led驱动中我还出现了一个这样的问题。比如,在裸机开发中,我们可以#define GPBCON (*(volatile unsigned long *) 0x56000010)GPBCON=0x015400;/设置GPB5,GPB6,GPB7,GPB8为输出但是在驱动程序中不能直接这样写,如果你这样写了,编译还是能通过,但是加载的时候会出现错误。因为linux开启了mmu,GPBCON的虚拟地址不是0x56000010,不能像裸机下那样直接对GPBCON的实际地址内容进行修改。具体的编写参考:/lijintao_27/232701/message.aspx,其实google下,你会发现有许多这方面的内容。8.好了,废话说了这么多,我的驱动程序如下:/* gpio_led.h */#ifndef _LED_H#define _LED_H/GPIO_LED DEVICE MAJOR#define GPIO_LED_NAME gpio_led#define GPIO_LED_DEVICE_FILENAME /dev/gpio_led/声明4个LED灯的I/O端口#define LED1 (1 5)#define LED2 (1 6)#define LED3 (1 7)#define LED4 (1 8)/定义LED灯状态#define LED_ON 0#define LED_OFF 1/定义控制命令#define ctl_GPIO_LED1 1#endif/* gpio_led.c */#include /配置头文件#include /* printk */#include /#include #include /* struct file, struct file_operations */#include /* module_init, module_exit */#include /* struct cdev, cdev_init, . */#include #include /* module_param */#include #include #include gpio_led.h static int major = 0; /* 采用字符设备号的动态分配 */module_param(major, int, 0);ssize_t GPIO_LED_read(struct file *file, char *buf, size_t count, loff_t *f_ops)return count;ssize_t GPIO_LED_write(struct file *file, const char _user *buf, size_t count, loff_t *f_ops)return count;int GPIO_LED_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long data)switch(cmd)case LED_ON: s3c2410_gpio_setpin(S3C2410_GPB5, LED_ON); s3c2410_gpio_setpin(S3C2410_GPB6, LED_ON); s3c2410_gpio_setpin(S3C2410_GPB7, LED_ON); s3c2410_gpio_setpin(S3C2410_GPB8, LED_ON); printk(_led on-kernel-n); break;case LED_OFF: s3c2410_gpio_setpin(S3C2410_GPB5, LED_OFF); s3c2410_gpio_setpin(S3C2410_GPB6, LED_OFF); s3c2410_gpio_setpin(S3C2410_GPB7, LED_OFF); s3c2410_gpio_setpin(S3C2410_GPB8, LED_OFF); printk(_led off-kernel-n); break;default: printk(no cmd run-kernel-n);return 0;ssize_t GPIO_LED_open(struct inode *inode, struct file *file)s3c2410_gpio_cfgpin(S3C2410_GPB5, S3C2410_GPB5_OUTP);s3c2410_gpio_cfgpin(S3C2410_GPB6, S3C2410_GPB6_OUTP);s3c2410_gpio_cfgpin(S3C2410_GPB7, S3C2410_GPB7_OUTP);s3c2410_gpio_cfgpin(S3C2410_GPB8, S3C2410_GPB8_OUTP);printk(_openn);return 0; int GPIO_LED_release(struct inode *inode, struct file *file)printk(_releasen);return 0; static struct file_operations GPIO_LED_fops =.owner = THIS_MODULE,.open = GPIO_LED_open,.read = GPIO_LED_read,.write = GPIO_LED_write,.ioctl = GPIO_LED_ioctl,.release = GPIO_LED_release,;struct cdev *gpio_led_devs;static int _init gpio_led_init(void)int result, err;dev_t dev = MKDEV(major, 0);if(major) /* 设备号的静态分配 */ result = register_chrdev_region(dev, 1, GPIO_LED_NAME);else result = alloc_chrdev_region(&dev, 0, 1, GPIO_LED_NAME); major = MAJOR(dev);if(result owner = THIS_MODULE;err = cdev_add(gpio_led_devs, dev, 1);if(err) printk(KERN_NOTICE Error %d adding gpion, err); return err;return 0;static void _exit gpio_led_exit(void)cdev_del(gpio_led_devs); /* 字符设备的注销 */unregister_chrdev_region(MKDEV(major, 0), 1); /* 设备号的注销 */printk(Gpio: device uninstalledn);module_init(gpio_led_init);module_exit(gpio_led_exit);MODULE_LICENSE(GPL);MODULE_AUTHOR(xinghun);MODULE_DESCRIPTION(A simple GPIO LED test);/* gpio_led驱动Makefile */DEVICE = gpio_ledifneq ($(KERNELRELEASE),)obj-m := $DEVICE.oelseKERNELDIR = /home/xinghun/linux-PWD := $(shell pwd)default:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesendifclean:rm -rf $DEVICE.o $DEVICE.ko $DEVICE.mod* Module.symvers9.gpio_led测试程序/* gpio_led_test.c */#include #include #include #include #include #include #include #include #define GPIO_LED_DEVICE_FILENAME /dev/gpio_

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论