基于linux操作系统下s5pv210板子的按键中断实验
首先,在做这个实验之前有两件事是要明确的:
1. linux下的中断实验不需要像裸机实验一样要自己写取消屏蔽,清除中断标志位的操作,系统会自动帮你完成;
2. 在每个板子配套来的内核代码大部分都是已经包含了按键中断驱动,如果想另自己写的按键中断驱动不与内核本身带的发生中断号上的冲突,应先找到内核代码下的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, }*/ };
下面给出完整代码:
1 #include <linux/module.h> 2 #include <linux/init.h> 3 #include <linux/miscdevice.h> 4 #include <linux/interrupt.h> 5 #include <linux/fs.h> 6 #include <linux/io.h> 7 #include <mach/gpio.h> 8 #include <mach/regs-gpio.h> 9 #include <linux/irq.h> 10 11 #define GPH2CON 0xe0200c40 12 13 irqreturn_t key_irq(int irq, void * dev_id) 14 { 15 //1. 检测是否发生了按键中断 16 17 18 //2. 清除已经发生的按键中断 19 20 21 //3. 打印按键值 22 printk("key down!\n"); 23 24 return 0; 25 } 26 27 int key_open (struct inode *inode, struct file *filp) 28 { 29 return 0; 30 } 31 32 void key_hw_init() 33 { 34 //unsigned int data; 35 unsigned int *gpio_config; 36 37 gpio_config = ioremap(GPH2CON,4); 38 //data = readl(gpio_config); 39 //data &= ~0b1111; 40 //data |= 0b1111; 41 writel(0x0000000f,gpio_config); 42 } 43 44 struct file_operations key_fops = 45 { 46 .open = key_open, 47 }; 48 struct miscdevice key_miscdevice = 49 { 50 /*MISC_DYNAMIC_MINOR代表动态分配次设备号,即由系统自动分配*/ 51 .minor = MISC_DYNAMIC_MINOR, 52 .name = "key_miscdev", 53 .fops = &key_fops, 54 }; 55 56 static int key_init() 57 { 58 /*注册混杂设备*/ 59 misc_register(&key_miscdevice); 60 61 /*申请中断,如果内核中已有按键中断驱动,则需要把arch/arm/mach-s5pv210/mach-mini210.c文件的gpio_buttons定义的相关按键去掉, 62 不然板子上的按键中断就已经被占用,不能注册中断*/ 63 /*注意:中断号这个参数应该用gpio_to_irq(S5PV210_GPH2(0)),假如用了IRQ_EINT16_31则按键驱动不会工作,我个人认为是内核代码写错了*/ 64 request_irq(gpio_to_irq(S5PV210_GPH2(0)), key_irq, IRQF_TRIGGER_FALLING, "key_miscdev", 0); 65 66 return 0; 67 68 } 69 70 static void key_exit() 71 { 72 /*注销设备*/ 73 misc_deregister(&key_miscdevice); 74 } 75 76 MODULE_LICENSE("GPL"); 77 78 module_init(key_init); 79 module_exit(key_exit);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。