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