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  */
53 typedef enum openjtag_tap_state {
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 #ifdef _DEBUG_USB_COMMS_
123 
124 #define DEBUG_TYPE_READ 0
125 #define DEBUG_TYPE_WRITE 1
126 #define DEBUG_TYPE_OCD_READ 2
127 #define DEBUG_TYPE_BUFFER 3
128 
129 #define LINE_LEN 16
130 static void openjtag_debug_buffer(uint8_t *buffer, int length, uint8_t type)
131 {
132  char line[128];
133  char s[4];
134  int i;
135  int j;
136 
137  switch (type) {
138  case DEBUG_TYPE_READ:
139  sprintf(line, "USB READ %d bytes", length);
140  break;
141  case DEBUG_TYPE_WRITE:
142  sprintf(line, "USB WRITE %d bytes", length);
143  break;
144  case DEBUG_TYPE_OCD_READ:
145  sprintf(line, "TO OpenOCD %d bytes", length);
146  break;
147  case DEBUG_TYPE_BUFFER:
148  sprintf(line, "Buffer %d bytes", length);
149  break;
150  }
151 
152  LOG_DEBUG("%s", line);
153 
154  for (i = 0; i < length; i += LINE_LEN) {
155  switch (type) {
156  case DEBUG_TYPE_READ:
157  sprintf(line, "USB READ: %04x", i);
158  break;
159  case DEBUG_TYPE_WRITE:
160  sprintf(line, "USB WRITE: %04x", i);
161  break;
162  case DEBUG_TYPE_OCD_READ:
163  sprintf(line, "TO OpenOCD: %04x", i);
164  break;
165  case DEBUG_TYPE_BUFFER:
166  sprintf(line, "BUFFER: %04x", i);
167  break;
168  }
169 
170  for (j = i; j < i + LINE_LEN && j < length; j++) {
171  sprintf(s, " %02x", buffer[j]);
172  strcat(line, s);
173  }
174  LOG_DEBUG("%s", line);
175  }
176 
177 }
178 
179 #endif
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 #ifdef _DEBUG_USB_COMMS_
211  openjtag_debug_buffer(buf, size, DEBUG_TYPE_WRITE);
212 #endif
213 
214  retval = ftdi_write_data(&ftdic, buf, size);
215  if (retval < 0) {
216  *bytes_written = 0;
217  LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
219  }
220 
221  *bytes_written = retval;
222 
223  return ERROR_OK;
224 }
225 
227  uint8_t *buf, int size, uint32_t *bytes_written)
228 {
229  int ret;
230 
231 #ifdef _DEBUG_USB_COMMS_
232  openjtag_debug_buffer(buf, size, DEBUG_TYPE_WRITE);
233 #endif
234 
235  if (size == 0) {
236  *bytes_written = 0;
237  return ERROR_OK;
238  }
239 
243  if (ret != ERROR_OK) {
244  LOG_ERROR("vendor command failed");
245  return ret;
246  }
247 
248  if (jtag_libusb_bulk_write(usbh, ep_out, (char *)buf, size,
249  CY7C65215_USB_TIMEOUT, &ret)) {
250  LOG_ERROR("bulk write failed, error");
252  }
253  *bytes_written = ret;
254 
255  return ERROR_OK;
256 }
257 
259  uint8_t *buf, int size, uint32_t *bytes_written)
260 {
261  switch (openjtag_variant) {
263  return openjtag_buf_write_cy7c65215(buf, size, bytes_written);
264  default:
265  return openjtag_buf_write_standard(buf, size, bytes_written);
266  }
267 }
268 
270  uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
271 {
272 
273  int retval;
274  int timeout = 5;
275 
276  *bytes_read = 0;
277 
278  while ((*bytes_read < qty) && timeout--) {
279  retval = ftdi_read_data(&ftdic, buf + *bytes_read,
280  qty - *bytes_read);
281  if (retval < 0) {
282  *bytes_read = 0;
283  LOG_DEBUG_IO("ftdi_read_data: %s",
284  ftdi_get_error_string(&ftdic));
286  }
287  *bytes_read += retval;
288  }
289 
290 #ifdef _DEBUG_USB_COMMS_
291  openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
292 #endif
293 
294  return ERROR_OK;
295 }
296 
298  uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
299 {
300  int ret;
301 
302  if (qty == 0) {
303  *bytes_read = 0;
304  goto out;
305  }
306 
308  CY7C65215_JTAG_READ, qty, 0,
310  if (ret != ERROR_OK) {
311  LOG_ERROR("vendor command failed");
312  return ret;
313  }
314 
315  if (jtag_libusb_bulk_read(usbh, ep_in, (char *)buf, qty,
316  CY7C65215_USB_TIMEOUT, &ret)) {
317  LOG_ERROR("bulk read failed, error");
319  }
320  *bytes_read = ret;
321 
322 out:
323 #ifdef _DEBUG_USB_COMMS_
324  openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
325 #endif
326 
327  return ERROR_OK;
328 }
329 
330 static int openjtag_buf_read(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
331 {
332  switch (openjtag_variant) {
334  return openjtag_buf_read_cy7c65215(buf, qty, bytes_read);
335  default:
336  return openjtag_buf_read_standard(buf, qty, bytes_read);
337  }
338 }
339 
340 static int openjtag_sendcommand(uint8_t cmd)
341 {
342  uint32_t written;
343  return openjtag_buf_write(&cmd, 1, &written);
344 }
345 
346 static int openjtag_speed(int speed)
347 {
348  int clockcmd;
349  switch (speed) {
350  case 48000:
351  clockcmd = 0x00;
352  break;
353  case 24000:
354  clockcmd = 0x20;
355  break;
356  case 12000:
357  clockcmd = 0x40;
358  break;
359  case 6000:
360  clockcmd = 0x60;
361  break;
362  case 3000:
363  clockcmd = 0x80;
364  break;
365  case 1500:
366  clockcmd = 0xA0;
367  break;
368  case 750:
369  clockcmd = 0xC0;
370  break;
371  case 375:
372  clockcmd = 0xE0;
373  break;
374  default:
375  clockcmd = 0xE0;
376  LOG_WARNING("adapter speed not recognized, reverting to 375 kHz");
377  break;
378  }
379  openjtag_sendcommand(clockcmd);
380 
381  return ERROR_OK;
382 }
383 
384 static int openjtag_init_standard(void)
385 {
386  uint8_t latency_timer;
387 
388  /* Open by device description */
389  if (!openjtag_device_desc) {
390  LOG_WARNING("no openjtag device description specified, "
391  "using default 'Open JTAG Project'");
392  openjtag_device_desc = "Open JTAG Project";
393  }
394 
395  if (ftdi_init(&ftdic) < 0)
396  return ERROR_JTAG_INIT_FAILED;
397 
398  /* context, vendor id, product id, description, serial id */
399  if (ftdi_usb_open_desc(&ftdic, openjtag_vid, openjtag_pid, openjtag_device_desc, NULL) < 0) {
400  LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
401  return ERROR_JTAG_INIT_FAILED;
402  }
403 
404  if (ftdi_usb_reset(&ftdic) < 0) {
405  LOG_ERROR("unable to reset ftdi device");
406  return ERROR_JTAG_INIT_FAILED;
407  }
408 
409  if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
410  LOG_ERROR("unable to set latency timer");
411  return ERROR_JTAG_INIT_FAILED;
412  }
413 
414  if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
415  LOG_ERROR("unable to get latency timer");
416  return ERROR_JTAG_INIT_FAILED;
417  }
418  LOG_DEBUG("current latency timer: %u", latency_timer);
419 
420  ftdi_disable_bitbang(&ftdic);
421  /* was (3000000 / 4) with a comment about a bug in libftdi when using high baudrate */
422  if (ftdi_set_baudrate(&ftdic, 3000000) < 0) {
423  LOG_ERROR("Can't set baud rate to max: %s",
424  ftdi_get_error_string(&ftdic));
426  }
427 
428  if (ftdi_tcioflush(&ftdic) < 0) {
429  LOG_ERROR("ftdi flush: %s", ftdic.error_str);
430  return ERROR_JTAG_INIT_FAILED;
431  }
432 
433  return ERROR_OK;
434 }
435 
436 static int openjtag_init_cy7c65215(void)
437 {
438  int ret;
439 
440  usbh = NULL;
442  if (ret != ERROR_OK) {
443  LOG_ERROR("unable to open cy7c65215 device");
444  goto err;
445  }
446 
449  CY7C65215_JTAG_SUBCLASS, -1, LIBUSB_TRANSFER_TYPE_BULK);
450  if (ret != ERROR_OK) {
451  LOG_ERROR("unable to claim JTAG interface");
452  goto err;
453  }
454 
458  0, 0, NULL, 0, CY7C65215_USB_TIMEOUT, NULL);
459  if (ret != ERROR_OK) {
460  LOG_ERROR("could not enable JTAG module");
461  goto err;
462  }
463 
464  return ERROR_OK;
465 
466 err:
467  if (usbh)
469  return ret;
470 }
471 
472 static int openjtag_init(void)
473 {
474  int ret;
475 
476  usb_tx_buf_offs = 0;
477  usb_rx_buf_len = 0;
479 
480  switch (openjtag_variant) {
482  ret = openjtag_init_cy7c65215();
483  break;
484  default:
485  ret = openjtag_init_standard();
486  }
487  if (ret != ERROR_OK)
488  return ret;
489 
490  openjtag_speed(375); /* Start at slowest adapter speed */
491  openjtag_sendcommand(0x75); /* MSB */
492 
493  return ERROR_OK;
494 }
495 
496 static int openjtag_quit_standard(void)
497 {
498  ftdi_usb_close(&ftdic);
499  ftdi_deinit(&ftdic);
500 
501  return ERROR_OK;
502 }
503 
504 static int openjtag_quit_cy7c65215(void)
505 {
506  int ret;
507 
511  0, 0, NULL, 0, CY7C65215_USB_TIMEOUT, NULL);
512  if (ret != ERROR_OK)
513  LOG_WARNING("could not disable JTAG module");
514 
516 
517  return ERROR_OK;
518 }
519 
520 static int openjtag_quit(void)
521 {
522  switch (openjtag_variant) {
524  return openjtag_quit_cy7c65215();
525  default:
526  return openjtag_quit_standard();
527  }
528 }
529 
530 static void openjtag_write_tap_buffer(void)
531 {
532  uint32_t written;
533  uint32_t rx_expected = 0;
534 
535  /* calculate expected number of return bytes */
536  for (int tx_offs = 0; tx_offs < usb_tx_buf_offs; tx_offs++) {
537  if ((usb_tx_buf[tx_offs] & 0x0F) == 6) {
538  rx_expected++;
539  tx_offs++;
540  } else if ((usb_tx_buf[tx_offs] & 0x0F) == 2) {
541  rx_expected++;
542  }
543  }
544 
547 
548  usb_tx_buf_offs = 0;
549 }
550 
552 {
554 
555  int res_count = 0;
556 
558 
559  int count;
560  int rx_offs = 0;
561  int len;
562 
563  /* for every pending result */
564  while (res_count < openjtag_scan_result_count) {
565 
566  /* get sent bits */
567  len = openjtag_scan_result_buffer[res_count].bits;
568 
569  count = 0;
570 
571  uint8_t *buffer = openjtag_scan_result_buffer[res_count].buffer;
572 
573  while (len > 0) {
574  if (len <= 8 && openjtag_variant != OPENJTAG_VARIANT_CY7C65215) {
575  LOG_DEBUG_IO("bits < 8 buf = 0x%X, will be 0x%X",
576  usb_rx_buf[rx_offs], usb_rx_buf[rx_offs] >> (8 - len));
577  buffer[count] = usb_rx_buf[rx_offs] >> (8 - len);
578  len = 0;
579  } else {
580  buffer[count] = usb_rx_buf[rx_offs];
581  len -= 8;
582  }
583 
584  rx_offs++;
585  count++;
586  }
587 
588 #ifdef _DEBUG_USB_COMMS_
589  openjtag_debug_buffer(buffer,
590  DIV_ROUND_UP(openjtag_scan_result_buffer[res_count].bits, 8), DEBUG_TYPE_OCD_READ);
591 #endif
593 
594  free(openjtag_scan_result_buffer[res_count].buffer);
595 
596  res_count++;
597  }
598  }
599 
601 
602  return ERROR_OK;
603 }
604 
605 static void openjtag_add_byte(char buf)
606 {
607 
609  LOG_DEBUG_IO("Forcing execute_tap_queue");
610  LOG_DEBUG_IO("TX Buff offs=%d", usb_tx_buf_offs);
612  }
613 
615  usb_tx_buf_offs++;
616 }
617 
618 static void openjtag_add_scan(uint8_t *buffer, int length, struct scan_command *scan_cmd)
619 {
620 
621  /* Ensure space to send long chains */
622  /* We add two byte for each eight (or less) bits, one for command, one for data */
624  LOG_DEBUG_IO("Forcing execute_tap_queue from scan");
625  LOG_DEBUG_IO("TX Buff offs=%d len=%d", usb_tx_buf_offs, DIV_ROUND_UP(length, 8) * 2);
627  }
628 
632 
633  uint8_t command;
634  uint8_t bits;
635  int count = 0;
636  while (length) {
637 
638  /* write command */
639  command = 6;
640 
641  /* last bits? */
642  if (length <= 8) {
643  /* tms high */
644  command |= (1 << 4);
645 
646  /* bits to transfer */
647  bits = (length - 1);
648  command |= bits << 5;
649  length = 0;
650  } else {
651  /* whole byte */
652 
653  /* bits to transfer */
654  command |= (7 << 5);
655  length -= 8;
656  }
657 
660  count++;
661  }
662 
664 }
665 
667 {
668 
669  LOG_DEBUG_IO("reset trst: %i srst %i",
670  cmd->cmd.reset->trst, cmd->cmd.reset->srst);
671 
672  uint8_t buf = 0x00;
673 
674  /* Pull SRST low for 5 TCLK cycles */
675  if (cmd->cmd.reset->srst) {
676  buf |= 0x04;
677  buf |= 0x05 << 4;
678  openjtag_add_byte(buf);
679  }
680 }
681 
683 {
684  jtag_sleep(cmd->cmd.sleep->us);
685 }
686 
687 static void openjtag_set_state(uint8_t openocd_state)
688 {
689  uint8_t state = openjtag_get_tap_state(openocd_state);
690 
691  uint8_t buf = 0;
692 
693  if (state != OPENJTAG_TAP_RESET) {
694  buf = 0x01;
695  buf |= state << 4;
696  } else {
697  /* Force software TLR */
698  buf = 0x03;
699  }
700 
701  openjtag_add_byte(buf);
702 }
703 
705 {
706  LOG_DEBUG_IO("state move to %i", cmd->cmd.statemove->end_state);
707 
708  tap_set_end_state(cmd->cmd.statemove->end_state);
709 
710  openjtag_set_state(cmd->cmd.statemove->end_state);
711 
713 }
714 
715 
717 {
718 
719  int scan_size, old_state;
720  uint8_t *buffer;
721 
722  LOG_DEBUG_IO("scan ends in %s", tap_state_name(cmd->cmd.scan->end_state));
723 
724  /* get scan info */
725  tap_set_end_state(cmd->cmd.scan->end_state);
726  scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
727 
728 #ifdef _DEBUG_USB_COMMS_
729  openjtag_debug_buffer(buffer, (scan_size + 7) / 8, DEBUG_TYPE_BUFFER);
730 #endif
731  /* set state */
732  old_state = tap_get_end_state();
733  openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
734  tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
735  tap_set_end_state(old_state);
736 
737  openjtag_add_scan(buffer, scan_size, cmd->cmd.scan);
738 
739  openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
740  tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
741 
742  if (tap_get_state() != tap_get_end_state()) {
745  }
746 }
747 
749 {
750 
751  tap_state_t end_state = cmd->cmd.runtest->end_state;
752  tap_set_end_state(end_state);
753 
754  /* only do a state_move when we're not already in IDLE */
755  if (tap_get_state() != TAP_IDLE) {
758  }
759 
761  cmd->cmd.runtest->num_cycles) {
762  uint8_t command;
763  int cycles = cmd->cmd.runtest->num_cycles;
764 
765  do {
766  command = 7;
767  command |= (((cycles > 16 ? 16 : cycles) - 1) & 0x0F) << 4;
768 
770  cycles -= 16;
771  } while (cycles > 0);
772  }
773 
774  tap_set_end_state(end_state);
775  if (tap_get_end_state() != tap_get_state()) {
776  openjtag_set_state(end_state);
777  tap_set_state(end_state);
778  }
779 }
780 
782 {
783  LOG_DEBUG_IO("openjtag_execute_command %i", cmd->type);
784  switch (cmd->type) {
785  case JTAG_RESET:
787  break;
788  case JTAG_SLEEP:
790  break;
791  case JTAG_TLR_RESET:
793  break;
794  case JTAG_SCAN:
796  break;
797  case JTAG_RUNTEST:
799  break;
800  case JTAG_PATHMOVE:
801  /* jlink_execute_pathmove(cmd); break; */
802  default:
803  LOG_ERROR("BUG: unknown Open JTAG command type encountered");
804  exit(-1);
805  }
806 }
807 
808 static int openjtag_execute_queue(struct jtag_command *cmd_queue)
809 {
810  struct jtag_command *cmd = cmd_queue;
811 
812  while (cmd) {
814  cmd = cmd->next;
815  }
816 
818 }
819 
820 static int openjtag_speed_div(int speed, int *khz)
821 {
822  *khz = speed;
823 
824  return ERROR_OK;
825 }
826 
827 static int openjtag_khz(int khz, int *jtag_speed)
828 {
829 
830  if (khz >= 48000)
831  *jtag_speed = 48000;
832  else if (khz >= 24000)
833  *jtag_speed = 24000;
834  else if (khz >= 12000)
835  *jtag_speed = 12000;
836  else if (khz >= 6000)
837  *jtag_speed = 6000;
838  else if (khz >= 3000)
839  *jtag_speed = 3000;
840  else if (khz >= 1500)
841  *jtag_speed = 1500;
842  else if (khz >= 750)
843  *jtag_speed = 750;
844  else
845  *jtag_speed = 375;
846 
847  return ERROR_OK;
848 }
849 
850 COMMAND_HANDLER(openjtag_handle_device_desc_command)
851 {
852  if (CMD_ARGC == 1)
853  openjtag_device_desc = strdup(CMD_ARGV[0]);
854  else
855  LOG_ERROR("require exactly one argument to "
856  "openjtag_device_desc <description>");
857  return ERROR_OK;
858 }
859 
860 COMMAND_HANDLER(openjtag_handle_variant_command)
861 {
862  if (CMD_ARGC == 1) {
863  const char * const *name = openjtag_variant_names;
864  int variant = 0;
865  for (; *name; name++, variant++) {
866  if (strcasecmp(CMD_ARGV[0], *name) == 0) {
867  openjtag_variant = variant;
868  return ERROR_OK;
869  }
870  }
871  LOG_ERROR("unknown openjtag variant '%s'", CMD_ARGV[0]);
872  } else {
873  LOG_ERROR("require exactly one argument to "
874  "openjtag_variant <variant>");
875  }
876  return ERROR_OK;
877 }
878 
879 COMMAND_HANDLER(openjtag_handle_vid_pid_command)
880 {
881  if (CMD_ARGC != 2)
883 
886 
887  return ERROR_OK;
888 }
889 
890 static const struct command_registration openjtag_subcommand_handlers[] = {
891  {
892  .name = "device_desc",
893  .handler = openjtag_handle_device_desc_command,
894  .mode = COMMAND_CONFIG,
895  .help = "set the USB device description of the OpenJTAG",
896  .usage = "description-string",
897  },
898  {
899  .name = "variant",
900  .handler = openjtag_handle_variant_command,
901  .mode = COMMAND_CONFIG,
902  .help = "set the OpenJTAG variant",
903  .usage = "variant-string",
904  },
905  {
906  .name = "vid_pid",
907  .handler = openjtag_handle_vid_pid_command,
908  .mode = COMMAND_CONFIG,
909  .help = "USB VID and PID of the adapter",
910  .usage = "vid pid",
911  },
913 };
914 
915 static const struct command_registration openjtag_command_handlers[] = {
916  {
917  .name = "openjtag",
918  .mode = COMMAND_ANY,
919  .help = "perform openjtag management",
921  .usage = "",
922  },
924 };
925 
926 static struct jtag_interface openjtag_interface = {
928 };
929 
931  .name = "openjtag",
932  .transports = jtag_only,
933  .commands = openjtag_command_handlers,
934 
935  .init = openjtag_init,
936  .quit = openjtag_quit,
937  .speed = openjtag_speed,
938  .khz = openjtag_khz,
939  .speed_div = openjtag_speed_div,
940 
941  .jtag_ops = &openjtag_interface,
942 };
const char *const jtag_only[]
Definition: adapter.c:27
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:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#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:442
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
@ 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:194
int jtag_read_buffer(uint8_t *buffer, const struct scan_command *cmd)
Definition: commands.c:235
@ 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
uint8_t type
Definition: esp_usb_jtag.c:0
uint8_t length
Definition: esp_usb_jtag.c:1
const char * tap_state_name(tap_state_t 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(tap_state_t 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
tap_state_t tap_get_end_state(void)
For more information,.
Definition: interface.c:56
tap_state_t 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:49
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1062
#define ERROR_JTAG_DEVICE_ERROR
Definition: jtag.h:559
@ 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:553
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
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:101
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
static int openjtag_quit_standard(void)
Definition: openjtag.c:496
static int openjtag_khz(int khz, int *jtag_speed)
Definition: openjtag.c:827
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:181
static int openjtag_quit(void)
Definition: openjtag.c:520
#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:605
static int openjtag_buf_read_cy7c65215(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
Definition: openjtag.c:297
#define CY7C65215_JTAG_WRITE
Definition: openjtag.c:110
static struct jtag_interface openjtag_interface
Definition: openjtag.c:926
static int openjtag_buf_read_standard(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
Definition: openjtag.c:269
#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:330
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:206
static void openjtag_write_tap_buffer(void)
Definition: openjtag.c:530
#define CY7C65215_USB_TIMEOUT
Definition: openjtag.c:112
static void openjtag_execute_command(struct jtag_command *cmd)
Definition: openjtag.c:781
static const struct command_registration openjtag_command_handlers[]
Definition: openjtag.c:915
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
@ OPENJTAG_VARIANT_STANDARD
Definition: openjtag.c:40
@ OPENJTAG_VARIANT_CY7C65215
Definition: openjtag.c:41
static void openjtag_execute_runtest(struct jtag_command *cmd)
Definition: openjtag.c:748
static int openjtag_execute_tap_queue(void)
Definition: openjtag.c:551
static uint32_t usb_rx_buf_len
Definition: openjtag.c:96
static void openjtag_execute_scan(struct jtag_command *cmd)
Definition: openjtag.c:716
static int openjtag_speed_div(int speed, int *khz)
Definition: openjtag.c:820
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:226
static int openjtag_speed(int speed)
Definition: openjtag.c:346
static const struct command_registration openjtag_subcommand_handlers[]
Definition: openjtag.c:890
static void openjtag_execute_sleep(struct jtag_command *cmd)
Definition: openjtag.c:682
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:704
struct adapter_driver openjtag_adapter_driver
Definition: openjtag.c:930
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:258
static unsigned int ep_in
Definition: openjtag.c:120
static int openjtag_execute_queue(struct jtag_command *cmd_queue)
Definition: openjtag.c:808
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:504
static int openjtag_sendcommand(uint8_t cmd)
Definition: openjtag.c:340
static struct ftdi_context ftdic
Definition: openjtag.c:82
static int openjtag_init_cy7c65215(void)
Definition: openjtag.c:436
static int openjtag_init_standard(void)
Definition: openjtag.c:384
enum openjtag_tap_state openjtag_tap_state_t
COMMAND_HANDLER(openjtag_handle_device_desc_command)
Definition: openjtag.c:850
#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:618
static void openjtag_set_state(uint8_t openocd_state)
Definition: openjtag.c:687
#define OPENJTAG_MAX_PENDING_RESULTS
Definition: openjtag.c:85
static int openjtag_init(void)
Definition: openjtag.c:472
static enum @28 openjtag_variant
static void openjtag_execute_reset(struct jtag_command *cmd)
Definition: openjtag.c:666
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
Represents a driver for a debugging interface.
Definition: interface.h:207
const char *const name
The name of the interface driver.
Definition: interface.h:209
const char * name
Definition: command.h:235
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:241
Represents a driver for a debugging interface.
Definition: interface.h:182
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
Definition: interface.h:195
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_s structures that should be sca...
Definition: commands.h:35
Definition: psoc6.c:84
#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