improved structure, add main() function
[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         tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
203
204         print "Please press RESET on your board..."
205
206         while True:
207                 tty.write('V')
208                 tty.flush()
209                 try:
210                         if tty.read() == 'F':
211                                 break
212                 except SerialPortException:
213                         # timeout happened, who cares ;-)
214                         pass
215
216         starttime = time.time() # save time at this point for evaluating the duration at the end
217
218         print "OK, trying to set baudrate..."
219         # set baudrate
220         bootromBAUDRATE(BOOTLOADER_BAUDRATE)
221         time.sleep(0.1) # just to get sure that the bootloader is really running in new baudrate mode!
222         del tty
223         tty = SerialPort(DEVICE, 100, BOOTLOADER_BAUDRATE)
224
225         print "Transfering pkernel program to IRAM",
226         # let the fun begin!
227         for seq in bootloaderseqs:
228                 if(seq.address <= 0x40000):
229                         addr = seq.address
230                 else:
231                         continue
232                 #print "RAMing", len(seq.data), "bytes at address", hex(addr)
233                 bootromWRITE(addr, len(seq.data), seq.data)
234                 tty.flush()
235                 sys.stdout.write(".")
236                 sys.stdout.flush()
237         print
238
239         # execute our pkernel finally and set pkernel conform baudrate
240         bootromCALL(0x30000)
241         time.sleep(0.1) # just to get sure that the pkernel is really running!
242         del tty
243         tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
244
245         print "Performing ChipErase..."
246         pkernCHIPERASE()
247
248         print "Flashing",
249         for seq in pkernelseqs:
250                 # skip seqs only consisting of 0xffs
251                 seqset = list(set(seq.data))
252                 if len(seqset) == 1 and seqset[0] == 0xff:
253                         continue
254                 #print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
255                 pkernWRITE(seq.address, len(seq.data), seq.data)
256                 tty.flush()
257                 sys.stdout.write(".")
258                 sys.stdout.flush()
259         print
260
261         duration = time.time() - starttime
262         print "Procedure complete, took", round(duration, 2), "seconds."
263
264         sendByte(0x97) # exit and restart
265         print "Program was started. Have fun!"
266
267
268 if __name__ == '__main__':
269         sys.exit(main())