What are the GPIO output modes (push-pull vs open-drain) and input options?
Short answer: A push-pull output actively drives both high (to VDD) and low (to ground), giving fast, strong edges for normal signals. An open-drain output can only pull low or release the line (high impedance); a pull-up resistor makes the high level. Open-drain allows several devices to share one line (wired-AND, as in I2C and shared interrupt lines), lets a device "drive" a line to a different voltage than its own supply (within the pin's tolerance), and prevents shorts when two outputs disagree. Inputs can be floating, pulled up or down, with optional Schmitt trigger and analog mode.
| Mode | Drives high | Drives low | Use |
|---|---|---|---|
| Push-pull output | yes | yes | LEDs, chip selects, SPI, UART TX, enables |
| Open-drain output | no (pull-up) | yes | I2C, shared IRQ lines, level translation to a lower/equal voltage |
| Input, floating | – | – | driven by an external push-pull source |
| Input with pull-up/down | – | – | buttons, open-drain sources |
| Analog | – | – | ADC/DAC/comparator pins (digital input buffer disabled: lowest leakage) |
| Alternate function | peripheral controls the pin | UART, SPI, timers, etc. |
Other GPIO settings worth knowing: output speed/slew rate (slower edges = less EMI and ringing), drive strength, and whether a pin is 5 V tolerant (FT pins on STM32 can accept 5 V inputs when configured as digital, not analog).
Why unused pins should be configured: floating inputs can toggle and draw current; set unused pins to analog mode (lowest power) or output low.
Atomic GPIO writes: use the set/reset register (e.g. STM32 BSRR) to change individual pins without a read-modify-write that could race with an interrupt changing other pins on the same port.