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