1# Copyright (c) 2017 Linaro Limited. 2# 3# SPDX-License-Identifier: Apache-2.0 4 5'''Runner for pyOCD .''' 6 7import os 8from os import path 9 10from runners.core import ZephyrBinaryRunner, RunnerCaps, \ 11 BuildConfiguration 12 13DEFAULT_PYOCD_GDB_PORT = 3333 14DEFAULT_PYOCD_TELNET_PORT = 4444 15 16 17class PyOcdBinaryRunner(ZephyrBinaryRunner): 18 '''Runner front-end for pyOCD.''' 19 20 def __init__(self, cfg, target, 21 pyocd='pyocd', 22 flash_addr=0x0, erase=False, flash_opts=None, 23 gdb_port=DEFAULT_PYOCD_GDB_PORT, 24 telnet_port=DEFAULT_PYOCD_TELNET_PORT, tui=False, 25 pyocd_config=None, 26 board_id=None, daparg=None, frequency=None, tool_opt=None): 27 super().__init__(cfg) 28 29 default = path.join(cfg.board_dir, 'support', 'pyocd.yaml') 30 if path.exists(default): 31 self.pyocd_config = default 32 else: 33 self.pyocd_config = None 34 35 36 self.target_args = ['-t', target] 37 self.pyocd = pyocd 38 self.flash_addr_args = ['-a', hex(flash_addr)] if flash_addr else [] 39 self.erase = erase 40 self.gdb_cmd = [cfg.gdb] if cfg.gdb is not None else None 41 self.gdb_port = gdb_port 42 self.telnet_port = telnet_port 43 self.tui_args = ['-tui'] if tui else [] 44 self.hex_name = cfg.hex_file 45 self.bin_name = cfg.bin_file 46 self.elf_name = cfg.elf_file 47 48 pyocd_config_args = [] 49 50 if self.pyocd_config is not None: 51 pyocd_config_args = ['--config', self.pyocd_config] 52 53 self.pyocd_config_args = pyocd_config_args 54 55 board_args = [] 56 if board_id is not None: 57 board_args = ['-u', board_id] 58 self.board_args = board_args 59 60 daparg_args = [] 61 if daparg is not None: 62 daparg_args = ['-da', daparg] 63 self.daparg_args = daparg_args 64 65 frequency_args = [] 66 if frequency is not None: 67 frequency_args = ['-f', frequency] 68 self.frequency_args = frequency_args 69 70 tool_opt_args = [] 71 if tool_opt is not None: 72 tool_opt_args = [tool_opt] 73 self.tool_opt_args = tool_opt_args 74 75 self.flash_extra = flash_opts if flash_opts else [] 76 77 @classmethod 78 def name(cls): 79 return 'pyocd' 80 81 @classmethod 82 def capabilities(cls): 83 return RunnerCaps(commands={'flash', 'debug', 'debugserver', 'attach'}, 84 flash_addr=True, erase=True) 85 86 @classmethod 87 def do_add_parser(cls, parser): 88 parser.add_argument('--target', required=True, 89 help='target override') 90 91 parser.add_argument('--daparg', 92 help='Additional -da arguments to pyocd tool') 93 parser.add_argument('--pyocd', default='pyocd', 94 help='path to pyocd tool, default is pyocd') 95 parser.add_argument('--flash-opt', default=[], action='append', 96 help='''Additional options for pyocd flash, 97 e.g. --flash-opt="-e=chip" to chip erase''') 98 parser.add_argument('--frequency', 99 help='SWD clock frequency in Hz') 100 parser.add_argument('--gdb-port', default=DEFAULT_PYOCD_GDB_PORT, 101 help='pyocd gdb port, defaults to {}'.format( 102 DEFAULT_PYOCD_GDB_PORT)) 103 parser.add_argument('--telnet-port', default=DEFAULT_PYOCD_TELNET_PORT, 104 help='pyocd telnet port, defaults to {}'.format( 105 DEFAULT_PYOCD_TELNET_PORT)) 106 parser.add_argument('--tui', default=False, action='store_true', 107 help='if given, GDB uses -tui') 108 parser.add_argument('--board-id', 109 help='ID of board to flash, default is to prompt') 110 parser.add_argument('--tool-opt', 111 help='''Additional options for pyocd Commander, 112 e.g. \'--script=user.py\' ''') 113 114 @classmethod 115 def do_create(cls, cfg, args): 116 build_conf = BuildConfiguration(cfg.build_dir) 117 flash_addr = cls.get_flash_address(args, build_conf) 118 119 ret = PyOcdBinaryRunner( 120 cfg, args.target, 121 pyocd=args.pyocd, 122 flash_addr=flash_addr, erase=args.erase, flash_opts=args.flash_opt, 123 gdb_port=args.gdb_port, telnet_port=args.telnet_port, tui=args.tui, 124 board_id=args.board_id, daparg=args.daparg, 125 frequency=args.frequency, 126 tool_opt=args.tool_opt) 127 128 daparg = os.environ.get('PYOCD_DAPARG') 129 if not ret.daparg_args and daparg: 130 ret.logger.warning('PYOCD_DAPARG is deprecated; use --daparg') 131 ret.logger.debug('--daparg={} via PYOCD_DAPARG'.format(daparg)) 132 ret.daparg_args = ['-da', daparg] 133 134 return ret 135 136 def port_args(self): 137 return ['-p', str(self.gdb_port), '-T', str(self.telnet_port)] 138 139 def do_run(self, command, **kwargs): 140 self.require(self.pyocd) 141 if command == 'flash': 142 self.flash(**kwargs) 143 else: 144 self.debug_debugserver(command, **kwargs) 145 146 def flash(self, **kwargs): 147 if self.hex_name is not None and os.path.isfile(self.hex_name): 148 fname = self.hex_name 149 elif self.bin_name is not None and os.path.isfile(self.bin_name): 150 self.logger.warning( 151 'hex file ({}) does not exist; falling back on .bin ({}). '. 152 format(self.hex_name, self.bin_name) + 153 'Consider enabling CONFIG_BUILD_OUTPUT_HEX.') 154 fname = self.bin_name 155 else: 156 raise ValueError( 157 'Cannot flash; no hex ({}) or bin ({}) files found. '.format( 158 self.hex_name, self.bin_name)) 159 160 erase_method = 'chip' if self.erase else 'sector' 161 162 cmd = ([self.pyocd] + 163 ['flash'] + 164 self.pyocd_config_args + 165 ['-e', erase_method] + 166 self.flash_addr_args + 167 self.daparg_args + 168 self.target_args + 169 self.board_args + 170 self.frequency_args + 171 self.tool_opt_args + 172 self.flash_extra + 173 [fname]) 174 175 self.logger.info('Flashing file: {}'.format(fname)) 176 self.check_call(cmd) 177 178 def log_gdbserver_message(self): 179 self.logger.info('pyOCD GDB server running on port {}'. 180 format(self.gdb_port)) 181 182 def debug_debugserver(self, command, **kwargs): 183 server_cmd = ([self.pyocd] + 184 ['gdbserver'] + 185 self.daparg_args + 186 self.port_args() + 187 self.target_args + 188 self.board_args + 189 self.frequency_args + 190 self.tool_opt_args) 191 192 if command == 'debugserver': 193 self.log_gdbserver_message() 194 self.check_call(server_cmd) 195 else: 196 if self.gdb_cmd is None: 197 raise ValueError('Cannot debug; gdb is missing') 198 if self.elf_name is None: 199 raise ValueError('Cannot debug; elf is missing') 200 client_cmd = (self.gdb_cmd + 201 self.tui_args + 202 [self.elf_name] + 203 ['-ex', 'target remote :{}'.format(self.gdb_port)]) 204 if command == 'debug': 205 client_cmd += ['-ex', 'monitor halt', 206 '-ex', 'monitor reset', 207 '-ex', 'load'] 208 209 self.require(client_cmd[0]) 210 self.log_gdbserver_message() 211 self.run_server_and_client(server_cmd, client_cmd) 212