Uncategorized

Program:

spi_util_01_v72.py tlfong01 2020aug06hkt2227

#

Function:

1. SPI one byte loopback

2. SPI repeatedly send one byte

#

System Config:

Rpi4B buster (r2020may23), python 3.7.3 (r2019dec20), thonny v3.2.7 (r2020jan22)

$ date Thu 25 Jun 2020 04:36:13 PM HKT

$ uname -a Linux raspberrypi 4.19.118-v7l+ #1311 SMP Mon Apr 27 14:26:42 BST 2020 armv7l GNU/Linux

$ ls /dev/ttyUSB* /dev/ttyUSB0

$ ls /dev/ttyS0 /dev/ttyS0

$ ls /dev/ttyAMA0 /dev/ttyAMA0

Test Function Definitions:

#

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.

1. SPI ports setup notes

To enable SPI and setup SPI 10, 11, 12 ports, add these two lines to /boot/config.txt

dtparam=spi=on

dtoverlay=spi1-3cs

To list SPI devices

pi@raspberrypi:~ $ ls /dev/spi*

/dev/spidev0.0 /dev/spidev0.1 /dev/spidev1.0 /dev/spidev1.1 /dev/spidev1.2

2. Notes of loopback function

Call example – testLoopbackOneByte(spiPort00)

Function – send byte 0x5b to SPI MOSI and read byte from MISO

Setup – must connect MOSI pin to MISO pin to loop back

Note – 1. Only checks if MISO echoes MOSI, CS0, CS1, CS2 is not checked

2. To check if SPI 0, CS0, CS1, or SPI1 CS0, CS1, CS2, need a scope to display repeat send bytes

from time import sleep
import spidev
import inspect
from datetime import datetime

*** SPI Port Setup/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

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 Port Dict ***

spiPortDict = {
‘SpiPort00’: spiPort00,
‘SpiPort01’: spiPort01,
‘SpiPort10’: spiPort10,
‘SpiPort11’: spiPort11,
‘SpiPort12’: spiPort12,
}

dataByteDict = {
‘0x00’ : 0x00,
‘0x5a’ : 0x5a,
‘0x5b’ : 0x5b,
‘0x5c’ : 0x5c,
‘0x5d’ : 0x5d,
‘0x5e’ : 0x5e,
‘0x5f’ : 0x5f,
‘0x55’ : 0x55,
‘0x66’ : 0x66,
‘0x77’ : 0x77,
}

*** SPI Contorl Byte Dict ***

repeatTimesDict = {
‘100 times’ : 100,
‘1000 times’ : 1000,
‘10000 times’ : 10000,
‘100000 times’ : 100000,
‘1000000 times’ : 1000000,
‘10000000 times’ : 10000000
}

pauseSecondsDict = {
‘0.001 second’ : 0.001,
‘0.002 second’ : 0.002,
‘0.01 second’ : 0.01,
‘0.1 second’ : 0.1
}

*** Print Utilities ***

indentFormat480 = [4, 80]
indentFormat608 = [6, 8]
indentFormat610 = [6, 10]
indentFormat615 = [6, 15]
indentFormat630 = [6, 30]
indentFormat640 = [6, 40]

def convertOneByteNumToFourCharStr(oneByteNum):
tempStr = ((hex(oneByteNum))[2:])
if (len(tempStr) != 2):
tempStr = ‘0’ + tempStr
fourCharStr = ‘0x’ + tempStr
return fourCharStr

def printTitle(title, indentFormat):
print((‘ ‘ * (indentFormat[0])), title.ljust(indentFormat[1]))
return

def printTitleNoNewLine(title, indentFormat):
print((‘ ‘ * (indentFormat[0])), title.ljust(indentFormat[1]), end = ”)
return

def printTitleString(title, indentFormat, string):
printTitleNoNewLine(title, indentFormat)
print(‘=’, string)
return

