execute own code from iram, however READ attention text before doing
[pyfrprog.git] / frprog.py
1 #!/usr/bin/env python
2 import sys, time
3 from SerialPort_linux import *
4
5 # serial device to communicate with
6 DEVICE="/dev/ttyUSB0"
7 # baudrate used for initialization
8 INIT_BAUDRATE=9600
9 # baudrate used for communication after init
10 REAL_BAUDRATE=38400
11
12 # contains the last received checksum from a READ, WRITE or CHECKSUM command
13 last_checksum = 0
14
15 def sendByte(byte):
16         time.sleep(0.001) # just to get sure, wait 1ms
17         tty.write(chr(byte))
18         tty.flush()
19
20 def sendWord(word):
21         sendByte(word & 0xFF)
22         sendByte((word >> 8) & 0xFF)
23
24 def sendDWord(dword):
25         sendByte(dword & 0xFF)
26         sendByte((dword >> 8) & 0xFF)
27         sendByte((dword >> 16) & 0xFF)
28         sendByte((dword >> 24) & 0xFF)
29
30 def recvByte():
31         return ord(tty.read())
32
33 def recvChecksum():
34         global last_checksum
35         last_checksum = recvByte()
36         last_checksum |= (recvByte() << 8)
37
38 def cmdREAD(address, size):
39         # send READ command
40         sendByte(0x01)
41         if (recvByte() != 0xF1):
42                 raise Exception
43         sendByte(0x02)
44         if (recvByte() != 0x82):
45                 raise Exception
46         # tell desired address and size
47         sendDWord(address)
48         sendWord(size)
49         # get binary stream of data
50         data = []
51         for i in range(0, size):
52                 data.append(recvByte())
53         # get checksum
54         recvChecksum()
55         return data
56
57 def cmdWRITE(address, size, data):
58         # send WRITE command
59         sendByte(0x01)
60         if (recvByte() != 0xF1):
61                 raise Exception
62         sendByte(0x03)
63         if (recvByte() != 0x83):
64                 raise Exception
65         # tell desired address and size
66         sendDWord(address)
67         sendWord(size)
68         # write binary stream of data
69         for i in range(0, size):
70                 sendByte(data[i])
71         # get checksum
72         recvChecksum()
73
74 # TODO: test this function!
75 def cmdCALL(address):
76         # send CALL command
77         sendByte(0x01)
78         if (recvByte() != 0xF1):
79                 raise Exception
80         sendByte(0x04)
81         if (recvByte() != 0x84):
82                 raise Exception
83         # tell desired address
84         sendDWord(address)
85         # wait for return parameter - not needed here!
86         #return recvByte()
87
88 # TODO: test this function!
89 def cmdCHECKSUM():
90         # call CHECKSUM command
91         sendByte(0x01)
92         if (recvByte() != 0xF1):
93                 raise Exception
94         sendByte(0x05)
95         if (recvByte() != 0x84):
96                 raise Exception
97         # get checksum
98         recvChecksum()
99
100 def cmdBAUDRATE(baudrate):
101         global last_checksum
102
103         # send BAUDRATE command
104         sendByte(0x01)
105         if (recvByte() != 0xF1):
106                 raise Exception
107         sendByte(0x06)
108         if (recvByte() != 0x86):
109                 raise Exception
110         # send desired baudrate
111         sendByte(baudrate & 0xFF)
112         sendByte((baudrate >> 8) & 0xFF)
113         sendByte((baudrate >> 16) & 0xFF)
114         sendByte((baudrate >> 24) & 0xFF)
115
116 print "Initializing serial port..."
117 tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
118
119 print "Please press RESET on your 1337 board..."
120
121 while 1:
122         tty.write('V')
123         tty.flush()
124         try: 
125                 if tty.read() == 'F':
126                         break
127         except SerialPortException: 
128                 # timeout happened, who cares ;-)
129                 pass
130
131 print "OK, trying to set baudrate..."
132
133 # set baudrate
134 cmdBAUDRATE(REAL_BAUDRATE)
135 tty = SerialPort(DEVICE, 100, REAL_BAUDRATE)
136 """
137 print
138 sendByte(0x01)
139 print recvByte()
140 sendByte(0x02)
141 print recvByte()
142 sys.exit(0)
143 """
144 """
145 # execute (existing) program in ram
146 cmdCALL(0x00033ffc)
147 sys.exit(0)
148 """
149
150 # read something from the IRAM
151 #print cmdREAD(0x00030000, 32)
152
153 #data = []
154 #for i in range(0, 32):
155 #       data.append(i)
156 #cmdWRITE(0x00030000, 32, data)
157
158
159 """
160 # write something to the begin of the IRAM
161 data_wr = []
162 checksum = 0
163 for i in range(0, 0x400):
164         value = i%256
165         data_wr.append(value)
166         checksum = (checksum + value) % (2**16)
167
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
172 print
173
174 print "Reading from the IRAM again..."
175 data_re = cmdREAD(0x00030000, len(data_wr))
176 print "Received data:", data_re, "Checksum:", last_checksum
177 """
178
179 """
180 # see whats in the iram at the moment
181 data_wr = []
182 print "Reading from the IRAM..."
183 data_re = cmdREAD(0x00030000, 0x10000-4)
184 print "Received data:", data_re, "Checksum:", last_checksum
185 """
186
187 """
188 # see whats in the dram at the moment
189 data_wr = []
190 print "Reading from the DRAM..."
191 data_re = cmdREAD(0x0002C000, 0x10000-0xC000-4)
192 print "Received data:", data_re, "Checksum:", last_checksum
193 """
194
195 """
196 # blank the iram
197 data_wr = []
198 for i in range(0, 0x10000-4):
199         value = 0
200         data_wr.append(value)
201
202 print "Writing", data_wr, "to the IRAM..."
203 cmdWRITE(0x00030000, len(data_wr), data_wr)
204 print "Received Checksum:", last_checksum
205 print
206 """
207
208 """
209 # blank the dram
210 data_wr = []
211 for i in range(0, 0x10000-0xC000-4):
212         value = 0
213         data_wr.append(value)
214
215 print "Writing", data_wr, "to the DRAM..."
216 cmdWRITE(0x0002C000, len(data_wr), data_wr)
217 print "Received Checksum:", last_checksum
218 print
219 """
220
221
222 """
223 S006000066756AB4
224 S20E0F40009B000D4ECFF11601E0FFF6
225 S214148000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF67
226 S804000000FB
227
228 NOP = 0x9fa0
229 """
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
241 print
242 cmdCALL(0x00030000)