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