300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > linux-2.6.32在mini2440开发板上移植(16)之LED 驱动程序移植

linux-2.6.32在mini2440开发板上移植(16)之LED 驱动程序移植

时间:2024-01-06 03:10:05

相关推荐

linux-2.6.32在mini2440开发板上移植(16)之LED 驱动程序移植

LED 驱动程序移植

编者;对于led的驱动程序,很多文章都有详细的介绍,我的博客里面有一篇专门详解这个的。需要看的,可以找下。led灯的驱动其实就代表了I/O口的驱动。在linux系统下,操作一个I/O口,可以说实在是麻烦至极与裸机操作相比较的话。这里简介移植过程,没写分析。

1 LED 驱动原理

这个就给个图就够了,搞驱动要连这个都搞不懂,那就完了。

2、驱动的移植。

在drivers/char 目录下,我们建立一个驱动程序文件mini2440_leds.c,内容如下:

[cpp]view plaincopy<spanstyle="font-size:18px;">#include<linux/miscdevice.h> #include<linux/delay.h> #include<asm/irq.h> #include<mach/regs-gpio.h> #include<mach/hardware.h> #include<linux/kernel.h> #include<linux/module.h> #include<linux/init.h> #include<linux/mm.h> #include<linux/fs.h> #include<linux/types.h> #include<linux/delay.h> #include<linux/moduleparam.h> #include<linux/slab.h> #include<linux/errno.h> #include<linux/ioctl.h> #include<linux/cdev.h> #include<linux/string.h> #include<linux/list.h> #include<linux/pci.h> #include<linux/gpio.h> #include<asm/uaccess.h> #include<asm/atomic.h> #include<asm/unistd.h> #defineDEVICE_NAME"leds"<spanstyle="color:#3366ff;">//设备名(/dev/leds</span>) <spanstyle="color:#3366ff;">//LED对应的GPIO端口列表 </span>staticunsignedlongled_table[]={ S3C2410_GPB(5), S3C2410_GPB(6), S3C2410_GPB(7), S3C2410_GPB(8), }; <spanstyle="color:#3366ff;">//LED对应端口将要输出的状态列表 </span>staticunsignedintled_cfg_table[]={ S3C2410_GPIO_OUTPUT, S3C2410_GPIO_OUTPUT, S3C2410_GPIO_OUTPUT, S3C2410_GPIO_OUTPUT, }; <spanstyle="color:#3366ff;">/*ioctl函数的实现 *在应用/用户层将通过ioctl函数向内核传递参数,以控制LED的输出状态 */ </span>staticintsbc2440_leds_ioctl( structinode*inode, structfile*file, unsignedintcmd, unsignedlongarg) { switch(cmd){ case0: case1: if(arg>4){ return-EINVAL; } <spanstyle="color:#3366ff;">//根据应用/用户层传递来的参数(取反),通过s3c2410_gpio_setpin函数设置LED对应的端口寄存 器,</span> s3c2410_gpio_setpin(led_table[arg],!cmd); return0; default: return-EINVAL; } } <spanstyle="color:#3366ff;">/* *设备函数操作集,在此只有ioctl函数,通常还有read,write,open,close等,因为本LED驱动在下面已经 *注册为misc设备,因此也可以不用open/close */ </span>staticstructfile_operationsdev_fops={ .owner=THIS_MODULE, .ioctl=sbc2440_leds_ioctl, }; <spanstyle="color:#3366ff;BACKGROUND-COLOR:#ffffff">/* *把LED驱动注册为MISC设备 */ </span>staticstructmiscdevicemisc={ .minor=MISC_DYNAMIC_MINOR,//动态设备号 .name=DEVICE_NAME, .fops=&dev_fops, }; <spanstyle="color:#3366ff;">/* *设备初始化 */ </span>staticint__initdev_init(void) { intret; inti; for(i=0;i<4;i++){ <spanstyle="color:#3366ff;">//设置LED对应的端口寄存器为输出(OUTPUT) </span>s3c2410_gpio_cfgpin(led_table[i],led_cfg_table[i]); <spanstyle="color:#3366ff;">//设置LED对应的端口寄存器为低电平输出,在模块加载结束后,四个LED应该是全部都是发光 状态</span> s3c2410_gpio_setpin(led_table[i],0); } ret=misc_register(&misc);<spanstyle="color:#3366ff;">//注册设备</span> printk(DEVICE_NAME"\tinitialized\n");<spanstyle="color:#3366ff;">//打印初始化信息</span> returnret; } staticvoid__exitdev_exit(void) { misc_deregister(&misc); } module_init(dev_init);<spanstyle="color:#3333ff;">//模块初始化,仅当使用insmod/podprobe命令加载时有用,如果设备不是通过模块方式加载,此处将不会被调用 </span>module_exit(dev_exit<spanstyle="BACKGROUND-COLOR:#ffffff">);<spanstyle="color:#3366ff;">//卸载模块,当该设备通过模块方式加载后,可以通过rmmod命令卸载,将调用此函 </span></span><spanstyle="BACKGROUND-COLOR:#3366ff">数 </span>MODULE_LICENSE("GPL");<spanstyle="color:#3366ff;">//版权信息 </span>MODULE_AUTHOR("FriendlyARMInc.");<spanstyle="color:#3366ff;BACKGROUND-COLOR:#ffffff">//开发者信息</span></span>

接下来,我们添加LED 设备的内核配置选项,打开drivers/char/Kconfig 文件,添加如下红色部分内容:

config DEVKMEM

bool "/dev/kmem virtual device support"

default y

help

Say Y here if you want to support the /dev/kmem device. The

/dev/kmem device is rarely used, but can be used for certain

kind of kernel debugging operations.

When in doubt, say "N".

config LEDS_MINI2440

tristate "LED Support for Mini2440 GPIO LEDs"

depends on MACH_MINI2440

default y if MACH_MINI2440

help

This option enables support for LEDs connected to GPIO lines

on Mini2440 boards.

config MINI2440_ADC

bool "ADC driver for FriendlyARM Mini2440 development boards"

depends on MACH_MINI2440

default y if MACH_MINI2440

help

this is ADC driver for FriendlyARM Mini2440 development boards

Notes: the touch-screen-driver required this option

接下来,再根据该驱动的配置定义,把对应的驱动目标文件加入内核中,打开linux-2.6.32.2/drivers/char/Makefile 文件,添加如下红色部分内容:

obj-$(CONFIG_JS_RTC) += js-rtc.o

js-rtc-y = rtc.o

obj-$(CONFIG_LEDS_MINI2440) += mini2440_leds.o

obj-$(CONFIG_MINI2440_ADC) += mini2440_adc.o

# Files generated that shall be removed upon make clean

clean-files := consolemap_deftbl.c defkeymap.c

这样,我们就在内核中添加做好了LED 驱动

3配置编译新内核并测试LED

接上面的步骤,在内核源代码目录下执行:make menuconfig 重新配置内核,依次选择进入如下子菜单项:

Device Drivers --->

Character devices --->

进入LED 驱动配置菜单,进行内核配置。

在内核源代码根目录下执行;make zImage,把生成的新内核烧写到开发板中。

3 测试LED

用自带的文件系统,启动后就会运行一个led程序。测试结果如图。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。