linux-2.6.32在mini2440開發板上移植(16)之LED 驅動程序移植

LED 驅動程序移植

編者;對於led的驅動程序,很多文章都有詳細的介紹,我的博客裏面有一篇專門詳解這個的。需要看的,可以找下。led燈的驅動其實就代表了I/O口的驅動。在linux系統下,操作一個I/O口,可以說實在是麻煩至極與裸機操作相比較的話。這裏簡介移植過程,沒寫分析。

1 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程序。測試結果如圖。

發佈了94 篇原創文章 · 獲贊 96 · 訪問量 40萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章