Friday 5 July 2013

Testing MCP23S17, HC137, and 25LC256 in sequence OK

Now I have jumper select CE0 for MCP23S17 Demux and CE1 for EEPROMs and ran all three tests in sequence:

1. Blink LEDs driven by MCP23S17 at CE0

2. Test HC137 demux driven by MCP23S17 at CE0

3. Test EEPROM at CE1

So far so good.

.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 SPI 0,1 ***
# ftspi.TestSpiLoopBackV01(spiChannelNumber = 0, spiChipEnableNumber = 1, testDataByte = 0x55, testTime = 0.001, testCount = 60)

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

# *** Demux SPI 0,0 ***
ftdemux.TestDemux(spiChannelNumber = 0, spiChipEnableNumber = 0, spiIoxSubAddress = 0, spiSlaveDeviceNumber = 6)

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

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

# .END

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

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

Now blinking LEDs, ...
spiChannelNumber    =  0
spiChipEnableNumber =  0
MCP23S17 Address    =  0
HC137 Input Address =  6

*** Start Test 25LC256 ***

Write data byte =  0x55
Read data byte  =  0x55

*** Stop  Test 25LC256 ***


*** Stop Program ***

pi@raspberrypi ~/fongtoy $ date
Fri Jul  5 04:58:42 UTC 2013
pi@raspberrypi ~/fongtoy $



# *****************************************************************************
# 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 (4):
        # *** 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 (4):
        # *** 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



# *****************************************************************************
# Module - ftdemux.py
# Description - HC137 1 of 8 demultiplexer 
# *****************************************************************************

# ftdemux.py v2.0 tlfong01 2013jul03

# *****************************************************************************
# Imports
# *****************************************************************************

import time
import spidev

import ftprint
import ftiox
import ftspi

# *****************************************************************************
# Demux Test - last modified 2013jul04
# *****************************************************************************

# *****************************************************************************
# Function - TestDemuxV01
# Description - 
#   This function does the following.
#   1. Set up SPI channel for MCP23S17
#   2. Disable HC137 (all outputs High)
#   3. Enable HC137 (any 1 output Low)
#   4. Loop select and hold n second HC137 output Y0 to Y7
#   5. Select and hold n second HC137 selected output 
#   6. Disable HC137 (all outputs High)   
#   7. Close SPI channel

# Sample call - 
#   ftdemux.TestDemux(spiChannelNumber = 0, spiChipEnableNumber = 0, 
#                     spiIoxSubAddress = 0, spiSlaveDeviceNumber = 6)
# *****************************************************************************
  
def TestDemux(spiChannelNumber, spiChipEnableNumber, spiIoxSubAddress, spiSlaveDeviceNumber):

    spiChannel = spidev.SpiDev() 
    spiChannel.open(spiChannelNumber, spiChipEnableNumber)

    print "spiChannelNumber    = ", spiChannelNumber
    print "spiChipEnableNumber = ", spiChipEnableNumber
    print "MCP23S17 Address    = ", spiIoxSubAddress
    print "HC137 Input Address = ", spiSlaveDeviceNumber

    DisableDemux(spiChannel, spiIoxSubAddress)
    time.sleep(0.5)

    EnableDemux(spiChannel, spiIoxSubAddress)
    time.sleep(0.5)

    for i in range(0, 8, 1):
        SelectSpiSlaveDevice(spiChannel, spiIoxSubAddress, i)
time.sleep(0.5)

    SelectSpiSlaveDevice(spiChannel, spiIoxSubAddress, spiSlaveDeviceNumber)
    time.sleep(2)

    DisableDemux(spiChannel, spiIoxSubAddress)

    spiChannel.close()

# *****************************************************************************
# Demux Basic functions - last modified 2013jul04
# *****************************************************************************

