目录
前言
物理按键触发电平不是很稳定,会呈现锯齿状使得一次按键状态触发多个中断,导致应用程序读取按键值产生错误。驱动层使用内核定时器可以解决此种错误,每次中断到来时触发定时器10ms后再对该中断进行处理。这样一次按键状态因为抖动而触发的多个中断,最终只会去处理一次。
内核定时器
Linux 内核定时器采用系统时钟来实现,与硬件定时器功能一样,当超时时间到了以后设
置的定时处理函数就会执行。内核定时器不需要一大堆寄存器的配置工作,并且内核定时器执行完超时处理函数以后就会自动关闭。若需要周期运行,则需要在处理函数中再次打开内核定时器。
Linux 内核使用 timer_list 结构体表示内核定时器, timer_list 定义在文件include/linux/timer.h 中,定义如下:
struct timer_list {
struct list_head entry;
unsigned long expires; //定时器超时时间,单位是节拍
void (*function)(unsigned long); //定时器超时回调函数
unsigned long data; //传给回调函数的参数
struct tvec_t_base_s *base;
#ifdef CONFIG_TIMER_STATS
void *start_site;
char start_comm[16];
int start_pid;
#endif
};
要使用内核定时器首先要先定义一个 timer_list 变量,表示定时器, tiemr_list 结构体的expires 成员变量表示超时时间,单位为节拍数。
内核定时器API函数
init_timer 函数负责初始化 timer_list 类型变量,当定义完timer_list结构体以后,需要使用此函数进行初始化。
void init_timer(struct timer_list *timer)
函数参数和返回值含义如下:
timer:要初始化定时器。
返回值: 没有返回值
add_timer 函数用于向 Linux 内核注册定时器,使用 add_timer 函数向内核注册定时器以后,定时器就会开始运行。
void add_timer(struct timer_list *timer)
函数参数和返回值含义如下:
timer:要注册的定时器。
返回值: 没有返回值。
del_timer 函数用于删除一个定时器,不管定时器有没有被激活,都可以使用此函数删除。
int del_timer(struct timer_list * timer)
函数参数和返回值含义如下:
timer:要删除的定时器。
返回值: 0,定时器还没被激活; 1,定时器已经激活。
del_timer_sync 函数是 del_timer 函数的同步版,会等待其他处理器使用完定时器再删除。
int del_timer_sync(struct timer_list *timer)
函数参数和返回值含义如下:
timer:要删除的定时器。
返回值: 0,定时器还没被激活; 1,定时器已经激活。
mod_timer 函数用于修改定时值,如果定时器还没有激活的话, mod_timer 函数会激活定时器,通常使用此函数达到定时器周期循环的目的。
int mod_timer(struct timer_list *timer, unsigned long expires)
函数参数和返回值含义如下:
timer:要修改超时时间(定时值)的定时器。
expires:修改后的超时时间。单位是节拍数。
返回值: 0,调用 mod_timer 函数前定时器未被激活; 1,调用 mod_timer 函数前定时器已
被激活。
可以使用以下函数,得到ms、us与系统节拍数的转换。
extern unsigned long msecs_to_jiffies(const unsigned int m);
extern unsigned long usecs_to_jiffies(const unsigned int u);
static irqreturn_t button_irq(int irq, void *dev_id)
{
irq_pd = (struct pin_desc *)dev_id;
mod_timer(&buttons_timer,jiffies+msecs_to_jiffies(10)); //设置定时器超时时间为10ms
return IRQ_RETVAL(IRQ_HANDLED);
}
使用定时器消抖的按键驱动
#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>
#define DEVICE_NAME "button" /* 加载模块后,执行”cat /proc/devices”命令看到的主设备名称 */
#define BUTTON_MAJOR 232 /* 主设备号 */
static struct class *buttons_class; //设备类 使用cd /sys/class 查看当前系统上的设备类
static struct class_device *buttons_class_dev; //类设备,一个设备类下可以有多个类设备 ls /dev/* 查看当前系统上的设备。或者 cd /sys/class/类名/ 查看当前设备类下的设备。
struct pin_desc{
unsigned int pin;
unsigned int pin_val;
};
struct pin_desc pins_desc[3] = {
{S3C2410_GPF0,0x1},
{S3C2410_GPF2,0x2},
{S3C2410_GPG3,0x3},
};
// 按下 0x1 0x2 0x3
// 松开 0x81 0x82 0x83
static unsigned char key_val;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;
static struct fasync_struct *buttons_async_queue;
static struct timer_list buttons_timer;
static struct pin_desc *irq_pd;
static irqreturn_t button_irq(int irq, void *dev_id)
{
irq_pd = (struct pin_desc *)dev_id;
mod_timer(&buttons_timer,jiffies+msecs_to_jiffies(10)); //设置定时器超时时间为10ms
return IRQ_RETVAL(IRQ_HANDLED);
}
/* 应用程序对设备文件/dev/button 执行open(...)时,
* 就会调用s3c24xx_buttons_open函数
*/
static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
// s3c2410_gpio_cfgpin(S3C2410_GPF0, S3C2410_GPF0_INP);
// s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_INP);
// s3c2410_gpio_cfgpin(S3C2410_GPG3, S3C2410_GPG3_INP);
request_irq(IRQ_EINT0,button_irq,IRQT_BOTHEDGE,"S0",&pins_desc[0]);
request_irq(IRQ_EINT2,button_irq,IRQT_BOTHEDGE,"S1",&pins_desc[1]);
request_irq(IRQ_EINT11,button_irq,IRQT_BOTHEDGE,"S2",&pins_desc[2]);
return 0;
}
static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
if (count != 1)
return -EINVAL;
wait_event_interruptible(button_waitq, ev_press);
copy_to_user(buff, &key_val, sizeof(key_val));
ev_press = 0;
return 0;
}
static ssize_t s3c24xx_buttons_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0,&pins_desc[0]);
free_irq(IRQ_EINT2,&pins_desc[1]);
free_irq(IRQ_EINT11,&pins_desc[2]);
return 0;
}
static unsigned int s3c24xx_buttons_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);
if (ev_press)
mask |= POLLIN | POLLWRNORM;
return mask;
}
static int s3c24xx_buttons_fasync(int fd, struct file *file, int on)
{
printk(" to call fasync_helper\n");
return fasync_helper(fd, file, on, &buttons_async_queue);
}
/* 这个结构是字符设备驱动程序的核心
* 当应用程序操作设备文件时所调用的open、read、write等函数,
* 最终会调用这个结构中指定的对应函数
*/
static struct file_operations s3c24xx_buttons_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = s3c24xx_buttons_open,
.read = s3c24xx_buttons_read,
.release = s3c24xx_buttons_close,
.poll = s3c24xx_buttons_poll,
.fasync = s3c24xx_buttons_fasync,
};
static void timer_deal(unsigned long data)
{
struct pin_desc *pindesc = irq_pd;
unsigned int pinval;
// printk(" enter %d \n",irq);
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval)
{
// 松开
key_val = 0x80 | pindesc->pin_val;
}
else
{
// 按下
key_val = pindesc->pin_val;
}
ev_press = 1;
wake_up_interruptible(&button_waitq);
kill_fasync(&buttons_async_queue, SIGIO, POLL_IN);
}
/*
* 执行insmod命令时就会调用这个函数
*/
static int __init s3c24xx_button_init(void)
{
int ret;
init_timer(&buttons_timer);
buttons_timer.function = timer_deal;
// add_timer(&buttons_timer);
/* 注册字符设备
* 参数为主设备号、设备名字、file_operations结构;
* 这样,主设备号就和具体的file_operations结构联系起来了,
* 操作主设备为BUTTON_MAJOR的设备文件时,就会调用s3c24xx_buttons_fops中的相关成员函数
* BUTTON_MAJOR可以设为0,表示由内核自动分配主设备号,返回值为主设备号
*/
ret = register_chrdev(BUTTON_MAJOR, DEVICE_NAME, &s3c24xx_buttons_fops);
if (ret < 0) {
printk(DEVICE_NAME " can't register major number\n");
return ret;
}
// 创建设备类
buttons_class = class_create(THIS_MODULE, "buttons");
if (IS_ERR(buttons_class))
return PTR_ERR(buttons_class);
// 创建类设备节点
buttons_class_dev = class_device_create(buttons_class, NULL, MKDEV(BUTTON_MAJOR,0), NULL, "button");
if (unlikely(IS_ERR(buttons_class_dev)))
return PTR_ERR(buttons_class_dev);
printk(DEVICE_NAME " initialized\n");
return 0;
}
/*
* 执行rmmod命令时就会调用这个函数
*/
static void __exit s3c24xx_button_exit(void)
{
del_timer_sync(&buttons_timer);
printk("to unregister_chrdev \n");
unregister_chrdev(BUTTON_MAJOR, DEVICE_NAME);
printk("to class_device_unregister \n");
class_device_unregister(buttons_class_dev);
printk("to class_destroy \n");
class_destroy(buttons_class);
}
/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(s3c24xx_button_init);
module_exit(s3c24xx_button_exit);
/* 描述驱动程序的一些信息,不是必须的 */
MODULE_VERSION("0.1.0");
MODULE_DESCRIPTION("S3C2410/S3C2440 BUTTON Driver");
MODULE_LICENSE("GPL");
编译测试
查看本专栏的之一和之二。
小结
内核定时器的使用框架
static struct timer_list buttons_timer; /* 定义定时器*/
/* 定时器回调函数 */
static void timer_deal(unsigned long data)
{
/* 如果需要定时器周期性运行的话就使用 mod_timer
* 函数重新设置超时值并且启动定时器。
*/
mod_timer(&buttons_timer,jiffies+msecs_to_jiffies(10));
}
/* 初始化函数 */
void init(void){
init_timer(&buttons_timer);
buttons_timer.function = timer_deal;
timer.expires=jffies + msecs_to_jiffies(10);/* 超时时间 2 秒 */
timer.data = (unsigned long)&dev; /* 将设备结构体作为参数 */
add_timer(&timer); /* 启动定时器 */
}
/* 退出函数 */
void exit(void){
del_timer(&timer); /* 删除定时器 */
/* 或者使用 */
del_timer_sync(&timer);
}