Topics / GPIO
Topic

GPIO General-purpose input/output

The pins themselves: drive them high or low, read them, or hand them to another peripheral.

What it is

Every pin of the MCU belongs to a GPIO port of 16 pins (PA0…PA15, PB0…, up to PK). Each port is a small peripheral with its own registers, and each pin is in one of four modes: input, output, alternate function or analog.

GPIO is the first thing every other peripheral depends on. A UART, a timer channel or the LCD controller only reaches the outside world after you switch its pins to alternate-function mode and pick the right AF number. So even if you never blink an LED, you will configure GPIO in every project.

You need it for
  • Turning LEDs, relays or enable lines on and off
  • Reading buttons, switches and digital sensor outputs
  • Connecting a pin to a peripheral (UART, SPI, timer…) through its alternate function
  • Switching a pin to analog for the ADC or DAC

Read in this order

The shortest path through the documents. Each link opens the exact page; the line under it says what to look for.

  1. RM043311.2 GPIO main featuresp.530One page: everything a pin can do
  2. RM043311.3.1 General-purpose I/O (GPIO)p.533The pin modes, and what the reset state is
  3. RM043311.3.2 I/O pin alternate function multiplexer and mappingp.533How AF0–AF15 connect a pin to a peripheral
  4. RM043311.3.5 I/O data bitwise handlingp.534Why BSRR exists: atomic set and reset
  5. RM043311.4 GPIO registersp.540The ten registers of a port. Use it as a dictionary
  6. DS125565 Pin descriptionsp.53Datasheet tables: which AF number gives which signal on which pin
  7. DS125566.3.15 I/O port characteristicsp.127Electrical limits: current per pin, input levels, 5 V tolerance

Registers to learn first

In the order you usually program them. The other 3 registers of the chapter can wait until you need them.

1RCC_AHB4ENRTurn on the port clock first, or nothing below worksp.457
2GPIOx_MODERTwo bits per pin: input, output, alternate function or analogp.540
3GPIOx_OTYPERPush-pull or open-drain outputp.540
4GPIOx_OSPEEDREdge speed: low for LEDs, high for fast buses like SDRAMp.541
5GPIOx_PUPDRInternal pull-up or pull-down for inputsp.541
6GPIOx_IDRRead the level of every pin of the portp.542
7GPIOx_BSRRSet or clear single output pins in one writep.543
8GPIOx_AFRLAF number for pins 0–7 (AFRH: pins 8–15)p.544
All 10 registers in General-purpose I/Os (GPIO)

On this board

NetPinSignalNote
LED1PJ2—LD7 green user LED, active-low
LED2PI13—LD6 red user LED, active-low
WAKEUPPC13—Blue user button B1: reads high when pressed (external pull-down)
LCD_DISPPD7—Plain output that switches the display on
LCD_BLPK0—Plain output that enables the backlight

Map of the chapter

Every reference-manual chapter has the same shape. Sections marked ● are in the reading order above.

Common mistakes

  • Forgetting the port clock in RCC_AHB4ENR: writes to the GPIO registers are silently lost.
  • Setting a mode with |= only. Most pins reset to analog (11), so OR-ing in 01 leaves 11. Clear the two bits first.
  • Using a pin that the board already wires to something (LCD, SDRAM, Ethernet). Check its net on the schematic before you pick it.
  • Picking the AF number from memory or another chip. Look it up in this datasheet’s table for that exact pin.

Where to go next