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