-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic_programmer.py
372 lines (321 loc) · 13.7 KB
/
pic_programmer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/python
"""
Copyright (C) 2012-2017 Kirill Kulakov, Jose Carlos Granja & Xerxes Ranby
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from serial import *
import getopt
import sys
from Hex import *
mcus = (["18f2455", 0x1260], ["18f2550", 0x1240], ["18f4455", 0x1202], ["18f4550", 0x1200], ["18f2420", 0x1140],
["18f2520", 0x1100], ["18f4420", 0x10C0], ["18f4520", 0x1080], ["18f4515", 3168])
def getOut():
print "For help use --help"
sys.exit(2)
def main():
try:
options, arguments = getopt.getopt(sys.argv[1:], 'hp:aP:i:levV', ['help', 'port=', 'list', 'erase'])
except getopt.error, msg:
print msg
getOut()
MCU = ""
PORT = ""
FILENAME = ""
ERASE_MODE = False
verbose = False
extraVerbose = False
for opt, arg in options:
if opt in ('-h', '--help'):
helpFile = open("help", "r")
print helpFile.read() + "\n"
sys.exit(0)
elif opt in ('-l', '--list'):
print "Supported MCUs:"
for mcu, i in mcus:
print "pic" + mcu
sys.exit(0)
elif opt in ('-e', '--erase'):
ERASE_MODE = True
elif opt in ('-p'):
MCU = arg
elif opt in ('-P', '--port'):
PORT = arg
elif opt in ('-i'):
FILENAME = arg
elif opt in ('-V'):
extraVerbose = True
verbose = True
elif opt in ('-v'):
verbose = True
if PORT == "":
PORT = '/dev/ttyACM0'
if FILENAME == "" and not ERASE_MODE:
print "You need to select an hex file with -i option"
getOut()
print ("Connecting to arduino..."),
# Open Serial port
try:
arduino = Serial(PORT, 2000000)
except SerialException, msg:
print msg
sys.exit(2)
time.sleep(2)
# Say hello to Arduino
arduino.flushInput()
arduino.write('HX')
if arduino.read() == 'H':
# If Arduino responds, check for the mcu
print ("\tSuccess")
print ("Connecting to the mcu..."),
# checking the MCU
mcu_found = False
# do not probe for MCU if specified on commandline
if MCU != "":
mcu_found = True
if not mcu_found:
arduino.flushInput()
arduino.write('DX') # asking Arduino for the DeviceID
deviceID = ord(arduino.read()) + ord(arduino.read()) * 256
print(deviceID)
for mcu, ID in mcus:
if ID == deviceID:
print "\tYour MCU: " + mcu
mcu_found = True
if mcu_found:
# Perform Bulk Erase
print "Erasing chip............",
arduino.flushInput()
arduino.write('EX')
if arduino.read() == "K":
print "\tSuccess"
if not ERASE_MODE:
# open and parse the hex file
hexFile = Hex(FILENAME)
# Program Memory
print "Programming flash memory...",
if verbose:
print "\n",
address = 0
while address < 0xF000:
if hexFile.haveData(address):
buf = "W"
buf += str(hex(address)[2:].zfill(4))
for j in range(0x20):
buf += str(hex(hexFile.getData(address + j))[2:].zfill(2))
buf += "X"
if verbose:
print buf
arduino.flushInput()
arduino.write(buf.upper())
arduino.read()
address += 0x20
print "\tSuccess"
# Program IDs
if hexFile.haveID():
print "Programming ID memory...",
if verbose:
print "\n",
# ID 0x200000 - 0x200007
address = 0
buf = "W"
buf += str(hex(address + 0x200000)[2:].zfill(6))
for j in range(0x8):
buf += str(hex(hexFile.getID(address + j))[2:].zfill(2))
buf += "X"
if verbose:
print buf
arduino.flushInput()
arduino.write(buf.upper())
arduino.read()
print "\tSuccess"
# Program Data EE
# TODO only some parts have EEPROM
print "Programming EEPROM......",
if verbose:
print "\n",
# EEPROM 0xF00000 - 0xF00100
address = 0
while address < 0x100:
if hexFile.haveEEPROM(address):
buf = "W"
buf += str(hex(address + 0xF00000)[2:].zfill(6))
for j in range(0x20):
buf += str(hex(hexFile.getEEPROM(address + j))[2:].zfill(2))
buf += "X"
if verbose:
print buf
arduino.flushInput()
arduino.write(buf.upper())
arduino.read()
address += 0x20
print "\tSuccess"
# verify Program
print "Verify flash memory...",
verification = 1
if verbose:
print "\n",
address = 0
while address < 0xF000:
if hexFile.haveData(address):
# Send read command
buf = "R"
buf += str(hex(address)[2:].zfill(6))
buf += "X"
arduino.flushInput()
arduino.write((buf).upper())
arduino.read()
# Receive data
buf = ""
r = arduino.read()
while r != "X":
buf += r
r = arduino.read()
buf += r
if verbose:
print buf
# Compare
c, buf = buf[:1], buf[1:]
if c != "R":
print "abort; wrong command received from arduino"
break
#return 1
iAddress, buf = int(buf[:6], 16), buf[6:]
for j in range(0x20):
data, buf = int(buf[:2], 16), buf[2:]
if hexFile.getData(iAddress + j) != data:
print "verification failed "+str(hex(hexFile.getData(iAddress + j)))[2:].zfill(2)+" do not match "+str(hex(data))[2:].zfill(2)
verification = 0
address += 0x20
if verification == 0:
print "\tFailed"
else:
print "\tSuccess"
# verify IDs
print "Verify ID memory...",
verification = 1
if verbose:
print "\n",
if hexFile.haveID():
# Send read command
buf = "R"
buf += str(hex(0x200000)[2:].zfill(6))
buf += "X"
arduino.flushInput()
arduino.write((buf).upper())
arduino.read()
# Receive data
buf = ""
r = arduino.read()
while r != "X":
buf += r
r = arduino.read()
buf += r
if verbose:
print buf
# Compare
c = buf[:1]
if c == "K":
#workaround python serial bug... K received twice!
# remove R
buf = buf[1:]
c = buf[:1]
if c != "R":
print "abort; wrong command received from arduino"
#return 1
# remove R
buf = buf[1:]
iAddress, buf = int(buf[:6], 16), buf[6:]
for j in range(0x8):
data, buf = int(buf[:2], 16), buf[2:]
if hexFile.getID(iAddress - 0x200000 + j) != data:
print "verification failed "+str(hex(hexFile.getID(iAddress - 0x200000 + j)))[2:].zfill(2)+" do not match "+str(hex(data))[2:].zfill(2)
verification = 0
if verification == 0:
print "\tFailed"
else:
print "\tSuccess"
# verify EEPROM Data
print "Verify EEPROM memory...",
verification = 1
if verbose:
print "\n",
address = 0
while address < 0x100:
if hexFile.haveEEPROM(address):
# Send read command
buf = "R"
buf += str(hex(address + 0xF00000)[2:].zfill(6))
buf += "X"
arduino.flushInput()
arduino.write((buf).upper())
arduino.read()
# Receive data
buf = ""
r = arduino.read()
while r != "X":
buf += r
r = arduino.read()
buf += r
if verbose:
print buf
# Compare
c = buf[:1]
if c == "K":
#workaround python serial bug... K received twice!
# remove R
buf = buf[1:]
c = buf[:1]
if c != "R":
print "abort; wrong command received from arduino"
return 1
# remove R
buf = buf[1:]
iAddress, buf = int(buf[:6], 16), buf[6:]
for j in range(0x20):
data, buf = int(buf[:2], 16), buf[2:]
if hexFile.getEEPROM(iAddress - 0xF00000 + j) != data:
print "verification failed "+str(hex(hexFile.getEEPROM(iAddress - 0xF00000 + j)))[2:].zfill(2)+" do not match "+str(hex(data))[2:].zfill(2)
verification = 0
address += 0x20
if verification == 0:
print "\tFailed"
else:
print "\tSuccess"
# program configuration bits
print "Programming the fuse bits...",
if verbose:
print "\n",
for i in range(0x0F):
if hexFile.fuseChanged(i):
buf = "C" + hex(i)[2:] + hex(hexFile.getFuse(i))[2:].zfill(2) + "X"
if verbose:
if extraVerbose:
print "fuse "+str(hex(i))+" changed to "+str(hex(hexFile.getFuse(i)))
print buf
arduino.flushInput()
arduino.write(buf.upper())
arduino.read()
print "\tSuccess"
# verify configuration bits
# TODO
else:
print "Couldn't erase the chip."
sys.exit(1)
else:
print "MCU not recognized. Check the list of compatible MCU'S and/or check your wire conections."
sys.exit(1)
else:
print "Couldn't connect to the Arduino."
sys.exit(2)
arduino.close()
if __name__ == "__main__":
main()