平台:TQ2440开发板
上传添加了防抖动的key.c,防抖动方式:
按键产生中断,中断处理函数中设置定时器,在100ms后调用定时器句柄函数
如果100ms后,定时器句柄函数中读取到的GPIO仍被按下(低电平),则说明是正常按键
如果100ms后,定时器句柄函数中读取到的GPIO没有继续被按下(高电平),则说明是抖动
(一)中断处理函数:
static irqreturn_t kobox_gpio_irq_handle(int irq, void *dev_id) { int key; disable_irq_nosync(irq); printk("irq = %d\n", irq); if(dev_id) printk("dev_id:%s\n", dev_id); switch(irq) { case IRQ_EINT1: key = 0; break; case IRQ_EINT4: key = 1; break; case IRQ_EINT2: key = 2; break; case IRQ_EINT0: key = 3; break; default: printk("invalid irq:%d\n", irq); return IRQ_HANDLED; } /* 去抖:延时100ms后,在buttons_timer中读取按键状态,如果还是按下的,就说明是被正常按下的 使用timer是一种方式,后面再采用工作队列、tasklet中的方式来处理 */ mod_timer(&key_timers[key], jiffies + KEY_TIMER_DELAY2); enable_irq(irq); return IRQ_RETVAL(IRQ_HANDLED); }
/* 去抖:中断中设置定时器100ms,在buttons_timer中读取按键状态,如果还是按下的,就说明是被正常按下的 */ static void buttons_timer(unsigned long arg) { int ret; unsigned int pin; /* 中断后100ms才会导致,执行该函数 */ printk("i am at [%s][%d], arg:%d\n", __FUNCTION__, __LINE__, arg); pin = key_gpio_res[arg].gpfPin; /* 将引脚由EINTX设置会GPIO */ set_gpio_as_gpio(); /* 读取对应引脚GPIO的值,返回0表示按键真正被按下,返回1表示抖动 */ ret = get_gpio_portf_value(pin); if(0 == ret) { pressCnt[arg]++; printk("key%d pressed: pressCnt[arg]:%d\n", arg, pressCnt[arg]); } /* 将引脚设置回EINTX */ set_gpio_as_eint(); return; }
(三)key.c完整代码
#include "key.h" #define S3C_ADDR_BASE 0xF6000000 #define S3C_ADDR(x) (S3C_ADDR_BASE + (x)) #define S3C2410_PA_UART (0x50000000) #define S3C2410_PA_GPIO (0x56000000) #define S3C_VA_UART S3C_ADDR(0x01000000) /* UART */ #define S3C24XX_PA_UART S3C2410_PA_UART #define S3C24XX_VA_UART S3C_VA_UART #define S3C24XX_PA_GPIO S3C2410_PA_GPIO #define S3C24XX_VA_GPIO ((S3C24XX_PA_GPIO - S3C24XX_PA_UART) + S3C24XX_VA_UART) #define S3C2410_GPIOREG(x) ((x) + S3C24XX_VA_GPIO) #define S3C2410_GPBCON S3C2410_GPIOREG(0x10) #define S3C2410_GPBDAT S3C2410_GPIOREG(0x14) #define S3C2410_GPBUP S3C2410_GPIOREG(0x18) #define S3C2410_GPFCON S3C2410_GPIOREG(0x50) #define S3C2410_GPFDAT S3C2410_GPIOREG(0x54) #define S3C2410_GPFUP S3C2410_GPIOREG(0x58) #define S3C2410_EXTINT0 S3C2410_GPIOREG(0x88) #define S3C2410_EXTINT1 S3C2410_GPIOREG(0x8C) #define S3C2410_EXTINT2 S3C2410_GPIOREG(0x90) #define S3C2410_CPUIRQ_OFFSET (16) #define S3C2410_IRQ(x) ((x) + S3C2410_CPUIRQ_OFFSET) /* main cpu interrupts */ #define IRQ_EINT0 S3C2410_IRQ(0) /* 16 */ #define IRQ_EINT1 S3C2410_IRQ(1) /* 17 */ #define IRQ_EINT2 S3C2410_IRQ(2) /* 18 */ #define IRQ_EINT4t7 S3C2410_IRQ(4) /* 20 */ #define IRQ_EINT4 S3C2410_IRQ(36) /* 52 */ #define IRQF_DISABLED 0x00000020 #define IRQF_SHARED 0x00000080 #define IRQF_PROBE_SHARED 0x00000100 #define __IRQF_TIMER 0x00000200 #define IRQF_PERCPU 0x00000400 #define IRQF_NOBALANCING 0x00000800 #define IRQF_IRQPOLL 0x00001000 #define IRQF_ONESHOT 0x00002000 #define IRQF_NO_SUSPEND 0x00004000 #define IRQF_FORCE_RESUME 0x00008000 #define IRQF_NO_THREAD 0x00010000 #define IRQF_EARLY_RESUME 0x00020000 typedef struct gpioRes { int irqNum; /* 中断号 */ unsigned int ctrlReg; /* 控制寄存器,用于设置复用为GPIO */ unsigned int ctrlBit; /* 控制寄存器的哪一位,用于复用为GPIO */ unsigned int trigReg; /* 中断方式寄存器,设置中断的触发方式 */ unsigned int trigBit; /* 中断方式寄存器哪一位,设置中断的触发方式 */ unsigned int irqFlag; /* 共享还是不共享,注册中断的flag */ char irqName[32]; /* 中断名称 */ unsigned int gpfPin; /* GPF的第几个pin */ char Reserved[10]; /* 保留 */ }gpioRes; unsigned int pressCnt[4] = {0, 0, 0, 0}; #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof((arr)[0])) static int kobox_key_open(struct inode *inode, struct file *file) { return 0; } static int kobox_key_release(struct inode *inode, struct file *file) { return 0; } static long kobox_key_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { return 0; } /* GPF相关寄存器: GPFCON 0x56000050 R/W Configures the pins of port F 0x0 GPFDAT 0x56000054 R/W The data register for port F Undef. GPFUP 0x56000058 R/W Pull-up disable register for port F 0x000 K1: GPF1 -EINT1: GPF1 [3:2] 00 = Input 01 = Output 10 = EINT[1] 11 = Reserved K2: GPF4 -EINT4: GPF4 [9:8] 00 = Input 01 = Output 10 = EINT[4] 11 = Reserved K3: GPF2 -EINT2: GPF2 [5:4] 00 = Input 01 = Output 10 = EINT2] 11 = Reserved K4: GPF0 -EINT0: GPF0 [1:0] 00 = Input 01 = Output 10 = EINT[0] 11 = Reserved */ gpioRes key_gpio_res[4] = { {IRQ_EINT1, S3C2410_GPFCON, 2, S3C2410_EXTINT0, 5, NULL, "key1", 1}, /* key1 */ {IRQ_EINT4, S3C2410_GPFCON, 8, S3C2410_EXTINT0, 17, IRQF_SHARED, "key2", 4}, /* key2 */ {IRQ_EINT2, S3C2410_GPFCON, 4, S3C2410_EXTINT0, 9, NULL, "key3", 2}, /* key3 */ {IRQ_EINT0, S3C2410_GPFCON, 0, S3C2410_EXTINT0, 1, NULL, "key4", 0}, /* key4 */ }; #define KEY_TIMER_DELAY1 (HZ/50) //按键按下去抖延时20毫秒 #define KEY_TIMER_DELAY2 (HZ/10) //按键抬起去抖延时100毫秒 #define KEY_COUNT 4 static struct timer_list key_timers[KEY_COUNT]; //定义4个按键去抖动定时器 static DECLARE_WAIT_QUEUE_HEAD(button_waitq); //定义并初始化等待队列 static void set_gpio_as_eint(void) { int i; unsigned uiVal = 0; for(i=0; i< ARRAY_SIZE(key_gpio_res); i++) { uiVal = readl(key_gpio_res[i].ctrlReg); uiVal &= ~(0x01 << key_gpio_res[i].ctrlBit); uiVal |= (0x01 << (key_gpio_res[i].ctrlBit + 1)); writel(uiVal, key_gpio_res[i].ctrlReg); } return; } static void set_gpio_as_gpio(void) { int i; unsigned uiVal = 0; for(i=0; i< ARRAY_SIZE(key_gpio_res); i++) { uiVal = readl(key_gpio_res[i].ctrlReg); uiVal &= ~(0x01 << key_gpio_res[i].ctrlBit); uiVal &= ~(0x01 << (key_gpio_res[i].ctrlBit + 1)); writel(uiVal, key_gpio_res[i].ctrlReg); } return; } static irqreturn_t kobox_gpio_irq_handle(int irq, void *dev_id) { int key; disable_irq_nosync(irq); printk("irq = %d\n", irq); if(dev_id) printk("dev_id:%s\n", dev_id); switch(irq) { case IRQ_EINT1: key = 0; break; case IRQ_EINT4: key = 1; break; case IRQ_EINT2: key = 2; break; case IRQ_EINT0: key = 3; break; default: printk("invalid irq:%d\n", irq); return IRQ_HANDLED; } /* 去抖:延时100ms后,在buttons_timer中读取按键状态,如果还是按下的,就说明是被正常按下的 使用timer是一种方式,后面再采用工作队列、tasklet中的方式来处理 */ mod_timer(&key_timers[key], jiffies + KEY_TIMER_DELAY2); enable_irq(irq); return IRQ_RETVAL(IRQ_HANDLED); } /* GPF相关寄存器: GPFCON 0x56000050 R/W Configures the pins of port F 0x0 GPFDAT 0x56000054 R/W The data register for port F Undef. GPFUP 0x56000058 R/W Pull-up disable register for port F 0x000 K1: GPF1 -EINT1: GPF1 [3:2] 00 = Input 01 = Output 10 = EINT[1] 11 = Reserved K2: GPF4 -EINT4: GPF4 [9:8] 00 = Input 01 = Output 10 = EINT[4] 11 = Reserved K3: GPF2 -EINT2: GPF2 [5:4] 00 = Input 01 = Output 10 = EINT2] 11 = Reserved K4: GPF0 -EINT0: GPF0 [1:0] 00 = Input 01 = Output 10 = EINT[0] 11 = Reserved */ /* 该函数返回0表示按键被按下,返回非0表示没有再被按下,认为这是电平毛刺导致的,是噪声信号 所以,该函数返回0,表示有按键被按下,返回非0表示是抖动 */ static int get_gpio_portf_value(unsigned int pin) { int ret; unsigned int uiVal = 0; printk("I AM @ [%s][%d], pin:%d\n", __FUNCTION__,__LINE__, pin); uiVal = readl(S3C2410_GPFDAT); ret = (0x1 << pin) & uiVal; printk("I AM @ [%s][%d], ret:%d\n", __FUNCTION__,__LINE__, ret); return ret; } /* 去抖:中断中设置定时器100ms,在buttons_timer中读取按键状态,如果还是按下的,就说明是被正常按下的 */ static void buttons_timer(unsigned long arg) { int ret; unsigned int pin; /* 中断后100ms才会导致,执行该函数 */ printk("i am at [%s][%d], arg:%d\n", __FUNCTION__, __LINE__, arg); pin = key_gpio_res[arg].gpfPin; /* 将引脚由EINTX设置会GPIO */ set_gpio_as_gpio(); /* 读取对应引脚GPIO的值,返回0表示按键真正被按下,返回1表示抖动 */ ret = get_gpio_portf_value(pin); if(0 == ret) { pressCnt[arg]++; printk("key%d pressed: pressCnt[arg]:%d\n", arg, pressCnt[arg]); } /* 将引脚设置回EINTX */ set_gpio_as_eint(); return; } static int request_irq_for_gpio(void) { int i; int ret; unsigned uiVal; int nouse; for(i=0; i<ARRAY_SIZE(key_gpio_res);i++) { /* 设置中断触发方式:下降沿有效,触发中断,以便根据GPIO的值来判断是否仍在按下 */ uiVal = readl(key_gpio_res[i].trigReg); uiVal |= (0x1 << (key_gpio_res[i].trigBit)); uiVal &= ~(0x1 << (key_gpio_res[i].trigBit + 1)); writel(uiVal, key_gpio_res[i].trigReg); /* 注册中断 */ ret = request_irq(key_gpio_res[i].irqNum, kobox_gpio_irq_handle, key_gpio_res[i].irqFlag, key_gpio_res[i].irqName, (void *)key_gpio_res[i].irqName); if(ret) printk("[func:%s][line:%d] request_irq failed, ret:%d!\n", __FUNCTION__,__LINE__,ret); else printk("[func:%s][line:%d] request_irq ok, irq:%d!\n", __FUNCTION__,__LINE__, key_gpio_res[i].irqNum); /* 初始化定时器,后面用于去抖动 */ setup_timer(&key_timers[i], buttons_timer, i); key_timers[i].expires = jiffies + KEY_TIMER_DELAY1*i; add_timer(&key_timers[i]); } return 0; } struct file_operations kobox_key_operations = { .owner = THIS_MODULE, .open = kobox_key_open, .release = kobox_key_release, .unlocked_ioctl = kobox_key_ioctl, }; //GPB0 int major; int minor; struct cdev cdev; struct class *kobox_key_class; struct device *pstdev = NULL; #define GPIO_KEY_NAME "kobox_key" int __init key_drv_init(void) { int error; dev_t dev; printk("#####enter key_drv_init!\n"); major = register_chrdev(0, GPIO_KEY_NAME, &kobox_key_operations); if (major < 0) { printk(" can't register major number\n"); return major; } /* create class */ kobox_key_class = class_create(THIS_MODULE, GPIO_KEY_NAME); if(IS_ERR(kobox_key_class)) { printk("class_create failed!\n"); goto fail; } /* create /dev/kobox_gpio */ pstdev = device_create(kobox_key_class, NULL, MKDEV(major, 0), NULL, GPIO_KEY_NAME); if(!pstdev) { printk("device_create failed!\n"); goto fail1; } /* set gpf0/1/2/4 as extern interrupt pins */ set_gpio_as_eint(); request_irq_for_gpio(); printk("#####key_drv_init ok!\n"); return 0; fail1: class_destroy(kobox_key_class); fail: unregister_chrdev(major, GPIO_KEY_NAME); return -1; } void __exit key_drv_exit(void) { printk("exit gpio drv!\n"); device_destroy(kobox_key_class, MKDEV(major, 0)); class_destroy(kobox_key_class); unregister_chrdev(major, GPIO_KEY_NAME); return; } module_init(key_drv_init); module_exit(key_drv_exit); MODULE_LICENSE("GPL");