Now I have tested the module ftspi.py. Using the following spi loop back tests, I made sure the SPI signals as displayed by the scope are OK.
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)
# *****************************************************************************
# Function - SpiWrite()
# Description - Output data byte list and read data byte list
# *****************************************************************************
def SpiWrite(spiChannel, spiWriteList):
spiReadList = spiChannel.xfer2(spiWriteList)
return spiReadList
# *****************************************************************************
# Function - TestSpiLoopBack()
# Description - Echos MOSI on MISO
# Hardware setup - connect MOSI to MSIO
# Function call - ftspi.TestSpiLoopBack(spiChannenNumber = 0, spiChipEnableNumber = 1, testByte = 0x55, testCount = 10000, testTime = 0.001)
# *** 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
# Time measurement using scope -
# clock cycle = 2uS
# clock frequency = 1/(2uS) = 500KHz
# time to send one byte = 2uS * 8 = 16uS
# byte frequency = 1/(16uS) = 62.5KHz = 62,500 bytes/sec
# *****************************************************************************
def TestSpiLoopBack(spiChannelNumber, spiChipEnableNumber, testDataByte, testCount, testTime):
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])
.END
*** fongtoy v1.1 tlfong01 2023may21
# 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
# *** Main function ***
ftprint.StartProgram(ProgramTitle)
# ftiox.TestMcp23s17BlinkLed - NOT WORKING !!! 2013may21
# print "Testing MCP23S17, ..."
# ftiox.TestMcp23s17BlinkLed(spiChannelNumber = 0, spiChipEnableNumber1bit = 0, spiChipSubAddress3bit = 0)
# ftspi.TestSpiLoopBack()
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)
ftprint.StopProgram()
#.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 - TestSpiLoopBack()
# Description - Echos MOSI on MISO
# Hardware setup - connect MOSI to MSIO
# Function call - ftspi.TestSpiLoopBack(spiChannenNumber = 0, spiChipEnableNumber = 1, testByte = 0x55, testCount = 10000, testTime = 0.001)
# *** 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
# Time measurement using scope -
# clock cycle = 2uS
# clock frequency = 1/(2uS) = 500KHz
# time to send one byte = 2uS * 8 = 16uS
# byte frequency = 1/(16uS) = 62.5KHz = 62,500 bytes/sec
# *****************************************************************************
def TestSpiLoopBack(spiChannelNumber, spiChipEnableNumber, testDataByte, testCount, testTime):
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
# ftprint v1.0 tlfong01 2013may20
import time
import RPIO as GPIO
import ftio
# *** Printing and debugging functions ***
# * Set/Reset one bit of a byte *
def SetDataBit(dataByte, bitIndex):
setDataByte = 0x01 << bitIndex
dataByte = dataByte | setDataByte
return dataByte
def ResetDataBit(dataByte, bitIndex):
resetDataByte = ~(0x01 << bitIndex)
dataByte = dataByte & resetDataByte
return dataByte
# * Print nibble/byte as 4/8 bit pattern for debugging *
def PrintFourBitPattern(message, dataByte):
fourBitPattern = ConvertIntegerToFourBitPattern(dataByte)
print message, fourBitPattern
def PrintEightBitPattern(message, dataByte):
eightBitPattern = ConvertIntegerToEightPattern(dataByte)
print message, eightBitPattern
def ConvertIntegerToFourBitPattern(integer):
FourBitPattern = [""]
for k in range(4):
FourBitPattern = [i+j for i in ['0','1'] for j in FourBitPattern]
return FourBitPattern[integer]
def ConvertIntegerToEightPattern(integer):
EightBitPattern = [""]
for k in range(8):
EightBitPattern = [i+j for i in ['0','1'] for j in EightBitPattern]
return EightBitPattern[integer]
# * Beep/Start/Stop program functions *
def StartProgram(programTitle):
ftio.CleanUpGpio()
ftio.SetupGPIO()
StartBeep()
message = "\n" + "*** Start Program - " + programTitle + " ***" + "\n"
print message
def StopProgram():
ftio.SetupGPIO()
StopBeep()
# print "\n" + "*** Resetting GPIO input, no pull up/down, no event detect. ***" + "\n"
ftio.CleanUpGpio()
print "\n" + "*** Stop Program ***" + "\n"
def Beep(count):
for i in range(count):
ftio.pulsePin(ftio.BuzzerPin, ftio.OnTime, ftio.OffTime)
def StartBeep():
Beep(2)
time.sleep(1)
def StopBeep():
Beep(2)
def OneBeep():
Beep(1)
def FourBeeps():
Beep(4)
# .END
No comments:
Post a Comment