Chinaunix首页 | 论坛 | 博客
  • 博客访问: 395126
  • 博文数量: 69
  • 博客积分: 1992
  • 博客等级: 上尉
  • 技术积分: 1162
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-03 19:50
文章分类
文章存档

2015年(1)

2011年(55)

2010年(13)

分类: LINUX

2010-07-15 20:07:01

程序实现功能:
key1~key4分别控制LED1~LED4的亮,key5和key6控制四个led全亮和全灭。

驱动程序:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>

#include <linux/delay.h>

#include <linux/irq.h>
#include <asm/irq.h>
#include <linux/interrupt.h>

#include <asm/uaccess.h>
//#include

#include <mach/regs-gpio.h>
//#include

#include <mach/hardware.h>

#include <linux/sched.h>
#include <linux/gpio.h>

#include <linux/cdev.h>

#include <asm/io.h>

#include <linux/device.h>//自动创建inode节点包含头文件


struct class *key_class;

#define DEVICE_NAME "mini2440_key"
#define KEY_MAJOR 0
static int key_major = KEY_MAJOR;

//static DECLARE_WAIT_QUEUE_HEAD(key_waitq);


//static volatile int ev_press = 0;


static volatile int press_cnt[] = { 1, 2, 3, 4, 5, 6};

#define GPBCON        (unsigned long) (0x56000010)
#define GPBDAT        (unsigned long) (0x56000014)
#define GPBUP        (unsigned long) (0x56000018)

unsigned long         _IO_GPBCON;
unsigned long         _IO_GPBDAT;
unsigned long         _IO_GPBUP;

#define GPB5_0        ~(1<<5)
#define GPB6_0        ~(1<<6)
#define GPB7_0        ~(1<<7)
#define GPB8_0        ~(1<<8)

#define GPB5_1         (1<<5)
#define GPB6_1         (1<<6)
#define GPB7_1         (1<<7)
#define GPB8_1         (1<<8)

void ledinit(void)
{
    _IO_GPBCON = (unsigned long)ioremap(GPBCON,0x4);
    _IO_GPBDAT = (unsigned long)ioremap(GPBDAT,0x4);
    _IO_GPBUP = (unsigned long)ioremap(GPBUP,0x4);
    *(volatile unsigned long *)_IO_GPBCON = 0x154FD;
    *(volatile unsigned long *)_IO_GPBUP = 0x7FF;
}

struct key_irq_desc {
 unsigned int irq;
 unsigned long flags;
 const char *name;
};

static struct key_irq_desc key_irqs[] = {
 {IRQ_EINT8, IRQF_TRIGGER_FALLING, "key1"},
 {IRQ_EINT11, IRQF_TRIGGER_FALLING, "key2"},
 {IRQ_EINT13, IRQF_TRIGGER_FALLING, "key3"},
 {IRQ_EINT14, IRQF_TRIGGER_FALLING, "key4"},
 {IRQ_EINT15, IRQF_TRIGGER_FALLING, "key5"},
 {IRQ_EINT19, IRQF_TRIGGER_FALLING, "key6"}
};

/*cdev结构体*/
struct key_dev {
    struct cdev cdev;
};

struct key_dev *key_devp;

/*中断处理函数*/
static irqreturn_t key_interrupt(int irq, void *dev_id)
{
    volatile int *press_cnt = (volatile int *)dev_id;
     
 //*press_cnt = *press_cnt + 1;

 //ev_press = 1;

 //wake_up_interruptible(&key_waitq);


    int val;
    val = *press_cnt;
    switch(val)
            {
                case 1:
                        *(volatile unsigned long *)_IO_GPBDAT &=GPB5_0;
                        break;
                case 2:
                        *(volatile unsigned long *)_IO_GPBDAT &=GPB6_0;
                        break;
                case 3:
                        *(volatile unsigned long *)_IO_GPBDAT &=GPB7_0;
                        break;
                case 4:
                        *(volatile unsigned long *)_IO_GPBDAT &=GPB8_0;
                        break;
                case 5:
                        *(volatile unsigned long *)_IO_GPBDAT &=GPB5_0&GPB6_0&GPB7_0&GPB8_0;
                        break;
                case 6:
                        *(volatile unsigned long *)_IO_GPBDAT |=GPB5_1|GPB6_1|GPB7_1|GPB8_1;
                        break;
            }        
 
    return IRQ_RETVAL(IRQ_HANDLED);
}
 
/*open()*/
static int mini2440_key_open(struct inode *inode, struct file *file)
{
     int i;
     int err;
    
    ledinit();
    *(volatile unsigned long *)_IO_GPBDAT = 0x1E0;
 
     for (i = 0; i < sizeof(key_irqs) / sizeof(key_irqs[0]); i++)
     {
          err = request_irq(key_irqs[i].irq, key_interrupt, key_irqs[i].flags,key_irqs[i].name, (void *)&press_cnt[i]);
          if (err)
              {
               break;
              }
     }
    if (err)
     {
          i--;
          for (; i >= 0; i--)
             {
                      free_irq(key_irqs[i].irq, (void *)&press_cnt[i]);
              }
     }
     return 0;
}

