</>EmbeddedInterview

How do you choose and configure the SPI clock?

MediumCommunication Protocols

Short answer: Use the highest clock that the device supports (from its datasheet's maximum SCLK and timing parameters), that the MCU's SPI peripheral can generate (usually the peripheral clock divided by a power of two), and that the board can carry with clean edges (trace length, capacitance, level shifters, connectors). Then check timing: setup and hold times, the device's output delay on MISO at that frequency, and the CS-to-clock delays.

Steps

  1. Device limits: max SCLK (e.g. 10 MHz for a sensor, 104 MHz for a QSPI flash in fast-read mode), often lower for some commands (e.g. reads vs writes, or SD cards at 400 kHz during initialisation).
  2. Peripheral divider: e.g. APB clock 84 MHz with prescalers /2, /4, /8 … → 42, 21, 10.5 MHz: pick the fastest ≤ the device limit.
  3. MISO timing: the device's clock-to-output delay plus board delay must be less than half a clock period before the controller samples (at 50 MHz, half a period is only 10 ns). Some MCUs offer a delayed sample point for this.
  4. Signal integrity: long traces or cables need slower clocks or series termination resistors (22–33 Ω) near the driver; level shifters often limit speed to a few MHz.
  5. Verify with a scope: check edges, ringing and that the clock actually reaches the device cleanly.

Throughput math: at 10 MHz, one byte takes 0.8 µs, but per-byte software overhead (polling flags) can dominate; use DMA and FIFOs for sustained rates, and keep CS asserted across multi-byte transactions.

Low-power and EMI: slower clocks and slower GPIO slew settings reduce emissions and power; use the minimum speed that meets the data-rate requirement.