Skip to content

Commit f10557c

Browse files
authored
Merge pull request #1025 from flit/bugfix/cmd_no_init_fixes
Commander --no-init fixes
2 parents 9ccbf2e + 9ab9819 commit f10557c

File tree

4 files changed

+83
-14
lines changed

4 files changed

+83
-14
lines changed

pyocd/commands/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ def _convert_value(self, arg):
125125
try:
126126
deref = (arg[0] == '[')
127127
if deref:
128+
if not self.context.selected_core:
129+
raise ToolError("cannot dereference when memory is not accessible")
128130
arg = arg[1:-1]
129131
offset = 0
130132
if ',' in arg:
@@ -133,7 +135,7 @@ def _convert_value(self, arg):
133135
offset = int(offset.strip(), base=0)
134136

135137
value = None
136-
if arg.lower() in self.context.selected_core.core_registers.by_name:
138+
if (self.context.selected_core) and (arg.lower() in self.context.selected_core.core_registers.by_name):
137139
value = self.context.selected_core.read_core_register(arg.lower())
138140
self.context.writei("%s = 0x%08x", arg.lower(), value)
139141
else:

pyocd/commands/commands.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ class MakeApCommand(CommandBase):
12681268
# TODO support ADIv6 AP addresses
12691269
def parse(self, args):
12701270
self.apsel = self._convert_value(args[0])
1271-
self.ap_addr = coresight.ap.APv1Address(apsel)
1271+
self.ap_addr = coresight.ap.APv1Address(self.apsel)
12721272

12731273
def execute(self):
12741274
if self.ap_addr in self.context.target.aps:
@@ -1463,15 +1463,20 @@ def parse(self, args):
14631463
self.args = args[1:]
14641464

14651465
def execute(self):
1466+
# Look up value class by name.
14661467
try:
14671468
value_class = self.context.command_set.values[self.name]
1468-
if 'r' not in value_class.INFO['access']:
1469-
raise exceptions.CommandError("value '%s' is not readable" % self.name)
1470-
value_object = value_class(self.context)
1471-
value_object.display(self.args)
14721469
except KeyError:
14731470
raise exceptions.CommandError("unknown value name '%s'" % self.name)
14741471

1472+
# Check readability.
1473+
if 'r' not in value_class.INFO['access']:
1474+
raise exceptions.CommandError("value '%s' is not readable" % self.name)
1475+
1476+
# Execute show operation.
1477+
value_object = value_class(self.context)
1478+
value_object.display(self.args)
1479+
14751480
@classmethod
14761481
def format_help(cls, context, max_width=72):
14771482
text = "Usage: {cmd} {usage}\n".format(cmd=cls.INFO['names'][0], usage=cls.INFO['usage'])
@@ -1504,15 +1509,20 @@ def parse(self, args):
15041509
self.args = args[1:]
15051510

15061511
def execute(self):
1512+
# Look up value class by name.
15071513
try:
15081514
value_class = self.context.command_set.values[self.name]
1509-
if 'w' not in value_class.INFO['access']:
1510-
raise exceptions.CommandError("value '%s' is not modifiable" % self.name)
1511-
value_object = value_class(self.context)
1512-
value_object.modify(self.args)
15131515
except KeyError:
15141516
raise exceptions.CommandError("unknown value name '%s'" % self.name)
15151517

1518+
# Check writability.
1519+
if 'w' not in value_class.INFO['access']:
1520+
raise exceptions.CommandError("value '%s' is not modifiable" % self.name)
1521+
1522+
# Execute set operation.
1523+
value_object = value_class(self.context)
1524+
value_object.modify(self.args)
1525+
15161526
@classmethod
15171527
def format_help(cls, context, max_width=72):
15181528
text = "Usage: {cmd} {usage}\n".format(cmd=cls.INFO['names'][0], usage=cls.INFO['usage'])
@@ -1563,7 +1573,7 @@ def execute(self):
15631573
", ".join("'%s'" % c for c in matched_commands))
15641574
return
15651575
elif len(matched_commands) == 0:
1566-
raise exceptions.CommandError("Error: unrecognized command '%s'", cmd_name)
1576+
raise exceptions.CommandError("Error: unrecognized command '%s'" % cmd_name)
15671577
cmd_name = matched_commands[0]
15681578
cmd_class = self.context.command_set.commands[cmd_name]
15691579

@@ -1577,7 +1587,7 @@ def execute(self):
15771587
", ".join("'%s'" % c for c in matched_values))
15781588
return
15791589
elif len(matched_values) == 0:
1580-
raise exceptions.CommandError("Error: unrecognized value '%s'", value_name)
1590+
raise exceptions.CommandError("Error: unrecognized value '%s'" % value_name)
15811591
value_name = matched_values[0]
15821592
cmd_class = self.context.command_set.values[value_name]
15831593
self.context.write(cmd_class.format_help(self.context, self.term_width))

pyocd/commands/execution_context.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import subprocess
2424

