summaryrefslogtreecommitdiffstats
path: root/libtoprammer/toprammer_main.py
blob: 9f064951b8a5e7f0b5eb19d64403f706f86b839d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"""
#    TOP2049 Open Source programming suite
#
#    Copyright (c) 2009-2010 Michael Buesch <mb@bu3sch.de>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

VERSION_MAJOR	= 0
VERSION_MINOR	= 5
VERSION = "%d.%d" % (VERSION_MAJOR, VERSION_MINOR)

from bitfile import *
from util import *

import sys
import time
import re
try:
	import usb
except (ImportError), e:
	print "Python USB support module not found. Please install python-usb."
	sys.exit(1)

from top_xxxx import *
from chip_xxxx import *
from user_interface import *


class TOP:
	# Supported programmer types
	TYPE_TOP2049		= "TOP2049"

	def __init__(self, busDev=None, verbose=0,
		     forceLevel=0, noqueue=False, usebroken=False,
		     forceBitfileUpload=False,
		     userInterface=ConsoleUserInterface()):
		"""busDev is a tuple (BUSID, DEVID) or None."""

		self.verbose = verbose
		self.forceLevel = forceLevel
		self.forceBitfileUpload = forceBitfileUpload
		self.noqueue = noqueue
		self.usebroken = usebroken
		self.userInterface = userInterface

		self.chip = None
		self.commandQueue = []

		# Find the device
		for bus in usb.busses():
			if busDev and bus.dirname != "%03d" % busDev[0]:
				continue
			for dev in bus.devices:
				if busDev and dev.filename != "%03d" % busDev[1]:
					continue
				if self.__isTOP(dev):
					break
				if busDev:
					raise TOPException(
						"Device %03d.%03d is not a TOP device" %\
						(busDev[0], busDev[1]))
			else:
				continue
			break
		else:
			raise TOPException("TOP programmer device not found!")
		self.usbbus = bus
		self.usbdev = dev
		self.bulkOut = None
		self.bulkIn = None

		# Set up the USB interface
		try:
			self.usbh = self.usbdev.open()
			config = self.usbdev.configurations[0]
			interface = config.interfaces[0][0]

			# Find the endpoints
			for ep in interface.endpoints:
				if not self.bulkIn and \
				   ep.type == usb.ENDPOINT_TYPE_BULK and \
				   (ep.address & (usb.ENDPOINT_IN | usb.ENDPOINT_OUT)) == usb.ENDPOINT_IN:
					self.bulkIn = ep
				if not self.bulkOut and \
				   ep.type == usb.ENDPOINT_TYPE_BULK and \
				   (ep.address & (usb.ENDPOINT_IN | usb.ENDPOINT_OUT)) == usb.ENDPOINT_OUT:
					self.bulkOut = ep
			if not self.bulkIn or not self.bulkOut:
				raise TOPException("Did not find all USB EPs")

			self.usbh.setConfiguration(config)
			self.usbh.claimInterface(interface)
			self.usbh.setAltInterface(interface)
			self.usbh.clearHalt(self.bulkOut.address)
			self.usbh.clearHalt(self.bulkIn.address)
		except (usb.USBError), e:
			raise TOPException("USB error: " + str(e))

		if self.noqueue:
			self.printWarning("WARNING: Command queuing disabled. " +\
				"Hardware access will be _really_ slow.")

		self.__initializeHardware()

	def initializeChip(self, chipID):
		"Initialize the programmer for a chip"
		# If a chip is initialized, shut it down first.
		if self.chip:
			self.shutdownChip()
		# Find the implementation of the chip.
		(descriptor, self.chip) = ChipDescription.find(self.topType, chipID, self.usebroken)
		if not self.chip:
			raise TOPException("Did not find an implementation for the chip %s" % chipID)
		self.chip.setTOP(self)
		# Find the bitfile for the chip.
		bitfile = bitfileFind(descriptor.bitfile)
		if not bitfile:
			self.chip = None
			raise TOPException("Did not find BIT-file for chip implementation %s" % chipID)
		# Open and parse the bitfile.
		self.bitfile = Bitfile()
		self.bitfile.parseFile(bitfile)
		# Initialize the hardware.
		self.__bitfileUpload(descriptor.runtimeID)
		self.chip.initializeChip()

	def shutdownChip(self):
		if self.chip:
			self.chip.shutdownChip()
			self.flushCommands()
			self.chip = None

	def getChip(self):
		"Get the chip. May return None"
		return self.chip

	def checkChip(self):
		if not self.chip:
			raise TOPException("Target chip not selected")

	def getForceLevel(self):
		return self.forceLevel

	def progressMeterInit(self, meterId, message, nrSteps):
		if self.verbose >= 1:
			self.userInterface.progressMeterInit(meterId, message, nrSteps)

	def progressMeterFinish(self, meterId):
		if self.verbose >= 1:
			self.userInterface.progressMeterFinish(meterId)

	def progressMeter(self, meterId, step):
		if self.verbose >= 1:
			self.userInterface.progressMeter(meterId, step)

	def printWarning(self, message, newline=True):
		if self.verbose >= 0:
			self.flushCommands()
			self.userInterface.warningMessage(message, newline)

	def printInfo(self, message, newline=True):
		if self.verbose >= 1:
			self.flushCommands()
			self.userInterface.infoMessage(message, newline)

	def printDebug(self, message, newline=True):
		if self.verbose >= 2:
			self.flushCommands()
			self.userInterface.debugMessage(message, newline)

	@staticmethod
	def __isTOP(usbdev):
		"Returns true, if the USB device is a supported TOP programmer device."
		ids = (
			(0x2471, 0x0853),	# TOP2049
		)
		return (usbdev.idVendor, usbdev.idProduct) in ids

	def __initializeHardware(self):
		"Initialize the hardware"

		versionRegex = (
			(r"top2049\s+ver\s*(\d+\.\d+)", self.TYPE_TOP2049),
		)

		versionString = self.cmdRequestVersion()
		for (regex, topType) in versionRegex:
			m = re.match(regex, versionString, re.IGNORECASE)
			if m:
				self.topType = topType
				self.topVersion = m.group(1)
				break
		else:
			raise TOPException("Connected TOP programmer '" + versionString +\
				"' is not supported by Toprammer, yet")
		self.printInfo("Initializing the " + self.topType + " version " + self.topVersion)

		# Initialize the programmer specific layouts
		if self.topType == self.TYPE_TOP2049:
			self.vccx = top2049.vccx_layouts.VCCXLayout(self)
			self.vpp = top2049.vpp_layouts.VPPLayout(self)
			self.gnd = top2049.gnd_layouts.GNDLayout(self)
		else:
			assert(0)

		self.queueCommand("\x0D")
		stat = self.cmdReadStatusReg32()
		if stat != 0x00020C69:
			raise TOPException("Init: Unexpected status register (a): 0x%08X" % stat)

		self.cmdSetVPPVoltage(0)
		self.cmdSetVPPVoltage(0)
		self.queueCommand("\x0E\x20\x00\x00")
		self.cmdFlush()
		self.cmdSetVCCXVoltage(0)

		self.cmdLoadGNDLayout(0)
		self.cmdLoadVPPLayout(0)
		self.cmdLoadVCCXLayout(0)

		self.queueCommand("\x0E\x20\x00\x00")
		self.cmdFlush()
		self.queueCommand("\x0E\x25\x00\x00")
		stat = self.cmdReadStatusReg32()
		if stat != 0x0000686C:
			raise TOPException("Init: Unexpected status register (b): 0x%08X" % stat)

	def __bitfileUpload(self, requiredRuntimeID):
		(requiredID, requiredRevision) = requiredRuntimeID
		if requiredID and requiredRevision and not self.forceBitfileUpload:
			# Check if the bitfile is already uploaded.
			self.cmdFPGAReadRaw(0xFD)
			self.cmdFPGAReadRaw(0xFE)
			self.cmdFPGAReadRaw(0xFF)
			data = self.cmdReadStatusReg()
			gotID = ord(data[0]) | (ord(data[1]) << 8)
			if gotID == 0xFEFD or gotID == 0xFFFF:
				gotID = 0
			gotRev = ord(data[2])
			if gotRev == 0xFF:
				gotRev = 0
			if gotID == requiredID and gotRev == requiredRevision:
				self.printDebug("Bitfile %s ID 0x%04X Rev 0x%02X is already uploaded." %\
						(self.bitfile.getFilename(), gotID, gotRev))
				return
			self.printDebug("Current runtime ID 0x%04X Rev 0x%02X. Uploading new bitfile..." %\
					(gotID, gotRev))

		self.printDebug("Uploading bitfile %s..." % self.bitfile.getFilename())

		self.cmdFPGAInitiateConfig()
		stat = self.cmdReadStatusReg8()
		expected = 0x01
		if stat != expected:
			raise TOPException("bit-upload: Failed to initiate " +\
				"config sequence (got 0x%02X, expected 0x%02X)" %\
				(stat, expected))

		data = self.bitfile.getPayload()
		for i in range(0, len(data), 60):
			self.cmdFPGAUploadConfig(data[i : i + 60])
		self.flushCommands()

	def readSignature(self):
		"""Reads the device signature and returns it."""
		self.printDebug("Reading signature from chip...")
		self.checkChip()
		sig = self.chip.readSignature()
		self.printDebug("Done reading %d bytes." % len(sig))
		return sig

	def eraseChip(self):
		"""Erase the chip."""
		self.printDebug("Erasing chip...")
		self.checkChip()
		self.chip.erase()

	def readProgmem(self):
		"""Reads the program memory image and returns it."""
		self.printDebug("Reading program memory from chip...")
		self.checkChip()
		image = self.chip.readProgmem()
		self.printDebug("Done reading %d bytes." % len(image))
		return image

	def writeProgmem(self, image):
		"""Writes a program memory image to the chip."""
		self.printDebug("Writing %d bytes of program memory to chip..." % len(image))
		self.checkChip()
		self.chip.writeProgmem(image)
		self.printDebug("Done writing image.")

	def readEEPROM(self):
		"""Reads the EEPROM image and returns it."""
		self.printDebug("Reading EEPROM from chip...")
		self.checkChip()
		image = self.chip.readEEPROM()
		self.printDebug("Done reading %d bytes." % len(image))
		return image

	def writeEEPROM(self, image):
		"""Writes an EEPROM image to the chip."""
		self.printDebug("Writing %d bytes of EEPROM to chip..." % len(image))
		self.checkChip()
		self.chip.writeEEPROM(image)
		self.printDebug("Done writing image.")

	def readFuse(self):
		"""Reads the fuses image and returns it."""
		self.printDebug("Reading fuses from chip...")
		self.checkChip()
		image = self.chip.readFuse()
		self.printDebug("Done reading %d bytes." % len(image))
		return image

	def writeFuse(self, image):
		"""Writes a fuses image to the chip."""
		self.printDebug("Writing %d bytes of fuses to chip..." % len(image))
		self.checkChip()
		self.chip.writeFuse(image)
		self.printDebug("Done writing image.")

	def readLockbits(self):
		"""Reads the Lock bits image and returns it."""
		self.printDebug("Reading lock-bits from chip...")
		self.checkChip()
		image = self.chip.readLockbits()
		self.printDebug("Done reading %d bytes." % len(image))
		return image

	def writeLockbits(self, image):
		"""Writes a Lock bits image to the chip."""
		self.printDebug("Writing %d bytes of lock-bits to chip..." % len(image))
		self.checkChip()
		self.chip.writeLockbits(image)
		self.printDebug("Done writing image.")

	def cmdFlush(self, count=1):
		"""Send 'count' flush requests."""
		assert(count >= 1)
		self.flushCommands()
		self.queueCommand(chr(0x1B) * count)
		self.flushCommands()

	def cmdReadStatusReg(self):
		"""Read the status register. Returns 64 bytes."""
		self.queueCommand(chr(0x07))
		return self.receive(64)

	def cmdReadStatusReg8(self):
		"""Read a 8bit value from the status register."""
		stat = self.cmdReadStatusReg()
		stat = ord(stat[0])
		return stat

	def cmdReadStatusReg16(self):
		"""Read a 16bit value from the status register."""
		stat = self.cmdReadStatusReg()
		stat = ord(stat[0]) | (ord(stat[1]) << 8)
		return stat

	def cmdReadStatusReg24(self):
		"""Read a 24bit value from the status register."""
		stat = self.cmdReadStatusReg()
		stat = ord(stat[0]) | (ord(stat[1]) << 8) | (ord(stat[2]) << 16)
		return stat

	def cmdReadStatusReg32(self):
		"""Read a 32bit value from the status register."""
		stat = self.cmdReadStatusReg()
		stat = ord(stat[0]) | (ord(stat[1]) << 8) | \
		       (ord(stat[2]) << 16) | (ord(stat[3]) << 24)
		return stat

	def cmdReadStatusReg48(self):
		"""Read a 48bit value from the status register."""
		stat = self.cmdReadStatusReg()
		stat = ord(stat[0]) | (ord(stat[1]) << 8) | \
		       (ord(stat[2]) << 16) | (ord(stat[3]) << 24) | \
		       (ord(stat[4]) << 32) | (ord(stat[5]) << 40)
		return stat

	def cmdRequestVersion(self):
		"""Returns the device ID and versioning string."""
		self.queueCommand("\x0E\x11\x00\x00")
		data = self.cmdReadStatusReg()
		return data[0:16].strip()

	def cmdFPGAInitiateConfig(self):
		"""Initiate a configuration sequence on the FPGA."""
		self.queueCommand("\x0E\x21\x00\x00")

	def cmdFPGAUploadConfig(self, data):
		"""Upload configuration data into the FPGA."""
		assert(len(data) <= 60)
		cmd = "\x0E\x22\x00\x00" + data
		cmd += "\x00" * (64 - len(cmd)) # padding
		self.queueCommand(cmd)

	def cmdFPGAReadByte(self):
		"""Read a byte from the FPGA data line into the status register."""
		self.queueCommand("\x01")

	def cmdFPGAReadRaw(self, address):
		"""Read a byte from the FPGA at address into the status register."""
		cmd = chr(0x0B) + chr(address)
		self.queueCommand(cmd)

	def cmdFPGAWrite(self, address, byte):
		"""Write a byte to an FPGA address."""
		cmd = chr(0x0A) + chr(address) + chr(byte)
		self.queueCommand(cmd)

	def cmdLoadGNDLayout(self, layout):
		"""Load the GND configuration into the H/L shiftregisters."""
		cmd = chr(0x0E) + chr(0x16) + chr(layout) + chr(0)
		self.queueCommand(cmd)
		self.delay(0.15)
		self.cmdFlush()

	def cmdSetVPPVoltage(self, voltage):
		"""Set the VPP voltage. voltage is a floating point voltage number."""
		centivolt = int(voltage * 10)
		cmd = chr(0x0E) + chr(0x12) + chr(centivolt) + chr(0)
		self.queueCommand(cmd)
		self.cmdFlush()

	def cmdLoadVPPLayout(self, layout):
		"""Load the VPP configuration into the shift registers."""
		cmd = chr(0x0E) + chr(0x14) + chr(layout) + chr(0)
		self.queueCommand(cmd)
		self.delay(0.15)
		self.cmdFlush()

	def cmdSetVCCXVoltage(self, voltage):
		"""Set the VCCX voltage. voltage is a floating point voltage number."""
		centivolt = int(voltage * 10)
		cmd = chr(0x0E) + chr(0x13) + chr(centivolt) + chr(0)
		self.queueCommand(cmd)
		self.cmdFlush()

	def cmdLoadVCCXLayout(self, layout):
		"""Load the VCCX configuration into the shift registers."""
		cmd = chr(0x0E) + chr(0x15) + chr(layout) + chr(0)
		self.queueCommand(cmd)
		self.delay(0.15)
		self.cmdFlush()

	def __doSend(self, command):
		try:
			assert(len(command) <= 64)
			if self.verbose >= 3:
				print "Sending command:"
				dumpMem(command)
			ep = self.bulkOut.address
			self.usbh.bulkWrite(ep, command)
		except (usb.USBError), e:
			raise TOPException("USB bulk write error: " + str(e))

	def queueCommand(self, command):
		"""Queue a raw command for transmission."""
		assert(len(command) <= 64)
		if self.noqueue:
			self.__doSend(command)
		else:
			self.commandQueue.append(command)
			if len(self.commandQueue) >= 128:
				self.flushCommands()

	def receive(self, size):
		"""Receive 'size' bytes on the bulk-in ep."""
		# If there are blocked commands in the queue, send them now.
		self.flushCommands()
		try:
			ep = self.bulkIn.address
			data = ""
			for c in self.usbh.bulkRead(ep, size):
				data += chr(c)
			if len(data) != size:
				raise TOPException("USB bulk read error: Could not read the " +\
					"requested number of bytes (req %d, got %d)" % (size, len(data)))
			if self.verbose >= 3:
				print "Received data:"
				dumpMem(data)
		except (usb.USBError), e:
			raise TOPException("USB bulk read error: " + str(e))
		return data

	def flushCommands(self):
		"""Flush the command queue, but don't unblock it."""
		command = ""
		for oneCommand in self.commandQueue:
			assert(len(oneCommand) <= 64)
			if len(command) + len(oneCommand) > 64:
				self.__doSend(command)
				command = ""
			command += oneCommand
		if command:
			self.__doSend(command)
		self.commandQueue = []

	def delay(self, seconds):
		"""Flush all commands and delay for 'seconds'"""
		self.flushCommands()
		time.sleep(seconds)
bues.ch cgit interface