SPI
Note
The following example uses the Raspberry Pi-compatible 40PIN pins of Rhino Pi-X1. For their specific locations, refer to Hardware Information.
SPI Overview Serial Peripheral Interface (SPI) is a de facto standard for synchronous serial communication (with many variants), primarily used for short-distance wired communication between integrated circuits in embedded systems.
Preparation
- One Rhino Pi-X1 device
- One female Dupont wire
Connection
Use the Dupont wire to short pins 19 and 21.
Testing
- Check the spidev device:
shell
ls /dev/spidev*- Open the terminal and install the required Python library with the following command:
shell
sudo pip3 install spidev- Create a new 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 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 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 loopback of a single byte"""
if not self.connected:
print("Please connect to SPI device first")
return False
try:
# Send and receive data
rx_data = self.spi.xfer2([test_byte])
received_byte = rx_data[0]
# Verify result
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 loopback of multiple bytes"""
if not self.connected:
print("Please connect to SPI device first")
return False
try:
# Send and receive data
rx_data = self.spi.xfer2(test_data)
# Verify result
success = True
print("Multi-byte test results:")
print("Sent\tReceived\tResult")
print("-" * 30)
for tx, rx in zip(test_data, rx_data):
status = "Passed" if tx == rx else "Failed"
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 complete test suite"""
print("\n===== Starting SPI Loopback Test =====")
print(f"Test configuration: Bus={self.bus}, Device={self.device}, Speed={self.speed}Hz")
# Test single special bytes
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
# Test random sequence
print("\n----- Multi-byte Sequence Test -----")
sequence = list(range(0, 256, 16)) # 0x00, 0x10, 0x20, ..., 0xF0
multi_byte_result = self.test_multiple_bytes(sequence)
# Test continuous data
print("\n----- Continuous Data Test -----")
continuous_data = list(range(10, 20)) # Continuous numbers from 10 to 19
continuous_result = self.test_multiple_bytes(continuous_data)
# Overall result
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 SPI connection"""
if self.connected:
self.spi.close()
self.connected = False
print("\nSPI connection closed")
if __name__ == "__main__":
# Create tester instance (default uses SPI0, CE0)
tester = SPILoopbackTester(bus=1, device=0, speed=1000000)
try:
if tester.connect():
# Run complete test
tester.run_complete_test()
time.sleep(1)
finally:
# Ensure resources are released properly
tester.close()- After saving, run the test with the following command:
shell
sudo python3 spitest.py- Check the output result: The result will print "✅ All SPI loopback tests passed!".