0
0
Embedded Cprogramming~5 mins

Why SPI is used in Embedded C - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why SPI is used
O(n)
Understanding Time Complexity

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?

Scenario Under Consideration

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 Repeating Operations

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

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
1010 byte sends
100100 byte sends
10001000 byte sends

Pattern observation: Doubling the data size doubles the total time needed.

Final Time Complexity

Time Complexity: O(n)

This means the time to send data over SPI grows linearly with the amount of data.

Common Mistake

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

Interview Connect

Understanding how SPI timing grows with data size helps you explain communication speed in embedded systems clearly and confidently.

Self-Check

"What if SPI could send multiple bytes at once using DMA? How would the time complexity change?"