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>
27 #include <transport/transport.h>
28 #include <helper/time_support.h>
30 #include <jtag/stlink/stlink_tcl.h>
31 #include <jtag/stlink/stlink_layout.h>
32 #include <jtag/stlink/stlink_transport.h>
33 #include <jtag/stlink/stlink_interface.h>
35 #include <target/target.h>
37 static struct stlink_interface_s stlink_if = { {0, 0, 0, 0, 0, 0}, 0, 0 };
39 int stlink_interface_open(enum stlink_transports tr)
41 LOG_DEBUG("stlink_interface_open");
43 /* set transport mode */
44 stlink_if.param.transport = tr;
46 return stlink_if.layout->open(&stlink_if);
49 int stlink_interface_init_target(struct target *t)
53 LOG_DEBUG("stlink_interface_init_target");
55 /* this is the interface for the current target and we
56 * can setup the private pointer in the tap structure
57 * if the interface match the tap idcode
59 res = stlink_if.layout->api->idcode(stlink_if.fd, &t->tap->idcode);
64 unsigned ii, limit = t->tap->expected_ids_cnt;
67 for (ii = 0; ii < limit; ii++) {
68 uint32_t expected = t->tap->expected_ids[ii];
70 /* treat "-expected-id 0" as a "don't-warn" wildcard */
71 if (!expected || (t->tap->idcode == expected)) {
79 ("stlink_interface_init_target: target not found: idcode: %x ",
84 t->tap->priv = &stlink_if;
85 t->tap->hasidcode = 1;
90 static int stlink_interface_init(void)
92 LOG_DEBUG("stlink_interface_init");
94 /* here we can initialize the layout */
95 return stlink_layout_init(&stlink_if);
98 static int stlink_interface_quit(void)
100 LOG_DEBUG("stlink_interface_quit");
105 static int stlink_interface_speed(int speed)
107 LOG_DEBUG("stlink_interface_speed: ignore speed %d", speed);
112 static int stlink_speed_div(int speed, int *khz)
118 static int stlink_khz(int khz, int *jtag_speed)
124 static int stlink_interface_execute_queue(void)
126 LOG_DEBUG("stlink_interface_execute_queue: ignored");
131 COMMAND_HANDLER(stlink_interface_handle_device_desc_command)
133 LOG_DEBUG("stlink_interface_handle_device_desc_command");
136 stlink_if.param.device_desc = strdup(CMD_ARGV[0]);
139 ("expected exactly one argument to stlink_device_desc <description>");
145 COMMAND_HANDLER(stlink_interface_handle_serial_command)
147 LOG_DEBUG("stlink_interface_handle_serial_command");
150 stlink_if.param.serial = strdup(CMD_ARGV[0]);
153 ("expected exactly one argument to stlink_serial <serial-number>");
159 COMMAND_HANDLER(stlink_interface_handle_layout_command)
161 LOG_DEBUG("stlink_interface_handle_layout_command");
164 LOG_ERROR("Need exactly one argument to stlink_layout");
165 return ERROR_COMMAND_SYNTAX_ERROR;
168 if (stlink_if.layout) {
169 LOG_ERROR("already specified stlink_layout %s",
170 stlink_if.layout->name);
171 return (strcmp(stlink_if.layout->name, CMD_ARGV[0]) != 0)
172 ? ERROR_FAIL : ERROR_OK;
175 for (const struct stlink_layout *l = stlink_layout_get_list(); l->name;
177 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
178 stlink_if.layout = l;
183 LOG_ERROR("No STLINK layout '%s' found", CMD_ARGV[0]);
187 COMMAND_HANDLER(stlink_interface_handle_vid_pid_command)
189 LOG_DEBUG("stlink_interface_handle_vid_pid_command");
193 ("ignoring extra IDs in stlink_vid_pid (maximum is 1 pair)");
194 return ERROR_COMMAND_SYNTAX_ERROR;
197 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], stlink_if.param.vid);
198 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], stlink_if.param.pid);
203 COMMAND_HANDLER(stlink_interface_handle_api_command)
206 return ERROR_COMMAND_SYNTAX_ERROR;
209 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], new_api);
210 if ((new_api == 0) || (new_api > 2))
211 return ERROR_COMMAND_SYNTAX_ERROR;
213 stlink_if.param.api = new_api;
218 static const struct command_registration stlink_interface_command_handlers[] = {
220 .name = "stlink_device_desc",
221 .handler = &stlink_interface_handle_device_desc_command,
222 .mode = COMMAND_CONFIG,
223 .help = "set the stlink device description of the STLINK device",
224 .usage = "description_string",
227 .name = "stlink_serial",
228 .handler = &stlink_interface_handle_serial_command,
229 .mode = COMMAND_CONFIG,
230 .help = "set the serial number of the STLINK device",
231 .usage = "serial_string",
234 .name = "stlink_layout",
235 .handler = &stlink_interface_handle_layout_command,
236 .mode = COMMAND_CONFIG,
237 .help = "set the layout of the STLINK to usb or sg",
238 .usage = "layout_name",
241 .name = "stlink_vid_pid",
242 .handler = &stlink_interface_handle_vid_pid_command,
243 .mode = COMMAND_CONFIG,
244 .help = "the vendor and product ID of the STLINK device",
245 .usage = "(vid pid)* ",
248 .name = "stlink_api",
249 .handler = &stlink_interface_handle_api_command,
250 .mode = COMMAND_CONFIG,
251 .help = "set the desired stlink api level",
252 .usage = "api version 1 or 2",
254 COMMAND_REGISTRATION_DONE
257 struct jtag_interface stlink_interface = {
260 .commands = stlink_interface_command_handlers,
261 .transports = stlink_transports,
262 .init = stlink_interface_init,
263 .quit = stlink_interface_quit,
264 .speed = stlink_interface_speed,
265 .speed_div = stlink_speed_div,
267 .execute_queue = stlink_interface_execute_queue,