2. Linux内核中使用定时器的步骤
static struct timer_list xxx_timer;
init_timer(&xxx_timer);
buttons_timer.function = xxx_function;
buttons_timer.expires = 0;
add_timer(&xxx_timer);
mod_timer(&xxx_timer, jiffies+HZ/100);
3. 驱动代码(Seven_drv.c)
#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 *seven_drv_class;
static struct class_device *seven_drv_class_dev;
static struct fasync_struct *buttons_async;
static struct timer_list buttons_timer;
typedef struct
{
unsigned int pin;
unsigned int key_val;
}s3c2440_buttons_desc;
s3c2440_buttons_desc pins_desc[]={
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};
static struct pins_desc *irq_pd;
unsigned int key_val;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;
static void buttons_timer_function (unsigned long data)
{
unsigned pin_val;
s3c2440_buttons_desc *pindesc = irq_pd;
if(!pindesc)
return;
pin_val = s3c2410_gpio_getpin(pindesc->pin);
if(pin_val)
key_val = pindesc->key_val | 0x80;
else
key_val = pindesc->key_val;
ev_press = 1;
wake_up_interruptible(&button_waitq);
kill_fasync(&buttons_async, SIGIO, POLL_IN);
}
irqreturn_t buttons_irq(int irq, void *dev_id)
{
irq_pd = (struct pins_desc *)dev_id;
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_HANDLED;
}
int seven_drv_open(struct inode *inode, struct file *file)
{
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;
}
ssize_t seven_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 0;
}
int seven_drv_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]);
free_irq(IRQ_EINT19, &pins_desc[3]);
return 0;
}
unsigned int seven_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;
}
int seven_drv_fsync(int fd, struct file *filp, int on)
{
return fasync_helper(fd, filp, on, &buttons_async);
}
static struct file_operations seven_drv_fops = {
.owner = THIS_MODULE,
.open = seven_drv_open,
.read = seven_drv_read,
.release = seven_drv_close,
.poll = seven_drv_poll,
.fasync = seven_drv_fsync,
};
static int major;
static int __init seven_drv_init(void)
{
init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;
buttons_timer.expires = 0;
add_timer(&buttons_timer);
major = register_chrdev(0, "seven_drv", &seven_drv_fops);
seven_drv_class = class_create(THIS_MODULE, "seven_drv");
seven_drv_class_dev = class_device_create(seven_drv_class, NULL, MKDEV(major, 0), NULL, "buttons_delay");
return 0;
}
static void __exit seven_drv_exit(void)
{
unregister_chrdev(major, "second_drv");
class_device_unregister(seven_drv_class_dev);
class_destroy(seven_drv_class);
return 0;
}
module_init(seven_drv_init);
module_exit(seven_drv_exit);
MODULE_LICENSE("GPL");
4. 测试代码
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#define DEV_DEVICE "/dev/buttons_delay"
int main(int argc, char *argv[])
{
int fd, ret;
unsigned char key_val;
fd = open(DEV_DEVICE, O_RDWR);
if(fd<0)
{
printf("can't open %s\n", DEV_DEVICE);
return -1;
}
while(1)
{
ret = read(fd, &key_val, 1);
printf("key_val=0x%x, ret=%d\n", key_val, ret);
}
close(fd);
return 0;
}