3 from SerialPort_linux import *
5 # baudrate used for initialization
7 # baudrate used for communication with the internal bootloader after init
8 BOOTLOADER_BAUDRATE=38400
9 # baudrate used for communication with the pkernel program that does the flashing eventually
10 KERNEL_BAUDRATE=115200
12 # contains the last received checksum from a READ, WRITE or CHECKSUM command
15 class FlashSequence(object):
16 def __init__(self, address, data):
17 self.address = address
25 sendByte((word >> 8) & 0xFF)
28 sendByte(dword & 0xFF)
29 sendByte((dword >> 8) & 0xFF)
30 sendByte((dword >> 16) & 0xFF)
31 sendByte((dword >> 24) & 0xFF)
34 return ord(tty.read())
38 last_checksum = recvByte()
39 last_checksum |= (recvByte() << 8)
41 def bootromREAD(address, size):
44 if (recvByte() != 0xF1):
47 if (recvByte() != 0x82):
49 # tell desired address and size
52 # get binary stream of data
54 for i in range(0, size):
55 data.append(recvByte())
60 def bootromWRITE(address, size, data):
63 if (recvByte() != 0xF1):
66 if (recvByte() != 0x83):
68 # tell desired address and size
71 # write binary stream of data
72 for i in range(0, size):
77 # TODO: test this function!
78 def bootromCALL(address):
81 if (recvByte() != 0xF1):
84 if (recvByte() != 0x84):
86 # tell desired address
88 # wait for return parameter - not needed here!
91 # TODO: test this function!
92 def bootromCHECKSUM():
93 # call CHECKSUM command
95 if (recvByte() != 0xF1):
98 if (recvByte() != 0x84):
103 def bootromBAUDRATE(baudrate):
104 # send BAUDRATE command
106 if (recvByte() != 0xF1):
109 if (recvByte() != 0x86):
111 # send desired baudrate
114 def pkernCHIPERASE():
116 if (recvByte() != 0x45):
118 # wait till completion...
119 if (recvByte() != 0x23):
122 def pkernERASE(address, size):
124 if (recvByte() != 0x11):
128 if (recvByte() != 0x18):
131 def pkernWRITE(address, size, data):
134 if (recvByte() != 0x37):
136 # tell desired address and size
140 # write binary stream of data
141 for i in range(0, size):
144 if (recvByte() != 0x28):
147 def readMHXFile(filename): # desired mhx filename
148 fp = open(filename, "r")
149 retval = [] # returns a list of FlashSequence objects
153 # get rid of newline characters
156 # we're only interested in S2 (data sequence with 3 address bytes) records by now
157 if line[0:2] == "S2":
158 byte_count = int(line[2:4], 16)
159 # just to get sure, check if byte count field is valid
160 if (len(line)-4) != (byte_count*2):
161 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
162 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
165 # address and checksum bytes are not needed
167 address = int(line[4:10], 16)
168 datastr = line[10:10+byte_count*2]
170 # convert data hex-byte-string to real byte data list
172 for i in range(0, len(datastr)/2):
173 data.append(int(datastr[2*i:2*i+2], 16))
175 # add flash sequence to our list
176 retval.append(FlashSequence(address, data))
181 # check command line arguments
184 if len(argv) != 2 and len(argv) != 4:
185 print "Usage: " + argv[0] + " <target mhx-file> [-d DEVICE]"
188 # standard serial device to communicate with
189 DEVICE="/dev/ttyUSB0"
193 # read in data from mhx-files before starting
196 bootloaderseqs = readMHXFile("pkernel/pkernel.mhx")
197 except IOError as error1:
198 bootloaderseqs = readMHXFile("%PREFIX%/share/frprog/pkernel.mhx")
199 pkernelseqs = readMHXFile(argv[1])
200 except IOError as error:
201 print argv[0] + ": Error - couldn't open file " + error.filename + "!"
204 print "Initializing serial port..."
206 tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
208 print "Please press RESET on your board..."
214 if tty.read() == 'F':
216 except SerialPortException:
217 # timeout happened, who cares ;-)
220 starttime = time.time() # save time at this point for evaluating the duration at the end
222 print "OK, trying to set baudrate..."
224 bootromBAUDRATE(BOOTLOADER_BAUDRATE)
225 time.sleep(0.1) # just to get sure that the bootloader is really running in new baudrate mode!
227 tty = SerialPort(DEVICE, 100, BOOTLOADER_BAUDRATE)
229 print "Transfering pkernel program to IRAM",
231 for seq in bootloaderseqs:
232 if(seq.address <= 0x40000):
236 #print "RAMing", len(seq.data), "bytes at address", hex(addr)
237 bootromWRITE(addr, len(seq.data), seq.data)
239 sys.stdout.write(".")
243 # execute our pkernel finally and set pkernel conform baudrate
245 time.sleep(0.1) # just to get sure that the pkernel is really running!
247 tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
249 print "Performing ChipErase..."
253 for seq in pkernelseqs:
254 # skip seqs only consisting of 0xffs
255 seqset = list(set(seq.data))
256 if len(seqset) == 1 and seqset[0] == 0xff:
258 #print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
259 pkernWRITE(seq.address, len(seq.data), seq.data)
261 sys.stdout.write(".")
265 duration = time.time() - starttime
266 print "Procedure complete, took", round(duration, 2), "seconds."
268 sendByte(0x97) # exit and restart
269 print "Program was started. Have fun!"
272 if __name__ == '__main__':