tq2440按键中断,去抖

http://www.oschina.net/code/snippet_174242_12600

/*****************************************************
功能:
能实现按键中断。
实现去抖动功能,按键基本准确。
******************************************************/
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>
 
#define DEVICE_NAME "driver_button"
 
staticvolatile int ev_press = 0;
staticvolatile int dev_num=0;
staticvolatile int press_cnt [] = {0, 0, 0, 0};
staticvolatile int flag_press[] = {0, 0, 0, 0};
staticDECLARE_WAIT_QUEUE_HEAD(button_waitq);
 
staticirqreturn_t irq_interrupt(intirq, intdev_id)
{
//disable_irq(IRQ_EINT0); ???????????????????????????ú
//printk("dev_id= %d\n",dev_id);
 
dev_num=dev_id;
ev_press=1;
switch( dev_num)
{
case1:
flag_press[0]=1;break;
case2:
flag_press[1]=1;break;
case3:
flag_press[2]=1;break;
case4:
flag_press[3]=1;break;
default:
break;
}
wake_up_interruptible(&button_waitq);
 
//enable_irq(IRQ_EINT0);
returnIRQ_RETVAL(IRQ_HANDLED);
}
        
// ------------------- OPEN ------------------------
ssize_t button_open (structinode * inode ,structfile * file)
{
request_irq(IRQ_EINT0, irq_interrupt, IRQ_TYPE_EDGE_FALLING, "KEY1", 1);
request_irq(IRQ_EINT1, irq_interrupt, IRQ_TYPE_EDGE_FALLING, "KEY2", 2);
request_irq(IRQ_EINT2, irq_interrupt, IRQ_TYPE_EDGE_FALLING, "KEY3", 3);
request_irq(IRQ_EINT4, irq_interrupt, IRQ_TYPE_EDGE_FALLING, "KEY4", 4);
 
printk("-----------------drive button open ok----------------\n");
return0;
 
}
 
// ------------------- RELEASE/CLOSE ---------------
ssize_t button_release (structinode  * inode ,structfile * file)
{
free_irq(IRQ_EINT0, 1);
free_irq(IRQ_EINT1, 2);
free_irq(IRQ_EINT2, 3);
free_irq(IRQ_EINT4, 4);
return0;
}
 
// ------------------- READ ------------------------
ssize_t button_read (structfile * file ,char* buf, size_tcount, loff_t * f_ops)
{
wait_event_interruptible(button_waitq, ev_press);
 
ev_press = 0;
msleep(200);
if(flag_press[0])
{
press_cnt[0]++;
}
elseif(flag_press[1])
{
press_cnt[1]++;
}
elseif(flag_press[2])
{
press_cnt[2]++;
}
elseif(flag_press[3])
{
press_cnt[3]++;
}
flag_press[0]=0; flag_press[1]=0; flag_press[2]=0; flag_press[3]=0;
printk("------------device read --------------------\n");
printk("press_cnt[0]= %d\n",press_cnt[0]);
copy_to_user( buf,(constvoid *)press_cnt,sizeof(press_cnt) );
return0;
}
 
// ------------------- WRITE -----------------------
ssize_t button_write (structfile * file ,constchar * buf, size_tcount, loff_t * f_ops)
{
return0;
}
 
// ------------------- IOCTL -----------------------
ssize_t button_ioctl (structinode * inode ,structfile * file, unsigned intcmd, unsigned longarg)
{
return0;
}
 
// -------------------------------------------------
staticstruct file_operations button_ops ={
.owner = THIS_MODULE,
.open   = button_open,
.read   = button_read,
.write   = button_write,
.ioctl   = button_ioctl,
.release = button_release,
};
 
staticstruct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &button_ops,
};
 
staticint __init init_button_init(void) 
{
intret;
ret = misc_register(&misc);
 
printk("-----------------drive button init ok----------------\n");
return0;
}
 
staticvoid __exit exit_button_ctl(void)
{
misc_deregister(&misc);
}
 
module_init(init_button_init);
module_exit(exit_button_ctl);
MODULE_LICENSE("GPL");
 
 
 
/************************
 *test app***************
 ***********************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>      // open() close()
#include <unistd.h>     // read() write()
 
#define DEVICE_NAME "/dev/driver_button"
 
//------------------------------------- main ---------------------------------------------
intmain(intargc, char**argv)
{
intfd,ret;
intcnt=0;
unsignedintkey_val[4];
       fd = open(DEVICE_NAME, O_RDWR);
  
if(fd == -1)
{
printf("can't open device mknod %s c zhu ci \n",DEVICE_NAME);
return0;
}
 
while(1)
{
read(fd,key_val,sizeof(key_val));
printf(" key_val=  %d  %d  %d  %d\n",key_val[0],key_val[1],key_val[2],key_val[3]);
}
 
ret = close(fd);
printf("close gpio_led_driver test\n");
 
return0;
}


你可能感兴趣的:(tq2440按键中断,去抖)