科创园

科创园地,分享技术知识,为科技助力发展,贡献一己之力。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

linux字符设备驱动之LED

Posted on 2012-02-25 10:09  科创园  阅读(5669)  评论(0编辑  收藏  举报

学习linux设备驱动程序,字符设备驱动是最基础的,在第一节简单字符设备中我们举了一个虚拟内存设备globalmem来说明字符设备驱动的基本框架。在linux设备驱动中,我们不是看程序的复杂(读内核源码也是不一定要全部读懂),而要掌握的是linux设备驱动的框架,而前面我们介绍的诸如linux中的多进程并发访问控制、阻塞访问与异步通知、中断处理等,这些知识是理解linux内核与驱动知识的一点小插曲,但又是我们不得不掌握的知识点,因为在linux设备驱动程序中,我们的驱动往往不会那么简单。说到这个基本框架,我们不得不背一些,因此,学习好linux设备驱动程序开发,我们要做的第一关:在理解了原理基础后,就是熟背那些常见的驱动框架。当然如果手中有一本参考手册,就是最好了。

好了,闲言少说,下面我们就用友善之臂的一个led驱动程序来分析我们linux字符设备驱动的特点。

先来一个插曲:混杂设备。

在linux中,包含了很多的设备驱动类型,而不管分类多么详细,总会有一些疏漏,因此我们将这些设备定义为混杂设备(用miscdevice结构体描述),linux内核提供的miscdevice有很强的包容性,如NVRAM,看门狗,DS1286时钟,字符LCD,LED等,miscdevice本质仍为字符设备,只是被增加了一些封装而已,因此其驱动的主题仍然是file_operation的成员函数,再之后的例子中,我们会看到很多linux设备驱动程序采用了面向对象的思想,对底层相同类似的操作进行了封装,采用了统一接口的形式,比如内核中典型的Kobject、input输入子系统、USB驱动等。

 

这里,我们的LED作为混杂设备,是为了讲解一下linux中混杂设备的使用。。(请读者一定要明白这里的区别)

 

miscdevice共享一个主设备号MISC_MAJOR(10),但次设备号不同。所有的miscdevice设备形成一个链表,对设备访问内核时根据此设备号查找对应的miscdevice设备,然后调用其file_operation结构体中注册的文件操作接口进行操作。

 

1 struct miscdevice{
2 int minor;
3 const char *name;
4 const struct file_operation *fops;
5 struct list_head list;
6 struct device *parent;
7 struct device *this_device
8 };

对miscdevice的注册和注销分别通过如下两个API函数
int misc_register(struct miscdevice *misc);
int misc_deregister(struct miscdevice  *misc);



 

 

/*led_driver.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 <asm/uaccess.h>

#include <asm/atomic.h>

#include <asm/unistd.h>

 

#define LED_ON  1

#define LED_OFF 0

 

#define DEVICE_NAME "leds"

 

static unsigned long led_table [] = {

       S3C2410_GPB5,

       S3C2410_GPB6,

       S3C2410_GPB7,

       S3C2410_GPB8,

};

 

static unsigned int led_cfg_table [] = {

       S3C2410_GPB5_OUTP,

       S3C2410_GPB6_OUTP,

       S3C2410_GPB7_OUTP,

       S3C2410_GPB8_OUTP,

};

 

static int s3c2440_leds_ioctl(

       struct inode *inode,

       struct file *file,

       unsigned int cmd,

       unsigned long arg)

{

       if(arg>3){

       printk("Led's number error,please check!");    

       return -EINVAL;

       }

 

       switch(cmd) {

       case LED_ON:

              s3c2410_gpio_setpin(led_table[arg],0); //led low light

              return 0;

       case LED_OFF:

              s3c2410_gpio_setpin(led_table[arg], 1);

              return 0;

       default:

              return -EINVAL;

       }

}

 

static struct file_operations dev_fops = {

       .owner    =     THIS_MODULE,

       .ioctl       =     s3c2440_leds_ioctl,

};

 

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++) {

              s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);

              s3c2410_gpio_setpin(led_table[i], 1);

       }

 

       ret = misc_register(&misc);

 

       printk (DEVICE_NAME"\tinitialized\n");

 

       return ret;

}

 

static void __exit dev_exit(void)

{

       misc_deregister(&misc);

}

 

module_init(dev_init);

module_exit(dev_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Feng dong rui");

MODULE_DESCRIPTION("Study s3c2440");

 

简单分析:

(1)   友善之臂的mini2440板子上的4个LED对应的GPIO是GPB5~GPB8,低电平点亮;

(2)   注册设备的时候,有两种方式:一种是使用register_chrdev(LED_MAJOR,DEVICE_NAME,&dev_fops),LED_MAJOR为定义的主设备号,DEVICE_NAME为定义的设备名称,dev_fops为定义的文件操作结构体。使用该函数向系统注册字符型设备驱动程序,主设备号LED_MAJOR自己定义,如该值为0则系统自动分配主设备号;另一种是使用misc_register(&misc)。如果是非标准设备则使用 misc_register,即一些字符设备不符合预先确定的字符设备范畴,就用这种方式,它固定使用主设备号10注册,如果多个设备次设备号不同。

(3)   使用register_chrdev(LED_MAJOR,DEVICE_NAME,&dev_fops)注册字符设备驱动程序时,如果有多个设备使用该函数注册驱动程序,LED_MAJOR不能相同,否则几个设备都无法注册(我已在友善的板子上验证)。如果模块使用该方式注册并且LED_MAJOR为0(自动分配主设备号),使用insmod命令加载模块时会在终端显示分配的主设备号和次设备号,在/dev目录下建立该节点,比如设备leds,如果加载该模块时分配的主设备号和次设备号为253和0,则建立节点:mknod leds c 253 0。使用register_chrdev(LED_MAJOR,DEVICE_NAME,&dev_fops)注册字符设备驱动程序时都要手动建立节点,否则在应用程序无法打开该设备。

(4)   在构建根文件系统时在配置选项中必须按照如下设置,才能加载和卸载模块:BusyboxLinux Module Utilities-à

       (/lib/modules)Default directory containing modules

       (modules.dep)Default name of modules.dep

[*]   insmod

[*]   rmmod

[*]   lsmod

[*]   modprobe

 

Makefile文件如下:

obj-m:=led_driver.o

CURRENT_PATH:=$(shell pwd)

ARM_LINUX_KERNEL:=/opt/linux-2.6.29.1

all:

       $(MAKE) -C $(ARM_LINUX_KERNEL) SUBDIRS=$(CURRENT_PATH) modules

clean:

       rm -rf *.cmd *.o *.ko  *.mod.c *.symvers *.order

 

测试程序如下:

 

/*led_app.c*/

#include <stdio.h>

#include <stdlib.h>

 

#define LED_ON   1

#define LED_OFF  0

#define LED_DEVICE  "/dev/leds"

 

int main(int argc,char **argv)

{

    int fd,led_num;

    fd = open(LED_DEVICE,0);

    if(fd < 0)

    {

        printf("can't open /dev/leds!\n");

        exit(0);

    }

   

    led_num = atoi(argv[1]);

    if(!(strcmp(argv[2],"on")))

    {

        ioctl(fd,LED_ON,led_num);

    }

    else if(!(strcmp(argv[2],"off")))

    {

        ioctl(fd,LED_OFF,led_num);

    }

    else

    {

    exit(0);

    }

    exit(0);

}

 

 

Makefile文件:

all:

       arm-linux-gcc led_app.c -o led_app

clean:

       rm -rf *.o led_app

 

编译模块得到leds_driver.ko,我把它拷到根文件系统的额home目录下,并在启动脚本里面设置自动加载,就是在/etc/init.d/rcS里面加了两句:

echo “---------insmod leds_driver.ko---------

insmod /home/leds_driver.ko

 

把编译得到的测试程序led_app拷贝到home目录下,使用命令点亮和熄灭某一LED:

 

./led_app 0 on                 //点亮LED1

        .

        .

        .

./led_app 3 on                 //点亮LED4

 

 

 

./led_app 0 off                 //熄灭LED1

        .

        .

        .

./led_app 3 off                 //熄灭LED4