def printStarLine():
print(”)
printTitle((‘*’ * 100), indentFormat480)
print(”)
return

def printBeginExecFunction():
functionName = inspect.stack()[1][3]
title = ‘Begin Execute Function ‘ + functionName + ‘ ‘ + str(datetime.now())[0:16]
printStarLine()
printTitle(title, indentFormat480)
print(”)
printTitleString(‘Function Name’, indentFormat640, functionName)
return

def printEndExecFunction():
title = ‘End Execute Function ‘ + inspect.stack()[1][3] + ‘ ‘ + str(datetime.now())[0:16]
print(”)
printTitle(title, indentFormat480)
printStarLine()
return

*** SPI Send/Receive/Loopback/RepeatSend Byte Functions ***

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

def spiSendRecvTwoBytes(spiPort, sendByte1, sendByte2):
sendByteArray = [sendByte1, sendByte2]
recvByteArray = spiPort.xfer2(sendByteArray)
return recvByteArray

def spiSendRecvThreeBytes(spiPort, sendByte1, sendByte2, sendByte3):
sendByteArray = [sendByte1, sendByte2, sendByte3]
recvByteArray = spiPort.xfer2(sendByteArray)
return recvByteArray

def spiSendRecvOneByteByName(spiPortName, sendByteName):
spiPort = spiPortDict[spiPortName]
sendByte = dataByteDict[sendByteName]

recvByteArray = spiSendRecvOneByte(spiPort, sendByte)
return recvByteArray

def spiSendRecvTwoBytesByName(spiPortName, sendByte1Name, sendByte2Name):
print(sendByte1Name)
spiPort = spiPortDict[spiPortName]
sendByte1 = dataByteDict[sendByte1Name]
sendByte2 = dataByteDict[sendByte2Name]
recvByteArray = spiSendRecvOneByte(spiPort, sendByte1. sendByte2)
return recvByteArray

def spiSendRecvThreeBytesByName(spiPortName, sendByte1Name, sendByte2Name, sendByte3Name):
spiPort = spiPortDict[spiPortName]
sendByte1 = dataByteDict[sendByte1Name]
sendByte2 = dataByteDict[sendByte2Name]
sendByte3 = dataByteDict[sendByte3Name]
recvByteArray = spiSendRecvOneByte(spiPort, sendByte1. sendByte2, sendByte3)
return recvByteArray

def loopBackOneByte(spiPort, sendByte):
recvByteArray = spiSendRecvOneByte(spiPort, sendByte)
recvByte = recvByteArray[0]

#print(‘\nBegin testLoopbackOneByte(),….’)
#print(”)
print(‘ sendByte = ‘, hex(sendByte))
print(‘ recvByte = ‘, hex(recvByte))
#print(”)
#print(‘End testLoopbackOneByte(),….’)
return

def loopbackTwoBytes(spiPort, sendByte1, sendByte2):
#fprint.printBeginExecFunction()
recvByteArray = spiSendRecvTwoBytes(spiPort, sendByte1, sendByte2)
recvByte1 = recvByteArray[0]
recvByte2 = recvByteArray[1]
sendBytes = convertOneByteNumToFourCharStr(sendByte1) + ‘ ‘ + convertOneByteNumToFourCharStr(sendByte2)
printTitleString(‘sendBytes’, indentFormat640, sendBytes)
recvBytes = convertOneByteNumToFourCharStr(recvByte1) + ‘ ‘ + convertOneByteNumToFourCharStr(recvByte2)
printTitleString(‘recvBytes’, indentFormat640, recvBytes)
#printEndExecFunction()
return

