Simvik

What an SPI mode means

Turn an SPI mode number into the clock's idle level and the edge that samples data. An easy embedded C problem (SPI), run and graded in the browser.

easy · SPI · spi · modes

SPI has four modes, made of two bits: CPOL (bit 1), the clock's idle level, and CPHA (bit 0), which edge samples the data: 0 = the first (leading) edge after chip select, 1 = the second (trailing) edge.

Write spi_mode_info(mode, idle_high, sample_rising): set *idle_high to 1 if the clock idles high, and *sample_rising to 1 if data is sampled on the rising edge, 0 if on the falling edge.

Remember that when the clock idles high, its first edge is a falling one.

Starter code

void spi_mode_info(int mode, int *idle_high, int *sample_rising) {
  *idle_high = (mode >> 1) & 1;
  *sample_rising = !(mode & 1);   // leading edge = rising?
}

Example tests

Modes 0 and 1

show(0);
show(1);

Expected output:

mode 0: idles low, samples on the rising edge
mode 1: idles low, samples on the falling edge

Mode 3

show(3);

Expected output:

mode 3: idles high, samples on the rising edge

Learn it first

  • SPI · Clock modes, chip select, multiple devices and throughput.

More SPI problems