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
21 sendByte((word >> 8) & 0xFF)
24 sendByte(dword & 0xFF)
25 sendByte((dword >> 8) & 0xFF)
26 sendByte((dword >> 16) & 0xFF)
27 sendByte((dword >> 24) & 0xFF)
30 return ord(tty.read())
34 last_checksum = recvByte()
35 last_checksum |= (recvByte() << 8)
37 def cmdREAD(address, size):
40 if (recvByte() != 0xF1):
43 if (recvByte() != 0x82):
45 # tell desired address and size
48 # get binary stream of data
50 for i in range(0, size):
51 data.append(recvByte())
56 def cmdWRITE(address, size, data):
59 if (recvByte() != 0xF1):
62 if (recvByte() != 0x83):
64 # tell desired address and size
67 # write binary stream of data
68 for i in range(0, size):
73 # TODO: test this function!
77 if (recvByte() != 0xF1):
80 if (recvByte() != 0x84):
82 # tell desired address
84 # wait for return parameter - not needed here!
87 # TODO: test this function!
89 # call CHECKSUM command
91 if (recvByte() != 0xF1):
94 if (recvByte() != 0x84):
99 def cmdBAUDRATE(baudrate):
102 # send BAUDRATE command
104 if (recvByte() != 0xF1):
107 if (recvByte() != 0x86):
109 # send desired baudrate
110 sendByte(baudrate & 0xFF)
111 sendByte((baudrate >> 8) & 0xFF)
112 sendByte((baudrate >> 16) & 0xFF)
113 sendByte((baudrate >> 24) & 0xFF)
115 class FlashSequence(object):
116 def __init__(self, address, data):
117 self.address = address
120 # list of all our address/data pairs to flash
123 # check command line arguments
124 if len(sys.argv) != 2:
125 print "Usage: " + sys.argv[0] + " [mhx-file]"
128 # read in data from mhx-file before starting
130 fp = open(sys.argv[1], "r")
132 print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
138 # get rid of newline characters
141 # we're only interested in S2 (data sequence with 3 address bytes) records by now
142 if line[0:2] == "S2":
143 byte_count = int(line[2:4], 16)
144 # just to get sure, check if byte count field is valid
145 if (len(line)-4) != (byte_count*2):
146 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
147 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
150 # address and checksum bytes are not needed
152 address = int(line[4:10], 16)
153 datastr = line[10:10+byte_count*2]
155 # convert data hex-byte-string to real byte data list
157 for i in range(0, len(datastr)/2):
158 data.append(int(datastr[2*i:2*i+2], 16))
160 # add flash sequence to our list
161 flashseqs.append(FlashSequence(address, data))
163 print "The following flash sequences have been read in:"
164 for seq in flashseqs:
165 print hex(seq.address) + ":", seq.data
168 print "Initializing serial port..."
169 tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
171 print "Please press RESET on your 1337 board..."
177 if tty.read() == 'F':
179 except SerialPortException:
180 # timeout happened, who cares ;-)
183 print "OK, trying to set baudrate..."
185 cmdBAUDRATE(REAL_BAUDRATE)
186 tty = SerialPort(DEVICE, 100, REAL_BAUDRATE)
189 for seq in flashseqs:
190 if(seq.address <= 0x40000):
194 print "RAMing", len(seq.data), "bytes at address", hex(addr)
195 cmdWRITE(addr, len(seq.data), seq.data)
202 # some tests here.......
204 # execute (existing) program in ram
209 # read something from the IRAM
210 #print cmdREAD(0x00030000, 32)
213 #for i in range(0, 32):
215 #cmdWRITE(0x00030000, 32, data)
219 # write something to the begin of the IRAM
222 for i in range(0, 0x400):
224 data_wr.append(value)
225 checksum = (checksum + value) % (2**16)
227 print "Calculated checksum:", checksum
228 print "Writing", data_wr, "to the IRAM..."
229 cmdWRITE(0x00030000, len(data_wr), data_wr)
230 print "Received Checksum:", last_checksum
233 print "Reading from the IRAM again..."
234 data_re = cmdREAD(0x00030000, len(data_wr))
235 print "Received data:", data_re, "Checksum:", last_checksum
239 # see whats in the iram at the moment
241 print "Reading from the IRAM..."
242 data_re = cmdREAD(0x00030000, 0x10000-4)
243 print "Received data:", data_re, "Checksum:", last_checksum
247 # see whats in the dram at the moment
249 print "Reading from the DRAM..."
250 data_re = cmdREAD(0x0002C000, 0x10000-0xC000-4)
251 print "Received data:", data_re, "Checksum:", last_checksum
257 for i in range(0, 0x10000-4):
259 data_wr.append(value)
261 print "Writing", data_wr, "to the IRAM..."
262 cmdWRITE(0x00030000, len(data_wr), data_wr)
263 print "Received Checksum:", last_checksum
270 for i in range(0, 0x10000-0xC000-4):
272 data_wr.append(value)
274 print "Writing", data_wr, "to the DRAM..."
275 cmdWRITE(0x0002C000, len(data_wr), data_wr)
276 print "Received Checksum:", last_checksum
281 # write some data in the iram and try to execute it
292 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0, #nop
293 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
294 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
295 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
296 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
297 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
298 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0]
299 print "Writing", data_wr, "to the IRAM..."
300 cmdWRITE(0x00030000, len(data_wr), data_wr)
301 print "Received Checksum:", last_checksum