Getting started with GPIO

Revision as of 09:52, 26 August 2020 by Registered User
Under construction.png Coming soon

This article explain what is and how to use GPIO through examplesl

1. GPIO definition

Stands for General Purpose Input/Output. GPIO 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 software.

1.1. Configure GPIO for LED toggling

1.1.1. Objective

  • Learn how to setup pin and GPIO port in STM32CubeMX
  • How to Generate Code in STM32CubeMX and use HAL functions

1.1.2. Goal

  • Configure GPIO pin in STM32CubeMX and Generate Code
  • Add in to project HAL_Delay function and HAL_GPIO_Toggle function
  • Verify the correct functionality on toggling LED

1.1.3. Create project in CubeMX

  • New project > Access to board selector on main panel or Menu > File > New Project
  • Select STM32L476RG

File:SelectL476.png

  • if you have chosen to start the project with a board LED pin is already setted ( PA5 on NucleoL476RGI, for other board check User Manual)

File:LED UM.png LedPinGPIO.png

1.1.3.1. GPIO Configuration
  • TAB>Configuration>System>GPIO

File:5 GPIO LAB.png

1.1.3.2. GPIO(Pin) Configuration
  • Select Push Pull mode
  • No pull-up and pull-down
  • Output speed to HIGH is important for faster peripheries like SPI, USART
  • Button OK

File:6 GPIO LAB.png

1.1.3.3. GPIO(Pin) output speed configuration
  • Change the rising and falling edge when pin change state from high to low or low to high
  • Higher GPIO speed increase EMI noise from STM32 and increase STM32 consumption
  • It is good to adapt GPIO speed with periphery speed. Ex.: Toggling GPIO on 1Hz is LOW optimal settings, but SPI on 45MHz the HIGH must be set

File:7 GPIO LAB.png

1.1.3.4. Now we set the project details for generation
  • Menu > Project > Project Settings
  • Set the project name
  • Project location
  • Type of toolchain

File:8 GPIO LAB.png

1.1.3.5. Now we can Generate Code
  • Menu > Project > Generate Code
1.1.3.6. Now we open the project in our IDE
  • The functions we want to put into main.c
Info white.png Information
Between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
 /* 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 */