1 定时器
(1)全局变量
jiffies:记录了系统启动以来,经过了多少tick,初始值为0。一个tick代表多长时间,在内核的CONFIG_HZ中定义。比如CONFIG_HZ=200,则一个jiffies对应5ms时间。
HZ:系统定时器每秒产生中断的频率,/arch/arm/include/asm/param.h中定义。
(2)函数
//include/linux/timer.h
struct timer_list {
struct list_head entry;
unsigned long expires; //定时器的定时值
void (*function)(unsigned long);//超时处理函数
unsigned long data;//传递给function的参数
struct tvec_base *base;
};
void init_timer( struct timer_list *timer );//初始化
add_timer(struct timer_list*) //添加定时器
mod_timer(struct timer_list *, unsigned long jiffier_timerout) //新的定时值
del_timer(struct timer_list*) //删除定时器
2 程序
驱动程序,在中断里,修改定时器,在定时器处理函数中,唤醒队列。
#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 <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/gpio.h>
#include <linux/device.h>
static struct class *buttons_class;
static struct class_device *buttons_class_dev;
static struct fasync_struct *button_async;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,forth_drv_read将它清0 */
static volatile int ev_press = 0;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
static struct timer_list buttons_timer;
static struct pin_desc *irq_dev_id;
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;
/*
* K1,K2,K3,K4对应GPG0,GPG3,GPG5,GPG6
*/
struct pin_desc pins_desc[4] = {
{S3C2410_GPG(0), 0x01},
{S3C2410_GPG(3), 0x02},
{S3C2410_GPG(5), 0x03},
{S3C2410_GPG(6), 0x04},
};
/*
* 确定按键值
*/
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
irq_dev_id = (struct pin_desc *)dev_id;
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int buttons_drv_open(struct inode *inode, struct file *file)
{
/* GPG0,GPG3,GPG5,GPG6为中断引脚: EINT8,EINT11,EINT13,EINT14 */
request_irq(IRQ_EINT8, buttons_irq, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "K1", &pins_desc[0]);
request_irq(IRQ_EINT11, buttons_irq, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "K2", &pins_desc[1]);
request_irq(IRQ_EINT13, buttons_irq, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "K3", &pins_desc[2]);
request_irq(IRQ_EINT14, buttons_irq, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "K4", &pins_desc[3]);
return 0;
}
ssize_t buttons_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if (size != 1)
return -EINVAL;
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return 1;
}
int buttons_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT8, &pins_desc[0]);
free_irq(IRQ_EINT11, &pins_desc[1]);
free_irq(IRQ_EINT13, &pins_desc[2]);
free_irq(IRQ_EINT14, &pins_desc[3]);
return 0;
}
static unsigned buttons_drv_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); // 不会立即休眠
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int buttons_fasync(int fd, struct file *file, int on)
{
return fasync_helper(fd, file, on, &button_async);
}
static struct file_operations buttons_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = buttons_drv_open,
.read = buttons_drv_read,
.release = buttons_drv_close,
.poll = buttons_drv_poll,
.fasync = buttons_fasync,
};
static void buttons_timer_fun(long data)
{
unsigned int pinval;
if (!irq_dev_id)
{
return;
}
pinval = s3c2410_gpio_getpin(irq_dev_id->pin);
if (pinval)
{
/* 松开 */
key_val = 0x80 | irq_dev_id->key_val;
}
else
{
/* 按下 */
key_val = irq_dev_id->key_val;
}
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
kill_fasync(&button_async, SIGIO, POLL_IN);
}
int major;
static int buttons_drv_init(void)
{
init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_fun;
add_timer(&buttons_timer);
major = register_chrdev(0, "forth_drv", &buttons_drv_fops);
buttons_class = class_create(THIS_MODULE, "buttons_drv");
buttons_class_dev = device_create(buttons_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */
return 0;
}
static void buttons_drv_exit(void)
{
del_timer(&buttons_timer);
unregister_chrdev(major, "buttons_drv");
device_unregister(buttons_class_dev);
class_destroy(buttons_class);
return 0;
}
module_init(buttons_drv_init);
module_exit(buttons_drv_exit);
MODULE_LICENSE("GPL");