def loopbackThreeBytes(spiPort, sendByte1, sendByte2, sendByte3):
#fprint.printBeginExecFunction()
recvByteArray = spiSendRecvThreeBytes(spiPort, sendByte1, sendByte2, sendByte3)
recvByte1 = recvByteArray[0]
recvByte2 = recvByteArray[1]
recvByte3 = recvByteArray[2]
sendBytes = convertOneByteNumToFourCharStr(sendByte1) + ‘ ‘ + convertOneByteNumToFourCharStr(sendByte2) + ‘ ‘ + \
convertOneByteNumToFourCharStr(sendByte3)
printTitleString(‘sendBytes’, indentFormat640, sendBytes)
recvBytes = convertOneByteNumToFourCharStr(recvByte1) + ‘ ‘ + convertOneByteNumToFourCharStr(recvByte2) + ‘ ‘ + \
convertOneByteNumToFourCharStr(recvByte3)
printTitleString(‘sendBytes’, indentFormat640, recvBytes)
#printEndExecFunction()
return

def loopBackOneByteByName(spiPortName, sendByteName):
#printBeginExecFunction()
spiPort = spiPortDict[spiPortName]
sendByte = dataByteDict[sendByteName]
loopBackOneByte(spiPort, sendByte)
#printEndExecFunction()
return

def loopBackTwoBytesByName(spiPortName, sendByte1Name, sendByte2Name):
#printBeginExecFunction()
spiPort = spiPortDict[spiPortName]
sendByte1 = dataByteDict[sendByte1Name]
sendByte2 = dataByteDict[sendByte2Name]

loopbackTwoBytes(spiPort, sendByte1, sendByte2)
#printEndExecFunction()
return

def loopBackThreeBytesByName(spiPortName, sendByte1Name, sendByte2Name, sendByte3Name):
#printBeginExecFunction()
spiPort = spiPortDict[spiPortName]
sendByte1 = dataByteDict[sendByte1Name]
sendByte2 = dataByteDict[sendByte2Name]
sendByte3 = dataByteDict[sendByte3Name]

loopbackThreeBytes(spiPort, sendByte1, sendByte2, sendByte3)
#printEndExecFunction()
return

def testLoopbackOneByte(spiPort, dataByte):
loopackOneByte(spiPort, dataByte)
return

def testLoopbackOneByteByName(spiPortName, dataByteName):
loopackOneByteByName(spiPortName, dataByteName)
return

def testLoopbackTwoBytesByName(spiPortName, dataByte1Name, dataByte2Name):
spiPort = spiPortDict[spiPortName]
dataByte1 = dataByteDict[dataByte1Name]
dataByte2 = dataByteDict[testByte2Name]
loopbackTwoBytes(spiPort, datatByte1, dataByte2)
return

def testLoopbackThreeBytesByName(spiPortName, dataByte1Name, dataByte2Name, dataByte3Name):
spiPort = spiPortDict[spiBusName]
dataByte1 = dataByteDict[dataByte1Name]
dataByte2 = dataByteDict[dataByte2Name]
dataByte3 = dataByteDict[testByte3Name]
loopbackThreeBytes(spiPort, dataByte1, dataByte2, datatByte3)
return

def testLoopbackOneByteByNameSpiPort00x5b():
printBeginExecFunction()
loopBackOneByteByName(‘SpiPort00’, ‘0x5b’)
printEndExecFunction()
return

def testLoopbackTwoBytesByNameSpiPort00x5bx5c():
printBeginExecFunction()
loopBackTwoBytesByName(‘SpiPort00’, ‘0x5b’, ‘0x5c’)
printEndExecFunction()
return

def testLoopbackThreeBytesByNameSpiPort00x5bx5cx5d():
printBeginExecFunction()
loopBackThreeBytesByName(‘SpiPort00’, ‘0x5b’, ‘0x5c’, ‘0x5d’)
printEndExecFunction()
return

*** Test Functions ***

*** Test SPI Loopback Functions, Comment out not required tests ***

def testLoopbackSpiPortsAll():
print(‘\n*** testLoopbackOneByte(spiPort00, 0x5b) ***’, end = ”)
testLoopbackOneByte(spiPort00, 0x5b)

