Datasheet

Table Of Contents
Though the PIO only has a total of nine instructions, it would be difficult to edit PIO program binaries by hand. PIO
assembly is a textual format, describing a PIO program, where each command corresponds to one instruction in the
output binary. Below is an example program in PIO assembly:
1 .program squarewave
2 again:
3 set pins, 1 [1] ; Drive pin high and then delay for one cycle
4 set pins, 0 ; Drive pin low
5 jmp again ; Set PC to label `again`
The PIO assembler is included with the Pico SDK, and is called pioasm. This program processes a PIO assembly input text
file, which may contain multiple programs, and writes out the assembled programs ready for use. For the Pico SDK these
assembled programs are emitted in form of C headers, containing constant arrays: For more information see Section 3.3
3.2.2. Control Flow
On every system clock cycle, each state machine fetches, decodes and executes one instruction. Each instruction takes
precisely one cycle, unless it explicitly stalls (such as the WAIT instruction). Instructions may also insert a delay of up to 31
cycles before the next instruction is executed to aid the writing of cycle-exact programs.
The program counter, or PC, points to the location in the instruction memory being executed on this cycle. Generally, PC
increments by one each cycle, wrapping at the end of the instruction memory. Jump instructions are an exception and
explicitly provide the next value that PC will take.
1 .program squarewave
2
3 again:
4 set pins, 1 [1] ; Drive pin high, then delay for one cycle
5 set pins, 0 ; Drive pin low
6 jmp again ; Set PC to label `again`
Our example assembly program shows both of these concepts in practice. It drives a 50/50 duty cycle square wave onto
a GPIO, with a period of four cycles. Using some other features (e.g. side-set, section Section 3.5.1) this can be made as
low as two cycles. The system has write-only access to the instruction memory, which is used to load programs:
1 // Load the assembled program into the PIO's instruction memory
2 for (int i = 0; i < count_of(squarewave_program_instructions); ++i)
3 pio->instr_mem[i] = squarewave_program_instructions[i];
The clock divider slows the state machine’s execution by a constant factor, represented as a 16.8 fixed-point fractional
number. Using the above example, if a clock division of 2.5 were programmed, the square wave would have a period of
cycles. This is useful for setting a precise baud rate for a serial interface, such as a UART.
1 // Configure state machine 0 to run at sysclk / 2.5
2 pio->sm[0].clkdiv = (int)(2.5f * 256);
The system can start and stop each state machine at any time, via the CTRL register. Multiple state machines can be
started simultaneously, and the deterministic nature of PIO means they can stay perfectly synchronised.
RP2040 Datasheet
3.2. Programmer’s Model 308