# fongtoy v1.26 tlfong01 2013may28
ProgramTitle = "FongToy v1.26 tlfong01 2013may28"
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
import ftgpio
import ftprint
import ftspi
import ftiox
import fteeprom
import ftguzuntypi
import ftdemux
import fttest
import ftadc
# *** Main program ***
# *** Start program message ***
ftprint.StartProgram(ProgramTitle)
# *** Troubleshooting functions ***
# *** GPIO tests v1.3 tlfong01 2013may23 ***
# ftgpio.TestLed()
# ftgpio.TestBuzzer()
# ftgpio.TestButtonEchoBuzzer()
# ftgpio.TestButtonEchoLed()
# *** SPI Tests v1.3 tlfong01 2013may23 ***
# 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)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 2, testStartAddress = 0x0123, testWriteDataByte = 0x5a)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 1, testStartAddress = 0x0123, testWriteDataByte = 0x3b)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 0, testStartAddress = 0x0123, testWriteDataByte = 0x3b)
# *** Current test functions ***
# ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 0, spiChipSubAddress = 0)
# ftdemux.TestSelectSpiSlaveDevice(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 5)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 0, testStartAddress = 0x0123, testWriteDataByte = 0x3b)
# fttest.TestDemuxEeprom(mcp23s17SubAddress = 0, eepromDemuxAddress = 1, testStartAddress = 0x0411, testWriteDataByte = 0x4c)
# fttest.TestDemuxGuzuntyClock(mcp23s17SubAddress = 0, guzuntyClockDemuxAddress = 2, secondCount = 10)
# fttest.TestMcp320103(testTime = 0.1, testCount = 10)
# fttest.TestMcp320103(testTime = 0.01, testCount = 100)
# fttest.TestMcp320103(testTime = 0.05, testCount = 50)
# fttest.TestMcp320103(testTime = 0.1, testCount = 1)
# ftspi.TestSpiLoopBackV01(spiChannelNumber = 0, spiChipEnableNumber = 1, testDataByte = 0x55, testTime = 0.001, testCount = 60000)
# ftadc.TestMcp320104()
# ftadc.TestMcp3208v01()
# ftadc.TestMcp3208v02(inputMode = 1, channelNumber = 0)
# *** ADC tests ***
# ftadc.TestMcp3208v03()
# ftadc.TestMcp3201v04()
# *** EEPROM tests ***
# fteeprom.TestEeporm25Lc256v01(spiChannelNumber = 0, spiChipEnableNumber = 1, startAddress = 0x4100, testDataByte = 0x55)
# *** Mcp23s17 tests ***
ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber
= 0, spiChipSubAddress = 0)
# *** Stop program message ***
ftprint.StopProgram()
#.END
# ftiox.py v1.3 tlfong01 2013may29
# *****************************************************************************
# Module - ftiox.py
# Description - IO expander functions using I2C MCP230x, SPI MCP23sxx
# *****************************************************************************
# *****************************************************************************
# Imports
# *****************************************************************************
import time
import spidev
import ftspi
# *****************************************************************************
# Constants and variables
# *****************************************************************************
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 Test functions
# *****************************************************************************
# *****************************************************************************
# Function - TestMcp23s17BlinkLed
# Description - Blink the 16 LEDs conntected to MCP23s17 Port A and Port B
# Sample call -
# TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber1bit = 0, spiChipSubAddress = 0)
# *****************************************************************************
def TestMcp23s17BlinkLed(spiChannelNumber, spiChipEnableNumber, spiChipSubAddress):
spiChannel = spidev.SpiDev()
spiChannel.open(spiChannelNumber, spiChipEnableNumber)
SetupMcp23s17Ports(spiChannel, spiChipSubAddress, portAsetupByte = All8pinOutput, portBsetupByte = 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)
# *****************************************************************************
# Basic MCP23s17 functions
# *****************************************************************************
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)
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 GetRegisterAddress(registerAddressArray, registerIndex, portType):
if (portType == PortA):
registerAddress = registerAddressArray[registerIndex]
if (portType == PortB):
registerAddress = registerAddressArray[registerIndex + 11]
return registerAddress
# .END
.END
No comments:
Post a Comment