OpenOCD
usb_blaster.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * Driver for USB-JTAG, Altera USB-Blaster and compatibles
5  *
6  * Inspired from original code from Kolja Waschk's USB-JTAG project
7  * (http://www.ixo.de/info/usb_jtag/), and from openocd project.
8  *
9  * Copyright (C) 2013 Franck Jullien franck.jullien@gmail.com
10  * Copyright (C) 2012 Robert Jarzmik robert.jarzmik@free.fr
11  * Copyright (C) 2011 Ali Lown ali@lown.me.uk
12  * Copyright (C) 2009 Catalin Patulea cat@vv.carleton.ca
13  * Copyright (C) 2006 Kolja Waschk usbjtag@ixo.de
14  *
15  */
16 
17 /*
18  * The following information is originally from Kolja Waschk's USB-JTAG,
19  * where it was obtained by reverse engineering an Altera USB-Blaster.
20  * See http://www.ixo.de/info/usb_jtag/ for USB-Blaster block diagram and
21  * usb_jtag-20080705-1200.zip#usb_jtag/host/openocd for protocol.
22  *
23  * The same information is also on the UrJTAG mediawiki, with some additional
24  * notes on bits marked as "unknown" by usb_jtag.
25  * (http://sourceforge.net/apps/mediawiki/urjtag/index.php?
26  * title=Cable_Altera_USB-Blaster)
27  *
28  * USB-JTAG, Altera USB-Blaster and compatibles are typically implemented as
29  * an FTDIChip FT245 followed by a CPLD which handles a two-mode protocol:
30  *
31  * _________
32  * | |
33  * | AT93C46 |
34  * |_________|
35  * __|__________ _________
36  * | | | |
37  * USB__| FTDI 245BM |__| EPM7064 |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
38  * |_____________| |_________|
39  * __|__________ _|___________
40  * | | | |
41  * | 6 MHz XTAL | | 24 MHz Osc. |
42  * |_____________| |_____________|
43  *
44  * USB-JTAG, Altera USB-Blaster II are typically implemented as a Cypress
45  * EZ-USB FX2LP followed by a CPLD.
46  * _____________ _________
47  * | | | |
48  * USB__| EZ-USB FX2 |__| EPM570 |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
49  * |_____________| |_________|
50  * __|__________
51  * | |
52  * | 24 MHz XTAL |
53  * |_____________|
54  */
55 
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59 
60 #if IS_CYGWIN == 1
61 #include "windows.h"
62 #undef LOG_ERROR
63 #endif
64 
65 /* project specific includes */
66 #include <jtag/adapter.h>
67 #include <jtag/interface.h>
68 #include <jtag/commands.h>
69 #include <helper/time_support.h>
70 #include <helper/replacements.h>
71 #include "ublast_access.h"
72 
73 /* system includes */
74 #include <string.h>
75 #include <stdlib.h>
76 #include <unistd.h>
77 #include <sys/time.h>
78 #include <time.h>
79 
80 /* Size of USB endpoint max packet size, ie. 64 bytes */
81 #define MAX_PACKET_SIZE 64
82 /*
83  * Size of data buffer that holds bytes in byte-shift mode.
84  * This buffer can hold multiple USB packets aligned to
85  * MAX_PACKET_SIZE bytes boundaries.
86  * BUF_LEN must be grater than or equal MAX_PACKET_SIZE.
87  */
88 #define BUF_LEN 4096
89 
90 /* USB-Blaster II specific command */
91 #define CMD_COPY_TDO_BUFFER 0x5F
92 
93 enum gpio_steer {
94  FIXED_0 = 0,
98 };
99 
100 struct ublast_info {
101  enum gpio_steer pin6;
102  enum gpio_steer pin8;
103  int tms;
104  int tdi;
107  uint8_t buf[BUF_LEN];
108  int bufidx;
109 
114  int flags;
116 };
117 
118 /*
119  * Global device control
120  */
121 static struct ublast_info info = {
122  .ublast_vid = 0x09fb, /* Altera */
123  .ublast_pid = 0x6001, /* USB-Blaster */
124  .lowlevel_name = NULL,
125  .srst_asserted = false,
126  .trst_asserted = false,
127  .pin6 = FIXED_1,
128  .pin8 = FIXED_1,
129 };
130 
131 /*
132  * Available lowlevel drivers (FTDI, libusb, ...)
133  */
134 struct drvs_map {
135  char *name;
136  struct ublast_lowlevel *(*drv_register)(void);
137 };
138 
139 static struct drvs_map lowlevel_drivers_map[] = {
140 #if BUILD_USB_BLASTER
141  { .name = "ftdi", .drv_register = ublast_register_ftdi },
142 #endif
143 #if BUILD_USB_BLASTER_2
144  { .name = "ublast2", .drv_register = ublast2_register_libusb },
145 #endif
146  { NULL, NULL },
147 };
148 
149 /*
150  * Access functions to lowlevel driver, agnostic of libftdi/libftdxx
151  */
152 static char *hexdump(uint8_t *buf, unsigned int size)
153 {
154  unsigned int i;
155  char *str = calloc(size * 2 + 1, 1);
156 
157  for (i = 0; i < size; i++)
158  sprintf(str + 2*i, "%02x", buf[i]);
159  return str;
160 }
161 
162 static int ublast_buf_read(uint8_t *buf, unsigned int size, uint32_t *bytes_read)
163 {
164  int ret = info.drv->read(info.drv, buf, size, bytes_read);
165  char *str = hexdump(buf, *bytes_read);
166 
167  LOG_DEBUG_IO("(size=%d, buf=[%s]) -> %" PRIu32, size, str,
168  *bytes_read);
169  free(str);
170  return ret;
171 }
172 
173 static int ublast_buf_write(uint8_t *buf, int size, uint32_t *bytes_written)
174 {
175  int ret = info.drv->write(info.drv, buf, size, bytes_written);
176  char *str = hexdump(buf, *bytes_written);
177 
178  LOG_DEBUG_IO("(size=%d, buf=[%s]) -> %" PRIu32, size, str,
179  *bytes_written);
180  free(str);
181  return ret;
182 }
183 
184 static int nb_buf_remaining(void)
185 {
186  return BUF_LEN - info.bufidx;
187 }
188 
189 static void ublast_flush_buffer(void)
190 {
191  uint32_t retlen;
192  int nb = info.bufidx, ret = ERROR_OK;
193 
194  while (ret == ERROR_OK && nb > 0) {
195  ret = ublast_buf_write(info.buf, nb, &retlen);
196  nb -= retlen;
197  }
198  info.bufidx = 0;
199 }
200 
201 /*
202  * Actually, the USB-Blaster offers a byte-shift mode to transmit up to 504 data
203  * bits (bidirectional) in a single USB packet. A header byte has to be sent as
204  * the first byte in a packet with the following meaning:
205  *
206  * Bit 7 (0x80): Must be set to indicate byte-shift mode.
207  * Bit 6 (0x40): If set, the USB-Blaster will also read data, not just write.
208  * Bit 5..0: Define the number N of following bytes
209  *
210  * All N following bytes will then be clocked out serially on TDI. If Bit 6 was
211  * set, it will afterwards return N bytes with TDO data read while clocking out
212  * the TDI data. LSB of the first byte after the header byte will appear first
213  * on TDI.
214  */
215 
216 /* Simple bit banging mode:
217  *
218  * Bit 7 (0x80): Must be zero (see byte-shift mode above)
219  * Bit 6 (0x40): If set, you will receive a byte indicating the state of TDO
220  * in return.
221  * Bit 5 (0x20): Output Enable/LED.
222  * Bit 4 (0x10): TDI Output.
223  * Bit 3 (0x08): nCS Output (not used in JTAG mode).
224  * Bit 2 (0x04): nCE Output (not used in JTAG mode).
225  * Bit 1 (0x02): TMS Output.
226  * Bit 0 (0x01): TCK Output.
227  *
228  * For transmitting a single data bit, you need to write two bytes (one for
229  * setting up TDI/TMS/TCK=0, and one to trigger TCK high with same TDI/TMS
230  * held). Up to 64 bytes can be combined in a single USB packet.
231  * It isn't possible to read a data without transmitting data.
232  */
233 
234 #define TCK (1 << 0)
235 #define TMS (1 << 1)
236 #define NCE (1 << 2)
237 #define NCS (1 << 3)
238 #define TDI (1 << 4)
239 #define LED (1 << 5)
240 #define READ (1 << 6)
241 #define SHMODE (1 << 7)
242 #define READ_TDO (1 << 0)
243 
252 static void ublast_queue_byte(uint8_t abyte)
253 {
254  if (nb_buf_remaining() < 1)
256  info.buf[info.bufidx++] = abyte;
257  if (nb_buf_remaining() == 0)
259  LOG_DEBUG_IO("(byte=0x%02x)", abyte);
260 }
261 
268 static bool ublast_compute_pin(enum gpio_steer steer)
269 {
270  switch (steer) {
271  case FIXED_0:
272  return 0;
273  case FIXED_1:
274  return 1;
275  case SRST:
276  return !info.srst_asserted;
277  case TRST:
278  return !info.trst_asserted;
279  default:
280  return 1;
281  }
282 }
283 
290 static uint8_t ublast_build_out(enum scan_type type)
291 {
292  uint8_t abyte = 0;
293 
294  abyte |= info.tms ? TMS : 0;
295  abyte |= ublast_compute_pin(info.pin6) ? NCE : 0;
296  abyte |= ublast_compute_pin(info.pin8) ? NCS : 0;
297  abyte |= info.tdi ? TDI : 0;
298  abyte |= LED;
299  if (type == SCAN_IN || type == SCAN_IO)
300  abyte |= READ;
301  return abyte;
302 }
303 
309 static void ublast_reset(int trst, int srst)
310 {
311  uint8_t out_value;
312 
313  info.trst_asserted = trst;
314  info.srst_asserted = srst;
315  out_value = ublast_build_out(SCAN_OUT);
316  ublast_queue_byte(out_value);
318 }
319 
326 static void ublast_clock_tms(int tms)
327 {
328  uint8_t out;
329 
330  LOG_DEBUG_IO("(tms=%d)", !!tms);
331  info.tms = !!tms;
332  info.tdi = 0;
333  out = ublast_build_out(SCAN_OUT);
334  ublast_queue_byte(out);
335  ublast_queue_byte(out | TCK);
336 }
337 
343 static void ublast_idle_clock(void)
344 {
345  uint8_t out = ublast_build_out(SCAN_OUT);
346 
347  LOG_DEBUG_IO(".");
348  ublast_queue_byte(out);
349 }
350 
364 static void ublast_clock_tdi(int tdi, enum scan_type type)
365 {
366  uint8_t out;
367 
368  LOG_DEBUG_IO("(tdi=%d)", !!tdi);
369  info.tdi = !!tdi;
370 
371  out = ublast_build_out(SCAN_OUT);
372  ublast_queue_byte(out);
373 
374  out = ublast_build_out(type);
375  ublast_queue_byte(out | TCK);
376 }
377 
389 static void ublast_clock_tdi_flip_tms(int tdi, enum scan_type type)
390 {
391  uint8_t out;
392 
393  LOG_DEBUG_IO("(tdi=%d)", !!tdi);
394  info.tdi = !!tdi;
395  info.tms = !info.tms;
396 
397  out = ublast_build_out(SCAN_OUT);
398  ublast_queue_byte(out);
399 
400  out = ublast_build_out(type);
401  ublast_queue_byte(out | TCK);
402 
403  out = ublast_build_out(SCAN_OUT);
404  ublast_queue_byte(out);
405 }
406 
416 static void ublast_queue_bytes(uint8_t *bytes, int nb_bytes)
417 {
418  if (info.bufidx + nb_bytes > BUF_LEN) {
419  LOG_ERROR("buggy code, should never queue more that %d bytes",
420  info.bufidx + nb_bytes);
421  exit(-1);
422  }
423  LOG_DEBUG_IO("(nb_bytes=%d, bytes=[0x%02x, ...])", nb_bytes,
424  bytes ? bytes[0] : 0);
425  if (bytes)
426  memcpy(&info.buf[info.bufidx], bytes, nb_bytes);
427  else
428  memset(&info.buf[info.bufidx], 0, nb_bytes);
429  info.bufidx += nb_bytes;
430  if (nb_buf_remaining() == 0)
432 }
433 
446 static void ublast_tms_seq(const uint8_t *bits, int nb_bits, int skip)
447 {
448  int i;
449 
450  LOG_DEBUG_IO("(bits=%02x..., nb_bits=%d)", bits[0], nb_bits);
451  for (i = skip; i < nb_bits; i++)
452  ublast_clock_tms((bits[i / 8] >> (i % 8)) & 0x01);
454 }
455 
460 static void ublast_tms(struct tms_command *cmd)
461 {
462  LOG_DEBUG_IO("(num_bits=%d)", cmd->num_bits);
463  ublast_tms_seq(cmd->bits, cmd->num_bits, 0);
464 }
465 
477 {
478  LOG_DEBUG_IO("(num_states=%u, last_state=%d)",
479  cmd->num_states, cmd->path[cmd->num_states - 1]);
480  for (unsigned int i = 0; i < cmd->num_states; i++) {
481  if (tap_state_transition(tap_get_state(), false) == cmd->path[i])
482  ublast_clock_tms(0);
483  if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
484  ublast_clock_tms(1);
485  tap_set_state(cmd->path[i]);
486  }
488 }
489 
498 static void ublast_state_move(enum tap_state state, int skip)
499 {
500  uint8_t tms_scan;
501  int tms_len;
502 
503  LOG_DEBUG_IO("(from %s to %s)", tap_state_name(tap_get_state()),
505  if (tap_get_state() == state)
506  return;
507  tms_scan = tap_get_tms_path(tap_get_state(), state);
509  ublast_tms_seq(&tms_scan, tms_len, skip);
511 }
512 
527 static int ublast_read_byteshifted_tdos(uint8_t *buf, int nb_bytes)
528 {
529  uint32_t retlen;
530  int ret = ERROR_OK;
531 
532  LOG_DEBUG_IO("%s(buf=%p, num_bits=%d)", __func__, buf, nb_bytes * 8);
534  while (ret == ERROR_OK && nb_bytes > 0) {
535  ret = ublast_buf_read(buf, nb_bytes, &retlen);
536  nb_bytes -= retlen;
537  }
538  return ret;
539 }
540 
557 static int ublast_read_bitbang_tdos(uint8_t *buf, int nb_bits)
558 {
559  int nb1 = nb_bits;
560  int i, ret = ERROR_OK;
561  uint32_t retlen;
562  uint8_t tmp[8];
563 
564  LOG_DEBUG_IO("%s(buf=%p, num_bits=%d)", __func__, buf, nb_bits);
565 
566  /*
567  * Ensure all previous bitbang writes were issued to the dongle, so that
568  * it returns back the read values.
569  */
571 
572  ret = ublast_buf_read(tmp, nb1, &retlen);
573  for (i = 0; ret == ERROR_OK && i < nb1; i++)
574  if (tmp[i] & READ_TDO)
575  *buf |= (1 << i);
576  else
577  *buf &= ~(1 << i);
578  return ret;
579 }
580 
600 static void ublast_queue_tdi(uint8_t *bits, int nb_bits, enum scan_type scan)
601 {
602  int nb8 = nb_bits / 8;
603  int nb1 = nb_bits % 8;
604  int nbfree_in_packet, i, trans = 0, read_tdos;
605  uint8_t *tdos = calloc(1, nb_bits / 8 + 1);
606  static uint8_t byte0[BUF_LEN];
607 
608  /*
609  * As the last TDI bit should always be output in bitbang mode in order
610  * to activate the TMS=1 transition to EXIT_?R state. Therefore a
611  * situation where nb_bits is a multiple of 8 is handled as follows:
612  * - the number of TDI shifted out in "byteshift mode" is 8 less than
613  * nb_bits
614  * - nb1 = 8
615  * This ensures that nb1 is never 0, and allows the TMS transition.
616  */
617  if (nb8 > 0 && nb1 == 0) {
618  nb8--;
619  nb1 = 8;
620  }
621 
622  read_tdos = (scan == SCAN_IN || scan == SCAN_IO);
623  for (i = 0; i < nb8; i += trans) {
624  /*
625  * Calculate number of bytes to fill USB packet of size MAX_PACKET_SIZE
626  */
627  nbfree_in_packet = (MAX_PACKET_SIZE - (info.bufidx%MAX_PACKET_SIZE));
628  trans = MIN(nbfree_in_packet - 1, nb8 - i);
629 
630  /*
631  * Queue a byte-shift mode transmission, with as many bytes as
632  * is possible with regard to :
633  * - current filling level of write buffer
634  * - remaining bytes to write in byte-shift mode
635  */
636  if (read_tdos)
637  ublast_queue_byte(SHMODE | READ | trans);
638  else
639  ublast_queue_byte(SHMODE | trans);
640  if (bits)
641  ublast_queue_bytes(&bits[i], trans);
642  else
643  ublast_queue_bytes(byte0, trans);
644  if (read_tdos) {
645  if (info.flags & COPY_TDO_BUFFER)
647  ublast_read_byteshifted_tdos(&tdos[i], trans);
648  }
649  }
650 
651  /*
652  * Queue the remaining TDI bits in bitbang mode.
653  */
654  for (i = 0; i < nb1; i++) {
655  int tdi = bits ? bits[nb8 + i / 8] & (1 << i) : 0;
656  if (bits && i == nb1 - 1)
658  else
659  ublast_clock_tdi(tdi, scan);
660  }
661  if (nb1 && read_tdos) {
662  if (info.flags & COPY_TDO_BUFFER)
664  ublast_read_bitbang_tdos(&tdos[nb8], nb1);
665  }
666 
667  if (bits)
668  memcpy(bits, tdos, DIV_ROUND_UP(nb_bits, 8));
669  free(tdos);
670 
671  /*
672  * Ensure clock is in lower state
673  */
675 }
676 
677 static void ublast_runtest(unsigned int num_cycles, enum tap_state state)
678 {
679  LOG_DEBUG_IO("%s(cycles=%u, end_state=%d)", __func__, num_cycles, state);
680 
682  ublast_queue_tdi(NULL, num_cycles, SCAN_OUT);
684 }
685 
686 static void ublast_stableclocks(unsigned int num_cycles)
687 {
688  LOG_DEBUG_IO("%s(cycles=%u)", __func__, num_cycles);
689  ublast_queue_tdi(NULL, num_cycles, SCAN_OUT);
690 }
691 
700 static int ublast_scan(struct scan_command *cmd)
701 {
702  int scan_bits;
703  uint8_t *buf = NULL;
704  enum scan_type type;
705  int ret = ERROR_OK;
706  static const char * const type2str[] = { "", "SCAN_IN", "SCAN_OUT", "SCAN_IO" };
707  char *log_buf = NULL;
708 
710  scan_bits = jtag_build_buffer(cmd, &buf);
711 
712  if (cmd->ir_scan)
714  else
716 
717  log_buf = hexdump(buf, DIV_ROUND_UP(scan_bits, 8));
718  LOG_DEBUG_IO("%s(scan=%s, type=%s, bits=%d, buf=[%s], end_state=%d)", __func__,
719  cmd->ir_scan ? "IRSCAN" : "DRSCAN",
720  type2str[type],
721  scan_bits, log_buf, cmd->end_state);
722  free(log_buf);
723 
724  ublast_queue_tdi(buf, scan_bits, type);
725 
726  ret = jtag_read_buffer(buf, cmd);
727  free(buf);
728  /*
729  * ublast_queue_tdi sends the last bit with TMS=1. We are therefore
730  * already in Exit1-DR/IR and have to skip the first step on our way
731  * to end_state.
732  */
733  ublast_state_move(cmd->end_state, 1);
734  return ret;
735 }
736 
737 static void ublast_usleep(int us)
738 {
739  LOG_DEBUG_IO("%s(us=%d)", __func__, us);
740  jtag_sleep(us);
741 }
742 
743 static void ublast_initial_wipeout(void)
744 {
745  static uint8_t tms_reset = 0xff;
746  uint8_t out_value;
747  uint32_t retlen;
748  int i;
749 
750  out_value = ublast_build_out(SCAN_OUT);
751  for (i = 0; i < BUF_LEN; i++)
752  info.buf[i] = out_value | ((i % 2) ? TCK : 0);
753 
754  /*
755  * Flush USB-Blaster queue fifos
756  * - empty the write FIFO (128 bytes)
757  * - empty the read FIFO (384 bytes)
758  */
759  ublast_buf_write(info.buf, BUF_LEN, &retlen);
760  /*
761  * Put JTAG in RESET state (five 1 on TMS)
762  */
763  ublast_tms_seq(&tms_reset, 5, 0);
765 }
766 
767 static int ublast_execute_queue(struct jtag_command *cmd_queue)
768 {
769  struct jtag_command *cmd;
770  static int first_call = 1;
771  int ret = ERROR_OK;
772 
773  if (first_call) {
774  first_call--;
776  }
777 
778  for (cmd = cmd_queue; ret == ERROR_OK && cmd;
779  cmd = cmd->next) {
780  switch (cmd->type) {
781  case JTAG_RESET:
782  ublast_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
783  break;
784  case JTAG_RUNTEST:
785  ublast_runtest(cmd->cmd.runtest->num_cycles,
786  cmd->cmd.runtest->end_state);
787  break;
788  case JTAG_STABLECLOCKS:
789  ublast_stableclocks(cmd->cmd.stableclocks->num_cycles);
790  break;
791  case JTAG_TLR_RESET:
792  ublast_state_move(cmd->cmd.statemove->end_state, 0);
793  break;
794  case JTAG_PATHMOVE:
795  ublast_path_move(cmd->cmd.pathmove);
796  break;
797  case JTAG_TMS:
798  ublast_tms(cmd->cmd.tms);
799  break;
800  case JTAG_SLEEP:
801  ublast_usleep(cmd->cmd.sleep->us);
802  break;
803  case JTAG_SCAN:
804  ret = ublast_scan(cmd->cmd.scan);
805  break;
806  default:
807  LOG_ERROR("BUG: unknown JTAG command type 0x%X",
808  cmd->type);
809  ret = ERROR_FAIL;
810  break;
811  }
812  }
813 
815  return ret;
816 }
817 
827 static int ublast_init(void)
828 {
829  int ret;
830 
831  for (unsigned int i = 0; lowlevel_drivers_map[i].name; i++) {
832  if (info.lowlevel_name) {
833  if (!strcmp(lowlevel_drivers_map[i].name, info.lowlevel_name)) {
835  if (!info.drv) {
836  LOG_ERROR("Error registering lowlevel driver \"%s\"",
839  }
840  break;
841  }
842  } else {
844  if (info.drv) {
846  LOG_INFO("No lowlevel driver configured, using %s", info.lowlevel_name);
847  break;
848  }
849  }
850  }
851 
852  if (!info.drv) {
853  LOG_ERROR("No lowlevel driver available");
855  }
856 
857  unsigned int num_vid_pid_pairs = 0;
858 
859  for (unsigned int i = 0; adapter_usb_get_vids()[i]; i++)
860  num_vid_pid_pairs++;
861 
862  if (num_vid_pid_pairs > 0) {
865  }
866 
867  if (num_vid_pid_pairs > 1) {
870  }
871 
872  if (num_vid_pid_pairs > 2)
873  LOG_WARNING("ignoring extra IDs in adapter usb vid_pid (maximum is 2 pairs)");
874 
875  /*
876  * Register the lowlevel driver
877  */
883 
884  info.flags |= info.drv->flags;
885 
886  ret = info.drv->open(info.drv);
887 
888  /*
889  * Let lie here : the TAP is in an unknown state, but the first
890  * execute_queue() will trigger a ublast_initial_wipeout(), which will
891  * put the TAP in RESET.
892  */
894  return ret;
895 }
896 
906 static int ublast_quit(void)
907 {
908  uint8_t byte0 = 0;
909  uint32_t retlen;
910 
911  ublast_buf_write(&byte0, 1, &retlen);
912  return info.drv->close(info.drv);
913 }
914 
915 COMMAND_HANDLER(ublast_handle_pin_command)
916 {
917  uint8_t out_value;
918  const char * const pin_name = CMD_ARGV[0];
919  enum gpio_steer *steer = NULL;
920  static const char * const pin_val_str[] = {
921  [FIXED_0] = "0",
922  [FIXED_1] = "1",
923  [SRST] = "SRST driven",
924  [TRST] = "TRST driven",
925  };
926 
927  if (CMD_ARGC > 2) {
928  LOG_ERROR("%s takes exactly one or two arguments", CMD_NAME);
930  }
931 
932  if (!strcmp(pin_name, "pin6"))
933  steer = &info.pin6;
934  if (!strcmp(pin_name, "pin8"))
935  steer = &info.pin8;
936  if (!steer) {
937  LOG_ERROR("%s: pin name must be \"pin6\" or \"pin8\"",
938  CMD_NAME);
940  }
941 
942  if (CMD_ARGC == 1) {
943  LOG_INFO("%s: %s is set as %s\n", CMD_NAME, pin_name,
944  pin_val_str[*steer]);
945  }
946 
947  if (CMD_ARGC == 2) {
948  const char * const pin_value = CMD_ARGV[1];
949  char val = pin_value[0];
950 
951  if (strlen(pin_value) > 1)
952  val = '?';
953  switch (tolower((unsigned char)val)) {
954  case '0':
955  *steer = FIXED_0;
956  break;
957  case '1':
958  *steer = FIXED_1;
959  break;
960  case 't':
961  *steer = TRST;
962  break;
963  case 's':
964  *steer = SRST;
965  break;
966  default:
967  LOG_ERROR("%s: pin value must be 0, 1, s (SRST) or t (TRST)",
968  pin_value);
970  }
971 
972  if (info.drv) {
973  out_value = ublast_build_out(SCAN_OUT);
974  ublast_queue_byte(out_value);
976  }
977  }
978  return ERROR_OK;
979 }
980 
981 COMMAND_HANDLER(ublast_handle_lowlevel_drv_command)
982 {
983  if (CMD_ARGC != 1)
985 
986  info.lowlevel_name = strdup(CMD_ARGV[0]);
987 
988  return ERROR_OK;
989 }
990 
991 COMMAND_HANDLER(ublast_firmware_command)
992 {
993  if (CMD_ARGC != 1)
995 
996  info.firmware_path = strdup(CMD_ARGV[0]);
997 
998  return ERROR_OK;
999 }
1000 
1001 
1002 static const struct command_registration ublast_subcommand_handlers[] = {
1003  {
1004  .name = "lowlevel_driver",
1005  .handler = ublast_handle_lowlevel_drv_command,
1006  .mode = COMMAND_CONFIG,
1007  .help = "set the lowlevel access for the USB Blaster (ftdi, ublast2)",
1008  .usage = "(ftdi|ublast2)",
1009  },
1010  {
1011  .name = "pin",
1012  .handler = ublast_handle_pin_command,
1013  .mode = COMMAND_ANY,
1014  .help = "show or set pin state for the unused GPIO pins",
1015  .usage = "(pin6|pin8) (0|1|s|t)",
1016  },
1017  {
1018  .name = "firmware",
1019  .handler = &ublast_firmware_command,
1020  .mode = COMMAND_CONFIG,
1021  .help = "configure the USB-Blaster II firmware location",
1022  .usage = "path/to/blaster_xxxx.hex",
1023  },
1025 };
1026 
1027 static const struct command_registration ublast_command_handlers[] = {
1028  {
1029  .name = "usb_blaster",
1030  .mode = COMMAND_ANY,
1031  .help = "perform usb_blaster management",
1032  .chain = ublast_subcommand_handlers,
1033  .usage = "",
1034  },
1036 };
1037 
1038 static struct jtag_interface usb_blaster_interface = {
1040  .execute_queue = ublast_execute_queue,
1041 };
1042 
1044  .name = "usb_blaster",
1045  .transport_ids = TRANSPORT_JTAG,
1046  .transport_preferred_id = TRANSPORT_JTAG,
1047  .commands = ublast_command_handlers,
1048 
1049  .init = ublast_init,
1050  .quit = ublast_quit,
1051 
1052  .jtag_ops = &usb_blaster_interface,
1053 };
const uint16_t * adapter_usb_get_pids(void)
Definition: adapter.c:340
const uint16_t * adapter_usb_get_vids(void)
Definition: adapter.c:335
const char * name
Definition: armv4_5.c:76
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
Definition: command.h:171
#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_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
enum scan_type jtag_scan_type(const struct scan_command *cmd)
Definition: commands.c:167
int jtag_read_buffer(uint8_t *buffer, const struct scan_command *cmd)
Definition: commands.c:230
scan_type
The inferred type of a scan_command structure, indicating whether the command has the host scan in fr...
Definition: commands.h:22
@ SCAN_IN
From device to host,.
Definition: commands.h:24
@ SCAN_OUT
From host to device,.
Definition: commands.h:26
@ SCAN_IO
Full-duplex scan.
Definition: commands.h:28
@ JTAG_TLR_RESET
Definition: commands.h:137
@ JTAG_SCAN
Definition: commands.h:129
@ JTAG_PATHMOVE
Definition: commands.h:140
@ JTAG_STABLECLOCKS
Definition: commands.h:142
@ JTAG_RUNTEST
Definition: commands.h:138
@ JTAG_SLEEP
Definition: commands.h:141
@ JTAG_RESET
Definition: commands.h:139
@ JTAG_TMS
Definition: commands.h:143
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint8_t type
Definition: esp_usb_jtag.c:0
enum tap_state tap_state_transition(enum tap_state cur_state, bool tms)
Function tap_state_transition takes a current TAP state and returns the next state according to the t...
Definition: interface.c:223
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
int tap_get_tms_path_len(enum tap_state from, enum tap_state to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
Definition: interface.c:195
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
int tap_get_tms_path(enum tap_state from, enum tap_state to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
Definition: interface.c:190
#define DEBUG_CAP_TMS_SEQ
Definition: interface.h:188
#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:1070
#define ERROR_JTAG_DEVICE_ERROR
Definition: jtag.h:558
tap_state
Defines JTAG Test Access Port states.
Definition: jtag.h:37
@ TAP_RESET
Definition: jtag.h:56
@ TAP_IRSHIFT
Definition: jtag.h:51
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_DRSHIFT
Definition: jtag.h:43
static struct scan_blk scan
Definition: lakemont.c:60
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:116
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define ERROR_OK
Definition: log.h:182
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
struct ublast_lowlevel *(* drv_register)(void)
Definition: usb_blaster.c:136
char * name
Definition: usb_blaster.c:135
Represents a driver for a debugging interface.
Definition: interface.h:183
unsigned int supported
Bit vector listing capabilities exposed by this driver.
Definition: interface.h:187
The scan_command provide a means of encapsulating a set of scan_field structures that should be scann...
Definition: commands.h:35
Encapsulates a series of bits to be clocked out, affecting state and mode of the interface.
Definition: commands.h:101
uint16_t ublast_pid_uninit
Definition: usb_blaster.c:113
uint16_t ublast_vid_uninit
Definition: usb_blaster.c:113
char * firmware_path
Definition: usb_blaster.c:115
uint16_t ublast_vid
Definition: usb_blaster.c:112
bool trst_asserted
Definition: usb_blaster.c:105
enum gpio_steer pin6
Definition: usb_blaster.c:101
uint8_t buf[BUF_LEN]
Definition: usb_blaster.c:107
enum gpio_steer pin8
Definition: usb_blaster.c:102
bool srst_asserted
Definition: usb_blaster.c:106
struct ublast_lowlevel * drv
Definition: usb_blaster.c:111
char * lowlevel_name
Definition: usb_blaster.c:110
uint16_t ublast_pid
Definition: usb_blaster.c:112
uint16_t ublast_pid_uninit
Definition: ublast_access.h:27
int(* open)(struct ublast_lowlevel *low)
Definition: ublast_access.h:35
int(* write)(struct ublast_lowlevel *low, uint8_t *buf, int size, uint32_t *bytes_written)
Definition: ublast_access.h:31
int(* read)(struct ublast_lowlevel *low, uint8_t *buf, unsigned int size, uint32_t *bytes_read)
Definition: ublast_access.h:33
uint16_t ublast_vid
Definition: ublast_access.h:24
uint16_t ublast_vid_uninit
Definition: ublast_access.h:26
uint16_t ublast_pid
Definition: ublast_access.h:25
int(* close)(struct ublast_lowlevel *low)
Definition: ublast_access.h:36
char * firmware_path
Definition: ublast_access.h:29
#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
struct ublast_lowlevel * ublast2_register_libusb(void)
struct ublast_lowlevel * ublast_register_ftdi(void)
ublast_register_ftdi - get a lowlevel USB Blaster driver ublast2_register_libusb - get a lowlevel USB...
#define COPY_TDO_BUFFER
Definition: ublast_access.h:21
#define NULL
Definition: usb.h:16
static int ublast_scan(struct scan_command *cmd)
ublast_scan - launches a DR-scan or IR-scan
Definition: usb_blaster.c:700
static char * hexdump(uint8_t *buf, unsigned int size)
Definition: usb_blaster.c:152
static void ublast_queue_tdi(uint8_t *bits, int nb_bits, enum scan_type scan)
ublast_queue_tdi - short description
Definition: usb_blaster.c:600
#define SHMODE
Definition: usb_blaster.c:241
#define TDI
Definition: usb_blaster.c:238
struct adapter_driver usb_blaster_adapter_driver
Definition: usb_blaster.c:1043
static const struct command_registration ublast_subcommand_handlers[]
Definition: usb_blaster.c:1002
static const struct command_registration ublast_command_handlers[]
Definition: usb_blaster.c:1027
static int ublast_init(void)
ublast_init - Initialize the Altera device
Definition: usb_blaster.c:827
static int ublast_read_byteshifted_tdos(uint8_t *buf, int nb_bytes)
ublast_read_byteshifted_tdos - read TDO of byteshift writes
Definition: usb_blaster.c:527
#define TCK
Definition: usb_blaster.c:234
static uint8_t ublast_build_out(enum scan_type type)
ublast_build_out - build bitbang mode output byte
Definition: usb_blaster.c:290
static void ublast_runtest(unsigned int num_cycles, enum tap_state state)
Definition: usb_blaster.c:677
#define CMD_COPY_TDO_BUFFER
Definition: usb_blaster.c:91
static void ublast_tms(struct tms_command *cmd)
ublast_tms - write a tms command
Definition: usb_blaster.c:460
static void ublast_initial_wipeout(void)
Definition: usb_blaster.c:743
static void ublast_clock_tms(int tms)
ublast_clock_tms - clock a TMS transition
Definition: usb_blaster.c:326
COMMAND_HANDLER(ublast_handle_pin_command)
Definition: usb_blaster.c:915
static int ublast_buf_write(uint8_t *buf, int size, uint32_t *bytes_written)
Definition: usb_blaster.c:173
static void ublast_state_move(enum tap_state state, int skip)
ublast_state_move - move JTAG state to the target state
Definition: usb_blaster.c:498
static int ublast_quit(void)
ublast_quit - Release the Altera device
Definition: usb_blaster.c:906
static void ublast_tms_seq(const uint8_t *bits, int nb_bits, int skip)
ublast_tms_seq - write a TMS sequence transition to JTAG
Definition: usb_blaster.c:446
#define TMS
Definition: usb_blaster.c:235
static struct drvs_map lowlevel_drivers_map[]
Definition: usb_blaster.c:139
static void ublast_path_move(struct pathmove_command *cmd)
ublast_path_move - write a TMS sequence transition to JTAG
Definition: usb_blaster.c:476
static int ublast_read_bitbang_tdos(uint8_t *buf, int nb_bits)
ublast_read_bitbang_tdos - read TDO of bitbang writes
Definition: usb_blaster.c:557
static void ublast_usleep(int us)
Definition: usb_blaster.c:737
static struct jtag_interface usb_blaster_interface
Definition: usb_blaster.c:1038
#define MAX_PACKET_SIZE
Definition: usb_blaster.c:81
static int ublast_execute_queue(struct jtag_command *cmd_queue)
Definition: usb_blaster.c:767
#define BUF_LEN
Definition: usb_blaster.c:88
#define READ_TDO
Definition: usb_blaster.c:242
static void ublast_flush_buffer(void)
Definition: usb_blaster.c:189
static void ublast_reset(int trst, int srst)
ublast_reset - reset the JTAG device is possible
Definition: usb_blaster.c:309
static void ublast_idle_clock(void)
ublast_idle_clock - put back TCK to low level
Definition: usb_blaster.c:343
static void ublast_queue_bytes(uint8_t *bytes, int nb_bytes)
ublast_queue_bytes - queue bytes for the USB Blaster
Definition: usb_blaster.c:416
static struct ublast_info info
Definition: usb_blaster.c:121
static int ublast_buf_read(uint8_t *buf, unsigned int size, uint32_t *bytes_read)
Definition: usb_blaster.c:162
gpio_steer
Definition: usb_blaster.c:93
@ TRST
Definition: usb_blaster.c:97
@ FIXED_0
Definition: usb_blaster.c:94
@ FIXED_1
Definition: usb_blaster.c:95
@ SRST
Definition: usb_blaster.c:96
static void ublast_stableclocks(unsigned int num_cycles)
Definition: usb_blaster.c:686
static void ublast_clock_tdi(int tdi, enum scan_type type)
ublast_clock_tdi - Output a TDI with bitbang mode
Definition: usb_blaster.c:364
#define NCE
Definition: usb_blaster.c:236
#define READ
Definition: usb_blaster.c:240
#define NCS
Definition: usb_blaster.c:237
static bool ublast_compute_pin(enum gpio_steer steer)
ublast_compute_pin - compute if gpio should be asserted
Definition: usb_blaster.c:268
#define LED
Definition: usb_blaster.c:239
static void ublast_queue_byte(uint8_t abyte)
ublast_queue_byte - queue one 'bitbang mode' byte for USB Blaster
Definition: usb_blaster.c:252
static void ublast_clock_tdi_flip_tms(int tdi, enum scan_type type)
ublast_clock_tdi_flip_tms - Output a TDI with bitbang mode, change JTAG state
Definition: usb_blaster.c:389
static int nb_buf_remaining(void)
Definition: usb_blaster.c:184
uint8_t cmd
Definition: vdebug.c:1
uint8_t state[4]
Definition: vdebug.c:21