print('\n*** testLoopbackOneByte(spiPort01, 0x5b) ***', end = '')
testLoopbackOneByte(spiPort01, 0x5b)

print('\n*** testLoopbackOneByte(spiPort10, 0x5b) ***', end = '')
testLoopbackOneByte(spiPort10, 0x5b)

print('\n*** testLoopbackOneByte(spiPort11, 0x5b) ***', end = '')
testLoopbackOneByte(spiPort11, 0x5b)

print('\n*** testLoopbackOneByte(spiPort12, 0x5b) ***', end = '')
testLoopbackOneByte(spiPort12, 0x5b)

print('\n*** testLoopbackOneByteByName("spiPort00", "0x5b") ***', end = '')
testLoopbackOneByteByName('SpiPort00', '0x5b')
return

def testLoopbackSpiPortsAll():
print(‘\n*** testLoopbackOneByte(spiPort00, 0x5b) ***’, end = ”)
testLoopbackOneByte(spiPort00, 0x5b)

print('\n*** testLoopbackOneByte(spiPort01, 0x5b) ***', end = '')
testLoopbackOneByte(spiPort01, 0x5b)

print('\n*** testLoopbackOneByte(spiPort10, 0x5b) ***', end = '')
testLoopbackOneByte(spiPort10, 0x5b)

print('\n*** testLoopbackOneByte(spiPort11, 0x5b) ***', end = '')
testLoopbackOneByte(spiPort11, 0x5b)

print('\n*** testLoopbackOneByte(spiPort12, 0x5b) ***', end = '')
testLoopbackOneByte(spiPort12, 0x5b)

print('\n*** testLoopbackOneByteByName("spiPort00", "0x5b") ***', end = '')
testLoopbackOneByteByName('SpiPort00', '0x5b')
return

def testLoopbackOneTwoThreeBytesSpiPorts00ByName():
testLoopbackOneByteByNameSpiPort00x5b()
testLoopbackTwoBytesByNameSpiPort00x5bx5c()
testLoopbackThreeBytesByNameSpiPort00x5bx5cx5d()
return

*** Repeat send byte functions ***

def repeatSendOneByte(spiPort, sendByte, pauseSeconds, repeatTimes):
print(‘\nBegin repeatSendByte(),….’)
print(‘ Now use a scope to display the SPI signals, MOSI, MISO, CSn, …’)
for i in range(repeatTimes):
spiSendRecvOneByte(spiPort, sendByte)
sleep(pauseSeconds)
print(‘End repeatSendByte().’)
return

def repeatSendOneByte1(spiPort, sendByte, pauseSeconds, repeatTimes):
print(‘ Begin repeatSendByte(),….’)
print(‘ Now use a scope to display the SPI signals, MOSI, MISO, CSn, …’)
for i in range(repeatTimes):
spiSendRecvOneByte(spiPort, sendByte)
sleep(pauseSeconds)
print(‘ End repeatSendByte().’, end = ”)
return

def repeatSendOneByteByName(spiPortName, sendByteName, pauseSecondsName, repeatTimesName):
spiPort = spiPortDict[spiPortName]
sendByte = dataByteDict[sendByteName]
pauseSeconds = pauseSecondsDict[pauseSecondsName]
repeatTimes = repeatTimesDict[repeatTimesName]
repeatSendOneByte1(spiPort, sendByte, pauseSeconds, repeatTimes)
return

def testRepeatSendOneByte(spiPort, sendByte, pauseSeconds, repeatTimes):
repeatSendOneByte(spiPort, sendByte, pauseSeconds, repeatTimes)
return

def testRepeatSendOneByteByName(spiPortName, sendByteName, pauseSecondsName, repeatTimesName):
repeatSendOneByteByName(spiPortName, sendByteName, pauseSecondsName, repeatTimesName)
return

def testRepeatSendOneByteByNameSpiPort000x5b0001Seconds100Times():
testRepeatSendOneByteByName(‘SpiPort00’, ‘0x5b’, ‘0.001 second’, ‘100 times’)
return

