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):
100 # send BAUDRATE command
102 if (recvByte() != 0xF1):
105 if (recvByte() != 0x86):
107 # send desired baudrate
111 class FlashSequence(object):
112 def __init__(self, address, data):
113 self.address = address
116 def readMHXFile(filename): # desired mhx filename
117 fp = open(filename, "r")
118 retval = [] # returns a list of FlashSequence objects
122 # get rid of newline characters
125 # we're only interested in S2 (data sequence with 3 address bytes) records by now
126 if line[0:2] == "S2":
127 byte_count = int(line[2:4], 16)
128 # just to get sure, check if byte count field is valid
129 if (len(line)-4) != (byte_count*2):
130 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
131 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
134 # address and checksum bytes are not needed
136 address = int(line[4:10], 16)
137 datastr = line[10:10+byte_count*2]
139 # convert data hex-byte-string to real byte data list
141 for i in range(0, len(datastr)/2):
142 data.append(int(datastr[2*i:2*i+2], 16))
144 # add flash sequence to our list
145 retval.append(FlashSequence(address, data))
150 # check command line arguments
151 if len(sys.argv) != 2:
152 print "Usage: " + sys.argv[0] + " [mhx-file]"
155 # read in data from mhx-file before starting
157 flashseqs = readMHXFile(sys.argv[1])
158 except IOError as error:
159 print sys.argv[0] + ": Error - couldn't open file " + error.filename + "!"
162 print "The following flash sequences have been read in:"
163 for seq in flashseqs:
164 print hex(seq.address) + ":", seq.data
166 print "Initializing serial port..."
167 tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
169 print "Please press RESET on your 1337 board..."
175 if tty.read() == 'F':
177 except SerialPortException:
178 # timeout happened, who cares ;-)
181 print "OK, trying to set baudrate..."
183 cmdBAUDRATE(REAL_BAUDRATE)
184 tty = SerialPort(DEVICE, 100, REAL_BAUDRATE)
187 for seq in flashseqs:
188 if(seq.address <= 0x40000):
192 print "RAMing", len(seq.data), "bytes at address", hex(addr)
193 cmdWRITE(addr, len(seq.data), seq.data)
200 # some tests here.......
202 # execute (existing) program in ram
207 # read something from the IRAM
208 #print cmdREAD(0x00030000, 32)
211 #for i in range(0, 32):
213 #cmdWRITE(0x00030000, 32, data)
217 # write something to the begin of the IRAM
220 for i in range(0, 0x400):
222 data_wr.append(value)
223 checksum = (checksum + value) % (2**16)
225 print "Calculated checksum:", checksum
226 print "Writing", data_wr, "to the IRAM..."
227 cmdWRITE(0x00030000, len(data_wr), data_wr)
228 print "Received Checksum:", last_checksum
231 print "Reading from the IRAM again..."
232 data_re = cmdREAD(0x00030000, len(data_wr))
233 print "Received data:", data_re, "Checksum:", last_checksum
237 # see whats in the iram at the moment
239 print "Reading from the IRAM..."
240 data_re = cmdREAD(0x00030000, 0x10000-4)
241 print "Received data:", data_re, "Checksum:", last_checksum
245 # see whats in the dram at the moment
247 print "Reading from the DRAM..."
248 data_re = cmdREAD(0x0002C000, 0x10000-0xC000-4)
249 print "Received data:", data_re, "Checksum:", last_checksum
255 for i in range(0, 0x10000-4):
257 data_wr.append(value)
259 print "Writing", data_wr, "to the IRAM..."
260 cmdWRITE(0x00030000, len(data_wr), data_wr)
261 print "Received Checksum:", last_checksum
268 for i in range(0, 0x10000-0xC000-4):
270 data_wr.append(value)
272 print "Writing", data_wr, "to the DRAM..."
273 cmdWRITE(0x0002C000, len(data_wr), data_wr)
274 print "Received Checksum:", last_checksum
279 # write some data in the iram and try to execute it
290 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0, #nop
291 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
292 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,
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 print "Writing", data_wr, "to the IRAM..."
298 cmdWRITE(0x00030000, len(data_wr), data_wr)
299 print "Received Checksum:", last_checksum