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
22 sendByte((word >> 8) & 0xFF)
25 sendByte(dword & 0xFF)
26 sendByte((dword >> 8) & 0xFF)
27 sendByte((dword >> 16) & 0xFF)
28 sendByte((dword >> 24) & 0xFF)
31 return ord(tty.read())
35 last_checksum = recvByte()
36 last_checksum |= (recvByte() << 8)
38 def bootromREAD(address, size):
41 if (recvByte() != 0xF1):
44 if (recvByte() != 0x82):
46 # tell desired address and size
49 # get binary stream of data
51 for i in range(0, size):
52 data.append(recvByte())
57 def bootromWRITE(address, size, data):
60 if (recvByte() != 0xF1):
63 if (recvByte() != 0x83):
65 # tell desired address and size
68 # write binary stream of data
69 for i in range(0, size):
74 # TODO: test this function!
75 def bootromCALL(address):
78 if (recvByte() != 0xF1):
81 if (recvByte() != 0x84):
83 # tell desired address
85 # wait for return parameter - not needed here!
88 # TODO: test this function!
89 def bootromCHECKSUM():
90 # call CHECKSUM command
92 if (recvByte() != 0xF1):
95 if (recvByte() != 0x84):
100 def bootromBAUDRATE(baudrate):
101 # send BAUDRATE command
103 if (recvByte() != 0xF1):
106 if (recvByte() != 0x86):
108 # send desired baudrate
111 def pkernCHIPERASE():
113 if (recvByte() != 0x45):
115 # wait till completion...
116 if (recvByte() != 0x23):
119 def pkernERASE(address, size):
121 if (recvByte() != 0x11):
125 if (recvByte() != 0x18):
129 def pkernWRITE(address, size, data):
132 if (recvByte() != 0x37):
134 # tell desired address and size
138 # write binary stream of data
139 for i in range(0, size):
142 if (recvByte() != 0x28):
146 class FlashSequence(object):
147 def __init__(self, address, data):
148 self.address = address
151 def readMHXFile(filename): # desired mhx filename
152 fp = open(filename, "r")
153 retval = [] # returns a list of FlashSequence objects
157 # get rid of newline characters
160 # we're only interested in S2 (data sequence with 3 address bytes) records by now
161 if line[0:2] == "S2":
162 byte_count = int(line[2:4], 16)
163 # just to get sure, check if byte count field is valid
164 if (len(line)-4) != (byte_count*2):
165 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
166 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
169 # address and checksum bytes are not needed
171 address = int(line[4:10], 16)
172 datastr = line[10:10+byte_count*2]
174 # convert data hex-byte-string to real byte data list
176 for i in range(0, len(datastr)/2):
177 data.append(int(datastr[2*i:2*i+2], 16))
179 # add flash sequence to our list
180 retval.append(FlashSequence(address, data))
185 # check command line arguments
186 if len(sys.argv) != 3:
187 print "Usage: " + sys.argv[0] + " [pkernel mhx-file] [target mhx-file]"
190 # read in data from mhx-files before starting
192 bootloaderseqs = readMHXFile(sys.argv[1])
193 pkernelseqs = readMHXFile(sys.argv[2])
194 except IOError as error:
195 print sys.argv[0] + ": Error - couldn't open file " + error.filename + "!"
198 print "Initializing serial port..."
199 tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
201 print "Please press RESET on your board..."
207 if tty.read() == 'F':
209 except SerialPortException:
210 # timeout happened, who cares ;-)
213 starttime = time.time() # save time at this point for evaluating the duration at the end
215 print "OK, trying to set baudrate..."
217 bootromBAUDRATE(BOOTLOADER_BAUDRATE)
218 time.sleep(0.1) # just to get sure that the bootloader is really running in new baudrate mode!
220 tty = SerialPort(DEVICE, 100, BOOTLOADER_BAUDRATE)
222 print "Transfering pkernel program to IRAM",
224 for seq in bootloaderseqs:
225 if(seq.address <= 0x40000):
229 #print "RAMing", len(seq.data), "bytes at address", hex(addr)
230 bootromWRITE(addr, len(seq.data), seq.data)
232 sys.stdout.write(".")
236 # execute our pkernel finally and set pkernel conform baudrate
238 time.sleep(0.1) # just to get sure that the pkernel is really running!
240 tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
242 print "Performing ChipErase..."
246 for seq in pkernelseqs:
247 # skip seqs only consisting of 0xffs
248 seqset = list(set(seq.data))
249 if len(seqset) == 1 and seqset[0] == 0xff:
251 #print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
252 pkernWRITE(seq.address, len(seq.data), seq.data)
254 sys.stdout.write(".")
258 duration = time.time() - starttime
259 print "Procedure complete, took", round(duration, 2), "seconds."
261 sendByte(0x97) # exit and restart
262 print "Program was started. Have fun!"