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

2015年(1)

2011年(55)

2010年(13)

分类: LINUX

2010-07-14 20:55:19

给程序,不解释,自己去理解
此版本主要参照韦东山的书,改成自动创建inode节点。

驱动程序:
#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/device.h>//自动创建inode节点包含头文件


struct class *key_class;

#define DEVICE_NAME "mini2440_key"
#define KEY_MAJOR 232
static DECLARE_WAIT_QUEUE_HEAD(key_waitq);
static volatile int ev_press = 0;
static volatile int press_cnt[] = { 0, 0, 0, 0, 0, 0};
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"}
};
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);
 
 return IRQ_RETVAL(IRQ_HANDLED);
}
 

static int mini2440_key_open(struct inode *inode, struct file *file)
{
 int i;
 int err;
 
 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;
}

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;
}
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 = 0;
 
 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;
}
static struct file_operations mini2440_key_fops = {
 .owner = THIS_MODULE,
 .open = mini2440_key_open,
 .release = mini2440_key_release,
 .read = mini2440_key_read,
};
static int __init mini2440_key_init(void)
{
 int ret;
 ret = register_chrdev(KEY_MAJOR, DEVICE_NAME, &mini2440_key_fops);
 if (ret < 0)
 {
  printk(KERN_NOTICE"can not register major number\n");
  return ret;
 }
    /*下面两行自动创建inode节点*/
    key_class = class_create(THIS_MODULE,"key_driver");
    device_create(key_class,NULL,MKDEV(KEY_MAJOR,0),NULL,DEVICE_NAME);

 printk(KERN_INFO" register sucess\n");
 return 0;
}
static void __exit mini2440_key_exit(void)
{
    
    device_destroy(key_class,MKDEV(KEY_MAJOR,0));
    class_destroy(key_class);
    unregister_chrdev(KEY_MAJOR, DEVICE_NAME);
 }
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_auto_mknod.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 (;;)
    {
        
        if((size = read(buttons_fd,buf,sizeof(buf))) < 0)
        {
            perror("read data error");
        }
        else
        for(i=0;i<sizeof(buf)/sizeof(buf[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_test: buttons_test.c
    $(CROSS)gcc -o button_test buttons_test.c

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


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