def SelectSpiSlaveDevice(spiChannel, spiIoxSubAddress, spiSlaveSubAddress):

    EnableHC137ControlByte  = 0x10 # (bit 5 HC137 ~E1 = Low, bit 4 HC137 E2 = High)
                      
    LoadHC137ControlByte   = 0b000000000 # bit 3 = 0 load address
    LatchHC137ControlByte  = 0b000001000 # bit 3 = 1 latch address

    loadAddressControlByte  = (EnableHC137ControlByte | spiSlaveSubAddress) | LoadHC137ControlByte 
    latchAddressControlByte = (EnableHC137ControlByte | spiSlaveSubAddress) | LatchHC137ControlByte 

    ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, loadAddressControlByte)
    ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, latchAddressControlByte)

    ftiox.WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiIoxSubAddress, loadAddressControlByte)
    ftiox.WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiIoxSubAddress, latchAddressControlByte)

def DisableDemux(spiChannel, spiIoxSubAddress):

    DisableHC137ControlByte = 0x20

    ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, DisableHC137ControlByte)
    ftiox.WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiIoxSubAddress, DisableHC137ControlByte)

def EnableDemux(spiChannel, spiIoxSubAddress):

    EnableHC137ControlByte  = 0x10 # (bit 5 HC137 ~E1 = Low, bit 4 HC137 E2 = High)

    ftiox.WriteDataByteMcp23s17OutputLatchPortA(spiChannel, spiIoxSubAddress, EnableHC137ControlByte)
    ftiox.WriteDataByteMcp23s17OutputLatchPortB(spiChannel, spiIoxSubAddress, EnableHC137ControlByte)


# .END



# *****************************************************************************
# Module - fteeprom.py
# Description - SPI EEPROM
# *****************************************************************************

# fteeprom.py v2.0 tlfong01 2013jul03

# *****************************************************************************
# Imports
# *****************************************************************************

import time
import spidev

import ftspi
import ftprint

# *****************************************************************************
# Constants and variables
# *****************************************************************************

# *****************************************************************************
# Function - TestWriteReadEepormDataByte()
# Version - v1.2 tlfong01 2013may29
# Description - Write 1 data byte then read back
# Sample call - fteeprom.TestWriteReadEepromDataByte(spiChannelNumber = 0, 
#               spiChipEnableNumber = 1, startAddress = 0x4100, 
#               testDataByte = 0x55)
# Sample output - 
# *** Start Test 25LC256 ***
# Write data byte =  0x55
# Read data byte  =  0x55
# *** Stop  Test 25LC256 ***
# ***************************************************************************** 

def TestEeporm25Lc256v01(spiChannelNumber, spiChipEnableNumber, startAddress, testDataByte): 

    ftprint.PrintDoubleSpaceLine("*** Start Test 25LC256 ***")   

    spiChannel = spidev.SpiDev()  
    spiChannel.open(spiChannelNumber, spiChipEnableNumber)   

    WriteEepromDataByte(spiChannel, startAddress, testDataByte)
    print "Write data byte = ", hex(testDataByte)

    readBackDataByte = ReadEepromDataByte(spiChannel, startAddress)
    print "Read data byte  = ", hex(readBackDataByte)

    ftprint.PrintDoubleSpaceLine("*** Stop  Test 25LC256 ***") 


# *****************************************************************************
# Basic functions
# *****************************************************************************

def WriteEepromDataByte(spiChannel, startAddress, dataByte): # v1.1 tlfong01 2013may21

    EePromCommandWriteLatchEnable    = 0x06 
    EepromCommandWrite               = 0x02 
    FiveMilliSeconds = 0.005

    startAddressUpper = startAddress >> 8
    startAddressLower = startAddress & 0x00ff

    ftspi.SpiWrite(spiChannel, [EePromCommandWriteLatchEnable])
    time.sleep(FiveMilliSeconds)

    ftspi.SpiWrite(spiChannel, [EepromCommandWrite, startAddressUpper, startAddressLower, dataByte])
    time.sleep(FiveMilliSeconds)

