2 * Copyright (c) 2010 by David Brownell
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * Infrastructure for specifying and managing the transport protocol
24 * used in a given debug or programming session.
26 * Examples of "debug-capable" transports are JTAG or SWD.
27 * Additionally, JTAG supports boundary scan testing.
29 * Examples of "programming-capable" transports include SPI or UART;
30 * those are used (often mediated by a ROM bootloader) for ISP style
31 * programming, to perform an initial load of code into flash, or
32 * sometimes into SRAM. Target code could use "variant" options to
33 * decide how to use such protocols. For example, Cortex-M3 cores
34 * from TI/Luminary and from NXP use different protocols for for
35 * UART or SPI based firmware loading.
37 * As a rule, there are protocols layered on top of the transport.
38 * For example, different chip families use JTAG in different ways
39 * for debugging. Also, each family that supports programming over
40 * a UART link for initial firmware loading tends to define its own
41 * messaging and error handling.
44 #include <helper/log.h>
45 #include <transport/transport.h>
47 extern struct command_context *global_cmd_ctx;
49 /*-----------------------------------------------------------------------*/
52 * Infrastructure internals
55 /** List of transports known to OpenOCD. */
56 static struct transport *transport_list;
59 * NULL-terminated Vector of names of transports which the
60 * currently selected debug adapter supports. This is declared
61 * by the time that adapter is fully set up.
63 static const char * const *allowed_transports;
65 /** * The transport being used for the current OpenOCD session. */
66 static struct transport *session;
68 static int transport_select(struct command_context *ctx, const char *name)
70 /* name may only identify a known transport;
71 * caller guarantees session's transport isn't yet set.*/
72 for (struct transport *t = transport_list; t; t = t->next) {
73 if (strcmp(t->name, name) == 0) {
74 int retval = t->select(ctx);
75 /* select() registers commands specific to this
76 * transport, and may also reset the link, e.g.
77 * forcing it to JTAG or SWD mode.
79 if (retval == ERROR_OK)
82 LOG_ERROR("Error selecting '%s' as transport", t->name);
87 LOG_ERROR("No transport named '%s' is available.", name);
92 * Called by debug adapter drivers, or affiliated Tcl config scripts,
93 * to declare the set of transports supported by an adapter. When
94 * there is only one member of that set, it is automatically selected.
96 int allow_transports(struct command_context *ctx, const char * const *vector)
98 /* NOTE: caller is required to provide only a list
99 * of *valid* transport names
101 * REVISIT should we validate that? and insist there's
102 * at least one non-NULL element in that list?
104 * ... allow removals, e.g. external strapping prevents use
105 * of one transport; C code should be definitive about what
106 * can be used when all goes well.
108 if (allowed_transports != NULL || session) {
109 LOG_ERROR("Can't modify the set of allowed transports.");
113 allowed_transports = vector;
115 /* autoselect if there's no choice ... */
117 LOG_INFO("only one transport option; autoselect '%s'", vector[0]);
118 return transport_select(ctx, vector[0]);
125 * Registers a transport. There are general purpose transports
126 * (such as JTAG), as well as relatively proprietary ones which are
127 * specific to a given chip (or chip family).
129 * Code implementing a transport needs to register it before it can
130 * be selected and then activated. This is a dynamic process, so
131 * that chips (and families) can define transports as needed (without
132 * needing error-prone static tables).
134 * @param new_transport the transport being registered. On a
135 * successful return, this memory is owned by the transport framework.
137 * @returns ERROR_OK on success, else a fault code.
139 int transport_register(struct transport *new_transport)
143 for (t = transport_list; t; t = t->next) {
144 if (strcmp(t->name, new_transport->name) == 0) {
145 LOG_ERROR("transport name already used");
150 if (!new_transport->select || !new_transport->init)
151 LOG_ERROR("invalid transport %s", new_transport->name);
153 /* splice this into the list */
154 new_transport->next = transport_list;
155 transport_list = new_transport;
156 LOG_DEBUG("register '%s'", new_transport->name);
162 * Returns the transport currently being used by this debug or
163 * programming session.
165 * @returns handle to the read-only transport entity.
167 struct transport *get_current_transport(void)
169 /* REVISIT -- constify */
173 /*-----------------------------------------------------------------------*/
176 * Infrastructure for Tcl interface to transports.
180 * Makes and stores a copy of a set of transports passed as
181 * parameters to a command.
183 * @param vector where the resulting copy is stored, as an argv-style
184 * NULL-terminated vector.
186 COMMAND_HELPER(transport_list_parse, char ***vector)
189 unsigned n = CMD_ARGC;
195 return ERROR_COMMAND_SYNTAX_ERROR;
197 /* our return vector must be NULL terminated */
198 argv = calloc(n + 1, sizeof(char *));
202 for (unsigned i = 0; i < n; i++) {
205 for (t = transport_list; t; t = t->next) {
206 if (strcmp(t->name, CMD_ARGV[i]) != 0)
208 argv[j++] = strdup(CMD_ARGV[i]);
212 LOG_ERROR("no such transport '%s'", CMD_ARGV[i]);
221 for (unsigned i = 0; i < n; i++)
227 COMMAND_HANDLER(handle_transport_init)
229 LOG_DEBUG("%s", __func__);
231 LOG_ERROR("session transport was not selected. Use 'transport select <transport>'");
233 /* no session transport configured, print transports then fail */
234 LOG_ERROR("Transports available:");
235 const char * const *vector = allowed_transports;
237 LOG_ERROR("%s", *vector);
243 return session->init(CMD_CTX);
246 COMMAND_HANDLER(handle_transport_list)
249 return ERROR_COMMAND_SYNTAX_ERROR;
251 command_print(CMD, "The following transports are available:");
253 for (struct transport *t = transport_list; t; t = t->next)
254 command_print(CMD, "\t%s", t->name);
260 * Implements the Tcl "transport select" command, choosing the
261 * transport to be used in this debug session from among the
262 * set supported by the debug adapter being used. Return value
263 * is scriptable (allowing "if swd then..." etc).
265 static int jim_transport_select(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
269 case 1: /* autoselect if necessary, then return/display current config */
271 if (!allowed_transports) {
272 LOG_ERROR("Debug adapter does not support any transports? Check config file order.");
275 LOG_INFO("auto-selecting first available session transport \"%s\". "
276 "To override use 'transport select <transport>'.", allowed_transports[0]);
277 res = transport_select(global_cmd_ctx, allowed_transports[0]);
281 Jim_SetResultString(interp, session->name, -1);
285 if (!strcmp(session->name, argv[1]->bytes)) {
286 LOG_WARNING("Transport \"%s\" was already selected", session->name);
287 Jim_SetResultString(interp, session->name, -1);
290 LOG_ERROR("Can't change session's transport after the initial selection was made");
295 /* Is this transport supported by our debug adapter?
296 * Example, "JTAG-only" means SWD is not supported.
298 * NOTE: requires adapter to have been set up, with
299 * transports declared via C.
301 if (!allowed_transports) {
302 LOG_ERROR("Debug adapter doesn't support any transports?");
306 for (unsigned i = 0; allowed_transports[i]; i++) {
308 if (strcmp(allowed_transports[i], argv[1]->bytes) == 0) {
309 if (transport_select(global_cmd_ctx, argv[1]->bytes) == ERROR_OK) {
310 Jim_SetResultString(interp, session->name, -1);
317 LOG_ERROR("Debug adapter doesn't support '%s' transport", argv[1]->bytes);
320 Jim_WrongNumArgs(interp, 1, argv, "[too many parameters]");
325 static const struct command_registration transport_commands[] = {
328 .handler = handle_transport_init,
329 /* this would be COMMAND_CONFIG ... except that
330 * it needs to trigger event handlers that may
331 * require COMMAND_EXEC ...
334 .help = "Initialize this session's transport",
339 .handler = handle_transport_list,
341 .help = "list all built-in transports",
346 .jim_handler = jim_transport_select,
348 .help = "Select this session's transport",
349 .usage = "[transport_name]",
351 COMMAND_REGISTRATION_DONE
354 static const struct command_registration transport_group[] = {
358 .help = "Transport command group",
359 .chain = transport_commands,
362 COMMAND_REGISTRATION_DONE
365 int transport_register_commands(struct command_context *ctx)
367 return register_commands(ctx, NULL, transport_group);