def testRepeatSendOneByteSpiPortsAll():
print(‘\n*** testRepeatSendOneByte(spiPort00, 0x5b, 0.001, 2x) ***’, end = ”)
testRepeatSendOneByte(spiPort00, 0x5b, 0.001, 200)

print('\n*** testRepeatSendOneByte(spiPort01, 0x5b, 0.001, 2x) ***', end = '')
testRepeatSendOneByte(spiPort01, 0x5b, 0.001, 200)

print('\n*** testRepeatSendOneByte(spiPort10, 0xb5, 0.001, 2x) ***', end = '')
testRepeatSendOneByte(spiPort10, 0xb5, 0.001, 200)

print('\n*** testRepeatSendOneByte(spiPort11, 0xb5, 0.001, 2x) ***', end = '')
testRepeatSendOneByte(spiPort11, 0xb5, 0.001, 200)

print('\n*** testRepeatSendOneByte(spiPort12, 0xb5, 0.001, 2x) ***', end = '')
testRepeatSendOneByte(spiPort12, 0xb5, 0.001, 200)

return

def testRepeatSendOneByteSpiPortsAllByName():
printBeginExecFunction()
testRepeatSendOneByteByName(‘SpiPort00’, ‘0x5b’, ‘0.001 second’, ‘100 times’)
printEndExecFunction()
return

*** Main ***

def main():

#testLoopbackSpiPortsAll()
#testLoopbackOneTwoThreeBytesSpiPorts00ByName()

#testRepeatSendOneByteSpiPortsAll()
testRepeatSendOneByteSpiPortsAllByName()

**

if name == ‘main‘:
main()

End of program

*** Sample Output ***

*** End ***

Program:

iox_util01_v69.py tlfong01 2020aug065hkt1528

#

Function:

Test (1) SPI loockback and repeat send byte

(2) setup MCP23017 and write/read registers

import spi_util_01_v67 as spiutil

*** Main ***

def testSpiUtilV01():

# *** Test SPI Loopback Functions, Comment out not required tests ***

print('\n*** testLoopbackOneByte(spiPort00, 0x5b) ***', end = '')
spiutil.testLoopbackOneByte(spiutil.spiPort00, 0x5b)

print('\n*** testLoopbackOneByte(spiPort01, 0x5b) ***', end = '')
spiutil.testLoopbackOneByte(spiutil.spiPort01, 0x5b)

print('\n*** testLoopbackOneByte(spiPort10, 0x5b) ***', end = '')
spiutil.testLoopbackOneByte(spiutil.spiPort10, 0x5b)   

print('\n*** testLoopbackOneByte(spiPort11, 0x5b) ***', end = '')
spiutil.testLoopbackOneByte(spiutil.spiPort11, 0x5b)

print('\n*** testLoopbackOneByte(spiPort12, 0x5b) ***', end = '')
spiutil.testLoopbackOneByte(spiutil.spiPort12, 0x5b)

# *** Test SPI Repeat Send One Byte Functions, Comment out not required tests ***

print('\n*** testRepeatSendOneByte(spiPort00, 0x5b, 0.001, 200) ***', end = '')
spiutil.testRepeatSendOneByte(spiutil.spiPort00, 0x5b, 0.001, 2000)

print('\n*** testRepeatSendOneByte(spiPort01, 0x5b, 0.001, 200) ***', end = '')
spiutil.testRepeatSendOneByte(spiutil.spiPort01, 0x5b, 0.001, 2000)

print('\n*** testRepeatSendOneByte(spiPort10, 0x5b, 0.001, 200) ***', end = '')
spiutil.testRepeatSendOneByte(spiutil.spiPort10, 0x5b, 0.001, 2000)

print('\n*** testRepeatSendOneByte(spiPort11, 0x5b, 0.001, 200) ***', end = '')
spiutil.testRepeatSendOneByte(spiutil.spiPort11, 0x5b, 0.001, 2000)

