0
0
Embedded Cprogramming~10 mins

SPI master-slave architecture in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SPI master-slave architecture
Master initiates communication
Master sends clock pulses
Master sends data bits
Slave reads data bits on clock edges
Slave sends data bits back (optional)
Master reads slave data bits
Communication ends when master stops clock
The SPI master controls the clock and data flow, sending bits to the slave while the slave reads and optionally sends data back, synchronized by the clock.
Execution Sample
Embedded C
void SPI_Master_Transmit(uint8_t data) {
    for (int i = 7; i >= 0; i--) {
        // Set MOSI according to data bit
        // Toggle clock
        // Read MISO if needed
    }
}
This code sends 8 bits from master to slave, one bit at a time, toggling the clock each time.
Execution Table
Bit IndexData Bit Sent (MOSI)Clock StateSlave Reads BitMaster Reads Bit (MISO)
71Low->HighReads 1Reads 0
60Low->HighReads 0Reads 1
51Low->HighReads 1Reads 0
41Low->HighReads 1Reads 1
30Low->HighReads 0Reads 0
20Low->HighReads 0Reads 1
11Low->HighReads 1Reads 0
00Low->HighReads 0Reads 1
----Communication ends after 8 bits
💡 All 8 bits sent and received, master stops clock to end communication
Variable Tracker
VariableStartAfter Bit 7After Bit 6After Bit 5After Bit 4After Bit 3After Bit 2After Bit 1After Bit 0Final
MOSI (Master Out Slave In)N/A10110010Last bit sent
ClockLowHighHighHighHighHighHighHighHighStopped
Slave Read BitN/A10110010All bits read
MISO (Master In Slave Out)N/A01010101All bits read by master
Key Moments - 3 Insights
Why does the master control the clock and not the slave?
The master controls the clock to synchronize data transfer; without the clock, the slave wouldn't know when to read or send bits. See execution_table rows where clock toggles before data read.
How does the slave know which bit is being sent?
The slave reads the MOSI line on the clock's rising edge, so each clock pulse tells the slave to read the next bit. This is shown in execution_table where slave reads bits on clock transitions.
Can the slave send data back at the same time?
Yes, SPI allows full-duplex communication; while master sends bits on MOSI, slave sends bits on MISO simultaneously, as seen in execution_table where master reads MISO each bit.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the MOSI bit sent at Bit Index 4?
A0
B1
CDoes not send at Bit 4
DSame as Bit 7
💡 Hint
Check the 'Data Bit Sent (MOSI)' column at row with Bit Index 4
At which bit index does the master read a '1' from MISO for the first time?
ABit 7
BBit 4
CBit 6
DBit 0
💡 Hint
Look at 'Master Reads Bit (MISO)' column and find first '1'
If the clock stopped after Bit 2, what would happen?
ACommunication ends early, only 6 bits sent
BSlave sends all bits anyway
CMaster reads all 8 bits regardless
DClock stopping has no effect
💡 Hint
Refer to exit_note and understand clock controls data transfer
Concept Snapshot
SPI Master-Slave Architecture:
- Master controls clock and data flow
- Data sent bit-by-bit on MOSI line
- Slave reads bits on clock edges
- Slave can send data back on MISO simultaneously
- Communication ends when master stops clock
Full Transcript
SPI (Serial Peripheral Interface) uses a master-slave setup where the master controls the clock signal. The master sends data bits one by one on the MOSI line, toggling the clock each time. The slave reads these bits on the clock's rising edge. At the same time, the slave can send data back on the MISO line, which the master reads. This process continues for 8 bits or more, and communication ends when the master stops the clock. The master always controls timing to keep both sides in sync.