Bird
Raised Fist0
Embedded Cprogramming~5 mins

SPI data transfer sequence in Embedded C

Choose your learning style10 modes available
Introduction

SPI data transfer sequence shows how devices send and receive data step-by-step using SPI. It helps devices talk to each other quickly and clearly.

When connecting a microcontroller to a sensor to get data.
When sending commands from a microcontroller to a display screen.
When reading data from an external memory chip.
When controlling a motor driver with a microcontroller.
When communicating between two microcontrollers.
Syntax
Embedded C
1. Pull the chip select (CS) line low to start communication.
2. Send data byte by byte over the MOSI line.
3. Receive data byte by byte over the MISO line.
4. After all bytes are sent and received, pull CS line high to end communication.

The CS line tells the device when to listen or ignore data.

Data is sent and received at the same time, one byte per clock cycle.

Examples
Basic single byte send and receive using SPI.
Embedded C
CS = LOW; // Start communication
SPI_Transmit(data_byte); // Send one byte
received = SPI_Receive(); // Receive one byte
CS = HIGH; // End communication
Send and receive multiple bytes in a loop.
Embedded C
CS = LOW;
for (int i = 0; i < length; i++) {
    SPI_Transmit(buffer[i]);
    buffer[i] = SPI_Receive();
}
CS = HIGH;
Sample Program

This program simulates sending three bytes over SPI and receiving data back. It shows the chip select going low and high around the transfer.

Embedded C
#include <stdint.h>
#include <stdio.h>

// Mock functions for SPI hardware
void CS_Low() { printf("CS LOW\n"); }
void CS_High() { printf("CS HIGH\n"); }
uint8_t SPI_TransmitReceive(uint8_t data) {
    printf("Sent: 0x%02X\n", data);
    // For demo, just return data + 1 as received
    return data + 1;
}

int main() {
    uint8_t send_data[3] = {0x10, 0x20, 0x30};
    uint8_t receive_data[3];

    CS_Low();
    for (int i = 0; i < 3; i++) {
        receive_data[i] = SPI_TransmitReceive(send_data[i]);
    }
    CS_High();

    printf("Received bytes: ");
    for (int i = 0; i < 3; i++) {
        printf("0x%02X ", receive_data[i]);
    }
    printf("\n");

    return 0;
}
OutputSuccess
Important Notes

Always pull CS low before starting SPI transfer and high after finishing.

SPI sends and receives data at the same time, so you get a byte back for every byte sent.

Clock polarity and phase settings must match between devices for correct data transfer.

Summary

SPI data transfer uses chip select to start and stop communication.

Data is sent and received byte by byte simultaneously.

Proper timing and settings are important for successful SPI communication.

Practice

(1/5)
1. In SPI communication, what happens during a data transfer sequence?
easy
A. Data is only sent, no data is received.
B. Data is sent and received simultaneously, one byte at a time.
C. Data is received first, then sent after completion.
D. Data is sent in large blocks without waiting for response.

Solution

  1. Step 1: Understand SPI full-duplex nature

    SPI sends and receives data simultaneously, byte by byte.
  2. Step 2: Confirm data transfer behavior

    Each byte sent triggers a byte received at the same time.
  3. Final Answer:

    Data is sent and received simultaneously, one byte at a time. -> Option B
  4. Quick Check:

    SPI full-duplex = simultaneous send/receive [OK]
Hint: Remember SPI sends and receives one byte at a time simultaneously [OK]
Common Mistakes:
  • Thinking SPI only sends data without receiving
  • Assuming data is received after all sending completes
  • Believing SPI transfers data in large blocks
2. Which of the following code snippets correctly waits for SPI transfer completion before reading received data?
easy
A. while(!(SPI_SR & SPI_SR_RXNE)); uint8_t data = SPI_DR;
B. uint8_t data = SPI_DR; while(!(SPI_SR & SPI_SR_RXNE));
C. while(SPI_SR & SPI_SR_BSY); uint8_t data = SPI_DR;
D. while(!(SPI_SR & SPI_SR_TXE)); uint8_t data = SPI_DR;

