Challenge - 5 Problems
SPI Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Think about what happens in SPI: data sent is simultaneously received.
✗ Incorrect
In SPI, when you send a byte, you simultaneously receive a byte. Here, the code writes 0xA5 and waits for transmission and reception to complete, then reads the received data. Since no other device modifies the data, the received byte matches the sent byte 0xA5.
🧠 Conceptual
intermediate1:30remaining
SPI Transfer Sequence: Which step comes first?
In an SPI data transfer sequence, which step must happen first to start communication?
Attempts:
2 left
💡 Hint
Think about how the slave device knows communication is starting.
✗ Incorrect
The chip select (CS) line must be set low first to enable the slave device. Only then can data be sent and received. Without enabling the slave, the SPI transfer won't start properly.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
The last received byte matches the last sent byte in this simple loop.
✗ Incorrect
The loop sends 0x10, then 0x20, then 0x30. Each call to spi_transfer returns the received byte, which matches the sent byte here. The final received byte is 0x30.
🔧 Debug
advanced2: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;
}Attempts:
2 left
💡 Hint
Check the flags being waited on in the while loops.
✗ Incorrect
The code waits twice for the transmit buffer empty flag (TXE). It never waits for the receive buffer not empty flag (RXNE). This causes the second loop to wait forever if RXNE is never set.
🧠 Conceptual
expert1: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?
Attempts:
2 left
💡 Hint
SPI is known for simultaneous send and receive.
✗ Incorrect
SPI is a full-duplex protocol, meaning data is sent and received at the same time. When the master sends a byte, it also receives a byte simultaneously from the slave.