Thursday 4 July 2013

MCP23S17 testing OK



























So I started testing MCP23S17.  The GAx, GBx LEDs blink as expected.


ProgramTitle = "FongToy v2.1 tlfong01 2013jul04"
ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 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, ... "

.END




# *****************************************************************************
# Module - fongtoy.py
# Description - main test program 
# *****************************************************************************

# fongtoy v2.x 2013

ProgramTitle = "FongToy v2.1 tlfong01 2013jul04"

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
import ftwebcam

# *** Troubleshooting functions ***

# *** GPIO tests v1.3 tlfong01 2013may23 ***
# ftgpio.TestLed()
# ftgpio.TestBuzzer()
# ftgpio.TestButtonEchoBuzzer()
# ftgpio.TestButtonEchoLed()

# *** SPI Tests v1.3 tlfong01 2013may23 ***

# 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)

# 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)

# *** Old tests ***

# *** SPI loop back ***
# ftspi.TestSpiLoopBackV01(spiChannelNumber = 0, spiChipEnableNumber = 1, testDataByte = 0x55, testTime = 0.001, testCount = 60000)

# *** ADC ***
# ftadc.TestMcp3208v03() 
# ftadc.TestMcp3201v04()

# *** EEPROM ***
# fteeprom.TestEeporm25Lc256v01(spiChannelNumber = 0, spiChipEnableNumber = 1, startAddress = 0x4100, testDataByte = 0x55)

# *** Demux ***
# ftdemux.TestDemuxV01(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 5)

# *** Guzunty Pi Step Motor ***
# ... test functions already moved to ftguzunti.py tlfong01 2013jun10 ...

# *** Webcam ***

# ftwebcam.TestWebcam()
# ftwebcam.TestWebcam01()
# ftwebcam.TestWebcam03(imageHoldSeconds = 10)
# ftwebcam.TestWebcamCamera04()
# ftwebcam.TestWebcamCamera05()
# ftwebcam.TestWebcamCamera06()

# ftwebcam.TestWebcamCamera07()

# ftwebcam.TestWebcamCamera08(cameraNumber = 0, imageSize = (640, 480), imageCount = 10, timerSecond = 1)
# ftwebcam.TestWebcamCamera08(cameraNumber = 1, imageSize = (640, 480), imageCount = 10, timerSecond = 1)
# ftwebcam.TestWebcamCamera08(cameraNumber = 0, imageSize = (1280, 720), imageCount = 10, timerSecond = 1)
# ftwebcam.TestWebcamCamera08(cameraNumber = 1, imageSize = (1280, 720), imageCount = 10, timerSecond = 1)

# ftwebcam.TestWebcamCamera09(cameraNumber = 0, imageSize = (1280, 720), imageCount = 120, timerSecond = 0.5, fileName = "file2013jun24hkt1044.bmp")

# *** Main program ***

# *** Start program message ***
ftprint.StartProgram(ProgramTitle)

# *** SPI loop back ***
# ftspi.TestSpiLoopBackV01(spiChannelNumber = 0, spiChipEnableNumber = 1, testDataByte = 0x55, testTime = 0.001, testCount = 60)

# *** EEPROM ***
# fteeprom.TestEeporm25Lc256v01(spiChannelNumber = 0, spiChipEnableNumber = 0, startAddress = 0x4100, testDataByte = 0x55)

# *** Mcp23s17 ***
ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber = 0, spiChipSubAddress = 0)

# *** Stop program message ***
ftprint.StopProgram()

# .END




pi@raspberrypi ~/fongtoy $ sudo python fongtoy.py

*** Start Program - FongToy v2.1 tlfong01 2013jul04 ***

Now blinking LEDs, ...

*** Stop Program ***

pi@raspberrypi ~/fongtoy $ date
Thu Jul  4 02:18:51 UTC 2013



# *****************************************************************************
# Module - ftiox.py
# Description - IO expander functions using I2C MCP230x, SPI MCP23sxx
# *****************************************************************************

# ftiox.py v2.1 tlfong01 2013jul04

# *****************************************************************************
# 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, spiChipEnableNumber = 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