STLINK: swd transport renamed and jtag+swim transport added
[openocd/andreasf.git] / src / jtag / stlink / stlink_interface.c
blob6a856945fd188e08f029a27fb58911be10ba928f
1 /***************************************************************************
2 * Copyright (C) 2011 by Mathias Kuester *
3 * Mathias Kuester <kesmtp@freenet.de> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 /* project specific includes */
25 #include <jtag/interface.h>
26 #include <transport/transport.h>
27 #include <helper/time_support.h>
29 #include <jtag/stlink/stlink_tcl.h>
30 #include <jtag/stlink/stlink_layout.h>
31 #include <jtag/stlink/stlink_transport.h>
32 #include <jtag/stlink/stlink_interface.h>
34 #include <target/target.h>
36 static struct stlink_interface_s stlink_if = { {0, 0, 0, 0, 0}, 0, 0 };
38 int stlink_interface_open(enum stlink_transports tr)
40 LOG_DEBUG("stlink_interface_open");
42 /* set transport mode */
43 stlink_if.param.transport = tr;
45 return stlink_if.layout->open(&stlink_if);
48 int stlink_interface_init_target(struct target *t)
50 int res;
52 LOG_DEBUG("stlink_interface_init_target");
54 /* this is the interface for the current target and we
55 * can setup the private pointer in the tap structure
56 * if the interface match the tap idcode
58 res = stlink_if.layout->api->idcode(stlink_if.fd, &t->tap->idcode);
60 if (res != ERROR_OK)
61 return res;
63 unsigned ii, limit = t->tap->expected_ids_cnt;
64 int found = 0;
66 for (ii = 0; ii < limit; ii++) {
67 uint32_t expected = t->tap->expected_ids[ii];
69 if (t->tap->idcode == expected) {
70 found = 1;
71 break;
75 if (found == 0) {
76 LOG_ERROR
77 ("stlink_interface_init_target: target not found: idcode: %x ",
78 t->tap->idcode);
79 return ERROR_FAIL;
82 t->tap->priv = &stlink_if;
83 t->tap->hasidcode = 1;
85 return ERROR_OK;
88 static int stlink_interface_init(void)
90 LOG_DEBUG("stlink_interface_init");
92 /* here we can initialize the layout */
93 return stlink_layout_init(&stlink_if);
96 static int stlink_interface_quit(void)
98 LOG_DEBUG("stlink_interface_quit");
100 return ERROR_OK;
103 static int stlink_interface_speed(int speed)
105 LOG_DEBUG("stlink_interface_speed: ignore speed %d", speed);
107 return ERROR_OK;
110 static int stlink_speed_div(int speed, int *khz)
112 *khz = speed;
113 return ERROR_OK;
116 static int stlink_khz(int khz, int *jtag_speed)
118 *jtag_speed = khz;
119 return ERROR_OK;
122 static int stlink_interface_execute_queue(void)
124 LOG_DEBUG("stlink_interface_execute_queue: ignored");
126 return ERROR_OK;
129 COMMAND_HANDLER(stlink_interface_handle_device_desc_command)
131 LOG_DEBUG("stlink_interface_handle_device_desc_command");
133 if (CMD_ARGC == 1) {
134 stlink_if.param.device_desc = strdup(CMD_ARGV[0]);
135 } else {
136 LOG_ERROR
137 ("expected exactly one argument to stlink_device_desc <description>");
140 return ERROR_OK;
143 COMMAND_HANDLER(stlink_interface_handle_serial_command)
145 LOG_DEBUG("stlink_interface_handle_serial_command");
147 if (CMD_ARGC == 1) {
148 stlink_if.param.serial = strdup(CMD_ARGV[0]);
149 } else {
150 LOG_ERROR
151 ("expected exactly one argument to stlink_serial <serial-number>");
154 return ERROR_OK;
157 COMMAND_HANDLER(stlink_interface_handle_layout_command)
159 LOG_DEBUG("stlink_interface_handle_layout_command");
161 if (CMD_ARGC != 1) {
162 LOG_ERROR("Need exactly one argument to stlink_layout");
163 return ERROR_COMMAND_SYNTAX_ERROR;
166 if (stlink_if.layout) {
167 LOG_ERROR("already specified stlink_layout %s",
168 stlink_if.layout->name);
169 return (strcmp(stlink_if.layout->name, CMD_ARGV[0]) != 0)
170 ? ERROR_FAIL : ERROR_OK;
173 for (const struct stlink_layout *l = stlink_layout_get_list(); l->name;
174 l++) {
175 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
176 stlink_if.layout = l;
177 return ERROR_OK;
181 LOG_ERROR("No STLINK layout '%s' found", CMD_ARGV[0]);
182 return ERROR_FAIL;
185 COMMAND_HANDLER(stlink_interface_handle_vid_pid_command)
187 LOG_DEBUG("stlink_interface_handle_vid_pid_command");
189 if (CMD_ARGC != 2) {
190 LOG_WARNING
191 ("ignoring extra IDs in stlink_vid_pid (maximum is 1 pair)");
192 return ERROR_COMMAND_SYNTAX_ERROR;
195 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], stlink_if.param.vid);
196 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], stlink_if.param.pid);
198 return ERROR_OK;
201 static const struct command_registration stlink_interface_command_handlers[] = {
203 .name = "stlink_device_desc",
204 .handler = &stlink_interface_handle_device_desc_command,
205 .mode = COMMAND_CONFIG,
206 .help = "set the stlink device description of the STLINK device",
207 .usage = "description_string",
210 .name = "stlink_serial",
211 .handler = &stlink_interface_handle_serial_command,
212 .mode = COMMAND_CONFIG,
213 .help = "set the serial number of the STLINK device",
214 .usage = "serial_string",
217 .name = "stlink_layout",
218 .handler = &stlink_interface_handle_layout_command,
219 .mode = COMMAND_CONFIG,
220 .help = "set the layout of the STLINK to usb or sg",
221 .usage = "layout_name",
224 .name = "stlink_vid_pid",
225 .handler = &stlink_interface_handle_vid_pid_command,
226 .mode = COMMAND_CONFIG,
227 .help = "the vendor and product ID of the STLINK device",
228 .usage = "(vid pid)* ",
230 COMMAND_REGISTRATION_DONE
233 struct jtag_interface stlink_interface = {
234 .name = "stlink",
235 .supported = 0,
236 .commands = stlink_interface_command_handlers,
237 .transports = stlink_transports,
238 .init = stlink_interface_init,
239 .quit = stlink_interface_quit,
240 .speed = stlink_interface_speed,
241 .speed_div = stlink_speed_div,
242 .khz = stlink_khz,
243 .execute_queue = stlink_interface_execute_queue,