首先,在做这个实验之前有三件事是要明确的:
1. linux下的中断实验不需要像裸机实验一样要自己写取消屏蔽,清除中断标志位的操作,系统会自动帮你完成;
2.中断号的申请我使用的是gpio_to_irq()这个宏,它会帮我们自动分配中断号,返回值为中断号;
3. 在每个板子配套来的内核代码大部分都是已经包含了按键中断驱动,如果想另自己写的按键中断驱动不与内核本身带的发生中断号上的冲突,应先找到内核代码下的arch/arm/mach-s5pv210/mach-mini210.c中的gpio_bottons中的对按键初始化的代码注释掉(其他板子也类似),如下代码所示:
static struct gpio_keys_button gpio_buttons[] = {
/*{
.gpio = S5PV210_GPH2(0),
.code = 158,
.desc = "BACK",
.active_low = 1,
.wakeup = 0,
}, {
.gpio = S5PV210_GPH2(1),
.code = 102,
.desc = "HOME",
.active_low = 1,
.wakeup = 1,
}, {
.gpio = S5PV210_GPH2(2),
.code = 139,
.desc = "MENU",
.active_low = 1,
.wakeup = 0,
}, {
.gpio = S5PV210_GPH2(3),
.code = 232,
.desc = "DPAD_CENTER",
.active_low = 1,
.wakeup = 0,
}, {
.gpio = S5PV210_GPH3(0),
.code = 105,
.desc = "DPAD_LEFT",
.active_low = 1,
.wakeup = 0,
}, {
.gpio = S5PV210_GPH3(1),
.code = 108,
.desc = "DPAD_DOWN",
.active_low = 1,
.wakeup = 0,
}, {
.gpio = S5PV210_GPH3(2),
.code = 103,
.desc = "DPAD_UP",
.active_low = 1,
.wakeup = 0,
}, {
.gpio = S5PV210_GPH3(3),
.code = 106,
.desc = "DPAD_RIGHT",
.active_low = 1,
.wakeup = 0,
}, {
.gpio = S5PV210_GPH1(7),
.code = 102,
.desc = "HOME",
.active_low = 1,
.wakeup = 1,
}*/
};
#include <linux/module.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <mach/gpio.h>
#include <mach/regs-gpio.h>
#include <linux/irq.h>
#define GPH2CON 0xe0200c40
irqreturn_t key_irq(int irq, void * dev_id)
{
//1. 检测是否发生了按键中断
//2. 清除已经发生的按键中断
//3. 打印按键值
printk("key down!\n");
return 0;
}
int key_open (struct inode *inode, struct file *filp)
{
return 0;
}
void key_hw_init()
{
//unsigned int data;
unsigned int *gpio_config;
gpio_config = ioremap(GPH2CON,4);
//data = readl(gpio_config);
//data &= ~0b1111;
//data |= 0b1111;
writel(0x0000000f,gpio_config);
}
struct file_operations key_fops =
{
.open = key_open,
};
struct miscdevice key_miscdevice =
{
/*MISC_DYNAMIC_MINOR代表动态分配次设备号,即由系统自动分配*/
.minor = MISC_DYNAMIC_MINOR,
.name = "key_miscdev",
.fops = &key_fops,
};
static int key_init()
{
/*注册混杂设备*/
misc_register(&key_miscdevice);
/*申请中断,如果内核中已有按键中断驱动,则需要把arch/arm/mach-s5pv210/mach-mini210.c文件的gpio_buttons定义的相关按键去掉,
不然板子上的按键中断就已经被占用,不能注册中断*/
/*注意:中断号这个参数应该用gpio_to_irq(S5PV210_GPH2(0)),假如用了IRQ_EINT16_31则按键驱动不会工作*/
request_irq(gpio_to_irq(S5PV210_GPH2(0)), key_irq, IRQF_TRIGGER_FALLING, "key_miscdev", 0);
request_irq(gpio_to_irq(S5PV210_GPH2(1)), key_irq, IRQF_TRIGGER_FALLING, "key_miscdev", 0);
request_irq(gpio_to_irq(S5PV210_GPH2(2)), key_irq, IRQF_TRIGGER_FALLING, "key_miscdev", 0);
request_irq(gpio_to_irq(S5PV210_GPH2(3)), key_irq, IRQF_TRIGGER_FALLING, "key_miscdev", 0);
return 0;
}
static void key_exit()
{
/*注销设备*/
misc_deregister(&key_miscdevice);
}
MODULE_LICENSE("GPL");
module_init(key_init);
module_exit(key_exit);