build: cleanup src/jtag/drivers directory
[openocd.git] / src / jtag / stlink / stlink_interface.c
blob9c0215f02d96592ab13f2106bb20e029329ad416
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 ***************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
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 };
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)
51 int res;
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);
61 if (res != ERROR_OK)
62 return res;
64 unsigned ii, limit = t->tap->expected_ids_cnt;
65 int found = 0;
67 for (ii = 0; ii < limit; ii++) {
68 uint32_t expected = t->tap->expected_ids[ii];
70 if (t->tap->idcode == expected) {
71 found = 1;
72 break;
76 if (found == 0) {
77 LOG_ERROR
78 ("stlink_interface_init_target: target not found: idcode: %x ",
79 t->tap->idcode);
80 return ERROR_FAIL;
83 t->tap->priv = &stlink_if;
84 t->tap->hasidcode = 1;
86 return ERROR_OK;
89 static int stlink_interface_init(void)
91 LOG_DEBUG("stlink_interface_init");
93 /* here we can initialize the layout */
94 return stlink_layout_init(&stlink_if);
97 static int stlink_interface_quit(void)
99 LOG_DEBUG("stlink_interface_quit");
101 return ERROR_OK;
104 static int stlink_interface_speed(int speed)
106 LOG_DEBUG("stlink_interface_speed: ignore speed %d", speed);
108 return ERROR_OK;
111 static int stlink_speed_div(int speed, int *khz)
113 *khz = speed;
114 return ERROR_OK;
117 static int stlink_khz(int khz, int *jtag_speed)
119 *jtag_speed = khz;
120 return ERROR_OK;
123 static int stlink_interface_execute_queue(void)
125 LOG_DEBUG("stlink_interface_execute_queue: ignored");
127 return ERROR_OK;
130 COMMAND_HANDLER(stlink_interface_handle_device_desc_command)
132 LOG_DEBUG("stlink_interface_handle_device_desc_command");
134 if (CMD_ARGC == 1) {
135 stlink_if.param.device_desc = strdup(CMD_ARGV[0]);
136 } else {
137 LOG_ERROR
138 ("expected exactly one argument to stlink_device_desc <description>");
141 return ERROR_OK;
144 COMMAND_HANDLER(stlink_interface_handle_serial_command)
146 LOG_DEBUG("stlink_interface_handle_serial_command");
148 if (CMD_ARGC == 1) {
149 stlink_if.param.serial = strdup(CMD_ARGV[0]);
150 } else {
151 LOG_ERROR
152 ("expected exactly one argument to stlink_serial <serial-number>");
155 return ERROR_OK;
158 COMMAND_HANDLER(stlink_interface_handle_layout_command)
160 LOG_DEBUG("stlink_interface_handle_layout_command");
162 if (CMD_ARGC != 1) {
163 LOG_ERROR("Need exactly one argument to stlink_layout");
164 return ERROR_COMMAND_SYNTAX_ERROR;
167 if (stlink_if.layout) {
168 LOG_ERROR("already specified stlink_layout %s",
169 stlink_if.layout->name);
170 return (strcmp(stlink_if.layout->name, CMD_ARGV[0]) != 0)
171 ? ERROR_FAIL : ERROR_OK;
174 for (const struct stlink_layout *l = stlink_layout_get_list(); l->name;
175 l++) {
176 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
177 stlink_if.layout = l;
178 return ERROR_OK;
182 LOG_ERROR("No STLINK layout '%s' found", CMD_ARGV[0]);
183 return ERROR_FAIL;
186 COMMAND_HANDLER(stlink_interface_handle_vid_pid_command)
188 LOG_DEBUG("stlink_interface_handle_vid_pid_command");
190 if (CMD_ARGC != 2) {
191 LOG_WARNING
192 ("ignoring extra IDs in stlink_vid_pid (maximum is 1 pair)");
193 return ERROR_COMMAND_SYNTAX_ERROR;
196 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], stlink_if.param.vid);
197 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], stlink_if.param.pid);
199 return ERROR_OK;
202 static const struct command_registration stlink_interface_command_handlers[] = {
204 .name = "stlink_device_desc",
205 .handler = &stlink_interface_handle_device_desc_command,
206 .mode = COMMAND_CONFIG,
207 .help = "set the stlink device description of the STLINK device",
208 .usage = "description_string",
211 .name = "stlink_serial",
212 .handler = &stlink_interface_handle_serial_command,
213 .mode = COMMAND_CONFIG,
214 .help = "set the serial number of the STLINK device",
215 .usage = "serial_string",
218 .name = "stlink_layout",
219 .handler = &stlink_interface_handle_layout_command,
220 .mode = COMMAND_CONFIG,
221 .help = "set the layout of the STLINK to usb or sg",
222 .usage = "layout_name",
225 .name = "stlink_vid_pid",
226 .handler = &stlink_interface_handle_vid_pid_command,
227 .mode = COMMAND_CONFIG,
228 .help = "the vendor and product ID of the STLINK device",
229 .usage = "(vid pid)* ",
231 COMMAND_REGISTRATION_DONE
234 struct jtag_interface stlink_interface = {
235 .name = "stlink",
236 .supported = 0,
237 .commands = stlink_interface_command_handlers,
238 .transports = stlink_transports,
239 .init = stlink_interface_init,
240 .quit = stlink_interface_quit,
241 .speed = stlink_interface_speed,
242 .speed_div = stlink_speed_div,
243 .khz = stlink_khz,
244 .execute_queue = stlink_interface_execute_queue,