gpio application

来源:互联网 发布:小米5如何备份数据 编辑:程序博客网 时间:2024/06/06 01:26

 

首先定义响应按键的IO接口与功能描述:

static struct gpio_keys_button mini6410_buttons[] = {

    {

        .gpio = S3C64XX_GPN(0),

        .code = KEY_BACK,

        .desc = "back",

    },

    {

        .gpio = S3C64XX_GPN(1),

        .code = KEY_MENU,

        .desc = "Menu",

    },

    {

        .gpio = S3C64XX_GPN(2),

        .code = KEY_HOME,

        .desc = "Home",

    },

    {

        .gpio = S3C64XX_GPN(3),

        .code = KEY_POWER,

        .desc = "Power",

    },

    {

        .gpio = S3C64XX_GPN(4),

        .code = KEY_VOLUMEUP,

        .desc = "Volume up",

    },

    {

        .gpio = S3C64XX_GPN(5),

        .code = KEY_VOLUMEDOWN,

        .desc = "Volume down",

    },

};

 

static struct gpio_keys_platform_data mini6410_button_data = {

    .buttons = mini6410_buttons,

    .nbuttons = ARRAY_SIZE(mini6410_buttons),

};

 

static struct platform_device mini6410_device_button = {

    .name = "gpio-keys",

    .id = -1,

    .dev = {

        .platform_data = &mini6410_button_data,

    }

};

 

最后将mini6410_device_button填入到板文件的初始化函数static void __init mini6410_machine_init(void),实现gpio的初始化启动过程。

 

测试:

确认gpio的事件是否创建成功,打开/dev/input文件夹,可以看到创建的对应event文件。

写个间的地测试功能,输入对应的案件在tty上显示出按键信息。

#include <fcntl.h>

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <linux/input.h>

#include <sys/mman.h>

 

int main(){

    struct input_event event;

    int keyfd = open("/dev/event0",O_RDWR);

        int ret;

        while(1){

                ret = read(keyfd,&event,sizeof(struct input_event));

                if (ret == sizeof(struct input_event)){

                        printf("type:%d code:%d \n",event.type,event.code);

                  }

        if( event.type == 1) {

            if (event.code == KEY_MENU) {

               printf("type:%d code:%d \n",event.type,event.code);

            }

            if (event.code == KEY_POWER){

               printf("type:%d code:%d \n",event.type,event.code);

            }

            if (event.code == KEY_BACK){

               printf("type:%d code:%d \n",event.type,event.code);

            }

            if (event.code == KEY_HOME){

                printf("type:%d code:%d \n",event.type,event.code);

            }

            if (event.code == KEY_VOLUMEUP){

                printf("type:%d code:%d \n",event.type,event.code);

            }

            if (event.code == KEY_VOLUMEDOWN) {

                 printf("type:%d code:%d \n",event.type,event.code);

            }

        }      

}