top of page

STM GPIO Introduction

The STM Discovery Boards typically have a host of different hardware built into them. This hardware is not useful unless it can be controlled by the microcontroller(uC) we are using. Typically this control is done using the uC's general purpose input and output (GPIO). Using these pins we can do many things from turning LEDs on and off to setting up communication channels. This tutorial will give the basics of turning on and off an LED, later tutorials will also utilize the GPIOs to do additional functions like interrupts and communication. This tutorial will focus on the most basic GPIO use, as a switch for an LED.

STM HAL Library

Before starting this tutorial note that when compiling code with the STCubeMX, libraries of pre-written commands are available. These commands form the HAL library. This library allows for basic and advanced control of the ST uC. For this tutorial, we will be using the HAL library to configure and control our GPIOs. For a full list of commands in the HAL library download the HAL and LL programming manual from the STM references page.

1) Set up GPIO using STCubeMX

The first thing that needs to be done is you need to open up STM32CubeMX and select the ST Discovery Board you are using. Be sure to select the Discovery Board option when looking up the specific uC. Further instructions for this along with videos can be found in my tutorial on Setting Up STM for Programming. If you are using a discovery board there should already be plenty of GPIOs set up in STM32CubeMX. Usually, for the Discovery Boards, these GPIOs are connected to an LED or a button. Be sure to note the name of the GPIO and what LED it is connected to. To better understand this a picture of the uC from STM32CubeMX is shown below. Note that there is Port A and Port B this is designated by PA and PB respectively. Also, note that each port has 16 pins (PA0-PA15). Note that for my specific Discovery board there are four LEDs connected to pins PB9(Green), PB8(Orange), PB7(Blue), and PB6(Red). Also note that the program is already configured to give them names, LD_R, LD_L, LD_D, LD_U.

Screen Shot 2019-09-11 at 10.03.40 PM.pn

Now that there are GPIO pins set up on the uC we can go ahead and use STM32CubeMX to generate our code scaffolding. To do this follow the tutorial Setting Up Visual Studio for STM programming, this tutorial will explain how to use STM32CubeMX to generate code to configure the LEDs for us and it will also detail how to set up Visual Studio Code to edit the generated code. Be sure to follow ALL of the steps in the linked tutorial otherwise the code will not generate correctly.

2) Understanding the Code Generated by STMCubeMX

Upon opening the file containing the code generated by STM32CubeMX you should see 3 folders labeled: Src, Drivers, and Include. There will also be a Makefile(which will be used to generate binary files later. Finally, there are a few additional files whose purpose I will not get into, in this tutorial. The Src folder contains the main.c file which in this tutorial is all that we will be changing. The Src folder is also used to store the source for libraries. This brings us to the include folder, this folder is primarily using to hold the header files for user-created and HAL libraries. Finally, the drivers folder contains two sub-folders. One of these folders contains the drivers for the HAL library, the other is used to define specific addresses within the uC, neither of these folders will be used in this tutorial. Now, go ahead and open the main.c file found in the Src directory.

Note that most of the code in the main.c file is there to configure the pins used for a variety of functions like our GPIO ports. Most of the code should be ignored. The code most relevant to us is found under the command MX_GPIO_init(). The function itself should be found near the end of the main.c file, and will look something like (below)

Screen Shot 2019-09-11 at 10.33.29 PM.pn

Note that this function utilizes HAL functions to set up the GPIO port. These functions operate by configuring traits of the pins, example traits include input vs output, high vs low output, pullup/pulldown/no-pull, speed, etc. If none of these traits make sense to you do not worry, since we are currently working with a Discovery board and the HAL library, it is not necessary to understand all the intricacies of GPIOs yet. Rest assured that these traits allow the GPIO to operate both efficiently and correctly. Finally, note that if you go to near the beginning of the main.c file, you will see the MX_GPIO_Init called there. This, of course, executes the initialization, which then means we can use the GPIO.

3)  Using HAL To Turn the LED On and Off.

The command to turn the LEDs on and off is a single line HAL function. Before we use this function it is helpful to remember that all the LEDs are configured for port B and all were given names which were listed above. Also, remember each LED is connected to a single pin and 16 pins make up one port. With this in mind, the commands we need are
​
HAL_GPIO_TogglePin( <error-type> *GPIOx, <error-type> GPIO_Pin)
HAL_GPIO_WritePin( <error-type> *GPIOx, <error-type> GPIO_Pin, GPIO_PinState)
​
For the GPIOx pointer, you can input either GPIOB or GPIOBA in our case use (B). The GPIO_Pin should be the name of one of the pins connected to the LEDs, in our case (LD_U_Pin, LD_D_Pin, etc). Finally, GPIO_Pinstate should either be GPIO_Pin_Set which corresponds to the LED being on or GPIO_Pin_Reset which corresponds to the LED being off.
​
Note that if you are writing a very simple code you can add these commands in the while loop contained in the main(void) function of the main.c file. If you are writing more complex code I would suggest creating libraries and including them in the main.c file.

4) Upload the Code to the uC

To flash the code follow the page on flashing code to the ST uC.

bottom of page