Library 02- STM32F429 Discovery GPIO tutorial with onboard leds and button

Your first blinky project works, but you don’t know how?

I will explain GPIO (General Purpose Input/Output) CMSIS Library. This library is used to work with physical pins on microcontroller. You can set pins to input or output, put them low (0 volts) or HIGH (3,3 volts), select pull resistors, choose output type and select clock speed.

To be able to work with pin, you have to enable pin clock. This is set inside RCC (Reset and Clock Control). All GPIO‘s are on AHB1 bus. We will use GPIOG port, because our onboard leds are connected to pins PG13 and PG14. We can enable clock with code below:

Next, you need a GPIO_InitTypeDef struct to set options for pin. If you have included GPIO library into project, you can do this with following:

GPIO_InitTypeDef has 5 options:

  1. GPIO_Pin: Choose pins you will use for set settings
  2. GPIO_Mode: Choose mode of operation. Look down for options
  3. GPIO_OType: Choose output type
  4. GPIO_PuPd: Choose pull resistor
  5. GPIO_Speed: Choose pin speed

Every of this settings has it’s own options.

  • GPIO_Pin: With this you select pins.
  • GPIO_Mode: Mode of pins operation
    • GPIO_Mode_IN: Set pin to input
    • GPIO_Mode_OUT: Set pin to be an output
    • GPIO_Mode_AF: Set pin to alternating function (to use with peripheral ex. SPI, USART, etc)
    • GPIO_Mode_AN: Set pin to be an analog (ADC or DAC)
  • GPIO_OType: Mode for pin’s output type
    • GPIO_OType_PP: Output type is push-pull
    • GPIO_OType_OD: Output type is open drain
  • GPIO_PuPd: Select pull resistors or disable it
    • GPIO_PuPd_UP: Enable pull up resistor
    • GPIO_PuPd_DOWN: Enable pull down resistor
    • GPIO_PuPd_NOPULL: Disable pull resistor
  • GPIO_Speed: Select GPIO speed
    • GPIO_Speed_100MHz
    • GPIO_Speed_50MHz
    • GPIO_Speed_25MHz
    • GPIO_Speed_2MHz





We never told the script which port we will use because we have more pins 13 and 14 on board. Here comes the first GPIO function called GPIO_Init(). It is located in stm32f4xx_gpio.h

We want to initialize pins PG13 and PG14 (there are red and green leds on board) to be an output, push-pull output type, without pull resistor and speed 50MHz. We do this like this:

Our pins are now ready. But leds are still off. We need to turn them on:

Or if we would like to put leds back off:

Toggle pin is possible too:

On discovery board also has blue “User button” connected to PA0. Let’s configure it as input with enabled pull down resistor.

We have configured pin as input, but how to read value on pin?

This is all. I made a little program to test all. Leds are turned off until you press on blue button.

And because I don’t always want to initialize leds and button, I made a library that will do this for you.

Library

Features

  • Sets led on, off or toggle
  • Checks led state if it is on or off
  • Checks button state
  • Supports STM32F429-Discovery leds and button
  • Supports STM32F4-Discovery leds and button
  • Supports STM32F401-Discovery leds and button
  • Supports Nucleo F401 led and button
  • Supports Nucleo F411 led and button

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
  • TM
    • TM GPIO
    • defines.h

By default, library don’t know which board is used in your project, so you have to tell it. Open defines.h file or global project’s defines (IDE options) and add various defines, according to the board you use. If you not specify settings, you will get an error “tm_stm32f4_disco.h: Please select your board“.

Functions and enumerations

Example

  • When you press the button, led will be turned on.

Project available on Github, download library below.

I hope this tutorial a little bit helps to you.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!