SPI
SPI Introduction
The Serial Peripheral Interface (SPI) is a practical standard for synchronous serial communication, commonly used for short-distance wired communication between integrated circuits in embedded systems.
Preparation
- A Rhino Pi X1
- A female Dupont wire connector
Connection
| No. | Board Silk Screen | GPIO/System Mapping | Description |
|---|---|---|---|
| 12 | SPI1_MISO | SPI1_MISO | SPI1 master input, slave output channel |
| 14 | SPI1_MOSI | SPI1_MOSI | SPI1 master output, slave input channel |
| 16 | SPI1_CLK | SPI1_CLK | SPI1 serial clock |
| 18 | SPI1_CS_N | SPI1_CS_N | SPI1 chip select, active low |
Short-connect pins 12 and 14.
Test
- Check for spidev devices:
shell
ls /dev/spidev*- Install the required Python package:
shell
sudo pip3 install spidev- Create a file named
spitest.pyand paste the following code into it:
python
import spidev
import time
class SPILoopbackTester:
def __init__(self, bus=0, device=0, speed=1000000):
"""
Initialize the SPI loopback tester.
:param bus: SPI bus number
:param device: SPI device number
:param speed: communication speed (Hz)
"""
self.spi = spidev.SpiDev()
self.bus = bus
self.device = device
self.speed = speed
self.connected = False
def connect(self):
"""Connect to the SPI device."""
try:
self.spi.open(self.bus, self.device)
self.spi.max_speed_hz = self.speed
self.connected = True
print(f"Successfully connected to SPI bus {self.bus}, device {self.device}, speed {self.speed}Hz")
return True
except Exception as e:
print(f"SPI connection failed: {e}")
return False
def test_single_byte(self, test_byte):
"""Test a single byte loopback."""
if not self.connected:
print("Please connect to the SPI device first.")
return False
try:
rx_data = self.spi.xfer2([test_byte])
received_byte = rx_data[0]
if received_byte == test_byte:
print(f"Test passed - sent: 0x{test_byte:02X}, received: 0x{received_byte:02X}")
return True
else:
print(f"Test failed - sent: 0x{test_byte:02X}, received: 0x{received_byte:02X}")
return False
except Exception as e:
print(f"Test error: {e}")
return False
def test_multiple_bytes(self, test_data):
"""Test multiple bytes loopback."""
if not self.connected:
print("Please connect to the SPI device first.")
return False
try:
rx_data = self.spi.xfer2(test_data)
success = True
print("Multi-byte test result:")
print("Sent\tReceived\tResult")
print("-" * 30)
for tx, rx in zip(test_data, rx_data):
status = "PASS" if tx == rx else "FAIL"
if tx != rx:
success = False
print(f"0x{tx:02X}\t0x{rx:02X}\t{status}")
return success
except Exception as e:
print(f"Test error: {e}")
return False
def run_complete_test(self):
"""Run the complete SPI loopback test suite."""
print("\n===== Starting SPI loopback test =====")
print(f"Test configuration: bus={self.bus}, device={self.device}, speed={self.speed}Hz")
test_bytes = [0x00, 0xFF, 0xAA, 0x55, 0x33, 0xCC]
single_byte_result = True
print("\n----- Single byte test -----")
for b in test_bytes:
if not self.test_single_byte(b):
single_byte_result = False
print("\n----- Multi-byte sequence test -----")
sequence = list(range(0, 256, 16))
multi_byte_result = self.test_multiple_bytes(sequence)
print("\n----- Continuous data test -----")
continuous_data = list(range(10, 20))
continuous_result = self.test_multiple_bytes(continuous_data)
print("\n===== Test summary =====")
overall_result = single_byte_result and multi_byte_result and continuous_result
if overall_result:
print("✅ All SPI loopback tests passed!")
else:
print("❌ Some SPI loopback tests failed!")
return overall_result
def close(self):
"""Close the SPI connection."""
if self.connected:
self.spi.close()
self.connected = False
print("\nSPI connection closed")
if __name__ == "__main__":
tester = SPILoopbackTester(bus=1, device=0, speed=1000000)
try:
if tester.connect():
tester.run_complete_test()
time.sleep(1)
finally:
tester.close()- Save the file and run:
shell
sudo python3 spitest.py- Verify the output.
The result should indicate that all SPI loopback tests passed.