|
/*****************************************************
功能:
能实现按键中断。
实现去抖动功能,按键基本准确。
******************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "driver_button"
static volatile int ev_press = 0;
static volatile int dev_num=0;
static volatile int press_cnt [] = {0, 0, 0, 0};
static volatile int flag_press[] = {0, 0, 0, 0};
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static irqreturn_t irq_interrupt(int irq, int dev_id)
{
//
//disable_irq(IRQ_EINT0); ???????????????????????????ú
//
//printk("dev_id= %dn",dev_id);
dev_num=dev_id;
ev_press=1;
switch( dev_num)
{
case 1:
flag_press[0]=1;break;
case 2:
flag_press[1]=1;break;
case 3:
flag_press[2]=1;break;
case 4:
flag_press[3]=1;break;
default:
break;
}
wake_up_interruptible(&button_waitq);
//enable_irq(IRQ_EINT0);
return IRQ_RETVAL(IRQ_HANDLED);
}
// ------------------- OPEN ------------------------
ssize_t button_open (struct inode * inode ,struct file * 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");
return 0;
}
// ------------------- RELEASE/CLOSE ---------------
ssize_t button_release (struct inode * inode ,struct file * file)
{
free_irq(IRQ_EINT0, 1);
free_irq(IRQ_EINT1, 2);
free_irq(IRQ_EINT2, 3);
free_irq(IRQ_EINT4, 4);
return 0;
}
// ------------------- READ ------------------------
ssize_t button_read (struct file * file ,char * buf, size_t count, loff_t * f_ops)
{
wait_event_interruptible(button_waitq, ev_press);
/* ???????????±??ev_press????1?????ü??0 */
ev_press = 0;
msleep(200);
if(flag_press[0])
{
press_cnt[0]++;
}
else if(flag_press[1])
{
press_cnt[1]++;
}
else if(flag_press[2])
{
press_cnt[2]++;
}
else if(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]= %dn",press_cnt[0]);
copy_to_user( buf,(const void *)press_cnt,sizeof(press_cnt) );
return 0;
}
// ------------------- WRITE -----------------------
ssize_t button_write (struct file * file ,const char * buf, size_t count, loff_t * f_ops)
{
return 0;
}
// ------------------- IOCTL -----------------------
ssize_t button_ioctl (struct inode * inode ,struct file * file, unsigned int cmd, unsigned long arg)
{
return 0;
}
// -------------------------------------------------
static struct file_operations button_ops ={
.owner = THIS_MODULE,
.open = button_open,
.read = button_read,
.write = button_write,
.ioctl = button_ioctl,
.release = button_release,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &button_ops,
};
static int __init init_button_init(void)
{
int ret;
ret = misc_register(&misc);
printk("-----------------drive button init ok----------------n");
return 0;
}
static void __exit exit_button_ctl(void)
{
misc_deregister(&misc);
}
module_init(init_button_init);
module_exit(exit_button_ctl);
MODULE_LICENSE("GPL");
#include
#include
#include
#include // open() close()
#include // read() write()
#define DEVICE_NAME "/dev/driver_button"
//------------------------------------- main ---------------------------------------------
int main(int argc, char **argv)
{
int fd,ret;
int cnt=0;
unsigned int key_val[4];
fd = open(DEVICE_NAME, O_RDWR);
if (fd == -1)
{
printf("can't open device mknod %s c zhu ci n",DEVICE_NAME);
return 0;
}
while(1)
{
read(fd,key_val,sizeof(key_val));
printf(" key_val= %d %d %d %dn",key_val[0],key_val[1],key_val[2],key_val[3]);
}
// close
ret = close(fd);
printf ("close gpio_led_driver testn");
return 0;
}// end main
感谢chen4013874的分享!
供货情况:
天嵌科技提供专业嵌入式板卡和行业解决方案。如有需要,可以联系天嵌科技的销售人员。
销售电话:020-38219416 38373101
技术支持:020-38219416转807 820
网址:http://www.embedsky.com
官方淘客:https://embedsky.taobao.com/
|
|