def ReadEepromDataByte(spiChannel, startAddress): # v1.1 tlfong01 2013may21

    EepromCommandRead = 0x03 
    FiveMilliSeconds = 0.005
    DummyDataByte = 0x00
    readSpiDataList  = [0x00, 0x00, 0x00, 0x00, 0x00]

    startAddressUpper = startAddress >> 8
    startAddressLower = startAddress & 0x00ff                                            

    readSpiDataList = ftspi.SpiWrite(spiChannel, [EepromCommandRead, startAddressUpper, startAddressLower, DummyDataByte]) 
    dataByte = readSpiDataList[3]
    return dataByte

def WriteReadEepromDateByte(spiChannel, startAddress, testDataByte): # v1.1 tlfong01 2013may21
    testDataByte = 0x34
    testStartAddress = 0x0411

    WriteEepromDataByte(spiChannel, startAddress, dataByte)
    print "Write data byte = ", hex(testDataByte)

    readBackDataByte = ReadEepromDataByte(spiChannel, startAddress)
    print "Read data byte  = ", hex(readBackDataByte) 


# *****************************************************************************
# Old functions
# *****************************************************************************

def TestMcp23S17SpiSlaveSelector01(mcp23S17Address, eepromSubAddress, testStartAddress, testDataByte):

    # *** Set up SPI channel 00 for MCP23S17, channel 01 for EEPROM 25LC256 ***

    SpiChannel0 = 0 
    SpiChipEnable0 = 0
    SpiChipEnable1 = 1

    Mcp23s17SubAddress0 = 0

    spiChannel00 = spidev.SpiDev()  
    spiChannel00.open(SpiChannel0, SpiChipEnable0)   

    spiChannel01 = spidev.SpiDev()  
    spiChannel01.open(SpiChannel0, SpiChipEnable1) 

    # *** Set up MCP23s17 Port A all out to drive HC137 #1, Port B to drive 2 74HC137 #2 ***

    All8pinOutput = 0x00
    SetupMcp23s17Ports(spiChannel00, Mcp23s17SubAddress0, All8pinOutput, All8pinOutput)

    # *** Select EEPROM 25LC256 ***

    SelectSpiSlave(spiChannel00, Mcp23s17SubAddress0, eepromSubAddress)

    # *** Write Eeprom ***

    WriteEepromDataByte(spiChannel01, startAddress = testStartAddress, dataByte = testDataByte)

    print "Write data byte = ", hex(testDataByte)

    # *** Read Eeprom ***

    readBackDataByte = ReadEepromDataByte(spiChannel01, startAddress = testStartAddress)

    print "Read data byte  = ", hex(readBackDataByte)

    # *** Close SPI channel ***

    spiChannel00.close() 
    spiChannel01.close() 

