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?"
Practice
Solution
Step 1: Understand SPI communication basics
SPI uses multiple lines including clock, data in, data out, and select lines to transfer data quickly.Step 2: Identify the main advantage of SPI
SPI is known for its speed and reliability compared to other protocols like I2C or UART.Final Answer:
Because it provides fast and reliable data transfer -> Option DQuick Check:
SPI speed and reliability = Because it provides fast and reliable data transfer [OK]
- Thinking SPI uses only one wire
- Confusing SPI with protocols that don't use clock
- Assuming SPI is slow
Solution
Step 1: Recall SPI signal names
SPI uses Master Out Slave In (MOSI), Master In Slave Out (MISO), Serial Clock (SCLK), and Slave Select (SS) lines.Step 2: Match options with SPI lines
Only MOSI, MISO, SCLK, SS lists the correct SPI lines.Final Answer:
MOSI, MISO, SCLK, SS -> Option AQuick Check:
SPI lines = MOSI, MISO, SCLK, SS [OK]
- Confusing SPI lines with UART or I2C lines
- Mixing up signal names
- Choosing generic signal names not used in SPI
uint8_t master_send = 0xA5;
uint8_t slave_response = 0x5A;
uint8_t received = spi_transfer(master_send); // spi_transfer sends and receives simultaneously
printf("0x%X", received);Solution
Step 1: Understand spi_transfer behavior
SPI sends data from master and simultaneously receives data from slave.Step 2: Identify received data
The function returns the data received from the slave, which is 0x5A.Final Answer:
0x5A -> Option BQuick Check:
Received data = 0x5A [OK]
- Assuming received data equals sent data
- Confusing master and slave data roles
- Expecting compilation error due to function call
SPI_InitTypeDef SPI_InitStruct; SPI_InitStruct.Mode = SPI_MODE_MASTER; SPI_InitStruct.Direction = SPI_DIRECTION_2LINES; SPI_InitStruct.DataSize = 8; SPI_InitStruct.CLKPolarity = 0; SPI_InitStruct.CLKPhase = 0; SPI_InitStruct.NSS = SPI_NSS_SOFT; SPI_InitStruct.BaudRatePrescaler = 0; SPI_Init(&SPI_InitStruct);
Solution
Step 1: Check BaudRatePrescaler valid values
BaudRatePrescaler must be a non-zero value representing division factor (e.g., 2, 4, 8).Step 2: Identify invalid value
Setting BaudRatePrescaler to 0 is invalid and will cause incorrect SPI speed or error.Final Answer:
BaudRatePrescaler cannot be zero -> Option AQuick Check:
BaudRatePrescaler ≠ 0 [OK]
- Setting BaudRatePrescaler to zero
- Confusing NSS software and hardware modes
- Changing CLKPolarity without reason
Solution
Step 1: Compare SPI with UART and I2C for multiple devices
SPI uses separate slave select lines allowing multiple devices and supports higher speeds than UART and I2C.Step 2: Identify why SPI is better for fast, reliable multi-device communication
SPI's dedicated lines and clock signal enable fast, simultaneous data transfer with multiple sensors.Final Answer:
SPI supports multiple devices with separate select lines and faster data rates -> Option CQuick Check:
SPI multi-device speed advantage = SPI supports multiple devices with separate select lines and faster data rates [OK]
- Assuming UART is faster than SPI
- Thinking I2C is always simpler and faster
- Believing SPI works without a clock
Ready to prove it?
Pass the test and this pattern moves unknown
