summaryrefslogtreecommitdiffstats
path: root/chip_atmega8dip28.py
blob: 3c6483c2f98a737a337735d8dfef5408ae5d58ec (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
"""
#    TOP2049 Open Source programming suite
#
#    Atmel Mega8 DIP28 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 *
import time


class ATMega8DIP28(Chip):
	# The Atmel Mega8 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):
		Chip.__init__(self, "atmega8dip28")

	def initializeChip(self):
		self.top.cmdSetGNDPin(18)
		self.top.cmdSetVCCXVoltage(5)
		self.top.cmdFlush()
		self.top.cmdSetVPPVoltage(0)
		self.top.cmdFlush()
		self.top.cmdSetVPPVoltage(12)

	def readImage(self):
		# First check if the chip is properly inserted
		self.top.blockCommands()
		self.top.cmdFlush()
		self.top.send("\x0A\x1D\x86")
		self.top.unblockCommands()

		self.top.cmdSetGNDPin(0)
		self.top.cmdLoadVPPLayout(0)
		self.top.cmdLoadVCCXLayout(0)

		self.top.blockCommands()
		self.top.cmdFlush(2)
		self.top.send("\x0A\x1B\xFF")
		self.top.unblockCommands()

		self.top.send("\x0E\x28\x00\x00")
		self.top.cmdSetVPPVoltage(0)
		self.top.cmdFlush()
		self.top.cmdSetVPPVoltage(5)
		self.top.cmdFlush(21)
		self.top.cmdLoadVPPLayout(14)

		self.top.blockCommands()
		self.top.cmdFlush(2)
		self.top.send("\x0B\x16")
		self.top.send("\x0B\x17")
		self.top.send("\x0B\x18")
		self.top.send("\x0B\x19")
		self.top.send("\x0B\x1A")
		self.top.send("\x0B\x1B")
		stat = self.top.cmdReadStatusReg32()
		self.top.unblockCommands()
		if stat != 0xFFFFFFC0:
			self.throwError("Did not detect chip. Please check connections.")

		self.top.cmdLoadVPPLayout(0)
		self.top.cmdLoadVCCXLayout(0)
		self.top.cmdFlush()
		self.top.send("\x0E\x28\x01\x00")
		self.top.send("\x0A\x1B\x00")
		self.top.cmdSetVPPVoltage(0)
		self.top.cmdFlush()
		self.top.cmdSetVPPVoltage(12)
		self.top.cmdFlush()
		self.top.cmdSetGNDPin(18)
		self.top.cmdFlush()
		self.top.cmdSetVCCXVoltage(4.4)
		self.top.cmdFlush()

		self.top.blockCommands()
		self.__setXA0(0)
		self.__setXA1(0)
		self.__setBS1(0)
		self.__setWR(0)
		self.top.unblockCommands()

		self.top.cmdSetGNDPin(18)
		self.top.cmdLoadVCCXLayout(13)

		self.top.blockCommands()
		self.top.cmdFlush()
		self.top.send("\x19")
		self.__setB1(0)
		self.top.send("\x34")
		self.__setB1(0)
		self.__setOE(0)
		self.__setWR(1)
		self.__setXTAL1(0)
		self.__setXA0(0)
		self.__setXA1(0)
		self.top.send("\x0A\x12\x08")
		self.__setBS1(0)
		self.__setBS2(0)
		self.__setPAGEL(0)
		self.__pulseXTAL1(10)
		self.top.send("\x19")
		self.top.unblockCommands()

		self.top.cmdLoadVPPLayout(7)

		self.top.blockCommands()
		self.top.send("\x34")
		self.top.send("\x0A\x12\x88")
		self.top.cmdFlush()
		self.top.unblockCommands()

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

		# Now read the image
		self.printInfo("Reading image ", newline=False)
		image = ""
		self.__setB1(1)
		high = 0
		self.top.blockCommands()
		for chunk in range(0, 256, 2):
			if chunk % 8 == 0:
				percent = (chunk * 100 / 256)
				if percent % 25 == 0:
					self.printInfo("%d%%" % percent, newline=False)
				else:
					self.printInfo(".", newline=False)
			self.top.send("\x34")
			self.__setB1(0)
			self.__setOE(1)
			self.__loadCommand(self.CMD_READFLASH)
			self.__loadAddrLow(chunk << 4)
			self.__loadAddrHigh(high)
			self.__setB1(1)
			for word in range(0, 31, 1):
				value = (chunk << 4) + (word + 1)
				high = (value >> 8) & 0xFF

				self.__readWordToStatusReg()
				self.__setB1(0)
				self.top.send("\x34")
				self.__setB1(0)
				self.__setOE(1)
				self.__loadCommand(self.CMD_READFLASH)
				self.__loadAddrLow(value)
				self.__loadAddrHigh(high)
				self.__setB1(1)
			self.__readWordToStatusReg()
			self.__setB1(0)
			data = self.top.cmdReadStatusReg()
			image += data
		self.top.unblockCommands()
		self.printInfo("100%")
		return image

	def writeImage(self, image):
		pass#TODO

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

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

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

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

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

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

	def __setB1(self, high):
		"""Set the B1 pin of the DUT"""
		if high:
			self.top.send("\x0A\x12\x81")
		else:
			self.top.send("\x0A\x12\x01")

	def __setOE(self, high):
		"""Set the OE pin of the DUT"""
		if high:
			self.top.send("\x0A\x12\x82")
		else:
			self.top.send("\x0A\x12\x02")

	def __setWR(self, high):
		"""Set the WR pin of the DUT"""
		if high:
			self.top.send("\x0A\x12\x83")
		else:
			self.top.send("\x0A\x12\x03")

	def __setBS1(self, high):
		"""Set the BS1 pin of the DUT"""
		if high:
			self.top.send("\x0A\x12\x84")
		else:
			self.top.send("\x0A\x12\x04")

	def __setXA0(self, high):
		"""Set the XA0 pin of the DUT"""
		if high:
			self.top.send("\x0A\x12\x85")
		else:
			self.top.send("\x0A\x12\x05")

	def __setXA1(self, high):
		"""Set the XA1 pin of the DUT"""
		if high:
			self.top.send("\x0A\x12\x86")
		else:
			self.top.send("\x0A\x12\x06")

	def __setXTAL1(self, high):
		"""Set the XTAL1 pin of the DUT"""
		if high:
			self.top.send("\x0A\x12\x87")
		else:
			self.top.send("\x0A\x12\x07")

	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"""
		if high:
			self.top.send("\x0A\x12\x89")
		else:
			self.top.send("\x0A\x12\x09")

	def __setBS2(self, high):
		"""Set the BS2 pin of the DUT"""
		if high:
			self.top.send("\x0A\x12\x8A")
		else:
			self.top.send("\x0A\x12\x0A")

supportedChips.append(ATMega8DIP28())
bues.ch cgit interface