Sub topic 16
Daisy chaining, dual SPI and quad SPI
This chapter covers the variants of SPI that change how many devices share a chip-select line or how many data lines carry a transfer. It assumes you already know the four SPI signals, the four modes, SPISettings, transactions and chip-select framing. It also assumes you are comfortable with bitwise operators and with reading a datasheet timing diagram.
Why variants exist
Standard SPI has one clock line, one data line per direction and one chip select per peripheral. Two limits follow from that:
- Each extra peripheral costs one more controller pin for chip select.
- Throughput is bounded by the clock rate multiplied by one bit per clock in each direction.
Daisy chaining attacks the first limit. Dual and quad SPI attack the second. They are different techniques, and a given chip rarely supports both.
Daisy chaining
In a daisy chain, the peripherals form a ring of shift registers. The controller's MOSI goes to the first device's input. Each device's output goes to the next device's input. The last device's output returns to the controller's MISO. SCLK and chip select are shared by every device.
The controller treats the whole chain as one long shift register. With three 8-bit devices, one frame is 24 clock pulses. The first byte sent ends up in device 3, and the last byte sent ends up in device 1. When chip select rises, every device latches the bits it holds at the same moment.
Typical chips that work this way are 74HC595 output shift registers, MAX7219 LED drivers and many LED and DAC drivers. The datasheet will say "daisy chain" or "cascade" if it is supported. A device that does not support it will not pass its input on to an output.
Example: three chained 74HC595 registers
The 74HC595 latches its outputs on a rising edge of a separate latch pin, which plays the role of chip select here. The sketch below sends three bytes in one frame. The first byte sent lands in the last register of the chain.
#include <SPI.h>
const uint8_t LATCH_PIN = 10; // wired to RCLK on every 595
const uint8_t CHAIN_LENGTH = 3;
// data[0] ends up in the register nearest the controller,
// data[CHAIN_LENGTH - 1] in the register farthest away.
void writeChain(const uint8_t *data) {
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
digitalWrite(LATCH_PIN, LOW);
// Send the farthest device's byte first, because it travels the whole chain.
for (int8_t i = CHAIN_LENGTH - 1; i >= 0; i--) {
SPI.transfer(data[i]);
}
digitalWrite(LATCH_PIN, HIGH); // all outputs update together
SPI.endTransaction();
}
void setup() {
pinMode(LATCH_PIN, OUTPUT);
digitalWrite(LATCH_PIN, HIGH);
SPI.begin();
}
void loop() {
uint8_t pattern[CHAIN_LENGTH] = {0x01, 0x02, 0x04};
writeChain(pattern);
delay(500);
}Trade-offs of daisy chaining
- You save chip-select pins. Ten devices need one chip select instead of ten.
- You cannot address one device alone. To change one register you must resend data for every device in the chain, so you need to keep a copy of each device's current state.
- Latency and bus time grow linearly with chain length.
- A broken device or a poor connection breaks every device after it.
- Reading back works only after the data has been shifted through the whole chain. The reply you read corresponds to what was sent one frame earlier, or to the register contents, depending on the part.
For chips with real command protocols, such as the MAX7219, each device expects a command word. A device you want to leave unchanged is sent a no-op command, and you must send one for each position in the chain.
Dual SPI and quad SPI
Standard SPI uses MOSI for one direction and MISO for the other. Dual and quad SPI reuse the data pins so that several bits move per clock in the same direction.
| Mode | Data lines | Bits per clock | Direction |
|---|---|---|---|
| Standard SPI | MOSI and MISO | 1 each way | Full duplex |
| Dual SPI | IO0 and IO1 | 2 | One direction at a time (half duplex) |
| Quad SPI (QSPI) | IO0 to IO3 | 4 | One direction at a time (half duplex) |
In dual mode, MOSI becomes IO0 and MISO becomes IO1. Both pins are driven by whichever side is sending. Quad mode adds IO2 and IO3. On many flash chips those pins are normally write protect and hold or reset, so they have a second function until quad mode is enabled.
Because the lines switch direction, the transfer is half duplex. A typical read from a quad flash chip runs through these phases within one chip-select frame:
- The controller sends the command byte, often still on one line.
- The controller sends the address, on one, two or four lines depending on the command.
- The bus turns around, and a number of dummy clock cycles gives the device time to fetch data.
- The device sends data on two or four lines.
The command set determines which phases use how many lines. Flash datasheets name these commands by pattern, for example "Fast Read Dual Output", "Fast Read Quad I/O" and "Quad Page Program". The datasheet table of instructions states the lines used for the command, address and data phases, and the number of dummy clocks. The dummy count often depends on the clock rate.
Throughput scales with the number of lines. At 40 MHz, standard SPI reads at most 5 MB/s from one line. Quad mode reads up to 20 MB/s at the same clock, ignoring command overhead.
What the Arduino SPI library supports
The Arduino SPI class drives MOSI and MISO only, so it cannot do dual or quad transfers. Using them requires one of these:
- A microcontroller with a dedicated QSPI or OctoSPI peripheral, such as the RP2040 (via its PIO or XIP flash interface), STM32, nRF52 and ESP32 families. These need the vendor's library or the core's own API.
- Bit-banging with direct port access. This is rarely worth it, since the main purpose of quad SPI is speed.
Many boards already use quad SPI internally to execute code from external flash, so you benefit from it without writing any quad code. If your goal is to read an external flash chip from the Arduino SPI library, the standard single-line read command works on almost all such chips, only slower.
Dual SPI versus quad SPI on a simple chip
Dual and quad support is a property of the specific chip and command, not of the bus. A chip may accept dual commands but not quad commands, and it may only accept multi-line transfers for data while the command and address stay on one line. Check three things in the datasheet:
- Which instructions use more than one line, and for which phase.
- How many dummy cycles each instruction needs at your clock rate.
- Whether a configuration bit must be set first.
Failure modes and gotchas
- Chain length mismatch. If the code sends fewer bytes than the chain holds, the last devices receive shifted data. If it sends more, the first bytes fall off the end.
- Latch timing. Raising chip select too early latches a partial frame. Raising it before the last clock edge has finished corrupts the final bit.
- Wrong byte order. The byte sent first goes farthest down the chain. Reversing the loop swaps the devices.
- Missing dummy cycles. In dual and quad reads, too few dummy clocks give data that is shifted by a few bits or entirely wrong.
- Pin conflicts. Quad-capable chips often tie IO2 and IO3 to write protect and hold. If you use a single-line library, leave those pins pulled high so the chip is neither write protected nor held.
- Signal integrity. Quad lines at high clock rates need short traces of similar length. Breadboard wiring that works at 1 MHz may fail at 40 MHz.
Exercise
You have four daisy-chained 8-bit output shift registers and want to change only the bits in the second register from the controller. What must the sketch send, and why?
Show answer
It must send four bytes in one chip-select frame, sending the byte for the farthest register first. Three of the bytes are the current states you already stored for the other registers, and one is the new value for the second register. A daisy chain cannot address one device alone, so every device receives and latches a byte at the same time. Without a stored copy of the other states, those registers would be overwritten with wrong values.
Exercise
A flash chip datasheet lists a "Fast Read Quad Output" command with 8 dummy clocks. Your sketch uses the Arduino SPI library on a standard board. Can you use that command, and what can you do instead?
Show answer
No. The Arduino SPI library drives only MOSI and MISO, so it cannot read on four lines. Use the standard single-line read command (often 0x03 or 0x0B) with the same chip-select framing. It is slower, but works. To use the quad command you need a controller with a QSPI peripheral and a library that supports it, and you must also enable the chip's quad enable bit first.