3 from SerialPort_linux import *
5 # serial device to communicate with
7 # baudrate used for initialization
9 # baudrate used for communication after init
12 # contains the last received checksum from a READ, WRITE or CHECKSUM command
16 time.sleep(0.001) # just to get sure, wait 1ms
22 sendByte((word >> 8) & 0xFF)
25 sendByte(dword & 0xFF)
26 sendByte((dword >> 8) & 0xFF)
27 sendByte((dword >> 16) & 0xFF)
28 sendByte((dword >> 24) & 0xFF)
31 return ord(tty.read())
35 last_checksum = recvByte()
36 last_checksum |= (recvByte() << 8)
38 def cmdREAD(address, size):
41 if (recvByte() != 0xF1):
44 if (recvByte() != 0x82):
46 # tell desired address and size
49 # get binary stream of data
51 for i in range(0, size):
52 data.append(recvByte())
57 def cmdWRITE(address, size, data):
60 if (recvByte() != 0xF1):
63 if (recvByte() != 0x83):
65 # tell desired address and size
68 # write binary stream of data
69 for i in range(0, size):
74 # TODO: test this function!
78 if (recvByte() != 0xF1):
81 if (recvByte() != 0x84):
83 # tell desired address
85 # wait for return parameter - not needed here!
88 # TODO: test this function!
90 # call CHECKSUM command
92 if (recvByte() != 0xF1):
95 if (recvByte() != 0x84):
100 def cmdBAUDRATE(baudrate):
103 # send BAUDRATE command
105 if (recvByte() != 0xF1):
108 if (recvByte() != 0x86):
110 # send desired baudrate
111 sendByte(baudrate & 0xFF)
112 sendByte((baudrate >> 8) & 0xFF)
113 sendByte((baudrate >> 16) & 0xFF)
114 sendByte((baudrate >> 24) & 0xFF)
116 class FlashSequence(object):
117 def __init__(self, address, data):
118 self.address = address
121 # list of all our address/data pairs to flash
124 # check command line arguments
125 if len(sys.argv) != 2:
126 print "Usage: " + sys.argv[0] + " [mhx-file]"
129 # read in data from mhx-file before starting
131 fp = open(sys.argv[1], "r")
133 print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
139 # get rid of newline characters
142 # we're only interested in S2 (data sequence with 3 address bytes) records by now
143 if line[0:2] == "S2":
144 byte_count = int(line[2:4], 16)
145 # just to get sure, check if byte count field is valid
146 if (len(line)-4) != (byte_count*2):
147 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
148 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
151 # address and checksum bytes are not needed
153 address = int(line[4:10], 16)
154 datastr = line[10:10+byte_count*2]
156 # convert data hex-byte-string to real byte data list
158 for i in range(0, len(datastr)/2):
159 data.append(int(datastr[2*i:2*i+2], 16))
161 # add flash sequence to our list
162 flashseqs.append(FlashSequence(address, data))
164 print "The following flash sequences have been read in:"
165 for seq in flashseqs:
166 print hex(seq.address) + ":", seq.data
169 print "Initializing serial port..."
170 tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
172 print "Please press RESET on your 1337 board..."
178 if tty.read() == 'F':
180 except SerialPortException:
181 # timeout happened, who cares ;-)
184 print "OK, trying to set baudrate..."
186 cmdBAUDRATE(REAL_BAUDRATE)
187 tty = SerialPort(DEVICE, 100, REAL_BAUDRATE)
190 for seq in flashseqs:
191 print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
192 cmdWRITE(seq.address, len(seq.data), seq.data)
196 # some tests here.......
198 # execute (existing) program in ram
203 # read something from the IRAM
204 #print cmdREAD(0x00030000, 32)
207 #for i in range(0, 32):
209 #cmdWRITE(0x00030000, 32, data)
213 # write something to the begin of the IRAM
216 for i in range(0, 0x400):
218 data_wr.append(value)
219 checksum = (checksum + value) % (2**16)
221 print "Calculated checksum:", checksum
222 print "Writing", data_wr, "to the IRAM..."
223 cmdWRITE(0x00030000, len(data_wr), data_wr)
224 print "Received Checksum:", last_checksum
227 print "Reading from the IRAM again..."
228 data_re = cmdREAD(0x00030000, len(data_wr))
229 print "Received data:", data_re, "Checksum:", last_checksum
233 # see whats in the iram at the moment
235 print "Reading from the IRAM..."
236 data_re = cmdREAD(0x00030000, 0x10000-4)
237 print "Received data:", data_re, "Checksum:", last_checksum
241 # see whats in the dram at the moment
243 print "Reading from the DRAM..."
244 data_re = cmdREAD(0x0002C000, 0x10000-0xC000-4)
245 print "Received data:", data_re, "Checksum:", last_checksum
251 for i in range(0, 0x10000-4):
253 data_wr.append(value)
255 print "Writing", data_wr, "to the IRAM..."
256 cmdWRITE(0x00030000, len(data_wr), data_wr)
257 print "Received Checksum:", last_checksum
264 for i in range(0, 0x10000-0xC000-4):
266 data_wr.append(value)
268 print "Writing", data_wr, "to the DRAM..."
269 cmdWRITE(0x0002C000, len(data_wr), data_wr)
270 print "Received Checksum:", last_checksum
275 # write some data in the iram and try to execute it
286 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0, #nop
287 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
288 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
289 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
290 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
291 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
292 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0]
293 print "Writing", data_wr, "to the IRAM..."
294 cmdWRITE(0x00030000, len(data_wr), data_wr)
295 print "Received Checksum:", last_checksum