STM32 GPIO Tutorial

Previous Tutorial Previous Tutorial Tutorial 3 Next Tutorial Next Tutorial
STM32 GPIO Tutorial
STM32 Course Home Page 🏠


STM32 GPIO Tutorial


In this tutorial, we’ll discuss the STM32 GPIO hardware. How it works and what sort of functionalities there are, so you can configure it in the best way to meet your applications’ needs. We’ll get into the GPIO speed, alternative functions, locking mechanism, and different possible configurations. So let’s get started!

   STM32 GPIO Ports   


Each of the general-purpose I/O ports has two 32-bit configuration registers, two 32-bit data registers, a 32-bit set/reset register, a 16-bit reset register, and a 32-bit locking register. Each I/O port bit is freely programmable, however, the I/O port registers have to be accessed as 32-bit words (half-word or byte accesses are not allowed). The purpose of the set/reset registers is to allow atomic read/modify accesses to any of the GPIO registers. This way, there is no risk that an IRQ occurs between the read and modify access.

Here is a digital diagram for the internal structure of a typical GPIO pin. It shows the diode protection, internal pull-up or down enable/disable, and also the push-pull output driver, output enable/disable for switching between input/output pin modes, Schmitt-triggered digital input, analog input.

STM32 GPIO Internal Diagram - ARM Tutorials

You have to assume that all GPIO pins are not 5v tolerant by default until you find in the datasheet that a specific pin is 5v tolerant, only then you can use it as a 5v pin. The pins are mostly 3.3v and can be damaged when hooked up to 5v digital input lines. Level shifting may be mandatory in many cases.

STM32 GPIO Maximum Voltage

So you have to be careful with the voltage level for input pins. And also you’ve to pay attention to the output current when you set GPIO output pins. The maximum current that could be sourced or sunk into any GPIO pin is 25mA as per the datasheet. And you’ve to check it for the specific target microcontroller you’re dealing with.

STM32 GPIO Output Current Maximum



   STM32 GPIO Speed   


For Input Mode

When a GPIO pin is set to input mode, the data present on the I/O pin is sampled into the Input Data Register every APB2 clock cycle. This means the APB2 bus speed determines the input sampling speed for the GPIO pins.

For Output Mode

When a GPIO pin is set to output mode, you’ll have the option to configure the pin speed mode by programming the respective bits in the configuration registers. Down below is a table for the different modes available in the datasheet for the STM32F103C8 microcontroller.

STM32 GPIO Pin Speed Modes Table

However, other microcontrollers will have different options for the pin output speed. For example, the other microcontroller we’ll be using in this course STM32L432KC. It has a dedicated speed control register for GPIO pins, which have 4 different speed options: Low-speed, Medium-speed, High-speed, Very High-speed.

STM32 GPIO Output Speed Control Register

You’ll find what do these speed grades translate to in the device datasheet io pin characteristics. Here is the table for this parameter from the STM32L432KC datasheet table.

Note: Typical values depend on various parameters of which the VDDio line voltage, the total pin capacitance.

  • Low Speed -> Up to 10MHz
  • Medium Speed -> Up to 50MHz
  • High Speed -> Up to 100MHz
  • Very High Speed -> Up to 180MHz
STM32 GPIO Output Speed Options
This table, for reference only, shows only the low-speed mode. The other modes are in the complete table in the datasheet.


   GPIO Bit Atomic Operations   


There is no need for the software to disable interrupts when programming the GPIOx_ODR at bit level: it is possible to modify only one or several bits in a single atomic APB2 write access. This is achieved by programming to ‘1’ the Bit Set/Reset Register (GPIOx_BSRR, or for reset only GPIOx_BRR) to select the bits to modify. The unselected bits will not be modified.


   STM32 GPIO External Interrupts   


All ports have external interrupt capability. To use external interrupt lines, the port must be configured in input mode. We’ll discuss this in a more detailed manner when we get into the EXTI  (external interrupt/event controller) topic.


   Peripheral Pin Select (PPS)   


To optimize the number of peripheral I/O functions for different device packages, it is possible to remap some alternate functions to some other pins. This is achieved by software, by programming the corresponding registers.

This option can help you remap the peripherals io pins, so you don’t have to change so much in the PCB layout when you change the target microcontroller on the board. This can be extremely advantageous and ease the routing process. And help you move the high-speed signals away so as to reduce the noise level at certain parts.


   STM32 GPIO Locking Mechanism   


The locking mechanism allows the IO configuration to be frozen. When the LOCK sequence has been applied on a port bit, it is no longer possible to modify the value of the port bit until the next reset.


   STM32 GPIO Configurations   


Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each port bit of the General Purpose IO (GPIO) Ports, can be individually configured by software in several modes:

PCBgogo Ad

Output Configuration

  • Output Open-Drain
  • Output Push-Pull

Input Configuration

  • Input Floating (Hi-Z)
  • Input Pull-Up
  • Input Pull-Down

Alternate Function Configuration

  • Alternate Function Push-Pull
  • Alternate Function Open-Drain

Analog Configuration


   STM32 GPIO Concluding Remarks   


Not all GPIO Pin are 5v tolerant. Mostly they are 3.3v.

You’ve to enable the clock for the GPIO you want to use regardless of the mode of operation.

When configuring a GPIO output pin, you can select the speed suitable for your application. Whether you need high-frequency switching on the IO line or not.

The APB2 bus speed determines the sampling rate for all GPIO input configured pins.

You can disconnect any GPIO pin (make it high impedance) by setting it into input mode with Hi-Z.

You can freeze or lock the GPIO pins configuration after the system initialization if no change is intended using the locking mechanism.

Alternative function pins can be interchanged (remapped) internally so you can enjoy more freedom in routing these signals.

All GPIO pins are interrupt-capable pins but have to be configured within the EXTI as we’ll see later on in the course.




That’s it for this tutorial. In the next tutorial, we’ll start our first practical LAB!



Previous Tutorial Previous Tutorial Tutorial 3 Next Tutorial Next Tutorial
Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author

Khaled Magdy

I'm an embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, Ai/ML, and other fields I'm passionate about. I love reading, writing, creating projects, and Technical training. A reader by day and a writer by night, it's my lifestyle. You can view my profile or follow me via contacts.

Leave a Reply