d99f48fc63cd87be463da9f795ebfda2b0b820d0
[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 main(argv=None):
181         # check command line arguments
182         if argv is None:
183                 argv = sys.argv
184
185         if len(argv) == 2 and (argv[1] == "-v" or argv[1] == "--version"):
186                 print "Version: %VERSION%"
187                 return 0
188
189         if len(argv) != 2 and len(argv) != 4:
190                 print "Usage: " + argv[0] + " <target mhx-file> [-d DEVICE]"
191                 return 1
192
193         # standard serial device to communicate with
194         DEVICE="/dev/ttyUSB0"
195         if len(argv) == 4:
196                 DEVICE = argv[3]
197
198         # read in data from mhx-files before starting
199         try:
200                 try:
201                         bootloaderseqs = readMHXFile("pkernel/pkernel.mhx")
202                 except IOError as error1:
203                         bootloaderseqs = readMHXFile("%PREFIX%/share/frprog/pkernel.mhx")
204                 pkernelseqs = readMHXFile(argv[1])
205         except IOError as error:
206                 print argv[0] + ": Error - couldn't open file " + error.filename + "!"
207                 return 1
208
209         print "Initializing serial port..."
210         global tty
211         tty = SerialPort(DEVICE, 100, INIT_BAUDRATE)
212
213         print "Please press RESET on your board..."
214
215         while True:
216                 tty.write('V')
217                 tty.flush()
218                 try:
219                         if tty.read() == 'F':
220                                 break
221                 except SerialPortException:
222                         # timeout happened, who cares ;-)
223                         pass
224
225         starttime = time.time() # save time at this point for evaluating the duration at the end
226
227         print "OK, trying to set baudrate..."
228         # set baudrate
229         bootromBAUDRATE(BOOTLOADER_BAUDRATE)
230         time.sleep(0.1) # just to get sure that the bootloader is really running in new baudrate mode!
231         del tty
232         tty = SerialPort(DEVICE, 100, BOOTLOADER_BAUDRATE)
233
234         print "Transfering pkernel program to IRAM",
235         # let the fun begin!
236         for seq in bootloaderseqs:
237                 if(seq.address <= 0x40000):
238                         addr = seq.address
239                 else:
240                         continue
241                 #print "RAMing", len(seq.data), "bytes at address", hex(addr)
242                 bootromWRITE(addr, len(seq.data), seq.data)
243                 tty.flush()
244                 sys.stdout.write(".")
245                 sys.stdout.flush()
246         print
247
248         # execute our pkernel finally and set pkernel conform baudrate
249         bootromCALL(0x30000)
250         time.sleep(0.1) # just to get sure that the pkernel is really running!
251         del tty
252         tty = SerialPort(DEVICE, None, KERNEL_BAUDRATE)
253
254         print "Performing ChipErase..."
255         pkernCHIPERASE()
256
257         print "Flashing",
258         for seq in pkernelseqs:
259                 # skip seqs only consisting of 0xffs
260                 seqset = list(set(seq.data))
261                 if len(seqset) == 1 and seqset[0] == 0xff:
262                         continue
263                 #print "Flashing", len(seq.data), "bytes at address", hex(seq.address)
264                 pkernWRITE(seq.address, len(seq.data), seq.data)
265                 tty.flush()
266                 sys.stdout.write(".")
267                 sys.stdout.flush()
268         print
269
270         duration = time.time() - starttime
271         print "Procedure complete, took", round(duration, 2), "seconds."
272
273         sendByte(0x97) # exit and restart
274         print "Program was started. Have fun!"
275
276
277 if __name__ == '__main__':
278         sys.exit(main())