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/adapter.h>
36 #include <jtag/interface.h>
37 #include <jtag/commands.h>
38 #include "libusb_helper.h"
39 
40 static enum {
44 
45 static const char * const openjtag_variant_names[] = {
46  "standard",
47  "cy7c65215",
48  NULL
49 };
50 
51 /*
52  * OpenJTAG-OpenOCD state conversion
53  */
72 };
73 
74 /* OPENJTAG access library includes */
75 #include "libftdi_helper.h"
76 
77 /* OpenJTAG vid/pid */
78 static uint16_t openjtag_vids[] = {0x0403, 0};
79 static uint16_t openjtag_pids[] = {0x6001, 0};
80 
81 static char *openjtag_device_desc;
82 
83 static struct ftdi_context ftdic;
84 
85 #define OPENJTAG_BUFFER_SIZE 504
86 #define OPENJTAG_MAX_PENDING_RESULTS 256
87 
89  uint32_t bits; /* Length in bits*/
90  struct scan_command *command; /* Corresponding scan command */
91  uint8_t *buffer;
92 };
93 
94 /* USB RX/TX buffers */
95 static int usb_tx_buf_offs;
97 static uint32_t usb_rx_buf_len;
99 
100 /* Pending readings */
103 
104 static struct libusb_device_handle *usbh;
105 
106 /* CY7C65215 model only */
107 #define CY7C65215_JTAG_REQUEST 0x40 /* bmRequestType: vendor host-to-device */
108 #define CY7C65215_JTAG_ENABLE 0xD0 /* bRequest: enable JTAG */
109 #define CY7C65215_JTAG_DISABLE 0xD1 /* bRequest: disable JTAG */
110 #define CY7C65215_JTAG_READ 0xD2 /* bRequest: read buffer */
111 #define CY7C65215_JTAG_WRITE 0xD3 /* bRequest: write buffer */
112 
113 #define CY7C65215_USB_TIMEOUT 100
114 
115 static const uint16_t cy7c65215_vids[] = {0x04b4, 0};
116 static const uint16_t cy7c65215_pids[] = {0x0007, 0};
117 
118 #define CY7C65215_JTAG_CLASS 0xff
119 #define CY7C65215_JTAG_SUBCLASS 0x04
120 
121 static unsigned int ep_in, ep_out;
122 
123 #define DEBUG_TYPE_READ 0
124 #define DEBUG_TYPE_WRITE 1
125 #define DEBUG_TYPE_OCD_READ 2
126 #define DEBUG_TYPE_BUFFER 3
127 
128 #define LINE_LEN 16
129 static void openjtag_debug_buffer(uint8_t *buffer, int length, uint8_t type)
130 {
132  return;
133 
134  char line[128];
135  char s[4];
136  int i;
137  int j;
138 
139  switch (type) {
140  case DEBUG_TYPE_READ:
141  sprintf(line, "USB READ %d bytes", length);
142  break;
143  case DEBUG_TYPE_WRITE:
144  sprintf(line, "USB WRITE %d bytes", length);
145  break;
146  case DEBUG_TYPE_OCD_READ:
147  sprintf(line, "TO OpenOCD %d bytes", length);
148  break;
149  case DEBUG_TYPE_BUFFER:
150  sprintf(line, "Buffer %d bytes", length);
151  break;
152  }
153 
154  LOG_DEBUG_USB("%s", line);
155 
156  for (i = 0; i < length; i += LINE_LEN) {
157  switch (type) {
158  case DEBUG_TYPE_READ:
159  sprintf(line, "USB READ: %04x", i);
160  break;
161  case DEBUG_TYPE_WRITE:
162  sprintf(line, "USB WRITE: %04x", i);
163  break;
164  case DEBUG_TYPE_OCD_READ:
165  sprintf(line, "TO OpenOCD: %04x", i);
166  break;
167  case DEBUG_TYPE_BUFFER:
168  sprintf(line, "BUFFER: %04x", i);
169  break;
170  }
171 
172  for (j = i; j < i + LINE_LEN && j < length; j++) {
173  sprintf(s, " %02x", buffer[j]);
174  strcat(line, s);
175  }
176  LOG_DEBUG_USB("%s", line);
177  }
178 
179 }
180 
181 static int8_t openjtag_get_tap_state(int8_t state)
182 {
183 
184  switch (state) {
185  case TAP_DREXIT2: return OPENJTAG_TAP_EXIT2_DR;
186  case TAP_DREXIT1: return OPENJTAG_TAP_EXIT1_DR;
187  case TAP_DRSHIFT: return OPENJTAG_TAP_SHIFT_DR;
188  case TAP_DRPAUSE: return OPENJTAG_TAP_PAUSE_DR;
193  case TAP_IREXIT2: return OPENJTAG_TAP_EXIT2_IR;
194  case TAP_IREXIT1: return OPENJTAG_TAP_EXIT1_IR;
195  case TAP_IRSHIFT: return OPENJTAG_TAP_SHIFT_IR;
196  case TAP_IRPAUSE: return OPENJTAG_TAP_PAUSE_IR;
197  case TAP_IDLE: return OPENJTAG_TAP_IDLE;
200  case TAP_RESET: return OPENJTAG_TAP_RESET;
201  case TAP_INVALID:
202  default: return OPENJTAG_TAP_INVALID;
203  }
204 }
205 
207  uint8_t *buf, int size, uint32_t *bytes_written)
208 {
209  int retval;
210 
212 
213  retval = ftdi_write_data(&ftdic, buf, size);
214  if (retval < 0) {
215  *bytes_written = 0;
216  LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
218  }
219 
220  *bytes_written = retval;
221 
222  return ERROR_OK;
223 }
224 
226  uint8_t *buf, int size, uint32_t *bytes_written)
227 {
228  int ret;
229 
231 
232  if (size == 0) {
233  *bytes_written = 0;
234  return ERROR_OK;
235  }
236 
240  if (ret != ERROR_OK) {
241  LOG_ERROR("vendor command failed");
242  return ret;
243  }
244 
245  if (jtag_libusb_bulk_write(usbh, ep_out, (char *)buf, size,
246  CY7C65215_USB_TIMEOUT, &ret)) {
247  LOG_ERROR("bulk write failed, error");
249  }
250  *bytes_written = ret;
251 
252  return ERROR_OK;
253 }
254 
256  uint8_t *buf, int size, uint32_t *bytes_written)
257 {
258  switch (openjtag_variant) {
260  return openjtag_buf_write_cy7c65215(buf, size, bytes_written);
261  default:
262  return openjtag_buf_write_standard(buf, size, bytes_written);
263  }
264 }
265 
267  uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
268 {
269 
270  int retval;
271  int timeout = 5;
272 
273  *bytes_read = 0;
274 
275  while ((*bytes_read < qty) && timeout--) {
276  retval = ftdi_read_data(&ftdic, buf + *bytes_read,
277  qty - *bytes_read);
278  if (retval < 0) {
279  *bytes_read = 0;
280  LOG_DEBUG_IO("ftdi_read_data: %s",
281  ftdi_get_error_string(&ftdic));
283  }
284  *bytes_read += retval;
285  }
286 
287  openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
288 
289  return ERROR_OK;
290 }
291 
293  uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
294 {
295  int ret;
296 
297  if (qty == 0) {
298  *bytes_read = 0;
299  goto out;
300  }
301 
303  CY7C65215_JTAG_READ, qty, 0,
305  if (ret != ERROR_OK) {
306  LOG_ERROR("vendor command failed");
307  return ret;
308  }
309 
310  if (jtag_libusb_bulk_read(usbh, ep_in, (char *)buf, qty,
311  CY7C65215_USB_TIMEOUT, &ret)) {
312  LOG_ERROR("bulk read failed, error");
314  }
315  *bytes_read = ret;
316 
317 out:
318  openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
319 
320  return ERROR_OK;
321 }
322 
323 static int openjtag_buf_read(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
324 {
325  switch (openjtag_variant) {
327  return openjtag_buf_read_cy7c65215(buf, qty, bytes_read);
328  default:
329  return openjtag_buf_read_standard(buf, qty, bytes_read);
330  }
331 }
332 
333 static int openjtag_sendcommand(uint8_t cmd)
334 {
335  uint32_t written;
336  return openjtag_buf_write(&cmd, 1, &written);
337 }
338 
339 static int openjtag_speed(int speed)
340 {
341  int clockcmd;
342  switch (speed) {
343  case 48000:
344  clockcmd = 0x00;
345  break;
346  case 24000:
347  clockcmd = 0x20;
348  break;
349  case 12000:
350  clockcmd = 0x40;
351  break;
352  case 6000:
353  clockcmd = 0x60;
354  break;
355  case 3000:
356  clockcmd = 0x80;
357  break;
358  case 1500:
359  clockcmd = 0xA0;
360  break;
361  case 750:
362  clockcmd = 0xC0;
363  break;
364  case 375:
365  clockcmd = 0xE0;
366  break;
367  default:
368  clockcmd = 0xE0;
369  LOG_WARNING("adapter speed not recognized, reverting to 375 kHz");
370  break;
371  }
372  openjtag_sendcommand(clockcmd);
373 
374  return ERROR_OK;
375 }
376 
377 static int openjtag_init_standard(void)
378 {
379  uint8_t latency_timer;
380 
381  /* Open by device description */
382  if (!openjtag_device_desc) {
383  LOG_WARNING("no openjtag device description specified, "
384  "using default 'Open JTAG Project'");
385  openjtag_device_desc = "Open JTAG Project";
386  }
387 
388  if (ftdi_init(&ftdic) < 0)
389  return ERROR_JTAG_INIT_FAILED;
390 
391  const uint16_t *vids = openjtag_vids;
392  const uint16_t *pids = openjtag_pids;
393 
394  if (adapter_usb_get_vids()[0] != 0) {
395  vids = adapter_usb_get_vids();
396  pids = adapter_usb_get_pids();
397  }
398 
399  for (unsigned int i = 0; vids[i] != 0; i++) {
400  /* context, vendor id, product id, description, serial id */
401  if (ftdi_usb_open_desc(&ftdic, vids[i], pids[i], openjtag_device_desc, NULL) < 0) {
402  LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
403  return ERROR_JTAG_INIT_FAILED;
404  }
405  }
406 
407  if (ftdi_usb_reset(&ftdic) < 0) {
408  LOG_ERROR("unable to reset ftdi device");
409  return ERROR_JTAG_INIT_FAILED;
410  }
411 
412  if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
413  LOG_ERROR("unable to set latency timer");
414  return ERROR_JTAG_INIT_FAILED;
415  }
416 
417  if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
418  LOG_ERROR("unable to get latency timer");
419  return ERROR_JTAG_INIT_FAILED;
420  }
421  LOG_DEBUG("current latency timer: %u", latency_timer);
422 
423  ftdi_disable_bitbang(&ftdic);
424  /* was (3000000 / 4) with a comment about a bug in libftdi when using high baudrate */
425  if (ftdi_set_baudrate(&ftdic, 3000000) < 0) {
426  LOG_ERROR("Can't set baud rate to max: %s",
427  ftdi_get_error_string(&ftdic));
429  }
430 
431  if (ftdi_tcioflush(&ftdic) < 0) {
432  LOG_ERROR("ftdi flush: %s", ftdic.error_str);
433  return ERROR_JTAG_INIT_FAILED;
434  }
435 
436  return ERROR_OK;
437 }
438 
439 static int openjtag_init_cy7c65215(void)
440 {
441  int ret;
442 
443  usbh = NULL;
445  if (ret != ERROR_OK) {
446  LOG_ERROR("unable to open cy7c65215 device");
447  goto err;
448  }
449 
452  CY7C65215_JTAG_SUBCLASS, -1, LIBUSB_TRANSFER_TYPE_BULK);
453  if (ret != ERROR_OK) {
454  LOG_ERROR("unable to claim JTAG interface");
455  goto err;
456  }
457 
461  0, 0, NULL, 0, CY7C65215_USB_TIMEOUT, NULL);
462  if (ret != ERROR_OK) {
463  LOG_ERROR("could not enable JTAG module");
464  goto err;
465  }
466 
467  return ERROR_OK;
468 
469 err:
470  if (usbh)
472  return ret;
473 }
474 
475 static int openjtag_init(void)
476 {
477  int ret;
478 
479  usb_tx_buf_offs = 0;
480  usb_rx_buf_len = 0;
482 
483  switch (openjtag_variant) {
485  ret = openjtag_init_cy7c65215();
486  break;
487  default:
488  ret = openjtag_init_standard();
489  }
490  if (ret != ERROR_OK)
491  return ret;
492 
493  openjtag_speed(375); /* Start at slowest adapter speed */
494  openjtag_sendcommand(0x75); /* MSB */
495 
496  return ERROR_OK;
497 }
498 
499 static int openjtag_quit_standard(void)
500 {
501  ftdi_usb_close(&ftdic);
502  ftdi_deinit(&ftdic);
503 
504  return ERROR_OK;
505 }
506 
507 static int openjtag_quit_cy7c65215(void)
508 {
509  int ret;
510 
514  0, 0, NULL, 0, CY7C65215_USB_TIMEOUT, NULL);
515  if (ret != ERROR_OK)
516  LOG_WARNING("could not disable JTAG module");
517 
519 
520  return ERROR_OK;
521 }
522 
523 static int openjtag_quit(void)
524 {
525  switch (openjtag_variant) {
527  return openjtag_quit_cy7c65215();
528  default:
529  return openjtag_quit_standard();
530  }
531 }
532 
533 static void openjtag_write_tap_buffer(void)
534 {
535  uint32_t written;
536  uint32_t rx_expected = 0;
537 
538  /* calculate expected number of return bytes */
539  for (int tx_offs = 0; tx_offs < usb_tx_buf_offs; tx_offs++) {
540  if ((usb_tx_buf[tx_offs] & 0x0F) == 6) {
541  rx_expected++;
542  tx_offs++;
543  } else if ((usb_tx_buf[tx_offs] & 0x0F) == 2) {
544  rx_expected++;
545  }
546  }
547 
550 
551  usb_tx_buf_offs = 0;
552 }
553 
555 {
557 
558  int res_count = 0;
559 
561 
562  int count;
563  int rx_offs = 0;
564  int len;
565 
566  /* for every pending result */
567  while (res_count < openjtag_scan_result_count) {
568 
569  /* get sent bits */
570  len = openjtag_scan_result_buffer[res_count].bits;
571 
572  count = 0;
573 
574  uint8_t *buffer = openjtag_scan_result_buffer[res_count].buffer;
575 
576  while (len > 0) {
577  if (len <= 8 && openjtag_variant != OPENJTAG_VARIANT_CY7C65215) {
578  LOG_DEBUG_IO("bits < 8 buf = 0x%X, will be 0x%X",
579  usb_rx_buf[rx_offs], usb_rx_buf[rx_offs] >> (8 - len));
580  buffer[count] = usb_rx_buf[rx_offs] >> (8 - len);
581  len = 0;
582  } else {
583  buffer[count] = usb_rx_buf[rx_offs];
584  len -= 8;
585  }
586 
587  rx_offs++;
588  count++;
589  }
590 
593 
595 
596  free(openjtag_scan_result_buffer[res_count].buffer);
597 
598  res_count++;
599  }
600  }
601 
603 
604  return ERROR_OK;
605 }
606 
607 static void openjtag_add_byte(char buf)
608 {
609 
611  LOG_DEBUG_IO("Forcing execute_tap_queue");
612  LOG_DEBUG_IO("TX Buff offs=%d", usb_tx_buf_offs);
614  }
615 
617  usb_tx_buf_offs++;
618 }
619 
620 static void openjtag_add_scan(uint8_t *buffer, int length, struct scan_command *scan_cmd)
621 {
622 
623  /* Ensure space to send long chains */
624  /* We add two byte for each eight (or less) bits, one for command, one for data */
626  LOG_DEBUG_IO("Forcing execute_tap_queue from scan");
627  LOG_DEBUG_IO("TX Buff offs=%d len=%d", usb_tx_buf_offs, DIV_ROUND_UP(length, 8) * 2);
629  }
630 
634 
635  uint8_t command;
636  uint8_t bits;
637  int count = 0;
638  while (length) {
639 
640  /* write command */
641  command = 6;
642 
643  /* last bits? */
644  if (length <= 8) {
645  /* tms high */
646  command |= (1 << 4);
647 
648  /* bits to transfer */
649  bits = (length - 1);
650  command |= bits << 5;
651  length = 0;
652  } else {
653  /* whole byte */
654 
655  /* bits to transfer */
656  command |= (7 << 5);
657  length -= 8;
658  }
659 
662  count++;
663  }
664 
666 }
667 
669 {
670 
671  LOG_DEBUG_IO("reset trst: %i srst %i",
672  cmd->cmd.reset->trst, cmd->cmd.reset->srst);
673 
674  uint8_t buf = 0x00;
675 
676  /* Pull SRST low for 5 TCLK cycles */
677  if (cmd->cmd.reset->srst) {
678  buf |= 0x04;
679  buf |= 0x05 << 4;
680  openjtag_add_byte(buf);
681  }
682 }
683 
685 {
686  jtag_sleep(cmd->cmd.sleep->us);
687 }
688 
689 static void openjtag_set_state(uint8_t openocd_state)
690 {
691  uint8_t state = openjtag_get_tap_state(openocd_state);
692 
693  uint8_t buf = 0;
694 
695  if (state != OPENJTAG_TAP_RESET) {
696  buf = 0x01;
697  buf |= state << 4;
698  } else {
699  /* Force software TLR */
700  buf = 0x03;
701  }
702 
703  openjtag_add_byte(buf);
704 }
705 
707 {
708  LOG_DEBUG_IO("state move to %i", cmd->cmd.statemove->end_state);
709 
710  tap_set_end_state(cmd->cmd.statemove->end_state);
711 
712  openjtag_set_state(cmd->cmd.statemove->end_state);
713 
715 }
716 
717 
719 {
720 
721  int scan_size, old_state;
722  uint8_t *buffer;
723 
724  LOG_DEBUG_IO("scan ends in %s", tap_state_name(cmd->cmd.scan->end_state));
725 
726  /* get scan info */
727  tap_set_end_state(cmd->cmd.scan->end_state);
728  scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
729 
730  openjtag_debug_buffer(buffer, (scan_size + 7) / 8, DEBUG_TYPE_BUFFER);
731 
732  /* set state */
733  old_state = tap_get_end_state();
734  openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
735  tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
736  tap_set_end_state(old_state);
737 
738  openjtag_add_scan(buffer, scan_size, cmd->cmd.scan);
739 
740  openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
741  tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
742 
743  if (tap_get_state() != tap_get_end_state()) {
746  }
747 }
748 
750 {
751 
752  enum tap_state end_state = cmd->cmd.runtest->end_state;
753  tap_set_end_state(end_state);
754 
755  /* only do a state_move when we're not already in IDLE */
756  if (tap_get_state() != TAP_IDLE) {
759  }
760 
762  cmd->cmd.runtest->num_cycles) {
763  uint8_t command;
764  unsigned int num_cycles = cmd->cmd.runtest->num_cycles;
765 
766  do {
767  const unsigned int num_cycles_cmd = MIN(num_cycles, 16);
768 
769  command = 7;
770  command |= ((num_cycles_cmd - 1) & 0x0F) << 4;
771 
773  num_cycles -= num_cycles_cmd;
774  } while (num_cycles > 0);
775  }
776 
777  tap_set_end_state(end_state);
778  if (tap_get_end_state() != tap_get_state()) {
779  openjtag_set_state(end_state);
780  tap_set_state(end_state);
781  }
782 }
783 
785 {
786  LOG_DEBUG_IO("openjtag_execute_command %i", cmd->type);
787  switch (cmd->type) {
788  case JTAG_RESET:
790  break;
791  case JTAG_SLEEP:
793  break;
794  case JTAG_TLR_RESET:
796  break;
797  case JTAG_SCAN:
799  break;
800  case JTAG_RUNTEST:
802  break;
803  case JTAG_PATHMOVE:
804  /* jlink_execute_pathmove(cmd); break; */
805  default:
806  LOG_ERROR("BUG: unknown Open JTAG command type encountered");
807  exit(-1);
808  }
809 }
810 
811 static int openjtag_execute_queue(struct jtag_command *cmd_queue)
812 {
813  struct jtag_command *cmd = cmd_queue;
814 
815  while (cmd) {
817  cmd = cmd->next;
818  }
819 
821 }
822 
823 static int openjtag_speed_div(int speed, int *khz)
824 {
825  *khz = speed;
826 
827  return ERROR_OK;
828 }
829 
830 static int openjtag_khz(int khz, int *jtag_speed)
831 {
832 
833  if (khz >= 48000)
834  *jtag_speed = 48000;
835  else if (khz >= 24000)
836  *jtag_speed = 24000;
837  else if (khz >= 12000)
838  *jtag_speed = 12000;
839  else if (khz >= 6000)
840  *jtag_speed = 6000;
841  else if (khz >= 3000)
842  *jtag_speed = 3000;
843  else if (khz >= 1500)
844  *jtag_speed = 1500;
845  else if (khz >= 750)
846  *jtag_speed = 750;
847  else
848  *jtag_speed = 375;
849 
850  return ERROR_OK;
851 }
852 
853 COMMAND_HANDLER(openjtag_handle_device_desc_command)
854 {
855  if (CMD_ARGC == 1)
856  openjtag_device_desc = strdup(CMD_ARGV[0]);
857  else
858  LOG_ERROR("require exactly one argument to "
859  "openjtag_device_desc <description>");
860  return ERROR_OK;
861 }
862 
863 COMMAND_HANDLER(openjtag_handle_variant_command)
864 {
865  if (CMD_ARGC == 1) {
866  const char * const *name = openjtag_variant_names;
867  int variant = 0;
868  for (; *name; name++, variant++) {
869  if (strcasecmp(CMD_ARGV[0], *name) == 0) {
870  openjtag_variant = variant;
871  return ERROR_OK;
872  }
873  }
874  LOG_ERROR("unknown openjtag variant '%s'", CMD_ARGV[0]);
875  } else {
876  LOG_ERROR("require exactly one argument to "
877  "openjtag_variant <variant>");
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  },
898 };
899 
900 static const struct command_registration openjtag_command_handlers[] = {
901  {
902  .name = "openjtag",
903  .mode = COMMAND_ANY,
904  .help = "perform openjtag management",
906  .usage = "",
907  },
909 };
910 
911 static struct jtag_interface openjtag_interface = {
913 };
914 
916  .name = "openjtag",
917  .transport_ids = TRANSPORT_JTAG,
918  .transport_preferred_id = TRANSPORT_JTAG,
919  .commands = openjtag_command_handlers,
920 
921  .init = openjtag_init,
922  .quit = openjtag_quit,
923  .speed = openjtag_speed,
924  .khz = openjtag_khz,
925  .speed_div = openjtag_speed_div,
926 
927  .jtag_ops = &openjtag_interface,
928 };
const uint16_t * adapter_usb_get_pids(void)
Definition: adapter.c:338
const uint16_t * adapter_usb_get_vids(void)
Definition: adapter.c:333
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 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_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:1073
#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:116
#define LOG_DEBUG_USB(expr,...)
Definition: log.h:114
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_LEVEL_IS(FOO)
Definition: log.h:112
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
@ LOG_LVL_DEBUG_USB
Definition: log.h:59
static void openjtag_debug_buffer(uint8_t *buffer, int length, uint8_t type)
Definition: openjtag.c:129
static int openjtag_quit_standard(void)
Definition: openjtag.c:499
static int openjtag_khz(int khz, int *jtag_speed)
Definition: openjtag.c:830
@ OPENJTAG_VARIANT_STANDARD
Definition: openjtag.c:41
@ OPENJTAG_VARIANT_CY7C65215
Definition: openjtag.c:42
static uint8_t usb_tx_buf[OPENJTAG_BUFFER_SIZE]
Definition: openjtag.c:96
static const uint16_t cy7c65215_vids[]
Definition: openjtag.c:115
static int8_t openjtag_get_tap_state(int8_t state)
Definition: openjtag.c:181
static int openjtag_quit(void)
Definition: openjtag.c:523
#define CY7C65215_JTAG_ENABLE
Definition: openjtag.c:108
static struct openjtag_scan_result openjtag_scan_result_buffer[OPENJTAG_MAX_PENDING_RESULTS]
Definition: openjtag.c:101
static void openjtag_add_byte(char buf)
Definition: openjtag.c:607
static int openjtag_buf_read_cy7c65215(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
Definition: openjtag.c:292
#define CY7C65215_JTAG_WRITE
Definition: openjtag.c:111
static struct jtag_interface openjtag_interface
Definition: openjtag.c:911
static int openjtag_buf_read_standard(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
Definition: openjtag.c:266
#define CY7C65215_JTAG_DISABLE
Definition: openjtag.c:109
static char * openjtag_device_desc
Definition: openjtag.c:81
static int openjtag_buf_read(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
Definition: openjtag.c:323
static int openjtag_buf_write_standard(uint8_t *buf, int size, uint32_t *bytes_written)
Definition: openjtag.c:206
static void openjtag_write_tap_buffer(void)
Definition: openjtag.c:533
#define CY7C65215_USB_TIMEOUT
Definition: openjtag.c:113
static void openjtag_execute_command(struct jtag_command *cmd)
Definition: openjtag.c:784
static const struct command_registration openjtag_command_handlers[]
Definition: openjtag.c:900
#define LINE_LEN
Definition: openjtag.c:128
static const uint16_t cy7c65215_pids[]
Definition: openjtag.c:116
#define OPENJTAG_BUFFER_SIZE
Definition: openjtag.c:85
#define CY7C65215_JTAG_SUBCLASS
Definition: openjtag.c:119
static void openjtag_execute_runtest(struct jtag_command *cmd)
Definition: openjtag.c:749
static int openjtag_execute_tap_queue(void)
Definition: openjtag.c:554
static uint32_t usb_rx_buf_len
Definition: openjtag.c:97
static void openjtag_execute_scan(struct jtag_command *cmd)
Definition: openjtag.c:718
static int openjtag_speed_div(int speed, int *khz)
Definition: openjtag.c:823
static int openjtag_scan_result_count
Definition: openjtag.c:102
static int usb_tx_buf_offs
Definition: openjtag.c:95
#define CY7C65215_JTAG_READ
Definition: openjtag.c:110
static const char *const openjtag_variant_names[]
Definition: openjtag.c:45
static int openjtag_buf_write_cy7c65215(uint8_t *buf, int size, uint32_t *bytes_written)
Definition: openjtag.c:225
#define DEBUG_TYPE_READ
Definition: openjtag.c:123
static int openjtag_speed(int speed)
Definition: openjtag.c:339
static const struct command_registration openjtag_subcommand_handlers[]
Definition: openjtag.c:882
#define DEBUG_TYPE_WRITE
Definition: openjtag.c:124
static void openjtag_execute_sleep(struct jtag_command *cmd)
Definition: openjtag.c:684
static uint8_t usb_rx_buf[OPENJTAG_BUFFER_SIZE]
Definition: openjtag.c:98
static void openjtag_execute_statemove(struct jtag_command *cmd)
Definition: openjtag.c:706
struct adapter_driver openjtag_adapter_driver
Definition: openjtag.c:915
static struct libusb_device_handle * usbh
Definition: openjtag.c:104
static unsigned int ep_out
Definition: openjtag.c:121
#define CY7C65215_JTAG_CLASS
Definition: openjtag.c:118
static int openjtag_buf_write(uint8_t *buf, int size, uint32_t *bytes_written)
Definition: openjtag.c:255
static unsigned int ep_in
Definition: openjtag.c:121
static uint16_t openjtag_vids[]
Definition: openjtag.c:78
static int openjtag_execute_queue(struct jtag_command *cmd_queue)
Definition: openjtag.c:811
openjtag_tap_state
Definition: openjtag.c:54
@ OPENJTAG_TAP_PAUSE_IR
Definition: openjtag.c:69
@ OPENJTAG_TAP_SELECT_DR
Definition: openjtag.c:58
@ OPENJTAG_TAP_EXIT2_DR
Definition: openjtag.c:63
@ OPENJTAG_TAP_SELECT_IR
Definition: openjtag.c:65
@ OPENJTAG_TAP_CAPURE_IR
Definition: openjtag.c:66
@ OPENJTAG_TAP_RESET
Definition: openjtag.c:56
@ OPENJTAG_TAP_SHIFT_IR
Definition: openjtag.c:67
@ OPENJTAG_TAP_UPDATE_IR
Definition: openjtag.c:71
@ OPENJTAG_TAP_IDLE
Definition: openjtag.c:57
@ OPENJTAG_TAP_EXIT2_IR
Definition: openjtag.c:70
@ OPENJTAG_TAP_EXIT1_IR
Definition: openjtag.c:68
@ OPENJTAG_TAP_INVALID
Definition: openjtag.c:55
@ OPENJTAG_TAP_UPDATE_DR
Definition: openjtag.c:64
@ OPENJTAG_TAP_SHIFT_DR
Definition: openjtag.c:60
@ OPENJTAG_TAP_EXIT1_DR
Definition: openjtag.c:61
@ OPENJTAG_TAP_PAUSE_DR
Definition: openjtag.c:62
@ OPENJTAG_TAP_CAPTURE_DR
Definition: openjtag.c:59
static int openjtag_quit_cy7c65215(void)
Definition: openjtag.c:507
static int openjtag_sendcommand(uint8_t cmd)
Definition: openjtag.c:333
static struct ftdi_context ftdic
Definition: openjtag.c:83
#define DEBUG_TYPE_OCD_READ
Definition: openjtag.c:125
static int openjtag_init_cy7c65215(void)
Definition: openjtag.c:439
static int openjtag_init_standard(void)
Definition: openjtag.c:377
static enum @31 openjtag_variant
COMMAND_HANDLER(openjtag_handle_device_desc_command)
Definition: openjtag.c:853
#define CY7C65215_JTAG_REQUEST
Definition: openjtag.c:107
static void openjtag_add_scan(uint8_t *buffer, int length, struct scan_command *scan_cmd)
Definition: openjtag.c:620
static uint16_t openjtag_pids[]
Definition: openjtag.c:79
#define DEBUG_TYPE_BUFFER
Definition: openjtag.c:126
static void openjtag_set_state(uint8_t openocd_state)
Definition: openjtag.c:689
#define OPENJTAG_MAX_PENDING_RESULTS
Definition: openjtag.c:86
static int openjtag_init(void)
Definition: openjtag.c:475
static void openjtag_execute_reset(struct jtag_command *cmd)
Definition: openjtag.c:668
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:90
uint8_t * buffer
Definition: openjtag.c:91
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