个人觉得中断的驱动还是蛮简单的,因为许多函数系统已经给你封装好了,比如中断的注册、获取按键值等等,但是如果想要彻底了解函数的工作过程还是很困难的。
以下是代码
驱动程序irq_botton_drive.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/gpio.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/sched.h>
//#include <asm/arch/regs-gpio.h>
//#include <asm/hardware.h>
static struct class *thirddrv_class;
static struct class_device*thirddrv_class_dev;
static unsigned char keyval;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/*
按键值按下时0x01,0x02,0x03,0x04,0x05,0x06
松开时 0x81,0x82,0x83,0x84,0x85,0x86
*/
struct pin_desc pins_desc[6] = {
{S3C64XX_GPN(0), 0x01},
{S3C64XX_GPN(1), 0x02},
{S3C64XX_GPN(2), 0x03},
{S3C64XX_GPN(3), 0x04},
{S3C64XX_GPN(4), 0x05},
{S3C64XX_GPN(5), 0x06},
};
static irqreturn_t botton_irq(int irq, void *dev_id)
{
struct pin_desc * pindes = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = gpio_get_value(pindes->pin);
if (pinval)
{
/* 松开的 */
keyval = 0x80 | pindes->key_val;
}
else
{
/* 按下的 */
keyval = pindes->key_val;
}
//printk(KERN_ERR "irq = %d\n", irq);
ev_press = 1;
wake_up_interruptible(&button_waitq);
return IRQ_HANDLED;
}
static int third_drv_open(struct inode *inode, struct file *file)
{
//printk("third_drv_open\n");
//request_irq(unsigned int irq,irq_handler_t handler,unsigned long flags,const char * name,void * dev)
request_irq(IRQ_EINT(0), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s2", &pins_desc[0]);
request_irq(IRQ_EINT(1), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s3", &pins_desc[1]);
request_irq(IRQ_EINT(2), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s4", &pins_desc[2]);
request_irq(IRQ_EINT(3), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s5", &pins_desc[3]);
request_irq(IRQ_EINT(4), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s6", &pins_desc[4]);
request_irq(IRQ_EINT(5), botton_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s7", &pins_desc[5]);
return 0;
}
static ssize_t third_drv_read(struct file *file, const 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, &keyval, 1);
ev_press = 0;
return 1;
}
int third_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT(0), &pins_desc[0]);
free_irq(IRQ_EINT(1), &pins_desc[1]);
free_irq(IRQ_EINT(2), &pins_desc[2]);
free_irq(IRQ_EINT(3), &pins_desc[3]);
free_irq(IRQ_EINT(4), &pins_desc[4]);
free_irq(IRQ_EINT(5), &pins_desc[5]);
return 0;
}
static struct file_operations third_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = third_drv_open,
.read=third_drv_read,
.release = third_drv_close,
};
int major;
static int third_drv_init(void)
{
//printk(KERN_ERR "third_drv_init\n");
major = register_chrdev(0, "thirdt_drv", &third_drv_fops); // 注册, 告诉内核
thirddrv_class = class_create(THIS_MODULE, "thirddrv");
thirddrv_class_dev = device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "bottons"); /* /dev/bottons */
return 0;
}
static void third_drv_exit(void)
{
//printk(KERN_ERR "third_drv_exit\n");
unregister_chrdev(major, "thirdt_drv"); // 卸载
device_unregister(thirddrv_class_dev);
class_destroy(thirddrv_class);
}
module_init(third_drv_init);
module_exit(third_drv_exit);
MODULE_LICENSE("GPL");
测试程序 irq_botton_test.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* irq_botton test
*
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
fd = open("/dev/bottons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
while (1)
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
return 0;
}
测试