0
0
Embedded Cprogramming~20 mins

SPI data transfer sequence in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SPI Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
SPI Data Transfer: What is the output?
Consider this SPI transfer code snippet. What will be the value of received_data after the transfer completes?
Embedded C
#define SPI_DATA_REGISTER (*(volatile unsigned char*)0x4001300C)
#define SPI_STATUS_REGISTER (*(volatile unsigned char*)0x40013008)
#define SPI_STATUS_TXE (1 << 1)  // Transmit buffer empty
#define SPI_STATUS_RXNE (1 << 0) // Receive buffer not empty

unsigned char spi_transfer(unsigned char data) {
    SPI_DATA_REGISTER = data; // Start transmission
    while (!(SPI_STATUS_REGISTER & SPI_STATUS_TXE)) {}
    while (!(SPI_STATUS_REGISTER & SPI_STATUS_RXNE)) {}
    return SPI_DATA_REGISTER; // Read received data
}

int main() {
    unsigned char sent_data = 0xA5;
    unsigned char received_data = spi_transfer(sent_data);
    return received_data;
}
A0x5A
B0xA5
C0xFF
D0x00
Attempts:
2 left
💡 Hint
Think about what happens in SPI: data sent is simultaneously received.
🧠 Conceptual
intermediate
1:30remaining
SPI Transfer Sequence: Which step comes first?
In an SPI data transfer sequence, which step must happen first to start communication?
ASet the chip select (CS) line low to enable the slave device
BSend the data byte to the SPI data register
CWait for the receive buffer to be not empty
DRead the received data from the SPI data register
Attempts:
2 left
💡 Hint
Think about how the slave device knows communication is starting.
Predict Output
advanced
2:00remaining
SPI Transfer Loop: What is the final value?
Given this code that sends multiple bytes over SPI, what is the final value of last_received?
Embedded C
#define SPI_DATA_REG (*(volatile unsigned char*)0x4001300C)
#define SPI_STATUS_REG (*(volatile unsigned char*)0x40013008)
#define SPI_STATUS_TXE (1 << 1)
#define SPI_STATUS_RXNE (1 << 0)

unsigned char spi_transfer(unsigned char data) {
    SPI_DATA_REG = data;
    while (!(SPI_STATUS_REG & SPI_STATUS_TXE)) {}
    while (!(SPI_STATUS_REG & SPI_STATUS_RXNE)) {}
    return SPI_DATA_REG;
}

int main() {
    unsigned char data_to_send[3] = {0x10, 0x20, 0x30};
    unsigned char last_received = 0;
    for (int i = 0; i < 3; i++) {
        last_received = spi_transfer(data_to_send[i]);
    }
    return last_received;
}
A0x30
B0x10
C0x00
D0x60
Attempts:
2 left
💡 Hint
The last received byte matches the last sent byte in this simple loop.
🔧 Debug
advanced
2:00remaining
Identify the error in SPI transfer code
This SPI transfer function is intended to send and receive a byte. What error will it cause?
Embedded C
unsigned char spi_transfer(unsigned char data) {
    SPI_DATA_REGISTER = data;
    while (!(SPI_STATUS_REGISTER & SPI_STATUS_TXE)) {}
    while (!(SPI_STATUS_REGISTER & SPI_STATUS_RXNE)) {}
    return SPI_DATA_REGISTER;
}
AIt causes a syntax error due to missing semicolon
BIt returns the wrong register value
CIt causes an infinite loop waiting for receive buffer flag
DIt reads the data before transmission starts
Attempts:
2 left
💡 Hint
Check the flags being waited on in the while loops.
🧠 Conceptual
expert
1:30remaining
SPI Full-Duplex Transfer: What happens during data exchange?
In SPI full-duplex communication, what happens when the master sends a byte to the slave?
AThe master only sends data; the slave sends data separately later
BThe master sends a byte first, then waits to receive a byte after transmission
CThe master sends a byte and the slave ignores it until the next clock cycle
DThe master sends a byte and simultaneously receives a byte from the slave
Attempts:
2 left
💡 Hint
SPI is known for simultaneous send and receive.