Solution

  1. Step 1: Identify the correct flag for received data ready

    SPI_SR_RXNE indicates data received and ready to read.
  2. Step 2: Confirm waiting loop and reading order

    Wait until RXNE is set, then read SPI_DR to get received byte.
  3. Final Answer:

    while(!(SPI_SR & SPI_SR_RXNE)); uint8_t data = SPI_DR; -> Option A
  4. Quick Check:

    Wait RXNE flag before reading data [OK]
Hint: Wait for RXNE flag before reading SPI data register [OK]
Common Mistakes:
  • Waiting for TXE instead of RXNE
  • Reading data before checking RXNE flag
  • Waiting for BSY flag incorrectly
3. Given the following code snippet, what will be the content of the array 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;
}
medium
A. [0xA1, 0xB2, 0xC3]
B. [unknown garbage values]
C. [data received from SPI slave during transfer]
D. [0x00, 0x00, 0x00]

Solution

  1. Step 1: Understand SPI transfer behavior

    Each byte sent causes a byte to be received simultaneously from the slave device.
  2. Step 2: Analyze received array assignment

    received[i] stores the byte read from SPI_DR after waiting for RXNE flag.
  3. Final Answer:

    Data received from SPI slave during transfer -> Option C
  4. Quick Check:

    Received array = slave response bytes [OK]
Hint: Received data is from slave, not the sent bytes [OK]
Common Mistakes:
  • Assuming received data equals sent data
  • Ignoring that SPI_DR reads slave response
  • Expecting zeros or uninitialized values
4. Identify the error in this SPI data transfer loop:
for(int i = 0; i < 4; i++) {
  SPI_DR = data[i];
  received[i] = SPI_DR;
  while(!(SPI_SR & SPI_SR_RXNE));
}
medium
A. Loop counter exceeds array size.
B. Not sending data before reading.
C. Missing SPI enable command.
D. Reading SPI_DR before waiting for RXNE flag.

Solution

  1. 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.
  2. Step 2: Correct sequence for SPI read

    Must wait for RXNE flag before reading SPI_DR to get valid received data.
  3. Final Answer:

    Reading SPI_DR before waiting for RXNE flag. -> Option D
  4. Quick Check:

    Wait RXNE before reading SPI_DR [OK]
Hint: Always wait RXNE flag before reading SPI_DR [OK]
Common Mistakes:
  • Reading SPI_DR too early
  • Ignoring RXNE flag status
  • Assuming data is ready immediately after writing
5. You want to send an array of 5 bytes over SPI and store the received bytes simultaneously. Which code snippet correctly implements this transfer sequence?
hard
A. for(int i=0; i<5; i++) { SPI_DR = tx[i]; while(!(SPI_SR & SPI_SR_RXNE)); rx[i] = SPI_DR; }
B. for(int i=0; i<5; i++) { while(!(SPI_SR & SPI_SR_TXE)); SPI_DR = tx[i]; rx[i] = SPI_DR; }
C. for(int i=0; i<5; i++) { SPI_DR = tx[i]; rx[i] = SPI_DR; while(!(SPI_SR & SPI_SR_RXNE)); }
D. for(int i=0; i<5; i++) { while(SPI_SR & SPI_SR_BSY); SPI_DR = tx[i]; rx[i] = SPI_DR; }

Solution

  1. Step 1: Confirm correct order of operations

    Send byte by writing SPI_DR, wait for RXNE flag, then read received byte.
  2. Step 2: Validate correct snippet

    The correct snippet writes SPI_DR, waits for RXNE, then reads SPI_DR into rx array correctly.
  3. Step 3: Check distractors for errors

    Others read SPI_DR before RXNE is set, read before waiting, or wait for incorrect flags like BSY.
  4. Final Answer:

    for(int i=0; i<5; i++) { SPI_DR = tx[i]; while(!(SPI_SR & SPI_SR_RXNE)); rx[i] = SPI_DR; } -> Option A
  5. Quick Check:

    Send, wait RXNE, then read = correct SPI transfer [OK]
Hint: Send byte, wait RXNE flag, then read received byte [OK]
Common Mistakes:
  • Reading SPI_DR before RXNE flag is set
  • Not waiting for transfer completion
  • Confusing TXE and RXNE flags
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard