OpenOCD
openjtag.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*******************************************************************************
4  * Driver for OpenJTAG Project (www.openjtag.org) *
5  * Compatible with libftdi drivers. *
6  * *
7  * Cypress CY7C65215 support *
8  * Copyright (C) 2015 Vianney le ClĂ©ment de Saint-Marcq, Essensium NV *
9  * <vianney.leclement@essensium.com> *
10  * *
11  * Copyright (C) 2010 by Ivan Meleca <mileca@gmail.com> *
12  * *
13  * Copyright (C) 2013 by Ryan Corbin, GlueLogix Inc. <corbin.ryan@gmail.com> *
14  * Updated to work with OpenOCD v0.7.0. Fixed libftdi read speed issue. *
15  * *
16  * Based on usb_blaster.c *
17  * Copyright (C) 2009 Catalin Patulea *
18  * Copyright (C) 2006 Kolja Waschk *
19  * *
20  * And jlink.c *
21  * Copyright (C) 2008 by Spencer Oliver *
22  * spen@spen-soft.co.uk *
23  ***************************************************************************/
24 
25 /***************************************************************************
26  * Version 1.0 Tested on a MCBSTM32 board using a Cortex-M3 (stm32f103x), *
27  * GDB and Eclipse under Linux (Ubuntu 10.04) *
28  * *
29  ***************************************************************************/
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #include <jtag/interface.h>
36 #include <jtag/commands.h>
37 #include "libusb_helper.h"
38 
39 static enum {
43 
44 static const char * const openjtag_variant_names[] = {
45  "standard",
46  "cy7c65215",
47  NULL
48 };
49 
50 /*
51  * OpenJTAG-OpenOCD state conversion
52  */
71 };
72 
73 /* OPENJTAG access library includes */
74 #include "libftdi_helper.h"
75 
76 /* OpenJTAG vid/pid */
77 static uint16_t openjtag_vid = 0x0403;
78 static uint16_t openjtag_pid = 0x6001;
79 
80 static char *openjtag_device_desc;
81 
82 static struct ftdi_context ftdic;
83 
84 #define OPENJTAG_BUFFER_SIZE 504
85 #define OPENJTAG_MAX_PENDING_RESULTS 256
86 
88  uint32_t bits; /* Length in bits*/
89  struct scan_command *command; /* Corresponding scan command */
90  uint8_t *buffer;
91 };
92 
93 /* USB RX/TX buffers */
94 static int usb_tx_buf_offs;
96 static uint32_t usb_rx_buf_len;
98 
99 /* Pending readings */
102 
103 static struct libusb_device_handle *usbh;
104 
105 /* CY7C65215 model only */
106 #define CY7C65215_JTAG_REQUEST 0x40 /* bmRequestType: vendor host-to-device */
107 #define CY7C65215_JTAG_ENABLE 0xD0 /* bRequest: enable JTAG */
108 #define CY7C65215_JTAG_DISABLE 0xD1 /* bRequest: disable JTAG */
109 #define CY7C65215_JTAG_READ 0xD2 /* bRequest: read buffer */
110 #define CY7C65215_JTAG_WRITE 0xD3 /* bRequest: write buffer */
111 
112 #define CY7C65215_USB_TIMEOUT 100
113 
114 static const uint16_t cy7c65215_vids[] = {0x04b4, 0};
115 static const uint16_t cy7c65215_pids[] = {0x0007, 0};
116 
117 #define CY7C65215_JTAG_CLASS 0xff
118 #define CY7C65215_JTAG_SUBCLASS 0x04
119 
120 static unsigned int ep_in, ep_out;
121 
122 #define DEBUG_TYPE_READ 0
123 #define DEBUG_TYPE_WRITE 1
124 #define DEBUG_TYPE_OCD_READ 2
125 #define DEBUG_TYPE_BUFFER 3
126 
127 #define LINE_LEN 16
128 static void openjtag_debug_buffer(uint8_t *buffer, int length, uint8_t type)
129 {
131  return;
132 
133  char line[128];
134  char s[4];
135  int i;
136  int j;
137 
138  switch (type) {
139  case DEBUG_TYPE_READ:
140  sprintf(line, "USB READ %d bytes", length);
141  break;
142  case DEBUG_TYPE_WRITE:
143  sprintf(line, "USB WRITE %d bytes", length);
144  break;
145  case DEBUG_TYPE_OCD_READ:
146  sprintf(line, "TO OpenOCD %d bytes", length);
147  break;
148  case DEBUG_TYPE_BUFFER:
149  sprintf(line, "Buffer %d bytes", length);
150  break;
151  }
152 
153  LOG_DEBUG_USB("%s", line);
154 
155  for (i = 0; i < length; i += LINE_LEN) {
156  switch (type) {
157  case DEBUG_TYPE_READ:
158  sprintf(line, "USB READ: %04x", i);
159  break;
160  case DEBUG_TYPE_WRITE:
161  sprintf(line, "USB WRITE: %04x", i);
162  break;
163  case DEBUG_TYPE_OCD_READ:
164  sprintf(line, "TO OpenOCD: %04x", i);
165  break;
166  case DEBUG_TYPE_BUFFER:
167  sprintf(line, "BUFFER: %04x", i);
168  break;
169  }
170 
171  for (j = i; j < i + LINE_LEN && j < length; j++) {
172  sprintf(s, " %02x", buffer[j]);
173  strcat(line, s);
174  }
175  LOG_DEBUG_USB("%s", line);
176  }
177 
178 }
179 
180 static int8_t openjtag_get_tap_state(int8_t state)
181 {
182 
183  switch (state) {
184  case TAP_DREXIT2: return OPENJTAG_TAP_EXIT2_DR;
185  case TAP_DREXIT1: return OPENJTAG_TAP_EXIT1_DR;
186  case TAP_DRSHIFT: return OPENJTAG_TAP_SHIFT_DR;
187  case TAP_DRPAUSE: return OPENJTAG_TAP_PAUSE_DR;
192  case TAP_IREXIT2: return OPENJTAG_TAP_EXIT2_IR;
193  case TAP_IREXIT1: return OPENJTAG_TAP_EXIT1_IR;
194  case TAP_IRSHIFT: return OPENJTAG_TAP_SHIFT_IR;
195  case TAP_IRPAUSE: return OPENJTAG_TAP_PAUSE_IR;
196  case TAP_IDLE: return OPENJTAG_TAP_IDLE;
199  case TAP_RESET: return OPENJTAG_TAP_RESET;
200  case TAP_INVALID:
201  default: return OPENJTAG_TAP_INVALID;
202  }
203 }
204 
206  uint8_t *buf, int size, uint32_t *bytes_written)
207 {
208  int retval;
209 
211 
212  retval = ftdi_write_data(&ftdic, buf, size);
213  if (retval < 0) {
214  *bytes_written = 0;
215  LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
217  }
218 
219  *bytes_written = retval;
220 
221  return ERROR_OK;
222 }
223 
225  uint8_t *buf, int size, uint32_t *bytes_written)
226 {
227  int ret;
228 
230 
231  if (size == 0) {
232  *bytes_written = 0;
233  return ERROR_OK;
234  }
235 
239  if (ret != ERROR_OK) {
240  LOG_ERROR("vendor command failed");
241  return ret;
242  }
243 
244  if (jtag_libusb_bulk_write(usbh, ep_out, (char *)buf, size,
245  CY7C65215_USB_TIMEOUT, &ret)) {
246  LOG_ERROR("bulk write failed, error");
248  }
249  *bytes_written = ret;
250 
251  return ERROR_OK;
252 }
253 
255  uint8_t *buf, int size, uint32_t *bytes_written)
256 {
257  switch (openjtag_variant) {
259  return openjtag_buf_write_cy7c65215(buf, size, bytes_written);
260  default:
261  return openjtag_buf_write_standard(buf, size, bytes_written);
262  }
263 }
264 
266  uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
267 {
268 
269  int retval;
270  int timeout = 5;
271 
272  *bytes_read = 0;
273 
274  while ((*bytes_read < qty) && timeout--) {
275  retval = ftdi_read_data(&ftdic, buf + *bytes_read,
276  qty - *bytes_read);
277  if (retval < 0) {
278  *bytes_read = 0;
279  LOG_DEBUG_IO("ftdi_read_data: %s",
280  ftdi_get_error_string(&ftdic));
282  }
283  *bytes_read += retval;
284  }
285 
286  openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
287 
288  return ERROR_OK;
289 }
290 
292  uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
293 {
294  int ret;
295 
296  if (qty == 0) {
297  *bytes_read = 0;
298  goto out;
299  }
300 
302  CY7C65215_JTAG_READ, qty, 0,
304  if (ret != ERROR_OK) {
305  LOG_ERROR("vendor command failed");
306  return ret;
307  }
308 
309  if (jtag_libusb_bulk_read(usbh, ep_in, (char *)buf, qty,
310  CY7C65215_USB_TIMEOUT, &ret)) {
311  LOG_ERROR("bulk read failed, error");
313  }
314  *bytes_read = ret;
315 
316 out:
317  openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
318 
319  return ERROR_OK;
320 }
321 
322 static int openjtag_buf_read(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
323 {
324  switch (openjtag_variant) {
326  return openjtag_buf_read_cy7c65215(buf, qty, bytes_read);
327  default:
328  return openjtag_buf_read_standard(buf, qty, bytes_read);
329  }
330 }
331 
332 static int openjtag_sendcommand(uint8_t cmd)
333 {
334  uint32_t written;
335  return openjtag_buf_write(&cmd, 1, &written);
336 }
337 
338 static int openjtag_speed(int speed)
339 {
340  int clockcmd;
341  switch (speed) {
342  case 48000:
343  clockcmd = 0x00;
344  break;
345  case 24000:
346  clockcmd = 0x20;
347  break;
348  case 12000:
349  clockcmd = 0x40;
350  break;
351  case 6000:
352  clockcmd = 0x60;
353  break;
354  case 3000:
355  clockcmd = 0x80;
356  break;
357  case 1500:
358  clockcmd = 0xA0;
359  break;
360  case 750:
361  clockcmd = 0xC0;
362  break;
363  case 375:
364  clockcmd = 0xE0;
365  break;
366  default:
367  clockcmd = 0xE0;
368  LOG_WARNING("adapter speed not recognized, reverting to 375 kHz");
369  break;
370  }
371  openjtag_sendcommand(clockcmd);
372 
373  return ERROR_OK;
374 }
375 
376 static int openjtag_init_standard(void)
377 {
378  uint8_t latency_timer;
379 
380  /* Open by device description */
381  if (!openjtag_device_desc) {
382  LOG_WARNING("no openjtag device description specified, "
383  "using default 'Open JTAG Project'");
384  openjtag_device_desc = "Open JTAG Project";
385  }
386 
387  if (ftdi_init(&ftdic) < 0)
388  return ERROR_JTAG_INIT_FAILED;
389 
390  /* context, vendor id, product id, description, serial id */
391  if (ftdi_usb_open_desc(&ftdic, openjtag_vid, openjtag_pid, openjtag_device_desc, NULL) < 0) {
392  LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
393  return ERROR_JTAG_INIT_FAILED;
394  }
395 
396  if (ftdi_usb_reset(&ftdic) < 0) {
397  LOG_ERROR("unable to reset ftdi device");
398  return ERROR_JTAG_INIT_FAILED;
399  }
400 
401  if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
402  LOG_ERROR("unable to set latency timer");
403  return ERROR_JTAG_INIT_FAILED;
404  }
405 
406  if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
407  LOG_ERROR("unable to get latency timer");
408  return ERROR_JTAG_INIT_FAILED;
409  }
410  LOG_DEBUG("current latency timer: %u", latency_timer);
411 
412  ftdi_disable_bitbang(&ftdic);
413  /* was (3000000 / 4) with a comment about a bug in libftdi when using high baudrate */
414  if (ftdi_set_baudrate(&ftdic, 3000000) < 0) {
415  LOG_ERROR("Can't set baud rate to max: %s",
416  ftdi_get_error_string(&ftdic));
418  }
419 
420  if (ftdi_tcioflush(&ftdic) < 0) {
421  LOG_ERROR("ftdi flush: %s", ftdic.error_str);
422  return ERROR_JTAG_INIT_FAILED;
423  }
424 
425  return ERROR_OK;
426 }
427 
428 static int openjtag_init_cy7c65215(void)
429 {
430  int ret;
431 
432  usbh = NULL;
434  if (ret != ERROR_OK) {
435  LOG_ERROR("unable to open cy7c65215 device");
436  goto err;
437  }
438 
441  CY7C65215_JTAG_SUBCLASS, -1, LIBUSB_TRANSFER_TYPE_BULK);
442  if (ret != ERROR_OK) {
443  LOG_ERROR("unable to claim JTAG interface");
444  goto err;
445  }
446 
450  0, 0, NULL, 0, CY7C65215_USB_TIMEOUT, NULL);
451  if (ret != ERROR_OK) {
452  LOG_ERROR("could not enable JTAG module");
453  goto err;
454  }
455 
456  return ERROR_OK;
457 
458 err:
459  if (usbh)
461  return ret;
462 }
463 
464 static int openjtag_init(void)
465 {
466  int ret;
467 
468  usb_tx_buf_offs = 0;
469  usb_rx_buf_len = 0;
471 
472  switch (openjtag_variant) {
474  ret = openjtag_init_cy7c65215();
475  break;
476  default:
477  ret = openjtag_init_standard();
478  }
479  if (ret != ERROR_OK)
480  return ret;
481 
482  openjtag_speed(375); /* Start at slowest adapter speed */
483  openjtag_sendcommand(0x75); /* MSB */
484 
485  return ERROR_OK;
486 }
487 
488 static int openjtag_quit_standard(void)
489 {
490  ftdi_usb_close(&ftdic);
491  ftdi_deinit(&ftdic);
492 
493  return ERROR_OK;
494 }
495 
496 static int openjtag_quit_cy7c65215(void)
497 {
498  int ret;
499 
503  0, 0, NULL, 0, CY7C65215_USB_TIMEOUT, NULL);
504  if (ret != ERROR_OK)
505  LOG_WARNING("could not disable JTAG module");
506 
508 
509  return ERROR_OK;
510 }
511 
512 static int openjtag_quit(void)
513 {
514  switch (openjtag_variant) {
516  return openjtag_quit_cy7c65215();
517  default:
518  return openjtag_quit_standard();
519  }
520 }
521 
522 static void openjtag_write_tap_buffer(void)
523 {
524  uint32_t written;
525  uint32_t rx_expected = 0;
526 
527  /* calculate expected number of return bytes */
528  for (int tx_offs = 0; tx_offs < usb_tx_buf_offs; tx_offs++) {
529  if ((usb_tx_buf[tx_offs] & 0x0F) == 6) {
530  rx_expected++;
531  tx_offs++;
532  } else if ((usb_tx_buf[tx_offs] & 0x0F) == 2) {
533  rx_expected++;
534  }
535  }
536 
539 
540  usb_tx_buf_offs = 0;
541 }
542 
544 {
546 
547  int res_count = 0;
548 
550 
551  int count;
552  int rx_offs = 0;
553  int len;
554 
555  /* for every pending result */
556  while (res_count < openjtag_scan_result_count) {
557 
558  /* get sent bits */
559  len = openjtag_scan_result_buffer[res_count].bits;
560 
561  count = 0;
562 
563  uint8_t *buffer = openjtag_scan_result_buffer[res_count].buffer;
564 
565  while (len > 0) {
566  if (len <= 8 && openjtag_variant != OPENJTAG_VARIANT_CY7C65215) {
567  LOG_DEBUG_IO("bits < 8 buf = 0x%X, will be 0x%X",
568  usb_rx_buf[rx_offs], usb_rx_buf[rx_offs] >> (8 - len));
569  buffer[count] = usb_rx_buf[rx_offs] >> (8 - len);
570  len = 0;
571  } else {
572  buffer[count] = usb_rx_buf[rx_offs];
573  len -= 8;
574  }
575 
576  rx_offs++;
577  count++;
578  }
579 
582 
584 
585  free(openjtag_scan_result_buffer[res_count].buffer);
586 
587  res_count++;
588  }
589  }
590 
592 
593  return ERROR_OK;
594 }
595 
596 static void openjtag_add_byte(char buf)
597 {
598 
600  LOG_DEBUG_IO("Forcing execute_tap_queue");
601  LOG_DEBUG_IO("TX Buff offs=%d", usb_tx_buf_offs);
603  }
604 
606  usb_tx_buf_offs++;
607 }
608 
609 static void openjtag_add_scan(uint8_t *buffer, int length, struct scan_command *scan_cmd)
610 {
611 
612  /* Ensure space to send long chains */
613  /* We add two byte for each eight (or less) bits, one for command, one for data */
615  LOG_DEBUG_IO("Forcing execute_tap_queue from scan");
616  LOG_DEBUG_IO("TX Buff offs=%d len=%d", usb_tx_buf_offs, DIV_ROUND_UP(length, 8) * 2);
618  }
619 
623 
624  uint8_t command;
625  uint8_t bits;
626  int count = 0;
627  while (length) {
628 
629  /* write command */
630  command = 6;
631 
632  /* last bits? */
633  if (length <= 8) {
634  /* tms high */
635  command |= (1 << 4);
636 
637  /* bits to transfer */
638  bits = (length - 1);
639  command |= bits << 5;
640  length = 0;
641  } else {
642  /* whole byte */
643 
644  /* bits to transfer */
645  command |= (7 << 5);
646  length -= 8;
647  }
648 
651  count++;
652  }
653 
655 }
656 
658 {
659 
660  LOG_DEBUG_IO("reset trst: %i srst %i",
661  cmd->cmd.reset->trst, cmd->cmd.reset->srst);
662 
663  uint8_t buf = 0x00;
664 
665  /* Pull SRST low for 5 TCLK cycles */
666  if (cmd->cmd.reset->srst) {
667  buf |= 0x04;
668  buf |= 0x05 << 4;
669  openjtag_add_byte(buf);
670  }
671 }
672 
674 {
675  jtag_sleep(cmd->cmd.sleep->us);
676 }
677 
678 static void openjtag_set_state(uint8_t openocd_state)
679 {
680  uint8_t state = openjtag_get_tap_state(openocd_state);
681 
682  uint8_t buf = 0;
683 
684  if (state != OPENJTAG_TAP_RESET) {
685  buf = 0x01;
686  buf |= state << 4;
687  } else {
688  /* Force software TLR */
689  buf = 0x03;
690  }
691 
692  openjtag_add_byte(buf);
693 }
694 
696 {
697  LOG_DEBUG_IO("state move to %i", cmd->cmd.statemove->end_state);
698 
699  tap_set_end_state(cmd->cmd.statemove->end_state);
700 
701  openjtag_set_state(cmd->cmd.statemove->end_state);
702 
704 }
705 
706 
708 {
709 
710  int scan_size, old_state;
711  uint8_t *buffer;
712 
713  LOG_DEBUG_IO("scan ends in %s", tap_state_name(cmd->cmd.scan->end_state));
714 
715  /* get scan info */
716  tap_set_end_state(cmd->cmd.scan->end_state);
717  scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
718 
719  openjtag_debug_buffer(buffer, (scan_size + 7) / 8, DEBUG_TYPE_BUFFER);
720 
721  /* set state */
722  old_state = tap_get_end_state();
723  openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
724  tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
725  tap_set_end_state(old_state);
726 
727  openjtag_add_scan(buffer, scan_size, cmd->cmd.scan);
728 
729  openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
730  tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
731 
732  if (tap_get_state() != tap_get_end_state()) {
735  }
736 }
737 
739 {
740 
741  enum tap_state end_state = cmd->cmd.runtest->end_state;
742  tap_set_end_state(end_state);
743 
744  /* only do a state_move when we're not already in IDLE */
745  if (tap_get_state() != TAP_IDLE) {
748  }
749 
751  cmd->cmd.runtest->num_cycles) {
752  uint8_t command;
753  unsigned int num_cycles = cmd->cmd.runtest->num_cycles;
754 
755  do {
756  const unsigned int num_cycles_cmd = MIN(num_cycles, 16);
757 
758  command = 7;
759  command |= ((num_cycles_cmd - 1) & 0x0F) << 4;
760 
762  num_cycles -= num_cycles_cmd;
763  } while (num_cycles > 0);
764  }
765 
766  tap_set_end_state(end_state);
767  if (tap_get_end_state() != tap_get_state()) {
768  openjtag_set_state(end_state);
769  tap_set_state(end_state);
770  }
771 }
772 
774 {
775  LOG_DEBUG_IO("openjtag_execute_command %i", cmd->type);
776  switch (cmd->type) {
777  case JTAG_RESET:
779  break;
780  case JTAG_SLEEP:
782  break;
783  case JTAG_TLR_RESET:
785  break;
786  case JTAG_SCAN:
788  break;
789  case JTAG_RUNTEST:
791  break;
792  case JTAG_PATHMOVE:
793  /* jlink_execute_pathmove(cmd); break; */
794  default:
795  LOG_ERROR("BUG: unknown Open JTAG command type encountered");
796  exit(-1);
797  }
798 }
799 
800 static int openjtag_execute_queue(struct jtag_command *cmd_queue)
801 {
802  struct jtag_command *cmd = cmd_queue;
803 
804  while (cmd) {
806  cmd = cmd->next;
807  }
808 
810 }
811 
812 static int openjtag_speed_div(int speed, int *khz)
813 {
814  *khz = speed;
815 
816  return ERROR_OK;
817 }
818 
819 static int openjtag_khz(int khz, int *jtag_speed)
820 {
821 
822  if (khz >= 48000)
823  *jtag_speed = 48000;
824  else if (khz >= 24000)
825  *jtag_speed = 24000;
826  else if (khz >= 12000)
827  *jtag_speed = 12000;
828  else if (khz >= 6000)
829  *jtag_speed = 6000;
830  else if (khz >= 3000)
831  *jtag_speed = 3000;
832  else if (khz >= 1500)
833  *jtag_speed = 1500;
834  else if (khz >= 750)
835  *jtag_speed = 750;
836  else
837  *jtag_speed = 375;
838 
839  return ERROR_OK;
840 }
841 
842 COMMAND_HANDLER(openjtag_handle_device_desc_command)
843 {
844  if (CMD_ARGC == 1)
845  openjtag_device_desc = strdup(CMD_ARGV[0]);
846  else
847  LOG_ERROR("require exactly one argument to "
848  "openjtag_device_desc <description>");
849  return ERROR_OK;
850 }
851 
852 COMMAND_HANDLER(openjtag_handle_variant_command)
853 {
854  if (CMD_ARGC == 1) {
855  const char * const *name = openjtag_variant_names;
856  int variant = 0;
857  for (; *name; name++, variant++) {
858  if (strcasecmp(CMD_ARGV[0], *name) == 0) {
859  openjtag_variant = variant;
860  return ERROR_OK;
861  }
862  }
863  LOG_ERROR("unknown openjtag variant '%s'", CMD_ARGV[0]);
864  } else {
865  LOG_ERROR("require exactly one argument to "
866  "openjtag_variant <variant>");
867  }
868  return ERROR_OK;
869 }
870 
871 COMMAND_HANDLER(openjtag_handle_vid_pid_command)
872 {
873  if (CMD_ARGC != 2)
875 
878 
879  return ERROR_OK;
880 }
881 
882 static const struct command_registration openjtag_subcommand_handlers[] = {
883  {
884  .name = "device_desc",
885  .handler = openjtag_handle_device_desc_command,
886  .mode = COMMAND_CONFIG,
887  .help = "set the USB device description of the OpenJTAG",
888  .usage = "description-string",
889  },
890  {
891  .name = "variant",
892  .handler = openjtag_handle_variant_command,
893  .mode = COMMAND_CONFIG,
894  .help = "set the OpenJTAG variant",
895  .usage = "variant-string",
896  },
897  {
898  .name = "vid_pid",
899  .handler = openjtag_handle_vid_pid_command,
900  .mode = COMMAND_CONFIG,
901  .help = "USB VID and PID of the adapter",
902  .usage = "vid pid",
903  },
905 };
906 
907 static const struct command_registration openjtag_command_handlers[] = {
908  {
909  .name = "openjtag",
910  .mode = COMMAND_ANY,
911  .help = "perform openjtag management",
913  .usage = "",
914  },
916 };
917 
918 static struct jtag_interface openjtag_interface = {
920 };
921 
923  .name = "openjtag",
924  .transport_ids = TRANSPORT_JTAG,
925  .transport_preferred_id = TRANSPORT_JTAG,
926  .commands = openjtag_command_handlers,
927 
928  .init = openjtag_init,
929  .quit = openjtag_quit,
930  .speed = openjtag_speed,
931  .khz = openjtag_khz,
932  .speed_div = openjtag_speed_div,
933 
934  .jtag_ops = &openjtag_interface,
935 };
const char * name
Definition: armv4_5.c:76
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
int jtag_build_buffer(const struct scan_command *cmd, uint8_t **buffer)
Definition: commands.c:192
int jtag_read_buffer(uint8_t *buffer, const struct scan_command *cmd)
Definition: commands.c:230
@ JTAG_TLR_RESET
Definition: commands.h:137
@ JTAG_SCAN
Definition: commands.h:129
@ JTAG_PATHMOVE
Definition: commands.h:140
@ JTAG_RUNTEST
Definition: commands.h:138
@ JTAG_SLEEP
Definition: commands.h:141
@ JTAG_RESET
Definition: commands.h:139
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint8_t type
Definition: esp_usb_jtag.c:0
uint8_t length
Definition: esp_usb_jtag.c:1
enum tap_state tap_get_end_state(void)
For more information,.
Definition: interface.c:56
const char * tap_state_name(enum tap_state state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
Definition: interface.c:344
void tap_set_end_state(enum tap_state new_end_state)
This function sets the state of an "end state follower" which tracks the state that any cable driver ...
Definition: interface.c:48
enum tap_state tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
Definition: interface.c:37
#define tap_set_state(new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
Definition: interface.h:50
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1075
#define ERROR_JTAG_DEVICE_ERROR
Definition: jtag.h:558
tap_state
Defines JTAG Test Access Port states.
Definition: jtag.h:37
@ TAP_IRCAPTURE
Definition: jtag.h:55
@ TAP_RESET
Definition: jtag.h:56
@ TAP_DRCAPTURE
Definition: jtag.h:47
@ TAP_DRSELECT
Definition: jtag.h:48
@ TAP_DREXIT1
Definition: jtag.h:42
@ TAP_IREXIT1
Definition: jtag.h:50
@ TAP_DRPAUSE
Definition: jtag.h:44
@ TAP_IRSELECT
Definition: jtag.h:45
@ TAP_IRUPDATE
Definition: jtag.h:54
@ TAP_IREXIT2
Definition: jtag.h:49
@ TAP_IRSHIFT
Definition: jtag.h:51
@ TAP_DREXIT2
Definition: jtag.h:41
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_DRSHIFT
Definition: jtag.h:43
@ TAP_IRPAUSE
Definition: jtag.h:52
@ TAP_DRUPDATE
Definition: jtag.h:46
@ TAP_INVALID
Definition: jtag.h:38
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:552
static int ftdi_tcioflush(struct ftdi_context *ftdi)
int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[], const char *product, struct libusb_device_handle **out, adapter_get_alternate_serial_fn adapter_get_alternate_serial)
int jtag_libusb_bulk_write(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int jtag_libusb_control_transfer(struct libusb_device_handle *dev, uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, char *bytes, uint16_t size, unsigned int timeout, int *transferred)
void jtag_libusb_close(struct libusb_device_handle *dev)
int jtag_libusb_bulk_read(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int jtag_libusb_choose_interface(struct libusb_device_handle *devh, unsigned int *usb_read_ep, unsigned int *usb_write_ep, int bclass, int subclass, int protocol, int trans_type)
Find the first interface optionally matching class, subclass and protocol and claim it.
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:109
#define LOG_DEBUG_USB(expr,...)
Definition: log.h:107
#define LOG_WARNING(expr ...)
Definition: log.h:137
#define LOG_ERROR(expr ...)
Definition: log.h:140
#define LOG_LEVEL_IS(FOO)
Definition: log.h:105
#define LOG_DEBUG(expr ...)
Definition: log.h:117
#define ERROR_OK
Definition: log.h:175
@ LOG_LVL_DEBUG_USB
Definition: log.h:53
static void openjtag_debug_buffer(uint8_t *buffer, int length, uint8_t type)
Definition: openjtag.c:128
static int openjtag_quit_standard(void)
Definition: openjtag.c:488
static int openjtag_khz(int khz, int *jtag_speed)
Definition: openjtag.c:819
@ OPENJTAG_VARIANT_STANDARD
Definition: openjtag.c:40
@ OPENJTAG_VARIANT_CY7C65215
Definition: openjtag.c:41
static uint8_t usb_tx_buf[OPENJTAG_BUFFER_SIZE]
Definition: openjtag.c:95
static const uint16_t cy7c65215_vids[]
Definition: openjtag.c:114
static int8_t openjtag_get_tap_state(int8_t state)
Definition: openjtag.c:180
static int openjtag_quit(void)
Definition: openjtag.c:512
#define CY7C65215_JTAG_ENABLE
Definition: openjtag.c:107
static struct openjtag_scan_result openjtag_scan_result_buffer[OPENJTAG_MAX_PENDING_RESULTS]
Definition: openjtag.c:100
static void openjtag_add_byte(char buf)
Definition: openjtag.c:596
static int openjtag_buf_read_cy7c65215(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
Definition: openjtag.c:291
#define CY7C65215_JTAG_WRITE
Definition: openjtag.c:110
static struct jtag_interface openjtag_interface
Definition: openjtag.c:918
static int openjtag_buf_read_standard(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
Definition: openjtag.c:265
#define CY7C65215_JTAG_DISABLE
Definition: openjtag.c:108
static char * openjtag_device_desc
Definition: openjtag.c:80
static int openjtag_buf_read(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
Definition: openjtag.c:322
static uint16_t openjtag_vid
Definition: openjtag.c:77
static int openjtag_buf_write_standard(uint8_t *buf, int size, uint32_t *bytes_written)
Definition: openjtag.c:205
static void openjtag_write_tap_buffer(void)
Definition: openjtag.c:522
#define CY7C65215_USB_TIMEOUT
Definition: openjtag.c:112
static void openjtag_execute_command(struct jtag_command *cmd)
Definition: openjtag.c:773
static const struct command_registration openjtag_command_handlers[]
Definition: openjtag.c:907
#define LINE_LEN
Definition: openjtag.c:127
static const uint16_t cy7c65215_pids[]
Definition: openjtag.c:115
#define OPENJTAG_BUFFER_SIZE
Definition: openjtag.c:84
#define CY7C65215_JTAG_SUBCLASS
Definition: openjtag.c:118
static void openjtag_execute_runtest(struct jtag_command *cmd)
Definition: openjtag.c:738
static int openjtag_execute_tap_queue(void)
Definition: openjtag.c:543
static uint32_t usb_rx_buf_len
Definition: openjtag.c:96
static void openjtag_execute_scan(struct jtag_command *cmd)
Definition: openjtag.c:707
static int openjtag_speed_div(int speed, int *khz)
Definition: openjtag.c:812
static uint16_t openjtag_pid
Definition: openjtag.c:78
static int openjtag_scan_result_count
Definition: openjtag.c:101
static int usb_tx_buf_offs
Definition: openjtag.c:94
#define CY7C65215_JTAG_READ
Definition: openjtag.c:109
static const char *const openjtag_variant_names[]
Definition: openjtag.c:44
static int openjtag_buf_write_cy7c65215(uint8_t *buf, int size, uint32_t *bytes_written)
Definition: openjtag.c:224
#define DEBUG_TYPE_READ
Definition: openjtag.c:122
static int openjtag_speed(int speed)
Definition: openjtag.c:338
static const struct command_registration openjtag_subcommand_handlers[]
Definition: openjtag.c:882
#define DEBUG_TYPE_WRITE
Definition: openjtag.c:123
static void openjtag_execute_sleep(struct jtag_command *cmd)
Definition: openjtag.c:673
static uint8_t usb_rx_buf[OPENJTAG_BUFFER_SIZE]
Definition: openjtag.c:97
static void openjtag_execute_statemove(struct jtag_command *cmd)
Definition: openjtag.c:695
struct adapter_driver openjtag_adapter_driver
Definition: openjtag.c:922
static struct libusb_device_handle * usbh
Definition: openjtag.c:103
static unsigned int ep_out
Definition: openjtag.c:120
#define CY7C65215_JTAG_CLASS
Definition: openjtag.c:117
static int openjtag_buf_write(uint8_t *buf, int size, uint32_t *bytes_written)
Definition: openjtag.c:254
static unsigned int ep_in
Definition: openjtag.c:120
static int openjtag_execute_queue(struct jtag_command *cmd_queue)
Definition: openjtag.c:800
openjtag_tap_state
Definition: openjtag.c:53
@ OPENJTAG_TAP_PAUSE_IR
Definition: openjtag.c:68
@ OPENJTAG_TAP_SELECT_DR
Definition: openjtag.c:57
@ OPENJTAG_TAP_EXIT2_DR
Definition: openjtag.c:62
@ OPENJTAG_TAP_SELECT_IR
Definition: openjtag.c:64
@ OPENJTAG_TAP_CAPURE_IR
Definition: openjtag.c:65
@ OPENJTAG_TAP_RESET
Definition: openjtag.c:55
@ OPENJTAG_TAP_SHIFT_IR
Definition: openjtag.c:66
@ OPENJTAG_TAP_UPDATE_IR
Definition: openjtag.c:70
@ OPENJTAG_TAP_IDLE
Definition: openjtag.c:56
@ OPENJTAG_TAP_EXIT2_IR
Definition: openjtag.c:69
@ OPENJTAG_TAP_EXIT1_IR
Definition: openjtag.c:67
@ OPENJTAG_TAP_INVALID
Definition: openjtag.c:54
@ OPENJTAG_TAP_UPDATE_DR
Definition: openjtag.c:63
@ OPENJTAG_TAP_SHIFT_DR
Definition: openjtag.c:59
@ OPENJTAG_TAP_EXIT1_DR
Definition: openjtag.c:60
@ OPENJTAG_TAP_PAUSE_DR
Definition: openjtag.c:61
@ OPENJTAG_TAP_CAPTURE_DR
Definition: openjtag.c:58
static int openjtag_quit_cy7c65215(void)
Definition: openjtag.c:496
static int openjtag_sendcommand(uint8_t cmd)
Definition: openjtag.c:332
static struct ftdi_context ftdic
Definition: openjtag.c:82
#define DEBUG_TYPE_OCD_READ
Definition: openjtag.c:124
static int openjtag_init_cy7c65215(void)
Definition: openjtag.c:428
static int openjtag_init_standard(void)
Definition: openjtag.c:376
static enum @31 openjtag_variant
COMMAND_HANDLER(openjtag_handle_device_desc_command)
Definition: openjtag.c:842
#define CY7C65215_JTAG_REQUEST
Definition: openjtag.c:106
static void openjtag_add_scan(uint8_t *buffer, int length, struct scan_command *scan_cmd)
Definition: openjtag.c:609
#define DEBUG_TYPE_BUFFER
Definition: openjtag.c:125
static void openjtag_set_state(uint8_t openocd_state)
Definition: openjtag.c:678
#define OPENJTAG_MAX_PENDING_RESULTS
Definition: openjtag.c:85
static int openjtag_init(void)
Definition: openjtag.c:464
static void openjtag_execute_reset(struct jtag_command *cmd)
Definition: openjtag.c:657
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
#define MIN(a, b)
Definition: replacements.h:22
Represents a driver for a debugging interface.
Definition: interface.h:208
const char *const name
The name of the interface driver.
Definition: interface.h:210
const char * name
Definition: command.h:239
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
Represents a driver for a debugging interface.
Definition: interface.h:183
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
Definition: interface.h:196
struct scan_command * command
Definition: openjtag.c:89
uint8_t * buffer
Definition: openjtag.c:90
The scan_command provide a means of encapsulating a set of scan_field structures that should be scann...
Definition: commands.h:35
Definition: psoc6.c:83
#define TRANSPORT_JTAG
Definition: transport.h:19
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22