summaryrefslogtreecommitdiffstats
path: root/libtoprammer/chips/at89c2051dip20.py
blob: c23a89dd0f5044907ac24ca4721da11895710728 (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
"""
#    TOP2049 Open Source programming suite
#
#    Atmel AT89C2051 DIP20 Support
#
#    Copyright (c) 2010 Guido
#    Copyright (c) 2010 Michael Buesch <m@bues.ch>
#
#    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 libtoprammer.chip import *


class Chip_AT89C2051dip20(Chip):
	STAT_BUSY	= 0x01 # Programmer is running a command
	STAT_ERR	= 0x02 # Error during write

	def __init__(self):
		Chip.__init__(self,
			      chipPackage = "DIP20",
			      chipPinVCC = 20,
			      chipPinsVPP = 1,
			      chipPinGND = 10)

	def __initChip(self):
		self.applyVCC(False)
		self.applyVPP(False)
		self.applyGND(True)
		self.top.cmdSetVCCVoltage(5)
		self.top.cmdSetVPPVoltage(5)

	def readSignature(self):
		self.__initChip()
		self.applyGND(True)
		self.applyVCC(True)
		self.top.cmdSetVPPVoltage(5)
		self.__loadCommand(5) # VPP on
		self.__loadCommand(1) # set P3.2
		self.__setP3x(P33=0, P34=0, P35=0, IA=0)
		data = ""
		self.top.cmdFPGARead(0x10)
		self.__setP3x(P33=0, P34=0, P35=0, IA=1)
		self.__setP3x(P33=0, P34=0, P35=0, IA=0)
		self.top.cmdFPGARead(0x10)
		self.__setP3x(P33=0, P34=0, P35=0, IA=1)
		self.__setP3x(P33=0, P34=0, P35=0, IA=0)
		self.top.cmdFPGARead(0x10)
		data += self.top.cmdReadBufferReg()
		self.__setP3x(P33=0, P34=1, P35=0, IA=0)
		self.__loadCommand(6) # VPP off
		signature = ""
		signature += data[0]
		signature += data[1]
		self.top.printInfo("Signature: %X, %X"  % (byte2int(signature[0]), byte2int(signature[1])))
		return signature

	def erase(self):
		self.__initChip()
		self.applyGND(True)
		self.applyVCC(True)
		self.__loadCommand(1) # set P3.2
		self.top.cmdSetVPPVoltage(5)
		self.applyVPP(True)
		self.__loadCommand(5) # VPP on
		self.__setP3x(P33=1, P34=0, P35=0, IA=0)
		self.top.cmdSetVPPVoltage(12)
		self.__runCommandSync(4)
		self.applyVPP(False)
		self.top.cmdSetVPPVoltage(5)
		self.__setP3x(P33=0, P34=1, P35=0, IA=0)
		self.__loadCommand(5) # VPP off
		self.top.flushCommands()
		self.top.printInfo("at89c2051dip20: Erasing flash, verifying ...")
		ok = self.__verifyErase()
		if ok == 0:
			self.top.printInfo("at89c2051dip20: Erase done.")
		else:
			self.top.printInfo("at89c2051dip20: Erase failed!")

	def readProgmem(self):
		self.__initChip()
		self.applyGND(True)
		self.applyVCC(True)
		self.__loadCommand(1) # set P3.2
		self.top.cmdSetVPPVoltage(5)
		self.applyVPP(True)
		self.__loadCommand(5) # VPP on
		self.__setP3x(P33=0, P34=0, P35=1, IA=0)
		image = ""
		byteCount = 0
		self.progressMeterInit("Reading Flash", 0x800)
		for addr in range(0, 0x800):
			self.progressMeter(addr)
			self.top.cmdFPGARead(0x10)
			self.__setP3x(P33=0, P34=0, P35=1, IA=1)
			self.__setP3x(P33=0, P34=0, P35=1, IA=0)
			byteCount += 1
			if byteCount == self.top.getBufferRegSize():
				image += self.top.cmdReadBufferReg(byteCount)
				byteCount = 0
		image += self.top.cmdReadBufferReg(byteCount)
		self.applyVPP(False)
		self.__setP3x(P33=0, P34=1, P35=0, IA=0)
		self.__loadCommand(5) # VPP off
		self.top.flushCommands()
		self.progressMeterFinish()

		return image

	def writeProgmem(self, image):
		if len(image) > 0x800:
			self.throwError("Invalid EPROM image size %d (expected <=%d)" %\
				(len(image), 0x800))
		self.__initChip()
		self.applyGND(True)
		self.applyVCC(True)
		self.__loadCommand(1) # set P3.2
		self.top.cmdSetVPPVoltage(5)
		self.applyVPP(True)
		self.__loadCommand(5) # VPP on
		self.__setP3x(P33=0, P34=1, P35=1, IA=0)
		self.top.cmdSetVPPVoltage(12)
		self.progressMeterInit("Writing Flash", len(image))
		for addr in range(0, len(image)):
			self.progressMeter(addr)
			data = byte2int(image[addr])
			if data != 0xFF:
				self.__loadData(data)
				self.__loadCommand(3)
				ok = self.__progWait()
				if (ok & self.STAT_ERR) != 0:
					self.throwError("Write byte failed.")
			self.__setP3x(P33=0, P34=1, P35=1, IA=1)
			self.__setP3x(P33=0, P34=1, P35=1, IA=0)
		self.applyVPP(False)
		self.top.cmdSetVPPVoltage(5)
		self.__setP3x(P33=0, P34=1, P35=0, IA=0)
		self.__loadCommand(5) # VPP off
		self.top.flushCommands()
		self.progressMeterFinish()
		ok = self.__verifyProgmem(image)
		if ok == 0:
			self.top.printInfo("at89c2051dip20: Write flash done.")
		else:
			self.top.printInfo("at89c2051dip20: Write flash failed!")

	def __verifyErase(self):
		ok = 0
		image = self.readProgmem()
		for addr in range(0, 0x800):
			if byte2int(image[addr]) != 0xFF:
				ok = 1
		return ok

	def __verifyProgmem(self,image):
		data = self.readProgmem()
		ok = 0
		for addr in range(0, 0x800):
			if byte2int(image[addr]) != byte2int(data[addr]):
				ok = 1
		return ok

	def __loadData(self, data):
		self.top.cmdFPGAWrite(0x10, data)

	def __loadCommand(self, command):
		self.top.cmdFPGAWrite(0x12, command & 0xFF)

	def __runCommandSync(self, command):
		self.__loadCommand(command)
		self.__busyWait()

	def __setP3x(self, P33, P34, P35, IA):
		data = 0
		if P33:
			data |= 1
		if P34:
			data |= 2
		if P35:
			data |= 4
		if IA:
			data |= 8
		self.top.cmdFPGAWrite(0x16, data)

	def __getStatusFlags(self):
		self.top.cmdFPGARead(0x12)
		stat = self.top.cmdReadBufferReg()
		return byte2int(stat[0])

	def __busy(self):
		return bool(self.__getStatusFlags() & self.STAT_BUSY)

	def __busyWait(self):
		for i in range(0, 26):
			if not self.__busy():
				return
			self.top.hostDelay(0.001)
		self.throwError("Timeout in busywait.")

	def __progWait(self):
		for i in range(0,4):
			self.top.cmdFPGARead(0x12)
			stat = self.top.cmdReadBufferReg()
			if (byte2int(stat[0]) & self.STAT_BUSY) == 0:
				return byte2int(stat[0])
			self.top.hostDelay(0.001)
		self.throwError("Timeout in busywait.")

ChipDescription(
	Chip_AT89C2051dip20,
	bitfile = "at89c2051dip20",
	runtimeID = (0x0005, 0x01),
	chipVendors = "Atmel",
	description = "AT89C2051",
	maintainer = None,
	packages = ( ("DIP20", ""), )
)
bues.ch cgit interface