def TestMcp23s17MultipleSlaveDevices(mcp23s17SubAddress, eepromSubAddress1, eepromSubAddress2):

    # *** Set up SPI channel 00 for MCP23S17, channel 01 for EEPROM 25LC256 ***

    SpiChannel0 = 0 
    SpiChipEnable0 = 0
    SpiChipEnable1 = 1

    Mcp23s17SubAddress0 = 0

    spiChannel00 = spidev.SpiDev()  
    spiChannel00.open(SpiChannel0, SpiChipEnable0)   

    spiChannel01 = spidev.SpiDev()  
    spiChannel01.open(SpiChannel0, SpiChipEnable1) 

    # *** Set up MCP23s17 Port A all out to drive HC137 #1, Port B to drive 2 74HC137 #2 ***

    All8pinOutput = 0x00
    SetupMcp23s17Ports(spiChannel00, mcp23s17SubAddress, All8pinOutput, All8pinOutput)

    SelectHC137PortA = 0x20 # Bits 4,5 = HC137 pins 5,6 = 0,1 selects chip 
    WriteDataByteMcp23s17(spiChannel00, mcp23s17SubAddress, RegisterAddressArrayMcp23s17, OutputLatchIndex, PortA, SelectHC137PortA)
                          
    PrintEightBitPattern(" SelectHC137PortA = ", SelectHC137PortA)

    print "Now select chip, then hold ..."
    while True:
        pass
    
    # *** Test EEPROM #1 ***

    # *** Select EEPROM 25LC256 #1 ***

    SelectSpiSlave(spiChannel00, mcp23s17SubAddress, eepromSubAddress1)

    # *** Write Eeprom ***

    testDataByte = 0x51
    testStartAddress = 0x0411
    WriteEepromDataByte(spiChannel01, startAddress = testStartAddress, dataByte = testDataByte)
    print "Write data byte = ", hex(testDataByte)

    # *** Read Eeprom ***

    readBackDataByte = ReadEepromDataByte(spiChannel01, startAddress = testStartAddress)
    print "Read data byte  = ", hex(readBackDataByte)

    # *** Test EEPROM #2 ***

    # *** Select EEPROM 25LC256 #2 ***

    SelectSpiSlave(spiChannel00, mcp23s17SubAddress, eepromSubAddress2)

    # *** Write Eeprom ***

    testDataByte = 0x34
    testStartAddress = 0x0411
    WriteEepromDataByte(spiChannel01, startAddress = testStartAddress, dataByte = testDataByte)
    print "Write data byte = ", hex(testDataByte)

    # *** Read Eeprom ***

    readBackDataByte = ReadEepromDataByte(spiChannel01, startAddress = testStartAddress)
    print "Read data byte  = ", hex(readBackDataByte)    
  
    # *** Close SPI channel ***

    spiChannel00.close()     
    spiChannel01.close() 

