Linux驱动入门(七)使用定时器消除按键抖动
copy from:https://blog.****.net/weixin_42462202/article/details/100019418
Linux驱动入门(七)使用定时器消除按键抖动
文章目录
Linux驱动入门(七)使用定时器消除按键抖动
一、为什么要消抖?
二、如何消抖?
三、Linux内核定时器
四、源码
五、测试
本文将在Linux驱动入门(五)阻塞方式实现按键驱动的基础上,使用定时器消除按键抖动
一、为什么要消抖?
对于没有做硬件消抖的按键,在按下按键到电平稳定期间,会有一段抖动,此时电平会在高电平和低电平不断地跳动
如果我们的按键中断采用下降沿触发,从图中可以看出有多段下降沿,那么想必按下一次按键,中断会被触发多次,那么我们button_read函数也会被唤醒多次,这就导致一次按键,应用层多次返回,这并不是我们想要的效果
二、如何消抖?
软件消抖的原理非常简单,只要通过延迟(一般为10ms),等待按键稳定后,再来检测按键是否被按下
在Linux驱动中,如果使用在原理打转的方式来实现延时的话,这是非常不可取的,在中断处理中更是大忌
所以要通过定时器来实现延迟,那具体怎么做呢?
在上面介绍中,我们了解在按键按下的时候会触发多次按键中断,那么我们可以在按键中断设置定时器,在定时器的处理函数中去读取按键状态
具体的处理方法是,在每一个下降沿触发的中断处理函数中,将定时器设置为10ms后超时,这样子,将在最后一个下降沿10ms后,定时器超时,再检测按键状态,达到消抖的目的,如图所示
三、Linux内核定时器
定时器对象
struct timer_list timer;
初始化定时器
void init_timer(struct timer_list * timer);
/* 设置定时器的处理函数和传递参数 */
timer.function = &xxx_do_timer;
timer.data = (unsigned long)dev;
内核提供一个宏定义完成上述功能
setup_timer(timer, fn, data)
注册定时器
void add_timer(struct timer_list *timer)
删除定时器
int del_timer(struct timer_list *timer)
修改定时器超时时间
int mod_timer(struct timer_list *timer, unsigned long expires)
内核中的jiffies表示时钟节拍数,宏定义HZ表示时钟节拍的频率
Linux内核的HZ为100,表示1s时钟跳动100下,所以时钟周期的10ms
如果要延迟10ms,可以这么做
mod_timer(timer, jiffies + HZ/100);
四、源码
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <asm/io.h>
#include <asm/uaccess.h>
static dev_t dev_id;
static struct cdev *button_dev;
static struct class *button_class;
/* 定义并初始化一个等待队列头 */
static DECLARE_WAIT_QUEUE_HEAD(button_wq_head);
static int button_conditon;
static int button_val;
static struct timer_list timer;
static void timer_fun(unsigned long data)
{
/* 判断等待队列中是否有等待元素 */
if(!waitqueue_active(&button_wq_head))
return;
/* 读取按键值 */
button_val = gpio_get_value(S5PV210_GPH0(2));
if(button_val) //按键未被按下
return;
/* 唤醒等待队列 */
button_conditon = 1;
wake_up_interruptible(&button_wq_head);
}
static irqreturn_t button_irq(int irq, void *data)
{
mod_timer(&timer, jiffies+HZ/100);
return IRQ_HANDLED;
}
static int button_open(struct inode *inode, struct file *file)
{
int ret;
ret = request_irq(IRQ_EINT2, button_irq, IRQF_TRIGGER_FALLING, "button_irq", NULL);
return 0;
}
static ssize_t button_read(struct file *file, char __user *data, size_t size, loff_t *loff)
{
int ret;
int val;
/* 睡眠等待 */
button_conditon = 0;
wait_event_interruptible(button_wq_head, button_conditon);
button_conditon = 0;
val = button_val;
ret = copy_to_user(data, &val, sizeof(val));
return sizeof(val);
}
static int button_release(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT2, NULL);
return 0;
}
static struct file_operations button_fops = {
.owner = THIS_MODULE,
.open = button_open,
.read = button_read,
.release = button_release,
};
static __init int button_init(void)
{
/* 申请设备号 */
alloc_chrdev_region(&dev_id, 1, 1, "button");
/* 分配字符设备 */
button_dev = cdev_alloc();
/* 设置字符设备 */
cdev_init(button_dev, &button_fops);
/* 注册字符设备 */
cdev_add(button_dev, dev_id, 1);
/* 创建设备节点 */
button_class = class_create(THIS_MODULE, "button"); //创建类
device_create(button_class, NULL, dev_id, NULL, "button"); //创建设备节点
gpio_request(S5PV210_GPH0(2), "button");
setup_timer(&timer, timer_fun, 0);
add_timer(&timer);
return 0;
}
static __exit void button_exit(void)
{
/* 注销设备节点 */
device_destroy(button_class, dev_id);
class_destroy(button_class);
/* 注销字符设备 */
cdev_del(button_dev);
kfree(button_dev);
/* 注销注册的设备号 */
unregister_chrdev_region(dev_id, 1);
gpio_free(S5PV210_GPH0(2));
del_timer(&timer);
}
module_init(button_init);
module_exit(button_exit);
MODULE_LICENSE("GPL");
五、测试
测试应用程序
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define BUTTON_DEV "/dev/button"
int main(int argc, char* argv[])
{
int val;
int fd = open(BUTTON_DEV, O_RDONLY);
if(fd < 0)
{
printf("failed to open %s
", BUTTON_DEV);
return -1;
}
while(1)
{
read(fd, &val, sizeof(val));
printf("read return
");
if(val == 0)
{
printf("button press
");
}
}
close(fd);
return 0;
}
加载驱动模块,执行测试应用程序
运行效果,只有按键按下时,read函数才会返回,且按键按下一次,read只返回一次
————————————————
版权声明:本文为****博主「JT同学」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.****.net/weixin_42462202/article/details/100019418