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 print "Initializing serial port..."
117 tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
119 print "Please press RESET on your 1337 board..."
125 if tty.read() == 'F':
127 except SerialPortException:
128 # timeout happened, who cares ;-)
131 print "OK, trying to set baudrate..."
134 cmdBAUDRATE(REAL_BAUDRATE)
135 tty = SerialPort(DEVICE, 100, REAL_BAUDRATE)
145 # execute (existing) program in ram
150 # read something from the IRAM
151 #print cmdREAD(0x00030000, 32)
154 #for i in range(0, 32):
156 #cmdWRITE(0x00030000, 32, data)
160 # write something to the begin of the IRAM
163 for i in range(0, 0x400):
165 data_wr.append(value)
166 checksum = (checksum + value) % (2**16)
168 print "Calculated checksum:", checksum
169 print "Writing", data_wr, "to the IRAM..."
170 cmdWRITE(0x00030000, len(data_wr), data_wr)
171 print "Received Checksum:", last_checksum
174 print "Reading from the IRAM again..."
175 data_re = cmdREAD(0x00030000, len(data_wr))
176 print "Received data:", data_re, "Checksum:", last_checksum
180 # see whats in the iram at the moment
182 print "Reading from the IRAM..."
183 data_re = cmdREAD(0x00030000, 0x10000-4)
184 print "Received data:", data_re, "Checksum:", last_checksum
188 # see whats in the dram at the moment
190 print "Reading from the DRAM..."
191 data_re = cmdREAD(0x0002C000, 0x10000-0xC000-4)
192 print "Received data:", data_re, "Checksum:", last_checksum
198 for i in range(0, 0x10000-4):
200 data_wr.append(value)
202 print "Writing", data_wr, "to the IRAM..."
203 cmdWRITE(0x00030000, len(data_wr), data_wr)
204 print "Received Checksum:", last_checksum
211 for i in range(0, 0x10000-0xC000-4):
213 data_wr.append(value)
215 print "Writing", data_wr, "to the DRAM..."
216 cmdWRITE(0x0002C000, len(data_wr), data_wr)
217 print "Received Checksum:", last_checksum
224 S20E0F40009B000D4ECFF11601E0FFF6
225 S214148000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF67
230 # write some data in the iram and try to execute it
231 # ATTENTION: before running this, run a program on the board (e.g. flashdemo).
232 # the bytecode will be executed after copied into iram, however it won't stop
233 # and executes the stuff after it in the iram. after a powerreset this can be
234 # randomcrap and this could be very dangerous! so pay attention please
235 data_wr =[0x9B,0x00,0x0D,0x4e,0xcf,0xf1,0x16,0x01,0xe0,0xff,0xf6,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0xe0,
236 0xcb,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,
237 0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0,0x9f,0xa0]
238 print "Writing", data_wr, "to the IRAM..."
239 cmdWRITE(0x00030000, len(data_wr), data_wr)
240 print "Received Checksum:", last_checksum