def Test25Lc256():

    PrintDoubleSpaceLine("*** Start testing 25LC256 ***")   

    # *** Set up SPI channel ***  
    # import spidev # WiringPi Python wrapper
    # spidev.max_speed_hz = 10000 # seems no effect, clock always 2 MHz !!!
    spiEeprom = spidev.SpiDev() 
    # spiEeprom.open(0, 0)  

    spiEeprom.open(0, 1)  

    # *** 25LC256 instructions ***
    EepromCommandWriteStatusRegister = 0x01
    EepromcCommandWrite              = 0x02 
    EePromCommandRead                = 0x03 
    EepromCommandWriteLatchDisable   = 0x04
    EepromCommandReadStatusRegister  = 0x05
    EePromCommandWriteLatchEnable    = 0x06 

    # *** 25LC256 addresses ***
    #EepromStartAddress0= 0x0000
    #EepromStartAddressUpper0 = 0x00
    #EepromStartAddressLower0 = 0x00

    EepromStartAddress1= 0x0300
    EepromStartAddressUpper1 = 0x03
    EepromStartAddressLower1 = 0x00

    # *** SPI variables and constants ***

    writeSpiDataList = [0x00]
    readSpiDataList  = [0x00, 0x00, 0x00, 0x00, 0x00]

    DummyDataByte = 0x00
    TestDataByte55  = 0x55
    TestDataByteAa  = 0xaa

    WriteProtectNone        = 0x00 # -
    WriteProtectUpperFourth = 0x04 # 0x6000 to 0x7fff
    WriteProtectUpperHalf   = 0x08 # 0x4000 to 0x7fff
    WriteProtectAll         = 0x0c # 0x0000 to 0x7fff

    FiveMilliSeconds = 0.005
  
    # *** Enable Write Enable Latch ***
    # while True:

    # ********************************************************
    # spiEeprom.xfer2([EePromCommandWriteLatchEnable])     

    #SpiWrite(spiEeprom.xfer2, [EePromCommandWriteLatchEnable])
    SpiWrite(spiEeprom, [EePromCommandWriteLatchEnable])

    time.sleep(FiveMilliSeconds)

    # *** Read Status Register ***
    readSpiDataList = spiEeprom.xfer2([EepromCommandReadStatusRegister, DummyDataByte]) 
    PrintDoubleSpaceLine("*** Checking WEL bit after Write Enable ***")  
    PrintEightBitPattern("Read back byte 0 = ", readSpiDataList[0])
    PrintEightBitPattern("Read back byte 1 = ", readSpiDataList[1])
    time.sleep(FiveMilliSeconds)

    # *** Disable Write Enable Latch ***
    spiEeprom.xfer2([EepromCommandWriteLatchDisable]) 
    time.sleep(FiveMilliSeconds)

    # *** Check WEL (Write Enable Latch) bit (Bit 1) ***
    readInByteList = spiEeprom.xfer2([EepromCommandReadStatusRegister, DummyDataByte]) 
    PrintDoubleSpaceLine("*** Checking WEL bit after Write Disable ***")  
    PrintEightBitPattern("Read back byte 0 = ", readInByteList[0])
    PrintEightBitPattern("Read back byte 1 = ", readInByteList[1])
    time.sleep(0.005)

    # *** Write Enable ***
    spiEeprom.xfer2([EePromCommandWriteLatchEnable]) 
    time.sleep(0.005)

    # *** Check WEL (Write Enable Latch) bit (Bit 1) ***
    readInByteList = spiEeprom.xfer2([EepromCommandReadStatusRegister, DummyDataByte]) 
    PrintDoubleSpaceLine("*** Checking WEL bit after Write Enable ***")  
    PrintEightBitPattern("Read back byte 0 = ", readInByteList[0])
    PrintEightBitPattern("Read back byte 1 = ", readInByteList[1])
    time.sleep(0.005)

    # *** Write protect upper fourth ****
    spiEeprom.xfer2([EepromCommandWriteStatusRegister, WriteProtectUpperFourth])
    time.sleep(0.005)

    # *** Check Status Register ***
    readInByteList = spiEeprom.xfer2([EepromCommandReadStatusRegister, DummyDataByte]) 
    PrintDoubleSpaceLine("*** Checking Write Protect Bits ***")  
    PrintEightBitPattern("Read back byte 0 = ", readInByteList[0])
    PrintEightBitPattern("Read back byte 1 = ", readInByteList[1])
    time.sleep(0.005)

    # *** Write Enable ***
    spiEeprom.xfer2([EePromCommandWriteLatchEnable]) 
    time.sleep(0.005)

    # *** Write protect none ****
    spiEeprom.xfer2([EepromCommandWriteStatusRegister, WriteProtectNone])
    time.sleep(0.005)

    # *** Check Status Register ***
    readInByteList = spiEeprom.xfer2([EepromCommandReadStatusRegister, DummyDataByte]) 
    PrintDoubleSpaceLine("*** Checking Write Protect Bits ***")  
    PrintEightBitPattern("Read back byte 0 = ", readInByteList[0])
    PrintEightBitPattern("Read back byte 1 = ", readInByteList[1])
    time.sleep(0.005)

    # *** Write Enable ***
    spiEeprom.xfer2([EePromCommandWriteLatchEnable]) 
    time.sleep(0.005)

    # *** Write 2 data bytes at address 0x0300 ****

    # while True:
    spiEeprom.xfer2([EepromcCommandWrite, EepromStartAddressUpper1, EepromStartAddressLower1, TestDataByte55, TestDataByte55]) 

    # *** Check WIP (Write In Progress) bit (Bit 0) ***
    readInByteList = spiEeprom.xfer2([EepromCommandReadStatusRegister, DummyDataByte]) 
    PrintDoubleSpaceLine("*** Checking WIP bit immediately after writing 1 byte without waiting 5 mS ***")  
    PrintEightBitPattern("Read back byte 0 = ", readInByteList[0])
    PrintEightBitPattern("Read back byte 1 = ", readInByteList[1]) 
    time.sleep(0.005)

    # *** Check WIP (Write In Progress) bit (Bit 0) ***
    readInByteList = spiEeprom.xfer2([EepromCommandReadStatusRegister, DummyDataByte]) 
    PrintDoubleSpaceLine("*** Checking WIP bit again, after waiting 5 mS ***")  
    PrintEightBitPattern("Read back byte 0 = ", readInByteList[0])
    PrintEightBitPattern("Read back byte 1 = ", readInByteList[1])

    # *** Enable Write Enable Latch ***
    spiEeprom.xfer2([EePromCommandWriteLatchEnable]) 
    time.sleep(FiveMilliSeconds)

    # *** Read back data at address 0x0000 ***
    
    time.sleep(1)

    #while True:
    readSpiDataList = spiEeprom.xfer2([EePromCommandRead, EepromStartAddressUpper1, EepromStartAddressLower1, DummyDataByte, DummyDataByte])   
    
    PrintDoubleSpaceLine("*** Read back data byte written ***")  
    PrintEightBitPattern("Read back byte 0 = ", readSpiDataList[0])
    PrintEightBitPattern("Read back byte 1 = ", readSpiDataList[1])
    PrintEightBitPattern("Read back byte 2 = ", readSpiDataList[2])
    PrintEightBitPattern("Read back byte 3 = ", readSpiDataList[3]) 
    PrintEightBitPattern("Read back byte 4 = ", readSpiDataList[4]) 

    # *** Write Enable ***
    spiEeprom.xfer2([EePromCommandWriteLatchEnable]) 
    time.sleep(0.005)

    # *** Write 2 data bytes at address 0x0000 ****
    spiEeprom.xfer2([EepromcCommandWrite, EepromStartAddressUpper1, EepromStartAddressLower1, TestDataByteAa, TestDataByteAa]) 

    # *** Check WIP (Write In Progress) bit (Bit 0) ***
    readInByteList = spiEeprom.xfer2([EepromCommandReadStatusRegister, DummyDataByte]) 
    PrintDoubleSpaceLine("*** Checking WIP bit immediately after writing 1 byte without waiting 5 mS ***")  
    PrintEightBitPattern("Read back byte 0 = ", readInByteList[0])
    PrintEightBitPattern("Read back byte 1 = ", readInByteList[1]) 
    time.sleep(0.005)

    # *** Check WIP (Write In Progress) bit (Bit 0) ***
    readInByteList = spiEeprom.xfer2([EepromCommandReadStatusRegister, DummyDataByte]) 
    PrintDoubleSpaceLine("*** Checking WIP bit again, after waiting 5 mS ***")  
    PrintEightBitPattern("Read back byte 0 = ", readInByteList[0])
    PrintEightBitPattern("Read back byte 1 = ", readInByteList[1])

    # *** Enable Write Enable Latch ***
    spiEeprom.xfer2([EePromCommandWriteLatchEnable]) 
    time.sleep(FiveMilliSeconds)

    # *** Read back data at address 0x0000 ***
    
    readSpiDataList = spiEeprom.xfer2([EePromCommandRead, EepromStartAddressUpper1, EepromStartAddressLower1, DummyDataByte, DummyDataByte])   
    
    PrintDoubleSpaceLine("*** Read back data byte written ***")  
    PrintEightBitPattern("Read back byte 0 = ", readSpiDataList[0])
    PrintEightBitPattern("Read back byte 1 = ", readSpiDataList[1])
    PrintEightBitPattern("Read back byte 2 = ", readSpiDataList[2])
    PrintEightBitPattern("Read back byte 3 = ", readSpiDataList[3]) 
    PrintEightBitPattern("Read back byte 4 = ", readSpiDataList[4])    
    
    # *** Close SPI channel ***
    spiEeprom.close() 

    #while True:
    #    pass

    PrintDoubleSpaceLine("*** Stop testing write/read 25LC256 ***") 


