Quick Start Guide to STM32 GPIO

โญ Quick Start Guide to STM32 GPIO

Includes: Best Practices | Common Functions | Debug Output | Example Code (Compilable)

๐Ÿงฉ 1. What is GPIO – The Fundamental “Control Interface”

Chinese English Brief Description
้€š็”จ่พ“ๅ…ฅ่พ“ๅ‡บ็ซฏๅฃ General Purpose Input/Output (GPIO) The most basic module for the MCU to “read from the outside” and “control the outside”.

Each pin of the STM32 can be configured for different functions, with GPIO being the most fundamental:

  • โ€ข Light up an LED (Output)
  • โ€ข Read a button (Input)
  • โ€ข Drive a buzzer (Output)
  • โ€ข Detect sensor signals (Input)
  • โ€ข Serve as multiplexed pins for SPI/UART/IยฒC (Multiplexed Mode)

Almost all projects rely on GPIO.

๐Ÿงฑ 2. Why are ports divided into A/B/C/D… with 16 pins each?

The core reason:๐Ÿ‘‰ Facilitates hardware design + Unified register structure + Increases efficiency

  • โ€ข STM32 registers are 32 bits
  • โ€ข Each pin requires 2 bits to represent its mode (MODER)
  • 16 pins = 16 ร— 2 bits = 32 bits, exactly one register can control one port

Therefore, each port is fixed at 16 pins:

Port Pins
GPIOA PA0 ~ PA15
GPIOB PB0 ~ PB15
GPIOC PC0 ~ PC15
โ€ฆ (Specific models determine availability)

๐Ÿงฑ 3. Four Basic Attributes of GPIO That Must Be Understood

A pin in STM32 is determined by at least the following configurations:

Attribute English Function
ๆจกๅผ Mode Input/Output/Multiplex/Analog
่พ“ๅ‡บ็ฑปๅž‹ Output Type Push-Pull or Open-Drain
ไธŠไธ‹ๆ‹‰ Pull-up/Pull-down Prevents floating inputs
่พ“ๅ‡บ้€Ÿๅบฆ Output Speed Speed of level switching

Understanding these 4 switches = Fully mastering GPIO.

โš™๏ธ 4. Common GPIO Code + Debug Output (Essential for Beginners)

The following code can be directly placed in <span>main.c</span> for verification.

1๏ธโƒฃ Output Mode: Control LED

Assuming there is an LED on <span>PC13</span> (common on minimal system boards)

CubeMX Configuration

  • โ€ข PC13 โ†’ GPIO_Output
  • โ€ข Output Type = Push-Pull
  • โ€ข Pull = No Pull
  • โ€ข Speed = Low

Example Code

// Light up LED
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET); 
// Note: On some boards, PC13 is lit when low

// Turn off LED
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);

// Toggle LED
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);

Add Debug Output (Recommended)

HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
printf("LED toggled. Current state: %d\r\n", 
        HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13));

Output Example

LED toggled. Current state: 0
LED toggled. Current state: 1

๐Ÿ‘‰ Strongly recommended:Print every action for easier debugging of peripheral issues.

2๏ธโƒฃ Input Mode: Read Button

Assuming the button is connected to PB12, pressed to ground, requiring internal pull-up.

CubeMX Configuration

  • โ€ข PB12 โ†’ GPIO_Input
  • โ€ข Pull = Pull-up

Example Code

if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_RESET)
{
    printf("Button Pressed!\r\n");
}

Output Example

Button Pressed!

3๏ธโƒฃ Open-Drain Output – Common in IยฒC Bus

Open-drain can only output:

  • โ€ข Low level (0)
  • โ€ข High-Z (floating)

External resistors are responsible for pulling the level high.

CubeMX Configuration (I2C SDA/SCL)

  • โ€ข GPIO Output Type: Open-drain
  • โ€ข Pull-up: External Pull-up (commonly 4.7k)

Manual Open-Drain Output Example

// Output low level
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);

// Release the bus (allow external pull-up resistor to pull high)
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET);

4๏ธโƒฃ Multiplexed Function AF: UART / SPI / TIM / PWM

