GPIO, general-purpose input and output, is the simplest peripheral an SoC has: a register whose bits drive output pins, and a register that reads the input pins. It is also the first peripheral software touches on a new chip, because a pin that toggles is visible with nothing more than a probe. This article explains the GPIO's two registers, the rules an APB peripheral follows to take a write, and how a program's store becomes a level on a pin. In exercise S11 you write it.

8 min read | Beginner to Intermediate

What You'll Learn

  • Describe the GPIO's OUT and IN registers and their reset values
  • Apply the APB rules a peripheral uses to accept a write and answer a read
  • Explain why a read-only register needs no storage
  • Follow a program's store from the core to the output pins

Before You Start

This section needs Verilog: see the Verilog Tutorial. The AXI4-Lite to APB Bridge explains the APB transfer from the bridge's side; this article is the peripheral's side. Memory-Mapped I/O and Registers explains register kinds and access types, and Peripherals: UART, Timers and GPIO describes GPIO in real chips. No earlier exercise is needed.

Two Registers

The GPIO in cvsoc has 32 output pins and 32 input pins, and two registers in its 4 KB slot at 0x4000_2000:

OffsetRegisterAccessMeaning
0x000OUTread/writedrives gpio_out; 0 after reset
0x004INread onlythe value of gpio_in; writes are ignored
The GPIO: an APB write sets the OUT register, which drives the 32 output pins; a read of IN returns the 32 input pins directly; prdata goes back to the bridge.

OUT is a real register: 32 flip-flops that keep the last value written, and whose outputs are the pins. Its reset value is 0, so the pins are low until software sets them; on a real board, a pin that came up high by accident could switch on a motor or a LED before the software is ready. IN is not stored at all: reading it returns the pins as they are in that cycle. There is nothing to write into, so a write to IN has no effect.

These are the ports, the same APB peripheral ports as every peripheral in cvsoc, plus the pins:

module cvsoc_gpio (
    input  logic        clk,
    input  logic        rst_n,
    input  logic        psel,
    input  logic        penable,
    input  logic        pwrite,
    input  logic [11:0] paddr,
    input  logic [31:0] pwdata,
    output logic [31:0] prdata,
    output logic        pready,
    output logic        pslverr,
    output logic [31:0] gpio_out,   // the output pins
    input  logic [31:0] gpio_in     // the input pins
);

The Peripheral's Side of APB

A peripheral sees a transfer addressed to it while its PSEL is 1. The first cycle is SETUP (PENABLE = 0), and then ACCESS (PENABLE = 1). For a peripheral that always answers at once, the rules are short:

  • A write takes effect at the clock edge of an ACCESS cycle: PSEL, PENABLE and PWRITE all 1. Here, at offset 0x000 only: that writes OUT.
  • A read returns PRDATA in the ACCESS cycle. Producing it combinationally from PADDR is fine: OUT for offset 0x000, the pins for 0x004.
  • PREADY is always 1 and PSLVERR always 0: the GPIO never needs extra cycles and never fails.

Why wait for ACCESS rather than act in SETUP? Because only PSEL and PENABLE together mark the transfer as really happening. In SETUP the bridge is still presenting it; a peripheral that acted then would act on transfers that the protocol has not committed to yet. Acting only when PSEL, PENABLE and PWRITE are all 1 is the standard APB pattern, and the S11 tests check both halves of it: a SETUP cycle without ACCESS must change nothing, and PENABLE with PSEL = 0 (a transfer to another peripheral) must change nothing either.

Quick Check

Q: OUT holds 0x0000_00A5. A program writes 0xFFFF_FFFF to offset 0x004, then reads offset 0x000 and offset 0x004 while the input pins are 0x0000_C0DE. What do the two reads return, and what are the output pins?

Show Answer

The write to IN is ignored, so OUT and the output pins stay 0x0000_00A5. The read of 0x000 returns 0x0000_00A5, and the read of 0x004 returns the input pins, 0x0000_C0DE.

From a Store to a Pin

The program hello contains the line GPIO_OUT = 0xA5;. The compiler turns it into a store to 0x4000_2000. The CPU subsystem sees an address outside the TCM and sends it through the manager port; the interconnect routes it to the bridge; the bridge sees slot 2 and runs an APB write with PADDR = 0x000; and at the edge of the ACCESS cycle, OUT takes 0xA5. From that edge on, pins 0, 2, 5 and 7 of gpio_out are high. The system report of hello shows the result at the end: gpio_out : 0x000000a5. The whole trip takes six cycles, as The AXI4-Lite to APB Bridge showed cycle by cycle.

The same program reads IN once and prints it: the testbench drives the input pins with 0x0000_C0DE, the way a board would tie some pins high and others low, and the terminal shows gpio_in 0000c0de.

Where Is This Used?

Real GPIO blocks have more registers than these two: a direction register per pin (so one pin can be input or output), separate SET and CLEAR registers so that software can change one pin without a read-modify-write, input filtering and interrupt-on-change. Memory-Mapped I/O and Registers explains why SET and CLEAR registers matter when two pieces of software share a port. The core of every one of them is the same pair you write here: a register that drives pins, and a read path from pins.

How It Is Tested

The S11 tests drive the GPIO with APB transfers:

  • the output pins after reset (0);
  • a write to OUT, checked on the pins and by reading OUT back;
  • reads of IN with different values on the input pins;
  • a write to IN (no effect), a SETUP cycle without ACCESS (no effect), and a write with PSEL = 0 (no effect);
  • 200 random reads and writes, and a final reset (hidden test).

Common Mistakes

Mistake #1: Writing in the SETUP cycle

Using PSEL and PWRITE without PENABLE writes OUT one cycle early, and also on a SETUP that is not followed by ACCESS. The test presents exactly such a SETUP cycle.

Mistake #2: Writing at any offset

Leaving PADDR out of the write condition makes a write to IN change OUT, and with it the pins. In a real system that is a pin glitch caused by software that never meant to touch the outputs.

Your Turn in the Lab

Exercise S11: write cvsoc_gpio with the ports above. Save it in your lab project for S16.

Open exercise S11 in ChipVerify Lab

Summary

  • The GPIO has OUT at offset 0x000, a read/write register that drives the 32 output pins and resets to 0, and IN at 0x004, which reads the 32 input pins directly.
  • An APB peripheral takes a write only at the edge of an ACCESS cycle (PSEL, PENABLE and PWRITE all 1), at its own offset.
  • The GPIO answers at once: PREADY is always 1, PSLVERR always 0, and PRDATA can be combinational.
  • A store to 0x4000_2000 crosses the manager port, the interconnect and the bridge; the pins change at the end of the APB ACCESS cycle, the fourth of the access's six cycles.
Did you find what you were looking for?

Quiz

No quiz questions available for this article.