2525
from ..core import exceptions
26+
from ..coresight.ap import MEM_AP
2627
from ..utility.compatibility import get_terminal_size
2728
from ..utility.cmdline import (
2829
split_command_line,
@@ -288,7 +289,10 @@ def selected_ap_address(self, value):
288289

289290
@property
290291
def selected_ap(self):
291-
return self.target.aps[self.selected_ap_address]
292+
if self.selected_ap_address is None:
293+
return None
294+
else:
295+
return self.target.aps[self.selected_ap_address]
292296

293297
def process_command_line(self, line):
294298
"""! @brief Run a command line consisting of one or more semicolon-separated commands."""

pyocd/commands/values.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,26 @@ def display(self, args):
9696
for i, core in self.context.target.cores.items():
9797
self.context.writei("Core %d type: %s%s", i,
9898
coresight.core_ids.CORE_TYPE_NAME[core.core_type],
99-
" (selected)" if (self.context.selected_core.core_number == i) else "")
99+
" (selected)" if ((self.context.selected_core is not None) \
100+
and (self.context.selected_core.core_number == i)) else "")
101+
102+
class APsValue(ValueBase):
103+
INFO = {
104+
'names': ['aps'],
105+
'group': 'standard',
106+
'category': 'target',
107+
'access': 'r',
108+
'help': "List discovered Access Ports.",
109+
}
110+
111+
def display(self, args):
112+
if self.context.target.is_locked():
113+
self.context.write("Target is locked")
114+
else:
115+
self.context.writei("%d APs:", len(self.context.target.aps))
116+
for addr, ap in sorted(self.context.target.aps.items(), key=lambda x: x[0]):
117+
self.context.writei("%s: %s%s", addr, ap.type_name,
118+
" (selected)" if (self.context.selected_ap_address == addr) else "")
100119

101120
class MemoryMapValue(ValueBase):
102121
INFO = {
@@ -108,6 +127,10 @@ class MemoryMapValue(ValueBase):
108127
}
109128

110129
def display(self, args):
130+
if self.context.selected_core is None:
131+
self.context.write("No core is selected")
132+
return
133+
111134
pt = prettytable.PrettyTable(["Region", "Type", "Start", "End", "Size", "Access", "Sector", "Page"])
112135
pt.align = 'l'
113136
pt.border = False
@@ -205,6 +228,10 @@ def print_fields(regname, value, fields, showAll):
205228
if showAll or bit != 0:
206229
self.context.writei(" %s = 0x%x", name, bit)
207230

231+
if self.context.selected_core is None:
232+
self.context.write("No core is selected")
233+
return
234+
208235
cfsr = self.context.selected_core.read32(CFSR)
209236
mmfsr = cfsr & 0xff
210237
bfsr = (cfsr >> 8) & 0xff
@@ -292,6 +319,9 @@ class MemApValue(ValueBase):
292319
}
293320

294321
def display(self, args):
322+
if self.context.selected_ap is None:
323+
self.context.write("No MEM-AP is selected")
324+
return
295325
self.context.writef("{} is selected", self.context.selected_ap.short_description)
296326

297327
def modify(self, args):
@@ -322,6 +352,9 @@ class HnonsecValue(ValueBase):
322352
}
323353

324354
def display(self, args):
355+
if self.context.selected_ap is None:
356+
self.context.write("No MEM-AP is selected")
357+
return
325358
self.context.writef("{} HNONSEC = {} ({})",
326359
self.context.selected_ap.short_description,
327360
self.context.selected_ap.hnonsec,
@@ -330,6 +363,9 @@ def display(self, args):
330363
def modify(self, args):
331364
if len(args) == 0:
332365
raise exceptions.CommandError("missing argument")
366+
if self.context.selected_ap is None:
367+
self.context.write("No MEM-AP is selected")
368+
return
333369
value = int(args[0], base=0)
334370
self.context.selected_ap.hnonsec = value
335371

@@ -343,6 +379,9 @@ class HprotValue(ValueBase):
343379
}
344380

345381
def display(self, args):
382+
if self.context.selected_ap is None:
383+
self.context.write("No MEM-AP is selected")
384+
return
346385
hprot = self.context.selected_ap.hprot
347386
self.context.writef("{} HPROT = {:#x}",
348387
self.context.selected_ap.short_description,
@@ -359,6 +398,9 @@ def display(self, args):
359398
def modify(self, args):
360399
if len(args) == 0:
361400
raise exceptions.CommandError("missing argument")
401+
if self.context.selected_ap is None:
402+
self.context.write("No MEM-AP is selected")
403+
return
362404
value = int(args[0], base=0)
363405
self.context.selected_ap.hprot = value
364406

@@ -399,6 +441,9 @@ class RegisterGroupsValue(ValueBase):
399441
}
400442

401443
def display(self, args):
444+
if self.context.selected_core is None:
445+
self.context.write("No core is selected")
446+
return
402447
for g in sorted(self.context.selected_core.core_registers.groups):
403448
self.context.write(g)
404449

@@ -415,6 +460,10 @@ class VectorCatchValue(ValueBase):
415460
}
416461

417462
def display(self, args):
463+
if self.context.selected_core is None:
464+
self.context.write("No core is selected")
465+
return
466+
418467
catch = self.context.selected_core.get_vector_catch()
419468

420469
self.context.write("Vector catch:")
@@ -426,6 +475,10 @@ def display(self, args):
426475
def modify(self, args):
427476
if len(args) == 0:
428477
raise exceptions.CommandError("missing vector catch setting")
478+
479+
if self.context.selected_core is None:
480+
self.context.write("No core is selected")
481+
return
429482

430483
try:
431484
self.context.selected_core.set_vector_catch(convert_vector_catch(args[0]))

0 commit comments

Comments
 (0)