ATmega328P from scratchPart 8 of 12

Build note

SPI: two chips passing notes

SPI is two people passing notes while a third person claps a steady beat. Every clap moves one note each way, and nobody speaks out of turn.

That is not a metaphor for politeness. It is the mechanism. SPI has a shared clock wire, and on every clock pulse each side shifts one bit out and one bit in at the same time.

Four wires

Wire Direction Job
SCK Master to slave One pulse per bit
MOSI Master to slave Data going out
MISO Slave to master Data coming back
SS Master to slave, active low Selects the slave

The clock travels with the data, so both sides stay in step without agreeing on a rate beforehand. That is why SPI reaches megahertz while UART and I2C are much slower.

Two properties cause most of the confusion:

  • It is an exchange, not a send. Every clock pulse sends a bit out on MOSI and brings a bit in on MISO. You always receive something, even if it is meaningless.
  • SS is the frame. There is no start bit and no stop bit. The low period of SS is the whole transaction.

The pins

Signal Port pin Chip pin Arduino pin
SCK PB5 19 D13
MISO PB4 18 D12
MOSI PB3 17 D11
SS PB2 16 D10

The registers

Register Page Job
SPCR 140 Enable, role, bit order, mode, clock divider
SPSR 141 Status flags and double-speed bit
SPDR 142 Write to send, read to receive

SPCR needs SPE to turn the hardware on and MSTR for master mode. SPR1:0 selects the clock divider. CPOL and CPHA select the timing mode, and almost every device uses mode 0, which is both bits at 0.

A transfer is one function

uint8_t spi_transfer(uint8_t data)
{
    SPDR = data;                       // writing starts the transfer
    while (!(SPSR & (1 << SPIF)))      // wait for completion
        ;
    return SPDR;                       // the byte that came back
}

That is the entire master side. The received byte is whatever the slave had loaded, not necessarily an answer to what you just sent.

Two chips, one echoing

The folder uses two ATmega328Ps:

MASTER                  SLAVE
PB5 SCK   ----------->  PB5 SCK
PB3 MOSI  ----------->  PB3 MOSI
PB4 MISO  <-----------  PB4 MISO
PB2 SS    ----------->  PB2 SS
GND       ------------  GND

The slave enables SPI in slave mode and an interrupt:

ISR(SPI_STC_vect)
{
    last_rx = SPDR;
    SPDR = last_rx;    // preload the next answer
}

The master sends an incrementing counter and ignores the reply. The slave echoes whatever it received, one transaction late.

The detail that explains everything

The master, on transfer N, receives the value it sent on transfer N-1. That is not a bug. The slave’s response to byte N cannot be shifted out until byte N+1, because everything moves on the same clock edges.

Once that clicks, the SPDR = last_rx line stops looking odd. The slave is not replying after the fact; it is loading the next byte before the next frame arrives.

The failures that cost time

  • Forgetting SPE. Nothing works at all.
  • The master not driving SS. In master mode SS is a normal GPIO. The hardware does not toggle it for you, so the frame never starts.
  • Wrong mode. If a device returns garbage at a plausible clock rate, check CPOL and CPHA after the wiring.
  • Long wires or too fast a clock. A breadboard fails well before the parts do. Start slow.

Next

The next part takes this exact exchange and reads it with a logic analyser, so you can see the frame, the eight clocks and both data lines at once.

How this was made

  • Written by: Rupesh, from his own c-09-spi notes and code, with AI drafting and editing.
  • Register details: from the ATmega328P datasheet.
  • AI-assisted sections: structure and copy editing.