This article explains what GPIO is and how to use it through examples
1. GPIO definition
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. A GPIO pin can be configured either as input or output:
- Once configured as an Input, the GPIO pin can be pulled up or pulled down.
- Once configured as an Output, the GPIO pin can be in mode push-pull or open drain.
2. Configure GPIO for LED toggling
2.1. Objective
- In this project, you will learn how to Toggle a pin on STM32L476 Nucleo board using Hardware Abstraction Layer (HAL) library
- Learn how to setup the pin and GPIO port in STM32CubeIDE
2.2. How
- Configure the GPIO pin in STM32CubeIDE and generate the code
- Add into the project the HAL_Delay function and HAL_GPIO_Toggle function
- Verify the correct functionality on toggling LED
2.3. Create the project in STM32CubeIDE
File > New > STM32 Project in main panel.
File:GPIO2.png
- Save the project
- In case you haven't downloaded the STM32L476 Cube library, it will be downloaded automatically, it may take some time.
- Once the download is done, you can select NUCLEO-L476RG in board Selector.
- 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)
- 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)
- Yellow pin are related to the power supply
- Unused pins are marked as Grey
Note: During the download of a firmware package with STM32CubeMX, 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
Note: With the STM32L476, there are no ready to use examples.
2.4. GPIO Configuration
- 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.
2.5. 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..
2.6. Set the project details for generation
- 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
2.6.1. Open the main.c in our IDE
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
/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_Delay(500);
}
/* USER CODE END 3 */
2.7. Compile and flash
- Every 500 ms the green LED state changes.