一 what
linux系统帮我们实现了一个定时器处理函数,对于驱动程序来说,只需要做初始化、添加定时器的方法即可,主要用到的函数就是init_timer(),add_timer()。
二 why
我们都知道一个按键的物理结构,就是一个弹片,在按下的过程中,弹片会跟下面的触点接触;松开的时候,弹片和触点断开;
但是由于物理结构的惯性作用,按键在按下与弹片接触瞬间,并不是一直保持稳定接触的,它会一会接触,一会断开,从波形上看,就是会产生一个脉冲波,对于cpu的gpio引脚来说,一会检测到一个上升沿,一会检测到一个下降沿,而实际效果来看,我们只需要一个边沿,这样就会造成检测错误。
那位了避免这种方法,就需要防抖功能,可以从硬件上实现,也可以从软件上实现。从软件实现角度来看的话,也可以有多种实现方式,今天我们介绍的是一种定时器延时一段时间后,再去读取引脚电平的方法。
三 how
我们仍然以字符设备驱动程序之读取按键状态的驱动来调用验证linux系统的定时器功能
1、在字符设备驱动初始化函数中,初始化定时器,前面三条语句就是定时器初始化和add部分,应该很好理解。
static int seventh_drv_init(void)
{
init_timer(&button_timer);
button_timer.function = (void (*)(unsigned long))buttons_timer_function;
//button_timer.data = (unsigned long)Scpnt;
//button_timer.expires = jiffies + 100 * HZ;
add_timer(&button_timer);
major = register_chrdev(0, "my_seventh_drv", &seventh_drv_fops);
seventhdrv_class = class_create(THIS_MODULE, "myseventhdrv");
seventhdrv_class_dev = class_device_create(seventhdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/xyz */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
2、因为我们需要的是定时器防抖功能,因此在按键检测中断中,设置定时器超超时等待时间
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
/* 10ms启动定时器 */
mod_timer(&button_timer, jiffies + HZ / 100);
irq_pd = (struct pin_desc *)dev_id;
return IRQ_RETVAL(IRQ_HANDLED);
}
3、当定时器超时时间到时,也会产生一个定时器中断,我们在这个函数中读取gpio引脚电平
static void buttons_timer_function(unsigned long data)
{
struct pin_desc *pindesc = (struct pin_desc *)irq_pd;
unsigned int pinval;
//printk("irq=%d\n", irq);
if (!pin_desc)
return 0;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if (pinval)
{
/* 松开 */
key_val = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
key_val = pindesc->key_val;
}
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
kill_fasync(&button_async, SIGIO, POLL_IN);
}
至此,定时器实现防抖功能的代码已经实现完成,完整代码如下:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>
static struct class *seventhdrv_class;
static struct class_device *seventhdrv_class_dev;
volatile unsigned long *gpfcon;
volatile unsigned long *gpgcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgdat;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,seventh_drv_read将它清0 */
static volatile int ev_press = 0;
static unsigned char key_val;
//定义原子变量canopen并初始化为1
static atomic_t canopen = ATOMIC_INIT(1);
struct pin_desc
{
unsigned int pin;
unsigned int key_val;
};
static struct pin_desc *irq_pd;
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
struct pin_desc pins_desc[4] =
{
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04}
};
static struct fasync_struct *button_async;
static struct timer_list button_timer;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
/* 10ms启动定时器 */
mod_timer(&button_timer, jiffies + HZ / 100);
irq_pd = (struct pin_desc *)dev_id;
return IRQ_RETVAL(IRQ_HANDLED);
}
static int seventh_drv_open(struct inode *inode, struct file *file)
{
if (!atomic_dec_and_test(&canopen))
{
printk("can not open\n");
atomic_inc(&canopen);
return -EBUSY;
}
/* 配置GPF0,2为输出引脚 */
/* 配置GPG3,11为输出引脚 */
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
return 0;
}
static int seventh_drv_read(struct file *filp, char __user *buf,
size_t size, loff_t *ppos)
{
/* 返回4个引脚电平状态 */
if (size != 1)
return -EINVAL;
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return (sizeof(key_val));
}
int seventh_drv_close(struct inode *inode, struct file *file)
{
atomic_inc(&canopen);
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT11, &pins_desc[2]);
free_irq(IRQ_EINT19, &pins_desc[3]);
return 0;
}
static unsigned int seventh_drv_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 int seventh_drv_fasync(int fd, struct file *filp, int on)
{
printk("driver: app use it\n");
return fasync_helper(fd, filp, on, &button_async);
}
static struct file_operations seventh_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = seventh_drv_open,
.read = seventh_drv_read,
.release = seventh_drv_close,
.poll = seventh_drv_poll,
.fasync = seventh_drv_fasync,
};
static int major;
static void buttons_timer_function(unsigned long data)
{
struct pin_desc *pindesc = (struct pin_desc *)irq_pd;
unsigned int pinval;
//printk("irq=%d\n", irq);
if (!pin_desc)
return 0;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if (pinval)
{
/* 松开 */
key_val = 0x80 | pindesc->key_val;
}
else
{
/* 按下 */
key_val = pindesc->key_val;
}
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
kill_fasync(&button_async, SIGIO, POLL_IN);
}
static int seventh_drv_init(void)
{
init_timer(&button_timer);
button_timer.function = (void (*)(unsigned long))buttons_timer_function;
//button_timer.data = (unsigned long)Scpnt;
//button_timer.expires = jiffies + 100 * HZ;
add_timer(&button_timer);
major = register_chrdev(0, "my_seventh_drv", &seventh_drv_fops);
seventhdrv_class = class_create(THIS_MODULE, "myseventhdrv");
seventhdrv_class_dev = class_device_create(seventhdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/xyz */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
static void seventh_drv_exit(void)
{
unregister_chrdev(major, "my_seventh_drv");
class_device_unregister(seventhdrv_class_dev);
class_destroy(seventhdrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(seventh_drv_init);
module_exit(seventh_drv_exit);
MODULE_LICENSE("GPL");
四 test
测试代码和《linux字符设备驱动之同步互斥阻塞(五)》基本一致,可参考这部分内容,写测试程序来测试我们的功能