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?
Practice
Solution
Step 1: Understand SPI roles
In SPI, the master initiates communication and controls timing.Step 2: Identify clock control
The master generates the clock signal to synchronize data transfer.Final Answer:
The master device controls the clock signal. -> Option AQuick Check:
Master controls clock = A [OK]
- Thinking slave controls clock
- Assuming no clock is needed
- Believing clock is shared equally
Solution
Step 1: Recall SPI slave selection method
In SPI, the master selects a slave by pulling its SS pin LOW.Step 2: Understand pin logic
Setting SS LOW activates the slave; HIGH deselects it.Final Answer:
Set the slave select (SS) pin LOW to select the slave. -> Option DQuick Check:
SS LOW selects slave = A [OK]
- Setting SS HIGH to select slave
- Using clock pin to select slave
- Sending data to select slave
SPI_SS_PIN = 0; // Select slave SPI_Write(0xA5); // Send data SPI_SS_PIN = 1; // Deselect slaveWhat happens on the SPI bus during this code execution?
Solution
Step 1: Analyze slave select pin changes
Setting SPI_SS_PIN to 0 selects the slave before sending data.Step 2: Understand data transmission
SPI_Write(0xA5) sends data from master to selected slave.Step 3: Deselect slave after sending
Setting SPI_SS_PIN to 1 ends communication by deselecting slave.Final Answer:
The master selects the slave, sends 0xA5, then deselects the slave. -> Option AQuick Check:
SS LOW + send data + SS HIGH = D [OK]
- Assuming slave sends data
- Ignoring slave select pin state
- Thinking data sends without slave selected
SPI_SS_PIN = 1; // Deselect slave SPI_Write(0x3C); // Send data SPI_SS_PIN = 0; // Select slave
Solution
Step 1: Check slave select pin timing
Slave is deselected (SS=1) before sending data, so slave ignores data.Step 2: Correct order for SPI communication
Slave must be selected (SS=0) before sending data to receive it.Final Answer:
Slave is deselected before sending data, so data won't be received. -> Option CQuick Check:
SS must be LOW before send = C [OK]
- Sending data before selecting slave
- Assuming data sends regardless of SS pin
- Misunderstanding SPI_Write timing
Solution
Step 1: Understand multiple slave selection
Each slave needs its own slave select pin controlled by the master.Step 2: Select only one slave at a time
Set only one slave select pin LOW to activate that slave; others stay HIGH.Step 3: Avoid simultaneous selection
Selecting multiple slaves at once causes bus conflicts and data errors.Final Answer:
Use separate slave select pins for each slave and set only one LOW at a time. -> Option BQuick Check:
Separate SS pins, one LOW at a time = B [OK]
- Using one SS pin for all slaves
- Trying to select slaves by clock toggling
- Selecting multiple slaves simultaneously
Ready to prove it?
Pass the test and this pattern moves unknown
