3 from SerialPort_linux import *
5 # serial device to communicate with
7 # baudrate used for initialization
9 # baudrate used for communication with the internal bootloader after init
10 BOOTLOADER_BAUDRATE=38400
11 # baudrate used for communication with the pkernel program that does the flashing eventually
12 KERNEL_BAUDRATE=115200
14 # contains the last received checksum from a READ, WRITE or CHECKSUM command
18 time.sleep(0.001) # just to get sure, wait 1ms
23 sendByte((word >> 8) & 0xFF)
26 sendByte(dword & 0xFF)
27 sendByte((dword >> 8) & 0xFF)
28 sendByte((dword >> 16) & 0xFF)
29 sendByte((dword >> 24) & 0xFF)
32 return ord(tty.read())
36 last_checksum = recvByte()
37 last_checksum |= (recvByte() << 8)
39 def cmdREAD(address, size):
42 if (recvByte() != 0xF1):
45 if (recvByte() != 0x82):
47 # tell desired address and size
50 # get binary stream of data
52 for i in range(0, size):
53 data.append(recvByte())
58 def cmdWRITE(address, size, data):
61 if (recvByte() != 0xF1):
64 if (recvByte() != 0x83):
66 # tell desired address and size
69 # write binary stream of data
70 for i in range(0, size):
75 # TODO: test this function!
79 if (recvByte() != 0xF1):
82 if (recvByte() != 0x84):
84 # tell desired address
86 # wait for return parameter - not needed here!
89 # TODO: test this function!
91 # call CHECKSUM command
93 if (recvByte() != 0xF1):
96 if (recvByte() != 0x84):
101 def cmdBAUDRATE(baudrate):
102 # send BAUDRATE command
104 if (recvByte() != 0xF1):
107 if (recvByte() != 0x86):
109 # send desired baudrate
112 def pkernCHIPERASE():
114 if (recvByte() != 0x45):
116 # wait till completion...
117 if (recvByte() != 0x23):
120 def pkernERASE(address, size):
122 if (recvByte() != 0x11):
126 if (recvByte() != 0x18):
130 def pkernWRITE(address, size, data):
133 if (recvByte() != 0x37):
135 # tell desired address and size
139 # write binary stream of data
140 for i in range(0, size):
143 if (recvByte() != 0x28):
147 class FlashSequence(object):
148 def __init__(self, address, data):
149 self.address = address
152 def readMHXFile(filename): # desired mhx filename
153 fp = open(filename, "r")
154 retval = [] # returns a list of FlashSequence objects
158 # get rid of newline characters
161 # we're only interested in S2 (data sequence with 3 address bytes) records by now
162 if line[0:2] == "S2":
163 byte_count = int(line[2:4], 16)
164 # just to get sure, check if byte count field is valid
165 if (len(line)-4) != (byte_count*2):
166 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
167 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
170 # address and checksum bytes are not needed
172 address = int(line[4:10], 16)
173 datastr = line[10:10+byte_count*2]
175 # convert data hex-byte-string to real byte data list
177 for i in range(0, len(datastr)/2):
178 data.append(int(datastr[2*i:2*i+2], 16))
180 # add flash sequence to our list
181 retval.append(FlashSequence(address, data))
186 # check command line arguments
187 if len(sys.argv) != 3:
188 print "Usage: " + sys.argv[0] + " [pkernel mhx-file] [target mhx-file]"
191 # read in data from mhx-files before starting
193 bootloaderseqs = readMHXFile(sys.argv[1])
194 pkernelseqs = readMHXFile(sys.argv[2])
195 except IOError as error:
196 print sys.argv[0] + ": Error - couldn't open file " + error.filename + "!"
199 print "Initializing serial port..."
200 tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
202 print "Please press RESET on your board..."
208 if tty.read() == 'F':
210 except SerialPortException:
211 # timeout happened, who cares ;-)
214 print "OK, trying to set baudrate..."
216 cmdBAUDRATE(BOOTLOADER_BAUDRATE)
217 tty = SerialPort(DEVICE, 100, BOOTLOADER_BAUDRATE)
219 print "Transfering pkernel program to IRAM",
221 for seq in bootloaderseqs:
222 if(seq.address <= 0x40000):
226 #print "RAMing", len(seq.data), "bytes at address", hex(addr)
227 cmdWRITE(addr, len(seq.data), seq.data)
229 sys.stdout.write(".")
233 # execute our pkernel finally and set pkernel conform baudrate
235 time.sleep(0.5) # just to get sure that the pkernel is really running!
237 tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
239 print "Performing ChipErase..."
241 print "Chip erasing done."
244 for seq in pkernelseqs:
245 # skip seqs only consisting of 0xffs
246 seqset = list(set(seq.data))
247 if len(seqset) == 1 and seqset[0] == 0xff:
249 #print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
250 pkernWRITE(seq.address, len(seq.data), seq.data)
252 sys.stdout.write(".")
255 print "Flashing done."
257 sendByte(0x97) # exit and restart
258 print "Program was started. Have fun!"
260 # some tests here.......
262 # execute (existing) program in ram
267 # read something from the IRAM
268 #print cmdREAD(0x00030000, 32)
271 #for i in range(0, 32):
273 #cmdWRITE(0x00030000, 32, data)
277 # write something to the begin of the IRAM
280 for i in range(0, 0x400):
282 data_wr.append(value)
283 checksum = (checksum + value) % (2**16)
285 print "Calculated checksum:", checksum
286 print "Writing", data_wr, "to the IRAM..."
287 cmdWRITE(0x00030000, len(data_wr), data_wr)
288 print "Received Checksum:", last_checksum
291 print "Reading from the IRAM again..."
292 data_re = cmdREAD(0x00030000, len(data_wr))
293 print "Received data:", data_re, "Checksum:", last_checksum
297 # see whats in the iram at the moment
299 print "Reading from the IRAM..."
300 data_re = cmdREAD(0x00030000, 0x10000-4)
301 print "Received data:", data_re, "Checksum:", last_checksum
305 # see whats in the dram at the moment
307 print "Reading from the DRAM..."
308 data_re = cmdREAD(0x0002C000, 0x10000-0xC000-4)
309 print "Received data:", data_re, "Checksum:", last_checksum
315 for i in range(0, 0x10000-4):
317 data_wr.append(value)
319 print "Writing", data_wr, "to the IRAM..."
320 cmdWRITE(0x00030000, len(data_wr), data_wr)
321 print "Received Checksum:", last_checksum
328 for i in range(0, 0x10000-0xC000-4):
330 data_wr.append(value)
332 print "Writing", data_wr, "to the DRAM..."
333 cmdWRITE(0x0002C000, len(data_wr), data_wr)
334 print "Received Checksum:", last_checksum
339 # write some data in the iram and try to execute it
350 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0, #nop
351 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
352 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
353 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
354 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
355 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
356 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0]
357 print "Writing", data_wr, "to the IRAM..."
358 cmdWRITE(0x00030000, len(data_wr), data_wr)
359 print "Received Checksum:", last_checksum