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
16 # is set to True if we are already communicating with the pkernel
20 if pkernelmode == False:
21 time.sleep(0.001) # just to get sure, wait 1ms
26 sendByte((word >> 8) & 0xFF)
29 sendByte(dword & 0xFF)
30 sendByte((dword >> 8) & 0xFF)
31 sendByte((dword >> 16) & 0xFF)
32 sendByte((dword >> 24) & 0xFF)
35 return ord(tty.read())
39 last_checksum = recvByte()
40 last_checksum |= (recvByte() << 8)
42 def cmdREAD(address, size):
45 if (recvByte() != 0xF1):
48 if (recvByte() != 0x82):
50 # tell desired address and size
53 # get binary stream of data
55 for i in range(0, size):
56 data.append(recvByte())
61 def cmdWRITE(address, size, data):
64 if (recvByte() != 0xF1):
67 if (recvByte() != 0x83):
69 # tell desired address and size
72 # write binary stream of data
73 for i in range(0, size):
78 # TODO: test this function!
82 if (recvByte() != 0xF1):
85 if (recvByte() != 0x84):
87 # tell desired address
89 # wait for return parameter - not needed here!
92 # TODO: test this function!
94 # call CHECKSUM command
96 if (recvByte() != 0xF1):
99 if (recvByte() != 0x84):
104 def cmdBAUDRATE(baudrate):
105 # send BAUDRATE command
107 if (recvByte() != 0xF1):
110 if (recvByte() != 0x86):
112 # send desired baudrate
115 def pkernCHIPERASE():
117 if (recvByte() != 0x45):
119 # wait till completion...
120 if (recvByte() != 0x23):
123 def pkernERASE(address, size):
125 if (recvByte() != 0x11):
129 if (recvByte() != 0x18):
133 def pkernWRITE(address, size, data):
136 if (recvByte() != 0x37):
138 # tell desired address and size
142 # write binary stream of data
143 for i in range(0, size):
146 if (recvByte() != 0x28):
150 class FlashSequence(object):
151 def __init__(self, address, data):
152 self.address = address
155 def readMHXFile(filename): # desired mhx filename
156 fp = open(filename, "r")
157 retval = [] # returns a list of FlashSequence objects
161 # get rid of newline characters
164 # we're only interested in S2 (data sequence with 3 address bytes) records by now
165 if line[0:2] == "S2":
166 byte_count = int(line[2:4], 16)
167 # just to get sure, check if byte count field is valid
168 if (len(line)-4) != (byte_count*2):
169 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
170 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
173 # address and checksum bytes are not needed
175 address = int(line[4:10], 16)
176 datastr = line[10:10+byte_count*2]
178 # convert data hex-byte-string to real byte data list
180 for i in range(0, len(datastr)/2):
181 data.append(int(datastr[2*i:2*i+2], 16))
183 # add flash sequence to our list
184 retval.append(FlashSequence(address, data))
189 # check command line arguments
190 if len(sys.argv) != 3:
191 print "Usage: " + sys.argv[0] + " [pkernel mhx-file] [target mhx-file]"
194 # read in data from mhx-files before starting
196 bootloaderseqs = readMHXFile(sys.argv[1])
197 pkernelseqs = readMHXFile(sys.argv[2])
198 except IOError as error:
199 print sys.argv[0] + ": Error - couldn't open file " + error.filename + "!"
202 print "Initializing serial port..."
203 tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
205 print "Please press RESET on your board..."
211 if tty.read() == 'F':
213 except SerialPortException:
214 # timeout happened, who cares ;-)
217 print "OK, trying to set baudrate..."
219 cmdBAUDRATE(BOOTLOADER_BAUDRATE)
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 cmdWRITE(addr, len(seq.data), seq.data)
232 sys.stdout.write(".")
236 # execute our pkernel finally and set pkernel conform baudrate
238 time.sleep(0.5) # just to get sure that the pkernel is really running!
241 tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
243 print "Performing ChipErase..."
245 print "Chip erasing done."
248 for seq in pkernelseqs:
249 # skip seqs only consisting of 0xffs
250 seqset = list(set(seq.data))
251 if len(seqset) == 1 and seqset[0] == 0xff:
253 #print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
254 pkernWRITE(seq.address, len(seq.data), seq.data)
256 sys.stdout.write(".")
259 print "Flashing done."
261 sendByte(0x97) # exit and restart
262 print "Program was started. Have fun!"