Wednesday, 3 July 2013

SPI connection tested OK by EEPROM

So I used the following function to test SPI loop back.

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

I found everything OK.  The function reads back what is written if MOSI shorts to MISO, otherwise reads back 0x00.

So SPI connection is OK.  Then I needed check why EEPROM does not work.  Then I found that I made a mistake when calling test EEPROM - I select CE1 instead of CE0!

Changing CE0 instead of CE1, then EEPROM happily reads back what is written!


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

.END

# ftspi.py v1.1 tlfong01 2013may21

import spidev
import time

# *****************************************************************************
# Function - SpiWrite()
# Description - Output data byte list and read data byte list
# *****************************************************************************

def SpiWrite(spiChannel, spiWriteList):
    spiReadList = spiChannel.xfer2(spiWriteList)
    return spiReadList

# *****************************************************************************
# Function - TestSpiLoopBack01()
# Description -
#   * Echos MOSI on MISO
#   * Write/Read testByte x 4 testCount number of times, display last time write and read
# Hardware setup - connect MOSI to MSIO
# Function call - ftspi.TestSpiLoopBack(spiChannenNumber = 0, spiChipEnableNumber = 1, testByte = 0x55, testCount = 10000, testTime = 0.001)
# Notes -
#   * Use scope to check clock
#   * Only echoes MOSI by MISO, does not bother clock and chip select signals
# *** Start Program - FongToy v1.0 tlfong01 2013may20  ***
# Sample output -
# Testing SPI loop back, ...
# Output data byte list =  0x55 0x55 0x55 0x55
# Input  data byte list =  0x55 0x55 0x55 0x55
# *****************************************************************************

def TestSpiLoopBackV01(spiChannelNumber, spiChipEnableNumber, testDataByte, testTime, testCount): 


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

    print "Testing SPI loop back, ..."

    outputDataByteList = [testDataByte]
    print "Output data byte list = ", hex(outputDataByteList[0])

    inputDataByteList = [0x00]

    for i in range(testCount):
        inputDataByteList = SpiWrite(spiChannel, outputDataByteList)
time.sleep(testTime)

    print "Input  data byte list = ", hex(inputDataByteList[0])

# *****************************************************************************
# Function - TestSpiLoopBack()
# Description -
#   * Echos MOSI on MISO
#   * Write/Read testByte x 4 testCount number of times, display last time write and read
# Hardware setup - connect MOSI to MSIO
# Function call - ftspi.TestSpiLoopBack(spiChannenNumber = 0, spiChipEnableNumber = 1, testByte = 0x55, testCount = 10000, testTime = 0.001)
# Notes -
#   * Use scope to check clock
#   * Only echoes MOSI by MISO, does not bother clock and chip select signals
# *** Start Program - FongToy v1.0 tlfong01 2013may20  ***
# Sample output -
# Testing SPI loop back, ...
# Output data byte list =  0x55 0x55 0x55 0x55
# Input  data byte list =  0x55 0x55 0x55 0x55
# *****************************************************************************

def TestSpiLoopBack(spiChannelNumber, spiChipEnableNumber, testDataByte, testCount, testTime): # v1.3 tlfong01 2013may23

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

    outputDataByteList = [testDataByte, testDataByte, testDataByte, testDataByte]
    print "Output data byte list = ", hex(outputDataByteList[0]), hex(outputDataByteList[1]), hex(outputDataByteList[2]), hex(outputDataByteList[3])

    inputDataByteList = [0x00, 0x00, 0x00, 0x00]
    for i in range(testCount):
        inputDataByteList = SpiWrite(testSpi, outputDataByteList)
time.sleep(testTime)
    print "Input  data byte list = ", hex(inputDataByteList[0]), hex(inputDataByteList[1]), hex(inputDataByteList[2]), hex(inputDataByteList[3])

# * WiringPi Python wrapping SPI test functions *******************************

def TestWiringPiSpi():
    print "\n" + "*** Start testing wiringPi SPI, ... ***" + "\n"

    spi = spidev.SpiDev() # create spidev object
    spi.open(0,0) # open SPI0, CE0_N

    SendByteList = [0x55, 0xaa, 0xAA]
    ReadByteList = spi.xfer2(SendByteList)
    print "Bytes read = " + hex(ReadByteList[0]) + " " + hex(ReadByteList[1]) + " " + hex(ReadByteList[2])

    print "\n" + "*** Stop testing wiringPi SPI, ... ***" + "\n"


# * SPI using bit banging (from Arduino/Netduino, not tested here *************

# SpiClockPin = RPiGpioGen11
# SpiMosiPin = RPiGpioGen10
# SpiMisoPin = RPiGpioGen9
# SpiSelect0Pin = RPiGpioGen8
# SpiSelect1Pin = RPiGpioGen7

def TestRfm12b(registerBaseAddress):
    SetupGPIOpins(OutputPinList, InputPinWithNoPullUpList, InputPinWithPullUpList )

def SpiSelectDevice(deviceNumber):
    if (deviceNumber == 0):
        writeOutputPin(SpiSelect0Pin, Low)
    else:
        writeOutputPin(SpiSelect1Pin, Low)

def SpiDisSelectDevice(deviceNumber):
    if (deviceNumber == 0):
        writeOutputPin(SpiSelect0Pin, High)
    else:
        writeOutputPin(SpiSelect1Pin, High)

def SpiClockPulse():
    writeOutputPin(SpiClockPin, High)
    time.sleep(1)
    writeOutputPin(SpiClockPin, Low)
    time.sleep(1)

def SpiWriteBit(dataBit):
    if (dataBit == 1):
        writeOutputPin(SpiMosiPin, High)      
    else:
        writeOutputPin(SpiMosiPin, Low)

def SpiReadBit(inputPin):
    dataBit  = readInputPin(inputPin)              
    # print dataBit
    return dataBit

# * Test Spi functions *

def TestSpiSelectDevice(deviceNumber, count):
    print "Now testing SPI device select pin", deviceNumber, ",..."
    for i in range (count):
        SpiSelectDevice(deviceNumber)
        time.sleep(1)
        SpiDisSelectDevice(deviceNumber)
time.sleep(1)

def TestSpiClockPulse(count):
    for i in range (count):
       SpiClockPulse()

def TestSpiWriteBit(count):
    for i in range (count):
        SpiWriteBit(1)
time.sleep(1)
SpiWriteBit(0)
time.sleep(1)

def TestSpiReadBit(inputPin, count):
    for i in range (count):
        dataBit = SpiReadBit(inputPin)  
if (dataBit == True):
   dataLevel = "High"
        else:
   dataLevel = "Low"
        print "dataBitRead at pin number ", inputPin, " = ", dataLevel
        time.sleep(1)

# .END

No comments:

Post a Comment