Challenge - 5 Problems
SPI Master-Slave Expert
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
SPI Master sends data to Slave
What is the output on the SPI slave's received buffer after the master sends the data array?
Embedded C
#include <stdio.h> #include <string.h> #define BUFFER_SIZE 5 char spi_slave_buffer[BUFFER_SIZE]; void spi_master_send(const char *data, int length) { // Simulate sending data to slave memcpy(spi_slave_buffer, data, length); } int main() { const char master_data[BUFFER_SIZE] = {'H', 'E', 'L', 'L', 'O'}; spi_master_send(master_data, BUFFER_SIZE); printf("%.*s", BUFFER_SIZE, spi_slave_buffer); return 0; }
Attempts:
2 left
💡 Hint
Remember that memcpy copies exactly the bytes given without adding null terminators.
✗ Incorrect
The master sends the array {'H','E','L','L','O'} to the slave buffer using memcpy. The slave buffer contains exactly these 5 characters. The printf prints these 5 characters without adding a null terminator, so the output is 'HELLO'.
❓ Predict Output
intermediate2:00remaining
SPI Slave responds to Master request
What will be printed by the master after receiving data from the slave?
Embedded C
#include <stdio.h> char spi_slave_data[] = {0x10, 0x20, 0x30}; void spi_slave_send(char *buffer, int length) { for (int i = 0; i < length; i++) { buffer[i] = spi_slave_data[i]; } } int main() { char master_receive[3] = {0}; spi_slave_send(master_receive, 3); printf("%02X %02X %02X", master_receive[0], master_receive[1], master_receive[2]); return 0; }
Attempts:
2 left
💡 Hint
The printf uses %02X to print hexadecimal values without the 0x prefix.
✗ Incorrect
The slave sends bytes 0x10, 0x20, 0x30 to the master buffer. The master prints them in uppercase hexadecimal without prefix, so output is '10 20 30'.
🔧 Debug
advanced2:00remaining
Identify the cause of SPI data corruption
This SPI master code sometimes sends corrupted data to the slave. What is the most likely cause?
Embedded C
void spi_master_send(char *data, int length) { for (int i = 0; i <= length; i++) { spi_send_byte(data[i]); } } // spi_send_byte sends one byte over SPI hardware int main() { char data[] = {0x01, 0x02, 0x03}; spi_master_send(data, 3); return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition carefully and how array indexing works.
✗ Incorrect
The loop runs from i=0 to i<=length, which means it runs length+1 times. This causes reading data[length], which is out of bounds and may send garbage or cause corruption.
📝 Syntax
advanced2:00remaining
Correct SPI Slave receive function syntax
Which option correctly implements a function to receive data on SPI slave and store it in a buffer?
Attempts:
2 left
💡 Hint
Remember array indexing starts at 0 and loops should run less than length.
✗ Incorrect
Option C correctly loops from 0 to length-1 and stores bytes read from spi_read_byte() into buffer. Other options either go out of bounds or skip first element.
🚀 Application
expert2:00remaining
Calculate SPI clock frequency divider
An SPI master runs at 16 MHz system clock. To communicate with a slave at 1 MHz SPI clock, which divider value should be set in the SPI control register if the SPI clock = system clock / (2 * (divider + 1))?
Attempts:
2 left
💡 Hint
Rearrange the formula: divider = (system_clock / (2 * SPI_clock)) - 1
✗ Incorrect
Given SPI_clock = 1 MHz, system_clock = 16 MHz,
divider = (16,000,000 / (2 * 1,000,000)) - 1 = (16 / 2) - 1 = 8 - 1 = 7.
So the divider value to set is 7.