1 /***************************************************************************
2 * Copyright (C) 2011 by Mathias Kuester *
3 * Mathias Kuester <kesmtp@freenet.de> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 /* project specific includes */
26 #include <jtag/interface.h>
28 #include <transport/transport.h>
29 #include <helper/time_support.h>
30 #include <target/target.h>
31 #include <jtag/stlink/stlink_tcl.h>
32 #include <jtag/stlink/stlink_transport.h>
33 #include <jtag/stlink/stlink_interface.h>
35 COMMAND_HANDLER(stlink_transport_jtag_command)
37 LOG_DEBUG("stlink_transport_jtag_command");
42 static const struct command_registration
43 stlink_transport_stlink_subcommand_handlers[] = {
46 .mode = COMMAND_CONFIG,
47 .jim_handler = jim_stlink_newtap,
48 .help = "Create a new TAP instance named basename.tap_type, "
49 "and appends it to the scan chain.",
50 .usage = "basename tap_type '-irlen' count "
51 "['-expected_id' number] ",
54 COMMAND_REGISTRATION_DONE
57 static const struct command_registration
58 stlink_transport_jtag_subcommand_handlers[] = {
62 .handler = stlink_transport_jtag_command,
68 .handler = stlink_transport_jtag_command,
72 .name = "arp_init-reset",
74 .handler = stlink_transport_jtag_command,
78 .name = "tapisenabled",
80 .jim_handler = jim_jtag_tap_enabler,
85 .jim_handler = jim_jtag_tap_enabler,
90 .handler = stlink_transport_jtag_command,
96 .handler = stlink_transport_jtag_command,
101 .mode = COMMAND_EXEC,
102 .jim_handler = jim_jtag_configure,
107 .handler = stlink_transport_jtag_command,
111 COMMAND_REGISTRATION_DONE
114 static const struct command_registration stlink_transport_command_handlers[] = {
119 .help = "perform stlink actions",
121 .chain = stlink_transport_stlink_subcommand_handlers,
127 .chain = stlink_transport_jtag_subcommand_handlers,
129 COMMAND_REGISTRATION_DONE
132 static int stlink_transport_register_commands(struct command_context *cmd_ctx)
134 return register_commands(cmd_ctx, NULL,
135 stlink_transport_command_handlers);
138 static int stlink_transport_init(struct command_context *cmd_ctx)
140 LOG_DEBUG("stlink_transport_init");
141 struct target *t = get_current_target(cmd_ctx);
142 struct transport *transport;
143 enum stlink_transports tr;
146 LOG_ERROR("no current target");
150 transport = get_current_transport();
153 LOG_ERROR("no transport selected");
157 LOG_DEBUG("current transport %s", transport->name);
159 /* get selected transport as enum */
160 tr = STLINK_TRANSPORT_UNKNOWN;
162 if (strcmp(transport->name, "stlink_swd") == 0)
163 tr = STLINK_TRANSPORT_SWD;
164 else if (strcmp(transport->name, "stlink_jtag") == 0)
165 tr = STLINK_TRANSPORT_JTAG;
166 else if (strcmp(transport->name, "stlink_swim") == 0)
167 tr = STLINK_TRANSPORT_SWIM;
169 int retval = stlink_interface_open(tr);
171 if (retval != ERROR_OK)
174 return stlink_interface_init_target(t);
177 static int stlink_transport_select(struct command_context *ctx)
179 LOG_DEBUG("stlink_transport_select");
183 /* NOTE: interface init must already have been done.
184 * That works with only C code ... no Tcl glue required.
187 retval = stlink_transport_register_commands(ctx);
189 if (retval != ERROR_OK)
195 static struct transport stlink_swd_transport = {
196 .name = "stlink_swd",
197 .select = stlink_transport_select,
198 .init = stlink_transport_init,
201 static struct transport stlink_jtag_transport = {
202 .name = "stlink_jtag",
203 .select = stlink_transport_select,
204 .init = stlink_transport_init,
207 static struct transport stlink_swim_transport = {
208 .name = "stlink_swim",
209 .select = stlink_transport_select,
210 .init = stlink_transport_init,
213 const char *stlink_transports[] = { "stlink_swd", "stlink_jtag", "stlink_swim", NULL };
215 static void stlink_constructor(void) __attribute__ ((constructor));
216 static void stlink_constructor(void)
218 transport_register(&stlink_swd_transport);
219 transport_register(&stlink_jtag_transport);
220 transport_register(&stlink_swim_transport);
223 bool transport_is_stlink(void)
225 return get_current_transport() == &stlink_swd_transport;