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.
- 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.
- RM043311.2 GPIO main featuresp.530One page: everything a pin can do
- RM043311.3.1 General-purpose I/O (GPIO)p.533The pin modes, and what the reset state is
- RM043311.3.2 I/O pin alternate function multiplexer and mappingp.533How AF0–AF15 connect a pin to a peripheral
- RM043311.3.5 I/O data bitwise handlingp.534Why BSRR exists: atomic set and reset
- RM043311.4 GPIO registersp.540The ten registers of a port. Use it as a dictionary
- DS125565 Pin descriptionsp.53Datasheet tables: which AF number gives which signal on which pin
- 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.
| 1 | RCC_AHB4ENR | Turn on the port clock first, or nothing below works | p.457 |
| 2 | GPIOx_MODER | Two bits per pin: input, output, alternate function or analog | p.540 |
| 3 | GPIOx_OTYPER | Push-pull or open-drain output | p.540 |
| 4 | GPIOx_OSPEEDR | Edge speed: low for LEDs, high for fast buses like SDRAM | p.541 |
| 5 | GPIOx_PUPDR | Internal pull-up or pull-down for inputs | p.541 |
| 6 | GPIOx_IDR | Read the level of every pin of the port | p.542 |
| 7 | GPIOx_BSRR | Set or clear single output pins in one write | p.543 |
| 8 | GPIOx_AFRL | AF number for pins 0–7 (AFRH: pins 8–15) | p.544 |
All 10 registers in General-purpose I/Os (GPIO)
On this board
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 in01leaves11. 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.