Chinaunix首页 | 论坛 | 博客
  • 博客访问: 185393
  • 博文数量: 87
  • 博客积分: 2975
  • 博客等级: 少校
  • 技术积分: 926
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-24 17:14
文章分类

全部博文(87)

文章存档

2010年(87)

我的朋友

分类: 嵌入式

2010-07-30 22:46:36

一、实现步骤

1. 硬件原理图分析。由原理图得知LED电路是共阳极的,并分别由2440的GPB5、GPB6、GPB7、GPB8口控制的

2. 去掉内核已有的LED驱动设置,因为IO口与mini2440开发板的不一致,根本就不能控制板上的LED。

#gedit arch/arm/plat-s3c24xx/common-smdk.c    //注释掉以下内容

/* LED devices */
/*
static struct s3c24xx_led_platdata smdk_pdata_led4 = {
    .gpio        = S3C2410_GPF4,
    .flags        = S3C24XX_LEDF_ACTLOW | S3C24XX_LEDF_TRISTATE,
    .name        = "led4",
    .def_trigger    = "timer",
};

static struct s3c24xx_led_platdata smdk_pdata_led5 = {
    .gpio        = S3C2410_GPF5,
    .flags        = S3C24XX_LEDF_ACTLOW | S3C24XX_LEDF_TRISTATE,
    .name        = "led5",
    .def_trigger    = "nand-disk",
};

static struct s3c24xx_led_platdata smdk_pdata_led6 = {
    .gpio        = S3C2410_GPF6,
    .flags        = S3C24XX_LEDF_ACTLOW | S3C24XX_LEDF_TRISTATE,
    .name        = "led6",
};

static struct s3c24xx_led_platdata smdk_pdata_led7 = {
    .gpio        = S3C2410_GPF7,
    .flags        = S3C24XX_LEDF_ACTLOW | S3C24XX_LEDF_TRISTATE,
    .name        = "led7",
};

static struct platform_device smdk_led4 = {
    .name        = "s3c24xx_led",
    .id        = 0,
    .dev        = {
        .platform_data = &smdk_pdata_led4,
    },
};

static struct platform_device smdk_led5 = {
    .name        = "s3c24xx_led",
    .id        = 1,
    .dev        = {
        .platform_data = &smdk_pdata_led5,
    },
};

static struct platform_device smdk_led6 = {
    .name        = "s3c24xx_led",
    .id        = 2,
    .dev        = {
        .platform_data = &smdk_pdata_led6,
    },
};

static struct platform_device smdk_led7 = {
    .name        = "s3c24xx_led",
    .id        = 3,
    .dev        = {
        .platform_data = &smdk_pdata_led7,
    },
};*/


static struct platform_device __initdata *smdk_devs[] = {
    &s3c_device_nand,
    
/*&smdk_led4,
    &smdk_led5,
    &smdk_led6,
    &smdk_led7,*/

};


void __init smdk_machine_init(void)
{
    /* Configure the LEDs (even if we have no LED support)*/
    
/*
    s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
    s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
    s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
    s3c2410_gpio_cfgpin(S3C2410_GPF7, S3C2410_GPF7_OUTP);

    s3c2410_gpio_setpin(S3C2410_GPF4, 1);
    s3c2410_gpio_setpin(S3C2410_GPF5, 1);
    s3c2410_gpio_setpin(S3C2410_GPF6, 1);
    s3c2410_gpio_setpin(S3C2410_GPF7, 1);*/


    if (machine_is_smdk2443())
        smdk_nand_info.twrph0 = 50;

    s3c_device_nand.dev.platform_data = &smdk_nand_info;

    platform_add_devices(smdk_devs, ARRAY_SIZE(smdk_devs));

    s3c_pm_init();
}

3. 编写适合mini2440开发板的LED驱动,代码如下,文件名称:my2440_leds.c

/*==============================================
 Name            : my2440_leds.c
 Author          : apple
 Date            : 08/12/2010
 Copyright       : Dual BSD/GPL
 Description     : my2440 leds driver
 ==============================================*/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <mach/hardware.h>
#include <mach/regs-gpio.h>

#define DEVICE_NAME    "my2440_leds"    //设备名称
#define LED_MAJOR    231                //主设备号
#define LED_ON        1                 //LED亮状态
#define LED_OFF        0                
//LED灭状态

static unsigned long led_table[] =      //控制LED的IO口
{
    S3C2410_GPB5,
    S3C2410_GPB6,
    S3C2410_GPB7,
    S3C2410_GPB8,
};

