3 from SerialPort_linux import *
5 # serial device to communicate with
7 # baudrate used for communication with pkernel
20 sendByte((word >> 8) & 0xFF)
23 sendByte(dword & 0xFF)
24 sendByte((dword >> 8) & 0xFF)
25 sendByte((dword >> 16) & 0xFF)
26 sendByte((dword >> 24) & 0xFF)
30 if (recvByte() != 0x45):
33 if (recvByte() != 0x23):
36 def pkernERASE(address, size):
38 if (recvByte() != 0x11):
42 if (recvByte() != 0x18):
44 #print "Erasing done."
47 def pkernWRITE(address, size, data):
50 if (recvByte() != 0x37):
52 # tell desired address and size
56 # write binary stream of data
57 for i in range(0, size):
60 if (recvByte() != 0x28):
62 #print "Flashing done."
65 class FlashSequence(object):
66 def __init__(self, address, data):
67 self.address = address
70 # list of all our address/data pairs to flash
74 print "Initializing serial port..."
75 tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
77 # check command line arguments
78 if len(sys.argv) != 2:
79 print "Usage: " + sys.argv[0] + " [mhx-file]"
82 # read in data from mhx-file before starting
84 fp = open(sys.argv[1], "r")
86 print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
92 # get rid of newline characters
95 # we're only interested in S2 (data sequence with 3 address bytes) records by now
97 byte_count = int(line[2:4], 16)
98 # just to get sure, check if byte count field is valid
99 if (len(line)-4) != (byte_count*2):
100 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
101 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
104 # address and checksum bytes are not needed
106 address = int(line[4:10], 16)
107 datastr = line[10:10+byte_count*2]
109 # convert data hex-byte-string to real byte data list
111 for i in range(0, len(datastr)/2):
112 data.append(int(datastr[2*i:2*i+2], 16))
114 # add flash sequence to our list
115 flashseqs.append(FlashSequence(address, data))
117 #print "The following flash sequences have been read in:"
118 #for seq in flashseqs:
119 # print hex(seq.address) + ":", [hex(x) for x in seq.data]
124 for seq in flashseqs:
125 print "Erasing", len(seq.data), "bytes at address", hex(seq.address)
126 pkernERASE(seq.address, len(seq.data))
130 print "Chip erasing done."
134 for seq in flashseqs:
135 sys.stdout.write(".")
137 # skip seqs only consisting of 0xffs
138 seqset = list(set(seq.data))
139 if len(seqset) == 1 and seqset[0] == 0xff:
141 #print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
142 pkernWRITE(seq.address, len(seq.data), seq.data)
146 sendByte(0x99) #exit and wait
147 print "Reset your board now to run code from Flash"
150 sendByte(0x97) #exit and restart