Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This application note shows how to control the STM32H7 GPIOs from the user level using the standard Linux GPIOLIB interface.

1. Linux GPIOLIB Interface

The generic GPIO interface is controlled by the CONFIG_GPIOLIB kernel option enabled by default in the rootfs project. Most of the STM32H7 GPIO pins can be used in different multiplexed I/O roles (for instance, some GPIO pins can be also configured as an SPI interface, etc). Depending on the requirements of your application, you need to configure the pins that you want to use as GPIO for the GPIO role and other pins for an alternative I/O function.

2. Linux LED and Keys Interfaces

The device driver for the STM32H7 GPIO controller is enabled in the kernel config. The gpio-leds and gpio-keys drivers will be enabled and configured in the DTS file, in order to support the USER LED 1/2 and USER BUTTON 1/2 of the STM32H7 SOM.

Verify that the USER LED and USER BUTTON interfaces are functional via the standard gpio-leds and gpio-keys interfaces:

  1. Use the following command to turn on the USER LED 1:

    Code Block
    # echo 1 > /sys/class/leds/user-led-1/brightness
  2. Use the following command to turn off the USER LED 1:

    Code Block
    # echo 0 > /sys/class/leds/user-led-1/brightness
  3. From the Linux shell, start the evtest utility. Then press the USER BUTTON 1 and make sure the evtest has reported the events correctly:

    Code Block
    / # evtest /dev/input/event0
    Event: time 14.575120, type EV_KEY, code KEY_A, value 1
    Event: time 14.575120, type EV_SYN, code SYN_REPORT, value 0
    Event: time 14.728753, type EV_KEY, code KEY_A, value 0
    Event: time 14.728753, type EV_SYN, code SYN_REPORT, value 0

3. Linux Raw GPIO Interface

In order to test raw GPIO functions, disable definition of the USER BUTTON 1 in the kernel DTS file, then rebuild and reinstall the project:

...

  1. Export PH2 and configure it as an input:

    Code Block
    / # echo 114 > /sys/class/gpio/export
    / # echo in > /sys/class/gpio/PH2/direction
  2. Make sure the value of PH2 is 1 when the USER BUTTON 1 is untouched (due to the internal PULL-UP being enabled):

    Code Block
    / # cat /sys/class/gpio/PH2/value
    1
    / #
  3. Press and hold the USER BUTTON 1 and make sure the PH2 value has changed to 0:

    Code Block
    / # cat /sys/class/gpio/PH2/value
    0

4. Alternative Ways to Access GPIO

In Linux, you may access GPIOs using different approaches, not only the ones described in this application note above. Here are some external links that might be useful if you decide to try an alternative approach.

...