summaryrefslogtreecommitdiffstats
path: root/chip_atmega_common.py
blob: 265cdaaf7c3f81f3bb901917cbfb2829f435b47e (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
"""
#    TOP2049 Open Source programming suite
#
#    Atmel Mega common support
#
#    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.
"""

from chip import *


class Chip_ATMega_common(Chip):
	# The Atmel Mega programming commands
	CMD_CHIPERASE		= 0x80 # Chip Erase
	CMD_WRITEFUSE		= 0x40 # Write Fuse Bits
	CMD_WRITELOCK		= 0x20 # Write Lock Bits
	CMD_WRITEFLASH		= 0x10 # Write Flash
	CMD_WRITEEEPROM		= 0x11 # Write EEPROM
	CMD_READSIG		= 0x08 # Read Signature bytes and Calibration byte
	CMD_READFUSELOCK	= 0x04 # Read Fuse and Lock bits
	CMD_READFLASH		= 0x02 # Read Flash
	CMD_READEEPROM		= 0x03 # Read EEPROM

	def __init__(self, chipID, signature,
		     presenceCheckLayout,
		     GNDLayout, VCCXLayout, VPPLayout,
		     flashPageSize, flashPages,
		     eepromPageSize, eepromPages,
		    ):
		Chip.__init__(self, chipID)
		self.signature = signature
		self.presenceCheckLayout = presenceCheckLayout
		self.GNDLayout = GNDLayout		# List of GND ZIF pins
		self.VCCXLayout = VCCXLayout		# List of VCCX ZIF pins
		self.VPPLayout = VPPLayout		# List of VPP ZIF pins
		self.flashPageSize = flashPageSize	# Flash page size, in words
		self.flashPages = flashPages		# Nr of flash pages
		self.eepromPageSize = eepromPageSize	# EEPROM page size, in bytes
		self.eepromPages = eepromPages		# Nr of EEPROM pages
		assert(eepromPageSize <= 64)

	def initializeChip(self):
		self.printDebug("Initializing chip")
		self.top.gnd.setLayoutPins(self.GNDLayout)
		self.top.vccx.setLayoutPins( [] )
		self.top.vpp.setLayoutPins( [] )
		self.top.cmdSetVCCXVoltage(5)
		self.top.cmdSetVPPVoltage(0)
		self.top.cmdSetVPPVoltage(12)

	def shutdownChip(self):
		self.printDebug("Shutdown chip")
		self.top.cmdSetVCCXVoltage(5)
		self.top.cmdSetVPPVoltage(5)
		self.top.vccx.setLayoutPins( [] )
		self.top.vpp.setLayoutPins( [] )
		self.top.gnd.setLayoutPins( [] )

	def readSignature(self):
		self.__initPins()

		(signature, calibration) = self.__readSigAndCalib()

		return signature

	def erase(self):
		self.__initPins()

		self.progressMeterInit("Erasing chip", 0)
		self.__loadCommand(self.CMD_CHIPERASE)
		self.__pulseWR()
		self.__waitForRDY()
		self.progressMeterFinish()

	def readProgmem(self):
		self.__initPins()

		self.progressMeterInit("Reading Flash", self.flashPages)
		image = ""
		for page in range(0, self.flashPages):
			self.progressMeter(page)
			readWords = 0
			for word in range(0, self.flashPageSize):
				self.__loadCommand(self.CMD_READFLASH)
				self.__loadAddr((page * self.flashPageSize) + word)
				self.__readWordToStatusReg()
				readWords += 1
				if readWords >= 32:
					image += self.top.cmdReadStatusReg()
					readWords = 0
			if readWords:
				data = self.top.cmdReadStatusReg()
				image += data[0:readWords]
		self.progressMeterFinish()
		return image

	def writeProgmem(self, image):
		flashBytes = self.flashPageSize * 2 * self.flashPages
		if len(image) != flashBytes:
			self.throwError("Invalid program memory image size %d (expected %d)" %\
				(len(image), flashBytes))
		self.__initPins()

		self.progressMeterInit("Writing Flash", self.flashPages)
		for page in range(0, self.flashPages):
			self.progressMeter(page)
			for word in range(0, self.flashPageSize):
				self.__loadCommand(self.CMD_WRITEFLASH)
				addr = (page * self.flashPageSize) + word
				self.__loadAddr(addr)
				addr *= 2
				data = image[addr : addr + 2]
				self.__loadData(ord(data[0]) | (ord(data[1]) << 8))
				self.__setBS1(1)
				self.__pulsePAGEL()
			self.__setBS1(0)
			self.__pulseWR()
			self.__waitForRDY()
		self.progressMeterFinish()

	def readEEPROM(self):
		self.__initPins()

		self.progressMeterInit("Reading EEPROM", self.eepromPages)
		image = ""
		for page in range(0, self.eepromPages):
			self.progressMeter(page)
			for byte in range(0, self.eepromPageSize):
				self.__loadCommand(self.CMD_READEEPROM)
				self.__loadAddr((page * self.eepromPageSize) + byte)
				self.__readLowByteToStatusReg()
			data = self.top.cmdReadStatusReg()
			image += data[0:self.eepromPageSize]
		self.progressMeterFinish()
		return image

	def writeEEPROM(self, image):
		eepromBytes = self.eepromPageSize * self.eepromPages
		if len(image) != eepromBytes:
			self.throwError("Invalid EEPROM image size %d (expected %d)" %\
				(len(image), eepromBytes))
		self.__initPins()

		self.progressMeterInit("Writing EEPROM", self.eepromPages)
		for page in range(0, self.eepromPages):
			self.progressMeter(page)
			for byte in range(0, self.eepromPageSize):
				self.__loadCommand(self.CMD_WRITEEEPROM)
				addr = (page * self.eepromPageSize) + byte
				self.__loadAddr(addr)
				data = image[addr]
				self.__loadDataLow(ord(data[0]))
				self.__pulsePAGEL()
			self.__setBS1(0)
			self.__pulseWR()
			self.__waitForRDY()
		self.progressMeterFinish()

	def readFuse(self):
		self.__initPins()

		self.progressMeterInit("Reading Fuse bits", 0)
		(fuse, lock) = self.__readFuseAndLockBits()
		self.progressMeterFinish()
		return fuse

	def writeFuse(self, image):
		if len(image) != 2:
			self.throwError("Invalid Fuses image size %d (expected %d)" %\
				(len(image), 2))
		self.__initPins()

		self.progressMeterInit("Writing Fuse bits", 0)
		self.__loadCommand(self.CMD_WRITEFUSE)
		self.__setBS2(0)
		self.__loadDataLow(ord(image[0]))
		self.__pulseWR()
		self.__waitForRDY()
		self.__loadCommand(self.CMD_WRITEFUSE)
		self.__loadDataLow(ord(image[1]))
		self.__setBS1(1)
		self.__pulseWR()
		self.__waitForRDY()
		self.progressMeterFinish()

	def readLockbits(self):
		self.__initPins()

		self.progressMeterInit("Reading lock bits", 0)
		(fuses, lockbits) = self.__readFuseAndLockBits()
		self.progressMeterFinish()

		return lockbits

	def writeLockbits(self, image):
		if len(image) != 1:
			self.throwError("Invalid lock-bits image size %d (expected %d)" %\
				(len(image), 1))
		self.__initPins()

		self.progressMeterInit("Writing lock bits", 0)
		self.__loadCommand(self.CMD_WRITELOCK)
		self.__loadDataLow(ord(image[0]))
		self.__pulseWR()
		self.__waitForRDY()
		self.progressMeterFinish()

	def __readSigAndCalib(self):
		"""Reads the signature and calibration bytes and returns them.
		This function expects a DUT present and pins initialized."""
		signature = ""
		calibration = ""
		for addr in range(0, 4):
			self.__loadCommand(self.CMD_READSIG)
			self.__loadAddr(addr)
			self.__readWordToStatusReg()
			data = self.top.cmdReadStatusReg()
			if addr <= 2:
				signature += data[0]
			calibration += data[1]
		return (signature, calibration)

	def __readFuseAndLockBits(self):
		"""Reads the Fuse and Lock bits and returns them.
		This function expects a DUT present and pins initialized."""
		self.__loadCommand(self.CMD_READFUSELOCK)
		self.__setBS2(0)
		self.__readWordToStatusReg()
		self.__setBS2(1)
		self.__readWordToStatusReg()
		self.__setBS2(0)
		data = self.top.cmdReadStatusReg()
		fuses = data[0] + data[3]
		lock = data[1]
		return (fuses, lock)

	def __initPins(self):
		"""Initialize the pin voltages and logic."""
		self.top.vpp.setLayoutPins( [] )
		self.top.vccx.setLayoutPins( [] )
		self.top.queueCommand("\x0E\x28\x01\x00")
		self.top.cmdFPGAWrite(0x1B, 0x00)
		self.top.cmdSetVPPVoltage(0)
		self.top.cmdSetVPPVoltage(12)
		self.top.gnd.setLayoutPins(self.GNDLayout)
		self.top.cmdSetVCCXVoltage(4.4)

		self.__setXA0(0)
		self.__setXA1(0)
		self.__setBS1(0)
		self.__setWR(0)
		self.top.flushCommands()

		self.top.gnd.setLayoutPins(self.GNDLayout)
		self.top.vccx.setLayoutPins(self.VCCXLayout)

		self.top.cmdFPGAReadRaw(0x16)
		self.top.cmdFPGAReadRaw(0x17)
		self.top.cmdFPGAReadRaw(0x18)
		self.top.cmdFPGAReadRaw(0x19)
		self.top.cmdFPGAReadRaw(0x1A)
		self.top.cmdFPGAReadRaw(0x1B)
		stat = self.top.cmdReadStatusReg48()
		if stat != self.presenceCheckLayout:
			msg = "Did not detect chip. Please check connections. (0x%012X)" % stat
			if self.top.getForceLevel() >= 2:
				self.printWarning(msg)
			else:
				self.throwError(msg)

		self.top.queueCommand("\x19")
		self.__setReadMode(0)
		self.top.queueCommand("\x34")
		self.__setReadMode(0)
		self.__setOE(0)
		self.__setWR(1)
		self.__setXTAL1(0)
		self.__setXA0(0)
		self.__setXA1(0)
		self.top.cmdFPGAWrite(0x12, 0x08)
		self.__setBS1(0)
		self.__setBS2(0)
		self.__setPAGEL(0)
		self.__pulseXTAL1(10)
		self.top.queueCommand("\x19")
		self.top.flushCommands()

		self.top.vpp.setLayoutPins(self.VPPLayout)

		self.top.queueCommand("\x34")
		self.top.cmdFPGAWrite(0x12, 0x88)
		self.__setOE(1)
		self.top.cmdFlush()

		(signature, calibration) = self.__readSigAndCalib()
		if signature != self.signature:
			msg = "Unexpected device signature. " +\
			      "Want %02X%02X%02X, but got %02X%02X%02X" % \
				(ord(self.signature[0]), ord(self.signature[1]),
				 ord(self.signature[2]),
				 ord(signature[0]), ord(signature[1]),
				 ord(signature[2]))
			if self.top.getForceLevel() >= 1:
				self.printWarning(msg)
			else:
				self.throwError(msg)

		self.top.cmdFlush(10)
		self.top.queueCommand("\x0E\x1F\x00\x00")
		self.top.delay(0.1)
		stat = self.top.cmdReadStatusReg32()
		if stat != 0xB9C80101:
			self.throwError("read: Unexpected status value 0x%08X" % stat)

	def __readWordToStatusReg(self):
		"""Read a data word from the DUT into the status register."""
		self.__setReadMode(1)
		self.__setBS1(0)
		self.__setOE(0)
		self.top.cmdFPGAReadByte()
		self.__setBS1(1)
		self.top.cmdFPGAReadByte()
		self.__setOE(1)
		self.__setReadMode(0)

	def __readLowByteToStatusReg(self):
		"""Read the low data byte from the DUT into the status register."""
		self.__setReadMode(1)
		self.__setBS1(0)
		self.__setOE(0)
		self.top.cmdFPGAReadByte()
		self.__setOE(1)
		self.__setReadMode(0)

	def __readHighByteToStatusReg(self):
		"""Read the high data byte from the DUT into the status register."""
		self.__setReadMode(1)
		self.__setBS1(1)
		self.__setOE(0)
		self.top.cmdFPGAReadByte()
		self.__setOE(1)
		self.__setReadMode(0)

	def __loadData(self, data):
		"""Load a data word."""
		self.__loadDataLow(data)
		self.__loadDataHigh(data >> 8)

	def __loadDataLow(self, dataLow):
		"""Load the low data byte."""
		self.__setBS1(0)
		self.__setXA0(1)
		self.__setXA1(0)
		self.top.cmdFPGAWrite(0x10, dataLow & 0xFF)
		self.__pulseXTAL1()

	def __loadDataHigh(self, dataHigh):
		"""Load the high data byte."""
		self.__setBS1(1)
		self.__setXA0(1)
		self.__setXA1(0)
		self.top.cmdFPGAWrite(0x10, dataHigh & 0xFF)
		self.__pulseXTAL1()

	def __loadAddr(self, addr):
		"""Load an address word."""
		self.__loadAddrLow(addr)
		self.__loadAddrHigh(addr >> 8)

	def __loadAddrLow(self, addrLow):
		"""Load the low address byte."""
		self.__setBS1(0)
		self.__setXA0(0)
		self.__setXA1(0)
		self.top.cmdFPGAWrite(0x10, addrLow & 0xFF)
		self.__pulseXTAL1()

	def __loadAddrHigh(self, addrHigh):
		"""Load the high address byte."""
		self.__setBS1(1)
		self.__setXA0(0)
		self.__setXA1(0)
		self.top.cmdFPGAWrite(0x10, addrHigh & 0xFF)
		self.__pulseXTAL1()

	def __loadCommand(self, command):
		"""Load a command into the device."""
		self.top.queueCommand("\x34")
		self.__setBS1(0)
		self.top.queueCommand("\x34")
		self.__setXA0(0)
		self.__setXA1(1)
		self.top.cmdFPGAWrite(0x10, command)
		self.__pulseXTAL1()

	def __setReadMode(self, high):
		"""Put the FPGA into read mode."""
		value = 0x01
		if high:
			value |= 0x80
		self.top.cmdFPGAWrite(0x12, value)

	def __waitForRDY(self):
		"""Wait for the RDY pin to go high."""
		self.top.delay(0.01)
		for i in range(0, 50):
			if self.__getRDY():
				return
			self.top.delay(0.01)
		self.throwError("Timeout waiting for READY signal from chip.")

	def __getRDY(self):
		"""Read the state of the RDY/BSY pin."""
		return bool(self.__getStatus() & 0x01)

	def __getStatus(self):
		"""Read the programmer status register"""
		self.top.cmdFPGAReadRaw(0x12)
		stat = self.top.cmdReadStatusReg()
		return ord(stat[0])

	def __setOE(self, high):
		"""Set the OE pin of the DUT"""
		value = 0x02
		if high:
			value |= 0x80
		self.top.cmdFPGAWrite(0x12, value)

	def __setWR(self, high):
		"""Set the WR pin of the DUT"""
		value = 0x03
		if high:
			value |= 0x80
		self.top.cmdFPGAWrite(0x12, value)

	def __pulseWR(self, count=1):
		"""Do a negative pulse on the WR pin of the DUT"""
		while count > 0:
			self.__setWR(0)
			self.__setWR(1)
			count -= 1

	def __setBS1(self, high):
		"""Set the BS1 pin of the DUT"""
		value = 0x04
		if high:
			value |= 0x80
		self.top.cmdFPGAWrite(0x12, value)

	def __setXA0(self, high):
		"""Set the XA0 pin of the DUT"""
		value = 0x05
		if high:
			value |= 0x80
		self.top.cmdFPGAWrite(0x12, value)

	def __setXA1(self, high):
		"""Set the XA1 pin of the DUT"""
		value = 0x06
		if high:
			value |= 0x80
		self.top.cmdFPGAWrite(0x12, value)

	def __setXTAL1(self, high):
		"""Set the XTAL1 pin of the DUT"""
		value = 0x07
		if high:
			value |= 0x80
		self.top.cmdFPGAWrite(0x12, value)

	def __pulseXTAL1(self, count=1):
		"""Do a positive pulse on the XTAL1 pin of the DUT"""
		while count > 0:
			self.__setXTAL1(1)
			self.__setXTAL1(0)
			count -= 1

	def __setPAGEL(self, high):
		"""Set the PAGEL pin of the DUT"""
		value = 0x09
		if high:
			value |= 0x80
		self.top.cmdFPGAWrite(0x12, value)

	def __pulsePAGEL(self, count=1):
		"""Do a positive pulse on the PAGEL pin of the DUT"""
		while count > 0:
			self.__setPAGEL(1)
			self.__setPAGEL(0)
			count -= 1

	def __setBS2(self, high):
		"""Set the BS2 pin of the DUT"""
		value = 0x0A
		if high:
			value |= 0x80
		self.top.cmdFPGAWrite(0x12, value)
bues.ch cgit interface