hla: add a way to pass arbitrary commands from user to layout and use for ICDI
[openocd.git] / src / jtag / hla / hla_interface.c
1 /***************************************************************************
2 * Copyright (C) 2011 by Mathias Kuester *
3 * Mathias Kuester <kesmtp@freenet.de> *
4 * *
5 * Copyright (C) 2012 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
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. *
12 * *
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. *
17 * *
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 ***************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 /* project specific includes */
29 #include <jtag/interface.h>
30 #include <transport/transport.h>
31 #include <helper/time_support.h>
32
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>
37
38 #include <target/target.h>
39
40 static struct hl_interface_s hl_if = { {0, 0, 0, 0, 0, HL_TRANSPORT_UNKNOWN, false, NULL, 0}, 0, 0 };
41
42 int hl_interface_open(enum hl_transports tr)
43 {
44 LOG_DEBUG("hl_interface_open");
45
46 enum reset_types jtag_reset_config = jtag_get_reset_config();
47
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;
51 else
52 LOG_WARNING("\'srst_nogate\' reset_config option is required");
53 }
54
55 /* set transport mode */
56 hl_if.param.transport = tr;
57
58 int result = hl_if.layout->open(&hl_if);
59 if (result != ERROR_OK)
60 return result;
61
62 return hl_interface_init_reset();
63 }
64
65 int hl_interface_init_target(struct target *t)
66 {
67 int res;
68
69 LOG_DEBUG("hl_interface_init_target");
70
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
74 */
75 res = hl_if.layout->api->idcode(hl_if.handle, &t->tap->idcode);
76
77 if (res != ERROR_OK)
78 return res;
79
80 unsigned ii, limit = t->tap->expected_ids_cnt;
81 int found = 0;
82
83 for (ii = 0; ii < limit; ii++) {
84 uint32_t expected = t->tap->expected_ids[ii];
85
86 /* treat "-expected-id 0" as a "don't-warn" wildcard */
87 if (!expected || !t->tap->idcode ||
88 (t->tap->idcode == expected)) {
89 found = 1;
90 break;
91 }
92 }
93
94 if (found == 0) {
95 LOG_ERROR("hl_interface_init_target: target not found: idcode: 0x%08" PRIx32,
96 t->tap->idcode);
97 return ERROR_FAIL;
98 }
99
100 t->tap->priv = &hl_if;
101 t->tap->hasidcode = 1;
102
103 return ERROR_OK;
104 }
105
106 static int hl_interface_init(void)
107 {
108 LOG_DEBUG("hl_interface_init");
109
110 /* here we can initialize the layout */
111 return hl_layout_init(&hl_if);
112 }
113
114 static int hl_interface_quit(void)
115 {
116 LOG_DEBUG("hl_interface_quit");
117
118 if (hl_if.param.trace_f) {
119 fclose(hl_if.param.trace_f);
120 hl_if.param.trace_f = NULL;
121 }
122 hl_if.param.trace_source_hz = 0;
123
124 return ERROR_OK;
125 }
126
127 static int hl_interface_execute_queue(void)
128 {
129 LOG_DEBUG("hl_interface_execute_queue: ignored");
130
131 return ERROR_OK;
132 }
133
134 int hl_interface_init_reset(void)
135 {
136 /* incase the adapter has not already handled asserting srst
137 * we will attempt it again */
138 if (hl_if.param.connect_under_reset) {
139 jtag_add_reset(0, 1);
140 hl_if.layout->api->assert_srst(hl_if.handle, 0);
141 } else {
142 jtag_add_reset(0, 0);
143 }
144
145 return ERROR_OK;
146 }
147
148 static int dummy_khz(int khz, int *jtag_speed)
149 {
150 *jtag_speed = khz;
151 return ERROR_OK;
152 }
153
154 static int dummy_speed_div(int speed, int *khz)
155 {
156 *khz = speed;
157 return ERROR_OK;
158 }
159
160 static int dummy_speed(int speed)
161 {
162 return ERROR_OK;
163 }
164
165 int hl_interface_override_target(const char **targetname)
166 {
167 if (hl_if.layout->api->override_target) {
168 if (hl_if.layout->api->override_target(*targetname)) {
169 *targetname = "hla_target";
170 return ERROR_OK;
171 } else
172 return ERROR_FAIL;
173 }
174 return ERROR_FAIL;
175 }
176
177 COMMAND_HANDLER(hl_interface_handle_device_desc_command)
178 {
179 LOG_DEBUG("hl_interface_handle_device_desc_command");
180
181 if (CMD_ARGC == 1) {
182 hl_if.param.device_desc = strdup(CMD_ARGV[0]);
183 } else {
184 LOG_ERROR("expected exactly one argument to hl_device_desc <description>");
185 }
186
187 return ERROR_OK;
188 }
189
190 COMMAND_HANDLER(hl_interface_handle_serial_command)
191 {
192 LOG_DEBUG("hl_interface_handle_serial_command");
193
194 if (CMD_ARGC == 1) {
195 hl_if.param.serial = strdup(CMD_ARGV[0]);
196 } else {
197 LOG_ERROR("expected exactly one argument to hl_serial <serial-number>");
198 }
199
200 return ERROR_OK;
201 }
202
203 COMMAND_HANDLER(hl_interface_handle_layout_command)
204 {
205 LOG_DEBUG("hl_interface_handle_layout_command");
206
207 if (CMD_ARGC != 1) {
208 LOG_ERROR("Need exactly one argument to stlink_layout");
209 return ERROR_COMMAND_SYNTAX_ERROR;
210 }
211
212 if (hl_if.layout) {
213 LOG_ERROR("already specified hl_layout %s",
214 hl_if.layout->name);
215 return (strcmp(hl_if.layout->name, CMD_ARGV[0]) != 0)
216 ? ERROR_FAIL : ERROR_OK;
217 }
218
219 for (const struct hl_layout *l = hl_layout_get_list(); l->name;
220 l++) {
221 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
222 hl_if.layout = l;
223 return ERROR_OK;
224 }
225 }
226
227 LOG_ERROR("No adapter layout '%s' found", CMD_ARGV[0]);
228 return ERROR_FAIL;
229 }
230
231 COMMAND_HANDLER(hl_interface_handle_vid_pid_command)
232 {
233 LOG_DEBUG("hl_interface_handle_vid_pid_command");
234
235 if (CMD_ARGC != 2) {
236 LOG_WARNING("ignoring extra IDs in hl_vid_pid (maximum is 1 pair)");
237 return ERROR_COMMAND_SYNTAX_ERROR;
238 }
239
240 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], hl_if.param.vid);
241 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], hl_if.param.pid);
242
243 return ERROR_OK;
244 }
245
246 COMMAND_HANDLER(interface_handle_trace_command)
247 {
248 FILE *f = NULL;
249 unsigned source_hz;
250
251 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
252 return ERROR_COMMAND_SYNTAX_ERROR;
253
254 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], source_hz);
255 if (source_hz == 0) {
256 return ERROR_COMMAND_SYNTAX_ERROR;
257 }
258
259 if (CMD_ARGC == 2) {
260 f = fopen(CMD_ARGV[1], "a");
261 if (!f)
262 return ERROR_COMMAND_SYNTAX_ERROR;
263 }
264
265 hl_if.param.trace_f = f;
266 hl_if.param.trace_source_hz = source_hz;
267
268 return ERROR_OK;
269 }
270
271 COMMAND_HANDLER(interface_handle_hla_command)
272 {
273 if (CMD_ARGC != 1)
274 return ERROR_COMMAND_SYNTAX_ERROR;
275
276 if (!hl_if.layout->api->custom_command) {
277 LOG_ERROR("The selected adapter doesn't support custom commands");
278 return ERROR_FAIL;
279 }
280
281 hl_if.layout->api->custom_command(hl_if.handle, CMD_ARGV[0]);
282
283 return ERROR_OK;
284 }
285
286 static const struct command_registration hl_interface_command_handlers[] = {
287 {
288 .name = "hla_device_desc",
289 .handler = &hl_interface_handle_device_desc_command,
290 .mode = COMMAND_CONFIG,
291 .help = "set the a device description of the adapter",
292 .usage = "description_string",
293 },
294 {
295 .name = "hla_serial",
296 .handler = &hl_interface_handle_serial_command,
297 .mode = COMMAND_CONFIG,
298 .help = "set the serial number of the adapter",
299 .usage = "serial_string",
300 },
301 {
302 .name = "hla_layout",
303 .handler = &hl_interface_handle_layout_command,
304 .mode = COMMAND_CONFIG,
305 .help = "set the layout of the adapter",
306 .usage = "layout_name",
307 },
308 {
309 .name = "hla_vid_pid",
310 .handler = &hl_interface_handle_vid_pid_command,
311 .mode = COMMAND_CONFIG,
312 .help = "the vendor and product ID of the adapter",
313 .usage = "(vid pid)* ",
314 },
315 {
316 .name = "trace",
317 .handler = &interface_handle_trace_command,
318 .mode = COMMAND_CONFIG,
319 .help = "configure trace reception",
320 .usage = "source_lock_hz [destination_path]",
321 },
322 {
323 .name = "hla_command",
324 .handler = &interface_handle_hla_command,
325 .mode = COMMAND_EXEC,
326 .help = "execute a custom adapter-specific command",
327 .usage = "hla_command <command>",
328 },
329 COMMAND_REGISTRATION_DONE
330 };
331
332 struct jtag_interface hl_interface = {
333 .name = "hla",
334 .supported = 0,
335 .commands = hl_interface_command_handlers,
336 .transports = hl_transports,
337 .init = hl_interface_init,
338 .quit = hl_interface_quit,
339 .execute_queue = hl_interface_execute_queue,
340 .speed = &dummy_speed,
341 .khz = &dummy_khz,
342 .speed_div = &dummy_speed_div,
343 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)