print('\n*** testRepeatSendOneByte(spiPort12, 0x5b, 0.001, 200) ***', end = '')
spiutil.testRepeatSendOneByte(spiutil.spiPort12, 0x5b, 0.001, 2000)

return

*** mcp23s17 Utilities ***

*** Mcp23s17 Data/Control Bytes ***

timeNowStr = str(datetime.datetime.now())[0:16]
ioDirRegAddrByteA = 0x00
dataByte0x55 = 0x55
dataByte0xaa = 0xaa

*** Mcp23s17 Functions ***

def readDvRegOneByte (i2cPort, dvAddrByte, DvRegAddrByte):
readByte = i2cPort.read_byte_data ( dvAddrByte, DvRegAddrByte)
return readByte

def writeDvRegOneByte (i2cPort, dvAddrByte, DvRegAddrByte, writeByte):
writeDviTwoBytes (i2cPort, dvAddrByte, DvRegAddrByte, writeByte)
return

def writeDviTwoBytes (i2cPort, dvAddrByte, dataByte1, dataByte2):
i2cPort.write_byte_data ( dvAddrByte, dataByte1, dataByte2)
return

def printDvRegOneByte (i2cPort, dvAddrByte, DvRegAddrByte, printTitle):
readByte = readDvRegOneByte (i2cPort, dvAddrByte, DvRegAddrByte)
print (printTitle, hex(readByte))

*** Test Functions ***

def testWriteReadPrintIoDirRegA(testDataByte):
printDvRegOneByte(i2cCh1, dvAddrByte, ioDirRegAddrByteA, ‘Old Byte =’)
writeDvRegOneByte(i2cCh1, dvAddrByte, ioDirRegAddrByteA, testDataByte)
printDvRegOneByte(i2cCh1, dvAddrByte, ioDirRegAddrByteA, ‘New Byte =’)
return

def test():
print(‘*** TimeNow = ‘, timeNowStr, ‘\n’) print(‘‘, programTitle, ‘Begin \n’) testWriteReadPrintIoDirRegA(dataByte0x55) testWriteReadPrintIoDirRegA(dataByte0xaa) print(‘\n’) print(‘‘, programTitle, ‘End ***’)
return

