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
17 class FlashSequence(object):
18 def __init__(self, address, data):
19 self.address = address
27 sendByte((word >> 8) & 0xFF)
30 sendByte(dword & 0xFF)
31 sendByte((dword >> 8) & 0xFF)
32 sendByte((dword >> 16) & 0xFF)
33 sendByte((dword >> 24) & 0xFF)
36 return ord(tty.read())
40 last_checksum = recvByte()
41 last_checksum |= (recvByte() << 8)
43 def bootromREAD(address, size):
46 if (recvByte() != 0xF1):
49 if (recvByte() != 0x82):
51 # tell desired address and size
54 # get binary stream of data
56 for i in range(0, size):
57 data.append(recvByte())
62 def bootromWRITE(address, size, data):
65 if (recvByte() != 0xF1):
68 if (recvByte() != 0x83):
70 # tell desired address and size
73 # write binary stream of data
74 for i in range(0, size):
79 # TODO: test this function!
80 def bootromCALL(address):
83 if (recvByte() != 0xF1):
86 if (recvByte() != 0x84):
88 # tell desired address
90 # wait for return parameter - not needed here!
93 # TODO: test this function!
94 def bootromCHECKSUM():
95 # call CHECKSUM command
97 if (recvByte() != 0xF1):
100 if (recvByte() != 0x84):
105 def bootromBAUDRATE(baudrate):
106 # send BAUDRATE command
108 if (recvByte() != 0xF1):
111 if (recvByte() != 0x86):
113 # send desired baudrate
116 def pkernCHIPERASE():
118 if (recvByte() != 0x45):
120 # wait till completion...
121 if (recvByte() != 0x23):
124 def pkernERASE(address, size):
126 if (recvByte() != 0x11):
130 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):
149 def readMHXFile(filename): # desired mhx filename
150 fp = open(filename, "r")
151 retval = [] # returns a list of FlashSequence objects
155 # get rid of newline characters
158 # we're only interested in S2 (data sequence with 3 address bytes) records by now
159 if line[0:2] == "S2":
160 byte_count = int(line[2:4], 16)
161 # just to get sure, check if byte count field is valid
162 if (len(line)-4) != (byte_count*2):
163 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
164 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
167 # address and checksum bytes are not needed
169 address = int(line[4:10], 16)
170 datastr = line[10:10+byte_count*2]
172 # convert data hex-byte-string to real byte data list
174 for i in range(0, len(datastr)/2):
175 data.append(int(datastr[2*i:2*i+2], 16))
177 # add flash sequence to our list
178 retval.append(FlashSequence(address, data))
183 # check command line arguments
187 print "Usage: " + argv[0] + " [target mhx-file]"
190 # read in data from mhx-files before starting
193 bootloaderseqs = readMHXFile("pkernel/pkernel.mhx")
194 except IOError as error1:
195 bootloaderseqs = readMHXFile("%PREFIX%/share/frprog/pkernel.mhx")
196 pkernelseqs = readMHXFile(argv[1])
197 except IOError as error:
198 print argv[0] + ": Error - couldn't open file " + error.filename + "!"
201 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 starttime = time.time() # save time at this point for evaluating the duration at the end
219 print "OK, trying to set baudrate..."
221 bootromBAUDRATE(BOOTLOADER_BAUDRATE)
222 time.sleep(0.1) # just to get sure that the bootloader is really running in new baudrate mode!
224 tty = SerialPort(DEVICE, 100, BOOTLOADER_BAUDRATE)
226 print "Transfering pkernel program to IRAM",
228 for seq in bootloaderseqs:
229 if(seq.address <= 0x40000):
233 #print "RAMing", len(seq.data), "bytes at address", hex(addr)
234 bootromWRITE(addr, len(seq.data), seq.data)
236 sys.stdout.write(".")
240 # execute our pkernel finally and set pkernel conform baudrate
242 time.sleep(0.1) # just to get sure that the pkernel is really running!
244 tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
246 print "Performing ChipErase..."
250 for seq in pkernelseqs:
251 # skip seqs only consisting of 0xffs
252 seqset = list(set(seq.data))
253 if len(seqset) == 1 and seqset[0] == 0xff:
255 #print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
256 pkernWRITE(seq.address, len(seq.data), seq.data)
258 sys.stdout.write(".")
262 duration = time.time() - starttime
263 print "Procedure complete, took", round(duration, 2), "seconds."
265 sendByte(0x97) # exit and restart
266 print "Program was started. Have fun!"
269 if __name__ == '__main__':