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
|
"""
# TOP2049 Open Source programming suite
#
# Cypress M8C In System Serial Programmer
#
# Copyright (c) 2010-2011 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 *
import time
class Chip_M8C_ISSP(Chip):
ISSPCMD_POR = 1 # Perform a power-on-reset
ISSPCMD_PWROFF = 2 # Turn power off
ISSPCMD_EXEC = 3 # Do an "execute" transfer
STAT_BUSY0 = 0x01
STAT_BUSY1 = 0x02
STAT_ISSPSTATE = 0x1C
STAT_ISSPSTATE_SHIFT = 2
STAT_SDATA = 0x20
STRVEC_INIT1 = (
"1100101010000000000111",
"0000000000000000000000",
"0000000000000000000000",
"0000000000000000000000",
"0000000000000000000000",
"0000000000000000000000",
"1101111011100010000111",
"1101111101000000000111",
"1101111011100000000111",
"1101111011100010000111",
"1101111111000000100111",
"1101110001000000100111",
"1101110000000000011111",
"1101111011100000000111",
"1001111100000111010111",
"1001111100100000011111",
"1001111101101000000111",
"1001111110000000000111",
"1001111111001010110111",
"1001111110100000001111",
"1001111111100000001111",
"1001111111110000000111",
"1101111011100010000111",
"1101110001000000000111",
"1101111111000000000111",
"1101110000000000000111",
"1101111011100000000111",
"1101111010000000011111",
"1101111010100000000111",
"1101111011000000000111",
"1101111100000000000111",
"1101111100100110000111",
)
STRVEC_INIT2 = (
"1001111101000000000111",
"1101111000000000110111",
"1101111100000000000111",
"1101111111100010010111",
)
STRVEC_IDSETUP = (
"1101111011100010000111",
"1101110000000000010111",
"1101111011100010000111",
"1101111101000000000111",
"1101111011100000000111",
"1101111011100010000111",
"1101111111000000100111",
"1101110001000000100111",
"1101110000000000011111",
"1101111011100000000111",
"1001111100000111010111",
"1001111100100000011111",
"1001111101101000000111",
"1001111110000000000111",
"1001111111001010110111",
"1001111110100000001111",
"1001111111100000001111",
"1001111111110000000111",
"1101111011100010000111",
"1101110001000000000111",
"1101111111000000000111",
"1101110000000000000111",
"1101111011100000000111",
"1101111010000000011111",
"1101111010100000000111",
"1101111011000000000111",
"1101111100000000000111",
"1101111100100110000111",
"1001111101000000000111",
"1101111000000000110111",
"1101111100000000000111",
"1101111111100010010111",
)
STRVEC_READBYTE = (
"101aaaaaaaaZDDDDDDDDZ1",
)
STRVEC_WRITEBYTE = (
"100aaaaaaaadddddddd111",
)
STRVEC_ERASEALL = (
"1001111110000010101111",
"1001111111001010110111",
"1101111011100010000111",
"1101111101000000000111",
"1101111011100000000111",
"1101111011100010000111",
"1101111111000000100111",
"1101110001000000100111",
"1101110000000000011111",
"1101111011100000000111",
"1001111100000111010111",
"1001111100100000011111",
"1001111101101000000111",
"1001111110000000000111",
"1001111111001010110111",
"1001111110100000001111",
"1001111111100000001111",
"1001111111110000000111",
"1101111011100010000111",
"1101110001000000000111",
"1101111111000000000111",
"1101110000000000000111",
"1101111011100000000111",
"1101111010000000011111",
"1101111010100000000111",
"1101111011000000000111",
"1101111100000000000111",
"1101111100100110000111",
"1101111000000000101111",
"1101111100000000000111",
"1101111111100010010111",
)
STRVEC_SETBLKNUM = (
"10011111010dddddddd111",
)
STRVEC_READBLK = (
"1101111011100010000111",
"1101111101000000000111",
"1101111011100000000111",
"1101111011100010000111",
"1101111111000000100111",
"1101110001000000100111",
"1101110000000000011111",
"1101111011100000000111",
"1001111100000111010111",
"1001111100100000011111",
"1001111101101000000111",
"1001111110000000000111",
"1001111111001010110111",
"1001111110100000001111",
"1001111111100000001111",
"1001111111110000000111",
"1101111011100010000111",
"1101110001000000000111",
"1101111111000000000111",
"1101110000000000000111",
"1101111011100000000111",
"1101111010000000011111",
"1101111010100000000111",
"1101111011000000000111",
"1101111100000000000111",
"1101111100100110000111",
"1101111000000000001111",
"1101111100000000000111",
"1101111111100010010111",
)
STRVEC_WRITEBLK = (
"1001111110001010100111",
"1001111111001010110111",
"1101111011100010000111",
"1101111101000000000111",
"1101111011100000000111",
"1101111011100010000111",
"1101111111000000100111",
"1101110001000000100111",
"1101110000000000011111",
"1101111011100000000111",
"1001111100000111010111",
"1001111100100000011111",
"1001111101101000000111",
"1001111110000000000111",
"1001111111001010110111",
"1001111110100000001111",
"1001111111100000001111",
"1001111111110000000111",
"1101111011100010000111",
"1101110001000000000111",
"1101111111000000000111",
"1101110000000000000111",
"1101111011100000000111",
"1101111010000000011111",
"1101111010100000000111",
"1101111011000000000111",
"1101111100000000000111",
"1101111100100110000111",
"1101111000000000010111",
"1101111100000000000111",
"1101111111100010010111",
)
STRVEC_READCHKSUM = (
"10111111001ZDDDDDDDDZ1",
"10111111000ZDDDDDDDDZ1",
)
STRVEC_READID = (
"10111111000ZDDDDDDDDZ1",
"10111111001ZDDDDDDDDZ1",
)
def __init__(self):
Chip.__init__(self)
# self.progmemSize = 1024 * 16
self.progmemSize = 256#XXX
def readSignature(self):
self.progressMeterInit("Reading chip ID", 0)
self.__powerOnReset()
gotID = self.__readID()
self.progressMeterFinish()
return int2byte(gotID & 0xFF) + int2byte((gotID >> 8) & 0xFF)
def erase(self):
self.progressMeterInit("Erasing chip", 0)
self.__powerOnReset()
self.__bitbangStringVectors(self.STRVEC_ERASEALL)
self.__runCommandSync(self.ISSPCMD_EXEC)
self.progressMeterFinish()
def writeProgmem(self, image):
if len(image) > self.progmemSize or len(image) % 64 != 0:
self.throwError("Invalid program memory image size %d "
"(expected <=%d and multiple of 64)" %\
(len(image), self.progmemSize))
self.progressMeterInit("Writing program memory", len(image))
self.__powerOnReset()
for blknum in range(0, len(image) // 64):
for i in range(0, 64):
self.progressMeter(blknum * 64 + i)
self.__writeByte(i, byte2int(image[blknum * 64 + i]))
vec = self.__stringVectorReplace(self.STRVEC_SETBLKNUM[0], "d", blknum)
self.__bitbangStringVector(vec)
self.__bitbangStringVectors(self.STRVEC_WRITEBLK)
self.__runCommandSync(self.ISSPCMD_EXEC)
self.progressMeterFinish()
def readProgmem(self):
self.progressMeterInit("Reading program memory", self.progmemSize)
self.__powerOnReset()
assert(self.progmemSize % 64 == 0)
image = []
for blknum in range(0, self.progmemSize // 64):
vec = self.__stringVectorReplace(self.STRVEC_SETBLKNUM[0], "d", blknum)
self.__bitbangStringVector(vec)
self.__bitbangStringVectors(self.STRVEC_READBLK)
self.__runCommandSync(self.ISSPCMD_EXEC)
for i in range(0, 64):
self.progressMeter(blknum * 64 + i)
image.append(int2byte(self.__readByte(i)))
#FIXME return_code
self.progressMeterFinish()
return b"".join(image)
def __powerDown(self):
"Turn the power to the device off"
self.printDebug("Powering device down...")
self.__runCommandSync(self.ISSPCMD_PWROFF)
self.top.hostDelay(5)
def __powerOnReset(self):
"Perform a complete power-on-reset and initialization"
self.top.vcc.setLayoutMask(0)
self.top.vpp.setLayoutMask(0)
self.top.gnd.setLayoutMask(0)
self.top.cmdSetVCCVoltage(5)
self.top.cmdSetVPPVoltage(5)
self.printDebug("Initializing supply power...")
self.top.gnd.setLayoutPins( (20,) )
self.top.vcc.setLayoutPins( (21,) )
#FIXME when to do exec?
self.__powerDown()
self.printDebug("Performing a power-on-reset...")
self.__uploadStringVector(self.STRVEC_INIT1[0])
self.__runCommandSync(self.ISSPCMD_POR)
self.printDebug("Sending vector 1...")
self.__bitbangStringVectors(self.STRVEC_INIT1[1:])
#XXX self.__runCommandSync(self.ISSPCMD_EXEC)
self.printDebug("Sending vector 2...")
self.__bitbangStringVectors(self.STRVEC_INIT2)
self.__runCommandSync(self.ISSPCMD_EXEC)
def __readID(self):
"Read the silicon ID"
self.__bitbangStringVectors(self.STRVEC_IDSETUP)
self.__runCommandSync(self.ISSPCMD_EXEC)
low = (self.__bitbangStringVector(self.STRVEC_READID[0]) >> 2) & 0xFF
high = (self.__bitbangStringVector(self.STRVEC_READID[1]) >> 2) & 0xFF
return low | (high << 8)
def __readByte(self, address):
vec = self.__stringVectorReplace(self.STRVEC_READBYTE[0], "a", address)
inputData = self.__bitbangStringVector(vec)
return (inputData >> 2) & 0xFF
def __writeByte(self, address, byte):
vec = self.__stringVectorReplace(self.STRVEC_WRITEBYTE[0], "a", address)
vec = self.__stringVectorReplace(vec, "d", byte)
self.__bitbangStringVector(vec)
def __loadCommand(self, command):
self.top.cmdFPGAWrite(0x11, command & 0xFF)
def __runCommandSync(self, command):
self.printDebug("Running synchronous command %d" % command)
self.__loadCommand(command)
self.__busyWait()
def __setBitbang(self, SDATA, SDATA_in, SCLK, SCLK_z):
value = 0
if SDATA:
value |= 0x01
if SDATA_in:
value |= 0x02
if SCLK:
value |= 0x04
if SCLK_z:
value |= 0x08
self.top.cmdFPGAWrite(0x10, value)
def __getStatusFlags(self):
self.top.cmdFPGARead(0x10)
stat = self.top.cmdReadBufferReg8()
isspState = (stat & self.STAT_ISSPSTATE) >> self.STAT_ISSPSTATE_SHIFT
sdata = bool(stat & self.STAT_SDATA)
isBusy = bool(stat & self.STAT_BUSY0) != bool(stat & self.STAT_BUSY1)
self.printDebug("isspState = 0x%02X, isBusy = %d, busyFlags = 0x%01X, sdata = %d" %\
(isspState, isBusy, (stat & (self.STAT_BUSY0 | self.STAT_BUSY1)), sdata))
return (isBusy, sdata, isspState)
def __busy(self):
(isBusy, sdata, isspState) = self.__getStatusFlags()
return isBusy
def __getSDATA(self):
(isBusy, sdata, isspState) = self.__getStatusFlags()
return int(sdata)
def __busyWait(self):
for i in range(0, 200):
if not self.__busy():
return
self.top.hostDelay(0.01)
self.throwError("Timeout in busywait. Chip not responding?")
def __stringVectorToBinary(self, vector):
binary = 0
inputMask = 0
assert(len(vector) == 22)
bit = len(vector) - 1
for b in vector:
if b == "1":
binary |= (1 << bit)
elif b == "0":
pass
elif b == "H" or b == "L" or b == "Z" or b == "D":
inputMask |= (1 << bit)
else:
assert(0)
bit -= 1
return (binary, inputMask)
def __stringVectorReplace(self, strVec, replace, data):
ret = ""
for i in range(len(strVec) - 1, -1, -1):
b = strVec[i]
if b == replace:
if (data & 1):
ret = "1" + ret
else:
ret = "0" + ret
data >>= 1
else:
ret = b + ret
return ret
def __bitbangStringVector(self, strVec):
vectorSize = len(strVec)
(vector, inputMask) = self.__stringVectorToBinary(strVec)
inputData = 0
self.__setBitbang(SDATA=0, SDATA_in=1, SCLK=0, SCLK_z=0)
for i in range(vectorSize - 1, -1, -1):
if inputMask & (1 << i):
self.__setBitbang(SDATA=0, SDATA_in=1, SCLK=1, SCLK_z=0)
self.__setBitbang(SDATA=0, SDATA_in=1, SCLK=0, SCLK_z=0)
self.top.cmdDelay(0.000001)
sdata = self.__getSDATA()
inputData |= (sdata << i)
else:
self.__setBitbang(SDATA=(vector & (1 << i)), SDATA_in=0,
SCLK=1, SCLK_z=0)
self.__setBitbang(SDATA=0, SDATA_in=0, SCLK=0, SCLK_z=0)
self.top.cmdDelay(0.000001)
return inputData
def __bitbangStringVectors(self, strVecList):
for strVec in strVecList:
self.__bitbangStringVector(strVec)
def __uploadStringVector(self, strVec):
(vector, inputMask) = self.__stringVectorToBinary(strVec)
assert(inputMask == 0)
self.top.cmdFPGAWrite(0x12, vector & 0xFF)
self.top.cmdFPGAWrite(0x13, (vector >> 8) & 0xFF)
self.top.cmdFPGAWrite(0x14, (vector >> 8) & 0xFF)
ChipDescription(
Chip_M8C_ISSP,
bitfile = "m8c-issp",
runtimeID = (0x0007, 0x01),
chipVendors = "Cypress",
description = "M8C In System Serial Programmer",
packages = ( ("M8C ISSP header", "Special adapter"), ),
comment = "Special adapter required",
broken = True
)
|