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 ***************************************************************************/
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
)
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
);
63 unsigned ii
, limit
= t
->tap
->expected_ids_cnt
;
66 for (ii
= 0; ii
< limit
; ii
++) {
67 uint32_t expected
= t
->tap
->expected_ids
[ii
];
69 if (t
->tap
->idcode
== expected
) {
77 ("stlink_interface_init_target: target not found: idcode: %x ",
82 t
->tap
->priv
= &stlink_if
;
83 t
->tap
->hasidcode
= 1;
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");
103 static int stlink_interface_speed(int speed
)
105 LOG_DEBUG("stlink_interface_speed: ignore speed %d", speed
);
110 static int stlink_speed_div(int speed
, int *khz
)
116 static int stlink_khz(int khz
, int *jtag_speed
)
122 static int stlink_interface_execute_queue(void)
124 LOG_DEBUG("stlink_interface_execute_queue: ignored");
129 COMMAND_HANDLER(stlink_interface_handle_device_desc_command
)
131 LOG_DEBUG("stlink_interface_handle_device_desc_command");
134 stlink_if
.param
.device_desc
= strdup(CMD_ARGV
[0]);
137 ("expected exactly one argument to stlink_device_desc <description>");
143 COMMAND_HANDLER(stlink_interface_handle_serial_command
)
145 LOG_DEBUG("stlink_interface_handle_serial_command");
148 stlink_if
.param
.serial
= strdup(CMD_ARGV
[0]);
151 ("expected exactly one argument to stlink_serial <serial-number>");
157 COMMAND_HANDLER(stlink_interface_handle_layout_command
)
159 LOG_DEBUG("stlink_interface_handle_layout_command");
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
;
175 if (strcmp(l
->name
, CMD_ARGV
[0]) == 0) {
176 stlink_if
.layout
= l
;
181 LOG_ERROR("No STLINK layout '%s' found", CMD_ARGV
[0]);
185 COMMAND_HANDLER(stlink_interface_handle_vid_pid_command
)
187 LOG_DEBUG("stlink_interface_handle_vid_pid_command");
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
);
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
= {
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
,
243 .execute_queue
= stlink_interface_execute_queue
,