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

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

LED 驱动程序移植

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

1 LED 驱动原理

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

 

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

2、驱动的移植。

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

#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>
#define DEVICE_NAME "leds" //设备名(/dev/leds)
//LED 对应的GPIO 端口列表
static unsigned long led_table [] = {
S3C2410_GPB(5),
S3C2410_GPB(6),
S3C2410_GPB(7),
S3C2410_GPB(8),
};
//LED 对应端口将要输出的状态列表
static unsigned int led_cfg_table [] = {
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
S3C2410_GPIO_OUTPUT,
};
/*ioctl 函数的实现
* 在应用/用户层将通过ioctl 函数向内核传递参数,以控制LED 的输出状态
*/
static int sbc2440_leds_ioctl(
struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd) {
case 0:
case 1:
if (arg > 4) {
return -EINVAL;
}
//根据应用/用户层传递来的参数(取反),通过s3c2410_gpio_setpin 函数设置LED 对应的端口寄存
器,
s3c2410_gpio_setpin(led_table[arg], !cmd);
return 0;
default:
return -EINVAL;
}
}
/*
* 设备函数操作集,在此只有ioctl 函数,通常还有read, write, open, close 等,因为本LED 驱动在下面已经
* 注册为misc 设备,因此也可以不用open/close
*/
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.ioctl = sbc2440_leds_ioctl,
};
/*
* 把LED 驱动注册为MISC 设备
*/
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR, //动态设备号
.name = DEVICE_NAME,
.fops = &dev_fops,
};
/*
* 设备初始化
*/
static int __init dev_init(void)
{
int ret;
int i;
for (i = 0; i < 4; i++) {
//设置LED 对应的端口寄存器为输出(OUTPUT)
s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
//设置LED 对应的端口寄存器为低电平输出,在模块加载结束后,四个LED 应该是全部都是发光
状态
s3c2410_gpio_setpin(led_table[i], 0);
}
ret = misc_register(&misc); //注册设备
printk (DEVICE_NAME"\tinitialized\n"); //打印初始化信息
return ret;
}
static void __exit dev_exit(void)
{
misc_deregister(&misc);
}
module_init(dev_init); //模块初始化,仅当使用insmod/podprobe 命令加载时有用,如果设备不是通过模块方式加载,此处将不会被调用
module_exit(dev_exit);//卸载模块,当该设备通过模块方式加载后,可以通过rmmod 命令卸载,将调用此函
MODULE_LICENSE("GPL"); //版权信息
MODULE_AUTHOR("FriendlyARM Inc."); //开发者信息

接下来,我们添加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程序。测试结果如图。

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