Sub topic 3
How a transfer exchanges bits in both directions
This chapter assumes you know the four SPI signals (SCLK, MOSI, MISO and chip select) from the previous chapter. You should also be comfortable with bitwise operators and hexadecimal. Here the question is what happens on those wires during one byte. The answer explains why SPI.transfer() returns a value, why reading a register needs a dummy byte, and why a read reply arrives one byte late.
Two shift registers joined in a ring
An SPI controller and an SPI peripheral each contain a shift register. This is a row of flip-flops (one-bit memory cells) that moves every stored bit one position on each clock pulse. For an 8-bit transfer, each side has an 8-bit register.
The two registers are wired into a loop. The controller's output bit goes to the peripheral through MOSI. The peripheral's output bit goes to the controller through MISO. SCLK is the controller's clock and moves both registers at the same time.
On each clock cycle, one bit leaves each register and one bit enters it. After 8 cycles, each register holds the byte that the other side started with. The two bytes have swapped places.
This is why SPI is full duplex: data moves in both directions during the same clock pulses. There is no separate "send phase" and "receive phase". Every transfer is both a send and a receive.
A traced example
Take mode 0, most significant bit first. The controller loads 0xA5 (binary 10100101) and the peripheral loads 0x3C (binary 00111100). The table shows each clock cycle.
| Clock | MOSI bit (controller to peripheral) | MISO bit (peripheral to controller) | Controller has received | Peripheral has received |
|---|---|---|---|---|
| 1 | 1 | 0 | 0 | 1 |
| 2 | 0 | 0 | 00 | 10 |
| 3 | 1 | 1 | 001 | 101 |
| 4 | 0 | 1 | 0011 | 1010 |
| 5 | 0 | 1 | 00111 | 10100 |
| 6 | 1 | 1 | 001111 | 101001 |
| 7 | 0 | 0 | 0011110 | 1010010 |
| 8 | 1 | 0 | 00111100 | 10100101 |
After clock 8, the controller holds 0x3C and the peripheral holds 0xA5. SPI.transfer(0xA5) returns 0x3C.
Two points follow from this:
- The number of bits sent always equals the number of bits received. One byte out means one byte in.
- The controller creates all the clock pulses. A peripheral cannot send anything unless the controller clocks it.
What SPI.transfer() does
In the Arduino SPI library, SPI.transfer(value) loads value into the controller's shift register, starts 8 clock pulses, waits for them to finish, and returns the byte that was shifted in. The function blocks until the byte is complete.
You can see the ring directly with a loopback test. Connect MOSI to MISO with one jumper wire (pins 11 and 12 on an Arduino Uno). The controller's output bit then feeds its own input, so every byte you send comes back unchanged.
#include <SPI.h>
void setup() {
Serial.begin(115200);
SPI.begin();
}
void loop() {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
uint8_t got = SPI.transfer(0xA5);
SPI.endTransaction();
Serial.print("sent A5, got ");
Serial.println(got, HEX); // prints A5 with the jumper in place
delay(1000);
}Remove the jumper and MISO floats. The result is then unpredictable, often 0x00 or 0xFF, or a changing value. That behavior is the symptom of a floating input line, so keep it in mind when you debug.
The same exchange in software
The hardware SPI block does this work for you. A bit-banged version makes the mechanism visible. This function performs a mode 0 transfer, most significant bit first.
// Bit-banged SPI mode 0 (CPOL=0, CPHA=0), most significant bit first.
// Data is placed on MOSI while SCK is low and sampled on the rising edge.
const uint8_t SCK_PIN = 13;
const uint8_t MOSI_PIN = 11;
const uint8_t MISO_PIN = 12;
uint8_t softTransferMode0(uint8_t out) {
uint8_t in = 0;
for (int8_t bit = 7; bit >= 0; bit--) {
digitalWrite(MOSI_PIN, (out >> bit) & 1); // set up data while the clock is low
digitalWrite(SCK_PIN, HIGH); // rising edge: both sides sample
in = (in << 1) | (digitalRead(MISO_PIN) ? 1 : 0);
digitalWrite(SCK_PIN, LOW); // falling edge: the next bit is shifted out
}
return in;
}
void setup() {
pinMode(SCK_PIN, OUTPUT);
pinMode(MOSI_PIN, OUTPUT);
pinMode(MISO_PIN, INPUT);
digitalWrite(SCK_PIN, LOW); // idle low for CPOL = 0
}
void loop() {}The loop mirrors the ring. Each pass puts one outgoing bit on MOSI and pulls one incoming bit off MISO. The expression (in << 1) | bit shifts the received value left and adds the new bit at the bottom, exactly as the controller's register does. The next chapter explains why the sample happens on the rising edge in this mode.
Why reading needs a dummy byte
Most peripherals are register-based. The controller sends a command byte that names a register. The peripheral then returns that register's contents. The ring shows why this takes two bytes.
While the controller shifts out the command byte, the peripheral is shifting out whatever was in its register. It cannot yet know what was asked, because it has not received all 8 bits. So the byte returned during the command is meaningless. The real reply can only start in the next byte.
To receive that reply, the controller must generate 8 more clock pulses. It has nothing to send, so it sends a dummy byte, usually 0x00. The dummy value is ignored by the peripheral. Its only job is to produce clock pulses.
#include <SPI.h>
const uint8_t CS_PIN = 10;
const uint8_t READ_FLAG = 0x80; // device-specific: many chips use the top address bit to mean read
uint8_t readRegister(uint8_t reg) {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
SPI.transfer(reg | READ_FLAG); // command byte: register address plus read flag
uint8_t value = SPI.transfer(0x00); // send a dummy byte to clock the reply out
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
return value;
}
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH); // keep the peripheral deselected
SPI.begin();
}
void loop() {
Serial.println(readRegister(0x0F), HEX); // register address is an example; take it from your datasheet
delay(1000);
}The first SPI.transfer() call returns a value, but the code throws it away. The second call returns the register contents. Read the two exchanges as a timeline:
| Byte | Controller sends | Peripheral sends | Useful data? |
|---|---|---|---|
| 1 | Command: address with read flag | Not meaningful | Controller to peripheral only |
| 2 | Dummy `0x00` | Register contents | Peripheral to controller only |
Both bytes travel in both directions. Each one is simply useful in one direction only.
Writes and read-only devices
A write works the same way in reverse. The controller sends the command and then the data byte. The peripheral usually returns nothing useful during those bytes, so you ignore the return values:
SPI.transfer(reg & 0x7F); // write command: read flag cleared on this device
SPI.transfer(data); // return values are ignoredSome peripherals have only the pins they need:
- A write-only device, such as many LED drivers and some displays, has no MISO pin. The controller still receives bytes in its register, but there is no wire connected, so they carry no information.
- A read-only device, such as some ADCs (analog-to-digital converters), may have no MOSI pin. The controller must still clock it, and whatever it sends on MOSI goes nowhere.
In both cases the hardware still shifts 8 bits each way. The unused direction is just not wired up.
Edge cases to watch for
- Latency bytes. Some chips need more than one dummy byte before data appears. Serial flash memory, for example, takes a command, then an address, and sometimes extra wait bytes before it returns data. Count the bytes the datasheet specifies.
- MISO when deselected. A peripheral should release MISO (set it to high impedance, meaning it neither drives high nor low) when its chip select is high. Otherwise several peripherals would fight over one shared line. A device that does not do this can corrupt reads from other devices.
- No framing in the shift register. The ring has no idea where a message begins or ends. Chip select defines the frame, and the next chapters cover it.
- Read order. The first bit received is the most significant one in
MSBFIRSTmode. If your byte looks reversed, the bit order setting is wrong.
Summary of the model
The ring has four consequences you can use when reasoning about any SPI code:
- Every clock pulse moves one bit each way.
- Every
SPI.transfer()returns a byte, even if you do not need it. - Sending a byte always costs the same time as receiving one.
- A reply to a command arrives in a later byte, so the controller sends dummy bytes to collect it.
Exercise
A peripheral expects a 1-byte command, then a 1-byte register address, and then it returns 2 data bytes. How many bytes does the controller clock in total, which of the returned bytes are meaningful, and what should the controller send while the 2 data bytes arrive?
Show answer
The controller clocks 4 bytes in one chip-select frame: command, address, data 1, data 2. The bytes returned during the command and address are not meaningful, because the peripheral is still receiving them. The last two returned bytes are the data. While those two arrive, the controller sends 2 dummy bytes (for example 0x00), because it must generate the clock pulses but has no data to send.
Exercise
You wire MOSI directly to MISO and call SPI.transfer(0x5A). What value does the call return, and why? What changes if you unplug the jumper?
Show answer
It returns 0x5A. The bit the controller shifts out on MOSI comes straight back in on MISO, so after 8 clocks the receive register holds the byte that was sent. With the jumper removed, MISO floats, so the returned value is unpredictable (often 0x00 or 0xFF).