Uncategorized

SPI Loopback test

SPI Loopback test

# SPI Loopback Test - tlfong01 2019may29

# Using two SPI ports simultaneously on Raspberry Pi 0 W - Asked 2019may29, Viewed 385 times
# https://raspberrypi.stackexchange.com/questions/99079/using-two-spi-ports-simultaneously-on-raspberry-pi-0-w

# spi_loopback_onebyte_v44_2019may3001 tlfong01 2019may30hkt1958 ***

# Rpi3B+ stretch 2019apr08, IDLE python 3.5.3 

# Test     - loopBackTest - SPI port send and receive one byte/two bytes/three bytes.
# Function - Send one byte/two bytes/three bytes to MOSI and read it back from MISO. 
# Setup    - Connet MOSI pin to MISO pin to form a loop.

# /boot/config.txt dtoverlay setting 
# dtparam=i2c_arm=on
# dtparam=spi=on
# dtoverlay=spi1-3cs

# /dev/spi listing
# ls -l /dev/spi*
# should show the following:
# 1. /dev/spidev0.0
# 2. /dev/spidev0.1
# 3. /dev/spidev1.0
# 4. /dev/spidev1.1
# 5. /dev/spidev1.2

from   time import sleep
import spidev

# *** SPI Channel 0 Config ***

spiPort00 = spidev.SpiDev()
spiPort00.open(0,0)
spiPort00.max_speed_hz = 100000

spiPort01 = spidev.SpiDev()
spiPort01.open(0,1)
spiPort01.max_speed_hz = 100000

# *** SPI Channel 1 Config ***

spiPort10 = spidev.SpiDev()
spiPort10.open(1,0)
spiPort10.max_speed_hz = 100000

spiPort11 = spidev.SpiDev()
spiPort11.open(1,1)
spiPort11.max_speed_hz = 100000

spiPort12 = spidev.SpiDev()
spiPort12.open(1,2)
spiPort12.max_speed_hz = 100000

# *** SPI Send/Receive OneByte/TwoBytes/ThreeBytes ***

def spiSendRecvOneByte(spiPort, sendByte):
    sendByteArray = [sendByte]
    recvByteArray = spiPort.xfer(sendByteArray)    
    return recvByteArray

# *** Loopback OneByte/TwoBytes/ThreeBytes ***

def loopbackOneByte(spiPort, sendByte):
    recvByteArray     = spiSendRecvOneByte(spiPort, sendByte)
    recvByte          = recvByteArray[0]
    print('\n  Begin loopbackOneByte(),....')
    #print('')
    print('      sendByte  = ', hex(sendByte))
    print('      recvByte  = ', hex(recvByte))
    #print('')
    print('  End   loopbackOneByte(),....', end = '')
    return

# *** Tests to loopback SPI Channel 0  ***

def testLoopbackOneByteSpiPort0():
    print('\nBegin loopbackOneByteSpiPort0(),....', end = '')
    loopbackOneByte(spiPort00, 0x5b)
    print('\nEnd   loopbackOneByteSpiPort0(),....', end = '')
    return

# *** Tests to loopback SPI Channel 1 ***

def testLoopbackOneByteSpiPort1():
    print('\nBegin   loopbackOneByteSpiPort1(),....', end = '')
    loopbackOneByte(spiPort10, 0x5b)
    print('\nEnd   loopbackOneByteSpiPort1(),....', end = '')
    return

# *** Main Tests ***

# *** Loopback Tests ***

#testLoopbackOneByteSpiPort0()
testLoopbackOneByteSpiPort1()

# *** Sample Output ***

''' Smple output 
Begin loopbackOneByteSpiPort0(),....
  Begin loopbackOneByte(),....
      sendByte  =  0x5b
      recvByte  =  0x5b
  End   loopbackOneByte(),....
End   loopbackOneByteSpiPort0(),....
'''
''' Smple output 
Begin   loopbackOneByteSpiPort1(),....
  Begin loopbackOneByte(),....
      sendByte  =  0x5b
      recvByte  =  0x5b
  End   loopbackOneByte(),....
End   loopbackOneByteSpiPort1(),....
'''

# *** End ***
The above is a python program to do SPI loopback test. I usually don’t like to use bash program and prefer to use this python program to check that SPI is working OK.
.END

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.