def Write25Lc256(spiChannel, channelNumber, chipEnableNumber, dataByte):

    spiChannel.open(channelNumber, chipEnableNumber)  

    PrintDoubleSpaceLine("*** Start writing 25LC256 ***")   

    # *** 25LC256 instructions ***
    EepromCommandWriteStatusRegister = 0x01
    EepromcCommandWrite              = 0x02 
    EePromCommandRead                = 0x03 
    EepromCommandWriteLatchDisable   = 0x04
    EepromCommandReadStatusRegister  = 0x05
    EePromCommandWriteLatchEnable    = 0x06 

    # *** 25LC256 addresses ***

    EepromStartAddress1= 0x0300
    EepromStartAddressUpper1 = 0x03
    EepromStartAddressLower1 = 0x00

    # *** SPI variables and constants ***

    writeSpiDataList = [dataByte]
    readSpiDataList  = [0x00, 0x00, 0x00, 0x00, 0x00]

    DummyDataByte = 0x00
    TestDataByte55  = 0x55
    TestDataByteAa  = 0xaa

    WriteProtectNone        = 0x00 # -
    WriteProtectUpperFourth = 0x04 # 0x6000 to 0x7fff
    WriteProtectUpperHalf   = 0x08 # 0x4000 to 0x7fff
    WriteProtectAll         = 0x0c # 0x0000 to 0x7fff

    FiveMilliSeconds = 0.005

    SpiWrite(spiChannel, [EePromCommandWriteLatchEnable])

    time.sleep(FiveMilliSeconds)

    # *** Write 2 data bytes at address 0x0000 ****
    spiChannel.xfer2([EepromcCommandWrite, EepromStartAddressUpper1, EepromStartAddressLower1, dataByte, dataByte])

    # *** Read Status Register ***
    readSpiDataList = spiChannel.xfer2([EepromCommandReadStatusRegister, DummyDataByte]) 
    PrintDoubleSpaceLine("*** Checking WEL bit after Write Enable ***")  
    PrintEightBitPattern("Read back byte 0 = ", readSpiDataList[0])
    PrintEightBitPattern("Read back byte 1 = ", readSpiDataList[1])
    time.sleep(FiveMilliSeconds)

    # *** Disable Write Enable Latch ***
    spiChannel.xfer2([EepromCommandWriteLatchDisable]) 
    time.sleep(FiveMilliSeconds)

    # *** Check WEL (Write Enable Latch) bit (Bit 1) ***
    readInByteList = spiChannel.xfer2([EepromCommandReadStatusRegister, DummyDataByte]) 
    PrintDoubleSpaceLine("*** Checking WEL bit after Write Disable ***")  
    PrintEightBitPattern("Read back byte 0 = ", readInByteList[0])
    PrintEightBitPattern("Read back byte 1 = ", readInByteList[1])
    time.sleep(0.005)

