【问题标题】:gpio-keys I have configured 2 events but I am only able to receive one of themgpio-keys 我已经配置了 2 个事件,但我只能接收其中一个
【发布时间】:2022-01-21 10:53:34
【问题描述】:

我正在使用基于 i.MX8X 的板,我需要通过输入从另一个板接收一些事件。为此,我决定使用 gpio-keys,因此我按照以下步骤操作:

  1. 配置设备树。我添加了以下节点:

     gpio-keys {
         compatible = "gpio-keys";
    
         gpio1_fpga_cpu {
             label = "BTN_TRIGGER_HAPPY1";
             gpios = <&lsio_gpio4 22 GPIO_ACTIVE_LOW>;
             linux,code = <BTN_TRIGGER_HAPPY1>;
         };
    
         gpio2_fpga_cpu {
             label = "BTN_TRIGGER_HAPPY2";
             gpios = <&lsio_gpio4 21 GPIO_ACTIVE_LOW>;
             linux,code = <BTN_TRIGGER_HAPPY2>;
         };
     };
    

执行此操作后,我可以在 /proc/bus/input/devices 中看到这样的事件:

I: Bus=0019 Vendor=0001 Product=0001 Version=0100
N: Name="gpio-keys"
P: Phys=gpio-keys/input0
S: Sysfs=/devices/platform/gpio-keys/input/input0
U: Uniq=
H: Handlers=event0
B: PROP=0
B: EV=3
B: KEY=3 0 0 0 0 0 0 0 0 0 0 0

问题是当我在文件描述符(“/dev/input/event0”)上使用“select”时,我只接收到 BTN_TRIGGER_HAPPY2 而从不接收 BTN_TRIGGER_HAPPY1。我已经验证了多次执行“cat /sys/kernel/debug/gpio”,我可以看到 GPIO 是如何切换其值的,所以我应该从两者接收事件,不是吗?

gpiochip4: GPIOs 128-159, parent: platform/5d0c0000.gpio, 5d0c0000.gpio:
gpio-129 ( |enable ) out hi ACTIVE LOW
gpio-149 ( |BTN_TRIGGER_HAPPY2 ) in hi IRQ ACTIVE LOW
gpio-150 ( |BTN_TRIGGER_HAPPY1 ) in lo IRQ ACTIVE LOW
gpio-155 ( |enable ) out hi ACTIVE LOW

root@sce-dropid1:~# cat /sys/kernel/debug/gpio
gpiochip4: GPIOs 128-159, parent: platform/5d0c0000.gpio, 5d0c0000.gpio:
gpio-129 ( |enable ) out hi ACTIVE LOW
gpio-149 ( |BTN_TRIGGER_HAPPY2 ) in lo IRQ ACTIVE LOW
gpio-150 ( |BTN_TRIGGER_HAPPY1 ) in hi IRQ ACTIVE LOW
gpio-155 ( |enable ) out hi ACTIVE LOW

这是我用来读取事件的函数(它们不可能同时发生):

static int handle_gpio_keys_event()
{
    int ret = MAIN_ERR_SUCCESS;
    int fd = -1;
    struct input_event ev; /** One per possible allowed event */
    int rd;
    fd_set rdfs;
    int i = 0;
    if ((fd = open(DP_EVENT_INPUT, O_RDONLY)) < 0)
    {
        if (errno == EACCES && getuid() != 0)
            fprintf(stderr, "You do not have access to %s. Try "
                    "running as root instead.\n",
                    DP_EVENT_INPUT);
        ret = MAIN_ERR_EVENT_FILE_PERMISSION;
    }
    else
    {
        FD_ZERO(&rdfs);
        FD_SET(fd, &rdfs);
        // ret = select(fd + 1, &rdfs, NULL, NULL, &tiomeout);
        ret = select(fd + 1, &rdfs, NULL, NULL, NULL);
        if (ret == -1)
        {
            ret = MAIN_ERR_UNEXPECTED_EVENT_READ;
            printf("Entered: %s (%d)- ret %d\n", __func__, __LINE__, ret);
        }
        else if (ret)
        {
            rd = read(fd, (void *)&ev, sizeof(ev));

            if (rd / sizeof(struct input_event) != 1)
            {/** More than one event was received and it is not valid for this application */
                printf("Entered: %s (%d)- number of events %ld\n", __func__, __LINE__, rd / sizeof(struct input_event));
            }
            else
            {
                printf("Entered: %s (%d)- number of events %ld\n", __func__, __LINE__, rd / sizeof(struct input_event));
                if(rd == (int) sizeof(struct input_event))
                {
                    printf("Entered: %s (%d)- ev.type %d\n", __func__, __LINE__, ev.type);
                    if (ev.type == EV_KEY)
                    {
                        printf("Entered: %s (%d)- ev.type %d\n", __func__, __LINE__, ev.code);
                        if (ev.code == BTN_TRIGGER_HAPPY1)
                        {
                            ev.code = BTN_TRIGGER_HAPPY1;
                            printf("%s(%d) BTN_TRIGGER_HAPPY1 received\n", __func__, __LINE__);
                            ret = 0; // SUCCESS
                        }
                        else if (ev.code == BTN_TRIGGER_HAPPY2)
                        {
                            ev.code = BTN_TRIGGER_HAPPY2;
                            printf("%s(%d) BTN_TRIGGER_HAPPY2 received\n", __func__, __LINE__);
                            ret = 0; // SUCCESS
                        }
                        else
                        {
                            printf("Entered: %s (%d)- UNEXPECTED EVENT: %d \n", __func__, __LINE__, ev.code);
                            // ret = -1;
                            ret = 0;
                        }
                            
                    }
                    else
                    {/** ERROR */
                        printf("Entered: %s (%d)- return -2, and EV TYPE: %d\n", __func__, __LINE__, ev.type);
                        ret = -2;
                    }
                }
                else if (rd > 0 && rd != (int) sizeof(struct input_event))
                {
                    ret = MAIN_ERR_UNEXPECTED_EVENT_RECEPTION;
                    printf("Entered: %s (%d)- ret %d\n", __func__, __LINE__, ret);
                }
                else
                {
                    ret = MAIN_ERR_UNEXPECTED_EVENT_READ;
                    printf("Entered: %s (%d)- ret %d\n", __func__, __LINE__, ret);
                }
            }
        }
        else
        {
            ret = MAIN_ERR_EVENT_RECEPTION_TIMEOUT;
            printf("%s(%d) - TIMEOUT \n", __func__, __LINE__);
        }
    }
    printf("%s LINE: %d ret: %d\n", __func__, __LINE__, ret);
    return ret;
}

知道我缺少什么吗? 提前致谢。

福尔戈。

【问题讨论】:

  • 我遗漏了一些东西,因为 evtest 工作正常......

标签: c linux linux-device-driver embedded-linux gpio


【解决方案1】:

好的...我没有阅读所有事件,只是阅读了一个...抱歉浪费您的时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 2014-03-12
    • 2016-10-20
    • 2023-04-05
    • 2016-03-20
    • 2011-07-08
    相关资源
    最近更新 更多