# rpi - SPI loop-back test
# provided by Anthony Vogelaar, Perth Scotland
# rev 0.10  2024-12-27  initial release

import spidev
import time

# ===================== Constants =====================

SPI_BUS	= 0          		# spidev0
SPI_CE0 = 0          		# spidev0.0
SPI_CLK	= 1000000  			# 1 Mhz

# ================== Hardware setup ===================

spi 			 = spidev.SpiDev()
spi.open(SPI_BUS, SPI_CE0)
spi.max_speed_hz = SPI_CLK
spi.mode         = 0b11

# ====================== Test =========================

# SPI loop-back test
def test():
    while True:
        data_tx = [0x31, 0x32]
        print("\nTX:", data_tx)
        data_rx = spi.xfer(data_tx)
        print("RX:", data_rx)
        time.sleep(0.5)
        
    
try:
    test()
except:
    print('Test aborted')
spi.close()
    
# ====================== End ==========================
