3 from SerialPort_linux import *
5 # serial device to communicate with
7 # baudrate used for communication with pkernel
18 time.sleep(0.005) # just to get sure, wait 5ms
24 sendByte((word >> 8) & 0xFF)
27 sendByte(dword & 0xFF)
28 sendByte((dword >> 8) & 0xFF)
29 sendByte((dword >> 16) & 0xFF)
30 sendByte((dword >> 24) & 0xFF)
34 if (recvByte() != 0x45):
37 if (recvByte() != 0x23):
39 print "Chip erasing done."
41 def pkernERASE(address, size):
43 if (recvByte() != 0x11):
47 if (recvByte() != 0x18):
52 def pkernWRITE(address, size, data):
53 print "address:", hex(address), "size:", size
56 if (recvByte() != 0x37):
58 # tell desired address and size
62 if (recvByte() != 0x04):
64 print "Received Metadata."
66 # write binary stream of data
67 for i in range(0, size):
70 if (recvByte() != 0x08):
72 print "Received Data."
74 if (recvByte() != 0x28):
76 print "Flashing done."
79 class FlashSequence(object):
80 def __init__(self, address, data):
81 self.address = address
84 # list of all our address/data pairs to flash
88 print "Initializing serial port..."
89 tty = SerialPort(DEVICE, 0, KERNEL_BAUDRATE)
91 # check command line arguments
92 if len(sys.argv) != 2:
93 print "Usage: " + sys.argv[0] + " [mhx-file]"
96 # read in data from mhx-file before starting
98 fp = open(sys.argv[1], "r")
100 print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
106 # get rid of newline characters
109 # we're only interested in S2 (data sequence with 3 address bytes) records by now
110 if line[0:2] == "S2":
111 byte_count = int(line[2:4], 16)
112 # just to get sure, check if byte count field is valid
113 if (len(line)-4) != (byte_count*2):
114 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
115 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
118 # address and checksum bytes are not needed
120 address = int(line[4:10], 16)
121 datastr = line[10:10+byte_count*2]
123 # convert data hex-byte-string to real byte data list
125 for i in range(0, len(datastr)/2):
126 data.append(int(datastr[2*i:2*i+2], 16))
128 # add flash sequence to our list
129 flashseqs.append(FlashSequence(address, data))
131 print "The following flash sequences have been read in:"
132 for seq in flashseqs:
133 print hex(seq.address) + ":", [hex(x) for x in seq.data]
138 for seq in flashseqs:
139 print "Erasing", len(seq.data), "bytes at address", hex(seq.address)
140 pkernERASE(seq.address, len(seq.data))
146 for seq in flashseqs:
147 print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
148 pkernWRITE(seq.address, len(seq.data), seq.data)
152 print "Reset your board now to run code from Flash"