DESK-MX6-L-AN-0010: User interaction with leds and pushbuttons

From DAVE Developer's Wiki
Jump to: navigation, search
Info Box
200px-Emblem-important.svg.png

This application note has been validated using the kit version in the History table.

History
Issue Date Notes
2024/01/22 DESK-MX6L-L 4.0.1


Introduction[edit | edit source]

Using leds and pushbuttons allows to have a simple user interaction with the userspace application.

It is possible to add a simple PCB adapter to the AXEL Lite Evaluation Kit WIDE connector.

This Application Note (AN) shows how to use it on a system running a DESK-MX6-L-based Yocto Linux distribution in Linux userspace.

Hardware adapter[edit | edit source]

A simple hardware PCB has been designed with 4 LEDS and 4 KEYs (pushbutton) useful for adding an easy visual and input interface to the AXEL Lite Evaluation Kit.

4 leds - 4 keys SBCX Add-on

The schematics for this adapter can be downloaded from our cloud server:

Linux device tree[edit | edit source]

The device tree has to be properly modified for configuring the adapter pins with the related pads pinmuxing.

gpio-leds[edit | edit source]

In the imx6dl-sbcx-cb0012.dts device tree, the gpio-leds driver can be used for configuring the leds interface in /sys/class/leds sysfs filesystem:

        leds {
                compatible = "gpio-leds";
                pinctrl-names = "default";
                pinctrl-0 = <&pinctrl_sbcx_leds>;

                led0 {
                        label = "led0";
                        gpios = <&gpio3 21 GPIO_ACTIVE_HIGH>;
                        default-state = "off";
                };

                led1 {
                        label = "led1";
                        gpios = <&gpio3 22 GPIO_ACTIVE_HIGH>;
                        default-state = "off";
                };

                led2 {
                        label = "led2";
                        gpios = <&gpio3 28 GPIO_ACTIVE_HIGH>;
                        default-state = "off";
                };

                led3 {
                        label = "led3";
                        gpios = <&gpio3 20 GPIO_ACTIVE_HIGH>;
                        default-state = "off";
                };

        };
};

The related pinctrl has to be added for configuring the pads:

        led {
                pinctrl_leds: ledgrp-1 {
                        fsl,pins = <
                                MX6QDL_PAD_EIM_D21__GPIO3_IO21  0x1b0b1         /* LED0 - gpio85        */
                                MX6QDL_PAD_EIM_D22__GPIO3_IO22  0x1b0b1         /* LED1 - gpio86        */
                                MX6QDL_PAD_EIM_D28__GPIO3_IO28  0x1b0b1         /* LED2 - gpio92        */
                                MX6QDL_PAD_EIM_D20__GPIO3_IO20  0x1b0b1         /* LED3 - gpio84        */
                        >;
                };
        };

gpio-keys[edit | edit source]

In the imx6dl-sbcx-cb0012.dts device tree, the gpio-keys driver can be used for adding a new input interface. The related entry in /dev/input/event1 will be populated by the kernel driver:

        gpiopushbuttons {
                compatible = "gpio-keys";
                pinctrl-names = "default";

                key0 {
                        label = "GPIO F1";
                        gpios = <&gpio3 26 GPIO_ACTIVE_LOW>;
                        linux,code = <KEY_F1>;
                };

                key1 {
                        label = "GPIO F2";
                        gpios = <&gpio3 27 GPIO_ACTIVE_LOW>;
                        linux,code = <KEY_F2>;
                };

                key2 {
                        label = "GPIO F3";
                        gpios = <&gpio7 12 GPIO_ACTIVE_LOW>;
                        linux,code = <KEY_F3>;
                };

                key3 {
                        label = "GPIO F4";
                        gpios = <&gpio1 6 GPIO_ACTIVE_LOW>;
                        linux,code = <KEY_F4>;
                };
        };

The related pinctrl has to be added for configuring the pads:

        pushbutton {
                pinctrl_gpio_push_button: gpiopushbuttongrp-1 {
                        fsl,pins = <
                                MX6QDL_PAD_EIM_D26__GPIO3_IO26  0x1b0b0         /* KEY0 - gpio 3 26     */
                                MX6QDL_PAD_EIM_D27__GPIO3_IO27  0x1b0b0         /* KEY1 - gpio 3 27     */
                                MX6QDL_PAD_GPIO_17__GPIO7_IO12  0x1b0b0         /* KEY2 - gpio 7 12     */
                                MX6QDL_PAD_GPIO_6__GPIO1_IO06   0x1b0b0         /* KEY3 - gpio 1 6      */
                        >;
                };
        };

