SPI vs UART trade-offs in Embedded C - Performance Comparison
When working with SPI and UART communication in embedded C, it's important to understand how the time cost grows as data size increases.
We want to see how the number of operations changes when sending more data using SPI or UART.
Analyze the time complexity of sending data byte-by-byte using SPI and UART.
// Send data using SPI
void sendSPI(uint8_t* data, int length) {
for (int i = 0; i < length; i++) {
SPI_Transmit(data[i]);
}
}
// Send data using UART
void sendUART(uint8_t* data, int length) {
for (int i = 0; i < length; i++) {
UART_Transmit(data[i]);
}
}
Both functions send data one byte at a time in a loop.
Both functions use a loop to send each byte.
- Primary operation: Sending one byte via SPI or UART.
- How many times: Once for each byte in the data (length times).
As the number of bytes to send increases, the total operations increase proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 byte sends |
| 100 | 100 byte sends |
| 1000 | 1000 byte sends |
Pattern observation: Doubling the data doubles the number of send operations.
Time Complexity: O(n)
This means the time to send data grows linearly with the number of bytes.
[X] Wrong: "SPI is always faster than UART, so time complexity is better for SPI."
[OK] Correct: Both SPI and UART send each byte one at a time, so the number of operations grows the same way. Speed differences come from hardware, not time complexity.
Understanding how communication protocols scale with data size helps you explain design choices clearly and shows you grasp practical embedded system trade-offs.
What if we changed the code to send data in larger blocks instead of byte-by-byte? How would the time complexity change?
Practice
Solution
Step 1: Understand SPI characteristics
SPI is known for high-speed data transfer and supports multiple devices with separate select lines.Step 2: Compare with UART features
UART uses fewer wires and is simpler but slower than SPI.Final Answer:
Faster data transfer speed -> Option DQuick Check:
SPI speed > UART speed [OK]
- Confusing wire count advantage
- Mixing speed with simplicity
- Assuming UART is faster
Solution
Step 1: Recall UART wiring basics
UART typically uses two wires: one for transmitting (TX) and one for receiving (RX).Step 2: Eliminate other options
SPI uses MOSI, MISO, and SCLK (3 wires), and UART does not use a separate clock line.Final Answer:
Uses two wires: TX and RX -> Option BQuick Check:
UART wires = TX + RX [OK]
- Confusing SPI wires with UART
- Thinking UART needs clock wire
- Assuming UART uses more than two wires
Sensor1, Sensor2, Sensor3 connected to MCUSolution
Step 1: Analyze device count and speed needs
Communicating with multiple sensors quickly requires an interface that supports multiple devices and high speed.Step 2: Match interface features
SPI supports multiple devices using separate chip select lines and is faster than UART, which is simpler but slower and not ideal for multiple devices on one bus.Final Answer:
SPI, because it supports multiple devices with separate select lines -> Option AQuick Check:
Multiple devices + speed = SPI [OK]
- Choosing UART for multiple devices
- Confusing speed advantage
- Ignoring chip select lines in SPI
spi_init(); spi_select_device(1); spi_send_byte(0xA5); spi_deselect_device(1); spi_send_byte(0x5A);
Solution
Step 1: Review SPI device selection logic
Data should be sent only when the device is selected; sending after deselecting is invalid.Step 2: Check code sequence
After deselecting device 1, sending another byte without selecting a device is an error.Final Answer:
Sending data after deselecting device causes error -> Option CQuick Check:
Send data only when device selected [OK]
- Ignoring device select/deselect order
- Confusing UART with SPI code
- Assuming multiple sends allowed without select
Solution
Step 1: Consider distance and wiring needs
For 10 meters distance, simpler wiring and signal integrity are important.Step 2: Match interface to requirements
UART uses fewer wires (TX, RX) and is better suited for longer distances than SPI, which requires more wires and is better for short distances.Final Answer:
UART, because it uses fewer wires and works better over longer distances -> Option AQuick Check:
Long distance + fewer wires = UART [OK]
- Choosing SPI for long distance
- Confusing wire count
- Ignoring distance limits of SPI
Ready to prove it?
Pass the test and this pattern moves unknown
