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)
32 def pkernERASE(address, size):
34 if (recvByte() != 0x11):
38 if (recvByte() != 0x18):
43 def pkernWRITE(address, size, data):
44 print "address:", hex(address), "size:", size
47 if (recvByte() != 0x37):
49 # tell desired address and size
53 if (recvByte() != 0x04):
55 print "Received Metadata."
57 # write binary stream of data
58 for i in range(0, size):
61 if (recvByte() != 0x08):
63 print "Received Data."
65 if (recvByte() != 0x28):
67 print "Flashing done."
70 class FlashSequence(object):
71 def __init__(self, address, data):
72 self.address = address
75 # list of all our address/data pairs to flash
79 print "Initializing serial port..."
80 tty = SerialPort(DEVICE, 0, KERNEL_BAUDRATE)
82 # check command line arguments
83 if len(sys.argv) != 2:
84 print "Usage: " + sys.argv[0] + " [mhx-file]"
87 # read in data from mhx-file before starting
89 fp = open(sys.argv[1], "r")
91 print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
97 # get rid of newline characters
100 # we're only interested in S2 (data sequence with 3 address bytes) records by now
101 if line[0:2] == "S2":
102 byte_count = int(line[2:4], 16)
103 # just to get sure, check if byte count field is valid
104 if (len(line)-4) != (byte_count*2):
105 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
106 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
109 # address and checksum bytes are not needed
111 address = int(line[4:10], 16)
112 datastr = line[10:10+byte_count*2]
114 # convert data hex-byte-string to real byte data list
116 for i in range(0, len(datastr)/2):
117 data.append(int(datastr[2*i:2*i+2], 16))
119 # add flash sequence to our list
120 flashseqs.append(FlashSequence(address, data))
122 print "The following flash sequences have been read in:"
123 for seq in flashseqs:
124 print hex(seq.address) + ":", [hex(x) for x in seq.data]
128 for seq in flashseqs:
129 print "Erasing", len(seq.data), "bytes at address", hex(seq.address)
130 pkernERASE(seq.address, len(seq.data))
132 for seq in flashseqs:
133 print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
134 pkernWRITE(seq.address, len(seq.data), seq.data)
138 print "Reset your board now to run code from Flash"