Skip to content

Add OpenOCD Serial Number Flag #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tockloader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,11 @@ def main():
default=None,
help="Specify a specific JLink via serial number. Useful when multiple JLinks are connected to the same machine.",
)
parent_channel.add_argument(
"--openocd-serial-number",
default=None,
help="Specify a specific board via serial number when using OpenOCD. Useful when multiple identical boards are connected.",
)
parent_channel.add_argument(
"--openocd-board", help="The cfg file in OpenOCD `board` folder."
)
Expand Down
24 changes: 18 additions & 6 deletions tockloader/openocd.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def __init__(self, args):
# Command can be passed in as an argument, otherwise use default.
self.openocd_cmd = getattr(self.args, "openocd_cmd")

# Store the serial number if provided
self.openocd_serial_number = getattr(self.args, "openocd_serial_number")

def attached_board_exists(self):
# Get a list of attached devices, check if that list has at least
# one entry.
Expand Down Expand Up @@ -132,8 +135,14 @@ def _gather_openocd_cmdline(self, commands, binary, write=True, exit=True):
# Defaults.
prefix = ""
source = "source [find board/{board}];".format(board=self.openocd_board)

cmd_prefix = "init; reset init; halt;"
cmd_suffix = ""
serial_no_cmd = ""

# Add serial number specification if provided
if hasattr(self, "openocd_serial_number") and self.openocd_serial_number:
serial_no_cmd = "adapter serial {};".format(self.openocd_serial_number)

# Do the customizations
if "workareazero" in self.openocd_options:
Expand All @@ -151,12 +160,15 @@ def _gather_openocd_cmdline(self, commands, binary, write=True, exit=True):
if exit:
cmd_suffix += "exit"

command_param = "{prefix} {source} {cmd_prefix} {cmd} {cmd_suffix}".format(
prefix=prefix,
source=source,
cmd_prefix=cmd_prefix,
cmd="; ".join(commands),
cmd_suffix=cmd_suffix,
command_param = (
"{prefix} {serial_no_cmd} {source} {cmd_prefix} {cmd} {cmd_suffix}".format(
prefix=prefix,
serial_no_cmd=serial_no_cmd,
source=source,
cmd_prefix=cmd_prefix,
cmd="; ".join(commands),
cmd_suffix=cmd_suffix,
)
)

return (
Expand Down
Loading