Datasheet

Table Of Contents
4.
Whether side-set writes to GPIO levels or GPIO directions. Configured by EXECCTRL_SIDE_PINDIR
In the above example, we have only one side-set data bit, and every instruction performs a side-set, so no enable bit is
required. SIDESET_COUNT would be 1, SIDE_EN would be false. SIDE_PINDIR would also be false, as we want to drive the clock
high and low, not high- and low-impedance. SIDESET_BASE would select the GPIO the clock is driven from.
3.5.2. Program Wrapping
PIO programs often have an "outer loop": they perform the same sequence of steps, repetitively, as they transfer a stream
of data between the FIFOs and the outside world. The square wave program from the introduction is a minimal example
of this:
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 main body of the program drives a pin high, and then low, producing one period of a square wave. The entire program
then loops, driving a periodic output. The jump itself takes one cycle, as does each set instruction, so to keep the high and
low periods of the same duration, the set pins, 1 has a single delay cycle added, which makes the state machine idle for
one cycle before executing the set pins, 0 instruction. In total, each loop takes four cycles. There are two frustrations
here:
The JMP takes up space in the instruction memory that could be used for other programs
The extra cycle taken to execute the JMP ends up halving the maximum output rate
As the Program Counter (PC) naturally wraps to 0 when incremented past 31, we could solve the second of these by filling
the entire instruction memory with a repeating pattern of set pins, 1 and set pins, 0, but this is wasteful. State machines
have a hardware feature, configured via their EXECCTRL control register, which solves this common case.
1 .program squarewave_wrap
2
3 .wrap_target
4 set pins, 1 [1] ; Drive pin high and then delay for one cycle
5 set pins, 0 [1] ; Drive pin low and then delay for one cycle
6 .wrap
After executing an instruction from the program memory, state machines use the following logic to update PC:
1.
If the current instruction is a JMP, and the Condition is true, set PC to the Target
2.
Otherwise, if PC matches EXECCTRL_WRAP_TOP, set PC to EXECCTRL_WRAP_BOTTOM
3.
Otherwise, increment PC, or set to 0 if the current value is 31.
The .wrap_target and .wrap assembly directives are essentially labels. They export constants which can be written to the
WRAP_BOTTOM and WRAP_TOP control fields, respectively:
1 // --- squarewave_wrap ---
2
3 static const uint16_t squarewave_wrap_program[] = {
4 0xe101, // 00
5 0xe100, // 01
6 };
7
RP2040 Datasheet
3.5. Functional Details 333