For example USART1:

Pin AF Function
PA9 AF7 (USART1_TX)
PA10 AF7 (USART1_RX)

CubeMX will generate this automatically.

๐Ÿš€ 5. Best Practices for GPIO (Engineer-Level Summary)

๐ŸŸฆ 1. Always add pull-up/down for inputs (to prevent floating noise)

Do not use Floating Input! Otherwise:

  • โ€ข Signal jitter
  • โ€ข CPU interrupts triggered incorrectly
  • โ€ข Program anomalies

Experience Summary:

Scenario Recommendation
Button Pull-up
Sensor Output Follow sensor manual recommendations
Signal Source with Open Circuit Possibility Pull-up or Pull-down

๐ŸŸฆ 2. Prefer Push-Pull for Outputs

Unless you are very sure you need open-drain.

Push-Pull is suitable for:

  • โ€ข LED lights
  • โ€ข Driving small loads
  • โ€ข Digital signals
  • โ€ข PWM outputs

Open-Drain is used for:

  • โ€ข I2C
  • โ€ข Multi-device shared lines
  • โ€ข Scenarios requiring external pull-up

๐ŸŸฆ 3. Use BSRR instead of ODR (Faster and Safer)

Not recommended:

GPIOC-&gt;ODR ^= (1 &lt;&lt; 13);

Recommended:

GPIOC-&gt;BSRR = (1 &lt;&lt; 13);       // Set
GPIOC-&gt;BSRR = (1 &lt;&lt; (13 + 16)); // Reset

Advantages of BSRR:

  • โ€ข Atomic operation, will not be interrupted and cause errors
  • โ€ข Faster speed

HAL’s <span>WritePin()</span> uses BSRR.

๐ŸŸฆ 4. Adding Debug Output (printf) is the Fastest Way for Beginners to Improve

Recommendation:

printf("Pin PB12 = %d\r\n", HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12));

Don’t be afraid of too many prints! If you can see it, you can understand it; if you can understand it, you can control it.

๐ŸŸฆ 5. Don’t set GPIO speed (OSPEEDR) too high

Low speed is sufficient (unless for high-speed PWM/SPI) Otherwise:

  • โ€ข Increased EMI
  • โ€ข Increased power consumption
  • โ€ข Sharp signal edges can cause interference in the circuit

๐Ÿ”ง 6. Comprehensive Demo – Button Controls LED (with Print)

#include "main.h"
#include &lt;stdio.h&gt;

void SystemClock_Config(void);
void MX_GPIO_Init(void);

int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();

    printf("GPIO Demo Start!\r\n");

    while (1)
    {
        if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_RESET)
        {
            HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
            printf("Button Pressed โ†’ LED ON\r\n");
        }
        else
        {
            HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
            printf("Button Released โ†’ LED OFF\r\n");
        }

        HAL_Delay(100);
    }
}

Output Example

GPIO Demo Start!
Button Released โ†’ LED OFF
Button Released โ†’ LED OFF
Button Pressed โ†’ LED ON
Button Pressed โ†’ LED ON
Button Released โ†’ LED OFF

๐Ÿงญ 7. Diagram of GPIO Working Model (Very Important)

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ MCU ๅ†…้ƒจ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                 GPIO PORT             โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚ โ”‚ MODER     โ”‚ OTYPER   โ”‚ PUPDR      โ”‚ โ†’ ็กฌไปถ้…็ฝฎๅฏ„ๅญ˜ๅ™จ
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚                 โ”‚                      โ”‚
โ”‚                 โ–ผ                      โ”‚
โ”‚             ๅผ•่„šๆŽงๅˆถ้€ป่พ‘               โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ–ผ
            ๏ผˆๅค–้ƒจๅผ•่„š PAx/PBx๏ผ‰

๐Ÿ“Œ Summary: The Core of GPIO in One Sentence

Input looks at IDR, output writes BSRR, configuration relies on MODER/OTYPER/PUPDR/OSPEEDR. Prefer push-pull, use pull resistors to prevent floating, print more, observe more.

Leave a Comment