def Read25Lc256DataByte(spiChannel, channelNumber, chipEnableNumber, eepromStartAddress):

    spiChannel.open(channelNumber, chipEnableNumber)  

    PrintDoubleSpaceLine("*** Start reading 25LC256 ***")   

    # *** 25LC256 instructions ***
    EePromCommandRead                = 0x03 

    eepromStartAddressUpper = eepromStartAddress >> 8
    eepromStartAddressLower = eepromStartAddress & 0x00ff

    # *** SPI variables and constants ***

    readSpiDataList  = [0x00, 0x00, 0x00, 0x00, 0x00]
    DummyDataByte = 0x00

    # *** Read back data at address 0x0000 ***
    
    readSpiDataList = spiChannel.xfer2([EePromCommandRead, eepromStartAddressUpper, eepromStartAddressLower, DummyDataByte])   
                                                       
    PrintDoubleSpaceLine("*** Read back data byte written ***")  
    PrintEightBitPattern("Read back byte 0 = ", readSpiDataList[0])
    PrintEightBitPattern("Read back byte 1 = ", readSpiDataList[1])
    PrintEightBitPattern("Read back byte 2 = ", readSpiDataList[2])
    PrintEightBitPattern("Read back byte 3 = ", readSpiDataList[3]) 

    dataByte = readSpiDataList[3]
    return dataByte


# .END

.END







No comments:

Post a Comment