static unsigned int led_cfg_table[] =   //LED IO口的模式
{
    S3C2410_GPB5_OUTP,
    S3C2410_GPB6_OUTP,
    S3C2410_GPB7_OUTP,
    S3C2410_GPB8_OUTP,
};

static int leds_open(struct inode *inode, struct file *file)
{
    return 0;
}

static int leds_ioctl(struct inode *inode, struct file *file,
                     unsigned int cmd, unsigned long arg)
{
    //检测是第几个LED,因开发板上只有4个,索引从0开始,即0,1,2,3,    
    if(arg < 0 || arg > 3)
    {
        return -EINVAL;
    }
    //判断LED要执行哪种状态
    switch(cmd)
    {
        case LED_ON:
        {
            s3c2410_gpio_setpin(led_table[arg], ~(LED_ON));
            break;
        }
        case LED_OFF:
        {
            s3c2410_gpio_setpin(led_table[arg], ~(LED_OFF));
            break;
        }
        default:
        {
            return -EINVAL;
        }
    }
    return 0;
}

static struct file_operations leds_fops =
{
    .owner    = THIS_MODULE,
    .open    = leds_open,
    .ioctl    = leds_ioctl,
};

static int __init led_init(void)
{
    int ret, i;
    for(i = 0; i < 4; i++)
    {
        //初始化各IO口为输出模式
        s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
        //由原理图可知LED电路是共阳极的(即各IO口输出低电平0才会点亮)        
        //这里初始化为1,不让LED点亮
        s3c2410_gpio_setpin(led_table[i], ~(LED_OFF));
    }
    //注册LED设备为字符设备
    ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &leds_fops);
    if(ret < 0)
    {
        printk(DEVICE_NAME " register falid!\n");
        return ret;
    }
}

static void __exit led_exit(void)
{
    //注销设备
    unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}

module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("apple");
MODULE_DESCRIPTION("My2440 led driver");

4. 采用动态方式把LED驱动代码加载到内核中去

 

5. 查看已加载的设备:#cat /proc/devices,可以看到my2440_leds的主设备号为231

[root@FriendlyARM driver]# cat /proc/devices                                            
Character devices:                                                                      
  1 mem                                                                                 
  4 /dev/vc/0                                                                           
  4 tty                                                                                 
  5 /dev/tty                                                                            
  5 /dev/console                                                                        
  5 /dev/ptmx                                                                           
  7 vcs                                                                                 
 10 misc                                                                                
 13 input                                                                               
 14 sound                                                                               
 29 fb                                                                                  
 81 video4linux                                                                         
 89 i2c                                                                                 
 90 mtd                                                                                 
116 alsa                                                                                
128 ptm                                                                                 
136 pts                                                                                 
180 usb                                                                                 
188 ttyUSB                                                                              
189 usb_device                                                                          
204 s3c2410_serial                                                                      
231 my2440_leds                                                                         
253 usb_endpoint                                                                        
254 rtc 

6. 编写应用程序测试LED驱动,文件名:leds_test.c

/*==============================================
 Name            : leds_test.c
 Author          : apple
 Date            : 08/12/2010
 Copyright       : Dual BSD/GPL
 Description     : my2440 leds driver test
 ==============================================*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
    int turn, index, fd;
    //检测输入的参数合法性
    if(argc != 3 || sscanf(argv[2], "%d", &index) != 1 || index < 1 || index > 4)
    {
        printf("Usage: leds_test on|off 1|2|3|4\n");
        exit(1);
    }
    if(strcmp(argv[1], "on") == 0)
    {
        turn = 1;
    }
    else if(strcmp(argv[1], "off") == 0)
    {
        turn = 0;
    }
    else
    {
        printf("Usage: leds_test on|off 1|2|3|4\n");
        exit(1);
    }
    fd = open("/dev/my2440_leds", 0);   //打开LED设备
    if(fd < 0)
    {
        printf("Open Led Device Faild!\n");
        exit(1);
    }
    ioctl(fd, turn, index - 1);   //IO控制
    close(fd);    //关闭LED设备
    return 0;
}

7. 在开发主机上交叉编译测试应用程序

#arm-linux-gcc -o leds_test leds_test.c

8. 在开发板上的文件系统中创建一个LED设备的节点,然后运行测试程序,效果图如下,观测开发板上的LED灯,可以看到每一步的操作对应的LED会点亮或者熄灭

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