🚀 Multi-Slave SPI Communication System

Sistem Komunikasi SPI Master-Slave dengan Protocol Custom

Real-time monitoring • Fast & Non-blocking • PC Control Interface

📡

SPI Protocol

Custom packet format

High Speed

2 MHz, 100 Hz

💾

EEPROM

Auto backup

🖥️

PC Interface

Serial control

📐 Arsitektur Sistem

Hardware Configuration

PinMasterSlave
MOSI5111
MISO5012
SCK5213
SS1/2/3A13/A14/A1510

SPI Configuration

ParameterMasterSlave
ModeMasterSlave
Clock2 MHzFollow
Data ModeMODE0MODE0
Baud1152009600

📦 Format Paket

Request (Master → Slave): 5 bytes

[0xFF] [0xFE] [ID] [CMD] [CS] SYNC1 SYNC2 0x01 0x01 ID^CMD

Response (Slave → Master)

Single Byte: 5 bytes

[0xFF] [0xFE] [ID] [DATA] [CS]

16-bit Data: 6 bytes

[0xFF] [0xFE] [ID] [HIGH] [LOW] [CS]

🎮 Command Set

SPI Commands

CommandValueDescription
CMD_READ_VALUE0x01Read PB2
CMD_READ_SENSOR0x02Read ADC
CMD_READ_COUNTER0x03Read Counter

PC Commands (Serial)

CommandOutput
X1>>> PB2
X2>>> ADC
X3>>> PB2 ADC
X7>>> PB2 ADC CNT
STOP[Display stopped]

🔄 Alur Data (Step 1-6)

Step 1: PC Request

PC: 13 // Slave 1, PB2+ADC

Step 2: Master Parse

slaveNum = 1; dataSelect = 3;

Step 3: Master → Slave (CMD 0x01)

[0xFF 0xFE 0x01 0x01 0x00]

Step 4: Slave Process

ISR validates & prepares response

Step 5: Slave → Master

[0xFF 0xFE 0x01 0x01 0x00] // PB2=1

Step 6: Master Validate

Read 15 bytes, find sync, validate CS

🔄 Alur Data (Step 7-12)

Step 7: Master → Slave (CMD 0x02)

[0xFF 0xFE 0x01 0x02 0x03]

Step 8: Slave → Master (16-bit)

[0xFF 0xFE 0x01 0x03 0x40 0x42] // 832

Step 9: Master Parse 16-bit

value = (0x03 << 8) | 0x40 = 832

Step 10: Save EEPROM

EEPROM[0x00-0x03] = {1, 0x03, 0x40, cnt}

Step 11: Display PC

Serial: ">>> 1 832"

Step 12: Loop (100 Hz)

Repeat every 10ms until STOP

🔍 Parsing & Validation

Master: Find Sync Pattern

for (int i = 0; i < 10; i++) { if (buf[i]==0xFF && buf[i+1]==0xFE && buf[i+2]==ID) { if (validateChecksum()) return parseData(); } }

Slave: State Machine

WAIT_SYNC1 → WAIT_SYNC2 → WAIT_ID → WAIT_CMD → WAIT_CS → SEND_RESPONSE

🔐 Checksum

XOR Algorithm

// Request CS = ID ^ CMD = 0x01 ^ 0x02 = 0x03 // Response (single) CS = ID ^ DATA = 0x01 ^ 0x01 = 0x00 // Response (16-bit) CS = ID ^ HIGH ^ LOW = 0x01 ^ 0x03 ^ 0x40 = 0x42

⚡ Timing & Performance

2 MHzSPI Clock
100 HzUpdate Rate
~2 msLatency

Communication Timeline

Time (ms)Event
0.000PC sends command
0.002Master activates SS
0.007Request sent
0.015Slave responds
0.020SS deactiviv
0.022Parse & validate
0.025Display to PC

💾 EEPROM Storage

Memory Map

AddressSlaveData
0x00-0x03Slave 1PB2,ADC_H,ADC_L,CNT
0x04-0x07Slave 2PB2,ADC_H,ADC_L,CNT
0x08-0x0BSlave 3PB2,ADC_H,ADC_L,CNT

Save/Load

// Save EEPROM.update(base+0, pb2); EEPROM.update(base+1, adc>>8); EEPROM.update(base+2, adc&0xFF); EEPROM.update(base+3, cnt); // Load pb2 = EEPROM.read(base+0); adc = (EEPROM.read(base+1)<<8) | EEPROM.read(base+2);

🖥️ LCD Display

16x2 LCD Format

┌────────────────┐ │S1 P:1 A:832 │ │ C:5 OK │ └────────────────┘

Update Code

lcd.clear(); lcd.setCursor(0,0); lcd.print("S1 P:" + pb2 + " A:" + adc); lcd.setCursor(0,1); lcd.print(" C:" + cnt + (valid ? " OK" : " ER"));

Rotates every 500ms: S1 → S2 → S3 → S1

🛡️ Error Handling

Error Detection

ErrorDetectionAction
Checksum failXOR mismatchReturn 0xFF/0xFFFF
No syncPattern not foundReturn error
Wrong IDID mismatchIgnore, reset state
No responseTimeoutMark invalid

Master Error Handling

if (pb2 == 0xFF || adc == 0xFFFF) { data.valid = false; return false; } data.valid = true; saveToEEPROM();

🌟 Keunggulan Sistem

🚀

Performance

  • 2 MHz SPI
  • 100 Hz rate
  • Non-blocking
  • ~2ms latency
🔒

Reliable

  • Checksum
  • Sync pattern
  • Error recovery
  • State machine
💾

Persistent

  • Auto EEPROM
  • Fast response
  • No data loss
  • 100k cycles
📡

Scalable

  • 3 slaves
  • Easy expand
  • Custom CMD
  • Flexible

📌 Summary

Multi-Slave SPI Communication System

Sistem komunikasi SPI yang menghubungkan 1 Master dengan 3 Slave menggunakan custom protocol dengan sync pattern dan checksum validation. Master dapat berkomunikasi dengan PC via Serial untuk real-time monitoring dengan kecepatan hingga 100 updates/detik.

Data Flow

PC → Master → Slave → EEPROM → Display 1. PC: "17" (Slave 1, All data) 2. Master polls via SPI (3 commands) 3. Slave responds with validated data 4. Master saves to EEPROM 5. Display: ">>> 1 832 5" 6. Repeat @ 100 Hz until "STOP"
2 MHzSPI Speed
100 HzDisplay Rate
~2 msLatency
3 SlavesScalable

Thank You! 🚀

Multi-Slave SPI Communication System

Questions & Discussion