0ca52c57de1fd788e5b06f4af00be107e0549c6d
[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 with the internal bootloader after init
10 BOOTLOADER_BAUDRATE=38400
11 # baudrate used for communication with the pkernel program that does the flashing eventually
12 KERNEL_BAUDRATE=115200
13
14 # contains the last received checksum from a READ, WRITE or CHECKSUM command
15 last_checksum = 0
16
17 class FlashSequence(object):
18         def __init__(self, address, data):
19                 self.address = address
20                 self.data = data
21
22 def sendByte(byte):
23         tty.write(chr(byte))
24
25 def sendWord(word):
26         sendByte(word & 0xFF)
27         sendByte((word >> 8) & 0xFF)
28
29 def sendDWord(dword):
30         sendByte(dword & 0xFF)
31         sendByte((dword >> 8) & 0xFF)
32         sendByte((dword >> 16) & 0xFF)
33         sendByte((dword >> 24) & 0xFF)
34
35 def recvByte():
36         return ord(tty.read())
37
38 def recvChecksum():
39         global last_checksum
40         last_checksum = recvByte()
41         last_checksum |= (recvByte() << 8)
42
43 def bootromREAD(address, size):
44         # send READ command
45         sendByte(0x01)
46         if (recvByte() != 0xF1):
47                 raise Exception
48         sendByte(0x02)
49         if (recvByte() != 0x82):
50                 raise Exception
51         # tell desired address and size
52         sendDWord(address)
53         sendWord(size)
54         # get binary stream of data
55         data = []
56         for i in range(0, size):
57                 data.append(recvByte())
58         # get checksum
59         recvChecksum()
60         return data
61
62 def bootromWRITE(address, size, data):
63         # send WRITE command
64         sendByte(0x01)
65         if (recvByte() != 0xF1):
66                 raise Exception
67         sendByte(0x03)
68         if (recvByte() != 0x83):
69                 raise Exception
70         # tell desired address and size
71         sendDWord(address)
72         sendWord(size)
73         # write binary stream of data
74         for i in range(0, size):
75                 sendByte(data[i])
76         # get checksum
77         recvChecksum()
78
79 # TODO: test this function!
80 def bootromCALL(address):
81         # send CALL command
82         sendByte(0x01)
83         if (recvByte() != 0xF1):
84                 raise Exception
85         sendByte(0x04)
86         if (recvByte() != 0x84):
87                 raise Exception
88         # tell desired address
89         sendDWord(address)
90         # wait for return parameter - not needed here!
91         #return recvByte()
92
93 # TODO: test this function!
94 def bootromCHECKSUM():
95         # call CHECKSUM command
96         sendByte(0x01)
97         if (recvByte() != 0xF1):
98                 raise Exception
99         sendByte(0x05)
100         if (recvByte() != 0x84):
101                 raise Exception
102         # get checksum
103         recvChecksum()
104
105 def bootromBAUDRATE(baudrate):
106         # send BAUDRATE command
107         sendByte(0x01)
108         if (recvByte() != 0xF1):
109                 raise Exception
110         sendByte(0x06)
111         if (recvByte() != 0x86):
112                 raise Exception
113         # send desired baudrate
114         sendDWord(baudrate)
115
116 def pkernCHIPERASE():
117         sendByte(0x15)
118         if (recvByte() != 0x45):
119                 raise Exception
120         # wait till completion...
121         if (recvByte() != 0x23):
122                 raise Exception
123
124 def pkernERASE(address, size):
125         sendByte(0x12)
126         if (recvByte() != 0x11):
127                 raise Exception
128         sendDWord(address)
129         sendWord(size)
130         if (recvByte() != 0x18):
131                 raise Exception
132
133 def pkernWRITE(address, size, data):
134         # send WRITE command
135         sendByte(0x13)
136         if (recvByte() != 0x37):
137                 raise Exception
138         # tell desired address and size
139         sendDWord(address)
140         sendWord(size)
141
142         # write binary stream of data
143         for i in range(0, size):
144                 sendByte(data[i])
145
146         if (recvByte() != 0x28):
147                 raise Exception
148
149 def readMHXFile(filename): # desired mhx filename
150         fp = open(filename, "r")
151         retval = [] # returns a list of FlashSequence objects
152         linecount = 0
153         for line in fp:
154                 linecount += 1
155                 # get rid of newline characters
156                 line = line.strip()
157
158                 # we're only interested in S2 (data sequence with 3 address bytes) records by now
159                 if line[0:2] == "S2":
160                         byte_count = int(line[2:4], 16)
161                         # just to get sure, check if byte count field is valid
162                         if (len(line)-4) != (byte_count*2):
163                                 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
164                                         sys.argv[1] + ":" + str(linecount) + ", skipping line!"
165                                 continue
166
167                         # address and checksum bytes are not needed
168                         byte_count -= 4
169                         address = int(line[4:10], 16)
170                         datastr = line[10:10+byte_count*2]
171
172                         # convert data hex-byte-string to real byte data list
173                         data = []
174                         for i in range(0, len(datastr)/2):
175                                 data.append(int(datastr[2*i:2*i+2], 16))
176
177                         # add flash sequence to our list
178                         retval.append(FlashSequence(address, data))
179         fp.close()
180         return retval
181
182 def main(argv=None):
183         # check command line arguments
184         if argv is None:
185                 argv = sys.argv
186         if len(argv) != 2:
187                 print "Usage: " + argv[0] + " [target mhx-file]"
188                 return 1
189
190         # read in data from mhx-files before starting
191         try:
192                 try:
193                         bootloaderseqs = readMHXFile("pkernel/pkernel.mhx")
194                 except IOError as error1:
195                         bootloaderseqs = readMHXFile("%PREFIX%/share/frprog/pkernel.mhx")
196                 pkernelseqs = readMHXFile(argv[1])
197         except IOError as error:
198                 print argv[0] + ": Error - couldn't open file " + error.filename + "!"
199                 return 1
200
201         print "Initializing serial port..."
202         global tty
203         tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
204
205         print "Please press RESET on your board..."
206
207         while True:
208                 tty.write('V')
209                 tty.flush()
210                 try:
211                         if tty.read() == 'F':
212                                 break
213                 except SerialPortException:
214                         # timeout happened, who cares ;-)
215                         pass
216
217         starttime = time.time() # save time at this point for evaluating the duration at the end
218
219         print "OK, trying to set baudrate..."
220         # set baudrate
221         bootromBAUDRATE(BOOTLOADER_BAUDRATE)
222         time.sleep(0.1) # just to get sure that the bootloader is really running in new baudrate mode!
223         del tty
224         tty = SerialPort(DEVICE, 100, BOOTLOADER_BAUDRATE)
225
226         print "Transfering pkernel program to IRAM",
227         # let the fun begin!
228         for seq in bootloaderseqs:
229                 if(seq.address <= 0x40000):
230                         addr = seq.address
231                 else:
232                         continue
233                 #print "RAMing", len(seq.data), "bytes at address", hex(addr)
234                 bootromWRITE(addr, len(seq.data), seq.data)
235                 tty.flush()
236                 sys.stdout.write(".")
237                 sys.stdout.flush()
238         print
239
240         # execute our pkernel finally and set pkernel conform baudrate
241         bootromCALL(0x30000)
242         time.sleep(0.1) # just to get sure that the pkernel is really running!
243         del tty
244         tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
245
246         print "Performing ChipErase..."
247         pkernCHIPERASE()
248
249         print "Flashing",
250         for seq in pkernelseqs:
251                 # skip seqs only consisting of 0xffs
252                 seqset = list(set(seq.data))
253                 if len(seqset) == 1 and seqset[0] == 0xff:
254                         continue
255                 #print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
256                 pkernWRITE(seq.address, len(seq.data), seq.data)
257                 tty.flush()
258                 sys.stdout.write(".")
259                 sys.stdout.flush()
260         print
261
262         duration = time.time() - starttime
263         print "Procedure complete, took", round(duration, 2), "seconds."
264
265         sendByte(0x97) # exit and restart
266         print "Program was started. Have fun!"
267
268
269 if __name__ == '__main__':
270         sys.exit(main())