/*release()*/
static int mini2440_key_release(struct inode *inode, struct file *file)
{
 int i;
 
 for (i = 0; i < sizeof(key_irqs) / sizeof(key_irqs[0]); i++)
 {
  free_irq(key_irqs[i].irq, (void *)&press_cnt[i]);
 }
 return 0;
}

/*read()*/
/*
static int mini2440_key_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
 unsigned long err;
 
 wait_event_interruptible(key_waitq, ev_press);//如果ev_press不为1,则读操作加入等待队列,进行睡眠,当ev_press为1时唤醒读进程
 
 ev_press = 0;//执行到这里时,ev_press=1,将它清零,读完之后代表没数据,以等待下一次数据
 
 err = copy_to_user(buff, (const void *)press_cnt, min(sizeof(press_cnt), count));
 
 //memset((void *)press_cnt, 0, sizeof(press_cnt));
 
 return err ? -EFAULT : 0;
}
*/


/*fops()*/
static struct file_operations mini2440_key_fops = {
 .owner = THIS_MODULE,
 .open = mini2440_key_open,
 .release = mini2440_key_release,
 //.read = mini2440_key_read,

};

/*init()*/
static int __init mini2440_key_init(void)
{
    int ret;
    dev_t devnum = MKDEV(KEY_MAJOR,0);
    if(key_major)
    {
        ret = register_chrdev_region(devnum,1,DEVICE_NAME);
        printk(KERN_INFO" static keymajor is %d\n",key_major);
    }
    else
    {
        ret = alloc_chrdev_region(&devnum,0,1,DEVICE_NAME);
        key_major = MAJOR(devnum);
        printk(KERN_INFO" auto keymajor is %d\n",key_major);
    }
// ret = register_chrdev(KEY_MAJOR, DEVICE_NAME, &mini2440_key_fops);

     if (ret < 0)
     {
        printk(KERN_NOTICE"can not register this major number!\n");
          return ret;
     }
    
    key_devp = kmalloc(sizeof(struct key_dev),GFP_KERNEL);
    if (!key_devp)
    {
        ret = -ENOMEM;
        goto fail_malloc;
    }
    
    /*下面两行自动创建inode节点*/
    key_class = class_create(THIS_MODULE,"key_driver");
    device_create(key_class,NULL,MKDEV(key_major,0),NULL,DEVICE_NAME);


    cdev_init(&key_devp->cdev,&mini2440_key_fops);
    key_devp->cdev.owner = THIS_MODULE;
    key_devp->cdev.ops = &mini2440_key_fops;
    ret = cdev_add(&key_devp->cdev,devnum,1);

    if(ret)
    {
        printk(KERN_NOTICE"regist char device error!\n");
    }    
    
     printk(KERN_INFO" button driver module has register sucessed!\n");

    fail_malloc:unregister_chrdev_region(devnum,1);
     return 0;
}

/*exit()*/
static void __exit mini2440_key_exit(void)
{
    
    //unregister_chrdev(KEY_MAJOR, DEVICE_NAME);

    
    cdev_del(&(key_devp->cdev));
    kfree(key_devp);
    key_devp = NULL;
    
    device_destroy(key_class,MKDEV(key_major,0));
    class_destroy(key_class);
    
    unregister_chrdev_region(MKDEV(key_major,0),1);    

    printk(KERN_INFO" button driver module has removed sucessed!\n");
 }

module_init(mini2440_key_init);
module_exit(mini2440_key_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("lwm");



驱动Makefile:
KERNELDIR = /home/lwm/linux-2.6.32.2
PWD := $(shell pwd)
CROSS_COMPILE = /usr/local/arm/4.3.2/bin/arm-linux-
CC = $(CROSS_COMPILE)gcc
obj-m := button_led.o
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
    rm -rf *.ko *.o *.mod.o *.mod.c *.symvers *.order *~
.PHONY: modules modules_install clean


测试程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>

int main(void)
{
    //int i;

    int buttons_fd;
    //int size;

    //int buf[6];

    
    buttons_fd = open("/dev/mini2440_key", 0);
    if (buttons_fd < 0)
    {
        perror("open device buttons error");
        exit(1);
    }
    for (;;);
    /*
    for (;;)
    {
        
        if((size = read(buttons_fd,buf,sizeof(buf))) < 0)
        {
            perror("read data error");
        }
        else
        for(i=0;i         {
            if(buf[i])
            {
                printf("Key%d has been pressed %d times!\n",i+1,buf[i]);
            }
        }
    }
*/

    close(buttons_fd);
    return 0;
}



Makefile:
CROSS=arm-linux-

button_led_test: button_led_test.c
    $(CROSS)gcc -o button_led_test button_led_test.c

clean:
    @rm -vf buttons *.o *~


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