3 from SerialPort_linux import *
5 # serial device to communicate with
7 # baudrate used for communication with pkernel
18 time.sleep(0.501) # just to get sure, wait 1ms
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 pkernWRITE(address, size, data):
33 print "address:", hex(address), "size:", size
36 if (recvByte() != 0x37):
38 # tell desired address and size
41 if (recvByte() != 0x04):
43 print "Received Metadata."
45 # write binary stream of data
46 for i in range(0, size):
49 if (recvByte() != 0x08):
51 print "Received Data."
53 if (recvByte() != 0x18):
57 if (recvByte() != 0x28):
59 print "Flashing done."
62 class FlashSequence(object):
63 def __init__(self, address, data):
64 self.address = address
67 # list of all our address/data pairs to flash
71 print "Initializing serial port..."
72 tty = SerialPort(DEVICE, 0, KERNEL_BAUDRATE)
74 # check command line arguments
75 if len(sys.argv) != 2:
76 print "Usage: " + sys.argv[0] + " [mhx-file]"
79 # read in data from mhx-file before starting
81 fp = open(sys.argv[1], "r")
83 print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
89 # get rid of newline characters
92 # we're only interested in S2 (data sequence with 3 address bytes) records by now
94 byte_count = int(line[2:4], 16)
95 # just to get sure, check if byte count field is valid
96 if (len(line)-4) != (byte_count*2):
97 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
98 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
101 # address and checksum bytes are not needed
103 address = int(line[4:10], 16)
104 datastr = line[10:10+byte_count*2]
106 # convert data hex-byte-string to real byte data list
108 for i in range(0, len(datastr)/2):
109 data.append(int(datastr[2*i:2*i+2], 16))
111 # add flash sequence to our list
112 flashseqs.append(FlashSequence(address, data))
114 print "The following flash sequences have been read in:"
115 for seq in flashseqs:
116 print hex(seq.address) + ":", seq.data
120 for seq in flashseqs:
121 print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
122 pkernWRITE(seq.address, len(seq.data), seq.data)
126 print "Reset your board now to run code from Flash"