cleaned up cmdBAUDRATE
[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
19 def sendWord(word):
20         sendByte(word & 0xFF)
21         sendByte((word >> 8) & 0xFF)
22
23 def sendDWord(dword):
24         sendByte(dword & 0xFF)
25         sendByte((dword >> 8) & 0xFF)
26         sendByte((dword >> 16) & 0xFF)
27         sendByte((dword >> 24) & 0xFF)
28
29 def recvByte():
30         return ord(tty.read())
31
32 def recvChecksum():
33         global last_checksum
34         last_checksum = recvByte()
35         last_checksum |= (recvByte() << 8)
36
37 def cmdREAD(address, size):
38         # send READ command
39         sendByte(0x01)
40         if (recvByte() != 0xF1):
41                 raise Exception
42         sendByte(0x02)
43         if (recvByte() != 0x82):
44                 raise Exception
45         # tell desired address and size
46         sendDWord(address)
47         sendWord(size)
48         # get binary stream of data
49         data = []
50         for i in range(0, size):
51                 data.append(recvByte())
52         # get checksum
53         recvChecksum()
54         return data
55
56 def cmdWRITE(address, size, data):
57         # send WRITE command
58         sendByte(0x01)
59         if (recvByte() != 0xF1):
60                 raise Exception
61         sendByte(0x03)
62         if (recvByte() != 0x83):
63                 raise Exception
64         # tell desired address and size
65         sendDWord(address)
66         sendWord(size)
67         # write binary stream of data
68         for i in range(0, size):
69                 sendByte(data[i])
70         # get checksum
71         recvChecksum()
72
73 # TODO: test this function!
74 def cmdCALL(address):
75         # send CALL command
76         sendByte(0x01)
77         if (recvByte() != 0xF1):
78                 raise Exception
79         sendByte(0x04)
80         if (recvByte() != 0x84):
81                 raise Exception
82         # tell desired address
83         sendDWord(address)
84         # wait for return parameter - not needed here!
85         #return recvByte()
86
87 # TODO: test this function!
88 def cmdCHECKSUM():
89         # call CHECKSUM command
90         sendByte(0x01)
91         if (recvByte() != 0xF1):
92                 raise Exception
93         sendByte(0x05)
94         if (recvByte() != 0x84):
95                 raise Exception
96         # get checksum
97         recvChecksum()
98
99 def cmdBAUDRATE(baudrate):
100         # send BAUDRATE command
101         sendByte(0x01)
102         if (recvByte() != 0xF1):
103                 raise Exception
104         sendByte(0x06)
105         if (recvByte() != 0x86):
106                 raise Exception
107         # send desired baudrate
108         sendDWord(baudrate)
109
110 class FlashSequence(object):
111         def __init__(self, address, data):
112                 self.address = address
113                 self.data = data
114
115 # list of all our address/data pairs to flash
116 flashseqs = []
117
118 # check command line arguments
119 if len(sys.argv) != 2:
120         print "Usage: " + sys.argv[0] + " [mhx-file]"
121         sys.exit(1)
122
123 # read in data from mhx-file before starting
124 try:
125         fp = open(sys.argv[1], "r")
126 except IOError:
127         print sys.argv[0] + ": Error - couldn't open file " + sys.argv[1] + "!"
128         sys.exit(1)
129
130 linecount = 0
131 for line in fp:
132         linecount += 1
133         # get rid of newline characters
134         line = line.strip()
135
136         # we're only interested in S2 (data sequence with 3 address bytes) records by now
137         if line[0:2] == "S2":
138                 byte_count = int(line[2:4], 16)
139                 # just to get sure, check if byte count field is valid
140                 if (len(line)-4) != (byte_count*2):
141                         print sys.argv[0] + ": Warning - inavlid byte count field in " + \
142                                 sys.argv[1] + ":" + str(linecount) + ", skipping line!"
143                         continue
144
145                 # address and checksum bytes are not needed
146                 byte_count -= 4
147                 address = int(line[4:10], 16)
148                 datastr = line[10:10+byte_count*2]
149
150                 # convert data hex-byte-string to real byte data list
151                 data = []
152                 for i in range(0, len(datastr)/2):
153                         data.append(int(datastr[2*i:2*i+2], 16))
154
155                 # add flash sequence to our list
156                 flashseqs.append(FlashSequence(address, data))
157
158 print "The following flash sequences have been read in:"
159 for seq in flashseqs:
160         print hex(seq.address) + ":", seq.data
161
162
163 print "Initializing serial port..."
164 tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
165
166 print "Please press RESET on your 1337 board..."
167
168 while 1:
169         tty.write('V')
170         tty.flush()
171         try: 
172                 if tty.read() == 'F':
173                         break
174         except SerialPortException: 
175                 # timeout happened, who cares ;-)
176                 pass
177
178 print "OK, trying to set baudrate..."
179 # set baudrate
180 cmdBAUDRATE(REAL_BAUDRATE)
181 tty = SerialPort(DEVICE, 100, REAL_BAUDRATE)
182
183 # let the fun begin!
184 for seq in flashseqs:
185         if(seq.address <= 0x40000):
186                 addr = seq.address
187         else:
188                 continue
189         print "RAMing", len(seq.data), "bytes at address", hex(addr)
190         cmdWRITE(addr, len(seq.data), seq.data)
191         tty.flush()
192
193 cmdCALL(0x30000);
194 sys.exit(0)
195
196
197 # some tests here.......
198 """
199 # execute (existing) program in ram
200 cmdCALL(0x00033ffc)
201 sys.exit(0)
202 """
203
204 # read something from the IRAM
205 #print cmdREAD(0x00030000, 32)
206
207 #data = []
208 #for i in range(0, 32):
209 #       data.append(i)
210 #cmdWRITE(0x00030000, 32, data)
211
212
213 """
214 # write something to the begin of the IRAM
215 data_wr = []
216 checksum = 0
217 for i in range(0, 0x400):
218         value = i%256
219         data_wr.append(value)
220         checksum = (checksum + value) % (2**16)
221
222 print "Calculated checksum:", checksum
223 print "Writing", data_wr, "to the IRAM..."
224 cmdWRITE(0x00030000, len(data_wr), data_wr)
225 print "Received Checksum:", last_checksum
226 print
227
228 print "Reading from the IRAM again..."
229 data_re = cmdREAD(0x00030000, len(data_wr))
230 print "Received data:", data_re, "Checksum:", last_checksum
231 """
232
233 """
234 # see whats in the iram at the moment
235 data_wr = []
236 print "Reading from the IRAM..."
237 data_re = cmdREAD(0x00030000, 0x10000-4)
238 print "Received data:", data_re, "Checksum:", last_checksum
239 """
240
241 """
242 # see whats in the dram at the moment
243 data_wr = []
244 print "Reading from the DRAM..."
245 data_re = cmdREAD(0x0002C000, 0x10000-0xC000-4)
246 print "Received data:", data_re, "Checksum:", last_checksum
247 """
248
249 """
250 # blank the iram
251 data_wr = []
252 for i in range(0, 0x10000-4):
253         value = 0
254         data_wr.append(value)
255
256 print "Writing", data_wr, "to the IRAM..."
257 cmdWRITE(0x00030000, len(data_wr), data_wr)
258 print "Received Checksum:", last_checksum
259 print
260 """
261
262 """
263 # blank the dram
264 data_wr = []
265 for i in range(0, 0x10000-0xC000-4):
266         value = 0
267         data_wr.append(value)
268
269 print "Writing", data_wr, "to the DRAM..."
270 cmdWRITE(0x0002C000, len(data_wr), data_wr)
271 print "Received Checksum:", last_checksum
272 print
273 """
274
275 """
276 # write some data in the iram and try to execute it
277 data_wr =[
278                 0x9B,0x00,
279                 0x0D,0x4e,
280                 0xcf,0xf1,
281                 0x16,0x01,
282                 0x9b,0x05,
283                 0x04,0xc7,
284                 0xc1,0x06,
285                 0x16,0x56,
286                 0xe0,0xfb, #branch
287                 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0, #nop
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                 0x9f,0xa0,0x9f,0xa0,0x9f,0xa0]
294 print "Writing", data_wr, "to the IRAM..."
295 cmdWRITE(0x00030000, len(data_wr), data_wr)
296 print "Received Checksum:", last_checksum
297 print
298 cmdCALL(0x00030000)
299 """