- Fix bug-in-waiting when adding more than one TAP event type
[openocd.git] / src / jtag / tcl.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 √ėyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2009 SoftPLC Corporation *
9 * http://softplc.com *
10 * dick@softplc.com *
11 * *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
14 * *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
19 * *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
24 * *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
29 ***************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "jtag.h"
35 #include "minidriver.h"
36 #include "interface.h"
37 #include "interfaces.h"
38
39 #ifdef HAVE_STRINGS_H
40 #include <strings.h>
41 #endif
42
43 static const Jim_Nvp nvp_jtag_tap_event[] = {
44 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
45 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
46
47 { .name = NULL, .value = -1 }
48 };
49
50 extern jtag_interface_t *jtag_interface;
51
52 /* jtag commands */
53 static int handle_interface_list_command(struct command_context_s *cmd_ctx,
54 char *cmd, char **args, int argc);
55 static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
56 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
57 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
58 static int handle_jtag_rclk_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
59 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
60 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
61 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
62 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
63
64 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
65
66 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
67 static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
70 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
71 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
72
73 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76
77 extern int jtag_examine_chain(void);
78 extern int jtag_validate_chain(void);
79
80 enum jtag_tap_cfg_param {
81 JCFG_EVENT
82 };
83
84 static Jim_Nvp nvp_config_opts[] = {
85 { .name = "-event", .value = JCFG_EVENT },
86
87 { .name = NULL, .value = -1 }
88 };
89
90 static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, jtag_tap_t * tap)
91 {
92 Jim_Nvp *n;
93 Jim_Obj *o;
94 int e;
95
96 /* parse config or cget options */
97 while (goi->argc > 0) {
98 Jim_SetEmptyResult (goi->interp);
99
100 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
101 if (e != JIM_OK) {
102 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
103 return e;
104 }
105
106 switch (n->value) {
107 case JCFG_EVENT:
108 if (goi->argc == 0) {
109 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
110 return JIM_ERR;
111 }
112
113 e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
114 if (e != JIM_OK) {
115 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
116 return e;
117 }
118
119 if (goi->isconfigure) {
120 if (goi->argc != 1) {
121 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
122 return JIM_ERR;
123 }
124 } else {
125 if (goi->argc != 0) {
126 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
127 return JIM_ERR;
128 }
129 }
130
131 {
132 jtag_tap_event_action_t *jteap;
133
134 jteap = tap->event_action;
135 /* replace existing? */
136 while (jteap) {
137 if (jteap->event == (enum jtag_event)n->value) {
138 break;
139 }
140 jteap = jteap->next;
141 }
142
143 if (goi->isconfigure) {
144 bool replace = true;
145 if (jteap == NULL) {
146 /* create new */
147 jteap = calloc(1, sizeof (*jteap));
148 replace = false;
149 }
150 jteap->event = n->value;
151 Jim_GetOpt_Obj(goi, &o);
152 if (jteap->body) {
153 Jim_DecrRefCount(interp, jteap->body);
154 }
155 jteap->body = Jim_DuplicateObj(goi->interp, o);
156 Jim_IncrRefCount(jteap->body);
157
158 if (!replace)
159 {
160 /* add to head of event list */
161 jteap->next = tap->event_action;
162 tap->event_action = jteap;
163 }
164 Jim_SetEmptyResult(goi->interp);
165 } else {
166 /* get */
167 if (jteap == NULL) {
168 Jim_SetEmptyResult(goi->interp);
169 } else {
170 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
171 }
172 }
173 }
174 /* loop for more */
175 break;
176 }
177 } /* while (goi->argc) */
178
179 return JIM_OK;
180 }
181
182 static int is_bad_irval(int ir_length, jim_wide w)
183 {
184 jim_wide v = 1;
185
186 v <<= ir_length;
187 v -= 1;
188 v = ~v;
189 return (w & v) != 0;
190 }
191
192 static int jim_newtap_cmd(Jim_GetOptInfo *goi)
193 {
194 jtag_tap_t *pTap;
195 jim_wide w;
196 int x;
197 int e;
198 int reqbits;
199 Jim_Nvp *n;
200 char *cp;
201 const Jim_Nvp opts[] = {
202 #define NTAP_OPT_IRLEN 0
203 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
204 #define NTAP_OPT_IRMASK 1
205 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
206 #define NTAP_OPT_IRCAPTURE 2
207 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
208 #define NTAP_OPT_ENABLED 3
209 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
210 #define NTAP_OPT_DISABLED 4
211 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
212 #define NTAP_OPT_EXPECTED_ID 5
213 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
214 { .name = NULL , .value = -1 },
215 };
216
217 pTap = calloc(1, sizeof(jtag_tap_t));
218 if (!pTap) {
219 Jim_SetResult_sprintf(goi->interp, "no memory");
220 return JIM_ERR;
221 }
222
223 /*
224 * we expect CHIP + TAP + OPTIONS
225 * */
226 if (goi->argc < 3) {
227 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
228 free(pTap);
229 return JIM_ERR;
230 }
231 Jim_GetOpt_String(goi, &cp, NULL);
232 pTap->chip = strdup(cp);
233
234 Jim_GetOpt_String(goi, &cp, NULL);
235 pTap->tapname = strdup(cp);
236
237 /* name + dot + name + null */
238 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
239 cp = malloc(x);
240 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
241 pTap->dotted_name = cp;
242
243 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
244 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
245
246 /* deal with options */
247 #define NTREQ_IRLEN 1
248 #define NTREQ_IRCAPTURE 2
249 #define NTREQ_IRMASK 4
250
251 /* clear them as we find them */
252 reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
253
254 while (goi->argc) {
255 e = Jim_GetOpt_Nvp(goi, opts, &n);
256 if (e != JIM_OK) {
257 Jim_GetOpt_NvpUnknown(goi, opts, 0);
258 free((void *)pTap->dotted_name);
259 free(pTap);
260 return e;
261 }
262 LOG_DEBUG("Processing option: %s", n->name);
263 switch (n->value) {
264 case NTAP_OPT_ENABLED:
265 pTap->disabled_after_reset = false;
266 break;
267 case NTAP_OPT_DISABLED:
268 pTap->disabled_after_reset = true;
269 break;
270 case NTAP_OPT_EXPECTED_ID:
271 {
272 uint32_t *new_expected_ids;
273
274 e = Jim_GetOpt_Wide(goi, &w);
275 if (e != JIM_OK) {
276 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
277 free((void *)pTap->dotted_name);
278 free(pTap);
279 return e;
280 }
281
282 new_expected_ids = malloc(sizeof(uint32_t) * (pTap->expected_ids_cnt + 1));
283 if (new_expected_ids == NULL) {
284 Jim_SetResult_sprintf(goi->interp, "no memory");
285 free((void *)pTap->dotted_name);
286 free(pTap);
287 return JIM_ERR;
288 }
289
290 memcpy(new_expected_ids, pTap->expected_ids, sizeof(uint32_t) * pTap->expected_ids_cnt);
291
292 new_expected_ids[pTap->expected_ids_cnt] = w;
293
294 free(pTap->expected_ids);
295 pTap->expected_ids = new_expected_ids;
296 pTap->expected_ids_cnt++;
297 break;
298 }
299 case NTAP_OPT_IRLEN:
300 case NTAP_OPT_IRMASK:
301 case NTAP_OPT_IRCAPTURE:
302 e = Jim_GetOpt_Wide(goi, &w);
303 if (e != JIM_OK) {
304 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
305 free((void *)pTap->dotted_name);
306 free(pTap);
307 return e;
308 }
309 switch (n->value) {
310 case NTAP_OPT_IRLEN:
311 if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
312 LOG_WARNING("huge IR length %d", (int) w);
313 pTap->ir_length = w;
314 reqbits &= (~(NTREQ_IRLEN));
315 break;
316 case NTAP_OPT_IRMASK:
317 if (is_bad_irval(pTap->ir_length, w)) {
318 LOG_ERROR("IR mask %x too big",
319 (int) w);
320 free((void *)pTap->dotted_name);
321 free(pTap);
322 return ERROR_FAIL;
323 }
324 pTap->ir_capture_mask = w;
325 reqbits &= (~(NTREQ_IRMASK));
326 break;
327 case NTAP_OPT_IRCAPTURE:
328 if (is_bad_irval(pTap->ir_length, w)) {
329 LOG_ERROR("IR capture %x too big",
330 (int) w);
331 free((void *)pTap->dotted_name);
332 free(pTap);
333 return ERROR_FAIL;
334 }
335 pTap->ir_capture_value = w;
336 reqbits &= (~(NTREQ_IRCAPTURE));
337 break;
338 }
339 } /* switch (n->value) */
340 } /* while (goi->argc) */
341
342 /* default is enabled-after-reset */
343 pTap->enabled = !pTap->disabled_after_reset;
344
345 /* Did all the required option bits get cleared? */
346 if (0 == reqbits)
347 {
348 jtag_tap_init(pTap);
349 return ERROR_OK;
350 }
351
352 Jim_SetResult_sprintf(goi->interp,
353 "newtap: %s missing required parameters",
354 pTap->dotted_name);
355 jtag_tap_free(pTap);
356 return JIM_ERR;
357 }
358
359 static void jtag_tap_handle_event(jtag_tap_t *tap, enum jtag_event e)
360 {
361 jtag_tap_event_action_t * jteap;
362 int done;
363
364 jteap = tap->event_action;
365
366 done = 0;
367 while (jteap) {
368 if (jteap->event == e) {
369 done = 1;
370 LOG_DEBUG("JTAG tap: %s event: %d (%s) action: %s\n",
371 tap->dotted_name,
372 e,
373 Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
374 Jim_GetString(jteap->body, NULL));
375 if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
376 Jim_PrintErrorMessage(interp);
377 } else {
378 /* NOTE: we currently assume the handlers
379 * can't fail. That presumes later code
380 * will be verifying the scan chains ...
381 */
382 if (e == JTAG_TAP_EVENT_ENABLE)
383 tap->enabled = true;
384 }
385 }
386
387 jteap = jteap->next;
388 }
389
390 if (!done) {
391 LOG_DEBUG("event %d %s - no action",
392 e,
393 Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name);
394 }
395 }
396
397
398 static int jim_jtag_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
399 {
400 Jim_GetOptInfo goi;
401 int e;
402 Jim_Nvp *n;
403 Jim_Obj *o;
404 struct command_context_s *context;
405
406 enum {
407 JTAG_CMD_INTERFACE,
408 JTAG_CMD_INIT_RESET,
409 JTAG_CMD_NEWTAP,
410 JTAG_CMD_TAPENABLE,
411 JTAG_CMD_TAPDISABLE,
412 JTAG_CMD_TAPISENABLED,
413 JTAG_CMD_CONFIGURE,
414 JTAG_CMD_CGET,
415 JTAG_CMD_NAMES,
416 };
417
418 const Jim_Nvp jtag_cmds[] = {
419 { .name = "interface" , .value = JTAG_CMD_INTERFACE },
420 { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
421 { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
422 { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
423 { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
424 { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
425 { .name = "configure" , .value = JTAG_CMD_CONFIGURE },
426 { .name = "cget" , .value = JTAG_CMD_CGET },
427 { .name = "names" , .value = JTAG_CMD_NAMES },
428
429 { .name = NULL, .value = -1 },
430 };
431
432 context = Jim_GetAssocData(interp, "context");
433 /* go past the command */
434 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
435
436 e = Jim_GetOpt_Nvp(&goi, jtag_cmds, &n);
437 if (e != JIM_OK) {
438 Jim_GetOpt_NvpUnknown(&goi, jtag_cmds, 0);
439 return e;
440 }
441 Jim_SetEmptyResult(goi.interp);
442 switch (n->value) {
443 case JTAG_CMD_INTERFACE:
444 /* return the name of the interface */
445 /* TCL code might need to know the exact type... */
446 /* FUTURE: we allow this as a means to "set" the interface. */
447 if (goi.argc != 0) {
448 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
449 return JIM_ERR;
450 }
451 Jim_SetResultString(goi.interp, jtag_interface->name, -1);
452 return JIM_OK;
453 case JTAG_CMD_INIT_RESET:
454 if (goi.argc != 0) {
455 Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
456 return JIM_ERR;
457 }
458 e = jtag_init_reset(context);
459 if (e != ERROR_OK) {
460 Jim_SetResult_sprintf(goi.interp, "error: %d", e);
461 return JIM_ERR;
462 }
463 return JIM_OK;
464 case JTAG_CMD_NEWTAP:
465 return jim_newtap_cmd(&goi);
466 break;
467 case JTAG_CMD_TAPISENABLED:
468 case JTAG_CMD_TAPENABLE:
469 case JTAG_CMD_TAPDISABLE:
470 if (goi.argc != 1) {
471 Jim_SetResultString(goi.interp, "Too many parameters",-1);
472 return JIM_ERR;
473 }
474
475 {
476 jtag_tap_t *t;
477
478 t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
479 if (t == NULL)
480 return JIM_ERR;
481
482 switch (n->value) {
483 case JTAG_CMD_TAPISENABLED:
484 break;
485 case JTAG_CMD_TAPENABLE:
486 if (t->enabled)
487 break;
488 jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
489 if (!t->enabled)
490 break;
491
492 /* FIXME add JTAG sanity checks, w/o TLR
493 * - scan chain length grew by one (this)
494 * - IDs and IR lengths are as expected
495 */
496
497 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
498 break;
499 case JTAG_CMD_TAPDISABLE:
500 if (!t->enabled)
501 break;
502 jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
503 if (t->enabled)
504 break;
505
506 /* FIXME add JTAG sanity checks, w/o TLR
507 * - scan chain length shrank by one (this)
508 * - IDs and IR lengths are as expected
509 */
510
511 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
512 break;
513 }
514 e = t->enabled;
515 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
516 return JIM_OK;
517 }
518 break;
519
520 case JTAG_CMD_CGET:
521 if (goi.argc < 2) {
522 Jim_WrongNumArgs(goi.interp, 0, NULL,
523 "cget tap_name queryparm");
524 return JIM_ERR;
525 }
526
527 {
528 jtag_tap_t *t;
529
530 Jim_GetOpt_Obj(&goi, &o);
531 t = jtag_tap_by_jim_obj(goi.interp, o);
532 if (t == NULL) {
533 return JIM_ERR;
534 }
535
536 goi.isconfigure = 0;
537 return jtag_tap_configure_cmd(&goi, t);
538 }
539 break;
540
541 case JTAG_CMD_CONFIGURE:
542 if (goi.argc < 3) {
543 Jim_WrongNumArgs(goi.interp, 0, NULL,
544 "configure tap_name attribute value ...");
545 return JIM_ERR;
546 }
547
548 {
549 jtag_tap_t *t;
550
551 Jim_GetOpt_Obj(&goi, &o);
552 t = jtag_tap_by_jim_obj(goi.interp, o);
553 if (t == NULL) {
554 return JIM_ERR;
555 }
556
557 goi.isconfigure = 1;
558 return jtag_tap_configure_cmd(&goi, t);
559 }
560 break;
561
562 case JTAG_CMD_NAMES:
563 if (goi.argc != 0) {
564 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
565 return JIM_ERR;
566 }
567 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
568 {
569 jtag_tap_t *tap;
570
571 for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
572 Jim_ListAppendElement(goi.interp,
573 Jim_GetResult(goi.interp),
574 Jim_NewStringObj(goi.interp,
575 tap->dotted_name, -1));
576 }
577 return JIM_OK;
578 }
579 break;
580
581 }
582
583 return JIM_ERR;
584 }
585
586 int jtag_register_commands(struct command_context_s *cmd_ctx)
587 {
588 register_jim(cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
589
590 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
591 COMMAND_CONFIG, "try to configure interface");
592 register_command(cmd_ctx, NULL,
593 "interface_list", &handle_interface_list_command,
594 COMMAND_ANY, "list all built-in interfaces");
595 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
596 COMMAND_ANY, "(DEPRECATED) set jtag speed (if supported)");
597 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
598 COMMAND_ANY, "set maximum jtag speed (if supported); "
599 "parameter is maximum khz, or 0 for adaptive clocking (RTCK).");
600 register_command(cmd_ctx, NULL, "jtag_rclk", handle_jtag_rclk_command,
601 COMMAND_ANY, "fallback_speed_khz - set JTAG speed to RCLK or use fallback speed");
602 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
603 COMMAND_CONFIG, "(DEPRECATED) jtag_device <ir_length> <ir_expected> <ir_mask>");
604 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
605 COMMAND_ANY,
606 "[none/trst_only/srst_only/trst_and_srst] [srst_pulls_trst/trst_pulls_srst] [combined/separate] [trst_push_pull/trst_open_drain] [srst_push_pull/srst_open_drain]");
607 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
608 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
609 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
610 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
611
612 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
613 COMMAND_EXEC, "print current scan chain configuration");
614
615 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
616 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
617 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
618 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
619 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
620 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
621 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
622 register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count, "returns number of times the JTAG queue has been flushed");
623 register_jim(cmd_ctx, "pathmove", Jim_Command_pathmove, "move JTAG to state1 then to state2, state3, etc. <state1>,<state2>,<stat3>...");
624
625 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
626 COMMAND_ANY, "verify value captured during Capture-IR <enable | disable>");
627 register_command(cmd_ctx, NULL, "verify_jtag", handle_verify_jtag_command,
628 COMMAND_ANY, "verify value capture <enable | disable>");
629 register_command(cmd_ctx, NULL, "tms_sequence", handle_tms_sequence_command,
630 COMMAND_ANY, "choose short(default) or long tms_sequence <short | long>");
631 return ERROR_OK;
632 }
633
634 static int default_khz(int khz, int *jtag_speed)
635 {
636 LOG_ERROR("Translation from khz to jtag_speed not implemented");
637 return ERROR_FAIL;
638 }
639
640 static int default_speed_div(int speed, int *khz)
641 {
642 LOG_ERROR("Translation from jtag_speed to khz not implemented");
643 return ERROR_FAIL;
644 }
645
646 static int default_power_dropout(int *dropout)
647 {
648 *dropout = 0; /* by default we can't detect power dropout */
649 return ERROR_OK;
650 }
651
652 static int default_srst_asserted(int *srst_asserted)
653 {
654 *srst_asserted = 0; /* by default we can't detect srst asserted */
655 return ERROR_OK;
656 }
657
658 static int handle_interface_command(struct command_context_s *cmd_ctx,
659 char *cmd, char **args, int argc)
660 {
661 /* check whether the interface is already configured */
662 if (jtag_interface)
663 {
664 LOG_WARNING("Interface already configured, ignoring");
665 return ERROR_OK;
666 }
667
668 /* interface name is a mandatory argument */
669 if (argc != 1 || args[0][0] == '\0')
670 return ERROR_COMMAND_SYNTAX_ERROR;
671
672 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
673 {
674 if (strcmp(args[0], jtag_interfaces[i]->name) != 0)
675 continue;
676
677 int retval = jtag_interfaces[i]->register_commands(cmd_ctx);
678 if (ERROR_OK != retval)
679 return retval;
680
681 jtag_interface = jtag_interfaces[i];
682
683 if (jtag_interface->khz == NULL)
684 jtag_interface->khz = default_khz;
685 if (jtag_interface->speed_div == NULL)
686 jtag_interface->speed_div = default_speed_div;
687 if (jtag_interface->power_dropout == NULL)
688 jtag_interface->power_dropout = default_power_dropout;
689 if (jtag_interface->srst_asserted == NULL)
690 jtag_interface->srst_asserted = default_srst_asserted;
691
692 return ERROR_OK;
693 }
694
695 /* no valid interface was found (i.e. the configuration option,
696 * didn't match one of the compiled-in interfaces
697 */
698 LOG_ERROR("The specified JTAG interface was not found (%s)", args[0]);
699 handle_interface_list_command(cmd_ctx, cmd, args, argc);
700 return ERROR_JTAG_INVALID_INTERFACE;
701 }
702
703 static int handle_interface_list_command(struct command_context_s *cmd_ctx,
704 char *cmd, char **args, int argc)
705 {
706 if (strcmp(cmd, "interface_list") == 0 && argc > 0)
707 return ERROR_COMMAND_SYNTAX_ERROR;
708
709 command_print(cmd_ctx, "The following JTAG interfaces are available:");
710 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
711 {
712 const char *name = jtag_interfaces[i]->name;
713 command_print(cmd_ctx, "%u: %s", i + 1, name);
714 }
715
716 return ERROR_OK;
717 }
718
719 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
720 {
721 int e;
722 char buf[1024];
723 Jim_Obj *newargs[ 10 ];
724 /*
725 * CONVERT SYNTAX
726 * argv[-1] = command
727 * argv[ 0] = ir length
728 * argv[ 1] = ir capture
729 * argv[ 2] = ir mask
730 * argv[ 3] = not actually used by anything but in the docs
731 */
732
733 if (argc < 4) {
734 command_print(cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
735 return ERROR_OK;
736 }
737 command_print(cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
738 command_print(cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
739 args[0],
740 args[1],
741 args[2]);
742 command_print(cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundaryscan(len5)");
743 command_print(cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
744 command_print(cmd_ctx, "jtag newtap stm32 boundary ....., and the tap: \"stm32.boundary\"");
745 command_print(cmd_ctx, "And then refer to the taps by the dotted name.");
746
747 newargs[0] = Jim_NewStringObj(interp, "jtag", -1);
748 newargs[1] = Jim_NewStringObj(interp, "newtap", -1);
749 sprintf(buf, "chip%d", jtag_tap_count());
750 newargs[2] = Jim_NewStringObj(interp, buf, -1);
751 sprintf(buf, "tap%d", jtag_tap_count());
752 newargs[3] = Jim_NewStringObj(interp, buf, -1);
753 newargs[4] = Jim_NewStringObj(interp, "-irlen", -1);
754 newargs[5] = Jim_NewStringObj(interp, args[0], -1);
755 newargs[6] = Jim_NewStringObj(interp, "-ircapture", -1);
756 newargs[7] = Jim_NewStringObj(interp, args[1], -1);
757 newargs[8] = Jim_NewStringObj(interp, "-irmask", -1);
758 newargs[9] = Jim_NewStringObj(interp, args[2], -1);
759
760 command_print(cmd_ctx, "NEW COMMAND:");
761 sprintf(buf, "%s %s %s %s %s %s %s %s %s %s",
762 Jim_GetString(newargs[0], NULL),
763 Jim_GetString(newargs[1], NULL),
764 Jim_GetString(newargs[2], NULL),
765 Jim_GetString(newargs[3], NULL),
766 Jim_GetString(newargs[4], NULL),
767 Jim_GetString(newargs[5], NULL),
768 Jim_GetString(newargs[6], NULL),
769 Jim_GetString(newargs[7], NULL),
770 Jim_GetString(newargs[8], NULL),
771 Jim_GetString(newargs[9], NULL));
772
773 e = jim_jtag_command(interp, 10, newargs);
774 if (e != JIM_OK) {
775 command_print(cmd_ctx, "%s", Jim_GetString(Jim_GetResult(interp), NULL));
776 }
777 return e;
778 }
779
780 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
781 {
782 jtag_tap_t *tap;
783
784 tap = jtag_all_taps();
785 command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
786 command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
787
788 while (tap) {
789 uint32_t expected, expected_mask, cur_instr, ii;
790 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
791 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
792 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
793
794 command_print(cmd_ctx,
795 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
796 tap->abs_chain_position,
797 tap->dotted_name,
798 tap->enabled ? 'Y' : 'n',
799 (unsigned int)(tap->idcode),
800 (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
801 (unsigned int)(tap->ir_length),
802 (unsigned int)(expected),
803 (unsigned int)(expected_mask),
804 (unsigned int)(cur_instr));
805
806 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
807 command_print(cmd_ctx, " | | | | 0x%08x | | | | ",
808 (unsigned int)(tap->expected_ids[ii]));
809 }
810
811 tap = tap->next_tap;
812 }
813
814 return ERROR_OK;
815 }
816
817 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
818 {
819 int new_cfg = 0;
820 int mask = 0;
821
822 if (argc < 1)
823 return ERROR_COMMAND_SYNTAX_ERROR;
824
825 /* Original versions cared about the order of these tokens:
826 * reset_config signals [combination [trst_type [srst_type]]]
827 * They also clobbered the previous configuration even on error.
828 *
829 * Here we don't care about the order, and only change values
830 * which have been explicitly specified.
831 */
832 for (; argc; argc--, args++) {
833 int tmp = 0;
834 int m;
835
836 /* signals */
837 m = RESET_HAS_TRST | RESET_HAS_SRST;
838 if (strcmp(*args, "none") == 0)
839 tmp = RESET_NONE;
840 else if (strcmp(*args, "trst_only") == 0)
841 tmp = RESET_HAS_TRST;
842 else if (strcmp(*args, "srst_only") == 0)
843 tmp = RESET_HAS_SRST;
844 else if (strcmp(*args, "trst_and_srst") == 0)
845 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
846 else
847 m = 0;
848 if (mask & m) {
849 LOG_ERROR("extra reset_config %s spec (%s)",
850 "signal", *args);
851 return ERROR_INVALID_ARGUMENTS;
852 }
853 if (m)
854 goto next;
855
856 /* combination (options for broken wiring) */
857 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
858 if (strcmp(*args, "separate") == 0)
859 /* separate reset lines - default */;
860 else if (strcmp(*args, "srst_pulls_trst") == 0)
861 tmp |= RESET_SRST_PULLS_TRST;
862 else if (strcmp(*args, "trst_pulls_srst") == 0)
863 tmp |= RESET_TRST_PULLS_SRST;
864 else if (strcmp(*args, "combined") == 0)
865 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
866 else
867 m = 0;
868 if (mask & m) {
869 LOG_ERROR("extra reset_config %s spec (%s)",
870 "combination", *args);
871 return ERROR_INVALID_ARGUMENTS;
872 }
873 if (m)
874 goto next;
875
876 /* trst_type (NOP without HAS_TRST) */
877 m = RESET_TRST_OPEN_DRAIN;
878 if (strcmp(*args, "trst_open_drain") == 0)
879 tmp |= RESET_TRST_OPEN_DRAIN;
880 else if (strcmp(*args, "trst_push_pull") == 0)
881 /* push/pull from adapter - default */;
882 else
883 m = 0;
884 if (mask & m) {
885 LOG_ERROR("extra reset_config %s spec (%s)",
886 "trst_type", *args);
887 return ERROR_INVALID_ARGUMENTS;
888 }
889 if (m)
890 goto next;
891
892 /* srst_type (NOP without HAS_SRST) */
893 m |= RESET_SRST_PUSH_PULL;
894 if (strcmp(*args, "srst_push_pull") == 0)
895 tmp |= RESET_SRST_PUSH_PULL;
896 else if (strcmp(*args, "srst_open_drain") == 0)
897 /* open drain from adapter - default */;
898 else
899 m = 0;
900 if (mask & m) {
901 LOG_ERROR("extra reset_config %s spec (%s)",
902 "srst_type", *args);
903 return ERROR_INVALID_ARGUMENTS;
904 }
905 if (m)
906 goto next;
907
908 /* caller provided nonsense; fail */
909 LOG_ERROR("unknown reset_config flag (%s)", *args);
910 return ERROR_INVALID_ARGUMENTS;
911
912 next:
913 /* Remember the bits which were specified (mask)
914 * and their new values (new_cfg).
915 */
916 mask |= m;
917 new_cfg |= tmp;
918 }
919
920 /* clear previous values of those bits, save new values */
921 enum reset_types old_cfg = jtag_get_reset_config();
922 old_cfg &= ~mask;
923 new_cfg |= old_cfg;
924 jtag_set_reset_config(new_cfg);
925
926 return ERROR_OK;
927 }
928
929 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx,
930 char *cmd, char **args, int argc)
931 {
932 if (argc > 1)
933 return ERROR_COMMAND_SYNTAX_ERROR;
934 if (argc == 1)
935 {
936 unsigned delay;
937 int retval = parse_uint(args[0], &delay);
938 if (ERROR_OK != retval)
939 return retval;
940 jtag_set_nsrst_delay(delay);
941 }
942 command_print(cmd_ctx, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
943 return ERROR_OK;
944 }
945
946 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx,
947 char *cmd, char **args, int argc)
948 {
949 if (argc > 1)
950 return ERROR_COMMAND_SYNTAX_ERROR;
951 if (argc == 1)
952 {
953 unsigned delay;
954 int retval = parse_uint(args[0], &delay);
955 if (ERROR_OK != retval)
956 return retval;
957 jtag_set_ntrst_delay(delay);
958 }
959 command_print(cmd_ctx, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
960 return ERROR_OK;
961 }
962
963 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
964 {
965 int retval = ERROR_OK;
966
967 command_print(cmd_ctx, "OLD SYNTAX: DEPRECATED - "
968 "use jtag_khz, not jtag_speed");
969
970 if (argc > 1)
971 return ERROR_COMMAND_SYNTAX_ERROR;
972 if (argc == 1)
973 {
974 LOG_DEBUG("handle jtag speed");
975
976 unsigned cur_speed = 0;
977 int retval = parse_uint(args[0], &cur_speed);
978 if (ERROR_OK != retval)
979 return retval;
980 retval = jtag_config_speed(cur_speed);
981
982 }
983 command_print(cmd_ctx, "jtag_speed: %d", jtag_get_speed());
984
985 return retval;
986 }
987
988 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
989 {
990 if (argc > 1)
991 return ERROR_COMMAND_SYNTAX_ERROR;
992
993 int retval = ERROR_OK;
994 if (argc == 1)
995 {
996 unsigned khz = 0;
997 int retval = parse_uint(args[0], &khz);
998 if (ERROR_OK != retval)
999 return retval;
1000 retval = jtag_config_khz(khz);
1001 if (ERROR_OK != retval)
1002 return retval;
1003 }
1004
1005 int cur_speed = jtag_get_speed_khz();
1006 retval = jtag_get_speed_readable(&cur_speed);
1007 if (ERROR_OK != retval)
1008 return retval;
1009
1010 if (cur_speed)
1011 command_print(cmd_ctx, "%d kHz", cur_speed);
1012 else
1013 command_print(cmd_ctx, "RCLK - adaptive");
1014
1015 return retval;
1016 }
1017
1018 static int handle_jtag_rclk_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1019 {
1020 if (argc > 1)
1021 return ERROR_COMMAND_SYNTAX_ERROR;
1022
1023 int retval = ERROR_OK;
1024 if (argc == 1)
1025 {
1026 unsigned khz = 0;
1027 int retval = parse_uint(args[0], &khz);
1028 if (ERROR_OK != retval)
1029 return retval;
1030 retval = jtag_config_rclk(khz);
1031 if (ERROR_OK != retval)
1032 return retval;
1033 }
1034
1035 int cur_khz = jtag_get_speed_khz();
1036 retval = jtag_get_speed_readable(&cur_khz);
1037 if (ERROR_OK != retval)
1038 return retval;
1039
1040 if (cur_khz)
1041 command_print(cmd_ctx, "RCLK not supported - fallback to %d kHz", cur_khz);
1042 else
1043 command_print(cmd_ctx, "RCLK - adaptive");
1044
1045 return retval;
1046 }
1047
1048 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx,
1049 char *cmd, char **args, int argc)
1050 {
1051 if (argc != 2)
1052 return ERROR_COMMAND_SYNTAX_ERROR;
1053
1054 int trst = -1;
1055 if (args[0][0] == '1')
1056 trst = 1;
1057 else if (args[0][0] == '0')
1058 trst = 0;
1059 else
1060 return ERROR_COMMAND_SYNTAX_ERROR;
1061
1062 int srst = -1;
1063 if (args[1][0] == '1')
1064 srst = 1;
1065 else if (args[1][0] == '0')
1066 srst = 0;
1067 else
1068 return ERROR_COMMAND_SYNTAX_ERROR;
1069
1070 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
1071 return ERROR_JTAG_INIT_FAILED;
1072
1073 jtag_add_reset(trst, srst);
1074 return jtag_execute_queue();
1075 }
1076
1077 static int handle_runtest_command(struct command_context_s *cmd_ctx,
1078 char *cmd, char **args, int argc)
1079 {
1080 if (argc != 1)
1081 return ERROR_COMMAND_SYNTAX_ERROR;
1082
1083 unsigned num_clocks;
1084 int retval = parse_uint(args[0], &num_clocks);
1085 if (ERROR_OK != retval)
1086 return retval;
1087
1088 jtag_add_runtest(num_clocks, TAP_IDLE);
1089 return jtag_execute_queue();
1090 }
1091
1092 /*
1093 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1094 * should be stable ... and *NOT* a shift state, otherwise free-running
1095 * jtag clocks could change the values latched by the update state.
1096 */
1097 static bool scan_is_safe(tap_state_t state)
1098 {
1099 switch (state)
1100 {
1101 case TAP_RESET:
1102 case TAP_IDLE:
1103 case TAP_DRPAUSE:
1104 case TAP_IRPAUSE:
1105 return true;
1106 default:
1107 return false;
1108 }
1109 }
1110
1111
1112 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1113 {
1114 int i;
1115 scan_field_t *fields;
1116 jtag_tap_t *tap;
1117 tap_state_t endstate;
1118
1119 if ((argc < 2) || (argc % 2))
1120 {
1121 return ERROR_COMMAND_SYNTAX_ERROR;
1122 }
1123
1124 /* optional "-endstate" "statename" at the end of the arguments,
1125 * so that e.g. IRPAUSE can let us load the data register before
1126 * entering RUN/IDLE to execute the instruction we load here.
1127 */
1128 endstate = TAP_IDLE;
1129
1130 if (argc >= 4) {
1131 /* have at least one pair of numbers. */
1132 /* is last pair the magic text? */
1133 if (0 == strcmp("-endstate", args[ argc - 2 ])) {
1134 const char *cpA;
1135 const char *cpS;
1136 cpA = args[ argc-1 ];
1137 for (endstate = 0 ; endstate < TAP_NUM_STATES ; endstate++) {
1138 cpS = tap_state_name(endstate);
1139 if (0 == strcmp(cpA, cpS)) {
1140 break;
1141 }
1142 }
1143 if (endstate >= TAP_NUM_STATES) {
1144 return ERROR_COMMAND_SYNTAX_ERROR;
1145 } else {
1146 if (!scan_is_safe(endstate))
1147 LOG_WARNING("irscan with unsafe "
1148 "endstate \"%s\"", cpA);
1149 /* found - remove the last 2 args */
1150 argc -= 2;
1151 }
1152 }
1153 }
1154
1155 int num_fields = argc / 2;
1156 size_t fields_len = sizeof(scan_field_t) * num_fields;
1157 fields = malloc(fields_len);
1158 memset(fields, 0, fields_len);
1159
1160 int retval;
1161 for (i = 0; i < num_fields; i++)
1162 {
1163 tap = jtag_tap_by_string(args[i*2]);
1164 if (tap == NULL)
1165 {
1166 int j;
1167 for (j = 0; j < i; j++)
1168 free(fields[j].out_value);
1169 free(fields);
1170 command_print(cmd_ctx, "Tap: %s unknown", args[i*2]);
1171
1172 return ERROR_FAIL;
1173 }
1174 int field_size = tap->ir_length;
1175 fields[i].tap = tap;
1176 fields[i].num_bits = field_size;
1177 fields[i].out_value = malloc(CEIL(field_size, 8));
1178
1179 uint32_t value;
1180 retval = parse_u32(args[i * 2 + 1], &value);
1181 if (ERROR_OK != retval)
1182 goto error_return;
1183 buf_set_u32(fields[i].out_value, 0, field_size, value);
1184 fields[i].in_value = NULL;
1185 }
1186
1187 /* did we have an endstate? */
1188 jtag_add_ir_scan(num_fields, fields, endstate);
1189
1190 retval = jtag_execute_queue();
1191
1192 error_return:
1193 for (i = 0; i < num_fields; i++)
1194 {
1195 if (NULL != fields[i].out_value)
1196 free(fields[i].out_value);
1197 }
1198
1199 free (fields);
1200
1201 return retval;
1202 }
1203
1204 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1205 {
1206 int retval;
1207 scan_field_t *fields;
1208 int num_fields;
1209 int field_count = 0;
1210 int i, e;
1211 jtag_tap_t *tap;
1212 tap_state_t endstate;
1213
1214 /* args[1] = device
1215 * args[2] = num_bits
1216 * args[3] = hex string
1217 * ... repeat num bits and hex string ...
1218 *
1219 * .. optionally:
1220 * args[N-2] = "-endstate"
1221 * args[N-1] = statename
1222 */
1223 if ((argc < 4) || ((argc % 2) != 0))
1224 {
1225 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1226 return JIM_ERR;
1227 }
1228
1229 endstate = TAP_IDLE;
1230
1231 script_debug(interp, "drscan", argc, args);
1232
1233 /* validate arguments as numbers */
1234 e = JIM_OK;
1235 for (i = 2; i < argc; i += 2)
1236 {
1237 long bits;
1238 const char *cp;
1239
1240 e = Jim_GetLong(interp, args[i], &bits);
1241 /* If valid - try next arg */
1242 if (e == JIM_OK) {
1243 continue;
1244 }
1245
1246 /* Not valid.. are we at the end? */
1247 if (((i + 2) != argc)) {
1248 /* nope, then error */
1249 return e;
1250 }
1251
1252 /* it could be: "-endstate FOO"
1253 * e.g. DRPAUSE so we can issue more instructions
1254 * before entering RUN/IDLE and executing them.
1255 */
1256
1257 /* get arg as a string. */
1258 cp = Jim_GetString(args[i], NULL);
1259 /* is it the magic? */
1260 if (0 == strcmp("-endstate", cp)) {
1261 /* is the statename valid? */
1262 cp = Jim_GetString(args[i + 1], NULL);
1263
1264 /* see if it is a valid state name */
1265 endstate = tap_state_by_name(cp);
1266 if (endstate < 0) {
1267 /* update the error message */
1268 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1269 } else {
1270 if (!scan_is_safe(endstate))
1271 LOG_WARNING("drscan with unsafe "
1272 "endstate \"%s\"", cp);
1273
1274 /* valid - so clear the error */
1275 e = JIM_OK;
1276 /* and remove the last 2 args */
1277 argc -= 2;
1278 }
1279 }
1280
1281 /* Still an error? */
1282 if (e != JIM_OK) {
1283 return e; /* too bad */
1284 }
1285 } /* validate args */
1286
1287 tap = jtag_tap_by_jim_obj(interp, args[1]);
1288 if (tap == NULL) {
1289 return JIM_ERR;
1290 }
1291
1292 num_fields = (argc-2)/2;
1293 fields = malloc(sizeof(scan_field_t) * num_fields);
1294 for (i = 2; i < argc; i += 2)
1295 {
1296 long bits;
1297 int len;
1298 const char *str;
1299
1300 Jim_GetLong(interp, args[i], &bits);
1301 str = Jim_GetString(args[i + 1], &len);
1302
1303 fields[field_count].tap = tap;
1304 fields[field_count].num_bits = bits;
1305 fields[field_count].out_value = malloc(CEIL(bits, 8));
1306 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
1307 fields[field_count].in_value = fields[field_count].out_value;
1308 field_count++;
1309 }
1310
1311 jtag_add_dr_scan(num_fields, fields, endstate);
1312
1313 retval = jtag_execute_queue();
1314 if (retval != ERROR_OK)
1315 {
1316 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
1317 return JIM_ERR;
1318 }
1319
1320 field_count = 0;
1321 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
1322 for (i = 2; i < argc; i += 2)
1323 {
1324 long bits;
1325 char *str;
1326
1327 Jim_GetLong(interp, args[i], &bits);
1328 str = buf_to_str(fields[field_count].in_value, bits, 16);
1329 free(fields[field_count].out_value);
1330
1331 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
1332 free(str);
1333 field_count++;
1334 }
1335
1336 Jim_SetResult(interp, list);
1337
1338 free(fields);
1339
1340 return JIM_OK;
1341 }
1342
1343
1344 static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1345 {
1346 tap_state_t states[8];
1347
1348 if ((argc < 2) || ((size_t)argc > (sizeof(states)/sizeof(*states) + 1)))
1349 {
1350 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
1351 return JIM_ERR;
1352 }
1353
1354 script_debug(interp, "pathmove", argc, args);
1355
1356 int i;
1357 for (i = 0; i < argc-1; i++)
1358 {
1359 const char *cp;
1360 cp = Jim_GetString(args[i + 1], NULL);
1361 states[i] = tap_state_by_name(cp);
1362 if (states[i] < 0)
1363 {
1364 /* update the error message */
1365 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
1366 return JIM_ERR;
1367 }
1368 }
1369
1370 if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
1371 {
1372 Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
1373 return JIM_ERR;
1374 }
1375
1376 jtag_add_pathmove(argc-2, states + 1);
1377
1378 if (jtag_execute_queue()!= ERROR_OK)
1379 {
1380 Jim_SetResultString(interp, "pathmove: failed",-1);
1381 return JIM_ERR;
1382 }
1383
1384 return JIM_OK;
1385 }
1386
1387
1388 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
1389 {
1390 script_debug(interp, "flush_count", argc, args);
1391
1392 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
1393
1394 return JIM_OK;
1395 }
1396
1397
1398 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1399 {
1400 if (argc > 1)
1401 return ERROR_COMMAND_SYNTAX_ERROR;
1402
1403 if (argc == 1)
1404 {
1405 if (strcmp(args[0], "enable") == 0)
1406 jtag_set_verify_capture_ir(true);
1407 else if (strcmp(args[0], "disable") == 0)
1408 jtag_set_verify_capture_ir(false);
1409 else
1410 return ERROR_COMMAND_SYNTAX_ERROR;
1411 }
1412
1413 const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
1414 command_print(cmd_ctx, "verify Capture-IR is %s", status);
1415
1416 return ERROR_OK;
1417 }
1418
1419 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1420 {
1421 if (argc > 1)
1422 return ERROR_COMMAND_SYNTAX_ERROR;
1423
1424 if (argc == 1)
1425 {
1426 if (strcmp(args[0], "enable") == 0)
1427 jtag_set_verify(true);
1428 else if (strcmp(args[0], "disable") == 0)
1429 jtag_set_verify(false);
1430 else
1431 return ERROR_COMMAND_SYNTAX_ERROR;
1432 }
1433
1434 const char *status = jtag_will_verify() ? "enabled": "disabled";
1435 command_print(cmd_ctx, "verify jtag capture is %s", status);
1436
1437 return ERROR_OK;
1438 }
1439
1440 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1441 {
1442 if (argc > 1)
1443 return ERROR_COMMAND_SYNTAX_ERROR;
1444
1445 if (argc == 1)
1446 {
1447 bool use_new_table;
1448 if (strcmp(args[0], "short") == 0)
1449 use_new_table = true;
1450 else if (strcmp(args[0], "long") == 0)
1451 use_new_table = false;
1452 else
1453 return ERROR_COMMAND_SYNTAX_ERROR;
1454
1455 tap_use_new_tms_table(use_new_table);
1456 }
1457
1458 command_print(cmd_ctx, "tms sequence is %s",
1459 tap_uses_new_tms_table() ? "short": "long");
1460
1461 return ERROR_OK;
1462 }