+ sendDWord(baudrate)
+
+def pkernCHIPERASE():
+ sendByte(0x15)
+ if (recvByte() != 0x45):
+ raise Exception
+ # wait till completion...
+ if (recvByte() != 0x23):
+ raise Exception
+
+def pkernERASE(address, size):
+ sendByte(0x12)
+ if (recvByte() != 0x11):
+ raise Exception
+ sendDWord(address)
+ sendWord(size)
+ if (recvByte() != 0x18):
+ raise Exception
+
+
+def pkernWRITE(address, size, data):
+ # send WRITE command
+ sendByte(0x13)
+ if (recvByte() != 0x37):
+ raise Exception
+ # tell desired address and size
+ sendDWord(address)
+ sendWord(size)
+
+ # write binary stream of data
+ for i in range(0, size):
+ sendByte(data[i])
+
+ if (recvByte() != 0x28):
+ raise Exception
+
+
+class FlashSequence(object):
+ def __init__(self, address, data):
+ self.address = address
+ self.data = data
+
+def readMHXFile(filename): # desired mhx filename
+ fp = open(filename, "r")
+ retval = [] # returns a list of FlashSequence objects
+ linecount = 0
+ for line in fp:
+ linecount += 1
+ # get rid of newline characters
+ line = line.strip()
+
+ # we're only interested in S2 (data sequence with 3 address bytes) records by now
+ if line[0:2] == "S2":
+ byte_count = int(line[2:4], 16)
+ # just to get sure, check if byte count field is valid
+ if (len(line)-4) != (byte_count*2):
+ print sys.argv[0] + ": Warning - inavlid byte count field in " + \
+ sys.argv[1] + ":" + str(linecount) + ", skipping line!"
+ continue
+
+ # address and checksum bytes are not needed
+ byte_count -= 4
+ address = int(line[4:10], 16)
+ datastr = line[10:10+byte_count*2]
+
+ # convert data hex-byte-string to real byte data list
+ data = []
+ for i in range(0, len(datastr)/2):
+ data.append(int(datastr[2*i:2*i+2], 16))
+
+ # add flash sequence to our list
+ retval.append(FlashSequence(address, data))
+ fp.close()
+ return retval
+
+
+# check command line arguments
+if len(sys.argv) != 3:
+ print "Usage: " + sys.argv[0] + " [pkernel mhx-file] [target mhx-file]"
+ sys.exit(1)
+
+# read in data from mhx-files before starting
+try:
+ bootloaderseqs = readMHXFile(sys.argv[1])
+ pkernelseqs = readMHXFile(sys.argv[2])
+except IOError as error:
+ print sys.argv[0] + ": Error - couldn't open file " + error.filename + "!"
+ sys.exit(1)