Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2242119
  • 博文数量: 395
  • 博客积分: 10994
  • 博客等级: 上将
  • 技术积分: 5586
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-17 19:49
文章存档

2014年(1)

2013年(10)

2012年(74)

2011年(303)

2010年(7)

分类: LINUX

2011-03-28 22:14:30

注:我此次转载的这篇博客的内容适用于2。6。30。4 ,同时也使用于2。6。29,我已经实验过了,完全能够达到实验的效果,只是由于复制,粘贴的原因,使得有些格式有点问题,你得稍微该一点。
#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

 

#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

#include

 

#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

 

 
阅读(1135) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~