πŸ”§ Understanding STM32 GPIO Input & Output Configuration (With Register Values Explained)

When you shift from 8-bit microcontrollers like AVR to ARM-based STM32, the first thing you notice is the increased complexity in configuring GPIO pins.
This is not a disadvantage β€” it’s actually the power and flexibility of STM32.

In this blog, we’ll break down all the GPIO configuration registers required to set a pin as input or output on STM32, along with the meaning of each value.

Whether you’re a beginner or transitioning from AVR, this guide will help you understand exactly why STM32 requires these configurations.


🟩 1. MODER β€” GPIO Mode Register (2 Bits per Pin)

Every GPIO pin in STM32 can operate in four different modes, unlike AVR which mainly has input/output.

ModeBinary ValueDescription
00InputReads external signal
01OutputDrives 0 or 1
10Alternate FunctionUsed for UART, SPI, I2C, PWM, etc.
11Analog ModeUsed for ADC or ultra-low power

Example

  • PA5 = 01 β†’ Output
  • PC13 = 00 β†’ Input

This register decides what the pin actually is.


🟦 2. OTYPER β€” Output Type Register (1 Bit per Pin)

Defines how the pin drives the output.

ValueTypeApplication
0Push-PullGeneral I/O, LEDs
1Open-DrainI2C, multi-device buses

Example

PA5 = 0 β†’ Push-Pull

STM32 provides open-drain at GPIO level, which AVR does not.


🟨 3. OSPEEDR β€” Output Speed Register (2 Bits per Pin)

Controls the switching speed of the pin.
Fast MCU clock speeds produce fast edges β†’ which can cause EMI or noise.

ValueSpeed
00Low Speed
01Medium Speed
10High Speed
11Very High Speed

Example

PA5 = 01 β†’ Medium speed

This is important when driving communication lines or fast peripherals.


πŸŸͺ 4. PUPDR β€” Pull-Up / Pull-Down Register (2 Bits per Pin)

STM32 pins are high-impedance.
So we must define how the pin behaves when nothing is driving it.

ValueMeaning
00No Pull-up / No Pull-down
01Pull-up
10Pull-down
11Reserved

Example

  • PA5 = 00 β†’ No pull (output doesn’t need one)
  • PC13 = 00 β†’ Button has external pull-up on board

AVR only has internal pull-up; STM32 gives full control.


πŸŸ₯ 5. IDR & ODR β€” Input and Output Data Registers

These registers directly represent the logic level on the pin.

IDR β€” Input Data Register

Used to read input pin:

GPIOC->IDR & (1 << 13);   // Read PC13

ODR β€” Output Data Register

Used to write output:

GPIOA->ODR |= (1<<5);   // Set PA5 HIGH  
GPIOA->ODR &= ~(1<<5);  // Set PA5 LOW

These two registers are the heart of GPIO operation.


🟧 6. BSRR β€” Bit Set/Reset Register (Atomic Operations)

BSRR is one of the most powerful GPIO features in STM32.
It allows atomic, interrupt-safe pin updates.

OperationBits Used
Set pinLower 16 bits
Reset pinUpper 16 bits

Example

Turn LED ON:

GPIOA->BSRR = (1 << 5);

Turn LED OFF:

GPIOA->BSRR = (1 << (5 + 16));

Unlike ODR (which is read-modify-write), BSRR updates only one bit safely.


🧠 Why AVR Never Needed All This?

Because AVR is:

  • 8-bit
  • Slow clock
  • Limited pin functions
  • No alternate function system
  • No high-speed switching
  • Only internal pull-up

STM32 pins must handle:

  • High-speed communication
  • Multiple roles per pin
  • Noise/EMI handling
  • Industrial interfacing
  • Open-drain buses
  • ADC/DAC
  • PWM timers

This is why STM32 requires more detailed configurationβ€”because it can do more.


🏁 Final Summary

To configure a GPIO pin correctly in STM32, we set:

RegisterPurpose
MODERSelect Input/Output/AF/Analog
OTYPERPush-Pull vs Open-Drain
OSPEEDRControl switching speed
PUPDRPull-up / Pull-down
IDRRead the input
ODRWrite the output
BSRRAtomic set/reset

This fine-grained control is what allows STM32 MCUs to operate in professional, industrial, and real-time applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top