Controlling GPIO from Linux User Space Print



This application note explains how to drive GPIO outputs and read the state of GPIO inputs from the Linux user-space on the STM32F7.

The API that is used to control GPIO is the standard Linux GPIOLIB interface. The API is described in the Linux documentation available in the kernel tree from the following file: linux/Documentation/gpio.txt.

Alternatively, the same document can be found in the Internet, for instance here:

To enable the generic GPIO interface in the kernel, you need to enable certain build-time configuration options. Specifically, from the kernel configuration menu, go to Device Drivers, enable GPIO Support (CONFIG_GPIOLIB), and then from the GPIO Support menu enable the sysfs interface item (CONFIG_GPIO_SYSFS):

Now, most of the STM32F7 GPIO pins can be used in different multiplexed I/O roles (for instance, some GPIOs can be also configured as an SPI interface, etc). Depending on specific requirements of your applications, you need to configure those pins that you want to use as GPIO for the GPIO role and other pins as an alternative I/O function.

For the purposes of this application note, we want to configure two pins as GPIO, specifically:

  • PI11 is connected to the User Push button on the STM32F7 Discovery board;
  • PI1 is connected to the User LED (LD1) on the STM32F7 Discovery board.

In the STM32F7 kernel, the I/O functions of the STM32F7 pins are defined in the following file:

Here is the code that is responsible for configuring the above two pins for the GPIO function:

if (platform == PLATFORM_STM32_STM32F7_DISCO) {
/* PI11 = User Push Button */
gpio_dsc.port = 8; = 11;
stm32f2_gpio_config(&gpio_dsc, STM32F2_GPIO_ROLE_IN_PUP);
/* PI1 = LED LD1 */
gpio_dsc.port = 8; = 1;
stm32f2_gpio_config(&gpio_dsc, STM32F2_GPIO_ROLE_OUT);

Having configured the kernel as described above, build the Linux image and load it to the STM32F7 SOM.

On the Linux running on the STM32F7 target, each GPIO is assigned a unique integer GPIO number within the GPIO chip range of 0 to 127:

/sys/class/gpio # cd /sys/class/gpio
/sys/class/gpio # ls
export gpiochip0 unexport
/sys/class/gpio # cat gpiochip0/ngpio

To calculate the GPIO number for a specific GPIO, use the following formula:

gpio = (bank * 16) + pin

Bank numbers start with 0 (corresponding to Bank A, etc), pin numbers start with 0 (corresponding to pin 0, etc).

For the User Push Button the specific pin is DI11, which corresponds to the following GPIO number:

(8 * 16) + 11 = 139

First step is to add (export) the pin to the GPIO array and define it as an input. This is done as follows:

/sys/class/gpio # echo 139 > export
/sys/class/gpio # echo in > gpio139/direction
/sys/class/gpio #

Now, all is ready to read the value of the User Push button. There is an external pull-up resistor on the User Push button GPIO so when unpressed the GPIO reads as 0:

/sys/class/gpio # cat gpio139/value
/sys/class/gpio # cat gpio139/value
/sys/class/gpio #

Push the button and make sure the GPIO value reads as 1:

/sys/class/gpio # cat gpio139/value
/sys/class/gpio # cat gpio139/value
/sys/class/gpio #

Release the button and validate that the value has returned to 1:

/sys/class/gpio # cat gpio139/value
/sys/class/gpio #

For the User LED the specific pin is PI1, which corresponds to the following GPIO number:

(8 * 16) + 1 = 129

Export the GPIO and define it as an output:

/sys/class/gpio # echo 129 > export
/sys/class/gpio # echo out > gpio129/direction

Now run the following shell commands to turn the LED on and off at a 1Hz frequency:

/sys/class/gpio # while busybox echo hey > /dev/null
> do
> echo 1 > gpio129/value; sleep 1
> echo 0 > gpio129/value; sleep 1
> done

If you need to control GPIO from C / C++ code, it is probably a good idea to do the GPIO setup (export, define direction, etc) from a start-up script (eg. in /etc/rc). You can then use the standard POSIX APIs such as open, read, write, etc on gpioxx/value files to control your GPIO from C code.