Understanding RP2350 PIO
The PIO (Programmable I/O) blocks are what make the RP2350 (and the RP2040 before it) different from almost every other microcontroller. Instead of a fixed set of peripherals, it has small, very fast state machines that run their own programs and drive or read GPIO pins with exact, cycle-by-cycle timing. With PIO you can build interfaces the chip doesn't have in hardware (an extra UART, WS2812 LEDs, CAN, VGA, DPI displays, logic analysers), all without loading the CPU.
This page follows a series of videos and examples that learn PIO step by step, starting from nothing. All examples run on a Streamline MCU RP2354 board in a Streamline backplane next to a Streamline Connect, which flashes the board over SWD and carries the UART console back to the PC. The source is in the rpfun repository, in the examples/pio_* folders.
What PIO is
The RP2350 has 3 PIO blocks (the RP2040 had 2). Each block contains:
- 4 state machines, so 12 in total. Each one runs a program on its own, with its own clock divider.
- 32 instructions of program memory, shared by the 4 state machines in that block. Several state machines can run the same program, or different programs side by side as long as they fit.
- A 4-word TX FIFO and a 4-word RX FIFO per state machine for passing data to and from the CPU (or DMA). The two can be joined into a single 8-word FIFO in one direction.
- 8 IRQ flags that state machines use to signal each other and the CPU.
Each state machine is a tiny processor with very few registers:
| Register | Purpose |
|---|---|
OSR |
Output Shift Register: data pulled from the TX FIFO, shifted out a few bits at a time |
ISR |
Input Shift Register: bits shifted in, then pushed to the RX FIFO |
X, Y |
Two 32-bit scratch registers, mostly used as loop counters |
PC |
Program counter |
Why the timing is exact
Every instruction takes exactly one PIO clock cycle. There are no interrupts, caches or bus waits inside a state machine; an instruction only stalls when it deliberately waits (for a FIFO, a pin or an IRQ flag). On top of that, each instruction can:
- add a delay of extra idle cycles, written in square brackets, e.g.
[31], and - change a few pins for free with side-set, in the same cycle as the instruction itself.
Delay and side-set share a 5-bit field in the instruction, so the more side-set pins a program uses, the shorter the maximum delay (31 cycles with no side-set, 15 with one side-set pin, and so on).
The PIO clock is the system clock divided by a clock divider with a 16-bit integer and 8-bit fractional part (1 to 65535 + 255/256). At the stock 150 MHz a state machine can therefore run anywhere between 150 million and about 2289 instructions per second. A fractional divider gives the right average rate, but individual cycles jitter by one system clock.
The instruction set
There are only 9 instructions:
| Instruction | What it does |
|---|---|
jmp |
Jump, always or on a condition: !x, x--, !y, y--, x!=y, pin, !osre (output shift register not empty)
|
wait |
Stall until a GPIO, a pin or an IRQ flag reaches a given level |
in |
Shift bits into the ISR (from pins, x, y, zeros, ...)
|
out |
Shift bits out of the OSR (to pins, pin directions, x, y, the PC, ...)
|
push |
Move the ISR into the RX FIFO |
pull |
Move a word from the TX FIFO into the OSR |
mov |
Copy between registers and pins, optionally inverted or bit-reversed |
irq |
Set or clear an IRQ flag, optionally waiting for it to be cleared |
set |
Write a constant (0-31) to pins, pin directions, x or y
|
nop is not a real instruction; the assembler turns it into mov y, y. With autopush and autopull switched on, the state machine pushes and pulls by itself whenever the configured number of bits has been shifted, so a program doesn't need explicit push/pull instructions.
What is new on the RP2350
The RP2350's PIO is mostly the same as the RP2040's, with a few useful additions[1]:
- A third PIO block (4 more state machines).
- GPIO base: the RP2350B (and so the RP2354B on the Streamline MCU RP2354) has 48 GPIOs, but a PIO block can still only see 32 at a time: either GPIO 0-31 or GPIO 16-47. See The GPIO base below.
- Random-access RX FIFO: the RX FIFO can be used as 4 registers that the state machine reads or writes by index (
mov rxfifo[y], isr,mov osr, rxfifo[0]), and the CPU accesses directly. Handy for status values or lookup tables. - IRQ flags across blocks:
irqandwait irqcan use the flags of the previous or next PIO block (prev/next), and state machines in different blocks can be started in sync. wait jmppin: wait on the pin used byjmp pin.
Tools
PIO programs are written in PIO assembly in a .pio file. The Pico SDK's pioasm assembler turns that into a C header with the instructions and a default configuration, and CMake runs it as part of the build:
# Assemble blink.pio into blink.pio.h with pioasm
pico_generate_pio_header(main
${CMAKE_CURRENT_LIST_DIR}/blink.pio
)
target_link_libraries(main
pico_stdlib
hardware_pio
)
A .pio file can also contain a block of C between % c-sdk { and %}, which pioasm copies into the generated header. That is the usual place for the function that sets up a state machine for the program.
Lesson 1: blink an LED without the CPU
The first example, pio_1_blink, does the same as the very first blink example, but with a state machine toggling the LED instead of the CPU.
The PIO program
.program blink
.define public CYCLES_PER_BLINK 2052
.wrap_target
set pins, 1 ; LED on
set x, 31 ; loop counter: 31, 30, ... 0 = 32 rounds
on_delay:
jmp x-- on_delay [31]
set pins, 0 ; LED off
set x, 31
off_delay:
jmp x-- off_delay [31]
.wrap ; jump back to .wrap_target for free (0 cycles)
Six instructions. The only new ideas are:
set pins, 1drives the pin(s) configured as "set pins" high.jmp x-- labeljumps if X is not zero, and decrements X either way. Starting from 31, it loops 32 times.[31]adds 31 idle cycles, so each round of the loop takes 32 cycles..wrap_target/.wrapmake the program loop forever without spending a cycle on a jump.
Counting the cycles for one half of the blink:
| Instruction | Cycles |
|---|---|
set pins |
1 |
set x, 31 |
1 |
jmp x-- [31], 32 rounds |
32 × 32 = 1024 |
| Total | 1026 |
A full blink (on and off) is 2052 PIO clock cycles, exported to C as blink_CYCLES_PER_BLINK.
Setting up the state machine
The C part of blink.pio hands the pin to PIO, configures it, and calculates the clock divider for the requested blink rate:
static inline float blink_program_init(PIO pio, uint sm, uint offset, uint pin, float blink_hz) {
// Hand the pin over to this PIO block and make it an output
pio_gpio_init(pio, pin);
pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
pio_sm_config c = blink_program_get_default_config(offset);
// 'set pins' drives 1 pin, starting at 'pin'
sm_config_set_set_pins(&c, pin, 1);
// PIO clock = system clock / divider. Max divider is 65535 + 255/256.
float div = (float)clock_get_hz(clk_sys) / (blink_hz * blink_CYCLES_PER_BLINK);
sm_config_set_clkdiv(&c, div);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
return div;
}
main() is the usual setup from the other examples: UART at 921600 baud and a 1 ms systick from a repeating timer (only core 0 is used). It lets the SDK find a PIO block and state machine and starts the program, and then the CPU is free. The main loop never touches the LED; it just counts how many times it gets round between ticks, to show that the CPU is busy with something else:
PIO pio;
uint sm;
uint offset;
bool ok = pio_claim_free_sm_and_add_program_for_gpio_range(&blink_program, &pio, &sm, &offset, PICO_DEFAULT_LED_PIN, 1, true);
hard_assert(ok);
float div = blink_program_init(pio, sm, offset, PICO_DEFAULT_LED_PIN, BLINK_HZ);
uint32_t now, loop_cnt = 0, next_tick = TICK_DELAY;
// Main loop for Core 0: the LED is not touched here, PIO does all the blinking
while (true) {
now = systick;
if (now >= next_tick) {
printf("Core 0 tick %lu (loop = %lu)\n", now, loop_cnt);
loop_cnt = 0;
next_tick = now + TICK_DELAY;
}
++loop_cnt;
tight_loop_contents();
}
The output on the Streamline MCU RP2354 (921600 baud). The loop count shows the CPU going round its loop about 30 million times a second while the LED blinks:
Core 0: Booting... Running on Arm Cortex-M33 at 150 MHz PIO2, state machine 0, program at offset 26 (6 instructions) GPIO base: 16, LED on GPIO 37 Clock divider: 36549.71 -> PIO clock 4104.0 Hz -> blink 2.00 Hz Core 0 tick 1000 (loop = 29899344) Core 0 tick 2000 (loop = 29947417) Core 0 tick 3000 (loop = 29947835)
Two things are worth noticing:
- The SDK picked PIO2, not PIO0, and loaded the program at offset 26, at the top of the program memory.
pio_claim_free_sm_and_add_program_for_gpio_range()looks for any block with room for the program and a free state machine; don't assume it will be PIO0, state machine 0. - The slowest this program can blink is set by the clock divider: at 150 MHz, 150 000 000 / (65536 × 2052) ≈ 1.1 Hz. For slower blinking the program needs longer loops (for example a larger value in X, pulled from the FIFO), which is where later lessons pick up.
The GPIO base
On the Streamline MCU RP2354 the LED is on GPIO 37. A PIO block can only reach 32 GPIOs at a time, so to drive GPIO 37 the block's GPIO base has to be 16 (GPIO 16-47) instead of the default 0 (GPIO 0-31). The last argument of pio_claim_free_sm_and_add_program_for_gpio_range() (true) lets the SDK set that up. Done by hand, it is:
pio_set_gpio_base(pio, 16); // This block now sees GPIO 16-47
The GPIO base belongs to the whole block, so all 4 state machines in it share the same window. A program that needs both GPIO 5 and GPIO 40 can't run in one state machine; it has to be split across blocks with different bases. On the RP2350A (and the RP2040), with only 30 GPIOs, this never comes up.
Planned lessons
The lessons are planned roughly in this order; it will change as the series goes on.
| # | Example | Topic |
|---|---|---|
| 1 | pio_1_blink | What PIO is, set, delays, loops with jmp x--, the clock divider, the GPIO base
|
| 2 | - | Square waves: side-set, .wrap, working out the output frequency, measured on a scope
|
| 3 | - | Passing data from the CPU: pull, out, autopull; PWM by hand
|
| 4 | - | UART transmitter |
| 5 | - | UART receiver: wait, in, jmp pin, autopush
|
| 6 | - | The WS2812 driver from the WS2812 examples, line by line |
| 7 | - | Feeding PIO with DMA |
| 8 | - | Several state machines working together: IRQ flags and synchronised start |
| 9 | - | A PIO logic analyser |
| 10 | - | PIO at 624 MHz (see RP2350 Extreme Overclock): the fastest square wave, and where the GPIO pads stop keeping up |
References
- RP2350 datasheet, chapter 11 (PIO)[1]
- Pico SDK hardware_pio documentation
- ↑ 1.0 1.1 RP2350 Datasheet, Raspberry Pi Ltd.