SPI master-slave architecture in Embedded C - Time & Space Complexity
We want to understand how the time taken by SPI communication changes as we send more data.
How does the number of bytes affect the total communication time?
Analyze the time complexity of the following SPI master transmit code.
void SPI_MasterTransmit(char *data, int length) {
for (int i = 0; i < length; i++) {
SPDR = data[i];
while (!(SPSR & (1 << SPIF))) {}
}
}
This code sends each byte one by one from the master to the slave using SPI.
Look at what repeats in the code.
- Primary operation: Sending one byte over SPI inside a loop.
- How many times: The loop runs once for each byte in the data array.
As you send more bytes, the total time grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 byte sends |
| 100 | 100 byte sends |
| 1000 | 1000 byte sends |
Pattern observation: Doubling the data doubles the time because each byte is sent one after another.
Time Complexity: O(n)
This means the time grows directly with the number of bytes sent.
[X] Wrong: "Sending more bytes takes the same time as sending one byte."
[OK] Correct: Each byte requires its own send cycle, so more bytes mean more time.
Understanding how communication time grows helps you design efficient embedded systems and explain your reasoning clearly.
What if we added a delay between sending each byte? How would the time complexity change?