Full name: General-purpose input/output.
Definition
GPIO (general-purpose input/output) is a pin of a chip that software controls directly as a digital signal, with no fixed function of its own. As an output, the pin drives a voltage that software sets high (logic 1) or low (logic 0). As an input, it reports whether the voltage applied from outside is high or low, and it can request an interrupt §6.11 when that level changes. Software controls the pins through the registers of the chip’s GPIO peripheral, the hardware block that drives and senses them.
The idea
An engineer wants to see on an oscilloscope, an instrument that plots voltage against time, exactly when firmware reaches a certain line of code. Printing a message over a serial line takes milliseconds. Setting a GPIO pin high takes one store instruction, and the instrument times the resulting voltage edge to within nanoseconds, with its own clock. Configured as an input, a pin lets an outside event, such as a button press or a trigger from test equipment, reach the software.
The edge is not the decision. Between the instruction that decides and the voltage crossing the instrument’s threshold lie the code that leads to the write, the write’s trip across the chip’s internal bus, the switching of the output, the change of the voltage, and the instrument’s detection. A ship’s signal flag is a fair picture: the captain decides, a sailor hoists the flag, and an observer timestamps the flag, not the decision. The picture stops being accurate in one respect: most of the chip’s steps take a fixed few nanoseconds, but the code path can vary widely, for example when an interrupt arrives in between.
How it works
Configuring a pin. Registers of the GPIO peripheral hold each pin’s settings:
direction: input or output;
output type: push-pull, which actively drives both high and low, or open-drain, which only pulls the line low and leaves a resistor to pull it high, so that several devices can share one line;
pull resistor: an optional internal resistor to the supply (pull-up) or to ground (pull-down) that gives an unconnected input a defined level;
drive strength: how much current the output supplies, which sets how fast its voltage changes;
function: a pin can be handed to another peripheral, such as a serial port, instead of serving as a GPIO, which is called pin multiplexing;
input interrupt: whether a rising edge, a falling edge or both request an interrupt.
Driving and reading. The registers are memory-mapped I/O §4.35: they sit at fixed addresses, and an ordinary store or load instruction to such an address reaches the peripheral instead of memory. An output data register holds one bit per pin, and an input data register reports each pin’s level. Most microcontrollers §6.1 also have a set register and a clear register. Writing 1 to bit of the set register makes pin high and leaves the other pins unchanged, in one store, with no read-modify-write sequence that other code could interrupt halfway. An operating system adds a device driver §6.38 on top. The Zephyr real-time operating system’s §6.33 gpio_pin_set_dt(), for example, takes a logical value and inverts it for a pin declared active-low, meaning active at the low voltage.
From decision to edge.
Code path: from the deciding instruction to the store that writes the GPIO register. A direct register write needs one or two instructions; a driver call adds a function call, checks and the active-low inversion, tens of cycles or more. Any interrupt handler §6.12 that runs in between adds its whole duration.
Bus transfer: the store crosses the chip’s internal bus, often through a bridge to a slower peripheral bus, in a few clock cycles. The processor may already run the next instructions while the write is on its way.
Output switching: the output register takes the new value and the output driver switches.
Voltage change: the voltage moves at a rate set by the drive strength and by the capacitance of everything attached, the pin, the wire and the probe. For an output with effective resistance driving a capacitance , it covers half the change after and goes from 10% to 90% of it in .
Detection: an oscilloscope triggers when the voltage crosses its threshold. A logic analyzer, which records each signal as 0 or 1 at a fixed sampling rate, sees the edge at its next sample, up to one sampling period late.
From edge to software. For an input, the chain runs the other way. A synchronizer, usually two flip-flops (one-bit storage cells updated on a clock edge), aligns the signal with the peripheral’s clock. An edge detector raises an interrupt request. On a Cortex-M3 or Cortex-M4 §6.3, entering the handler takes 12 clock cycles when memory has no wait states, plus any wait for a handler of higher priority to finish. Then the handler’s own code runs.
Worked example
A latency budget. A processor runs at 64 MHz (megahertz), so one clock cycle is s = 15.625 ns (nanoseconds). The following values are assumed for illustration; a real chip must be measured.
Code path through a driver call: 32 cycles, ns.
Bus transfer: 2 cycles, ns.
Voltage change to the 50% threshold, with and pF (picofarads): s = 2 ns, and ns.
A logic analyzer sampling at 100 MHz takes a sample every 10 ns, so it adds 0 to 10 ns.
The edge appears ns after the decision, and the analyzer reports it between 532.64 and 542.64 ns after the decision. Writing the register directly instead of calling the driver removes most of the 500 ns. An interrupt handler of 320 cycles that runs between the decision and the store delays the edge by ns, which is 5 microseconds.
Bracketing a function with a pulse. Firmware sets a pin high just before a function and low just after it, and an oscilloscope measures the pulse width. Count clock cycles from the store that sets the pin high, at cycle 0. The rising edge appears at cycle 3. The driver call returns and the function starts at cycle 10; it ends at cycle 10,010, so it runs for 10,000 cycles. The call that sets the pin low reaches its store at cycle 10,042, and the falling edge appears at cycle 10,045. The pulse is cycles, about microseconds, while the function took microseconds. The extra 42 cycles are from the rising edge to the function’s start, from the function’s end to the second store, and from that store to the falling edge: . The same two markers around an empty function measure this overhead, so that it can be subtracted, provided that no interrupt hits either measurement.
Stated precisely
Let be the time at which the deciding instruction completes, the time at which the store to the GPIO register is issued, and the time at which the instrument registers the edge. The latency from decision to observed edge is where is the code path, including any interrupt handled in between, and is the sum of bus transfer, output switching, voltage change up to the threshold, and detection delay.
For a pulse made by stores issued at times (high) and (low), with delays and from each store to its observed edge, the observed width is If the two delays are equal, : the pulse measures exactly the time between the two stores, which contains the measured code, the marker code after the first store and before the second, and every interrupt handled in between. This assumes that the compiler kept the two stores and the measured code in source order and that the instrument used the same threshold for both edges.
Limits and common mistakes
A pulse measures the time between two stores, not the work of the code between them. Interrupt handlers and other threads that run inside the pulse widen it, and the marker code itself is instrumentation overhead §11.32.
The compiler may move computation across a marker. Accesses to objects declared with the volatile qualifier §4.34, which tells a C compiler that every access must happen as written, stay in program order relative to each other, but computation that touches no volatile object may move before or after the marker store. Check the generated machine code, or store the measured code’s result to a volatile variable before the second marker.
A completed store instruction is not yet a changed pin: the write can still be crossing the bus while the processor runs on. Reading the same register back is the usual way to wait until the write has arrived.
A read-modify-write of a shared output register can lose an update. If an interrupt handler changes one pin between a thread’s read of the register and its write-back, the thread’s write undoes the handler’s change. Set and clear registers, or interrupts disabled around the sequence, remove this race condition §9.2.
Electrical and board details matter. A pin assigned to another peripheral ignores the GPIO setting; an input that nothing drives and that has no pull resistor floats and reads random values; a voltage above the chip’s supply can damage an input not rated for it; and a mechanical switch bounces, producing several edges for one press. Finally, an edge shows only that the store instruction ran, not that the action it marks succeeded.
Questions answered
What latency lies between a software decision and a pin transition?
The latency between a software decision and a pin transition is the sum of five parts: the code path from the deciding instruction to the store that writes the GPIO (general-purpose input/output) register; the transfer of that write across the chip’s internal bus; the switching of the output driver; the rise or fall of the voltage up to the observer’s threshold; and the observing instrument’s detection delay. On a microcontroller, a direct register write right after the decision adds only a few clock cycles of bus transfer and switching; a call through an operating-system driver adds tens of cycles or more; the voltage change takes nanoseconds and depends on the load; and a logic analyzer adds up to one sampling period.
Only the code path varies much. An interrupt handled between the decision and the store adds its whole duration, with no upper bound unless interrupts are disabled or their worst case is known. To know the latency on a given device, measure it, for example by marking two events on two pins and timing both on one instrument. A pulse made by two such writes measures the time between the two stores, including everything that ran between them, not the work of the bracketed code alone.
Related entries: Measurement uncertainty (§11.33), Memory-mapped I/O (MMIO) (§4.35), Response time versus execution time (§9.32), Instrumentation overhead (§11.32), Interrupt (§6.11).