1 |
2 |
nuubik |
#! /usr/bin/python
|
2 |
17 |
nuubik |
# -*- coding: ISO-8859-1 -*-
|
3 |
|
|
|
4 |
2 |
nuubik |
##########################################################################
|
5 |
|
|
# LPC Dongle programming software
|
6 |
|
|
#
|
7 |
|
|
# Copyright (C) 2006 Artec Design
|
8 |
|
|
#
|
9 |
|
|
# This library is free software; you can redistribute it and/or
|
10 |
|
|
# modify it under the terms of the GNU Lesser General Public
|
11 |
|
|
# License as published by the Free Software Foundation; either
|
12 |
|
|
# version 2.1 of the License, or (at your option) any later version.
|
13 |
|
|
#
|
14 |
|
|
# This software is distributed in the hope that it will be useful,
|
15 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
17 |
|
|
# Lesser General Public License for more details.
|
18 |
|
|
|
19 |
|
|
# You should have received a copy of the GNU Lesser General Public
|
20 |
|
|
# License along with this library; if not, write to the Free Software
|
21 |
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
22 |
|
|
##########################################################################
|
23 |
|
|
|
24 |
|
|
#-------------------------------------------------------------------------
|
25 |
|
|
# Project: LPC Dongle programming software
|
26 |
|
|
# Name: dongle.py
|
27 |
|
|
# Purpose: Executable command line tool
|
28 |
|
|
#
|
29 |
17 |
nuubik |
# Author: J’ri Toomessoo <jyrit@artecdesign.ee>
|
30 |
8 |
nuubik |
# Copyright: (c) 2006 by Artec Design
|
31 |
2 |
nuubik |
# Licence: LGPL
|
32 |
|
|
#
|
33 |
|
|
# Created: 06 Oct. 2006
|
34 |
|
|
# History: 12 oct. 2006 Version 1.0 released
|
35 |
8 |
nuubik |
# 22 Feb. 2007 Test options added to test PCB board
|
36 |
17 |
nuubik |
# 14 Nov. 2007 Moved dongle spesific code to class Dongle from USPP
|
37 |
|
|
# USPP is allmost standard now (standard USPP would work)
|
38 |
|
|
# Artec USPP has serial open retry
|
39 |
|
|
# 14 Nov. 2007 Improved help.
|
40 |
2 |
nuubik |
#-------------------------------------------------------------------------
|
41 |
|
|
|
42 |
|
|
import os
|
43 |
|
|
import sys
|
44 |
|
|
import string
|
45 |
|
|
import time
|
46 |
|
|
from sets import *
|
47 |
|
|
from struct import *
|
48 |
|
|
from Uspp.uspp import *
|
49 |
|
|
|
50 |
|
|
#### global funcs ####
|
51 |
|
|
def usage(s):
|
52 |
17 |
nuubik |
print "Artec USB Dongle programming utility ver. 2.0"
|
53 |
|
|
print "Usage:"
|
54 |
|
|
print "Write file : ",s," [-vq] -c <name> <file> <offset>"
|
55 |
|
|
print "Readback file : ",s," [-vq] -c <name> [-vq] -r <offset> <length> <file>"
|
56 |
2 |
nuubik |
print "Options:"
|
57 |
17 |
nuubik |
print " <file> <offset> When file and offset are given file will be written to dongle"
|
58 |
|
|
print " file: File name to be written to dongle"
|
59 |
|
|
print " offset: Specifies data writing starting point in bytes to 4M window"
|
60 |
|
|
print " For ThinCan boot code the offset = 4M - filesize. To write"
|
61 |
|
|
print " 256K file the offset must be 3840K"
|
62 |
|
|
print " "
|
63 |
|
|
print " -c <name> Indicate port name where the USB Serial Device is"
|
64 |
|
|
print " name: COM port name in Windows or Linux Examples: COM3,/dev/ttyS3"
|
65 |
|
|
print " See Device Manager in windows for USB Serial Port number"
|
66 |
|
|
print " "
|
67 |
|
|
print " -v Enable verbose mode. Displays more progress information"
|
68 |
|
|
print " "
|
69 |
|
|
print " -q Perform flash query to see if dongle flash is responding"
|
70 |
|
|
print " "
|
71 |
|
|
print " -r <offset> <length> <file> Readback data. Available window size is 4MB"
|
72 |
|
|
print " offset: Hexademical offset byte addres inside 4MB window"
|
73 |
|
|
print " length: Amount in bytes to read starting from offset. Example: 1M"
|
74 |
|
|
print " use M for MegaBytes, K for KiloBytes, none for bytes"
|
75 |
|
|
print " file: Filename where data will be written"
|
76 |
|
|
print " "
|
77 |
|
|
print " -e Erase device. Erases Full 4 MegaBytes"
|
78 |
8 |
nuubik |
print "Board test options: "
|
79 |
17 |
nuubik |
print " -t Marching one and zero test. Device must be empty"
|
80 |
|
|
print " To test dongle erase the flash with command -e"
|
81 |
|
|
print " Enables dongle memory tests to be executed by user"
|
82 |
|
|
print " "
|
83 |
|
|
print " -b Leave flash blanc after test. Used with option -t"
|
84 |
8 |
nuubik |
print ""
|
85 |
2 |
nuubik |
print "Examples:"
|
86 |
|
|
print ""
|
87 |
17 |
nuubik |
print " ",s," -c COM3 loader.bin 0 "
|
88 |
|
|
print " ",s," -c /dev/ttyS3 boot.bin 3840K"
|
89 |
|
|
print " ",s," -c COM3 -r 0x3C0000 256K flashcontent.bin"
|
90 |
2 |
nuubik |
######################
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
class DongleMode:
|
94 |
|
|
def __init__(self):
|
95 |
|
|
self.v = 0
|
96 |
|
|
self.f = 0
|
97 |
|
|
self.d = 0
|
98 |
|
|
self.q = 0
|
99 |
|
|
self.r = 0
|
100 |
8 |
nuubik |
self.t = 0
|
101 |
|
|
self.e = 0
|
102 |
|
|
self.b = 0
|
103 |
2 |
nuubik |
self.filename=""
|
104 |
|
|
self.portname=""
|
105 |
|
|
self.address=-1
|
106 |
|
|
self.offset=-1
|
107 |
|
|
self.length=-1
|
108 |
|
|
|
109 |
|
|
def convParamStr(self,param):
|
110 |
|
|
mult = 1
|
111 |
|
|
value = 0
|
112 |
|
|
str = param
|
113 |
|
|
if str.find("K")>-1:
|
114 |
|
|
mult = 1024
|
115 |
|
|
str=str.strip("K")
|
116 |
|
|
if str.find("M")>-1:
|
117 |
|
|
mult = 1024*1024
|
118 |
|
|
str=str.strip("M")
|
119 |
|
|
try:
|
120 |
|
|
if str.find("x")>-1:
|
121 |
|
|
value = int(str,0)*mult #conver hex string to int
|
122 |
|
|
else:
|
123 |
|
|
value = int(str)*mult #conver demical string to int
|
124 |
|
|
except ValueError:
|
125 |
|
|
print "Bad parameter format given for: ",param
|
126 |
|
|
|
127 |
|
|
return value
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
class Dongle:
|
133 |
|
|
def __init__(self,name, baud, timeout): #time out in millis 1000 = 1s baud like 9600, 57600
|
134 |
|
|
try:
|
135 |
|
|
self.tty = SerialPort(name,timeout, baud)
|
136 |
17 |
nuubik |
except SerialPortException , e:
|
137 |
|
|
print "Unable to open port " + name
|
138 |
2 |
nuubik |
sys.exit();
|
139 |
9 |
nuubik |
|
140 |
|
|
def testReturn(self,byteCount):
|
141 |
|
|
i=0
|
142 |
|
|
while don.tty.inWaiting()<byteCount:
|
143 |
|
|
i=i+1
|
144 |
|
|
if i==10000*byteCount:
|
145 |
|
|
break
|
146 |
|
|
if i==10000*byteCount:
|
147 |
|
|
return 0
|
148 |
|
|
return don.tty.inWaiting() ## ret two bytes
|
149 |
|
|
|
150 |
2 |
nuubik |
def getReturn(self,byteCount):
|
151 |
|
|
i=0
|
152 |
|
|
while don.tty.inWaiting()<byteCount:
|
153 |
|
|
i=i+1
|
154 |
|
|
if i==10000*byteCount:
|
155 |
|
|
break
|
156 |
|
|
if i==10000*byteCount:
|
157 |
9 |
nuubik |
print "Dongle not communicating"
|
158 |
|
|
sys.exit()
|
159 |
2 |
nuubik |
return don.tty.read(byteCount) ## ret two bytes
|
160 |
9 |
nuubik |
|
161 |
2 |
nuubik |
|
162 |
|
|
def write_command(self,command):
|
163 |
|
|
lsb = command&0xff
|
164 |
|
|
msb = (command>>8)&0xff
|
165 |
17 |
nuubik |
self.write_2bytes(msb,lsb)
|
166 |
|
|
|
167 |
|
|
def write_2bytes(self, msb,lsb):
|
168 |
|
|
"""Write one word MSB,LSB to the serial port MSB first"""
|
169 |
|
|
s = pack('BB', msb, lsb)
|
170 |
|
|
self.tty.write(s)
|
171 |
|
|
# Wait for the write to complete
|
172 |
|
|
#WaitForSingleObject(overlapped.hEvent, INFINITE)
|
173 |
2 |
nuubik |
|
174 |
|
|
def get_address_buf(self,address): #set word address
|
175 |
|
|
lsbyte = address&0xff
|
176 |
|
|
byte = (address>>8)&0xff
|
177 |
|
|
msbyte = (address>>16)&0xff
|
178 |
|
|
buffer = ""
|
179 |
|
|
buffer += chr(lsbyte)
|
180 |
|
|
buffer += chr(0xA0)
|
181 |
|
|
buffer += chr(byte)
|
182 |
|
|
buffer += chr(0xA1)
|
183 |
|
|
buffer += chr(msbyte)
|
184 |
|
|
buffer += chr(0xA2)
|
185 |
|
|
evaluate = (address>>24)
|
186 |
|
|
if evaluate != 0:
|
187 |
|
|
print "Addressign fault. Too large address passed"
|
188 |
|
|
sys.exit()
|
189 |
|
|
return buffer
|
190 |
|
|
|
191 |
|
|
|
192 |
|
|
def set_address(self,address): #set word address
|
193 |
|
|
lsbyte = address&0xff
|
194 |
|
|
byte = (address>>8)&0xff
|
195 |
|
|
msbyte = (address>>16)&0xff
|
196 |
|
|
evaluate = (address>>24)
|
197 |
|
|
if evaluate != 0:
|
198 |
|
|
print "Addressign fault. Too large address passed"
|
199 |
|
|
sys.exit()
|
200 |
17 |
nuubik |
self.write_2bytes(lsbyte,0xA0) #set internal address to dongle
|
201 |
|
|
self.write_2bytes(byte,0xA1) #set internal address to dongle
|
202 |
|
|
self.write_2bytes(msbyte,0xA2) #send query command
|
203 |
2 |
nuubik |
|
204 |
|
|
def read_data(self,wordCount,address):
|
205 |
|
|
command = 0
|
206 |
|
|
byteCount = wordCount<<1 #calc byte count
|
207 |
|
|
if wordCount>0 :
|
208 |
|
|
command = (command|wordCount)<<8;
|
209 |
|
|
command = command|0xCD
|
210 |
|
|
self.set_address(address) # send read address
|
211 |
|
|
self.write_command(command) # send get data command
|
212 |
|
|
return self.getReturn(byteCount)
|
213 |
|
|
else:
|
214 |
|
|
print "Word count can't be under 1"
|
215 |
|
|
sys.exit()
|
216 |
|
|
|
217 |
|
|
def read_status(self):
|
218 |
|
|
don.write_command(0x0070) # 0x0098 //clear status
|
219 |
|
|
command = 0
|
220 |
|
|
wordCount= 1 #calc byte count
|
221 |
|
|
byteCount = wordCount<<1
|
222 |
|
|
command = (command|wordCount)<<8;
|
223 |
|
|
command = command|0xCD
|
224 |
|
|
self.write_command(command) # send get data command
|
225 |
|
|
return self.getReturn(byteCount)
|
226 |
|
|
|
227 |
|
|
|
228 |
|
|
def get_block_no(self,address):
|
229 |
|
|
return address >> 16 # 16 bit mode block is 64Kwords
|
230 |
|
|
|
231 |
|
|
def wait_on_busy(self):
|
232 |
|
|
exit=0
|
233 |
|
|
while exit==0:
|
234 |
|
|
buf=self.read_status()
|
235 |
|
|
statReg = ord(buf[0]) #8 bit reg
|
236 |
|
|
if statReg>>7 == 1:
|
237 |
|
|
exit=1
|
238 |
|
|
|
239 |
|
|
def parse_status(self): # use only after wait on busy commad to get result of the operation
|
240 |
|
|
exit = 0
|
241 |
|
|
buf=self.read_status()
|
242 |
|
|
statReg = ord(buf[0]) #8 bit reg
|
243 |
|
|
if (statReg>>5)&1 == 1:
|
244 |
|
|
print "Block erase suspended"
|
245 |
|
|
exit = 1
|
246 |
|
|
if (statReg>>4)&3 == 3:
|
247 |
|
|
print "Error in command order" #if bits 4 and 5 are set then
|
248 |
|
|
exit = 1
|
249 |
|
|
if (statReg>>4)&3 == 1:
|
250 |
|
|
print "Error in setting lock bit"
|
251 |
|
|
exit = 1
|
252 |
|
|
if (statReg>>3)&1 == 1:
|
253 |
|
|
print "Low Programming Voltage Detected, Operation Aborted"
|
254 |
|
|
exit = 1
|
255 |
|
|
if (statReg>>2)&1 == 1:
|
256 |
|
|
print "Programming suspended"
|
257 |
|
|
exit = 1
|
258 |
|
|
if (statReg>>1)&1 == 1:
|
259 |
|
|
print "Block lock bit detected"
|
260 |
|
|
exit = 1
|
261 |
|
|
if exit == 1:
|
262 |
|
|
sys.exit()
|
263 |
|
|
|
264 |
|
|
def erase_block(self,blockNo):
|
265 |
|
|
blockAddress = blockNo << 16
|
266 |
|
|
command = 0x0020
|
267 |
|
|
self.set_address(blockAddress)
|
268 |
|
|
self.write_command(command) #issue block erase
|
269 |
|
|
command = 0x00D0
|
270 |
|
|
self.write_command(command) #issue block erase confirm
|
271 |
|
|
self.wait_on_busy()
|
272 |
|
|
self.parse_status()
|
273 |
|
|
|
274 |
|
|
def buffer_write(self,wordCount,startAddress,buffer):
|
275 |
|
|
# to speed up buffer writing compose all commands into one buffer
|
276 |
|
|
# instead of multiple single writes this is needed as the FTDI chip
|
277 |
|
|
# round lag is amazingly large with VCOM drivers
|
278 |
|
|
#u = len(buffer)
|
279 |
|
|
if len(buffer)<32: #don't ever make unaligned writes
|
280 |
|
|
i=len(buffer)
|
281 |
|
|
while len(buffer)<32:
|
282 |
|
|
buffer += "\xff"
|
283 |
|
|
adrBuf = self.get_address_buf(startAddress) #6 bytes total
|
284 |
|
|
cmd_e8="" #8 bytes total
|
285 |
|
|
cmd_e8+= chr(16) #make it always 16 wordCount
|
286 |
|
|
cmd_e8+= chr(0xE8)
|
287 |
|
|
cmd_wcnt="" #10 bytes total
|
288 |
|
|
cmd_wcnt+= chr(0x00)
|
289 |
|
|
cmd_wcnt+= chr(16-1)
|
290 |
|
|
cmd_buf="" #12 bytes total
|
291 |
|
|
cmd_buf+= chr(0x00)
|
292 |
|
|
cmd_buf+= chr(0xD0)
|
293 |
|
|
wr_buffer_cmd = adrBuf + cmd_e8 + cmd_wcnt + buffer + cmd_buf #44 bytes total
|
294 |
17 |
nuubik |
self.write_buf_cmd(wr_buffer_cmd)
|
295 |
2 |
nuubik |
# no wait needad as the FTDI chip is so slow
|
296 |
17 |
nuubik |
|
297 |
|
|
def write_buf_cmd(self, buffer):
|
298 |
|
|
"""Write one word MSB,LSB to the serial port MSB first"""
|
299 |
|
|
a=0
|
300 |
|
|
s=""
|
301 |
|
|
if (len(buffer) < 44): # if buffer is shorter than expected then pad with read array mode commands
|
302 |
|
|
i=0
|
303 |
|
|
while i<len(buffer):
|
304 |
|
|
print '0x%02x'%(ord(buffer[i]))
|
305 |
|
|
i+=1
|
306 |
|
|
while(a < len(buffer)):
|
307 |
|
|
if a < 10:
|
308 |
|
|
s= pack('2c', buffer[a], buffer[a+1])
|
309 |
|
|
self.tty.write(s)
|
310 |
|
|
elif a < len(buffer)-2:
|
311 |
|
|
s= pack('2c', buffer[a+1], buffer[a])
|
312 |
|
|
self.tty.write(s)
|
313 |
|
|
elif len(buffer)==2:
|
314 |
|
|
s=pack('2c', buffer[a], buffer[a+1])
|
315 |
|
|
self.tty.write(s)
|
316 |
|
|
else:
|
317 |
|
|
s=pack('2c', buffer[a], chr(0xFF))
|
318 |
|
|
self.tty.write(s)
|
319 |
|
|
a+=2
|
320 |
|
|
else:
|
321 |
|
|
#first 10 bytes are in correct order + 32 data bytes are in wrong order and + 2 confirm bytes are in correct order
|
322 |
|
|
s=pack('44c',
|
323 |
|
|
buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7],
|
324 |
|
|
buffer[8], buffer[9], buffer[11], buffer[10], buffer[13], buffer[12], buffer[15], buffer[14],
|
325 |
|
|
buffer[17], buffer[16], buffer[19], buffer[18], buffer[21], buffer[20], buffer[23], buffer[22],
|
326 |
|
|
buffer[25], buffer[24], buffer[27], buffer[26], buffer[29], buffer[28], buffer[31], buffer[30],
|
327 |
|
|
buffer[33], buffer[32], buffer[35], buffer[34], buffer[37], buffer[36], buffer[39], buffer[38],
|
328 |
|
|
buffer[41], buffer[40], buffer[42], buffer[43]
|
329 |
|
|
)
|
330 |
|
|
self.tty.write(s)
|
331 |
2 |
nuubik |
|
332 |
17 |
nuubik |
# Wait for the write to complete
|
333 |
|
|
#WaitForSingleObject(overlapped.hEvent, INFINITE)
|
334 |
|
|
n = 0
|
335 |
|
|
if sys.platform=='win32':
|
336 |
|
|
while (n < 1024):
|
337 |
|
|
n += 1;
|
338 |
|
|
elif sys.platform=='linux2':
|
339 |
|
|
#Linux FTDI VCP driver is way faster and needs longer grace time than windows driver
|
340 |
|
|
while (n < 1024*7):
|
341 |
|
|
n += 1;
|
342 |
2 |
nuubik |
|
343 |
17 |
nuubik |
|
344 |
2 |
nuubik |
################## Main program #########################
|
345 |
|
|
|
346 |
|
|
|
347 |
|
|
last_ops = 0
|
348 |
|
|
mode = DongleMode()
|
349 |
|
|
# PARSE ARGUMENTS
|
350 |
|
|
for arg in sys.argv:
|
351 |
|
|
if len(sys.argv) == 1: # if no arguments display help
|
352 |
8 |
nuubik |
#usage(sys.argv[0])
|
353 |
|
|
usage("dongle.py")
|
354 |
2 |
nuubik |
sys.exit()
|
355 |
|
|
if arg in ("-h","--help","/help","/h"):
|
356 |
8 |
nuubik |
#usage(sys.argv[0])
|
357 |
|
|
usage("dongle.py")
|
358 |
2 |
nuubik |
sys.exit()
|
359 |
|
|
if arg in ("-c"):
|
360 |
|
|
last_ops = sys.argv.index(arg) + 1 #if remains last set of options from here start ordered strings
|
361 |
|
|
i = sys.argv.index(arg)
|
362 |
|
|
print "Opening port: "+sys.argv[i+1]
|
363 |
|
|
mode.portname = sys.argv[i+1] # next element after -c open port for usage
|
364 |
|
|
if arg[0]=="-" and arg[1]!="c": # if other opptions
|
365 |
|
|
# parse all options in this
|
366 |
|
|
last_ops = sys.argv.index(arg) #if remains last set of options from here start ordered strings
|
367 |
|
|
ops = arg[1:]# get all besides the - sign
|
368 |
|
|
for op in ops:
|
369 |
|
|
if op=="q":
|
370 |
|
|
mode.q = 1
|
371 |
|
|
if op=="v":
|
372 |
|
|
mode.v = 1
|
373 |
|
|
if op=="f":
|
374 |
|
|
mode.f = 1
|
375 |
|
|
if op=="d":
|
376 |
|
|
mode.d = 1
|
377 |
|
|
if op=="r":
|
378 |
8 |
nuubik |
mode.r = 1
|
379 |
|
|
if op=="t":
|
380 |
|
|
mode.t = 1
|
381 |
|
|
if op=="e":
|
382 |
|
|
mode.e = 1
|
383 |
|
|
if op=="b":
|
384 |
|
|
mode.b = 1
|
385 |
2 |
nuubik |
else:
|
386 |
|
|
i = sys.argv.index(arg)
|
387 |
|
|
if i == last_ops + 1:
|
388 |
|
|
if mode.r==1:
|
389 |
|
|
mode.offset=mode.convParamStr(arg)
|
390 |
|
|
else:
|
391 |
|
|
mode.filename=arg
|
392 |
|
|
if i == last_ops + 2:
|
393 |
|
|
if mode.r==1:
|
394 |
|
|
mode.length=mode.convParamStr(arg)
|
395 |
|
|
else:
|
396 |
|
|
mode.address=mode.convParamStr(arg)
|
397 |
|
|
|
398 |
|
|
if i == last_ops + 3:
|
399 |
|
|
if mode.r==1:
|
400 |
|
|
mode.filename=arg
|
401 |
|
|
else:
|
402 |
|
|
print "Too many parameters provided"
|
403 |
|
|
sys.exit()
|
404 |
|
|
if i > last_ops + 3:
|
405 |
|
|
print "Too many parameters provided"
|
406 |
|
|
sys.exit()
|
407 |
|
|
|
408 |
|
|
# END PARSE ARGUMENTS
|
409 |
|
|
|
410 |
|
|
if mode.portname=="":
|
411 |
|
|
print "No port name given see -h for help"
|
412 |
|
|
sys.exit()
|
413 |
|
|
else:
|
414 |
|
|
# test PC speed to find sutable delay for linux driver
|
415 |
|
|
# to get 250 us
|
416 |
|
|
mytime = time.clock()
|
417 |
|
|
n = 0
|
418 |
|
|
while (n < 100000):
|
419 |
|
|
n += 1;
|
420 |
|
|
k10Time = time.clock() - mytime # time per 10000 while cycles
|
421 |
|
|
wait = k10Time/100000.0 # time per while cycle
|
422 |
|
|
wait = (0.00025/wait) * 1.20 # count for 250us + safe margin
|
423 |
|
|
# ok done
|
424 |
9 |
nuubik |
reopened = 0
|
425 |
|
|
|
426 |
|
|
don = Dongle(mode.portname,115200,100)
|
427 |
|
|
don.tty.wait = wait
|
428 |
|
|
while 1:
|
429 |
|
|
don.write_command(0x0050) # 0x0098
|
430 |
|
|
don.write_command(0x00C5) #send dongle check internal command
|
431 |
|
|
don_ret=don.testReturn(2)
|
432 |
|
|
if don_ret==2:
|
433 |
|
|
break
|
434 |
|
|
if reopened == 3:
|
435 |
|
|
print 'Dongle connected, but does not communicate'
|
436 |
|
|
sys.exit()
|
437 |
|
|
reopened = reopened + 1
|
438 |
|
|
# reopen and do new cycle
|
439 |
|
|
don = Dongle(mode.portname,115200,100)
|
440 |
|
|
don.tty.wait = wait
|
441 |
|
|
|
442 |
2 |
nuubik |
buf=don.getReturn(2) # two bytes expected to this command
|
443 |
9 |
nuubik |
|
444 |
2 |
nuubik |
if ord(buf[1])==0x32 and ord(buf[0])==0x10:
|
445 |
|
|
print "Dongle OK"
|
446 |
|
|
else:
|
447 |
|
|
print 'Dongle returned on open: %02x %02x '%(ord(buf[1]), ord(buf[0]))
|
448 |
|
|
|
449 |
|
|
|
450 |
|
|
if mode.q == 1: # perform a query from dongle
|
451 |
9 |
nuubik |
|
452 |
|
|
buf=don.read_data(4,0x0) # word count and word address
|
453 |
2 |
nuubik |
don.write_command(0x0050) # 0x0098
|
454 |
|
|
don.write_command(0x0098) # 0x0098
|
455 |
|
|
buf=don.read_data(3,0x000010) # word count and word address
|
456 |
|
|
if ord(buf[0])==0x51 and ord(buf[2])==0x52 and ord(buf[4])==0x59:
|
457 |
|
|
buf=don.read_data(2,0x000000) # word count and word address
|
458 |
|
|
print 'Query OK, Factory: 0x%02x device: 0x%02x '%(ord(buf[0]),ord(buf[2]))
|
459 |
|
|
buf=don.read_data(2,0x000002)
|
460 |
|
|
print 'lock bit is 0x%02x 0x%02x'%(ord(buf[0]),ord(buf[1]))
|
461 |
|
|
else:
|
462 |
|
|
print "Got bad query data:"
|
463 |
|
|
print 'Query address 0x10 = 0x%02x%02x '%(ord(buf[1]),ord(buf[0]))
|
464 |
9 |
nuubik |
print 'Query address 0x12 = 0x%02x%02x '%(ord(buf[3]),ord(buf[2]))
|
465 |
|
|
print 'Query address 0x14 = 0x%02x%02x '%(ord(buf[5]),ord(buf[4]))
|
466 |
2 |
nuubik |
print "Read byte count:",len(buf)
|
467 |
|
|
|
468 |
|
|
don.write_command(0x00FF) # 0x0098
|
469 |
9 |
nuubik |
buf=don.read_data(4,0xff57c0>>1) # word count and word address
|
470 |
|
|
|
471 |
2 |
nuubik |
print 'Data: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x '%(ord(buf[1]),ord(buf[0]),ord(buf[3]),ord(buf[2]),ord(buf[5]),ord(buf[4]),ord(buf[7]),ord(buf[6]) )
|
472 |
|
|
|
473 |
|
|
|
474 |
|
|
|
475 |
|
|
if mode.filename!="" and mode.address!=-1:
|
476 |
|
|
#Calculate number of blocks and start of blocks
|
477 |
|
|
size = 0
|
478 |
|
|
mode.address = mode.address>>1 #make word address
|
479 |
|
|
try:
|
480 |
|
|
f=open(mode.filename,"rb")
|
481 |
|
|
f.seek(0,2) #seek to end
|
482 |
|
|
size = f.tell()
|
483 |
|
|
f.seek(0) #seek to start
|
484 |
|
|
print 'File size %iK '%(size/1024)
|
485 |
|
|
f.close()
|
486 |
|
|
except IOError:
|
487 |
|
|
print "IO Error on file open"
|
488 |
|
|
sys.exit()
|
489 |
|
|
#clear blockLock bits
|
490 |
|
|
don.write_command(0x0060) # 0x0098
|
491 |
|
|
don.write_command(0x00D0) # 0x0098
|
492 |
|
|
don.wait_on_busy()
|
493 |
|
|
don.parse_status()
|
494 |
|
|
wordSize = (size+ (size&1))>> 1 # round byte count up and make word address
|
495 |
|
|
endBlock = don.get_block_no(mode.address+wordSize - 1)
|
496 |
|
|
startBlock = don.get_block_no(mode.address)
|
497 |
|
|
i=startBlock
|
498 |
9 |
nuubik |
print 'Erasing from block %i to %i '%(i,endBlock)
|
499 |
2 |
nuubik |
while i <= endBlock:
|
500 |
9 |
nuubik |
if mode.v == 1:
|
501 |
|
|
print 'Erasing block %i '%(i)
|
502 |
|
|
else:
|
503 |
|
|
sys.stdout.write(".")
|
504 |
|
|
sys.stdout.flush()
|
505 |
2 |
nuubik |
don.erase_block(i)
|
506 |
|
|
don.wait_on_busy()
|
507 |
|
|
don.parse_status() #do this after programming all but uneaven ending
|
508 |
|
|
i=i+1
|
509 |
9 |
nuubik |
if mode.v == 0:
|
510 |
|
|
print " "
|
511 |
2 |
nuubik |
#don.write_command(0x00FF) # 0x0098
|
512 |
|
|
#buf=don.read_data(4,0x000000) # word count and word address
|
513 |
|
|
#print 'Data: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x '%(ord(buf[0]),ord(buf[1]),ord(buf[2]),ord(buf[3]),ord(buf[4]),ord(buf[5]),ord(buf[6]),ord(buf[7]) )
|
514 |
|
|
|
515 |
|
|
f=open(mode.filename,"rb")
|
516 |
|
|
f.seek(0) #seek to start
|
517 |
|
|
address= mode.address
|
518 |
|
|
#don.set_address(address)
|
519 |
9 |
nuubik |
print 'Writing %iK'%(size/1024)
|
520 |
2 |
nuubik |
while 1:
|
521 |
9 |
nuubik |
if (address/(1024*64) != (address-16)/(1024*64)) and address != mode.address: # get bytes from words if 512
|
522 |
|
|
if mode.v == 1:
|
523 |
|
|
print 'Progress: %iK of %iK at 0x%06x'%((address-mode.address)/512,size/1024,address)
|
524 |
|
|
else:
|
525 |
|
|
sys.stdout.write(".")
|
526 |
|
|
sys.stdout.flush()
|
527 |
2 |
nuubik |
buf = f.read(32) #16 words is maximum write here bytes are read
|
528 |
|
|
if len(buf)==32:
|
529 |
|
|
don.buffer_write(16,address,buf)
|
530 |
|
|
address = address + 16
|
531 |
|
|
elif len(buf)>0:
|
532 |
|
|
don.parse_status() #do this after programming all but uneaven ending
|
533 |
|
|
print "Doing an unaligned write..."
|
534 |
|
|
length = len(buf)
|
535 |
|
|
length = (length + (length&1))>> 1 #round up to get even word count
|
536 |
|
|
buf = buf+"\xff" #pad just in case rounding took place
|
537 |
|
|
don.buffer_write(len,address,buf)
|
538 |
|
|
address = address + 16 #inc word address
|
539 |
|
|
break
|
540 |
|
|
else:
|
541 |
|
|
break
|
542 |
9 |
nuubik |
if mode.v == 0:
|
543 |
|
|
print " "
|
544 |
2 |
nuubik |
print "Write DONE!"
|
545 |
|
|
don.parse_status() #do this after programming all but uneaven ending
|
546 |
|
|
f.close()
|
547 |
|
|
|
548 |
|
|
if mode.r == 1: # perform a readback
|
549 |
|
|
if mode.offset!=-1 and mode.length!=-1 and mode.filename!="":
|
550 |
|
|
mode.offset=mode.offset>>1 #make word offset
|
551 |
|
|
mode.length= mode.length>>1 #make word length
|
552 |
9 |
nuubik |
print 'Reading %iK'%(mode.length/512)
|
553 |
2 |
nuubik |
try:
|
554 |
|
|
f=open(mode.filename,"wb")
|
555 |
|
|
don.write_command(0x00FF) # put flash to data read mode
|
556 |
|
|
address = mode.offset # set word address
|
557 |
|
|
while 1:
|
558 |
|
|
if address/(1024*32) != (address-128)/(1024*32): # get K bytes from words if 512
|
559 |
9 |
nuubik |
if mode.v == 1:
|
560 |
|
|
print 'Progress: %iK of %iK'%((address-mode.offset)/512,mode.length/512)
|
561 |
|
|
else:
|
562 |
|
|
sys.stdout.write(".")
|
563 |
|
|
sys.stdout.flush()
|
564 |
2 |
nuubik |
buf=don.read_data(128,address) # word count and byte address read 64 words to speed up
|
565 |
|
|
f.write(buf)
|
566 |
|
|
#print "from address:",address<<1," ", len(buf)
|
567 |
|
|
if address+128 >= (mode.offset + mode.length): # 2+64 estimates the end to end in right place
|
568 |
|
|
break
|
569 |
|
|
address = address + 128 #this is word address
|
570 |
|
|
f.close()
|
571 |
9 |
nuubik |
if mode.v == 0:
|
572 |
|
|
print " "
|
573 |
2 |
nuubik |
print "Readback done!"
|
574 |
|
|
except IOError:
|
575 |
|
|
print "IO Error on file open"
|
576 |
|
|
sys.exit()
|
577 |
9 |
nuubik |
|
578 |
2 |
nuubik |
else:
|
579 |
|
|
print "Some of readback parameters missing..."
|
580 |
|
|
print mode.offset,mode.length, mode.filename
|
581 |
|
|
sys.exit()
|
582 |
8 |
nuubik |
|
583 |
|
|
if mode.t == 1: # perform dongle test
|
584 |
|
|
print "Dongle TEST"
|
585 |
|
|
if mode.e == 1:
|
586 |
|
|
#Erase Dongle
|
587 |
9 |
nuubik |
print "Erasing"
|
588 |
8 |
nuubik |
don.write_command(0x0060) # 0x0098
|
589 |
|
|
don.write_command(0x00D0) # 0x0098
|
590 |
|
|
don.wait_on_busy()
|
591 |
|
|
don.parse_status()
|
592 |
|
|
endBlock = 31
|
593 |
|
|
startBlock = 0
|
594 |
|
|
i=startBlock
|
595 |
|
|
while i <= endBlock:
|
596 |
9 |
nuubik |
if mode.v == 1:
|
597 |
|
|
print 'Erasing block %i '%(i)
|
598 |
|
|
else:
|
599 |
|
|
sys.stdout.write(".")
|
600 |
|
|
sys.stdout.flush()
|
601 |
8 |
nuubik |
don.erase_block(i)
|
602 |
|
|
don.wait_on_busy()
|
603 |
|
|
don.parse_status() #do this after programming all but uneaven ending
|
604 |
9 |
nuubik |
i=i+1
|
605 |
|
|
if mode.v == 0: # add CRTL return to dots
|
606 |
|
|
print ""
|
607 |
8 |
nuubik |
#Do marching one test on data and address
|
608 |
|
|
mode.length= 0 #make word length
|
609 |
|
|
try:
|
610 |
|
|
#Marching one test
|
611 |
|
|
#---------------------------------------------------------------------------
|
612 |
|
|
address = 0x100000 # set word address
|
613 |
|
|
data = 0x100000
|
614 |
|
|
while mode.length<20: # last address to test 0x20 0000
|
615 |
|
|
buf1=pack('BBBB', (0x000000FF&data),(0x0000FF00&data)>>8 ,(0x00FF0000&data)>>16 ,(0xFF0000&data)>>24 )
|
616 |
|
|
don.buffer_write(2,address,buf1)
|
617 |
|
|
don.parse_status() #do this after programming all but uneaven ending
|
618 |
|
|
don.write_command(0x00FF) # put flash to data read mode
|
619 |
|
|
buf2=don.read_data(2,address) # word count and byte address read 64 words to speed up
|
620 |
|
|
if buf1 != buf2:
|
621 |
|
|
print 'IN %02x %02x %02x %02x '%(ord(buf1[3]), ord(buf1[2]),ord(buf1[1]), ord(buf1[0]))
|
622 |
|
|
print 'OUT %02x %02x %02x %02x '%(ord(buf2[3]), ord(buf2[2]),ord(buf2[1]), ord(buf2[0]))
|
623 |
|
|
print "Test FAIL!!!!!"
|
624 |
|
|
sys.exit()
|
625 |
|
|
address = address >> 1
|
626 |
|
|
if address == 0x2:
|
627 |
|
|
address = address >> 1 # 0x2 is written and will return zero on read as write new write will fail
|
628 |
|
|
data = data >> 1
|
629 |
|
|
mode.length = mode.length + 1
|
630 |
|
|
buf2=don.read_data(1,0) #read first byte
|
631 |
|
|
if ord(buf2[0]) != 0xFF:
|
632 |
|
|
print "Test FAIL (At least one address line const. 0)!!!!!"
|
633 |
9 |
nuubik |
sys.exit()
|
634 |
8 |
nuubik |
#-----------------------------------------------------------------------
|
635 |
|
|
#Marching zero test
|
636 |
|
|
address = 0xFFEFFFFF # set word address
|
637 |
|
|
data = 0x100000
|
638 |
|
|
while mode.length<18: # last address to test 0x20 0000
|
639 |
|
|
buf1=pack('BBBB', (0x000000FF&data),(0x0000FF00&data)>>8 ,(0x00FF0000&data)>>16 ,(0xFF0000&data)>>24 )
|
640 |
|
|
don.buffer_write(2,address,buf1)
|
641 |
|
|
don.parse_status() #do this after programming all but uneaven ending
|
642 |
|
|
don.write_command(0x00FF) # put flash to data read mode
|
643 |
|
|
buf2=don.read_data(2,address&0x1FFFFF) # word count and byte address read 64 words to speed up
|
644 |
|
|
if buf1 != buf2:
|
645 |
|
|
print 'IN %02x %02x %02x %02x '%(ord(buf1[3]), ord(buf1[2]),ord(buf1[1]), ord(buf1[0]))
|
646 |
|
|
print 'OUT %02x %02x %02x %02x '%(ord(buf2[3]), ord(buf2[2]),ord(buf2[1]), ord(buf2[0]))
|
647 |
|
|
print "Test FAIL!!!!!"
|
648 |
|
|
sys.exit()
|
649 |
|
|
address = (address >> 1)|0xFF000000
|
650 |
|
|
data = data >> 1
|
651 |
|
|
mode.length = mode.length + 1
|
652 |
|
|
buf2=don.read_data(1,0x1FFFFF) #read first byte
|
653 |
|
|
if ord(buf2[0]) != 0xFF:
|
654 |
|
|
print "Test FAIL (At least two address lines bonded)!!!!!"
|
655 |
9 |
nuubik |
sys.exit()
|
656 |
8 |
nuubik |
if mode.b == 1:
|
657 |
|
|
#Erase Dongle
|
658 |
9 |
nuubik |
print "Erasing"
|
659 |
8 |
nuubik |
don.write_command(0x0060) # 0x0098
|
660 |
|
|
don.write_command(0x00D0) # 0x0098
|
661 |
|
|
don.wait_on_busy()
|
662 |
|
|
don.parse_status()
|
663 |
|
|
endBlock = 31
|
664 |
|
|
startBlock = 0
|
665 |
|
|
i=startBlock
|
666 |
|
|
while i <= endBlock:
|
667 |
9 |
nuubik |
if mode.v == 1:
|
668 |
|
|
print 'Blanking block %i '%(i)
|
669 |
|
|
else:
|
670 |
|
|
sys.stdout.write(".")
|
671 |
|
|
sys.stdout.flush()
|
672 |
8 |
nuubik |
don.erase_block(i)
|
673 |
|
|
don.wait_on_busy()
|
674 |
|
|
don.parse_status() #do this after programming all but uneaven ending
|
675 |
9 |
nuubik |
i=i+1
|
676 |
|
|
if mode.v == 0:
|
677 |
|
|
print " "
|
678 |
8 |
nuubik |
print "Test SUCCESSFUL!"
|
679 |
9 |
nuubik |
sys.exit()
|
680 |
8 |
nuubik |
except IOError:
|
681 |
|
|
print "IO Error on file open"
|
682 |
|
|
sys.exit()
|
683 |
|
|
|
684 |
9 |
nuubik |
if mode.e == 1: # perform dongle test
|
685 |
|
|
#Erase Dongle
|
686 |
|
|
print "Erasing all"
|
687 |
|
|
don.write_command(0x0060) # 0x0098
|
688 |
|
|
don.write_command(0x00D0) # 0x0098
|
689 |
|
|
don.wait_on_busy()
|
690 |
|
|
don.parse_status()
|
691 |
|
|
endBlock = 31
|
692 |
|
|
startBlock = 0
|
693 |
|
|
i=startBlock
|
694 |
|
|
while i <= endBlock:
|
695 |
|
|
if mode.v == 1:
|
696 |
|
|
print 'Erasing block %i '%(i)
|
697 |
|
|
else:
|
698 |
|
|
sys.stdout.write(".")
|
699 |
|
|
sys.stdout.flush()
|
700 |
|
|
don.erase_block(i)
|
701 |
|
|
don.wait_on_busy()
|
702 |
|
|
don.parse_status() #do this after programming all but uneaven ending
|
703 |
|
|
i=i+1
|
704 |
|
|
if mode.v == 0: # add CRTL return to dots
|
705 |
|
|
print ""
|
706 |
|
|
print "Erase done."
|
707 |
8 |
nuubik |
|
708 |
2 |
nuubik |
##########################################################
|