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 with the peripheral speed. For example toggling GPIO on 1 Hz is low optimal settings, but with SPI on 45 MHz the very high must be set..

File:GPIO8.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

In this example the NUCLEO-L476RG board is used.

  • Select NUCLEO-L476RG board using Board Selector as shown in the below figure.

Select NUCLEO-L476RG board.png

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

  • Save the project.

File:GPIO5.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 some products like STM32G0 can be found at 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
With the STM32L476, there are no ready to use examples.

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 pin 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.

File:GPIO6.png

2.4. Generate source code and edit main.c

The easiest way the generate the code is 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:GPIO10.png
Now, open the main.c file which is the main source file for this application

  • We do the LED toggling in a function inside main.c
Info white.png Information
Insert your code between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags


 /* USER CODE BEGIN 3 */
  /* Infinite loop */
  {
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
    HAL_Delay(500);
    
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
    HAL_Delay(500);
  }
  /* USER CODE END 3 */
Info white.png Information
Make sure to keep User Code when re-generating !

File:GPIO12.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

coda Michael|Escoda Michael]] (-) 14:01, 28 October 2022 (CEST)
Please use official picture not photo}}