1 /***************************************************************************
2 * Copyright (C) 2011 by Mathias Kuester *
3 * Mathias Kuester <kesmtp@freenet.de> *
5 * Copyright (C) 2012 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
22 ***************************************************************************/
28 /* project specific includes */
29 #include <jtag/interface.h>
30 #include <transport/transport.h>
31 #include <helper/time_support.h>
33 #include <jtag/hla/hla_tcl.h>
34 #include <jtag/hla/hla_layout.h>
35 #include <jtag/hla/hla_transport.h>
36 #include <jtag/hla/hla_interface.h>
38 #include <target/target.h>
40 static struct hl_interface_s hl_if
= { {0, 0, 0, 0, 0, HL_TRANSPORT_UNKNOWN
, 0, false, NULL
, 0}, 0, 0 };
42 int hl_interface_open(enum hl_transports tr
)
44 LOG_DEBUG("hl_interface_open");
46 enum reset_types jtag_reset_config
= jtag_get_reset_config();
48 if (jtag_reset_config
& RESET_CNCT_UNDER_SRST
) {
49 if (jtag_reset_config
& RESET_SRST_NO_GATING
)
50 hl_if
.param
.connect_under_reset
= true;
52 LOG_WARNING("\'srst_nogate\' reset_config option is required");
55 /* set transport mode */
56 hl_if
.param
.transport
= tr
;
58 int result
= hl_if
.layout
->open(&hl_if
);
59 if (result
!= ERROR_OK
)
62 return hl_interface_init_reset();
65 int hl_interface_init_target(struct target
*t
)
69 LOG_DEBUG("hl_interface_init_target");
71 /* this is the interface for the current target and we
72 * can setup the private pointer in the tap structure
73 * if the interface match the tap idcode
75 res
= hl_if
.layout
->api
->idcode(hl_if
.fd
, &t
->tap
->idcode
);
80 unsigned ii
, limit
= t
->tap
->expected_ids_cnt
;
83 for (ii
= 0; ii
< limit
; ii
++) {
84 uint32_t expected
= t
->tap
->expected_ids
[ii
];
86 /* treat "-expected-id 0" as a "don't-warn" wildcard */
87 if (!expected
|| (t
->tap
->idcode
== expected
)) {
94 LOG_ERROR("hl_interface_init_target: target not found: idcode: 0x%08x",
99 t
->tap
->priv
= &hl_if
;
100 t
->tap
->hasidcode
= 1;
105 static int hl_interface_init(void)
107 LOG_DEBUG("hl_interface_init");
109 /* here we can initialize the layout */
110 return hl_layout_init(&hl_if
);
113 static int hl_interface_quit(void)
115 LOG_DEBUG("hl_interface_quit");
117 if (hl_if
.param
.trace_f
) {
118 fclose(hl_if
.param
.trace_f
);
119 hl_if
.param
.trace_f
= NULL
;
125 static int hl_interface_execute_queue(void)
127 LOG_DEBUG("hl_interface_execute_queue: ignored");
132 int hl_interface_init_reset(void)
134 /* incase the adapter has not already handled asserting srst
135 * we will attempt it again */
136 if (hl_if
.param
.connect_under_reset
) {
137 jtag_add_reset(0, 1);
138 hl_if
.layout
->api
->assert_srst(hl_if
.fd
, 0);
144 COMMAND_HANDLER(hl_interface_handle_device_desc_command
)
146 LOG_DEBUG("hl_interface_handle_device_desc_command");
149 hl_if
.param
.device_desc
= strdup(CMD_ARGV
[0]);
151 LOG_ERROR("expected exactly one argument to hl_device_desc <description>");
157 COMMAND_HANDLER(hl_interface_handle_serial_command
)
159 LOG_DEBUG("hl_interface_handle_serial_command");
162 hl_if
.param
.serial
= strdup(CMD_ARGV
[0]);
164 LOG_ERROR("expected exactly one argument to hl_serial <serial-number>");
170 COMMAND_HANDLER(hl_interface_handle_layout_command
)
172 LOG_DEBUG("hl_interface_handle_layout_command");
175 LOG_ERROR("Need exactly one argument to stlink_layout");
176 return ERROR_COMMAND_SYNTAX_ERROR
;
180 LOG_ERROR("already specified hl_layout %s",
182 return (strcmp(hl_if
.layout
->name
, CMD_ARGV
[0]) != 0)
183 ? ERROR_FAIL
: ERROR_OK
;
186 for (const struct hl_layout
*l
= hl_layout_get_list(); l
->name
;
188 if (strcmp(l
->name
, CMD_ARGV
[0]) == 0) {
194 LOG_ERROR("No adapter layout '%s' found", CMD_ARGV
[0]);
198 COMMAND_HANDLER(hl_interface_handle_vid_pid_command
)
200 LOG_DEBUG("hl_interface_handle_vid_pid_command");
203 LOG_WARNING("ignoring extra IDs in hl_vid_pid (maximum is 1 pair)");
204 return ERROR_COMMAND_SYNTAX_ERROR
;
207 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], hl_if
.param
.vid
);
208 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[1], hl_if
.param
.pid
);
213 COMMAND_HANDLER(stlink_interface_handle_api_command
)
216 return ERROR_COMMAND_SYNTAX_ERROR
;
219 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], new_api
);
220 if ((new_api
== 0) || (new_api
> 2))
221 return ERROR_COMMAND_SYNTAX_ERROR
;
223 hl_if
.param
.api
= new_api
;
228 COMMAND_HANDLER(interface_handle_trace_command
)
234 return ERROR_COMMAND_SYNTAX_ERROR
;
236 f
= fopen(CMD_ARGV
[0], "a");
238 return ERROR_COMMAND_SYNTAX_ERROR
;
240 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], source_hz
);
241 if (source_hz
== 0) {
243 return ERROR_COMMAND_SYNTAX_ERROR
;
246 hl_if
.param
.trace_f
= f
;
247 hl_if
.param
.trace_source_hz
= source_hz
;
252 static const struct command_registration hl_interface_command_handlers
[] = {
254 .name
= "hla_device_desc",
255 .handler
= &hl_interface_handle_device_desc_command
,
256 .mode
= COMMAND_CONFIG
,
257 .help
= "set the a device description of the adapter",
258 .usage
= "description_string",
261 .name
= "hla_serial",
262 .handler
= &hl_interface_handle_serial_command
,
263 .mode
= COMMAND_CONFIG
,
264 .help
= "set the serial number of the adapter",
265 .usage
= "serial_string",
268 .name
= "hla_layout",
269 .handler
= &hl_interface_handle_layout_command
,
270 .mode
= COMMAND_CONFIG
,
271 .help
= "set the layout of the adapter",
272 .usage
= "layout_name",
275 .name
= "hla_vid_pid",
276 .handler
= &hl_interface_handle_vid_pid_command
,
277 .mode
= COMMAND_CONFIG
,
278 .help
= "the vendor and product ID of the adapter",
279 .usage
= "(vid pid)* ",
282 .name
= "stlink_api",
283 .handler
= &stlink_interface_handle_api_command
,
284 .mode
= COMMAND_CONFIG
,
285 .help
= "set the desired stlink api level",
286 .usage
= "api version 1 or 2",
290 .handler
= &interface_handle_trace_command
,
291 .mode
= COMMAND_CONFIG
,
292 .help
= "configure trace reception",
293 .usage
= "destination_path source_lock_hz",
295 COMMAND_REGISTRATION_DONE
298 struct jtag_interface hl_interface
= {
301 .commands
= hl_interface_command_handlers
,
302 .transports
= hl_transports
,
303 .init
= hl_interface_init
,
304 .quit
= hl_interface_quit
,
305 .execute_queue
= hl_interface_execute_queue
,