OpenCores
URL https://opencores.org/ocsvn/vspi/vspi/trunk

Subversion Repositories vspi

[/] [vspi/] [trunk/] [scripts/] [master/] [spitest.py] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 mjlyons
import sys
2
import spilib
3
import time
4
import datetime
5
import random
6
 
7
def GenRandomByteArray(byteCount):
8
  dataToSend = []
9
  for byteIndex in range(byteCount):
10
    dataToSend.append(random.randint(0, 255))
11
  return dataToSend
12
 
13
#
14
# SingleBytePacketTest: 
15
#
16
# Sends a predefined pattern of single-byte SPI transmissions (master-out)
17
#
18
def SingleBytePacketsSendTest():
19
  packetsToSend = [[0x55],[0xAA],[0x33],[0x66],[0xCC],[0x99],[0xF0],\
20
                   [0x0F],[0xFF],[0x00]]
21
  packetIndex = 0
22
  while True:
23
    dataToSend = packetsToSend[packetIndex]
24
 
25
    sys.stdout.write("Sending: [")
26
    for sendByte in dataToSend:
27
      sys.stdout.write(" 0x%02x" % (sendByte))
28
    sys.stdout.write(" ]\n")
29
    spi.SendToSlave(dataToSend)
30
 
31
    packetIndex = (packetIndex + 1) % len(packetsToSend)
32
    time.sleep(1)
33
 
34
    packetIndex + 1
35
 
36
#
37
# MultiBytePacketSendTest:
38
#
39
# Sends a predefined single multi-byte packet over SPI (master-out)
40
#
41
def MultiBytePacketSendTest():
42
  dataToSend = [0x01, 0x55, 0xAA, 0x33, 0x66, 0xCC, 0x99, 0xF0, 0xFF, 0x0F]
43
  sys.stdout.write("Sending: [")
44
  for sendByte in dataToSend:
45
    sys.stdout.write(" 0x%02x" % (sendByte))
46
  sys.stdout.write(" ]\n")
47
  spi.SendToSlave(dataToSend)
48
 
49
#
50
# MemLoopbackTest:
51
#
52
# Sends randomly generated blocks of data over SPI, then reads back 
53
# data. Assumes that slave is using a common read and write buffer
54
# and reads the block of data back. The test verifies that the block of 
55
# data is identical to the one sent. The test will run continuously
56
# until a received packet does not match the sent packet.
57
#
58
def MemLoopbackTest():
59
  passCount = 0
60
  loopbackErrors = 0
61
  packetByteSize = 4000
62
  while loopbackErrors == 0:
63
    print("PassCount: %d" % (passCount))
64
 
65
    dataToSend = GenRandomByteArray(packetByteSize)
66
 
67
    print("Sending [0x%x 0x%x 0x%x 0x%x 0x%x ...]" % (
68
        dataToSend[0], dataToSend[1], dataToSend[2],
69
        dataToSend[3], dataToSend[4]))
70
 
71
    # Send some data to the slave
72
    sys.stdout.write("Sending data to slave...");
73
    spi.WriteMemory(dataToSend)
74
    sys.stdout.write("done!\n")
75
 
76
    # Receive some data from the slave
77
    recvData = spi.ReadMemory(packetByteSize)
78
 
79
    # Make sure bytes sent match bytes received
80
    if len(recvData) != packetByteSize:
81
      sys.stdout.write("[FAIL] Did not receive 4096 bytes from spiifc\n")
82
      sys.exit(0)
83
    for byteIndex in range(packetByteSize):
84
      if recvData[byteIndex] != dataToSend[byteIndex]:
85
        sys.stdout.write("[FAIL] mismatch at index %d: sent=0x%x, recv=0x%x\n" \
86
            % (byteIndex, dataToSend[byteIndex], recvData[byteIndex]))
87
        loopbackErrors = loopbackErrors + 1
88
        if loopbackErrors >= 5:
89
          break
90
    if 0 == loopbackErrors:
91
      sys.stdout.write("[PASS] All loopback SPI bytes match.\n");
92
      passCount = passCount + 1
93
 
94
  # Print number of passed tests before failure
95
  print("PassCount: %d" % (passCount))
96
 
97
#
98
# RegLoopbackTest:
99
#
100
# Randomly writes and reads to vSPI's register and makes sure
101
# values are as expected
102
# 
103
def RegLoopbackTest():
104
  # First, zero out all registers
105
  print("Zeroing all registers...")
106
  for regId in range(16):
107
    spi.WriteReg(regId=regId, value=0)
108
 
109
  # Second, check that all registers are zeroed
110
  print("Checking all registers are zeroed...")
111
  for regId in range(16):
112
    regVal = spi.ReadReg(regId)
113
    if (0 != regVal):
114
      sys.stdout.write("ERROR: Reg %d was not zeroed (was 0x%08x)\n" % \
115
          (regId, regVal))
116
      return
117
 
118
  # Local mapping of expected register values
119
  expectedRegs = list(0 for i in range(16))
120
 
121
  # Random read and writes
122
  passCount = 0
123
  while (True):
124
    print("")
125
    regWriteId = random.randint(0, 15)
126
    regReadId = random.randint(0, 15)
127
    regWriteVal = random.randint(0, 0xFFFFFFFF)
128
 
129
    print("Writing Reg%d=0x%08x" % (regWriteId, regWriteVal))
130
    spi.WriteReg(regId=regWriteId, value=regWriteVal)
131
    expectedRegs[regWriteId] = regWriteVal
132
 
133
    print("Checking Reg%d==0x%08x" % (regReadId, expectedRegs[regReadId]))
