Getting started with GPIO

This article explains what GPIO is and how to use it through examples.

1. What is a general purpose input output (GPIO)

GPIO stands for general purpose input/output. It is a type of pin found on an integrated circuit that does not have a specific function. While most pins have a dedicated purpose, such as sending a signal to a certain component, the function of a GPIO pin is customizable and can be controlled by the software.

  • Pin Mode : Each port bit of the general-purpose I/O (GPIO) ports can be individually configured by software in several modes:
    • input or output
    • analog
    • alternate function (AF).
  • Pin characteristics :
    • Input : no pull-up and no pull-down or pull-up or pull-down
    • Output : push-pull or open-drain with pull-up or pull-down capability
    • Alternate function : push-pull or open-drain with pull-up or pull-down capability.

GPIO Functional description graph.png

1.1. GPIO (pin) output-speed configuration

  • Change the rising and falling edge when the pin state changes from high to low or low to high.
  • A higher GPIO speed increases the EMI noise from STM32 and increases the STM32 consumption.
  • It is good to adapt the GPIO speed to the peripheral speed. For example, low speed is optimal for toggling GPIO at 1 Hz, while using SPI at 45 MHz requires very high speed setting.

GPIO Edge.png


2. Configure GPIO for LED toggling

2.1. Objective

Learn how to Toggle a pin on STM32L476 Nucleo board using Hardware Abstraction Layer (HAL) library and learn how to setup the pin and GPIO port in STM32CubeIDE.

2.2. Create the project in STM32CubeIDE

'File > New > STM32 Project in main panel.
create STM32CubeIDE project.png

This example uses the NUCLEO-L476RG board.
Start by selecting the NUCLEO-L476RG board using the Board Selector as shown in the figure below:
Select NUCLEO-L476RG board.png

In case you haven't downloaded the STM32L476 Cube library, it will be downloaded automatically. This however may take some time.

Save the project.
Setup menu GPIO.png

  • You will then get a popup asking if you want to initialize peripherals to their default configuration.
  • No need to configure any peripheral as only the core/Flash/SRAMs are used (default peripherals).
Info white.png Information
Existing examples for products like the STM32G0 can be found using the following path: c:\Users\YourUserName\STM32Cube\Repository\STM32Cube_FW_G0_V1.3.0\Projects\NUCLEO-G071RB\Examples\GPIO\GPIO_IOToggle\GPIO_IOToggle.ioc

You can open the GPIO_IOToggle.ioc file with STM32CubeIDE

.

Warning white.png Warning
There are no ready-to-use examples for STM32L476.

2.3. Configure GPIO

  • If you want to start the project with a board, the LED pin is already selected (PA5 on NucleoL476RG. For other boards refer to the user manual).

LedPinGPIO.png LED UM GPIO.png

  • Yellow pins are related to the power supply
  • Unused pins are marked as Grey
  • Select the push-pull mode
  • No pull-up and pull-down
  • Output speed set to very high is important for faster peripherals such as SPI or USART.

Setup GPIO.png

2.4. Generate project and edit main.c

The easiest way to generate the code is to save your current project : Ctrl + S
The code is generated so you can see it in the left side of the screen in the project explorer
File tree GPIO.png
Now, open the main.c file which is the main source file for this application

  • The LED toggling is done with a HAL function inside main.c
 /* USER CODE BEGIN 3 */
  /* Infinite loop */
  {
    HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);
    HAL_Delay(500);
  }
  /* USER CODE END 3 */
Info white.png Information
Make sure to keep User Code when re-generating !

Code genreation panel.png

2.5. Compile and flash

  • Click on Build button Built.png
  • Click on Debug button (to run step by step) Debug.png
  • Or on Run button (to execute) Run.png

Every 500 ms the green LED state changes.
ST10543 NUCLEO L476RG top D2Blink.png

Warning white.png Warning
All GPIOs are able to drive 5 V and 3.3 V in input mode, but they are only able to generate 3.3V in output push-pull mode