diff --git a/scripts/west_commands/runners/openocd.py b/scripts/west_commands/runners/openocd.py index 36d7bfcba0c..8b98fe67ec7 100644 --- a/scripts/west_commands/runners/openocd.py +++ b/scripts/west_commands/runners/openocd.py @@ -16,9 +16,8 @@ DEFAULT_OPENOCD_GDB_PORT = 3333 class OpenOcdBinaryRunner(ZephyrBinaryRunner): '''Runner front-end for openocd.''' - def __init__(self, cfg, - pre_init_cmd=None, pre_cmd=None, load_cmd=None, - verify_cmd=None, post_cmd=None, + def __init__(self, cfg, pre_init=None, pre_load=None, + load_cmd=None, verify_cmd=None, post_verify=None, tui=None, config=None, tcl_port=DEFAULT_OPENOCD_TCL_PORT, telnet_port=DEFAULT_OPENOCD_TELNET_PORT, @@ -37,11 +36,11 @@ class OpenOcdBinaryRunner(ZephyrBinaryRunner): self.openocd_cmd = [cfg.openocd] + search_args self.hex_name = cfg.hex_file self.elf_name = cfg.elf_file + self.pre_init = pre_init or [] + self.pre_load = pre_load or [] self.load_cmd = load_cmd self.verify_cmd = verify_cmd - self.pre_init_cmd = pre_init_cmd - self.pre_cmd = pre_cmd - self.post_cmd = post_cmd + self.post_verify = post_verify or [] self.tcl_port = tcl_port self.telnet_port = telnet_port self.gdb_port = gdb_port @@ -57,17 +56,20 @@ class OpenOcdBinaryRunner(ZephyrBinaryRunner): parser.add_argument('--config', help='if given, override default config file') # Options for flashing: - parser.add_argument('--cmd-pre-init', - help='Command to run before calling init') - parser.add_argument('--cmd-pre-load', - help='Command to run before flashing') + parser.add_argument('--cmd-pre-init', action='append', + help='''Command to run before calling init; + may be given multiple times''') + parser.add_argument('--cmd-pre-load', action='append', + help='''Command to run before flashing; + may be given multiple times''') parser.add_argument('--cmd-load', help='''Command to load/flash binary (required when flashing)''') parser.add_argument('--cmd-verify', help='''Command to verify flashed binary''') - parser.add_argument('--cmd-post-verify', - help='Command to run after verification') + parser.add_argument('--cmd-post-verify', action='append', + help='''Command to run after verification; + may be given multiple times''') # Options for debugging: parser.add_argument('--tui', default=False, action='store_true', @@ -84,9 +86,9 @@ class OpenOcdBinaryRunner(ZephyrBinaryRunner): def create(cls, cfg, args): return OpenOcdBinaryRunner( cfg, - pre_init_cmd=args.cmd_pre_init, - pre_cmd=args.cmd_pre_load, load_cmd=args.cmd_load, - verify_cmd=args.cmd_verify, post_cmd=args.cmd_post_verify, + pre_init=args.cmd_pre_init, + pre_load=args.cmd_pre_load, load_cmd=args.cmd_load, + verify_cmd=args.cmd_verify, post_verify=args.cmd_post_verify, tui=args.tui, config=args.config, tcl_port=args.tcl_port, telnet_port=args.telnet_port, gdb_port=args.gdb_port) @@ -115,30 +117,15 @@ class OpenOcdBinaryRunner(ZephyrBinaryRunner): if self.verify_cmd is None: raise ValueError('Cannot flash; verify command is missing') - pre_cmd = [] - if self.pre_cmd is not None: - pre_cmd = ['-c', self.pre_cmd] - - pre_init_cmd = [] - if self.pre_init_cmd is not None: - pre_init_cmd = ['-c', self.pre_init_cmd] - - post_cmd = [] - if self.post_cmd is not None: - post_cmd = ['-c', self.post_cmd] - self.logger.info('Flashing file: {}'.format(self.hex_name)) - cmd = (self.openocd_cmd + - self.cfg_cmd + - pre_init_cmd + - ['-c', 'init', - '-c', 'targets'] + - pre_cmd + - ['-c', 'reset halt', - '-c', self.load_cmd + ' ' + self.hex_name, - '-c', 'reset halt', - '-c', self.verify_cmd + ' ' + self.hex_name] + - post_cmd + + cmd = (self.openocd_cmd + self.cfg_cmd + + self.pre_init + ['-c', 'init', + '-c', 'targets'] + + self.pre_load + ['-c', 'reset halt', + '-c', self.load_cmd + ' ' + self.hex_name, + '-c', 'reset halt'] + + ['-c', self.verify_cmd + ' ' + self.hex_name] + + self.post_verify + ['-c', 'reset run', '-c', 'shutdown']) self.check_call(cmd) @@ -149,14 +136,13 @@ class OpenOcdBinaryRunner(ZephyrBinaryRunner): if self.elf_name is None: raise ValueError('Cannot debug; no .elf specified') - server_cmd = (self.openocd_cmd + - self.cfg_cmd + + server_cmd = (self.openocd_cmd + self.cfg_cmd + ['-c', 'tcl_port {}'.format(self.tcl_port), '-c', 'telnet_port {}'.format(self.telnet_port), - '-c', 'gdb_port {}'.format(self.gdb_port), - '-c', 'init', - '-c', 'targets', - '-c', 'halt']) + '-c', 'gdb_port {}'.format(self.gdb_port)] + + self.pre_init + ['-c', 'init', + '-c', 'targets', + '-c', 'halt']) gdb_cmd = (self.gdb_cmd + self.tui_arg + ['-ex', 'target remote :{}'.format(self.gdb_port), self.elf_name]) @@ -164,9 +150,11 @@ class OpenOcdBinaryRunner(ZephyrBinaryRunner): self.run_server_and_client(server_cmd, gdb_cmd) def do_debugserver(self, **kwargs): - cmd = (self.openocd_cmd + - self.cfg_cmd + - ['-c', 'init', - '-c', 'targets', - '-c', 'reset halt']) + cmd = (self.openocd_cmd + self.cfg_cmd + + ['-c', 'tcl_port {}'.format(self.tcl_port), + '-c', 'telnet_port {}'.format(self.telnet_port), + '-c', 'gdb_port {}'.format(self.gdb_port)] + + self.pre_init + ['-c', 'init', + '-c', 'targets', + '-c', 'reset halt']) self.check_call(cmd)