1. Purpose[edit source]
The purpose of this article is to explain how to configure the GPIO internal peripheral through the GPIOLib framework when this peripheral is assigned to Linux® OS. The configuration is performed using the device tree[1].
To better understand I/O management, it is recommended to read the I/O pins overview article [2].
This article also provides an example explaining how to add a new GPIO in the device tree.
If the peripheral is assigned to another execution context, refer to How to assign an internal peripheral to a runtime context article for guidelines on peripheral assignment and configuration.
2. DT bindings documentation[edit source]
The STM32 GPIO bindings are composed of:
- Bindings for GPIO controller [3]
- Bindings for devices using a GPIO [4]
It is recommended to read these reference documents if you have never played with GPIO DT.
3. DT configuration[edit source]
3.1. DT configuration (STM32 level)[edit source]
The GPIO controller node (gpio controller) is located in the pin controller node that is declared Inside the SoC dtsi file:
See Device tree for more explanations about device tree files split.
There is one gpio controller node per GPIO bank.
Refer to Pin controller configuration and to GPIO controller node bindings[3] for explanations on the gpio controller node.
3.2. DT configuration (board level)[edit source]
Generic guidelines for adding a GPIO to a client device can be found in the document "GPIO bindings for board" [4].
- Below an example of basic GPIO usage extracted from "GPIO bindings for board" [4]:
GPIO mappings are defined in the consumer device node, through a property named <function>-gpios, where <function> is requested by the Linux driver through gpiod_get().
foo_device { ... led-gpios = <&gpioa 15 GPIO_ACTIVE_HIGH>, /* red */ <&gpioa 16 GPIO_ACTIVE_HIGH>, /* green */ <&gpioa 17 GPIO_ACTIVE_HIGH>; /* blue */ power-gpios = <&gpiob 1 GPIO_ACTIVE_LOW>; };
Where:
- - &gpiox: phandle on gpio-controller node.
- - 15: line offset in gpio-controller.
- - GPIO_ACTIVE_HIGH: flag to be used for the GPIO. The list of all available GPIO flags can be found in Linux kernel source code[7].
3.2.1. Secure GPIO configuration[edit source]
GPIO banks may contain some specific secure settings. A peripheral set as secure MUST use secure GPIOs.
A specific binding is used in OP-TEE device tree to set the security at boot time, preventing non secure to use it, by defining the st,protreg property. It is defined at board level:
&gpiox { st,protreg = <TZPROT(1)|TZPROT(6)>; };
Where:
- - &gpiox: phandle on gpio-controller node.
- - TZPROT: Macro use to define the PIN number to be set as secure.
3.3. DT configuration examples[edit source]
3.3.1. How to add a GPIO dedicated to a device connected on the board[edit source]
The example below shows how to add a GPIOB5 (PB5 on schematics) to a foo_device.
foo_device { ... x-gpios = <&gpiob 5 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>; /* x function */ ... };
Note: GPIO_ACTIVE_HIGH means that the GPIO line is driven by the logical level 1 (while GPIO_ACTIVE_LOW means that it is driven by the logical level 0). GPIO_PULL_UP means that the internal pull-up resistor is enabled.
4. How to configure GPIOs using STM32CubeMX[edit source]
The STM32CubeMX tool can be used to configure the STM32MPU device and get the corresponding platform configuration device tree files.
The STM32CubeMX may not support all the properties described in the above DT bindings documentation paragraph. If so, the tool inserts user sections in the generated device tree. These sections can then be edited to add some properties and they are preserved from one generation to another. Refer to STM32CubeMX user manual for further information.
5. References[edit source]
Please refer to the following links for additional information:
- ↑ Device tree, Device tree mechanism
- ↑ Overview of GPIO pins, Overview of GPIO pins article
- ↑ 3.0 3.1 Kernel device tree bindings documentation - gpio.txt, GPIO device tree bindings
- ↑ 4.0 4.1 4.2 GPIO bindings for board
- ↑ stm32mp131.dtsi , STM32MP131 device tree file
- ↑ stm32mp151.dtsi , STM32MP151 device tree file
- ↑ include/dt-bindings/gpio/gpio.h
}}