The Eight Pin Modes of STM32

GPIO

This article is based on the HAL library

The Eight States of GPIO

Output Input
Push-Pull Floating
Multiplexed Push-Pull Pull-Up
Open-Drain Pull-Down
Multiplexed Open-Drain Analog
Note: STM32 pins are 3.3V tolerant (3.6V is the limit), 5V tolerant (5.5V is the limit). Exceeding the pin tolerance will damage the chip.

Output

Both push-pull and open-drain outputs can be viewed as a MOS switch controlled by voltage levels (not a mechanical switch; the circuit remains connected, and when the MOS transistor is in a high-impedance state, it can be approximated as an open circuit. If the external voltage is too high, it may still break down the circuit).Operating principle: The configuration layer registers determine the pin function, while the data source layer (on-chip peripherals/output data registers) provides control signals, and the output driver stage controls the pin voltage levels.

Push-Pull Output:

Uses the pin voltage to connect to the internal 3.3V as the circuit power supply, with external grounding.When the control signal is high, the circuit is on (turning off N MOS, P MOS has a delay to turn on).When the control signal is low, the circuit is open (turning off P MOS, N MOS has a delay to turn on).

Open-Drain Output:

Uses external voltage as the power supply, with internal grounding.When the control signal is high, the circuit is open (N MOS is off).When the control signal is low, the circuit is on (N MOS is on).

Multiplexed Push-Pull/Open-Drain Output

In push-pull/open-drain output, the control signal is provided by the output data register.In multiplexing, the control signal is provided by the peripheral register (USART, I²C, etc.).

Input

Operating principle: Pull-Up/Pull-Down/Floating (Digital Input)The input passes through a Schmitt-triggered Schottky flip-flop(Two reference voltage lines: above the high reference line is recognized as high, below the low reference line is recognized as low, and between the two reference lines, it maintains the last recognized voltage state).The processed output goes to the input data register & on-chip peripherals.Analog Inputis directly sent to the on-chip peripherals without processing.

Pull-Up Input

Internally connects to VDD and has a weak resistor (30-50KΩ) in series between the pin and VDD.When there is no external signal, the pin is at high level (VDD).

Pull-Down Input

Internally connects to VSS and has a weak resistor (30-50KΩ) in series between the pin and VSS.When there is no external signal, the pin is at low level (VSS).

Floating Input

Directly connected externally; when there is no external input, the pin level is determined by environmental noise (random and uncontrollable).Use when there is a definite external level.

Analog Input

Directly samples the external continuous voltage as input.

The GPIO addresses of STM32 are given by Port, and during operation, the Pin bit mask is used to modify one of them.Basic operations for learning include the following:

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // Set PA5 to high level
HAL_GPIO_ReadPin (GPIOB, GPIO_PIN_0); // Read the state of PB0
Hal_GPIO_TooglePin(LED_RED_GPIO_PORT,LED_RED_PIN); // Toggle the LED_RED pin level under LED_RED_GPIO group

Leave a Comment