SPI data transfer sequence in Embedded C - Time & Space Complexity
We want to understand how the time taken by SPI data transfer changes as we send more data.
How does the number of bytes affect the total time spent transferring data?
Analyze the time complexity of the following 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 and waits for the transfer to finish before sending the next.
- 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
lengthtimes.
Each additional byte adds one more send-and-wait cycle, so the total time grows directly with the number of bytes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 send-and-wait cycles |
| 100 | 100 send-and-wait cycles |
| 1000 | 1000 send-and-wait cycles |
Pattern observation: The time increases steadily and directly as the number of bytes increases.
Time Complexity: O(n)
This means the time to transfer data grows in a straight line with the number of bytes sent.
[X] Wrong: "The transfer time stays the same no matter how many bytes we send."
[OK] Correct: Each byte requires its own send and wait, so more bytes mean more time.
Understanding how data transfer time scales helps you design efficient communication in embedded systems and shows you can analyze real hardware-related code.
"What if we used DMA (Direct Memory Access) to send data instead of sending byte-by-byte? How would the time complexity change?"
Practice
Solution
Step 1: Understand SPI full-duplex nature
SPI sends and receives data simultaneously, byte by byte.Step 2: Confirm data transfer behavior
Each byte sent triggers a byte received at the same time.Final Answer:
Data is sent and received simultaneously, one byte at a time. -> Option BQuick Check:
SPI full-duplex = simultaneous send/receive [OK]
- Thinking SPI only sends data without receiving
- Assuming data is received after all sending completes
- Believing SPI transfers data in large blocks
Solution
Step 1: Identify the correct flag for received data ready
SPI_SR_RXNE indicates data received and ready to read.Step 2: Confirm waiting loop and reading order
Wait until RXNE is set, then read SPI_DR to get received byte.Final Answer:
while(!(SPI_SR & SPI_SR_RXNE)); uint8_t data = SPI_DR; -> Option AQuick Check:
Wait RXNE flag before reading data [OK]
- Waiting for TXE instead of RXNE
- Reading data before checking RXNE flag
- Waiting for BSY flag incorrectly
received[3] after execution?uint8_t data_to_send[3] = {0xA1, 0xB2, 0xC3};
uint8_t received[3];
for(int i = 0; i < 3; i++) {
SPI_DR = data_to_send[i];
while(!(SPI_SR & SPI_SR_RXNE));
received[i] = SPI_DR;
}Solution
Step 1: Understand SPI transfer behavior
Each byte sent causes a byte to be received simultaneously from the slave device.Step 2: Analyze received array assignment
received[i] stores the byte read from SPI_DR after waiting for RXNE flag.Final Answer:
Data received from SPI slave during transfer -> Option CQuick Check:
Received array = slave response bytes [OK]
- Assuming received data equals sent data
- Ignoring that SPI_DR reads slave response
- Expecting zeros or uninitialized values
for(int i = 0; i < 4; i++) {
SPI_DR = data[i];
received[i] = SPI_DR;
while(!(SPI_SR & SPI_SR_RXNE));
}Solution
Step 1: Check order of reading SPI_DR and waiting for RXNE
The code reads SPI_DR before waiting for RXNE flag, which is incorrect.Step 2: Correct sequence for SPI read
Must wait for RXNE flag before reading SPI_DR to get valid received data.Final Answer:
Reading SPI_DR before waiting for RXNE flag. -> Option DQuick Check:
Wait RXNE before reading SPI_DR [OK]
- Reading SPI_DR too early
- Ignoring RXNE flag status
- Assuming data is ready immediately after writing
Solution
Step 1: Confirm correct order of operations
Send byte by writing SPI_DR, wait for RXNE flag, then read received byte.Step 2: Validate correct snippet
The correct snippet writes SPI_DR, waits for RXNE, then reads SPI_DR into rx array correctly.Step 3: Check distractors for errors
Others read SPI_DR before RXNE is set, read before waiting, or wait for incorrect flags like BSY.Final Answer:
for(int i=0; i<5; i++) { SPI_DR = tx[i]; while(!(SPI_SR & SPI_SR_RXNE)); rx[i] = SPI_DR; } -> Option AQuick Check:
Send, wait RXNE, then read = correct SPI transfer [OK]
- Reading SPI_DR before RXNE flag is set
- Not waiting for transfer completion
- Confusing TXE and RXNE flags
Ready to prove it?
Pass the test and this pattern moves unknown
