SPI Loopback Test V10 2021jan0201
# spitestv10.py tlfong01 2021jan02 hkt2209 ***
# *** Configuration Description ***
# Rpi4B Thonny Python 3.7.3 (/usr/bin/python3)
# pi@raspberrypi:~ $ date Fri 01 Jan 2021 12:17:54 PM HKT
# pi@raspberrypi:~ $ uname -a Linux raspberrypi 5.4.79-v7l+ #1373 SMP Mon Nov 23 13:27:40 GMT 2020 armv7l GNU/Linux
# *** Test Description ***
# Test 1 - loopBackTest() - SPI port send and receive one byte.
# Function - Send one byte to MSOI and read it back from MISO.
# Setup - Connet MOSI pin to MISO pin to form a loop.
# Test 2 - repeatSendByte() - SPI port repeatedly send out single bytes.
# Function - Repeat many times sending a byte, pause after each byte.
# *** I2C/SPI Config ***
'''
pi@raspberrypi:~ $ date Sat 02 Jan 2021 10:18:37 PM HKT
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 5.4.79-v7l+ #1373 SMP Mon Nov 23 13:27:40 GMT 2020 armv7l GNU/Linux
pi@raspberrypi:~ $ ls /dev/spi*
/dev/spidev0.0 /dev/spidev0.1 /dev/spidev1.0 /dev/spidev1.1 /dev/spidev1.2
pi@raspberrypi:~ $ ls /dev/i2c*
/dev/i2c-1 /dev/i2c-3
pi@raspberrypi:~ $ cat /boot/config.txt
disable_overscan=1
dtparam=i2c_arm=on
dtparam=spi=on
dtparam=audio=on
[pi4]
dtoverlay=vc4-fkms-v3d
max_framebuffers=2
[all]
enable_uart=1
# *** I2C Bus 3 - tlfong 2020dec29hkt2055 ***
dtoverlay=i2c3,pins_4_5
# *** Spi Bus1 - tlfong 2021jan02hkt2144
dtoverlay=spi1-3cs
# ***End of config.txt ***
pi@raspberrypi:~ $ ^C
'''
# *** Import ***
from datetime import datetime
from time import sleep
import spidev
# ** SPI Setup/Config ***
spiPort0 = spidev.SpiDev()
spiPort0.open(0,0)
spiPort0.max_speed_hz = 100000
spiPort1 = spidev.SpiDev()
spiPort1.open(1,0)
spiPort1.max_speed_hz = 100000
#*** System Functions ***
timeNowLong = str(datetime.now())
timeNowShort = str(datetime.now())[0:16]
# *** SPI System Functions ***
def spiSendRecvOneByte(spiPort, sendByte):
sendByteArray = [sendByte]
recvByteArray = spiPort.xfer(sendByteArray)
return recvByteArray
def loopBackOneByte(spiPort, sendByte):
recvByteArray = spiSendRecvOneByte(spiPort, sendByte)
recvByte = recvByteArray[0]
print(' Begin testLoopbackOneByte(),....')
#print('')
print(' Note 1 = Connect SPI MOSI pin to MISO pin to loop back!')
print(' Note 2 = Loopback results below:')
print(' sendByte = ', hex(sendByte))
print(' recvByte = ', hex(recvByte))
#print('')
print(' End testLoopbackOneByte(),....')
return
def repeatSendOneByte(spiPort, sendByte, pauseTimeBetweenBytes, repeatCount):
print('\nBegin repeatSendByte(),....')
for i in range(repeatCount):
spiSendRecvOneByte(spiPort, sendByte)
sleep(pauseTimeBetweenBytes)
print('End repeatSendByte().')
return
# *** SPI Test Functions ***
def testRepeatSendOneByte():
repeatSendOneByte(spiPort0, 0x5b, 0.0001, 20000000)
return
def testLoopbackOneByte():
loopBackOneByte(spiPort0, 0x5b)
return
# *** Init/Main Function ***
def init():
pass
return
def main():
#testRepeatSendOneByte()
#testLoopbackOneByte()
print('Begin main(), ... - tlfong01', timeNowShort)
#loopBackOneByte(spiPort0, 0x5b)
loopBackOneByte(spiPort1, 0x5c)
print('End main().')
return
# *** Main()
if __name__ == '__main__':
main()
# *** End of Program ***
# *** Sample Output ***
# Smple output tlfong
'''
>>> %Run sptestv0901.py
Begin main(), ... - tlfong01 2021-01-02 21:59
Begin testLoopbackOneByte(),....
Note 1 = Connect SPI MOSI pin to MISO pin to loop back!
Note 2 = Loopback results below:
sendByte = 0x5c
recvByte = 0x5c
End testLoopbackOneByte(),....
End main().
>>>
>>> %Run sptestv08_2021jan0103.py
Begin main(), ... - tlfong01 2021-01-01 12:37
Begin testLoopbackOneByte(),....
Note 1 = Connect SPI MOSI pin to MISO pin to loop back!
Note 2 = Loopback results below:
sendByte = 0x5b
recvByte = 0x5b
End testLoopbackOneByte(),....
End main().
>>>
'''
# *** End of Sample Output ***
No comments:
Post a Comment