OpenOCD
ft232r.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2010 Serge Vakulenko *
5  * serge@vak.ru *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #if IS_CYGWIN == 1
13 #include "windows.h"
14 #undef LOG_ERROR
15 #endif
16 
17 /* project specific includes */
18 #include <jtag/adapter.h>
19 #include <jtag/interface.h>
20 #include <jtag/commands.h>
21 #include <helper/time_support.h>
22 #include "libusb_helper.h"
23 
24 /* system includes */
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <sys/time.h>
29 #include <time.h>
30 
31 /*
32  * Sync bit bang mode is implemented as described in FTDI Application
33  * Note AN232R-01: "Bit Bang Modes for the FT232R and FT245R".
34  */
35 
36 /*
37  * USB endpoints.
38  */
39 #define IN_EP 0x02
40 #define OUT_EP 0x81
41 
42 /* Requests */
43 #define SIO_RESET 0 /* Reset the port */
44 #define SIO_MODEM_CTRL 1 /* Set the modem control register */
45 #define SIO_SET_FLOW_CTRL 2 /* Set flow control register */
46 #define SIO_SET_BAUD_RATE 3 /* Set baud rate */
47 #define SIO_SET_DATA 4 /* Set the data characteristics of the port */
48 #define SIO_POLL_MODEM_STATUS 5
49 #define SIO_SET_EVENT_CHAR 6
50 #define SIO_SET_ERROR_CHAR 7
51 #define SIO_SET_LATENCY_TIMER 9
52 #define SIO_GET_LATENCY_TIMER 10
53 #define SIO_SET_BITMODE 11
54 #define SIO_READ_PINS 12
55 #define SIO_READ_EEPROM 0x90
56 #define SIO_WRITE_EEPROM 0x91
57 #define SIO_ERASE_EEPROM 0x92
58 
59 #define FT232R_BUF_SIZE_EXTRA 4096
60 
61 // Default VID/PID pair for FTDI FT232R.
62 static uint16_t ft232r_vids[] = {0x0403, 0};
63 static uint16_t ft232r_pids[] = {0x6001, 0};
64 
65 static struct libusb_device_handle *adapter;
66 
67 static uint8_t *ft232r_output;
68 static size_t ft232r_output_len;
69 
73 #define FT232R_BIT_COUNT 8
75  "TXD", /* 0: pin 1 TCK output */
76  "RXD", /* 1: pin 5 TDI output */
77  "RTS", /* 2: pin 3 TDO input */
78  "CTS", /* 3: pin 11 TMS output */
79  "DTR", /* 4: pin 2 /TRST output */
80  "DSR", /* 5: pin 9 unused */
81  "DCD", /* 6: pin 10 /SYSRST output */
82  "RI" /* 7: pin 6 unused */
83 };
84 
85 static int tck_gpio; /* initialized to 0 by default */
86 static int tdi_gpio = 1;
87 static int tdo_gpio = 2;
88 static int tms_gpio = 3;
89 static int ntrst_gpio = 4;
90 static int nsysrst_gpio = 6;
95 static uint16_t ft232r_restore_bitmode = 0xFFFF;
96 
103 static int ft232r_send_recv(void)
104 {
105  /* FIFO TX buffer has 128 bytes.
106  * FIFO RX buffer has 256 bytes.
107  * First two bytes of received packet contain contain modem
108  * and line status and are ignored.
109  * Unfortunately, transfer sizes bigger than 64 bytes
110  * frequently cause hang ups. */
111  assert(ft232r_output_len > 0);
112 
113  size_t total_written = 0;
114  size_t total_read = 0;
115  int rxfifo_free = 128;
116 
117  while (total_read < ft232r_output_len) {
118  /* Write */
119  int bytes_to_write = ft232r_output_len - total_written;
120  if (bytes_to_write > 64)
121  bytes_to_write = 64;
122  if (bytes_to_write > rxfifo_free)
123  bytes_to_write = rxfifo_free;
124 
125  if (bytes_to_write) {
126  int n;
127 
129  (char *) ft232r_output + total_written,
130  bytes_to_write, 1000, &n) != ERROR_OK) {
131  LOG_ERROR("usb bulk write failed");
133  }
134 
135  total_written += n;
136  rxfifo_free -= n;
137  }
138 
139  /* Read */
140  uint8_t reply[64];
141  int n;
142 
143  if (jtag_libusb_bulk_read(adapter, OUT_EP, (char *) reply,
144  sizeof(reply), 1000, &n) != ERROR_OK) {
145  LOG_ERROR("usb bulk read failed");
147  }
148  if (n > 2) {
149  /* Copy data, ignoring first 2 bytes. */
150  memcpy(ft232r_output + total_read, reply + 2, n - 2);
151  int bytes_read = n - 2;
152  total_read += bytes_read;
153  rxfifo_free += bytes_read;
154  if (total_read > total_written) {
155  LOG_ERROR("read more bytes than wrote");
157  }
158  }
159  }
160  ft232r_output_len = 0;
161  return ERROR_OK;
162 }
163 
164 static void ft232r_increase_buf_size(size_t new_buf_size)
165 {
166  uint8_t *new_buf_ptr;
167  if (new_buf_size >= ft232r_buf_size) {
168  new_buf_size += FT232R_BUF_SIZE_EXTRA;
169  new_buf_ptr = realloc(ft232r_output, new_buf_size);
170  if (new_buf_ptr) {
171  ft232r_output = new_buf_ptr;
172  ft232r_buf_size = new_buf_size;
173  }
174  }
175 }
176 
180 static void ft232r_write(int tck, int tms, int tdi)
181 {
182  unsigned int out_value = (1 << ntrst_gpio) | (1 << nsysrst_gpio);
183  if (tck)
184  out_value |= (1<<tck_gpio);
185  if (tms)
186  out_value |= (1<<tms_gpio);
187  if (tdi)
188  out_value |= (1<<tdi_gpio);
189 
191 
193  /* FIXME: should we just execute queue here? */
194  LOG_ERROR("ft232r_write: buffer overflow");
195  return;
196  }
197  ft232r_output[ft232r_output_len++] = out_value;
198 }
199 
204 static void ft232r_reset(int trst, int srst)
205 {
206  unsigned int out_value = (1 << ntrst_gpio) | (1 << nsysrst_gpio);
207  LOG_DEBUG("ft232r_reset(%d,%d)", trst, srst);
208 
209  if (trst == 1)
210  out_value &= ~(1<<ntrst_gpio); /* switch /TRST low */
211  else if (trst == 0)
212  out_value |= (1<<ntrst_gpio); /* switch /TRST high */
213 
214  if (srst == 1)
215  out_value &= ~(1<<nsysrst_gpio); /* switch /SYSRST low */
216  else if (srst == 0)
217  out_value |= (1<<nsysrst_gpio); /* switch /SYSRST high */
218 
220 
222  /* FIXME: should we just execute queue here? */
223  LOG_ERROR("ft232r_write: buffer overflow");
224  return;
225  }
226 
227  ft232r_output[ft232r_output_len++] = out_value;
229 }
230 
231 static int ft232r_speed(int divisor)
232 {
233  int baud = (divisor == 0) ? 3000000 :
234  (divisor == 1) ? 2000000 :
235  3000000 / divisor;
236  LOG_DEBUG("ft232r_speed(%d) rate %d bits/sec", divisor, baud);
237 
239  LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
240  SIO_SET_BAUD_RATE, divisor, 0, NULL, 0, 1000, NULL) != ERROR_OK) {
241  LOG_ERROR("cannot set baud rate");
243  }
244  return ERROR_OK;
245 }
246 
247 static int ft232r_init(void)
248 {
249  const uint16_t *vids = ft232r_vids;
250  const uint16_t *pids = ft232r_pids;
251 
252  if (adapter_usb_get_vids()[0] != 0) {
253  vids = adapter_usb_get_vids();
254  pids = adapter_usb_get_pids();
255  }
256 
257  if (jtag_libusb_open(vids, pids, NULL, &adapter, NULL)) {
258  char usb_vid_pid_str[256] = {0};
259  for (unsigned int i = 0; vids[i] != 0; i++)
260  snprintf(usb_vid_pid_str + strlen(usb_vid_pid_str),
261  sizeof(usb_vid_pid_str) - strlen(usb_vid_pid_str),
262  "%04x:%04x ", vids[i], pids[i]);
263 
264  const char *ft232r_serial_desc = adapter_get_required_serial();
265  LOG_ERROR("ft232r not found: serial=%s, vid/pid=%s\n",
266  (!ft232r_serial_desc) ? "[any]" : ft232r_serial_desc, usb_vid_pid_str);
267  return ERROR_JTAG_INIT_FAILED;
268  }
269 
270  if (ft232r_restore_bitmode == 0xFFFF) /* serial port will not be restored after jtag: */
271  libusb_detach_kernel_driver(adapter, 0);
272  else /* serial port will be restored after jtag: */
273  libusb_set_auto_detach_kernel_driver(adapter, 1); /* 1: DONT_DETACH_SIO_MODULE */
274 
275  if (libusb_claim_interface(adapter, 0)) {
276  LOG_ERROR("unable to claim interface");
277  return ERROR_JTAG_INIT_FAILED;
278  }
279 
280  /* Reset the device. */
282  LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
283  SIO_RESET, 0, 0, NULL, 0, 1000, NULL) != ERROR_OK) {
284  LOG_ERROR("unable to reset device");
285  return ERROR_JTAG_INIT_FAILED;
286  }
287 
288  /* Sync bit bang mode. */
290  LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
291  SIO_SET_BITMODE, (1<<tck_gpio) | (1<<tdi_gpio) | (1<<tms_gpio) | (1<<ntrst_gpio) | (1<<nsysrst_gpio) | 0x400,
292  0, NULL, 0, 1000, NULL) != ERROR_OK) {
293  LOG_ERROR("cannot set sync bitbang mode");
294  return ERROR_JTAG_INIT_FAILED;
295  }
296 
297  /* Exactly 500 nsec between updates. */
298  unsigned int divisor = 1;
299  unsigned char latency_timer = 1;
300 
301  /* Frequency divisor is 14-bit non-zero value. */
303  LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
304  SIO_SET_BAUD_RATE, divisor,
305  0, NULL, 0, 1000, NULL) != ERROR_OK) {
306  LOG_ERROR("cannot set baud rate");
307  return ERROR_JTAG_INIT_FAILED;
308  }
310  LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
311  SIO_SET_LATENCY_TIMER, latency_timer, 0, NULL, 0, 1000, NULL) != ERROR_OK) {
312  LOG_ERROR("unable to set latency timer");
313  return ERROR_JTAG_INIT_FAILED;
314  }
315 
316  ft232r_output = malloc(ft232r_buf_size);
317  if (!ft232r_output) {
318  LOG_ERROR("Unable to allocate memory for the buffer");
319  return ERROR_JTAG_INIT_FAILED;
320  }
321 
322  return ERROR_OK;
323 }
324 
325 static int ft232r_quit(void)
326 {
327  /* to restore serial port: set TXD RTS DTR as outputs, others as inputs, disable sync bit bang mode. */
328  if (ft232r_restore_bitmode != 0xFFFF) {
330  LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
332  0, NULL, 0, 1000, NULL) != ERROR_OK) {
333  LOG_ERROR("cannot set bitmode to restore serial port");
334  }
335  }
336 
337  if (libusb_release_interface(adapter, 0) != 0)
338  LOG_ERROR("usb release interface failed");
339 
341 
342  free(ft232r_output); /* free used memory */
343  ft232r_output = NULL; /* reset pointer to memory */
344  ft232r_buf_size = FT232R_BUF_SIZE_EXTRA; /* reset next initial buffer size */
345 
346  return ERROR_OK;
347 }
348 
349 static int ft232r_speed_div(int divisor, int *khz)
350 {
351  /* Maximum 3 Mbaud for bit bang mode. */
352  if (divisor == 0)
353  *khz = 3000;
354  else if (divisor == 1)
355  *khz = 2000;
356  else
357  *khz = 3000 / divisor;
358  return ERROR_OK;
359 }
360 
361 static int ft232r_khz(int khz, int *divisor)
362 {
363  if (khz == 0) {
364  LOG_DEBUG("RCLK not supported");
365  return ERROR_FAIL;
366  }
367 
368  /* Calculate frequency divisor. */
369  if (khz > 2500)
370  *divisor = 0; /* Special case: 3 MHz */
371  else if (khz > 1700)
372  *divisor = 1; /* Special case: 2 MHz */
373  else {
374  *divisor = (2*3000 / khz + 1) / 2;
375  if (*divisor > 0x3FFF)
376  *divisor = 0x3FFF;
377  }
378  return ERROR_OK;
379 }
380 
382 {
383  if (bit >= 0 && bit < FT232R_BIT_COUNT)
384  return ft232r_bit_name_array[bit];
385  return "?";
386 }
387 
388 static int ft232r_bit_name_to_number(const char *name)
389 {
390  int i;
391  if (name[0] >= '0' && name[0] <= '9' && name[1] == '\0') {
392  i = atoi(name);
393  if (i >= 0 && i < FT232R_BIT_COUNT)
394  return i;
395  }
396  for (i = 0; i < FT232R_BIT_COUNT; i++)
397  if (strcasecmp(name, ft232r_bit_name_array[i]) == 0)
398  return i;
399  return -1;
400 }
401 
402 COMMAND_HANDLER(ft232r_handle_jtag_nums_command)
403 {
404  if (CMD_ARGC == 4) {
409  } else if (CMD_ARGC != 0)
411 
412  if (tck_gpio < 0)
414  if (tms_gpio < 0)
416  if (tdi_gpio < 0)
418  if (tdo_gpio < 0)
420 
422  "FT232R nums: TCK = %d %s, TMS = %d %s, TDI = %d %s, TDO = %d %s",
427 
428  return ERROR_OK;
429 }
430 
431 COMMAND_HANDLER(ft232r_handle_tck_num_command)
432 {
433  if (CMD_ARGC == 1)
435  else if (CMD_ARGC != 0)
437 
438  if (tck_gpio < 0)
440 
442  "FT232R num: TCK = %d %s", tck_gpio, ft232r_bit_number_to_name(tck_gpio));
443 
444  return ERROR_OK;
445 }
446 
447 COMMAND_HANDLER(ft232r_handle_tms_num_command)
448 {
449  if (CMD_ARGC == 1)
451  else if (CMD_ARGC != 0)
453 
454  if (tms_gpio < 0)
456 
458  "FT232R num: TMS = %d %s", tms_gpio, ft232r_bit_number_to_name(tms_gpio));
459 
460  return ERROR_OK;
461 }
462 
463 COMMAND_HANDLER(ft232r_handle_tdo_num_command)
464 {
465  if (CMD_ARGC == 1)
467  else if (CMD_ARGC != 0)
469 
470  if (tdo_gpio < 0)
472 
474  "FT232R num: TDO = %d %s", tdo_gpio, ft232r_bit_number_to_name(tdo_gpio));
475 
476  return ERROR_OK;
477 }
478 
479 COMMAND_HANDLER(ft232r_handle_tdi_num_command)
480 {
481  if (CMD_ARGC == 1)
483  else if (CMD_ARGC != 0)
485 
486  if (tdi_gpio < 0)
488 
490  "FT232R num: TDI = %d %s", tdi_gpio, ft232r_bit_number_to_name(tdi_gpio));
491 
492  return ERROR_OK;
493 }
494 
495 COMMAND_HANDLER(ft232r_handle_trst_num_command)
496 {
497  if (CMD_ARGC == 1)
499  else if (CMD_ARGC != 0)
501 
502  if (ntrst_gpio < 0)
504 
506  "FT232R num: TRST = %d %s", ntrst_gpio, ft232r_bit_number_to_name(ntrst_gpio));
507 
508  return ERROR_OK;
509 }
510 
511 COMMAND_HANDLER(ft232r_handle_srst_num_command)
512 {
513  if (CMD_ARGC == 1)
515  else if (CMD_ARGC != 0)
517 
518  if (nsysrst_gpio < 0)
520 
522  "FT232R num: SRST = %d %s", nsysrst_gpio, ft232r_bit_number_to_name(nsysrst_gpio));
523 
524  return ERROR_OK;
525 }
526 
527 COMMAND_HANDLER(ft232r_handle_restore_serial_command)
528 {
529  if (CMD_ARGC == 1)
531  else if (CMD_ARGC != 0)
533 
535  "FT232R restore serial: 0x%04X (%s)",
536  ft232r_restore_bitmode, ft232r_restore_bitmode == 0xFFFF ? "disabled" : "enabled");
537 
538  return ERROR_OK;
539 }
540 
541 static const struct command_registration ft232r_subcommand_handlers[] = {
542  {
543  .name = "jtag_nums",
544  .handler = ft232r_handle_jtag_nums_command,
545  .mode = COMMAND_CONFIG,
546  .help = "gpio numbers for tck, tms, tdi, tdo. (in that order)",
547  .usage = "<0-7|TXD-RI> <0-7|TXD-RI> <0-7|TXD-RI> <0-7|TXD-RI>",
548  },
549  {
550  .name = "tck_num",
551  .handler = ft232r_handle_tck_num_command,
552  .mode = COMMAND_CONFIG,
553  .help = "gpio number for tck.",
554  .usage = "<0-7|TXD|RXD|RTS|CTS|DTR|DSR|DCD|RI>",
555  },
556  {
557  .name = "tms_num",
558  .handler = ft232r_handle_tms_num_command,
559  .mode = COMMAND_CONFIG,
560  .help = "gpio number for tms.",
561  .usage = "<0-7|TXD|RXD|RTS|CTS|DTR|DSR|DCD|RI>",
562  },
563  {
564  .name = "tdo_num",
565  .handler = ft232r_handle_tdo_num_command,
566  .mode = COMMAND_CONFIG,
567  .help = "gpio number for tdo.",
568  .usage = "<0-7|TXD|RXD|RTS|CTS|DTR|DSR|DCD|RI>",
569  },
570  {
571  .name = "tdi_num",
572  .handler = ft232r_handle_tdi_num_command,
573  .mode = COMMAND_CONFIG,
574  .help = "gpio number for tdi.",
575  .usage = "<0-7|TXD|RXD|RTS|CTS|DTR|DSR|DCD|RI>",
576  },
577  {
578  .name = "srst_num",
579  .handler = ft232r_handle_srst_num_command,
580  .mode = COMMAND_CONFIG,
581  .help = "gpio number for srst.",
582  .usage = "<0-7|TXD|RXD|RTS|CTS|DTR|DSR|DCD|RI>",
583  },
584  {
585  .name = "trst_num",
586  .handler = ft232r_handle_trst_num_command,
587  .mode = COMMAND_CONFIG,
588  .help = "gpio number for trst.",
589  .usage = "<0-7|TXD|RXD|RTS|CTS|DTR|DSR|DCD|RI>",
590  },
591  {
592  .name = "restore_serial",
593  .handler = ft232r_handle_restore_serial_command,
594  .mode = COMMAND_CONFIG,
595  .help = "bitmode control word that restores serial port.",
596  .usage = "bitmode_control_word",
597  },
599 };
600 
601 static const struct command_registration ft232r_command_handlers[] = {
602  {
603  .name = "ft232r",
604  .mode = COMMAND_ANY,
605  .help = "perform ft232r management",
607  .usage = "",
608  },
610 };
611 
612 /*
613  * Synchronous bitbang protocol implementation.
614  */
615 
617 {
620  else {
621  LOG_ERROR("BUG: %i is not a valid end state", state);
622  exit(-1);
623  }
624 }
625 
626 static void syncbb_state_move(int skip)
627 {
628  int i = 0, tms = 0;
629  uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
631 
632  for (i = skip; i < tms_count; i++) {
633  tms = (tms_scan >> i) & 1;
634  ft232r_write(0, tms, 0);
635  ft232r_write(1, tms, 0);
636  }
637  ft232r_write(0, tms, 0);
638 
640 }
641 
647 {
648  unsigned int num_bits = cmd->cmd.tms->num_bits;
649  const uint8_t *bits = cmd->cmd.tms->bits;
650 
651  LOG_DEBUG_IO("TMS: %u bits", num_bits);
652 
653  int tms = 0;
654  for (unsigned int i = 0; i < num_bits; i++) {
655  tms = ((bits[i/8] >> (i % 8)) & 1);
656  ft232r_write(0, tms, 0);
657  ft232r_write(1, tms, 0);
658  }
659  ft232r_write(0, tms, 0);
660 
661  return ERROR_OK;
662 }
663 
665 {
666  unsigned int num_states = cmd->num_states;
667  int state_count;
668  int tms = 0;
669 
670  state_count = 0;
671  while (num_states) {
672  if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count]) {
673  tms = 0;
674  } else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count]) {
675  tms = 1;
676  } else {
677  LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
679  tap_state_name(cmd->path[state_count]));
680  exit(-1);
681  }
682 
683  ft232r_write(0, tms, 0);
684  ft232r_write(1, tms, 0);
685 
686  tap_set_state(cmd->path[state_count]);
687  state_count++;
688  num_states--;
689  }
690 
691  ft232r_write(0, tms, 0);
692 
694 }
695 
696 static void syncbb_runtest(unsigned int num_cycles)
697 {
698 
699  enum tap_state saved_end_state = tap_get_end_state();
700 
701  /* only do a state_move when we're not already in IDLE */
702  if (tap_get_state() != TAP_IDLE) {
705  }
706 
707  /* execute num_cycles */
708  for (unsigned int i = 0; i < num_cycles; i++) {
709  ft232r_write(0, 0, 0);
710  ft232r_write(1, 0, 0);
711  }
712  ft232r_write(0, 0, 0);
713 
714  /* finish in end_state */
715  syncbb_end_state(saved_end_state);
716  if (tap_get_state() != tap_get_end_state())
718 }
719 
728 static void syncbb_stableclocks(unsigned int num_cycles)
729 {
730  int tms = (tap_get_state() == TAP_RESET ? 1 : 0);
731 
732  /* send num_cycles clocks onto the cable */
733  for (unsigned int i = 0; i < num_cycles; i++) {
734  ft232r_write(1, tms, 0);
735  ft232r_write(0, tms, 0);
736  }
737 }
738 
739 static void syncbb_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
740 {
741  enum tap_state saved_end_state = tap_get_end_state();
742  int bit_cnt, bit0_index;
743 
744  if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) || (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) {
745  if (ir_scan)
747  else
749 
751  syncbb_end_state(saved_end_state);
752  }
753 
754  bit0_index = ft232r_output_len;
755  for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++) {
756  int tms = (bit_cnt == scan_size-1) ? 1 : 0;
757  int tdi;
758  int bytec = bit_cnt/8;
759  int bcval = 1 << (bit_cnt % 8);
760 
761  /* if we're just reading the scan, but don't care about the output
762  * default to outputting 'low', this also makes valgrind traces more readable,
763  * as it removes the dependency on an uninitialised value
764  */
765  tdi = 0;
766  if ((type != SCAN_IN) && (buffer[bytec] & bcval))
767  tdi = 1;
768 
769  ft232r_write(0, tms, tdi);
770  ft232r_write(1, tms, tdi);
771  }
772 
773  if (tap_get_state() != tap_get_end_state()) {
774  /* we *KNOW* the above loop transitioned out of
775  * the shift state, so we skip the first state
776  * and move directly to the end state.
777  */
779  }
781 
782  if (type != SCAN_OUT)
783  for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++) {
784  int bytec = bit_cnt/8;
785  int bcval = 1 << (bit_cnt % 8);
786  int val = ft232r_output[bit0_index + bit_cnt*2 + 1];
787 
788  if (val & (1<<tdo_gpio))
789  buffer[bytec] |= bcval;
790  else
791  buffer[bytec] &= ~bcval;
792  }
793 }
794 
795 static int syncbb_execute_queue(struct jtag_command *cmd_queue)
796 {
797  struct jtag_command *cmd = cmd_queue; /* currently processed command */
798  int scan_size;
799  enum scan_type type;
800  uint8_t *buffer;
801  int retval;
802 
803  /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
804  * that wasn't handled by a caller-provided error handler
805  */
806  retval = ERROR_OK;
807 
808 /* ft232r_blink(1);*/
809 
810  while (cmd) {
811  switch (cmd->type) {
812  case JTAG_RESET:
813  LOG_DEBUG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
814 
815  if (cmd->cmd.reset->trst == 1 ||
816  (cmd->cmd.reset->srst &&
819 
820  ft232r_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
821  break;
822 
823  case JTAG_RUNTEST:
824  LOG_DEBUG_IO("runtest %u cycles, end in %s", cmd->cmd.runtest->num_cycles,
825  tap_state_name(cmd->cmd.runtest->end_state));
826 
827  syncbb_end_state(cmd->cmd.runtest->end_state);
828  syncbb_runtest(cmd->cmd.runtest->num_cycles);
829  break;
830 
831  case JTAG_STABLECLOCKS:
832  /* this is only allowed while in a stable state. A check for a stable
833  * state was done in jtag_add_clocks()
834  */
835  syncbb_stableclocks(cmd->cmd.stableclocks->num_cycles);
836  break;
837 
838  case JTAG_TLR_RESET: /* renamed from JTAG_STATEMOVE */
839  LOG_DEBUG_IO("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state));
840 
841  syncbb_end_state(cmd->cmd.statemove->end_state);
843  break;
844 
845  case JTAG_PATHMOVE:
846  LOG_DEBUG_IO("pathmove: %u states, end in %s", cmd->cmd.pathmove->num_states,
847  tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
848 
849  syncbb_path_move(cmd->cmd.pathmove);
850  break;
851 
852  case JTAG_SCAN:
853  LOG_DEBUG_IO("%s scan end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR",
854  tap_state_name(cmd->cmd.scan->end_state));
855 
856  syncbb_end_state(cmd->cmd.scan->end_state);
857  scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
858  type = jtag_scan_type(cmd->cmd.scan);
859  syncbb_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
860  if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
861  retval = ERROR_JTAG_QUEUE_FAILED;
862  free(buffer);
863  break;
864 
865  case JTAG_SLEEP:
866  LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
867 
868  jtag_sleep(cmd->cmd.sleep->us);
869  break;
870 
871  case JTAG_TMS:
872  retval = syncbb_execute_tms(cmd);
873  break;
874  default:
875  LOG_ERROR("BUG: unknown JTAG command type encountered");
876  exit(-1);
877  }
878  if (ft232r_output_len > 0)
880  cmd = cmd->next;
881  }
882 /* ft232r_blink(0);*/
883 
884  return retval;
885 }
886 
887 static struct jtag_interface ft232r_interface = {
889  .execute_queue = syncbb_execute_queue,
890 };
891 
893  .name = "ft232r",
894  .transport_ids = TRANSPORT_JTAG,
895  .transport_preferred_id = TRANSPORT_JTAG,
896  .commands = ft232r_command_handlers,
897 
898  .init = ft232r_init,
899  .quit = ft232r_quit,
900  .speed = ft232r_speed,
901  .khz = ft232r_khz,
902  .speed_div = ft232r_speed_div,
903 
904  .jtag_ops = &ft232r_interface,
905 };
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Definition: adapter.c:309
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
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
int jtag_build_buffer(const struct scan_command *cmd, uint8_t **buffer)
Definition: commands.c:192
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
@ 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
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint8_t type
Definition: esp_usb_jtag.c:0
COMMAND_HANDLER(ft232r_handle_jtag_nums_command)
Definition: ft232r.c:402
static uint16_t ft232r_vids[]
Definition: ft232r.c:62
static int tdi_gpio
Definition: ft232r.c:86
static void syncbb_stableclocks(unsigned int num_cycles)
Function syncbb_stableclocks issues a number of clock cycles while staying in a stable state.
Definition: ft232r.c:728
static void syncbb_end_state(enum tap_state state)
Definition: ft232r.c:616
#define FT232R_BUF_SIZE_EXTRA
Definition: ft232r.c:59
struct adapter_driver ft232r_adapter_driver
Definition: ft232r.c:892
static int ntrst_gpio
Definition: ft232r.c:89
static char * ft232r_bit_number_to_name(int bit)
Definition: ft232r.c:381
static void syncbb_path_move(struct pathmove_command *cmd)
Definition: ft232r.c:664
static void syncbb_runtest(unsigned int num_cycles)
Definition: ft232r.c:696
static struct libusb_device_handle * adapter
Definition: ft232r.c:65
static int ft232r_bit_name_to_number(const char *name)
Definition: ft232r.c:388
#define OUT_EP
Definition: ft232r.c:40
#define IN_EP
Definition: ft232r.c:39
static uint16_t ft232r_restore_bitmode
0xFFFF disables restore by default, after exit serial port will not work.
Definition: ft232r.c:95
static struct jtag_interface ft232r_interface
Definition: ft232r.c:887
static const struct command_registration ft232r_subcommand_handlers[]
Definition: ft232r.c:541
static char * ft232r_bit_name_array[FT232R_BIT_COUNT]
Definition: ft232r.c:74
static uint8_t * ft232r_output
Definition: ft232r.c:67
#define FT232R_BIT_COUNT
FT232R GPIO bit number to RS232 name.
Definition: ft232r.c:73
static int ft232r_init(void)
Definition: ft232r.c:247
static int tdo_gpio
Definition: ft232r.c:87
#define SIO_SET_BAUD_RATE
Definition: ft232r.c:46
static int ft232r_khz(int khz, int *divisor)
Definition: ft232r.c:361
static int ft232r_speed_div(int divisor, int *khz)
Definition: ft232r.c:349
static int ft232r_speed(int divisor)
Definition: ft232r.c:231
#define SIO_SET_LATENCY_TIMER
Definition: ft232r.c:51
static const struct command_registration ft232r_command_handlers[]
Definition: ft232r.c:601
static int tck_gpio
Definition: ft232r.c:85
static int tms_gpio
Definition: ft232r.c:88
static int ft232r_quit(void)
Definition: ft232r.c:325
static int syncbb_execute_queue(struct jtag_command *cmd_queue)
Definition: ft232r.c:795
static int nsysrst_gpio
Definition: ft232r.c:90
static size_t ft232r_output_len
Definition: ft232r.c:68
static void ft232r_write(int tck, int tms, int tdi)
Add one TCK/TMS/TDI sample to send buffer.
Definition: ft232r.c:180
static int syncbb_execute_tms(struct jtag_command *cmd)
Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG (or SWD) state machine.
Definition: ft232r.c:646
static void syncbb_state_move(int skip)
Definition: ft232r.c:626
#define SIO_SET_BITMODE
Definition: ft232r.c:53
static void syncbb_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
Definition: ft232r.c:739
static void ft232r_increase_buf_size(size_t new_buf_size)
Definition: ft232r.c:164
static int ft232r_send_recv(void)
Perform sync bitbang output/input transaction.
Definition: ft232r.c:103
#define SIO_RESET
Definition: ft232r.c:43
static size_t ft232r_buf_size
Definition: ft232r.c:91
static uint16_t ft232r_pids[]
Definition: ft232r.c:63
static void ft232r_reset(int trst, int srst)
Control /TRST and /SYSRST pins.
Definition: ft232r.c:204
enum tap_state tap_get_end_state(void)
For more information,.
Definition: interface.c:56
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
void tap_set_end_state(enum tap_state new_end_state)
This function sets the state of an "end state follower" which tracks the state that any cable driver ...
Definition: interface.c:48
enum tap_state tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
Definition: interface.c:37
bool tap_is_state_stable(enum tap_state astate)
Function tap_is_state_stable returns true if the astate is stable.
Definition: interface.c:200
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
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1742
#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
#define ERROR_JTAG_QUEUE_FAILED
Definition: jtag.h:556
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:552
@ RESET_SRST_PULLS_TRST
Definition: jtag.h:220
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)
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:116
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
static uint32_t bit(uint32_t value, unsigned int b)
Definition: opcodes.h:39
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
Represents a driver for a debugging interface.
Definition: interface.h:208
const char *const name
The name of the interface driver.
Definition: interface.h:210
const char * name
Definition: command.h:239
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
Represents a driver for a debugging interface.
Definition: interface.h:183
unsigned int supported
Bit vector listing capabilities exposed by this driver.
Definition: interface.h:187
#define TRANSPORT_JTAG
Definition: transport.h:19
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t state[4]
Definition: vdebug.c:21