Now I am refactoring the FongToy modules ftmain, ftiox, and ftdemux. The main test module ftmain.py has the following 5 test functions:
1. ftspi.TestSpiLoopBack(...)
2. ftiox.TestMcp23s17BlinkLed(...)
3. fteeprom.TestWriteReadEepormDataByte(...)
4. ftguzuntypi.TestGuzuntyPi4digit7segmentLedModule(...)
5. ftdemux.TestSelectSpiSlaveDevice(...)
These test functions can be used to troubleshoot the following:
1. If SPI can do MOSI MISO loop back.
2. If MCP23S17's 16 output pins can blink 16 LEDs.
3. If 2 EEPROM 25LC256s can write and read back a byte.
4. If Guzunty Pi's LED driver can display 4 digit LED module.
5. If both 3V3 and 5V0 HC137s can select 1 of 8 outputs
So far all the 5 tests look so good.
The old version of ftdemux calls WriteDataByteMcp23s17() with 6 parameters to write a byte to MCP23S17's output latch register.
There are 3 parameters which are function invariant. So I wrote a new ftiox
function ftiox.WriteDataByteMcp23s17OutputLatchPortA without those parameters, and making the function less verbose, les error prone, and more readable.
ftiox.WriteDataByteMcp23s17(spiChannel, spiIoxSubAddress,
ftiox.RegisterAddressArrayMcp23s17, ftiox.OutputLatchIndex,
ftiox.PortA, DisableHC137ControlByte)
ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, DisableHC137ControlByte)
.END
# ftmain v1.2 tlfong01 2013may22
# *** Program Description ***
ProgramTitle = "FongToy v1.2 tlfong01 2013may22 "
# *** Python imports ***
import sys
import time
import smbus
import pdb
import spidev
import wiringpi
import wiringpi2
import RPIO as GPIO
from RPIO import PWM
from enum import Enum
from subprocess import call
# *** FongToy imports ***
import ftio
import ftprint
import ftspi
import ftiox
import fteeprom
import ftguzuntypi
import ftdemux
# *** Main function ***
ftprint.StartProgram(ProgramTitle)
# ftspi.TestSpiLoopBack(spiChannelNumber = 0, spiChipEnableNumber = 1, testDataByte = 0x55, testCount = 1000, testTime = 0.001)
# ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 0, spiChipSubAddress = 0)
# fteeprom.TestWriteReadEepormDataByte(spiChannelNumber = 0, spiChipEnableNumber = 1, startAddress = 0x0410, testDataByte = 0x55)
# ftguzuntypi.TestGuzuntyPi4digit7segmentLedModule(spiChannelNumber = 0, spiChipEnableNumber = 1)
ftdemux.TestSelectSpiSlaveDevice(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 5)
ftprint.StopProgram()
#.END
# ftiox.py v1.2 tlfong01 2013may22
import time
import spidev
import ftspi
PortA = 0
PortB = 1
InputOutputDirectionIndex = 0
InputPolarityIndex = 1
InterruptEnableIndex = 2
DefaultValueIndex = 3
CompareModeIndex = 4
BankInterruptPinModeIndex = 5
PullUpIndex = 6
InterruptFlagIndex = 7
InterruptCaptureIndex = 8
PortStatusIndex = 9
OutputLatchIndex = 10
All8pinOutput = 0x00
All8pinInput = 0xff
All8bitOne = 0xff
All8bitZero = 0x00
All8bitPullUp = 0xff
Upper8bitOneLower8bitZero = 0xff00
Upper8bitZeroLower8bitOne = 0x00ff
Upper4bitOneLower4bitZero = 0xf0
Upper4bitZeroLower4bitOne = 0x0f
Upper8pinInputLower8pinOutput = 0xff00
Upper8pinOutputLower8pinInput = 0x00ff
Upper4pinInputLower4pinOutput = 0xf0
Upper4pinOutputLower4pinInput = 0x0f
DataByte0x55 = 0x55
DataByte0xaa = 0xaa
DataByte0x00 = 0x00
DataByte0xff = 0xff
RegisterAddressArrayMcp23s17 = [0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14,
0x01, 0x03, 0x05, 0x07, 0x09, 0x0b, 0x1d, 0x0f, 0x11, 0x13, 0x15]
Mcp23s17WriteCommand = 0b01000000
# *** Mcp23s17 Functions ******************************************************
def GetRegisterAddress(registerAddressArray, registerIndex, portType):
if (portType == PortA):
registerAddress = registerAddressArray[registerIndex]
if (portType == PortB):
registerAddress = registerAddressArray[registerIndex + 11]
return registerAddress
def WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, registerAddressArray, registerIndex, portType, dataByte):
mcp23s17WriteCommand = Mcp23s17WriteCommand | (spiChipSubAddress << 1)
mcp23s17RegisterAddress = GetRegisterAddress(registerAddressArray, registerIndex, portType)
mcp23s17WriteDataByte = dataByte
# PrintEightBitPattern("mcp23s17WriteCommand = ", mcp23s17WriteCommand)
# PrintEightBitPattern("mcp23s17RegisterAddress = ", mcp23s17RegisterAddress)
# PrintEightBitPattern("mcp23s17WriteDataByte = ", mcp23s17WriteDataByte)
spiWriteList = [mcp23s17WriteCommand, mcp23s17RegisterAddress, mcp23s17WriteDataByte]
ftspi.SpiWrite(spiChannel, spiWriteList)
def SetupMcp23s17Ports(spiChannel, spiChipSubAddress, portAsetupByte, portBsetupByte):
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, InputOutputDirectionIndex, PortA, portAsetupByte)
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, InputOutputDirectionIndex, PortB, portBsetupByte)
def WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiChipSubAddress, dataByte):
mcp23s17WriteCommand = Mcp23s17WriteCommand | (spiChipSubAddress << 1)
mcp23s17RegisterAddress = GetRegisterAddress(RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA)
spiWriteList = [mcp23s17WriteCommand, mcp23s17RegisterAddress, dataByte]
ftspi.SpiWrite(spiChannel, spiWriteList)
def WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiChipSubAddress, dataByte):
mcp23s17WriteCommand = Mcp23s17WriteCommand | (spiChipSubAddress << 1)
mcp23s17RegisterAddress = GetRegisterAddress(RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB)
spiWriteList = [mcp23s17WriteCommand, mcp23s17RegisterAddress, dataByte]
ftspi.SpiWrite(spiChannel, spiWriteList)
# *****************************************************************************
# Function - TestMcp23s1701BlinkLed
# Description -
# Open SPI channel for MCP23S17 at sub address 0b000
# Assign MCP23s17 Port A and Port B all 8 pins as output
# Write 0x55 to Port A and 0xaa to Port B
# *****************************************************************************
# *** Sample call to test MCP23S17 with SPI channel 0, CE0, chip address 0
# TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber1bit = 0, spiChipSubAddress = 0)
def TestMcp23s17BlinkLed(spiChannelNumber, spiChipEnableNumber, spiChipSubAddress):
spiChannel = spidev.SpiDev()
spiChannel.open(spiChannelNumber, spiChipEnableNumber)
SetupMcp23s17Ports(spiChannel, spiChipSubAddress, All8pinOutput, All8pinOutput)
# *** Write Port A and Port B with constants ***
DisableHC137ControlByte = 0x20 # b0010 0000 = CS1 Low, CS2 High, All Yn High
EnableHC137ControlByte = 0x10 # b0001 0000 = CS1 High, CS2 Low, 1 of 8 Yn low
# WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiChipSubAddress, EnableHC137ControlByte)
# WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiChipSubAddress, EnableHC137ControlByte)
# WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiChipSubAddress, DisableHC137ControlByte)
# WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiChipSubAddress, DisableHC137ControlByte)
# while True:
# pass
print "Now blinking LEDs, ... "
# *** Blink Port A and Port B LEDs ***
for i in range (10):
# *** Write 0x55 to Port A and 0xaa to Port B ***
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0x55)
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB, DataByte0xaa)
time.sleep(0.5)
# *** Write 0xaa to Port A and 0x55 to Port B ***
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0xaa)
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB, DataByte0x55)
time.sleep(0.5)
for i in range (10):
# *** Write 0x00 to Port A and 0x00 to Port B ***
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0x00)
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB, DataByte0x00)
time.sleep(0.5)
# *** Write 0xff to Port A and 0xff to Port B ***
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0xff)
WriteDataByteMcp23s17(spiChannel, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB, DataByte0xff)
time.sleep(0.5)
# .END
# ftdemux.py v1.2 tlfong01 2013may22
import time
import spidev
import ftprint
import ftiox
import ftspi
# *****************************************************************************
# Function -
# Description -
# Sample call -
# TestSelectSpiSlaveDevice(spiChannelNumber = 0, spiChipEnableNumber = 0,
# mcp23s17SubAddress = 0, spiSlaveDeviceNumber = 2)
# Notes -
# EEPROM 25LC256 # 1 - spiSlaveDeviceNumber = 0
# EEPROM 25LC256 # 2 - spiSlaveDeviceNumber = 1
# Guzunty Pi 4 digit 7 segment LED module - spiSlaveDeviceNumber = 2
# Sample output -
# ...
# *****************************************************************************
def SelectSpiSlaveDevice(spiChannel, spiIoxSubAddress, spiSlaveSubAddress):
# *** Disable/Enable 2 HC137s controlled by MCP23S17 Port A and Port B ***
DisableHC137ControlByte = 0x20 # b0010 0000 = CS1 Low, CS2 High, All Yn High
EnableHC137ControlByte = 0x10 # b0001 0000 = CS1 High, CS2 Low, 1 of 8 Yn low, others High
#ftiox.WriteDataByteMcp23s17(spiChannel, spiIoxSubAddress, ftiox.RegisterAddressArrayMcp23s17, ftiox.OutputLatchIndex, ftiox.PortA, DisableHC137ControlByte)
ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, DisableHC137ControlByte)
ftiox.WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiIoxSubAddress, DisableHC137ControlByte)
# time.sleep(2)
ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, EnableHC137ControlByte)
ftiox.WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiIoxSubAddress, EnableHC137ControlByte)
# time.sleep(2)
# *** Select HC137 ***
print "Now select chip, then hold ..."
LoadHC137 = 0b000000000 # bit 3 = 0 loads address
LatchHC137 = 0b000001000 # bit 3 = 1 latchs address
loadAddress = (EnableHC137ControlByte | spiSlaveSubAddress) | LoadHC137
latchAddress = (EnableHC137ControlByte | spiSlaveSubAddress) | LatchHC137
ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, loadAddress)
ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, latchAddress)
ftiox.WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiIoxSubAddress, loadAddress)
ftiox.WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiIoxSubAddress, latchAddress)
while True:
pass
def TestSelectSpiSlaveDevice(spiChannelNumber, spiChipEnableNumber, spiIoxSubAddress, spiSlaveDeviceNumber):
spiChannel = spidev.SpiDev()
spiChannel.open(spiChannelNumber, spiChipEnableNumber)
SelectSpiSlaveDevice(spiChannel, spiIoxSubAddress, spiSlaveDeviceNumber)
# .END
.END
No comments:
Post a Comment