//驱动
//未加去抖动
//通过poll判断是否可读来得到中断值
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/cdev.h>
#define DEVICE_NAME "buttons_my"
#define BUTTON_MAJOR 0 // 定义0使用自动分配设备号
unsigned int button_major = BUTTON_MAJOR;
struct button_irq_desc
{
int irq; /*中断号*/
int pin; /*中断控制的寄存器*/
int pin_setting; /*中断的引脚*/
int number; /*编号*/
char *name; /*名称*/
};
static struct button_irq_desc button_irqs [] =
{
{IRQ_EINT11,S3C2410_GPG3, S3C2410_GPG3_EINT11, 1,"KEY1"}, /*K1*/
{IRQ_EINT0, S3C2410_GPF0, S3C2410_GPF0_EINT0, 2,"KEY2"}, /*K2*/
{IRQ_EINT19,S3C2410_GPG11,S3C2410_GPG11_EINT19,3,"KEY3"}, /*K3*/
{IRQ_EINT2, S3C2410_GPF2, S3C2410_GPF2_EINT2, 4,"KEY4"}, /*K4*/
{IRQ_EINT6, S3C2410_GPF6, S3C2410_GPF6_EINT6, 5,"KEY5"}, /*K5*/
{IRQ_EINT5, S3C2410_GPF5, S3C2410_GPF5_EINT5, 6,"KEY6"}, /*K6*/
};
static volatile int key_values [] = {0,0,0,0,0,0};/*key_values数组中是否有数据的标志,0表示无数据可读,1表示有数据可读*/
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0; /*初始为0*/
// 中断处理
static irqreturn_t buttons_interrupt(int irq,void *dev_id)
{
struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;
// int up = s3c2410_gpio_getpin(button_irqs->pin); //读I/O
//没去抖动
key_values[button_irqs->number - 1] = button_irqs->number;
ev_press = 1; /*表示数组已经可读了*/
wake_up_interruptible(&button_waitq);
return IRQ_RETVAL(IRQ_HANDLED);
}
//open 注册中断
static int buttons_open(struct inode *inode,struct file *file)
{
int i;
int err;
for(i = 0;i < sizeof(button_irqs)/sizeof(button_irqs[0]);i++)
{
s3c2410_gpio_cfgpin(button_irqs.pin,button_irqs.pin_setting);
err = request_irq(button_irqs.irq,buttons_interrupt,NULL,button_irqs.name,(void *)&button_irqs);
set_irq_type(button_irqs.irq,IRQT_FALLING); //IRQT_BOTHEDGE 双边沿触发
if(err)
break;
}
if(err)
{
i--;
for(;i >=0; i--)
{
disable_irq(button_irqs.irq);
free_irq(button_irqs.irq,(void *)&button_irqs);
}
return -EBUSY;
}
return 0;
}
//close 释放中断
static int buttons_close(struct inode *inode,struct file *file)
{
int i;
for(i = 0;i < sizeof(button_irqs)/sizeof(button_irqs[0]);i++)
{
disable_irq(button_irqs.irq);
free_irq(button_irqs.irq,(void *)&button_irqs);
}
return 0;
}
//read
static int buttons_read(struct file *filp,char __user *buff,size_t count,loff_t *offp)
{
unsigned long err;
if(!ev_press) /*标志位为0,即无数据时*/
{
if(filp->f_flags & O_NONBLOCK) //不能以非阻塞读取
return -EAGAIN;
else
wait_event_interruptible(button_waitq,ev_press);
}
err = copy_to_user(buff,(const void *)key_values,min(sizeof(key_values),count));
memset((void *)key_values,0,sizeof(key_values));
ev_press = 0;
if(err)
return -EFAULT;
else
return min(sizeof(key_values),count);
}
//buttons_poll
static unsigned int buttons_poll(struct file *file,struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file,&button_waitq,wait);
if(ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static struct file_operations buttons_fops =
{
.owner = THIS_MODULE,
.open = buttons_open, /*open()*/
.release = buttons_close, /*release()*/
.read = buttons_read, /*read()*/
.poll = buttons_poll /*poll()*/
};
struct cdev button_cdev; // cdev结构体
/* 初始化并注册cdev */
static void buttons_setup_cdev(void)
{
int err,devno = MKDEV(button_major,0);
cdev_init(&button_cdev,&buttons_fops);
button_cdev.owner = THIS_MODULE;
button_cdev.ops = &buttons_fops;
err = cdev_add(&button_cdev, devno, 1);
if (err)
printk(KERN_NOTICE "Error %d adding utukey", err);
}
//init
static int __init buttons_init(void)
{
int result;
dev_t devno = MKDEV(button_major,0); // 用主次设备号生成设备号
/* 申请设备号 */
if (button_major)
result = register_chrdev_region(devno, 1, DEVICE_NAME);
else /* 动态申请设备号 */
{
result = alloc_chrdev_region(&devno, 0, 1, DEVICE_NAME);
button_major = MAJOR(devno);
printk(KERN_INFO "Todo: mknod /dev/%s c %d 0\n", DEVICE_NAME, button_major);
}
if (result < 0)
return result;
buttons_setup_cdev();
printk(DEVICE_NAME " initialized\n");
return 0;
}
//buttons_exit()
static void __exit buttons_exit(void)
{
cdev_del(&button_cdev); // 注销cdev
unregister_chrdev_region(MKDEV(button_major, 0), 1); // 释放设备号
}
module_init(buttons_init);
module_exit(buttons_exit);
MODULE_AUTHOR("http://www.xxx.net");
MODULE_DESCRIPTION("S3C2410/S3C2440 BUTTON Driver");
MODULE_LICENSE("GPL");
//测试代码
/*按键测试程序*/
#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 key_value[6];
fd_set rds;
int ret;
buttons_fd = open("/dev/buttons_my",0);
if(buttons_fd < 0)
{
perror("open device buttons");
exit(1);
}
while(1)
{
FD_ZERO(&rds);
FD_SET(buttons_fd,&rds);
ret = select(buttons_fd + 1,&rds,NULL,NULL,NULL);
if(ret < 0)
{
perror("select");
exit(1);
}
if(ret == 0)
{
printf("Timeout.\n");
}
else if(FD_ISSET(buttons_fd,&rds))
{
int ret = read(buttons_fd,key_value,sizeof(key_value));
if(ret != sizeof(key_value))
{
if(errno != EAGAIN)
perror("read buttons\n");
continue;
}
else
{
for(i = 0; i < 6 ; i++)
{
if(key_value != 0)
printf("You pressed buttons %d\n", key_value);
}
}
}
}
/*关闭设备*/
close(buttons_fd);
return 0;
}
没去抖动导致误报频繁。
用户539229 2009-9-6 10:12