分享

基于linux操作系统下s5pv210板子的按键中断实验

 XeonGate 2015-10-07

首先,在做这个实验之前有三件事是要明确的:

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,
    }*/
};
复制代码

 

下面给出完整代码:

复制代码
 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     request_irq(gpio_to_irq(S5PV210_GPH2(1)), key_irq, IRQF_TRIGGER_FALLING, "key_miscdev", 0);
66     request_irq(gpio_to_irq(S5PV210_GPH2(2)), key_irq, IRQF_TRIGGER_FALLING, "key_miscdev", 0);
67     request_irq(gpio_to_irq(S5PV210_GPH2(3)), key_irq, IRQF_TRIGGER_FALLING, "key_miscdev", 0);
68 
69     return 0;
70         
71 }
72 
73 static void key_exit()
74 {
75     /*注销设备*/
76     misc_deregister(&key_miscdevice);
77 }
78 
79 MODULE_LICENSE("GPL");
80 
81 module_init(key_init);
82 module_exit(key_exit);
复制代码

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多