Userspace commands[edit | edit source]

The configured leds and keys can be checked using the /sys/kernel/debug/gpio interface:

root@desk-mx6:~# cat /sys/kernel/debug/gpio | grep "led"
 gpio-84  (                    |led3                ) out lo
 gpio-85  (                    |led0                ) out lo
 gpio-86  (                    |led1                ) out lo
 gpio-92  (                    |led2                ) out lo
root@desk-mx6:~# cat /sys/kernel/debug/gpio | grep "GPIO F"
 gpio-6   (                    |GPIO F4             ) in  hi IRQ ACTIVE LOW
 gpio-90  (                    |GPIO F1             ) in  hi IRQ ACTIVE LOW
 gpio-91  (                    |GPIO F2             ) in  hi IRQ ACTIVE LOW
 gpio-204 (                    |GPIO F3             ) in  hi IRQ ACTIVE LOW
root@desk-mx6:~#

The sysfs /sys/class/leds/led[x] entries can be listed:

root@desk-mx6:~# ls -la /sys/class/leds/led*
lrwxrwxrwx 1 root root 0 Jan 22 08:29 /sys/class/leds/led0 -> ../../devices/platform/leds/leds/led0
lrwxrwxrwx 1 root root 0 Jan 22 08:29 /sys/class/leds/led1 -> ../../devices/platform/leds/leds/led1
lrwxrwxrwx 1 root root 0 Jan 22 08:29 /sys/class/leds/led2 -> ../../devices/platform/leds/leds/led2
lrwxrwxrwx 1 root root 0 Jan 22 08:29 /sys/class/leds/led3 -> ../../devices/platform/leds/leds/led3
root@desk-mx6:~#

Leds ON/OFF[edit | edit source]

A userspace command can be issued for, using led1 as an example, switching the led ON or OFF:

root@desk-mx6:~# echo 1 > /sys/class/leds/led1/brightness
root@desk-mx6:~# echo 0 > /sys/class/leds/led1/brightness

Pushbutton input events[edit | edit source]

The userspace utility evtest can be used for detecting the user interaction:

root@desk-mx6:~# evtest /dev/input/event1
Input driver version is 1.0.1
Input device ID: bus 0x19 vendor 0x1 product 0x1 version 0x100
Input device name: "gpio-push-button"
Supported events:
  Event type 0 (EV_SYN)
  Event type 1 (EV_KEY)
    Event code 59 (KEY_F1)
    Event code 60 (KEY_F2)
    Event code 61 (KEY_F3)
    Event code 62 (KEY_F4)
Properties:
Testing ... (interrupt to exit)
Event: time 1705922290.536050, type 1 (EV_KEY), code 59 (KEY_F1), value 1
Event: time 1705922290.536050, -------------- SYN_REPORT ------------
Event: time 1705922290.733822, type 1 (EV_KEY), code 59 (KEY_F1), value 0
Event: time 1705922290.733822, -------------- SYN_REPORT ------------
Event: time 1705922292.632106, type 1 (EV_KEY), code 60 (KEY_F2), value 1
Event: time 1705922292.632106, -------------- SYN_REPORT ------------
Event: time 1705922292.868167, type 1 (EV_KEY), code 60 (KEY_F2), value 0
Event: time 1705922292.868167, -------------- SYN_REPORT ------------
Event: time 1705922293.389389, type 1 (EV_KEY), code 61 (KEY_F3), value 1
Event: time 1705922293.389389, -------------- SYN_REPORT ------------
Event: time 1705922293.581487, type 1 (EV_KEY), code 61 (KEY_F3), value 0
Event: time 1705922293.581487, -------------- SYN_REPORT ------------
Event: time 1705922293.995503, type 1 (EV_KEY), code 62 (KEY_F4), value 1
Event: time 1705922293.995503, -------------- SYN_REPORT ------------
Event: time 1705922294.153806, type 1 (EV_KEY), code 62 (KEY_F4), value 0
Event: time 1705922294.153806, -------------- SYN_REPORT ------------