Getting started with GPIO

Revision as of 16:29, 2 July 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 CubeMX 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

  • Menu > File > New Project
  • Select STM32F4 > STM32F429/439 > LQFP144 > STM32F439ZITx
  • Configure LED pin as GPIO_Output

File:1 GPIO LAB.png

1.1.4. For debug purpose is recommended to select debug pins SWD or JTAG

  • Select can be done in TAB>Pinout>SYS
  • On discovery is available only SWD option
  • If SWD/JTAG is not selected and the Set all free pins as analog (MENU>Project>Settings>TAB>Code Generator) is selected, debug is not possible

File:2 GPIO LAB.png

1.1.4.1. Clock Configuration overview 10
  • External clock enabling
    • TAB>Pinout
    • Select HSE and LSE clocks
    • Bypass or crystal

File:3 GPIO LAB.png

1.1.4.2. In order to run on maximum frequency, setup clock system

File:4 GPIO LAB.png

1.1.4.3. GPIO Configuration
  • TAB>Configuration>System>GPIO

File:5 GPIO LAB.png

1.1.4.4. 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.4.5. 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.4.6. 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.4.7. Now we can Generate Code
  • Menu > Project > Generate Code
1.1.4.8. 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 */