Sub topic 12
Bulk transfers and getting more throughput
Prerequisites: you should understand setup() and loop(), digital output pins, hexadecimal values, and the roles of SPI data lines. You should also know that SPI is full duplex: every clock pulse sends one bit and receives one bit.
Why transfer a buffer at once?
A byte-by-byte loop can move data correctly, but it adds overhead for every byte. Each function call may perform argument handling and library checks. Repeatedly changing chip select can also break one device command into several frames.
A bulk transfer keeps chip select active while the controller clocks many bytes. The SPI library can handle the repeated byte exchanges inside one call.
#include <SPI.h>
const uint8_t CS_PIN = 10;
const size_t BUFFER_LENGTH = 64;
uint8_t buffer[BUFFER_LENGTH];
void setup() {
Serial.begin(115200);
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH); // Peripheral is deselected.
SPI.begin();
for (size_t i = 0; i < BUFFER_LENGTH; i++) {
buffer[i] = static_cast<uint8_t>(i);
}
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
SPI.transfer(buffer, BUFFER_LENGTH);
digitalWrite(CS_PIN, HIGH);
SPI.endTransaction();
Serial.println("Transfer complete");
}
void loop() {
}SPI.transfer(buffer, BUFFER_LENGTH) transmits the bytes in buffer. At the same time, it receives bytes from MISO and writes those received bytes back into the same buffer. After the call, buffer no longer contains the original transmit data.
This behavior is useful when the transmitted bytes are temporary. If you need to preserve them, use separate transmit and receive arrays, or copy the transmit data after the exchange.
Framing a bulk protocol
Many peripherals expect a command and address before the payload. The command and payload must remain inside the same chip-select frame unless the datasheet specifies otherwise.
For example, a memory device might expect:
- A read command.
- A starting address.
- Several dummy bytes, which produce the returned data.
A bulk call can transfer the payload efficiently, but the command format remains device-specific. Read the datasheet for the required command, address length, dummy bytes, and maximum transfer length.
Measuring throughput
The ideal raw byte rate is approximately:
| SPI clock | Ideal raw rate |
|---|---|
| 4 MHz | 500,000 bytes/s |
The calculation is clock frequency / 8, because eight clock pulses move one byte. Real throughput is lower because of command bytes, chip-select setup, function-call overhead, delays, and device processing time.
A longer transfer usually improves efficiency, but only if the peripheral supports that length. Some devices limit transfers at page boundaries or require a pause while writing internal memory.
Choosing a practical transfer size
Use a buffer that fits the board's memory. Avoid allocating large temporary arrays inside loop(), especially on small Arduino boards. Static or global buffers have predictable storage.
Increase the clock only after confirming that the peripheral, controller, wiring, and voltage levels support it. Check the result with a logic analyzer when data becomes corrupted. Look for gaps between bytes, unexpected chip-select transitions, and a clock rate above the device limit.
Exercise
A 2 MHz SPI clock is used for a 100-byte payload. Ignoring overhead, how long does the payload take to transfer?
Show answer
There are 800 bits. At 2,000,000 bits per second, the transfer takes 800 / 2,000,000 seconds, or 0.4 milliseconds.