Why SPI is used in Embedded C - Performance Analysis
We want to understand how the time it takes to communicate using SPI changes as the amount of data grows.
How does SPI handle sending multiple bytes and how does that affect speed?
Analyze the time complexity of the following SPI data transfer code snippet.
void spi_transfer(uint8_t *data, uint16_t length) {
for (uint16_t i = 0; i < length; i++) {
SPI_SendByte(data[i]);
while (!SPI_TransferComplete());
}
}
This code sends each byte of data one by one over SPI, waiting for each byte to finish before sending the next.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending one byte over SPI and waiting for completion.
- How many times: This happens once for each byte in the data array, so 'length' times.
Each byte requires a fixed amount of time to send, so total time grows directly with the number of bytes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 byte sends |
| 100 | 100 byte sends |
| 1000 | 1000 byte sends |
Pattern observation: Doubling the data size doubles the total time needed.
Time Complexity: O(n)
This means the time to send data over SPI grows linearly with the amount of data.
[X] Wrong: "SPI sends all data instantly regardless of size."
[OK] Correct: SPI sends data one byte at a time, so more data means more time.
Understanding how SPI timing grows with data size helps you explain communication speed in embedded systems clearly and confidently.
"What if SPI could send multiple bytes at once using DMA? How would the time complexity change?"