Skip to content

Commit

Permalink
commands: reinit: set exec context attrs to defaults (#1482)
Browse files Browse the repository at this point in the history
After reinitialising the target, also reinit the command execution
context's selected core and mem-ap attributes. To support this, the
relevant code in CommandExecutionContext.attach_session() was extracted
to .set_context_defaults().
  • Loading branch information
flit authored Nov 26, 2022
1 parent 1b991e6 commit 138efda
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions pyocd/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,7 @@ class ReinitCommand(CommandBase):

def execute(self):
self.context.target.init()
self.context.set_context_defaults()

class WhereCommand(CommandBase):
INFO = {
Expand Down
43 changes: 26 additions & 17 deletions pyocd/commands/execution_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,29 +213,38 @@ def attach_session(self, session):

# Select the first core's MEM-AP by default.
if not self._no_init:
try:
# Selected core defaults to the target's default selected core.
if self.selected_core is None:
self.selected_core = self.target.selected_core

# Get the AP for the selected core.
if self.selected_core is not None:
self.selected_ap_address = self.selected_core.ap.address
except IndexError:
pass

# Fall back to the first MEM-AP.
if self.selected_ap_address is None:
for ap_num in sorted(self.target.aps.keys()):
if isinstance(self.target.aps[ap_num], MEM_AP):
self.selected_ap_address = ap_num
break
self.set_context_defaults()

# Add user-defined commands once we know we have a session created.
self.command_set.add_command_group('user')

return True

def set_context_defaults(self) -> None:
"""@brief Sets context attributes to their default values.
Sets the selected core and selected MEM-AP to the default values.
"""
assert self.target

try:
# Selected core defaults to the target's default selected core.
if self.selected_core is None:
self.selected_core = self.target.selected_core

# Get the AP for the selected core.
if self.selected_core is not None:
self.selected_ap_address = self.selected_core.ap.address
except IndexError:
pass

# Fall back to the first MEM-AP.
if self.selected_ap_address is None:
for ap_num in sorted(self.target.aps.keys()):
if isinstance(self.target.aps[ap_num], MEM_AP):
self.selected_ap_address = ap_num
break

@property
def session(self):
return self._session
Expand Down

0 comments on commit 138efda

Please sign in to comment.