23
23
# A simple programmer which works with the ISP protocol on NXP LPC arm
24
24
# processors.
25
25
26
+ import argparse
26
27
import binascii
27
28
import os
28
29
import sys
57
58
INVALID_STOP_BIT = 18
58
59
CODE_READ_PROTECTION_ENABLED = 19
59
60
61
+ parser = argparse .ArgumentParser ()
62
+
63
+
64
+ parser .add_argument (
65
+ '--binary' ,
66
+ required = True ,
67
+ help = 'path to the firmware.bin file you want to program the board with.' )
68
+
69
+ parser .add_argument (
70
+ '--device' ,
71
+ required = True ,
72
+ type = str ,
73
+ default = "" ,
74
+ help = 'Path to serial device file. In linux the name should be '
75
+ 'something similar to "/dev/ttyUSB0", WSL "/dev/ttyS0", and '
76
+ 'Max OSX "/dev/tty-usbserial-AJ20A5".' )
77
+
78
+ parser .add_argument (
79
+ '--udp' ,
80
+ help = 'program processor using Ethernet.' ,
81
+ action = 'store_true' )
82
+
83
+ parser .add_argument (
84
+ '--cpu' ,
85
+ type = str ,
86
+ default = "" ,
87
+ help = 'set the cpu type.' )
88
+
89
+ parser .add_argument (
90
+ '--osfreq' ,
91
+ type = int ,
92
+ default = 0 ,
93
+ help = 'set the oscillator frequency.' )
94
+
95
+ parser .add_argument (
96
+ '--baud' ,
97
+ type = int ,
98
+ nargs = '?' ,
99
+ default = 0 ,
100
+ help = 'set the baud rate.' )
101
+
102
+ parser .add_argument (
103
+ '--xonxoff' ,
104
+ help = 'enable xonxoff flow control.' ,
105
+ action = 'store_true' )
106
+
107
+ parser .add_argument (
108
+ '--control' ,
109
+ help = 'use RTS and DTR to control reset and int0.' ,
110
+ action = 'store_true' )
111
+
112
+ parser .add_argument (
113
+ '--start' ,
114
+ help = 'start the device at a set address.' ,
115
+ action = 'store_true' )
116
+
117
+ parser .add_argument (
118
+ '-v' ,
119
+ '--verbose' ,
120
+ help = 'Enable version debug message output.' ,
121
+ action = 'store_true' )
122
+
123
+ parser .add_argument (
124
+ '--read' ,
125
+ type = str ,
126
+ default = "" ,
127
+ help = 'read from a file.' )
128
+
129
+ parser .add_argument (
130
+ '--len' ,
131
+ type = int ,
132
+ default = 0 ,
133
+ help = 'number of bytes to be read.' )
134
+
135
+ parser .add_argument (
136
+ '--serialnumber' ,
137
+ help = 'get the device serial number.' ,
138
+ action = 'store_true' )
139
+
140
+ parser .add_argument (
141
+ '--list' ,
142
+ help = 'list supported processors.' ,
143
+ action = 'store_true' )
144
+
145
+ parser .add_argument (
146
+ '--addr' ,
147
+ type = str ,
148
+ default = '' ,
149
+ help = 'set the base address for the image.' )
150
+
151
+ parser .add_argument (
152
+ '--verify' ,
153
+ help = 'read the device after programming.' ,
154
+ action = 'store_true' )
155
+
156
+ parser .add_argument (
157
+ '--verifyonly' ,
158
+ help = 'don\' t program, just verify.' ,
159
+ action = 'store_true' )
160
+
161
+ parser .add_argument (
162
+ '--eraseonly' ,
163
+ help = 'don\' t program, just erase. Implies --eraseall.' ,
164
+ action = 'store_true' )
165
+
166
+ parser .add_argument (
167
+ '--eraseall' ,
168
+ help = 'erase all flash not just the area written to.' ,
169
+ action = 'store_true' )
170
+
171
+ parser .add_argument (
172
+ '--blankcheck' ,
173
+ help = 'don\' t program, just check that the flash is blank.' ,
174
+ action = 'store_true' )
175
+
176
+ parser .add_argument (
177
+ '--filetype' ,
178
+ type = str ,
179
+ default = 'bin' ,
180
+ help = 'set filetype to intel hex format or raw binary.' )
181
+
182
+ parser .add_argument (
183
+ '--bank' ,
184
+ type = int ,
185
+ default = 0 ,
186
+ help = 'set filetype to intel hex format or raw binary.' )
187
+
188
+ parser .add_argument (
189
+ '--port' ,
190
+ type = int ,
191
+ default = 41825 ,
192
+ help = 'UDP port number to use (default 41825).' )
193
+
194
+ parser .add_argument (
195
+ '--mac' ,
196
+ type = str ,
197
+ default = '' ,
198
+ help = 'MAC address to associate IP address with.' )
199
+
60
200
# flash sector sizes for lpc23xx/lpc24xx/lpc214x processors
61
201
flash_sector_lpc23xx = (
62
202
4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 ,
@@ -721,7 +861,7 @@ def sync(self, osc):
721
861
s = self .dev_readline ()
722
862
if not s :
723
863
panic ("Sync timeout" )
724
- logging .error ("initial sync =" , s )
864
+ logging .debug ("initial sync = %s" % s )
725
865
if s != self .sync_str :
726
866
panic ("No sync string" )
727
867
@@ -735,7 +875,7 @@ def sync(self, osc):
735
875
elif s == self .OK :
736
876
self .echo_on = False
737
877
else :
738
- logging .debug ("echo sync =" , s )
878
+ logging .debug ("echo sync = %s" % s )
739
879
panic ("No sync string" )
740
880
741
881
if s != self .OK :
@@ -1106,7 +1246,7 @@ def prog_image(self, image, flash_addr_base=0,
1106
1246
self .dev_readline () # offset
1107
1247
success = False
1108
1248
else :
1109
- self .errexit ("'%s' error" % cmd , status )
1249
+ self .errexit ("'%s' '%s' error" % ( cmd , status ) )
1110
1250
1111
1251
return success
1112
1252
@@ -1206,13 +1346,14 @@ def get_serial_number(self):
1206
1346
1207
1347
def main (argv = None ):
1208
1348
global prog
1209
- if argv is None :
1210
- argv = sys . argv
1349
+
1350
+ args = parser . parse_args ()
1211
1351
1212
1352
# defaults
1213
1353
osc_freq = 16000 # kHz
1214
1354
baud = 115200
1215
1355
cpu = "autodetect"
1356
+ filename = ""
1216
1357
flash_addr_base = 0
1217
1358
erase_all = False
1218
1359
erase_only = False
@@ -1232,85 +1373,72 @@ def main(argv=None):
1232
1373
port = - 1
1233
1374
mac = "" # "0C-1D-12-E0-1F-10"
1234
1375
1235
- optlist , args = getopt .getopt (argv [1 :], '' ,
1236
- ['cpu=' , 'oscfreq=' , 'baud=' , 'addr=' , 'start=' ,
1237
- 'filetype=' , 'bank=' , 'read=' , 'len=' , 'serialnumber' ,
1238
- 'udp' , 'port=' , 'mac=' , 'verify' , 'verifyonly' , 'blankcheck' ,
1239
- 'xonxoff' , 'eraseall' , 'eraseonly' , 'list' , 'control' ])
1240
-
1241
- for o , a in optlist :
1242
- if o == "--list" :
1243
- logging .info ("Supported cpus:" )
1244
- for val in sorted (cpu_parms .keys ()):
1245
- logging .info (" %s" % val )
1246
- sys .exit (0 )
1247
- if o == "--cpu" :
1248
- cpu = a
1249
- elif o == "--xonxoff" :
1250
- xonxoff = True
1251
- elif o == "--oscfreq" :
1252
- osc_freq = int (a )
1253
- elif o == "--addr" :
1254
- flash_addr_base = int (a , 0 )
1255
- elif o == "--baud" :
1256
- baud = int (a )
1257
- elif o == "--eraseall" :
1258
- erase_all = True
1259
- elif o == "--eraseonly" :
1260
- erase_only = True
1261
- elif o == "--verbose" :
1262
- verbose = True
1263
- elif o == "--verify" :
1264
- verify = True
1265
- elif o == "--verifyonly" :
1266
- verify = True
1267
- verify_only = True
1268
- elif o == "--blankcheck" :
1269
- verify = True
1270
- blank_check = True
1271
- elif o == "--control" :
1272
- control = True
1273
- elif o == "--filetype" :
1274
- filetype = a
1275
- if not ( filetype == "bin" or filetype == "ihex" ):
1276
- panic ("Invalid filetype: %s" % filetype )
1277
- elif o == "--start" :
1278
- start = True
1279
- if a :
1280
- startaddr = int (a , 0 )
1281
- else :
1282
- startaddr = 0
1283
- elif o == "--bank" :
1284
- select_bank = True
1285
- bank = int (a )
1286
- elif o == "--read" :
1287
- read = True
1288
- readfile = a
1289
- elif o == "--serialnumber" :
1290
- get_serial_number = True
1291
- elif o == "--len" :
1292
- readlen = int (a )
1293
- elif o == "--udp" :
1294
- udp = True
1295
- elif o == "--port" :
1296
- port = int (a )
1297
- elif o == "--mac" :
1298
- mac = a
1299
- else :
1300
- panic ("Unhandled option: %s" % o )
1301
-
1302
- if cpu != "autodetect" and not cpu in cpu_parms :
1303
- panic ("Unsupported cpu %s" % cpu )
1304
-
1305
- if len (args ) == 0 :
1306
- syntax ()
1307
-
1308
- if verbose :
1376
+ if args .list :
1377
+ logging .info ("Supported cpus:" )
1378
+ for val in sorted (cpu_parms .keys ()):
1379
+ logging .info (" %s" % val )
1380
+ sys .exit (0 )
1381
+ if args .verbose :
1309
1382
logging .basicConfig (stream = sys .stdout , level = logging .DEBUG )
1310
1383
else :
1311
1384
logging .basicConfig (stream = sys .stdout , level = logging .INFO )
1385
+ if args .binary :
1386
+ filename = args .binary
1387
+ if args .device :
1388
+ device = args .device
1389
+ if args .cpu :
1390
+ cpu = args .cpu
1391
+ if args .xonxoff :
1392
+ xonxoff = True
1393
+ if args .osfreq :
1394
+ os_freq = args .osfreq
1395
+ if args .addr :
1396
+ addr = int (args .addr , 0 )
1397
+ if args .baud :
1398
+ baud = args .baud
1399
+ if args .eraseall :
1400
+ erase_all = args .eraseall
1401
+ if args .eraseonly :
1402
+ erase_only = args .eraseonly
1403
+ if args .verify :
1404
+ verify = True
1405
+ if args .verifyonly :
1406
+ verify = True
1407
+ verify_only = True
1408
+ if args .blankcheck :
1409
+ verify = True
1410
+ blank_check = True
1411
+ if args .control :
1412
+ control = True
1413
+ if args .filetype :
1414
+ filetype = args .filetype
1415
+ if not (filetype == "bin" or filetype == "ihex" ):
1416
+ panic ("Invalid filetype: %s" % filetype )
1417
+ if args .start :
1418
+ start = True
1419
+ if args .addr :
1420
+ startaddr = int (args .addr , 0 )
1421
+ else :
1422
+ startaddr = 0
1423
+ if args .bank :
1424
+ select_bank = True
1425
+ bank = args .bank
1426
+ if args .read :
1427
+ read = True
1428
+ readfile = a
1429
+ if args .serialnumber :
1430
+ get_serial_number = True
1431
+ if args .len :
1432
+ readlen = args .len
1433
+ if args .udp :
1434
+ udp = True
1435
+ if args .port :
1436
+ port = int (args .port )
1437
+ if args .mac :
1438
+ mac = args .mac
1312
1439
1313
- device = args [0 ]
1440
+ if cpu != "autodetect" and not cpu in cpu_parms :
1441
+ panic ("Unsupported cpu %s" % cpu )
1314
1442
1315
1443
if udp :
1316
1444
if '.' in device :
@@ -1361,10 +1489,6 @@ def main(argv=None):
1361
1489
prog .read_block (flash_addr_base , readlen , fd )
1362
1490
fd .close ()
1363
1491
else :
1364
- if len (args ) != 2 :
1365
- syntax ()
1366
-
1367
- filename = args [1 ]
1368
1492
1369
1493
if filetype == "autodetect" :
1370
1494
filetype = "ihex" if filename .endswith ('hex' ) else "bin"
0 commit comments