0
0
Embedded Cprogramming~5 mins

SPI master-slave architecture in Embedded C - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: SPI master-slave architecture
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As you send more bytes, the total time grows in a straight line.

Input Size (n)Approx. Operations
1010 byte sends
100100 byte sends
10001000 byte sends

Pattern observation: Doubling the data doubles the time because each byte is sent one after another.

Final Time Complexity

Time Complexity: O(n)

This means the time grows directly with the number of bytes sent.

Common Mistake

[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.

Interview Connect

Understanding how communication time grows helps you design efficient embedded systems and explain your reasoning clearly.

Self-Check

What if we added a delay between sending each byte? How would the time complexity change?