0
0
Embedded Cprogramming~20 mins

SPI master-slave architecture in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SPI Master-Slave Expert
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
AError: Buffer overflow
BHELLO
CHELLO\0
Dhello
Attempts:
2 left
💡 Hint
Remember that memcpy copies exactly the bytes given without adding null terminators.
Predict Output
intermediate
2: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;
}
AError: Uninitialized buffer
B0x10 0x20 0x30
C16 32 48
D10 20 30
Attempts:
2 left
💡 Hint
The printf uses %02X to print hexadecimal values without the 0x prefix.
🔧 Debug
advanced
2: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;
}
AThe loop uses <= length causing one extra byte to be sent, reading out of bounds
Bspi_send_byte is not called inside the loop
CThe data array is not initialized
DThe function spi_master_send should use a while loop instead
Attempts:
2 left
💡 Hint
Check the loop condition carefully and how array indexing works.
📝 Syntax
advanced
2: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?
A
void spi_slave_receive(char *buffer, int length) {
    int i = 0;
    while (i &lt;= length) {
        buffer[i] = spi_read_byte();
        i++;
    }
}
B
void spi_slave_receive(char buffer[], int length) {
    for (int i = 0; i &lt;= length; i++) {
        buffer[i] = spi_read_byte();
    }
}
C
void spi_slave_receive(char *buffer, int length) {
    for (int i = 0; i &lt; length; i++) {
        buffer[i] = spi_read_byte();
    }
}
D
void spi_slave_receive(char buffer[], int length) {
    for (int i = 1; i &lt; length; i++) {
        buffer[i] = spi_read_byte();
    }
}
Attempts:
2 left
💡 Hint
Remember array indexing starts at 0 and loops should run less than length.
🚀 Application
expert
2: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))?
A7
B3
C15
D1
Attempts:
2 left
💡 Hint
Rearrange the formula: divider = (system_clock / (2 * SPI_clock)) - 1