日期:2014-05-16  浏览次数:20655 次

linux设备驱动篇之LED驱动(一)

内核版本:linux-3.0

_________________________________________________________________

说起这个LED设备驱动,我有一肚子苦水,期间经历的问题层出不穷,遇到的问题有以下这些:

1、网上很多的LED驱动几乎都是2.6的内核版本,但是3.0内核和2.6的内核驱动编写有很多地方是不同的在2.6上能运行的设备驱动不一定能在3.0内核上运行。这对于刚刚踏入驱动大门人,急于想了解LED驱动是什么样的,他是怎么工作的,与应用程序又是如何协同工作的呢?对于我们可能第一个要去做的就是去找个例程先跑一跑,了解是怎么回事,增强一些自信和在战略上“藐视”设备驱动的气魄。先介绍一下我的工程结构如下说是,第一个是建立在后面第三个文件s3c_led.c这个LED设备驱动上运行的led运行程序,第二个是Make文件,


我先把可以运行的这个LED驱动贴上来:

(如果你拷贝到你的vim上格式多乱了的话,教你一个快速的方法:在命令行模式下连续按下gg=G这上个字符就能帮你自动调整格式。

你在下面的程序中找到这个nt led[LED_NUM] = {5,6,7,8};  /* Four LEDs use GPB5,GPB6,GPB8,GPB10 */,因为我的led是在GPBIO5、6、7、8这个几个端口,结合你自己的情况修改,其他的不用改

/*********************************************************************************
 *      Copyright:  (C) 2012 Guo Wenxue<guowenxue@gmail.com>  
 *                  All rights reserved.
 *
 *       Filename:  s3c_led.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(07/26/2012~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "07/26/2012 10:03:40 PM"
 *                 
 ********************************************************************************/


#include <linux/module.h>   /* Every Linux kernel module must include this head */
#include <linux/init.h>     /* Every Linux kernel module must include this head */
#include <linux/kernel.h>   /* printk() */
#include <linux/fs.h>       /* struct fops */
#include <linux/errno.h>    /* error codes */
#include <linux/cdev.h>     /* cdev_alloc()  */
#include <asm/io.h>         /* ioremap()  */
#include <linux/ioport.h>   /* request_mem_region() */


#include <asm/ioctl.h>      /* Linux kernel space head file for macro _IO() to generate ioctl command  */
#ifndef __KERNEL__
#include <sys/ioctl.h>      /* User space head file for macro _IO() to generate ioctl command */
#endif
//#include <linux/printk.h> /* Define log level KERN_DEBUG, no need include here */




#define DRV_AUTHOR                "Guo Wenxue <guowenxue@gmail.com>"
#define DRV_DESC                  "S3C24XX LED driver"


#define DEV_NAME                  "led"
#define LED_NUM                   4


/* Set the LED dev major number */
//#define LED_MAJOR                 79
#ifndef LED_MAJOR
#define LED_MAJOR                 0
#endif


#define DRV_MAJOR_VER             1
#define DRV_MINOR_VER             0
#define DRV_REVER_VER             0


#define DISABLE                   0
#define ENABLE                    1


#define GPIO_INPUT                0x00
#define GPIO_OUTPUT