3 from SerialPort_linux import *
5 # serial device to communicate with
7 # baudrate used for communication with pkernel
15 time.sleep(0.005) # just to get sure, wait 5ms
21 sendByte((word >> 8) & 0xFF)
24 sendByte(dword & 0xFF)
25 sendByte((dword >> 8) & 0xFF)
26 sendByte((dword >> 16) & 0xFF)
27 sendByte((dword >> 24) & 0xFF)
31 if (recvByte() != 0x45):
34 if (recvByte() != 0x23):
36 print "Chip erasing done."
38 def pkernERASE(address, size):
40 if (recvByte() != 0x11):
44 if (recvByte() != 0x18):
49 def pkernWRITE(address, size, data):
50 print "address:", hex(address), "size:", size
53 if (recvByte() != 0x37):
55 # tell desired address and size
59 if (recvByte() != 0x04):
61 print "Received Metadata."
63 # write binary stream of data
64 for i in range(0, size):
67 if (recvByte() != 0x08):
69 print "Received Data."
71 if (recvByte() != 0x28):
73 print "Flashing done."
76 class FlashSequence(object):
77 def __init__(self, address, data):
78 self.address = address
81 # list of all our address/data pairs to flash
85 print "Initializing serial port..."
86 tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
88 # check command line arguments
89 if len(sys.argv) != 2:
90 print "Usage: " + sys.argv[0] + " [mhx-file]"
93 # read in data from mhx-file before starting
95 fp = open(sys.argv[1], "r")
97 print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
103 # get rid of newline characters
106 # we're only interested in S2 (data sequence with 3 address bytes) records by now
107 if line[0:2] == "S2":
108 byte_count = int(line[2:4], 16)
109 # just to get sure, check if byte count field is valid
110 if (len(line)-4) != (byte_count*2):
111 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
112 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
115 # address and checksum bytes are not needed
117 address = int(line[4:10], 16)
118 datastr = line[10:10+byte_count*2]
120 # convert data hex-byte-string to real byte data list
122 for i in range(0, len(datastr)/2):
123 data.append(int(datastr[2*i:2*i+2], 16))
125 # add flash sequence to our list
126 flashseqs.append(FlashSequence(address, data))
128 print "The following flash sequences have been read in:"
129 for seq in flashseqs:
130 print hex(seq.address) + ":", [hex(x) for x in seq.data]
135 for seq in flashseqs:
136 print "Erasing", len(seq.data), "bytes at address", hex(seq.address)
137 pkernERASE(seq.address, len(seq.data))
143 for seq in flashseqs:
144 print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
145 pkernWRITE(seq.address, len(seq.data), seq.data)
149 print "Reset your board now to run code from Flash"