nuc972-中断按键

添加按键中断

当前设备的gpio都是通过qgpio驱动控制的,只能使用轮询的方式检测,比较耗费CPU。此文档指导优化按键,使用中断的方式进行检测

注册设备

  1. 进入内核中芯片对应的目录: 02.linux内核3.10.x/arch/arm/mach-nuc970

  2. 修改dve.c,注册按键,按键为 NUC970_PI0~NUC970_PI11 共12个,添加如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    static struct gpio_keys_button n30_buttons[] = {
    {
    .gpio = NUC970_PI0,
    .code = KEY_A,
    .desc = "KEY_A",
    .active_low = 1,
    },
    {
    .gpio = NUC970_PI1,
    .code = KEY_B,
    .desc = "KEY_B",
    .active_low = 1,
    },
    {
    .gpio = NUC970_PI2,
    .code = KEY_L,
    .desc = "KEY_L",
    .active_low = 1,
    },
    {
    .gpio = NUC970_PI3,
    .code = KEY_C,
    .desc = "KEY_C",
    .active_low = 1,
    },
    {
    .gpio = NUC970_PI4,
    .code = KEY_D,
    .desc = "KEY_D",
    .active_low = 1,
    },
    {
    .gpio = NUC970_PI5,
    .code = KEY_E,
    .desc = "KEY_E",
    .active_low = 1,
    },
    {
    .gpio = NUC970_PI6,
    .code = KEY_F,
    .desc = "KEY_F",
    .active_low = 1,
    },
    {
    .gpio = NUC970_PI7,
    .code = KEY_G,
    .desc = "KEY_G",
    .active_low = 1,
    },
    {
    .gpio = NUC970_PI8,
    .code = KEY_H,
    .desc = "KEY_H",
    .active_low = 1,
    },
    {
    .gpio = NUC970_PI9,
    .code = KEY_I,
    .desc = "KEY_I",
    .active_low = 1,
    },
    {
    .gpio = NUC970_PI10,
    .code = KEY_J,
    .desc = "KEY_J",
    .active_low = 1,
    },
    {
    .gpio = NUC970_PI11,
    .code = KEY_K,
    .desc = "KEY_K",
    .active_low = 1,
    },
    };

    static struct gpio_keys_platform_data n30_button_data = {
    .buttons = n30_buttons,
    .nbuttons = ARRAY_SIZE(n30_buttons),
    };

    static struct platform_device n30_button_device = {
    .name = "gpio-keys",
    .id = -1,
    .dev = {
    .platform_data = &n30_button_data,
    }
    };
  3. 初始化设备,在nuc970_public_dev[]数组中添加我们的按键设备:

    1
    2
    3
    4
    5
    6
    7
    8
    static struct platform_device *nuc970_public_dev[] __initdata = {
    &nuc970_serial_device0,

    //我们注册的设备
    &n30_button_device,

    // 后面的设备省略
    };
  4. 添加头文件:

    1
    #include <linux/gpio_keys.h>

打开驱动

使用make menuconfig命令配置内核,打开GPIO Buttons配置项:

1
2
3
4
Device Drivers  --->
Input device support --->
[*] Keyboards --->
<*> GPIO Buttons

重编内核

使用make uImage编译内核

1
2
3
4
5
6
7
8
Image Name:   Linux-3.10.101
Created: Fri Mar 24 16:07:17 2023
Image Type: ARM Linux Kernel Image (uncompressed)
Data Size: 2881112 Bytes = 2813.59 kB = 2.75 MB
Load Address: 00008000
Entry Point: 00008000
Image arch/arm/boot/uImage is ready
cp arch/arm/boot/uImage ../image/970uimage

编译成功后得到970uimage

中断使用

NUC970_PI0~NUC970_PI11 12个按键的中断通过 /dev/input/event1 来获取,示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
char devname[] = "/dev/input/event1";
char devname_buf[64];
int device;
struct input_event ev;
int read_size;

device = open(devname, O_RDONLY);

while(1)
{
read_size = read(device,&ev, sizeof(ev));
if (read_size == sizeof(ev)) {
printf("Key: %d State: %d\n",ev.code,ev.value);
}
else{
printf("read error\n");
}
}
close(device);

return 0;
}


nuc972-中断按键
https://carl-5535.github.io/2023/03/24/工作总结/nuc972-中断按键/
作者
Carl Chen
发布于
2023年3月24日
许可协议