I have also built fongtoy module ftiox and tested MCP23S17BlinkLed() OK.
# ftmain v1.1 tlfong01 2013may21
# *** Program Description ***
ProgramTitle = "FongToy v1.0 tlfong01 2013may20 "
# *** System 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
# *** Main function ***
ftprint.StartProgram(ProgramTitle)
# *** Testing SPI loop back ***
# print "Testing SPI loop back, ..."
# ftspi.TestSpiLoopBack(spiChannelNumber = 0, spiChipEnableNumber = 1, testDataByte = 0x55, testCount = 1000, testTime = 0.001)
# ftspi.TestSpiLoopBack(spiChannelNumber = 0, spiChipEnableNumber = 0, testDataByte = 0x55, testCount = 600000, testTime = 0.001)
# *** Testing EEPROM
# fteeprom.TestWriteReadEepormDataByte(spiChannelNumber = 0, spiChipEnableNumber = 1, startAddress = 0x0410, testDataByte = 0x55)
# *** Testing MCP23S17 blink LED ***
print "Testing MCP23S17 blink LED, ..."
ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 0, spiChipSubAddress = 0)
ftprint.StopProgram()
#.END
# ftiox.py v1.1 tlfong01 2013may21
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)
# *****************************************************************************
# Function - TestMcp23s1701
# 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
# TestMcp23s17(spiChannelNumber = 0, spiChipEnableNumber1bit = 0, spiChipSubAddress = 0)
def TestMcp23s17BlinkLed(spiChannelNumber, spiChipEnableNumber, spiChipSubAddress):
spiChannelMcp23s17 = spidev.SpiDev()
spiChannelMcp23s17.open(spiChannelNumber, spiChipEnableNumber)
SetupMcp23s17Ports(spiChannelMcp23s17, spiChipSubAddress, All8pinOutput, All8pinOutput)
# *** Write 0x55 to Port A and 0xaa to Port B ***
WriteDataByteMcp23s17(spiChannelMcp23s17, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0x55)
WriteDataByteMcp23s17(spiChannelMcp23s17, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB, DataByte0x55)
# *** Blink Port A and Port B LEDs ***
for i in range (10):
# *** Write 0x55 to Port A and 0xaa to Port B ***
WriteDataByteMcp23s17(spiChannelMcp23s17, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0x55)
WriteDataByteMcp23s17(spiChannelMcp23s17, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB, DataByte0xaa)
time.sleep(0.5)
# *** Write 0xaa to Port A and 0x55 to Port B ***
WriteDataByteMcp23s17(spiChannelMcp23s17, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0xaa)
WriteDataByteMcp23s17(spiChannelMcp23s17, 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(spiChannelMcp23s17, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0x00)
WriteDataByteMcp23s17(spiChannelMcp23s17, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB, DataByte0x00)
time.sleep(0.5)
# *** Write 0xff to Port A and 0xff to Port B ***
WriteDataByteMcp23s17(spiChannelMcp23s17, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, DataByte0xff)
WriteDataByteMcp23s17(spiChannelMcp23s17, spiChipSubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortB, DataByte0xff)
time.sleep(0.5)
# .END
No comments:
Post a Comment