”’
regAddrByteDict = \
{
‘IODIRA’ : 0x00,
‘IODIRB’ : 0x01,
‘IPOLA’ : 0x02,
‘IPOLB’ : 0x03,
‘GPINTENA’ : 0x04,
‘GPINTENB’ : 0x05,
‘DEFVALA’ : 0x06,
‘DEFVALB’ : 0x07,
‘INTCONA’ : 0x08,
‘INTCONB’ : 0x09,
‘IOCON’ : 0x0b,
‘IOCON’ : 0x0a,
‘GPPUA’ : 0x0c,
‘GPPUB’ : 0x0d,
‘INTFA’ : 0x0e,
‘INTFB’ : 0x0f,
‘INTCAPA’ : 0x10,
‘INTCAPB’ : 0x11,
‘GPIOA’ : 0x12,
‘GPIOB’ : 0x13,
‘OLATA’ : 0x14,
‘OLATB’ : 0x15,
‘PortA’ : \
{
‘Direction’ : 0x00,
‘OutLatch’ : 0x14,
‘GpioPort’ : 0x12,
‘IntPinsEnable’ : 0x04,
‘IntPinsDefault’ : 0x06,
‘IntPinsCompare’ : 0x08,
‘InterruptFlagByte’ : 0x0e,
‘IntCap’ : 0x10,
‘PulregAddrByte = \
{
‘IODIRA’ : 0x00,
‘IODIRB’ : 0x01,
‘IPOLA’ : 0x02,
‘IPOLB’ : 0x03,
‘GPINTENA’ : 0x04,
‘GPINTENB’ : 0x05,
‘DEFVALA’ : 0x06,
‘DEFVALB’ : 0x07,
‘INTCONA’ : 0x08,
‘INTCONB’ : 0x09,
‘IOCON’ : 0x0b,
‘IOCON’ : 0x0a,
‘GPPUA’ : 0x0c,
‘GPPUB’ : 0x0d,
‘INTFA’ : 0x0e,
‘INTFB’ : 0x0f,
‘INTCAPA’ : 0x10,
‘INTCAPB’ : 0x11,
‘GPIOA’ : 0x12,
‘GPIOB’ : 0x13,
‘OLATA’ : 0x14,
‘OLATB’ : 0x15,
‘PortA’ : \
{
‘Direction’ : 0x00,
‘OutLatch’ : 0x14,
‘GpioPort’ : 0x12,
‘IntPinsEnable’ : 0x04,
‘IntPinsDefault’ : 0x06,
‘IntPinsCompare’ : 0x08,
‘InterruptFlagByte’ : 0x0e,
‘IntCap’ : 0x10,
‘PullUp’ : 0x0c
},
‘PortB’ : \
{
‘Direction’ : 0x01,
‘OutLatch’ : 0x15,
‘GpioPort’ : 0x13,
‘IntPinsEnable’ : 0x05,
‘IntPinsDefault’ : 0x07,
‘IntPinsCompare’ : 0x09,
‘InterruptFlagByte’ : 0x0f,
‘IntCap’ : 0x11,
‘PullUp’ : 0x0d
},
}
}
”’

controlByteDict = \
{
‘WriteRegBaseByte’ : 0x40,
‘ReadRegBaseByte’ : 0x41,
‘EnableAddressPins’ : 0x08,
‘AllPinsOutput’ : 0x00,
‘AllPinsInput’ : 0xff,
‘Pin0OutputOtherPinsInput’ : 0xfe,
‘LowNibblePinsOutputHighNibblePinsInput’ : 0xf0,
‘IntAllPinsEnable’ : 0xff,
‘IntPinsEnable’ : 0xff,
‘IntAllPinsCompareDefault’ : 0xff,
‘AllPinsPullUp’ : 0xff,
‘AllHigh’ : 0xff,
‘AllLow’ : 0x00,
‘AllPinsHigh’ : 0xff,
‘AllPinsLow’ : 0x00,
‘IntAllPinsHigh’ : 0xff,
‘IntAllPinsLow’ : 0x00,
‘IntLowNibblePinsEnable’ : 0x0f,
‘IntLowNibblePinsComparePrevious’ : 0x00,
‘0x01’ : 0x01,
‘0x55’ : 0x55,
‘0x66’ : 0x66,
‘0xaa’ : 0xaa,
‘0x0f’ : 0x0f,
‘0xf0’ : 0xf0,
}

# *** mcp23s17 Write/Read Functions ***

def mcp23s17WritedDevRegOneByte(spiPortName, devRegName, writeByteName, devRegAddrDict, dataByteDict):
spiPort = spiutil.spiPortDict[spiPortName]
devRegAddr = devRegAddrDict[devRegName]
writeDevRegAddr = devRegAddr << 1
writeByte = dataByteDict[writeByteName]
spiutil.spiSendRecvTwoBytes(spiBus, writeDevRegAddr, writeByte)
return

def mcp23s17ReadDevRegOneByte(spiBusName, devRegAddrDict, devRegName):
spiBus = spiBusDict[spiBusName]
devRegAddr = devRegAddrDict[devRegName]
readDevRegAddr = (devRegaddr << 1) | 0x01
dummyByte = 0x00
recvByteArray = spiSendRecvTwoBytes(spiBus, readDevRegAddr, dummyByte)
readByte = recvByteArray[1]
return readByte

*** Main ***

def main():
testSpiUtilV01()
return

if name == ‘main‘:
main()

End of program

*** Sample Output ***

*** 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.