Tutorials

Why an RP2040 SPI Request for 8 MHz Can Yield 7.8125 MHz

Published · Updated

An SPI peripheral with an 8 MHz maximum does not need a driver that prints exactly 8,000,000. It needs an SCK rate within its timing limits, with enough throughput after chip-select gaps and software overhead.

Start with clk_peri and the two hardware divisors

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.

The RP2040 SPI controller divides its peripheral clock by an even prescaler and by one plus SCR. Read the actual peripheral clock for this application instead of assuming the CPU and peripheral clocks are identical. The Pico SDK returns the baud rate it configured.

Preserve that return value during bring-up and compare it with a capture. A correct SCK frequency still leaves mode, rise time and the peripheral’s setup and hold margins to check.

Check CPOL and CPHA on the captured waveform · Separate throughput from transaction latency

Fit an 8 MHz ceiling to a 125 MHz peripheral clock

2 × (7 + 1) / 7.8125 MHz / Within 8 MHz; 2 × (6 + 1) / 8.929 MHz / Above ceiling; 256 bytes at 7.8125 MHz / 2,048 clocks / 262.144 µs
Divider choice / SCK / Decision.

The required total division is at least 125/8, or 15.625. Prescale 2 with SCR 7 divides by 16, giving 7.8125 MHz. A division of 14 would produce about 8.93 MHz and exceed the assumed ceiling.

At 7.8125 MHz, 256 eight-bit data bytes take 262.144 µs on the clock line before framing overhead. Measure the entire chip-select transaction if the real requirement is a block deadline. The gaps can matter more than the 2.34% difference from the requested clock.

The measured transaction is 320 µs. Is the SPI divider wrong?

Measure a run of adjacent SCK edges separately from the full chip-select window. Inter-word gaps, command bytes or firmware delays can account for the remaining time.

Trace the three worksheet rows2 × (7 + 1) / 7.8125 MHz / Within 8 MHz; 2 × (6 + 1) / 8.929 MHz / Above ceiling; 256 bytes at 7.8125 MHz / 2,048 clocks / 262.144 µs
  1. 2 × (7 + 1) · 7.8125 MHz · Within 8 MHz
  2. 2 × (6 + 1) · 8.929 MHz · Above ceiling
  3. 256 bytes at 7.8125 MHz · 2,048 clocks · 262.144 µs

Budget FIFO capacity during a service stall · Use the board bring-up checklist

Bound queue growth during a storage stall.

Raspberry Pi RP2040 Datasheet, 4.4 SPI.

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=478