Welcome to RDK X5 Board repository
5.Using SPI
Using SPI
RDK X5 leads out the chip's SPI1 bus on the physical pins 19, 21, 23, 24, 26 on the 40PIN, supports two chip selects, and the IO voltage is 3.3V.
Please refer to /app/40pin_samples/test_spi.py for details on how to use SPI.
Loopback Test
Connect MISO and MOSI pins together on the hardware, then run the spi test program to perform write and read operations. The expected result is that the read data should be identical to the written data.
Hardware Connection
Connect MISO and MOSI directly together on the hardware using a jumper cap.

Test Procedure
- Run
python3 /app/40pin_samples/test_spi.py - Select the bus number and chip select number from the printed spi controllers as input options. For example, if you want to test
spidev0.0, then bothbus numandcs numshould be0. Press enter to confirm:
xxxxxxxxxxList of enabled spi controllers:/dev/spidev0.0 /dev/spidev0.1Please input SPI bus num:0Please input SPI cs num:0
- Once the program is running correctly, it will continuously print
0x55 0xAA. If it prints0x00 0x00, then the loopback test of SPI has failed.
xxxxxxxxxxStarting demo now! Press CTRL+C to exit0x55 0xAA0x55 0xAA
Test Code
xxxxxxxxxx#!/usr/bin/env python3
from __future__ import print_functionimport sysimport osimport time
```# Import the spidev module import spidev
def BytesToHex(Bytes): return ''.join(["0x%02X " % x for x in Bytes]).strip()
def spidevTest(): # Set the spi bus number (0, 1, 2) and cs (0, 1) spi_bus = input("Please input SPI bus num:") spi_device = input("Please input SPI cs num:") # Create an object of the spidev class to access the Python functions based on spidev spi = spidev.SpiDev() # Open the spi bus handle spi.open(int(spi_bus), int(spi_device))
# Set spi frequency to 12MHz spi.max_speed_hz = 12000000
print("Starting demo now! Press CTRL+C to exit")
# Send [0x55, 0xAA] and receive should also be [0x55, 0xAA] try: while True: resp = spi.xfer2([0x55, 0xAA]) print(BytesToHex(resp)) time.sleep(1)
except KeyboardInterrupt: spi.close()
if __name__ == '__main__': print("List of enabled spi controllers:") os.system('ls /dev/spidev*')
spidevTest()