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/interface.h>
67 #include <jtag/commands.h>
68 #include <helper/time_support.h>
69 #include <helper/replacements.h>
70 #include "ublast_access.h"
71 
72 /* system includes */
73 #include <string.h>
74 #include <stdlib.h>
75 #include <unistd.h>
76 #include <sys/time.h>
77 #include <time.h>
78 
79 /* Size of USB endpoint max packet size, ie. 64 bytes */
80 #define MAX_PACKET_SIZE 64
81 /*
82  * Size of data buffer that holds bytes in byte-shift mode.
83  * This buffer can hold multiple USB packets aligned to
84  * MAX_PACKET_SIZE bytes boundaries.
85  * BUF_LEN must be grater than or equal MAX_PACKET_SIZE.
86  */
87 #define BUF_LEN 4096
88 
89 /* USB-Blaster II specific command */
90 #define CMD_COPY_TDO_BUFFER 0x5F
91 
92 enum gpio_steer {
93  FIXED_0 = 0,
97 };
98 
99 struct ublast_info {
100  enum gpio_steer pin6;
101  enum gpio_steer pin8;
102  int tms;
103  int tdi;
106  uint8_t buf[BUF_LEN];
107  int bufidx;
108 
113  int flags;
115 };
116 
117 /*
118  * Global device control
119  */
120 static struct ublast_info info = {
121  .ublast_vid = 0x09fb, /* Altera */
122  .ublast_pid = 0x6001, /* USB-Blaster */
123  .lowlevel_name = NULL,
124  .srst_asserted = false,
125  .trst_asserted = false,
126  .pin6 = FIXED_1,
127  .pin8 = FIXED_1,
128 };
129 
130 /*
131  * Available lowlevel drivers (FTDI, libusb, ...)
132  */
133 struct drvs_map {
134  char *name;
135  struct ublast_lowlevel *(*drv_register)(void);
136 };
137 
138 static struct drvs_map lowlevel_drivers_map[] = {
139 #if BUILD_USB_BLASTER
140  { .name = "ftdi", .drv_register = ublast_register_ftdi },
141 #endif
142 #if BUILD_USB_BLASTER_2
143  { .name = "ublast2", .drv_register = ublast2_register_libusb },
144 #endif
145  { NULL, NULL },
146 };
147 
148 /*
149  * Access functions to lowlevel driver, agnostic of libftdi/libftdxx
150  */
151 static char *hexdump(uint8_t *buf, unsigned int size)
152 {
153  unsigned int i;
154  char *str = calloc(size * 2 + 1, 1);
155 
156  for (i = 0; i < size; i++)
157  sprintf(str + 2*i, "%02x", buf[i]);
158  return str;
159 }
160 
161 static int ublast_buf_read(uint8_t *buf, unsigned int size, uint32_t *bytes_read)
162 {
163  int ret = info.drv->read(info.drv, buf, size, bytes_read);
164  char *str = hexdump(buf, *bytes_read);
165 
166  LOG_DEBUG_IO("(size=%d, buf=[%s]) -> %" PRIu32, size, str,
167  *bytes_read);
168  free(str);
169  return ret;
170 }
171 
172 static int ublast_buf_write(uint8_t *buf, int size, uint32_t *bytes_written)
173 {
174  int ret = info.drv->write(info.drv, buf, size, bytes_written);
175  char *str = hexdump(buf, *bytes_written);
176 
177  LOG_DEBUG_IO("(size=%d, buf=[%s]) -> %" PRIu32, size, str,
178  *bytes_written);
179  free(str);
180  return ret;
181 }
182 
183 static int nb_buf_remaining(void)
184 {
185  return BUF_LEN - info.bufidx;
186 }
187 
188 static void ublast_flush_buffer(void)
189 {
190  uint32_t retlen;
191  int nb = info.bufidx, ret = ERROR_OK;
192 
193  while (ret == ERROR_OK && nb > 0) {
194  ret = ublast_buf_write(info.buf, nb, &retlen);
195  nb -= retlen;
196  }
197  info.bufidx = 0;
198 }
199 
200 /*
201  * Actually, the USB-Blaster offers a byte-shift mode to transmit up to 504 data
202  * bits (bidirectional) in a single USB packet. A header byte has to be sent as
203  * the first byte in a packet with the following meaning:
204  *
205  * Bit 7 (0x80): Must be set to indicate byte-shift mode.
206  * Bit 6 (0x40): If set, the USB-Blaster will also read data, not just write.
207  * Bit 5..0: Define the number N of following bytes
208  *
209  * All N following bytes will then be clocked out serially on TDI. If Bit 6 was
210  * set, it will afterwards return N bytes with TDO data read while clocking out
211  * the TDI data. LSB of the first byte after the header byte will appear first
212  * on TDI.
213  */
214 
215 /* Simple bit banging mode:
216  *
217  * Bit 7 (0x80): Must be zero (see byte-shift mode above)
218  * Bit 6 (0x40): If set, you will receive a byte indicating the state of TDO
219  * in return.
220  * Bit 5 (0x20): Output Enable/LED.
221  * Bit 4 (0x10): TDI Output.
222  * Bit 3 (0x08): nCS Output (not used in JTAG mode).
223  * Bit 2 (0x04): nCE Output (not used in JTAG mode).
224  * Bit 1 (0x02): TMS Output.
225  * Bit 0 (0x01): TCK Output.
226  *
227  * For transmitting a single data bit, you need to write two bytes (one for
228  * setting up TDI/TMS/TCK=0, and one to trigger TCK high with same TDI/TMS
229  * held). Up to 64 bytes can be combined in a single USB packet.
230  * It isn't possible to read a data without transmitting data.
231  */
232 
233 #define TCK (1 << 0)
234 #define TMS (1 << 1)
235 #define NCE (1 << 2)
236 #define NCS (1 << 3)
237 #define TDI (1 << 4)
238 #define LED (1 << 5)
239 #define READ (1 << 6)
240 #define SHMODE (1 << 7)
241 #define READ_TDO (1 << 0)
242 
251 static void ublast_queue_byte(uint8_t abyte)
252 {
253  if (nb_buf_remaining() < 1)
255  info.buf[info.bufidx++] = abyte;
256  if (nb_buf_remaining() == 0)
258  LOG_DEBUG_IO("(byte=0x%02x)", abyte);
259 }
260 
267 static bool ublast_compute_pin(enum gpio_steer steer)
268 {
269  switch (steer) {
270  case FIXED_0:
271  return 0;
272  case FIXED_1:
273  return 1;
274  case SRST:
275  return !info.srst_asserted;
276  case TRST:
277  return !info.trst_asserted;
278  default:
279  return 1;
280  }
281 }
282 
289 static uint8_t ublast_build_out(enum scan_type type)
290 {
291  uint8_t abyte = 0;
292 
293  abyte |= info.tms ? TMS : 0;
294  abyte |= ublast_compute_pin(info.pin6) ? NCE : 0;
295  abyte |= ublast_compute_pin(info.pin8) ? NCS : 0;
296  abyte |= info.tdi ? TDI : 0;
297  abyte |= LED;
298  if (type == SCAN_IN || type == SCAN_IO)
299  abyte |= READ;
300  return abyte;
301 }
302 
308 static void ublast_reset(int trst, int srst)
309 {
310  uint8_t out_value;
311 
312  info.trst_asserted = trst;
313  info.srst_asserted = srst;
314  out_value = ublast_build_out(SCAN_OUT);
315  ublast_queue_byte(out_value);
317 }
318 
325 static void ublast_clock_tms(int tms)
326 {
327  uint8_t out;
328 
329  LOG_DEBUG_IO("(tms=%d)", !!tms);
330  info.tms = !!tms;
331  info.tdi = 0;
332  out = ublast_build_out(SCAN_OUT);
333  ublast_queue_byte(out);
334  ublast_queue_byte(out | TCK);
335 }
336 
342 static void ublast_idle_clock(void)
343 {
344  uint8_t out = ublast_build_out(SCAN_OUT);
345 
346  LOG_DEBUG_IO(".");
347  ublast_queue_byte(out);
348 }
349 
363 static void ublast_clock_tdi(int tdi, enum scan_type type)
364 {
365  uint8_t out;
366 
367  LOG_DEBUG_IO("(tdi=%d)", !!tdi);
368  info.tdi = !!tdi;
369 
370  out = ublast_build_out(SCAN_OUT);
371  ublast_queue_byte(out);
372 
373  out = ublast_build_out(type);
374  ublast_queue_byte(out | TCK);
375 }
376 
388 static void ublast_clock_tdi_flip_tms(int tdi, enum scan_type type)
389 {
390  uint8_t out;
391 
392  LOG_DEBUG_IO("(tdi=%d)", !!tdi);
393  info.tdi = !!tdi;
394  info.tms = !info.tms;
395 
396  out = ublast_build_out(SCAN_OUT);
397  ublast_queue_byte(out);
398 
399  out = ublast_build_out(type);
400  ublast_queue_byte(out | TCK);
401 
402  out = ublast_build_out(SCAN_OUT);
403  ublast_queue_byte(out);
404 }
405 
415 static void ublast_queue_bytes(uint8_t *bytes, int nb_bytes)
416 {
417  if (info.bufidx + nb_bytes > BUF_LEN) {
418  LOG_ERROR("buggy code, should never queue more that %d bytes",
419  info.bufidx + nb_bytes);
420  exit(-1);
421  }
422  LOG_DEBUG_IO("(nb_bytes=%d, bytes=[0x%02x, ...])", nb_bytes,
423  bytes ? bytes[0] : 0);
424  if (bytes)
425  memcpy(&info.buf[info.bufidx], bytes, nb_bytes);
426  else
427  memset(&info.buf[info.bufidx], 0, nb_bytes);
428  info.bufidx += nb_bytes;
429  if (nb_buf_remaining() == 0)
431 }
432 
445 static void ublast_tms_seq(const uint8_t *bits, int nb_bits, int skip)
446 {
447  int i;
448 
449  LOG_DEBUG_IO("(bits=%02x..., nb_bits=%d)", bits[0], nb_bits);
450  for (i = skip; i < nb_bits; i++)
451  ublast_clock_tms((bits[i / 8] >> (i % 8)) & 0x01);
453 }
454 
459 static void ublast_tms(struct tms_command *cmd)
460 {
461  LOG_DEBUG_IO("(num_bits=%d)", cmd->num_bits);
462  ublast_tms_seq(cmd->bits, cmd->num_bits, 0);
463 }
464 
476 {
477  LOG_DEBUG_IO("(num_states=%u, last_state=%d)",
478  cmd->num_states, cmd->path[cmd->num_states - 1]);
479  for (unsigned int i = 0; i < cmd->num_states; i++) {
480  if (tap_state_transition(tap_get_state(), false) == cmd->path[i])
481  ublast_clock_tms(0);
482  if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
483  ublast_clock_tms(1);
484  tap_set_state(cmd->path[i]);
485  }
487 }
488 
497 static void ublast_state_move(tap_state_t state, int skip)
498 {
499  uint8_t tms_scan;
500  int tms_len;
501 
502  LOG_DEBUG_IO("(from %s to %s)", tap_state_name(tap_get_state()),
504  if (tap_get_state() == state)
505  return;
506  tms_scan = tap_get_tms_path(tap_get_state(), state);
508  ublast_tms_seq(&tms_scan, tms_len, skip);
510 }
511 
526 static int ublast_read_byteshifted_tdos(uint8_t *buf, int nb_bytes)
527 {
528  uint32_t retlen;
529  int ret = ERROR_OK;
530 
531  LOG_DEBUG_IO("%s(buf=%p, num_bits=%d)", __func__, buf, nb_bytes * 8);
533  while (ret == ERROR_OK && nb_bytes > 0) {
534  ret = ublast_buf_read(buf, nb_bytes, &retlen);
535  nb_bytes -= retlen;
536  }
537  return ret;
538 }
539 
556 static int ublast_read_bitbang_tdos(uint8_t *buf, int nb_bits)
557 {
558  int nb1 = nb_bits;
559  int i, ret = ERROR_OK;
560  uint32_t retlen;
561  uint8_t tmp[8];
562 
563  LOG_DEBUG_IO("%s(buf=%p, num_bits=%d)", __func__, buf, nb_bits);
564 
565  /*
566  * Ensure all previous bitbang writes were issued to the dongle, so that
567  * it returns back the read values.
568  */
570 
571  ret = ublast_buf_read(tmp, nb1, &retlen);
572  for (i = 0; ret == ERROR_OK && i < nb1; i++)
573  if (tmp[i] & READ_TDO)
574  *buf |= (1 << i);
575  else
576  *buf &= ~(1 << i);
577  return ret;
578 }
579 
599 static void ublast_queue_tdi(uint8_t *bits, int nb_bits, enum scan_type scan)
600 {
601  int nb8 = nb_bits / 8;
602  int nb1 = nb_bits % 8;
603  int nbfree_in_packet, i, trans = 0, read_tdos;
604  uint8_t *tdos = calloc(1, nb_bits / 8 + 1);
605  static uint8_t byte0[BUF_LEN];
606 
607  /*
608  * As the last TDI bit should always be output in bitbang mode in order
609  * to activate the TMS=1 transition to EXIT_?R state. Therefore a
610  * situation where nb_bits is a multiple of 8 is handled as follows:
611  * - the number of TDI shifted out in "byteshift mode" is 8 less than
612  * nb_bits
613  * - nb1 = 8
614  * This ensures that nb1 is never 0, and allows the TMS transition.
615  */
616  if (nb8 > 0 && nb1 == 0) {
617  nb8--;
618  nb1 = 8;
619  }
620 
621  read_tdos = (scan == SCAN_IN || scan == SCAN_IO);
622  for (i = 0; i < nb8; i += trans) {
623  /*
624  * Calculate number of bytes to fill USB packet of size MAX_PACKET_SIZE
625  */
626  nbfree_in_packet = (MAX_PACKET_SIZE - (info.bufidx%MAX_PACKET_SIZE));
627  trans = MIN(nbfree_in_packet - 1, nb8 - i);
628 
629  /*
630  * Queue a byte-shift mode transmission, with as many bytes as
631  * is possible with regard to :
632  * - current filling level of write buffer
633  * - remaining bytes to write in byte-shift mode
634  */
635  if (read_tdos)
636  ublast_queue_byte(SHMODE | READ | trans);
637  else
638  ublast_queue_byte(SHMODE | trans);
639  if (bits)
640  ublast_queue_bytes(&bits[i], trans);
641  else
642  ublast_queue_bytes(byte0, trans);
643  if (read_tdos) {
644  if (info.flags & COPY_TDO_BUFFER)
646  ublast_read_byteshifted_tdos(&tdos[i], trans);
647  }
648  }
649 
650  /*
651  * Queue the remaining TDI bits in bitbang mode.
652  */
653  for (i = 0; i < nb1; i++) {
654  int tdi = bits ? bits[nb8 + i / 8] & (1 << i) : 0;
655  if (bits && i == nb1 - 1)
657  else
658  ublast_clock_tdi(tdi, scan);
659  }
660  if (nb1 && read_tdos) {
661  if (info.flags & COPY_TDO_BUFFER)
663  ublast_read_bitbang_tdos(&tdos[nb8], nb1);
664  }
665 
666  if (bits)
667  memcpy(bits, tdos, DIV_ROUND_UP(nb_bits, 8));
668  free(tdos);
669 
670  /*
671  * Ensure clock is in lower state
672  */
674 }
675 
676 static void ublast_runtest(unsigned int num_cycles, tap_state_t state)
677 {
678  LOG_DEBUG_IO("%s(cycles=%u, end_state=%d)", __func__, num_cycles, state);
679 
681  ublast_queue_tdi(NULL, num_cycles, SCAN_OUT);
683 }
684 
685 static void ublast_stableclocks(unsigned int num_cycles)
686 {
687  LOG_DEBUG_IO("%s(cycles=%u)", __func__, num_cycles);
688  ublast_queue_tdi(NULL, num_cycles, SCAN_OUT);
689 }
690 
699 static int ublast_scan(struct scan_command *cmd)
700 {
701  int scan_bits;
702  uint8_t *buf = NULL;
703  enum scan_type type;
704  int ret = ERROR_OK;
705  static const char * const type2str[] = { "", "SCAN_IN", "SCAN_OUT", "SCAN_IO" };
706  char *log_buf = NULL;
707 
709  scan_bits = jtag_build_buffer(cmd, &buf);
710 
711  if (cmd->ir_scan)
713  else
715 
716  log_buf = hexdump(buf, DIV_ROUND_UP(scan_bits, 8));
717  LOG_DEBUG_IO("%s(scan=%s, type=%s, bits=%d, buf=[%s], end_state=%d)", __func__,
718  cmd->ir_scan ? "IRSCAN" : "DRSCAN",
719  type2str[type],
720  scan_bits, log_buf, cmd->end_state);
721  free(log_buf);
722 
723  ublast_queue_tdi(buf, scan_bits, type);
724 
725  ret = jtag_read_buffer(buf, cmd);
726  free(buf);
727  /*
728  * ublast_queue_tdi sends the last bit with TMS=1. We are therefore
729  * already in Exit1-DR/IR and have to skip the first step on our way
730  * to end_state.
731  */
732  ublast_state_move(cmd->end_state, 1);
733  return ret;
734 }
735 
736 static void ublast_usleep(int us)
737 {
738  LOG_DEBUG_IO("%s(us=%d)", __func__, us);
739  jtag_sleep(us);
740 }
741 
742 static void ublast_initial_wipeout(void)
743 {
744  static uint8_t tms_reset = 0xff;
745  uint8_t out_value;
746  uint32_t retlen;
747  int i;
748 
749  out_value = ublast_build_out(SCAN_OUT);
750  for (i = 0; i < BUF_LEN; i++)
751  info.buf[i] = out_value | ((i % 2) ? TCK : 0);
752 
753  /*
754  * Flush USB-Blaster queue fifos
755  * - empty the write FIFO (128 bytes)
756  * - empty the read FIFO (384 bytes)
757  */
758  ublast_buf_write(info.buf, BUF_LEN, &retlen);
759  /*
760  * Put JTAG in RESET state (five 1 on TMS)
761  */
762  ublast_tms_seq(&tms_reset, 5, 0);
764 }
765 
766 static int ublast_execute_queue(struct jtag_command *cmd_queue)
767 {
768  struct jtag_command *cmd;
769  static int first_call = 1;
770  int ret = ERROR_OK;
771 
772  if (first_call) {
773  first_call--;
775  }
776 
777  for (cmd = cmd_queue; ret == ERROR_OK && cmd;
778  cmd = cmd->next) {
779  switch (cmd->type) {
780  case JTAG_RESET:
781  ublast_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
782  break;
783  case JTAG_RUNTEST:
784  ublast_runtest(cmd->cmd.runtest->num_cycles,
785  cmd->cmd.runtest->end_state);
786  break;
787  case JTAG_STABLECLOCKS:
788  ublast_stableclocks(cmd->cmd.stableclocks->num_cycles);
789  break;
790  case JTAG_TLR_RESET:
791  ublast_state_move(cmd->cmd.statemove->end_state, 0);
792  break;
793  case JTAG_PATHMOVE:
794  ublast_path_move(cmd->cmd.pathmove);
795  break;
796  case JTAG_TMS:
797  ublast_tms(cmd->cmd.tms);
798  break;
799  case JTAG_SLEEP:
800  ublast_usleep(cmd->cmd.sleep->us);
801  break;
802  case JTAG_SCAN:
803  ret = ublast_scan(cmd->cmd.scan);
804  break;
805  default:
806  LOG_ERROR("BUG: unknown JTAG command type 0x%X",
807  cmd->type);
808  ret = ERROR_FAIL;
809  break;
810  }
811  }
812 
814  return ret;
815 }
816 
826 static int ublast_init(void)
827 {
828  int ret, i;
829 
830  for (i = 0; lowlevel_drivers_map[i].name; i++) {
831  if (info.lowlevel_name) {
832  if (!strcmp(lowlevel_drivers_map[i].name, info.lowlevel_name)) {
834  if (!info.drv) {
835  LOG_ERROR("Error registering lowlevel driver \"%s\"",
838  }
839  break;
840  }
841  } else {
843  if (info.drv) {
845  LOG_INFO("No lowlevel driver configured, using %s", info.lowlevel_name);
846  break;
847  }
848  }
849  }
850 
851  if (!info.drv) {
852  LOG_ERROR("No lowlevel driver available");
854  }
855 
856  /*
857  * Register the lowlevel driver
858  */
864 
865  info.flags |= info.drv->flags;
866 
867  ret = info.drv->open(info.drv);
868 
869  /*
870  * Let lie here : the TAP is in an unknown state, but the first
871  * execute_queue() will trigger a ublast_initial_wipeout(), which will
872  * put the TAP in RESET.
873  */
875  return ret;
876 }
877 
887 static int ublast_quit(void)
888 {
889  uint8_t byte0 = 0;
890  uint32_t retlen;
891 
892  ublast_buf_write(&byte0, 1, &retlen);
893  return info.drv->close(info.drv);
894 }
895 
896 COMMAND_HANDLER(ublast_handle_vid_pid_command)
897 {
898  if (CMD_ARGC > 4) {
899  LOG_WARNING("ignoring extra IDs in ublast_vid_pid "
900  "(maximum is 2 pairs)");
901  CMD_ARGC = 4;
902  }
903 
904  if (CMD_ARGC >= 2) {
907  } else {
908  LOG_WARNING("incomplete ublast_vid_pid configuration");
909  }
910 
911  if (CMD_ARGC == 4) {
914  } else {
915  LOG_WARNING("incomplete ublast_vid_pid configuration");
916  }
917 
918  return ERROR_OK;
919 }
920 
921 COMMAND_HANDLER(ublast_handle_pin_command)
922 {
923  uint8_t out_value;
924  const char * const pin_name = CMD_ARGV[0];
925  enum gpio_steer *steer = NULL;
926  static const char * const pin_val_str[] = {
927  [FIXED_0] = "0",
928  [FIXED_1] = "1",
929  [SRST] = "SRST driven",
930  [TRST] = "TRST driven",
931  };
932 
933  if (CMD_ARGC > 2) {
934  LOG_ERROR("%s takes exactly one or two arguments", CMD_NAME);
936  }
937 
938  if (!strcmp(pin_name, "pin6"))
939  steer = &info.pin6;
940  if (!strcmp(pin_name, "pin8"))
941  steer = &info.pin8;
942  if (!steer) {
943  LOG_ERROR("%s: pin name must be \"pin6\" or \"pin8\"",
944  CMD_NAME);
946  }
947 
948  if (CMD_ARGC == 1) {
949  LOG_INFO("%s: %s is set as %s\n", CMD_NAME, pin_name,
950  pin_val_str[*steer]);
951  }
952 
953  if (CMD_ARGC == 2) {
954  const char * const pin_value = CMD_ARGV[1];
955  char val = pin_value[0];
956 
957  if (strlen(pin_value) > 1)
958  val = '?';
959  switch (tolower((unsigned char)val)) {
960  case '0':
961  *steer = FIXED_0;
962  break;
963  case '1':
964  *steer = FIXED_1;
965  break;
966  case 't':
967  *steer = TRST;
968  break;
969  case 's':
970  *steer = SRST;
971  break;
972  default:
973  LOG_ERROR("%s: pin value must be 0, 1, s (SRST) or t (TRST)",
974  pin_value);
976  }
977 
978  if (info.drv) {
979  out_value = ublast_build_out(SCAN_OUT);
980  ublast_queue_byte(out_value);
982  }
983  }
984  return ERROR_OK;
985 }
986 
987 COMMAND_HANDLER(ublast_handle_lowlevel_drv_command)
988 {
989  if (CMD_ARGC != 1)
991 
992  info.lowlevel_name = strdup(CMD_ARGV[0]);
993 
994  return ERROR_OK;
995 }
996 
997 COMMAND_HANDLER(ublast_firmware_command)
998 {
999  if (CMD_ARGC != 1)
1001 
1002  info.firmware_path = strdup(CMD_ARGV[0]);
1003 
1004  return ERROR_OK;
1005 }
1006 
1007 
1008 static const struct command_registration ublast_subcommand_handlers[] = {
1009  {
1010  .name = "vid_pid",
1011  .handler = ublast_handle_vid_pid_command,
1012  .mode = COMMAND_CONFIG,
1013  .help = "the vendor ID and product ID of the USB-Blaster and "
1014  "vendor ID and product ID of the uninitialized device "
1015  "for USB-Blaster II",
1016  .usage = "vid pid vid_uninit pid_uninit",
1017  },
1018  {
1019  .name = "lowlevel_driver",
1020  .handler = ublast_handle_lowlevel_drv_command,
1021  .mode = COMMAND_CONFIG,
1022  .help = "set the lowlevel access for the USB Blaster (ftdi, ublast2)",
1023  .usage = "(ftdi|ublast2)",
1024  },
1025  {
1026  .name = "pin",
1027  .handler = ublast_handle_pin_command,
1028  .mode = COMMAND_ANY,
1029  .help = "show or set pin state for the unused GPIO pins",
1030  .usage = "(pin6|pin8) (0|1|s|t)",
1031  },
1032  {
1033  .name = "firmware",
1034  .handler = &ublast_firmware_command,
1035  .mode = COMMAND_CONFIG,
1036  .help = "configure the USB-Blaster II firmware location",
1037  .usage = "path/to/blaster_xxxx.hex",
1038  },
1040 };
1041 
1042 static const struct command_registration ublast_command_handlers[] = {
1043  {
1044  .name = "usb_blaster",
1045  .mode = COMMAND_ANY,
1046  .help = "perform usb_blaster management",
1047  .chain = ublast_subcommand_handlers,
1048  .usage = "",
1049  },
1051 };
1052 
1053 static struct jtag_interface usb_blaster_interface = {
1055  .execute_queue = ublast_execute_queue,
1056 };
1057 
1059  .name = "usb_blaster",
1060  .transports = jtag_only,
1061  .commands = ublast_command_handlers,
1062 
1063  .init = ublast_init,
1064  .quit = ublast_quit,
1065 
1066  .jtag_ops = &usb_blaster_interface,
1067 };
const char *const jtag_only[]
Definition: adapter.c:27
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:166
#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: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
uint8_t type
Definition: esp_usb_jtag.c:0
tap_state_t tap_state_transition(tap_state_t 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(tap_state_t 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(tap_state_t from, tap_state_t to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
Definition: interface.c:190
int tap_get_tms_path_len(tap_state_t from, tap_state_t to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
Definition: interface.c:195
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 DEBUG_CAP_TMS_SEQ
Definition: interface.h:187
#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_RESET
Definition: jtag.h:56
@ TAP_IRSHIFT
Definition: jtag.h:51
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_DRSHIFT
Definition: jtag.h:43
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
static struct scan_blk scan
Definition: lakemont.c:60
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:101
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_INFO(expr ...)
Definition: log.h:126
#define ERROR_OK
Definition: log.h:164
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
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
struct ublast_lowlevel *(* drv_register)(void)
Definition: usb_blaster.c:135
char * name
Definition: usb_blaster.c:134
Represents a driver for a debugging interface.
Definition: interface.h:182
unsigned int supported
Bit vector listing capabilities exposed by this driver.
Definition: interface.h:186
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:112
uint16_t ublast_vid_uninit
Definition: usb_blaster.c:112
char * firmware_path
Definition: usb_blaster.c:114
uint16_t ublast_vid
Definition: usb_blaster.c:111
bool trst_asserted
Definition: usb_blaster.c:104
enum gpio_steer pin6
Definition: usb_blaster.c:100
uint8_t buf[BUF_LEN]
Definition: usb_blaster.c:106
enum gpio_steer pin8
Definition: usb_blaster.c:101
bool srst_asserted
Definition: usb_blaster.c:105
struct ublast_lowlevel * drv
Definition: usb_blaster.c:110
char * lowlevel_name
Definition: usb_blaster.c:109
uint16_t ublast_pid
Definition: usb_blaster.c:111
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 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:699
static char * hexdump(uint8_t *buf, unsigned int size)
Definition: usb_blaster.c:151
static void ublast_queue_tdi(uint8_t *bits, int nb_bits, enum scan_type scan)
ublast_queue_tdi - short description
Definition: usb_blaster.c:599
#define SHMODE
Definition: usb_blaster.c:240
#define TDI
Definition: usb_blaster.c:237
struct adapter_driver usb_blaster_adapter_driver
Definition: usb_blaster.c:1058
static const struct command_registration ublast_subcommand_handlers[]
Definition: usb_blaster.c:1008
static const struct command_registration ublast_command_handlers[]
Definition: usb_blaster.c:1042
static int ublast_init(void)
ublast_init - Initialize the Altera device
Definition: usb_blaster.c:826
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:526
#define TCK
Definition: usb_blaster.c:233
static uint8_t ublast_build_out(enum scan_type type)
ublast_build_out - build bitbang mode output byte
Definition: usb_blaster.c:289
#define CMD_COPY_TDO_BUFFER
Definition: usb_blaster.c:90
static void ublast_tms(struct tms_command *cmd)
ublast_tms - write a tms command
Definition: usb_blaster.c:459
static void ublast_initial_wipeout(void)
Definition: usb_blaster.c:742
static void ublast_clock_tms(int tms)
ublast_clock_tms - clock a TMS transition
Definition: usb_blaster.c:325
static int ublast_buf_write(uint8_t *buf, int size, uint32_t *bytes_written)
Definition: usb_blaster.c:172
static int ublast_quit(void)
ublast_quit - Release the Altera device
Definition: usb_blaster.c:887
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:445
#define TMS
Definition: usb_blaster.c:234
static struct drvs_map lowlevel_drivers_map[]
Definition: usb_blaster.c:138
static void ublast_path_move(struct pathmove_command *cmd)
ublast_path_move - write a TMS sequence transition to JTAG
Definition: usb_blaster.c:475
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:556
static void ublast_usleep(int us)
Definition: usb_blaster.c:736
static struct jtag_interface usb_blaster_interface
Definition: usb_blaster.c:1053
#define MAX_PACKET_SIZE
Definition: usb_blaster.c:80
static int ublast_execute_queue(struct jtag_command *cmd_queue)
Definition: usb_blaster.c:766
#define BUF_LEN
Definition: usb_blaster.c:87
#define READ_TDO
Definition: usb_blaster.c:241
static void ublast_flush_buffer(void)
Definition: usb_blaster.c:188
static void ublast_reset(int trst, int srst)
ublast_reset - reset the JTAG device is possible
Definition: usb_blaster.c:308
static void ublast_idle_clock(void)
ublast_idle_clock - put back TCK to low level
Definition: usb_blaster.c:342
static void ublast_queue_bytes(uint8_t *bytes, int nb_bytes)
ublast_queue_bytes - queue bytes for the USB Blaster
Definition: usb_blaster.c:415
static struct ublast_info info
Definition: usb_blaster.c:120
static int ublast_buf_read(uint8_t *buf, unsigned int size, uint32_t *bytes_read)
Definition: usb_blaster.c:161
gpio_steer
Definition: usb_blaster.c:92
@ TRST
Definition: usb_blaster.c:96
@ FIXED_0
Definition: usb_blaster.c:93
@ FIXED_1
Definition: usb_blaster.c:94
@ SRST
Definition: usb_blaster.c:95
static void ublast_runtest(unsigned int num_cycles, tap_state_t state)
Definition: usb_blaster.c:676
static void ublast_stableclocks(unsigned int num_cycles)
Definition: usb_blaster.c:685
static void ublast_clock_tdi(int tdi, enum scan_type type)
ublast_clock_tdi - Output a TDI with bitbang mode
Definition: usb_blaster.c:363
#define NCE
Definition: usb_blaster.c:235
#define READ
Definition: usb_blaster.c:239
static void ublast_state_move(tap_state_t state, int skip)
ublast_state_move - move JTAG state to the target state
Definition: usb_blaster.c:497
#define NCS
Definition: usb_blaster.c:236
static bool ublast_compute_pin(enum gpio_steer steer)
ublast_compute_pin - compute if gpio should be asserted
Definition: usb_blaster.c:267
#define LED
Definition: usb_blaster.c:238
static void ublast_queue_byte(uint8_t abyte)
ublast_queue_byte - queue one 'bitbang mode' byte for USB Blaster
Definition: usb_blaster.c:251
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:388
COMMAND_HANDLER(ublast_handle_vid_pid_command)
Definition: usb_blaster.c:896
static int nb_buf_remaining(void)
Definition: usb_blaster.c:183
uint8_t cmd
Definition: vdebug.c:1
uint8_t state[4]
Definition: vdebug.c:21