make install && README
[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 def sendByte(byte):
18         tty.write(chr(byte))
19
20 def sendWord(word):
21         sendByte(word & 0xFF)
22         sendByte((word >> 8) & 0xFF)
23
24 def sendDWord(dword):
25         sendByte(dword & 0xFF)
26         sendByte((dword >> 8) & 0xFF)
27         sendByte((dword >> 16) & 0xFF)
28         sendByte((dword >> 24) & 0xFF)
29
30 def recvByte():
31         return ord(tty.read())
32
33 def recvChecksum():
34         global last_checksum
35         last_checksum = recvByte()
36         last_checksum |= (recvByte() << 8)
37
38 def bootromREAD(address, size):
39         # send READ command
40         sendByte(0x01)
41         if (recvByte() != 0xF1):
42                 raise Exception
43         sendByte(0x02)
44         if (recvByte() != 0x82):
45                 raise Exception
46         # tell desired address and size
47         sendDWord(address)
48         sendWord(size)
49         # get binary stream of data
50         data = []
51         for i in range(0, size):
52                 data.append(recvByte())
53         # get checksum
54         recvChecksum()
55         return data
56
57 def bootromWRITE(address, size, data):
58         # send WRITE command
59         sendByte(0x01)
60         if (recvByte() != 0xF1):
61                 raise Exception
62         sendByte(0x03)
63         if (recvByte() != 0x83):
64                 raise Exception
65         # tell desired address and size
66         sendDWord(address)
67         sendWord(size)
68         # write binary stream of data
69         for i in range(0, size):
70                 sendByte(data[i])
71         # get checksum
72         recvChecksum()
73
74 # TODO: test this function!
75 def bootromCALL(address):
76         # send CALL command
77         sendByte(0x01)
78         if (recvByte() != 0xF1):
79                 raise Exception
80         sendByte(0x04)
81         if (recvByte() != 0x84):
82                 raise Exception
83         # tell desired address
84         sendDWord(address)
85         # wait for return parameter - not needed here!
86         #return recvByte()
87
88 # TODO: test this function!
89 def bootromCHECKSUM():
90         # call CHECKSUM command
91         sendByte(0x01)
92         if (recvByte() != 0xF1):
93                 raise Exception
94         sendByte(0x05)
95         if (recvByte() != 0x84):
96                 raise Exception
97         # get checksum
98         recvChecksum()
99
100 def bootromBAUDRATE(baudrate):
101         # send BAUDRATE command
102         sendByte(0x01)
103         if (recvByte() != 0xF1):
104                 raise Exception
105         sendByte(0x06)
106         if (recvByte() != 0x86):
107                 raise Exception
108         # send desired baudrate
109         sendDWord(baudrate)
110
111 def pkernCHIPERASE():
112         sendByte(0x15)
113         if (recvByte() != 0x45):
114                 raise Exception
115         # wait till completion...
116         if (recvByte() != 0x23):
117                 raise Exception
118
119 def pkernERASE(address, size):
120         sendByte(0x12)
121         if (recvByte() != 0x11):
122                 raise Exception
123         sendDWord(address)
124         sendWord(size)
125         if (recvByte() != 0x18):
126                 raise Exception
127
128
129 def pkernWRITE(address, size, data):
130         # send WRITE command
131         sendByte(0x13)
132         if (recvByte() != 0x37):
133                 raise Exception
134         # tell desired address and size
135         sendDWord(address)
136         sendWord(size)
137
138         # write binary stream of data
139         for i in range(0, size):
140                 sendByte(data[i])
141
142         if (recvByte() != 0x28):
143                 raise Exception
144
145
146 class FlashSequence(object):
147         def __init__(self, address, data):
148                 self.address = address
149                 self.data = data
150
151 def readMHXFile(filename): # desired mhx filename
152         fp = open(filename, "r")
153         retval = [] # returns a list of FlashSequence objects
154         linecount = 0
155         for line in fp:
156                 linecount += 1
157                 # get rid of newline characters
158                 line = line.strip()
159
160                 # we're only interested in S2 (data sequence with 3 address bytes) records by now
161                 if line[0:2] == "S2":
162                         byte_count = int(line[2:4], 16)
163                         # just to get sure, check if byte count field is valid
164                         if (len(line)-4) != (byte_count*2):
165                                 print sys.argv[0] + ": Warning - inavlid byte count field in " + \
166                                         sys.argv[1] + ":" + str(linecount) + ", skipping line!"
167                                 continue
168
169                         # address and checksum bytes are not needed
170                         byte_count -= 4
171                         address = int(line[4:10], 16)
172                         datastr = line[10:10+byte_count*2]
173
174                         # convert data hex-byte-string to real byte data list
175                         data = []
176                         for i in range(0, len(datastr)/2):
177                                 data.append(int(datastr[2*i:2*i+2], 16))
178
179                         # add flash sequence to our list
180                         retval.append(FlashSequence(address, data))
181         fp.close()
182         return retval
183
184
185 # check command line arguments
186 if len(sys.argv) != 2:
187         print "Usage: " + sys.argv[0] + " [target mhx-file]"
188         sys.exit(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(sys.argv[1])
197 except IOError as error:
198         print sys.argv[0] + ": Error - couldn't open file " + error.filename + "!"
199         sys.exit(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!"