3 from SerialPort_linux import *
5 # serial device to communicate with
7 # baudrate used for communication with pkernel
17 def recvByteTO(b): #with timeout
20 while len(i)==0 and j < 400:
27 if(recvByte() != 0xaa):
28 raise Exception #ok, something really failed
34 time.sleep(0.005) # just to get sure, wait 5ms
40 sendByte((word >> 8) & 0xFF)
43 sendByte(dword & 0xFF)
44 sendByte((dword >> 8) & 0xFF)
45 sendByte((dword >> 16) & 0xFF)
46 sendByte((dword >> 24) & 0xFF)
50 if (recvByte() != 0x45):
53 if (recvByte() != 0x23):
55 print "Chip erasing done."
57 def pkernERASE(address, size):
59 if (recvByte() != 0x11):
63 if (recvByte() != 0x18):
68 def pkernWRITE(address, size, data):
69 print "address:", hex(address), "size:", size
74 # tell desired address and size
78 # write binary stream of data
79 for i in range(0, size):
83 class FlashSequence(object):
84 def __init__(self, address, data):
85 self.address = address
88 # list of all our address/data pairs to flash
92 print "Initializing serial port..."
93 tty = SerialPort(DEVICE, 0, KERNEL_BAUDRATE)
95 # check command line arguments
96 if len(sys.argv) != 2:
97 print "Usage: " + sys.argv[0] + " [mhx-file]"
100 # read in data from mhx-file before starting
102 fp = open(sys.argv[1], "r")
104 print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
110 # get rid of newline characters
113 # we're only interested in S2 (data sequence with 3 address bytes) records by now
114 if line[0:2] == "S2":
115 byte_count = int(line[2:4], 16)
116 # just to get sure, check if byte count field is valid
117 if (len(line)-4) != (byte_count*2):
118 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
119 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
122 # address and checksum bytes are not needed
124 address = int(line[4:10], 16)
125 datastr = line[10:10+byte_count*2]
127 # convert data hex-byte-string to real byte data list
129 for i in range(0, len(datastr)/2):
130 data.append(int(datastr[2*i:2*i+2], 16))
132 # add flash sequence to our list
133 flashseqs.append(FlashSequence(address, data))
135 print "The following flash sequences have been read in:"
136 for seq in flashseqs:
137 print hex(seq.address) + ":", [hex(x) for x in seq.data]
142 for seq in flashseqs:
143 print "Erasing", len(seq.data), "bytes at address", hex(seq.address)
144 pkernERASE(seq.address, len(seq.data))
150 for seq in flashseqs:
151 print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
152 pkernWRITE(seq.address, len(seq.data), seq.data)
154 #sendByte(0x99); #exit and wait
155 #print "Reset your board now to run code from Flash"
158 sendByte(0x97); #exit and restart