134
    regReadVal = spi.ReadReg(regReadId)
135
    if (expectedRegs[regReadId] != regReadVal):
136
      sys.stderr.write("ERROR: Reg %d - expected=0x%08x, actual=0x%08x\n" % \
137
          (expectedRegs[regReadId], regReadVal))
138
      return
139
 
140
    passCount = passCount + 1
141
    print("PASS [%d]" % (passCount))
142
 
143
#
144
# ReadRegsTest
145
#
146
# Reads out the value of all registers
147
#
148
def ReadRegsTest():
149
  for regId in range(16):
150
    regVal = spi.ReadReg(regId)
151
    print("Reg%d = 0x%08x" % (regId, regVal))
152
 
153
#
154
# WriteRegsTest
155
#
156
# Writes a test pattern to the spi registers
157
#
158
def WriteRegsTest():
159
  for regId in range(16):
160
    regVal = 255 - regId
161
    regVal = (regVal * 256) + regVal
162
    regVal = (regVal * 256 * 256) + regVal
163
    print("Reg%d = 0x%08x" % (regId, regVal))
164
    spi.WriteReg(regId=regId, value=regVal)
165
 
166
#
167
# XpsLoopbackTest
168
#
169
# Tests loopback utilizing the XPS/XSDK system and SPI memory + SPI.r0
170
#
171
def XpsLoopbackTest():
172
  dataBytes = 4096
173
 
174
  passCount = 0
175
 
176
  while (True):
177
    print("Starting run %d..." % (passCount + 1))
178
 
179
    # Generate random data blob
180
    dataToSend = GenRandomByteArray(dataBytes)
181
 
182
    # Send data blob
183
    print("Sending data to FPGA")
184
    spi.WriteMemory(dataToSend)
185
 
186
    # Set SPI.reg0 = 0x1 (M->S transfer complete)
187
    print("Setting spi.r0[0] = 1")
188
    spi.WriteReg(regId=0, value=0x1)
189
 
190
    # Wait for microblaze to finish DMAing data from MOSI to MISO buffer
191
    print("Waiting for FPGA's DMA to complete (poll spi.r0[1])")
192
    while 0 == (spi.ReadReg(regId=0) & 0x2):
193
      pass
194
 
195
    # Read MISO buffer
196
    print("Read data back from FPGA")
197
    recvData = spi.ReadMemory(len(dataToSend))
198
 
199
    print("Verify: comparing send and received data")
200
    # Verify loopbacked data matches original data
201
    errorCount = 0
202
    if len(recvData) != len(dataToSend):
203
      sys.stdout.write("[ERROR] received data blob length (%d) does not " + \
204
                       "match original length (%d)" % \
205
                       (len(recvData), len(dataToSend)))
206
      errorCount = errorCount + 1
207
      return
208
    for byteIndex in range(len(dataToSend)):
209
      if dataToSend[byteIndex] != recvData[byteIndex]:
210
        print("[ERROR] transmission mismatch - Index %d, Expect:0x%02x, " + \
211
              "Actual:0x%02x" % (byteIndex, dataToSend[byteIndex], \
212
              recvData[byteIndex]))
213
        errorCount = errorCount + 1
214
        if errorCount >= 5:
215
          print("Stopping due to errors...")
216
          return
217
 
218
    if 0 == errorCount:
219
      print("[PASS] Loopbacked without any errors\n")
220
      passCount = passCount + 1
221
    else:
222
      return
223
 
224
#
225
# PrintCliSyntax:
226
#
227
# Displays the syntax for the command line interface (CLI)
228
def PrintCliSyntax():
229
  print """
230
Syntax:
231
  spitest.py test [random-seed]
232
 
233
Valid tests (case sensitive):
234
  - SingleBytePacketsSend
235
  - MultiBytePacketSend
236
  - MemLoopback
237
  - RegLoopback
238
  - ReadRegs
239
  - WriteRegs
240
  - XpsLoopback
241
"""
242
 
243
#
244
# Main
245
#  
246
 
247
# Parse CLI
248
if len(sys.argv) < 2 or len(sys.argv) > 3:
249
  PrintCliSyntax()
250
  sys.exit(1)
251
 
252
# Retreive specified test function(s)
253
cliTest = sys.argv[1]
254
testMapping = {'SingleBytePacketsSend' : [SingleBytePacketsSendTest],
255
               'MultiBytePacketSend' : [MultiBytePacketSendTest],
256
               'MemLoopback' : [MemLoopbackTest],
257
               'RegLoopback' : [RegLoopbackTest],
258
               'ReadRegs' : [ReadRegsTest],
259
               'WriteRegs' : [WriteRegsTest],
260
               'XpsLoopback' : [XpsLoopbackTest]}
261
if cliTest not in testMapping:
262
  sys.stderr.write('%s is not a valid test.\n' % (cliTest,))
263
  PrintCliSyntax()
264
  sys.exit(1)
265
testsToRun = testMapping[cliTest]
266
 
267
# Seed random number generator
268
if len(sys.argv) == 3:
269
  seed = int(sys.argv[2])
270
else:
271
  seed = datetime.datetime.utcnow().microsecond
272
 
273
# Initialize Cheetah SPI/USB adapter
274
spi = spilib.SpiComm()
275
 
276
# Seed random
277
print("Random seed: %d" % seed)
278
random.seed(seed)
279
 
280
# Run specified test
281
for testToRun in testsToRun:
282
  testToRun()
283
 
284
# Close and exit
285
sys.stdout.write("Exiting...\n")
286
sys.exit(0)
287
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.