Skip to content

Commit 506e9b0

Browse files
committed
Refactored argument parsing to use argparse
Now nxpprog makes use of the argparse module for all command line arguments.
1 parent ff5a6f2 commit 506e9b0

File tree

1 file changed

+208
-84
lines changed

1 file changed

+208
-84
lines changed

nxpprog.py

Lines changed: 208 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# A simple programmer which works with the ISP protocol on NXP LPC arm
2424
# processors.
2525

26+
import argparse
2627
import binascii
2728
import os
2829
import sys
@@ -57,6 +58,145 @@
5758
INVALID_STOP_BIT = 18
5859
CODE_READ_PROTECTION_ENABLED = 19
5960

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+
60200
# flash sector sizes for lpc23xx/lpc24xx/lpc214x processors
61201
flash_sector_lpc23xx = (
62202
4, 4, 4, 4, 4, 4, 4, 4,
@@ -721,7 +861,7 @@ def sync(self, osc):
721861
s = self.dev_readline()
722862
if not s:
723863
panic("Sync timeout")
724-
logging.error("initial sync =", s)
864+
logging.debug("initial sync = %s" % s)
725865
if s != self.sync_str:
726866
panic("No sync string")
727867

@@ -735,7 +875,7 @@ def sync(self, osc):
735875
elif s == self.OK:
736876
self.echo_on = False
737877
else:
738-
logging.debug("echo sync =", s)
878+
logging.debug("echo sync = %s" % s)
739879
panic("No sync string")
740880

741881
if s != self.OK:
@@ -1106,7 +1246,7 @@ def prog_image(self, image, flash_addr_base=0,
11061246
self.dev_readline() # offset
11071247
success = False
11081248
else:
1109-
self.errexit("'%s' error" % cmd, status)
1249+
self.errexit("'%s' '%s' error" % (cmd, status))
11101250

11111251
return success
11121252

@@ -1206,13 +1346,14 @@ def get_serial_number(self):
12061346

12071347
def main(argv=None):
12081348
global prog
1209-
if argv is None:
1210-
argv = sys.argv
1349+
1350+
args = parser.parse_args()
12111351

12121352
# defaults
12131353
osc_freq = 16000 # kHz
12141354
baud = 115200
12151355
cpu = "autodetect"
1356+
filename = ""
12161357
flash_addr_base = 0
12171358
erase_all = False
12181359
erase_only = False
@@ -1232,85 +1373,72 @@ def main(argv=None):
12321373
port = -1
12331374
mac = "" # "0C-1D-12-E0-1F-10"
12341375

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:
13091382
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
13101383
else:
13111384
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
13121439

1313-
device = args[0]
1440+
if cpu != "autodetect" and not cpu in cpu_parms:
1441+
panic("Unsupported cpu %s" % cpu)
13141442

13151443
if udp:
13161444
if '.' in device:
@@ -1361,10 +1489,6 @@ def main(argv=None):
13611489
prog.read_block(flash_addr_base, readlen, fd)
13621490
fd.close()
13631491
else:
1364-
if len(args) != 2:
1365-
syntax()
1366-
1367-
filename = args[1]
13681492

13691493
if filetype == "autodetect":
13701494
filetype = "ihex" if filename.endswith('hex') else "bin"

0 commit comments

Comments
 (0)