GPIO

GPIO (General Purpose Input/Output) refers to freely programmable digital inputs and outputs of an integrated circuit, such as a microcontroller, microprocessor, or SoC. These pins do not have a predefined function initially and can be configured via software. This allows for the detection of different external signals or direct control of electronic components.

GPIO pins provide a fundamental interface between a processor's digital logic and the outside world. Sensors, switches, or communication lines can be connected via these pins. At the same time, GPIO outputs can be used to control LEDs, relays, drivers, or other electronic modules.

Operating principle

A GPIO pin can typically be used in two basic modes:

InputThe Microcontroller reads the logical state of an external signal. A distinction is often made between two states: High and Low.

OutputThe microcontroller actively sets a logic level on a pin, thereby controlling external components.

Pins are usually grouped into ports. A port consists of several pins that can be addressed together via a register. This allows multiple signals to be read or written simultaneously.

To read or write a port, the registers must be read or written using bitwise arithmetic, depending on whether the bus width is 32 or 64 bits.

Programming GPIO pins

The configuration and control of GPIO pins are done via the microcontroller's hardware registers. These registers are located in the controller's memory space and can be written to directly by software. Many microcontroller manufacturers provide defined software stacks for GPIO routines. Especially in the aerospace or railway context, it is absolutely common Drivers and Low-Level Software to write for peripherals like GPIOs yourself.

An important register is the GPIO Mode Register. This register defines the operating mode of a pin. The register is typically 32 bits wide, with two bits reserved for each pin.

The possible operating modes are often:

  • Input Mode
    The pin works as a digital input.
  • General Purpose Output Mode
    The pin is used as a digital output.
  • Alternate Function Mode
    The pin takes on a special hardware function, for example, for UART, SPI, I²C, or Timer
GPIO Port MODE Register Structure: 32 fields MODER15–0, each with 2 bit fields (rw). Example for embedded hardware/electronics.
GPIO Mode Register from a STM32Data sheet

The latter point means that if a peripheral function is required, on most microcontrollers, the GPIO must also be initialized (port clock and mode registers).

In the case of output, the value is then set via the GPIO Output Data Register. The output state of a pin is controlled via this register. Setting or clearing a bit changes the logical level on the corresponding GPIO pin accordingly.

Schema of an ODR register set in an embedded hardware context: rw fields, reserved area, address offset 0x14.
GPIO Output Data Register

In many microcontrollers, additional registers exist for:

  • Pull-up / Pull-down configuration
  • Initial velocity
  • Open-Drain or Push-Pull output modes

Typical Applications

GPIO pins are used in embedded systems for numerous tasks, for example:

  • Reading buttons or switches
  • LED Control
  • Control of relays or motors
  • Communication with Simple Peripherals

Furthermore, GPIO pins can be used to implement communication protocols purely in software. For example, methods like I²C or SPI can be realized through so-called bit-banging techniques.

GPIO Interrupts

One of the most important functions of GPIO Pins is the possibility, Interrupts to trigger. The microcontroller reacts immediately to an external event on a GPIO pin without the software having to poll the pin continuously.

In principle, a program could regularly check the state of a GPIO pin (so-called polling). However, this consumes runtime because the processor must constantly expend resources to monitor the pin. With interrupts, an event – for example, a signal change on a pin – can instead automatically trigger an interrupt service routine (ISR).

When such an event is detected, the microcontroller briefly interrupts the current program execution and jumps to the associated interrupt routine. There, the event can be processed, such as reading a button, starting a measurement, or reacting to an external signal.

In embedded systems, in particular, GPIO interrupts are of great importance because they enable event-driven programming. This allows the processor to operate energy-efficiently while reacting very quickly to external events. Interrupts are therefore frequently used for time-critical signals, user interactions, or external communication interfaces.

Conclusion

GPIOs are among the most important interfaces of a microcontroller. They enable flexible connection between digital logic and external hardware. Due to their programmable configuration, they can be used in a wide variety of applications, thus forming a fundamental component of many embedded systems.

Zurück zum Glossar