Sub topic 3
How a Transfer Exchanges Bits in Both Directions
Prerequisites and the basic model
This chapter assumes that you know basic Arduino sketches, digital I/O, binary and hexadecimal numbers, and simple bitwise operators. You should also know that SPI uses SCLK, MOSI, MISO, and chip select. This chapter focuses on what happens during one transfer.
SPI is full-duplex. Full-duplex means that the controller sends and receives data at the same time. Each clock pulse moves one bit in each direction:
- The controller places one bit on MOSI.
- The peripheral places one bit on MISO.
- SCLK tells both sides when to sample and shift.
- After eight clock pulses, both sides have exchanged one byte.
The sent byte and received byte do not have to contain related data. A controller might send a command while receiving a status byte. It might also send a dummy byte, such as 0x00, when it only wants to read.
Shift registers and clock pulses
Inside an SPI device, a shift register holds the byte being transmitted or received. On each clock cycle, the register shifts by one position. The outgoing bit leaves through the data line, and the incoming bit enters the other end.
For most-significant-bit first order, bit 7 is sent first, followed by bit 6, down to bit 0. After eight cycles, the received bits form a complete byte.
The controller generates SCLK, so the peripheral cannot send a byte without the controller providing clock pulses. This explains why a read operation usually sends a command first and then sends a dummy byte to create the clocks needed for the response.
Seeing the exchange in code
The Arduino SPI.transfer() function returns the byte received while it sends a byte. In this example, the first transfer sends a register-read command. The second sends 0x00 only to generate clock pulses.
#include <SPI.h>
const uint8_t CS_PIN = 10;
const uint8_t READ_FLAG = 0x80;
uint8_t readRegister(uint8_t reg) {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
SPI.transfer(reg | READ_FLAG); // Received byte is ignored.
uint8_t value = SPI.transfer(0x00); // Dummy output; response is collected.
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
return value;
}
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
SPI.begin();
}
void loop() {
Serial.println(readRegister(0x0F), HEX);
delay(1000);
}The command format is device-specific. The peripheral may return an undefined value during the command byte because it has not yet decoded the command. Its useful response may begin during the next byte.
Common mistakes
- Expecting the first received byte to contain the answer when the datasheet defines a command-then-response sequence.
- Forgetting that
SPI.transfer()returns received data. - Sending the least-significant bit first when the peripheral expects most-significant bit first.
- Assuming one transfer call always equals one device-level message. The datasheet may require several bytes while chip select remains active.
Exercise
Why does a controller send a dummy byte when reading a peripheral register?
Show answer
The dummy byte produces the clock pulses that let the peripheral shift its response onto MISO.