Chinaunix首页 | 论坛 | 博客
  • 博客访问: 931221
  • 博文数量: 96
  • 博客积分: 10071
  • 博客等级: 上将
  • 技术积分: 1118
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-20 17:54
文章分类

全部博文(96)

文章存档

2011年(3)

2010年(3)

2009年(29)

2008年(54)

2007年(7)

分类: LINUX

2008-04-29 23:19:25

linux驱动开发之led
creator
sz111@126.com
今天做了led的驱动,程序测试ok。
测试如下:
先通过cat /proc/devices 插看utuled的主号为253.
mknod /dev/utuled c 253 0
通过echo -n 7 > /dev/utuled测试ok。
驱动程序如下:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

struct utuled_dev_t
{
    struct cdev cdev;
    unsigned int led_status;
};
struct utuled_dev_t utuled_dev;

#define UTULED_MAJOR 0
static dev_t utuled_major = UTULED_MAJOR;

#define LED1 S3C2410_GPF4
#define LED2 S3C2410_GPF5
#define LED3 S3C2410_GPF6
#define LED4 S3C2410_GPF7
#define LED1_CFG S3C2410_GPF4_OUTP
#define LED2_CFG S3C2410_GPF5_OUTP
#define LED3_CFG S3C2410_GPF6_OUTP
#define LED4_CFG S3C2410_GPF7_OUTP

static unsigned int LED[] = {LED1,LED2,LED3,LED4};
static unsigned int LED_CFG[] = {LED1_CFG,LED2_CFG,LED3_CFG,LED4_CFG};

#define NumOfLed (sizeof(LED)/sizeof(LED[0]))

static int utuled_open(struct inode *inode, struct file *filp);
static int utuled_release(struct inode *inode, struct file *filp);
static ssize_t utuled_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
static ssize_t utuled_read(struct file *file,char __user *buffer, size_t count, loff_t *ppos);

static const struct file_operations utuled_fops =
{
    .owner = THIS_MODULE,
    .read  = utuled_read,
    .write = utuled_write,
    .open  = utuled_open,
    .release = utuled_release,
};

static void update_utuled(int led)
{
    int i;
    for(i=0; i    {
        if (led & (1<        {
            s3c2410_gpio_setpin(LED[i],0);
        }else
        {
            s3c2410_gpio_setpin(LED[i],1);
        }
    }
}
static void led_init(void)
{
    int i;
    for(i=0; i    {
        s3c2410_gpio_cfgpin(LED[i],LED_CFG[i]);
        s3c2410_gpio_setpin(LED[i],1);
    }
}

static void utuled_setup_cdev(void)
{
    int err,devno = MKDEV(utuled_major,0);
    cdev_init(&utuled_dev.cdev,&utuled_fops);
    utuled_dev.cdev.owner = THIS_MODULE;
    utuled_dev.cdev.ops = &utuled_fops;
    utuled_dev.led_status = 0;
    err = cdev_add(&utuled_dev.cdev, devno, 1);
    if (err)
    {
        printk(KERN_NOTICE "Error %d adding utuled",err);        
    }
}

static int utuled_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &utuled_dev;
    printk(KERN_NOTICE "utuled opened\n");
    return 0;
}

static int utuled_release(struct inode *inode, struct file *filp)
{
    printk(KERN_NOTICE "utuled released\n");
    return 0;
}

static ssize_t utuled_write(struct file *filp, const char __user *buffer, size_t count, loff_t *ppos)
{
    struct utuled_dev_t *dev = filp->private_data;
    copy_from_user(&dev->led_status,buffer,sizeof(dev->led_status));
    update_utuled(dev->led_status);
    
    printk(KERN_NOTICE "utuled write\n");
    return sizeof(dev->led_status);
}

static ssize_t utuled_read(struct file *filp,char __user *buffer, size_t count, loff_t *ppos)
{
    printk(KERN_NOTICE "utuled read\n");
    return 0;
}

static int __init utuled_init(void)
{
    int result;
    dev_t devno = MKDEV(utuled_major,0);
    if (utuled_major)
    {
        result = register_chrdev_region(devno, 1, "utuled");
    }else
    {
        result = alloc_chrdev_region(&devno, 0, 1, "utuled");
        utuled_major = MAJOR(devno);
    }
    if (result < 0)
    {
        return result;
    }
    utuled_setup_cdev();
    //led init
    led_init();
    return 0;
}
static void __exit utuled_exit(void)
{
    cdev_del(&utuled_dev.cdev);
    unregister_chrdev_region(MKDEV(utuled_major,0),1);
}

MODULE_AUTHOR("Creator");
MODULE_LICENSE("Dual BSD/GPL");

module_init(utuled_init);
module_exit(utuled_exit);
阅读(2059) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

l0p0c2009-02-16 15:08:45

系统还没有移植好呢…… 先过来看看。