Tutorials

Count PIO Instruction Cycles Before Choosing the RP2040 Clock Divider

Published · Updated

An RP2040 PIO state machine clocked at 10 MHz can produce a 1 MHz square wave without sending ten instructions to instruction memory. The delay fields consume state-machine cycles too.

Count the executed path from one rising edge to the next

Eight-channel USB logic analyser. For a digital timing capture, check the analyser sample rate, input threshold and the ground path for the signal in question.
Eight-channel USB logic analyser. For a digital timing capture, check the analyser sample rate, input threshold and the ground path for the signal in question. Dilshan Jayakody from Maharagama, Sri Lanka (Wikimedia Commons), CC BY-SA 2.0. Site-sized copy.

Write the short program on paper. Mark instructions that change a pin, instructions that occupy time, and conditional branches that may take another path. Ordinary wait-free PIO instructions execute in one state-machine cycle plus any encoded delay.

Hardware wrap can return to the start without an explicit jump cycle. A blocking FIFO access or a WAIT whose condition has not arrived stretches the waveform beyond that nominal cycle count.

Look for missed trigger edges · Check CPOL and CPHA on the captured waveform

Balance five high cycles against five low cycles

SET high with delay 4 / 1 + 4 / 0.5 µs; SET low with delay 4 / 1 + 4 / 0.5 µs; Hardware wrap / 10 total cycles / 1 MHz output
Instruction or setting / State-machine cycles / Nominal interval.

Suppose clk_sys is 125 MHz. An SM divider of 12.5 gives an average 10 MHz state-machine rate. With the output pin configured for SET, execute an instruction to set it high with four additional delay cycles, then one to set it low with the same delay, using hardware wrap.

Ten cycles make a nominal 1 µs period. Because the divider is fractional, individual SM ticks can span different integer numbers of system-clock periods. If edge-to-edge jitter matters, compare an integer-divider design against the timing requirement.

Why does the measured low interval occasionally grow by many microseconds?

Inspect blocking pulls, waits and the executed branch path. Divider quantization can move an edge by system-clock ticks. A stalled state machine can wait until data or an external condition arrives.

Replay the worked exampleSET high with delay 4 / 1 + 4 / 0.5 µs; SET low with delay 4 / 1 + 4 / 0.5 µs; Hardware wrap / 10 total cycles / 1 MHz output
  1. SET high with delay 4 · 1 + 4 · 0.5 µs
  2. SET low with delay 4 · 1 + 4 · 0.5 µs
  3. Hardware wrap · 10 total cycles · 1 MHz output

Separate throughput from transaction latency · Use the board bring-up checklist

Test a counter at its boundaries.

Raspberry Pi RP2040 Datasheet, chapter 3, Programmable I/O.

Photograph credit and licence

Dilshan Jayakody from Maharagama, Sri Lanka (Wikimedia Commons), CC BY-SA 2.0. Site-sized copy.

Original photograph https://commons.wikimedia.org/wiki/File:8_Channel_USB_logic_analyzer_(5172098012).jpg

Licence https://creativecommons.org/licenses/by-sa/2.0

Back to Tutorials

Related evidence and next tasks

Continue the task with the relevant source scope. These reading paths do not establish interchangeability or current supply.

Source addresses and citation trail

Full source addresses are retained below. Read each citation with its document revision, page, table and applicability conditions above.

  1. https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf#page=304