OpenOCD
ftdi.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /**************************************************************************
4 * Copyright (C) 2012 by Andreas Fritiofson *
5 * andreas.fritiofson@gmail.com *
6 ***************************************************************************/
7 
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59 
60 /* project specific includes */
61 #include <jtag/adapter.h>
62 #include <jtag/interface.h>
63 #include <jtag/swd.h>
64 #include <transport/transport.h>
65 #include <helper/time_support.h>
66 #include <helper/log.h>
67 #include <helper/nvp.h>
68 
69 #if IS_CYGWIN == 1
70 #include <windows.h>
71 #endif
72 
73 #include <assert.h>
74 
75 /* FTDI access library includes */
76 #include "mpsse.h"
77 
78 #if BUILD_FTDI_CJTAG == 1
79 #define DO_CLOCK_DATA clock_data
80 #define DO_CLOCK_TMS_CS clock_tms_cs
81 #define DO_CLOCK_TMS_CS_OUT clock_tms_cs_out
82 #else
83 #define DO_CLOCK_DATA mpsse_clock_data
84 #define DO_CLOCK_TMS_CS mpsse_clock_tms_cs
85 #define DO_CLOCK_TMS_CS_OUT mpsse_clock_tms_cs_out
86 #endif
87 
88 #define JTAG_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
89 #define JTAG_MODE_ALT (LSB_FIRST | NEG_EDGE_IN | NEG_EDGE_OUT)
90 #define SWD_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
91 
92 static char *ftdi_device_desc;
93 static uint8_t ftdi_channel;
94 static uint8_t ftdi_jtag_mode = JTAG_MODE;
95 
96 static bool swd_mode;
97 
98 #if BUILD_FTDI_CJTAG == 1
99 #define ESCAPE_SEQ_OAC_BIT2 28
100 
101 static void cjtag_reset_online_activate(void);
102 
103 /*
104  The cJTAG 2-wire OScan1 protocol, in lieu of 4-wire JTAG, is a configuration option
105  for some SoCs. An FTDI-based adapter that can be configured to appropriately drive
106  the bidirectional pin TMSC is able to drive OScan1 protocol. For example, an Olimex
107  ARM-USB-TINY-H with the ARM-JTAG-SWD adapter, connected to a cJTAG-enabled
108  target board is such a topology. A TCK cycle with TMS=1/TDI=N translates to a TMSC
109  output of N, and a TCK cycle with TMS=0 translates to a TMSC input from the target back
110  to the adapter/probe. The OScan1 protocol uses 3 TCK cycles to generate the data flow
111  that is equivalent to that of a single TCK cycle in 4-wire JTAG. The OScan1-related
112  code in this module translates IR/DR scan commanads and JTAG state traversal commands
113  to the two-wire clocking and signaling of OScan1 protocol, if placed into OScan1 mode
114  during initialization.
115 */
116 static void oscan1_mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
117  unsigned int in_offset, unsigned int length, uint8_t mode);
118 static void oscan1_mpsse_clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
119  unsigned int in_offset, unsigned int length, bool tdi, uint8_t mode);
120 static void oscan1_mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset,
121  unsigned int length, bool tdi, uint8_t mode);
122 
123 static bool oscan1_mode;
124 
125 /*
126  The cJTAG 4-wire JScan3 allows to use standard JTAG protocol with cJTAG hardware
127 */
128 static bool jscan3_mode;
129 #endif
130 
131 #define MAX_USB_IDS 8
132 /* vid = pid = 0 marks the end of the list */
133 static uint16_t ftdi_vid[MAX_USB_IDS + 1] = { 0 };
134 static uint16_t ftdi_pid[MAX_USB_IDS + 1] = { 0 };
135 
136 static struct mpsse_ctx *mpsse_ctx;
137 
138 struct signal {
139  const char *name;
140  uint16_t data_mask;
141  uint16_t input_mask;
142  uint16_t oe_mask;
145  bool invert_oe;
146  struct signal *next;
147 };
148 
149 static struct signal *signals;
150 
151 /* FIXME: Where to store per-instance data? We need an SWD context. */
152 static struct swd_cmd_queue_entry {
153  uint8_t cmd;
154  uint32_t *dst;
155  uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)];
157 static size_t swd_cmd_queue_length;
158 static size_t swd_cmd_queue_alloced;
159 static int queued_retval;
160 static int freq;
161 
162 static uint16_t output;
163 static uint16_t direction;
164 static uint16_t jtag_output_init;
165 static uint16_t jtag_direction_init;
166 
167 static int ftdi_swd_switch_seq(enum swd_special_seq seq);
168 
169 static struct signal *find_signal_by_name(const char *name)
170 {
171  for (struct signal *sig = signals; sig; sig = sig->next) {
172  if (strcmp(name, sig->name) == 0)
173  return sig;
174  }
175  return NULL;
176 }
177 
178 static struct signal *create_signal(const char *name)
179 {
180  struct signal **psig = &signals;
181  while (*psig)
182  psig = &(*psig)->next;
183 
184  *psig = calloc(1, sizeof(**psig));
185  if (!*psig)
186  return NULL;
187 
188  (*psig)->name = strdup(name);
189  if (!(*psig)->name) {
190  free(*psig);
191  *psig = NULL;
192  }
193  return *psig;
194 }
195 
196 static int ftdi_set_signal(const struct signal *s, char value)
197 {
198  bool data;
199  bool oe;
200 
201  if (s->data_mask == 0 && s->oe_mask == 0) {
202  LOG_ERROR("interface doesn't provide signal '%s'", s->name);
203  return ERROR_FAIL;
204  }
205  switch (value) {
206  case '0':
207  data = s->invert_data;
208  oe = !s->invert_oe;
209  break;
210  case '1':
211  if (s->data_mask == 0) {
212  LOG_ERROR("interface can't drive '%s' high", s->name);
213  return ERROR_FAIL;
214  }
215  data = !s->invert_data;
216  oe = !s->invert_oe;
217  break;
218  case 'z':
219  case 'Z':
220  if (s->oe_mask == 0) {
221  LOG_ERROR("interface can't tri-state '%s'", s->name);
222  return ERROR_FAIL;
223  }
224  data = s->invert_data;
225  oe = s->invert_oe;
226  break;
227  default:
228  LOG_ERROR("invalid signal level specifier \'%c\'(0x%02x)", value, value);
229  return ERROR_FAIL;
230  }
231 
232  uint16_t old_output = output;
233  uint16_t old_direction = direction;
234 
235  output = data ? output | s->data_mask : output & ~s->data_mask;
236  if (s->oe_mask == s->data_mask)
237  direction = oe ? direction | s->oe_mask : direction & ~s->oe_mask;
238  else
239  output = oe ? output | s->oe_mask : output & ~s->oe_mask;
240 
241  if ((output & 0xff) != (old_output & 0xff) || (direction & 0xff) != (old_direction & 0xff))
243  if ((output >> 8 != old_output >> 8) || (direction >> 8 != old_direction >> 8))
245 
246  return ERROR_OK;
247 }
248 
249 static int ftdi_get_signal(const struct signal *s, uint16_t *value_out)
250 {
251  uint8_t data_low = 0;
252  uint8_t data_high = 0;
253 
254  if (s->input_mask == 0) {
255  LOG_ERROR("interface doesn't provide signal '%s'", s->name);
256  return ERROR_FAIL;
257  }
258 
259  if (s->input_mask & 0xff)
261  if (s->input_mask >> 8)
263 
265 
266  *value_out = (((uint16_t)data_high) << 8) | data_low;
267 
268  if (s->invert_input)
269  *value_out = ~(*value_out);
270 
271  *value_out &= s->input_mask;
272 
273  return ERROR_OK;
274 }
275 
276 #if BUILD_FTDI_CJTAG == 1
277 static void clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
278  unsigned int in_offset, unsigned int length, uint8_t mode)
279 {
280  if (oscan1_mode)
281  oscan1_mpsse_clock_data(ctx, out, out_offset, in, in_offset, length, mode);
282  else
283  mpsse_clock_data(ctx, out, out_offset, in, in_offset, length, mode);
284 }
285 
286 static void clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
287  unsigned int in_offset, unsigned int length, bool tdi, uint8_t mode)
288 {
289  if (oscan1_mode)
290  oscan1_mpsse_clock_tms_cs(ctx, out, out_offset, in, in_offset, length, tdi, mode);
291  else
292  mpsse_clock_tms_cs(ctx, out, out_offset, in, in_offset, length, tdi, mode);
293 }
294 
295 static void clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset,
296  unsigned int length, bool tdi, uint8_t mode)
297 {
298  if (oscan1_mode)
299  oscan1_mpsse_clock_tms_cs_out(ctx, out, out_offset, length, tdi, mode);
300  else
301  mpsse_clock_tms_cs_out(ctx, out, out_offset, length, tdi, mode);
302 }
303 #endif
304 
313 static void move_to_state(enum tap_state goal_state)
314 {
315  enum tap_state start_state = tap_get_state();
316 
317  /* goal_state is 1/2 of a tuple/pair of states which allow convenient
318  lookup of the required TMS pattern to move to this state from the
319  start state.
320  */
321 
322  /* do the 2 lookups */
323  uint8_t tms_bits = tap_get_tms_path(start_state, goal_state);
324  int tms_count = tap_get_tms_path_len(start_state, goal_state);
325  assert(tms_count <= 8);
326 
327  LOG_DEBUG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
328 
329  /* Track state transitions step by step */
330  for (int i = 0; i < tms_count; i++)
331  tap_set_state(tap_state_transition(tap_get_state(), (tms_bits >> i) & 1));
332 
334  &tms_bits,
335  0,
336  tms_count,
337  false,
339 }
340 
341 static int ftdi_speed(int speed)
342 {
343  int retval;
344  retval = mpsse_set_frequency(mpsse_ctx, speed);
345 
346  if (retval < 0) {
347  LOG_ERROR("couldn't set FTDI TCK speed");
348  return retval;
349  }
350 
351  if (!swd_mode && speed >= 10000000 && ftdi_jtag_mode != JTAG_MODE_ALT)
352  LOG_INFO("ftdi: if you experience problems at higher adapter clocks, try "
353  "the command \"ftdi tdo_sample_edge falling\"");
354  return ERROR_OK;
355 }
356 
357 static int ftdi_speed_div(int speed, int *khz)
358 {
359  *khz = speed / 1000;
360  return ERROR_OK;
361 }
362 
363 static int ftdi_khz(int khz, int *jtag_speed)
364 {
365  if (khz == 0 && !mpsse_is_high_speed(mpsse_ctx)) {
366  LOG_DEBUG("RCLK not supported");
367  return ERROR_FAIL;
368  }
369 
370  *jtag_speed = khz * 1000;
371  return ERROR_OK;
372 }
373 
374 static void ftdi_end_state(enum tap_state state)
375 {
378  else {
379  LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
380  exit(-1);
381  }
382 }
383 
385 {
386  uint8_t zero = 0;
387 
388  LOG_DEBUG_IO("runtest %u cycles, end in %s",
389  cmd->cmd.runtest->num_cycles,
390  tap_state_name(cmd->cmd.runtest->end_state));
391 
392  if (tap_get_state() != TAP_IDLE)
394 
395  /* TODO: Reuse ftdi_execute_stableclocks */
396  unsigned int i = cmd->cmd.runtest->num_cycles;
397  while (i > 0) {
398  /* there are no state transitions in this code, so omit state tracking */
399  unsigned int this_len = i > 7 ? 7 : i;
400  DO_CLOCK_TMS_CS_OUT(mpsse_ctx, &zero, 0, this_len, false, ftdi_jtag_mode);
401  i -= this_len;
402  }
403 
404  ftdi_end_state(cmd->cmd.runtest->end_state);
405 
406  if (tap_get_state() != tap_get_end_state())
408 
409  LOG_DEBUG_IO("runtest: %u, end in %s",
410  cmd->cmd.runtest->num_cycles,
412 }
413 
415 {
416  LOG_DEBUG_IO("statemove end in %s",
417  tap_state_name(cmd->cmd.statemove->end_state));
418 
419  ftdi_end_state(cmd->cmd.statemove->end_state);
420 
421  /* shortest-path move to desired end state */
424 }
425 
430 static void ftdi_execute_tms(struct jtag_command *cmd)
431 {
432  LOG_DEBUG_IO("TMS: %u bits", cmd->cmd.tms->num_bits);
433 
434  /* TODO: Missing tap state tracking, also missing from ft2232.c! */
436  cmd->cmd.tms->bits,
437  0,
438  cmd->cmd.tms->num_bits,
439  false,
441 }
442 
444 {
445  enum tap_state *path = cmd->cmd.pathmove->path;
446  unsigned int num_states = cmd->cmd.pathmove->num_states;
447 
448  LOG_DEBUG_IO("pathmove: %u states, current: %s end: %s", num_states,
450  tap_state_name(path[num_states-1]));
451 
452  int state_count = 0;
453  unsigned int bit_count = 0;
454  uint8_t tms_byte = 0;
455 
456  LOG_DEBUG_IO("-");
457 
458  /* this loop verifies that the path is legal and logs each state in the path */
459  while (num_states--) {
460 
461  /* either TMS=0 or TMS=1 must work ... */
462  if (tap_state_transition(tap_get_state(), false)
463  == path[state_count])
464  buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
465  else if (tap_state_transition(tap_get_state(), true)
466  == path[state_count]) {
467  buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
468 
469  /* ... or else the caller goofed BADLY */
470  } else {
471  LOG_ERROR("BUG: %s -> %s isn't a valid "
472  "TAP state transition",
474  tap_state_name(path[state_count]));
475  exit(-1);
476  }
477 
478  tap_set_state(path[state_count]);
479  state_count++;
480 
481  if (bit_count == 7 || num_states == 0) {
483  &tms_byte,
484  0,
485  bit_count,
486  false,
488  bit_count = 0;
489  }
490  }
492 }
493 
494 static void ftdi_execute_scan(struct jtag_command *cmd)
495 {
496  LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
497  jtag_scan_type(cmd->cmd.scan));
498 
499  /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
500  while (cmd->cmd.scan->num_fields > 0
501  && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
502  cmd->cmd.scan->num_fields--;
503  LOG_DEBUG_IO("discarding trailing empty field");
504  }
505 
506  if (!cmd->cmd.scan->num_fields) {
507  LOG_DEBUG_IO("empty scan, doing nothing");
508  return;
509  }
510 
511  if (cmd->cmd.scan->ir_scan) {
512  if (tap_get_state() != TAP_IRSHIFT)
514  } else {
515  if (tap_get_state() != TAP_DRSHIFT)
517  }
518 
519  ftdi_end_state(cmd->cmd.scan->end_state);
520 
521  struct scan_field *field = cmd->cmd.scan->fields;
522  unsigned int scan_size = 0;
523 
524  for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
525  scan_size += field->num_bits;
526  LOG_DEBUG_IO("%s%s field %u/%u %u bits",
527  field->in_value ? "in" : "",
528  field->out_value ? "out" : "",
529  i,
530  cmd->cmd.scan->num_fields,
531  field->num_bits);
532 
533  if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
534  /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
535  * movement. This last field can't have length zero, it was checked above. */
537  field->out_value,
538  0,
539  field->in_value,
540  0,
541  field->num_bits - 1,
543  uint8_t last_bit = 0;
544  if (field->out_value)
545  bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
546 
547  /* If endstate is TAP_IDLE, clock out 1-1-0 (->EXIT1 ->UPDATE ->IDLE)
548  * Otherwise, clock out 1-0 (->EXIT1 ->PAUSE)
549  */
550  uint8_t tms_bits = 0x03;
552  &tms_bits,
553  0,
554  field->in_value,
555  field->num_bits - 1,
556  1,
557  last_bit,
560  if (tap_get_end_state() == TAP_IDLE) {
562  &tms_bits,
563  1,
564  2,
565  last_bit,
569  } else {
571  &tms_bits,
572  2,
573  1,
574  last_bit,
577  }
578  } else
580  field->out_value,
581  0,
582  field->in_value,
583  0,
584  field->num_bits,
586  }
587 
588  if (tap_get_state() != tap_get_end_state())
590 
591  LOG_DEBUG_IO("%s scan, %i bits, end in %s",
592  (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
594 }
595 
596 static int ftdi_reset(int trst, int srst)
597 {
598  struct signal *sig_ntrst = find_signal_by_name("nTRST");
599  struct signal *sig_nsrst = find_signal_by_name("nSRST");
600 
601  LOG_DEBUG_IO("reset trst: %i srst %i", trst, srst);
602 
603  if (!swd_mode) {
604  if (trst == 1) {
605  if (sig_ntrst)
606  ftdi_set_signal(sig_ntrst, '0');
607  else
608  LOG_ERROR("Can't assert TRST: nTRST signal is not defined");
609  } else if (sig_ntrst && jtag_get_reset_config() & RESET_HAS_TRST &&
610  trst == 0) {
612  ftdi_set_signal(sig_ntrst, 'z');
613  else
614  ftdi_set_signal(sig_ntrst, '1');
615  }
616  }
617 
618  if (srst == 1) {
619  if (sig_nsrst)
620  ftdi_set_signal(sig_nsrst, '0');
621  else
622  LOG_ERROR("Can't assert SRST: nSRST signal is not defined");
623  } else if (sig_nsrst && jtag_get_reset_config() & RESET_HAS_SRST &&
624  srst == 0) {
626  ftdi_set_signal(sig_nsrst, '1');
627  else
628  ftdi_set_signal(sig_nsrst, 'z');
629  }
630 
631  return mpsse_flush(mpsse_ctx);
632 }
633 
634 static void ftdi_execute_sleep(struct jtag_command *cmd)
635 {
636  LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
637 
639  jtag_sleep(cmd->cmd.sleep->us);
640  LOG_DEBUG_IO("sleep %" PRIu32 " usec while in %s",
641  cmd->cmd.sleep->us,
643 }
644 
646 {
647  /* this is only allowed while in a stable state. A check for a stable
648  * state was done in jtag_add_clocks()
649  */
650  unsigned int num_cycles = cmd->cmd.stableclocks->num_cycles;
651 
652  /* 7 bits of either ones or zeros. */
653  uint8_t tms = tap_get_state() == TAP_RESET ? 0x7f : 0x00;
654 
655  /* TODO: Use mpsse_clock_data with in=out=0 for this, if TMS can be set to
656  * the correct level and remain there during the scan */
657  while (num_cycles > 0) {
658  /* there are no state transitions in this code, so omit state tracking */
659  unsigned int this_len = num_cycles > 7 ? 7 : num_cycles;
660  DO_CLOCK_TMS_CS_OUT(mpsse_ctx, &tms, 0, this_len, false, ftdi_jtag_mode);
661  num_cycles -= this_len;
662  }
663 
664  LOG_DEBUG_IO("clocks %u while in %s",
665  cmd->cmd.stableclocks->num_cycles,
667 }
668 
670 {
671  switch (cmd->type) {
672 #if BUILD_FTDI_CJTAG == 1
673  case JTAG_RESET:
674  if (cmd->cmd.reset->trst)
675  cjtag_reset_online_activate(); /* put the target (back) into selected cJTAG mode */
676  break;
677 #endif
678  case JTAG_RUNTEST:
680  break;
681  case JTAG_TLR_RESET:
682 #if BUILD_FTDI_CJTAG == 1
683  cjtag_reset_online_activate(); /* put the target (back) into selected cJTAG mode */
684 #endif
686  break;
687  case JTAG_PATHMOVE:
689  break;
690  case JTAG_SCAN:
692  break;
693  case JTAG_SLEEP:
695  break;
696  case JTAG_STABLECLOCKS:
698  break;
699  case JTAG_TMS:
701  break;
702  default:
703  LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
704  break;
705  }
706 }
707 
708 static int ftdi_execute_queue(struct jtag_command *cmd_queue)
709 {
710  /* blink, if the current layout has that feature */
711  struct signal *led = find_signal_by_name("LED");
712  if (led)
713  ftdi_set_signal(led, '1');
714 
715  for (struct jtag_command *cmd = cmd_queue; cmd; cmd = cmd->next) {
716  /* fill the write buffer with the desired command */
718  }
719 
720  if (led)
721  ftdi_set_signal(led, '0');
722 
723  int retval = mpsse_flush(mpsse_ctx);
724  if (retval != ERROR_OK)
725  LOG_ERROR("error while flushing MPSSE queue: %d", retval);
726 
727  return retval;
728 }
729 
730 static int ftdi_initialize(void)
731 {
733  LOG_DEBUG("ftdi interface using 7 step jtag state transitions");
734  else
735  LOG_DEBUG("ftdi interface using shortest path jtag state transitions");
736 
737  if (!ftdi_vid[0] && !ftdi_pid[0]) {
738  LOG_ERROR("Please specify ftdi vid_pid");
739  return ERROR_JTAG_INIT_FAILED;
740  }
741 
744  if (!mpsse_ctx)
745  return ERROR_JTAG_INIT_FAILED;
746 
749 
750  if (swd_mode) {
751  struct signal *sig = find_signal_by_name("SWD_EN");
752  if (!sig) {
753  LOG_ERROR("SWD mode is active but SWD_EN signal is not defined");
754  return ERROR_JTAG_INIT_FAILED;
755  }
756  /* A dummy SWD_EN would have zero mask */
757  if (sig->data_mask)
758  ftdi_set_signal(sig, '1');
759 #if BUILD_FTDI_CJTAG == 1
760  } else if (oscan1_mode || jscan3_mode) {
761  struct signal *sig = find_signal_by_name("JTAG_SEL");
762  if (!sig) {
763  LOG_ERROR("A cJTAG mode is active but JTAG_SEL signal is not defined");
764  return ERROR_JTAG_INIT_FAILED;
765  }
766  /* A dummy JTAG_SEL would have zero mask */
767  if (sig->data_mask) {
768  ftdi_set_signal(sig, '0');
769  } else if (jscan3_mode) {
770  LOG_ERROR("In JScan3 mode JTAG_SEL signal cannot be dummy, data mask needed");
771  return ERROR_JTAG_INIT_FAILED;
772  }
773 #endif
774  }
775 
778 
780 
782 
783  return mpsse_flush(mpsse_ctx);
784 }
785 
786 static int ftdi_quit(void)
787 {
789 
790  struct signal *sig = signals;
791  while (sig) {
792  struct signal *next = sig->next;
793  free((void *)sig->name);
794  free(sig);
795  sig = next;
796  }
797 
798  free(ftdi_device_desc);
799 
800  free(swd_cmd_queue);
801 
802  return ERROR_OK;
803 }
804 
805 #if BUILD_FTDI_CJTAG == 1
806 static void oscan1_mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
807  unsigned int in_offset, unsigned int length, uint8_t mode)
808 {
809  static const uint8_t zero;
810  static const uint8_t one = 1;
811 
812  struct signal *tmsc_en = find_signal_by_name("TMSC_EN");
813 
814  LOG_DEBUG_IO("%sout %d bits", in ? "in" : "", length);
815 
816  for (unsigned int i = 0; i < length; i++) {
817  int bitnum;
818  uint8_t bit;
819 
820  /* OScan1 uses 3 separate clocks */
821 
822  /* drive TMSC to the *negation* of the desired TDI value */
823  bitnum = out_offset + i;
824  bit = out ? ((out[bitnum / 8] >> (bitnum % 8)) & 0x1) : 0;
825 
826  /* Try optimized case first: if desired TDI bit is 1, then we
827  can fuse what would otherwise be the first two MPSSE commands */
828  if (bit) {
829  const uint8_t tmsbits = 0x3; /* 1, 1 */
830  mpsse_clock_tms_cs_out(mpsse_ctx, &tmsbits, 0, 2, false, mode);
831  } else {
832  /* Can't fuse because TDI varies; less efficient */
833  mpsse_clock_tms_cs_out(mpsse_ctx, &one, 0, 1, bit ? 0 : 1, mode);
834 
835  /* drive TMSC to desired TMS value (always zero in this context) */
836  mpsse_clock_tms_cs_out(mpsse_ctx, &one, 0, 1, false, mode);
837  }
838 
839  if (tmsc_en)
840  ftdi_set_signal(tmsc_en, '0'); /* put TMSC in high impedance */
841 
842  /* drive another TCK without driving TMSC (TDO cycle) */
843  mpsse_clock_tms_cs(mpsse_ctx, &zero, 0, in, in_offset + i, 1, false, mode);
844 
845  if (tmsc_en)
846  ftdi_set_signal(tmsc_en, '1'); /* drive again TMSC */
847  }
848 }
849 
850 static void oscan1_mpsse_clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
851  unsigned int in_offset, unsigned int length, bool tdi, uint8_t mode)
852 {
853  static const uint8_t zero;
854  static const uint8_t one = 1;
855 
856  struct signal *tmsc_en = find_signal_by_name("TMSC_EN");
857 
858  LOG_DEBUG_IO("%sout %d bits, tdi=%d", in ? "in" : "", length, tdi);
859 
860  for (unsigned int i = 0; i < length; i++) {
861  int bitnum;
862  uint8_t tmsbit;
863  uint8_t tdibit;
864 
865  /* OScan1 uses 3 separate clocks */
866 
867  /* drive TMSC to the *negation* of the desired TDI value */
868  tdibit = tdi ? 0 : 1;
869 
870  /* drive TMSC to desired TMS value */
871  bitnum = out_offset + i;
872  tmsbit = ((out[bitnum / 8] >> (bitnum % 8)) & 0x1);
873 
874  if (tdibit == tmsbit) {
875  /* Can squash into a single MPSSE command */
876  const uint8_t tmsbits = 0x3;
877  mpsse_clock_tms_cs_out(mpsse_ctx, &tmsbits, 0, 2, tdibit, mode);
878  } else {
879  /* Unoptimized case, can't formulate with a single command */
880  mpsse_clock_tms_cs_out(mpsse_ctx, &one, 0, 1, tdibit, mode);
881  mpsse_clock_tms_cs_out(mpsse_ctx, &one, 0, 1, (tmsbit != 0), mode);
882  }
883 
884  if (tmsc_en)
885  ftdi_set_signal(tmsc_en, '0'); /* put TMSC in high impedance */
886 
887  /* drive another TCK without driving TMSC (TDO cycle) */
888  mpsse_clock_tms_cs(mpsse_ctx, &zero, 0, in, in_offset + i, 1, false, mode);
889 
890  if (tmsc_en)
891  ftdi_set_signal(tmsc_en, '1'); /* drive again TMSC */
892  }
893 }
894 
895 static void oscan1_mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset,
896  unsigned int length, bool tdi, uint8_t mode)
897 {
898  oscan1_mpsse_clock_tms_cs(ctx, out, out_offset, 0, 0, length, tdi, mode);
899 }
900 
901 static void cjtag_set_tck_tms_tdi(struct signal *tck, char tckvalue, struct signal *tms,
902  char tmsvalue, struct signal *tdi, char tdivalue)
903 {
904  ftdi_set_signal(tms, tmsvalue);
905  ftdi_set_signal(tdi, tdivalue);
906  ftdi_set_signal(tck, tckvalue);
907 }
908 
909 static void cjtag_reset_online_activate(void)
910 {
911  /* After TAP reset, the cJTAG-to-JTAG adapter is in offline and
912  non-activated state. Escape sequences are needed to bring the
913  TAP online and activated into the desired working mode. */
914 
915  struct signal *tck = find_signal_by_name("TCK");
916  struct signal *tdi = find_signal_by_name("TDI");
917  struct signal *tms = find_signal_by_name("TMS");
918  struct signal *tdo = find_signal_by_name("TDO");
919  struct signal *tmsc_en = find_signal_by_name("TMSC_EN");
920  uint16_t tdovalue;
921 
922  static struct {
923  int8_t tck;
924  int8_t tms;
925  int8_t tdi;
926  } sequence[] = {
927  /* TCK=0, TMS=1, TDI=0 (drive TMSC to 0 baseline) */
928  {'0', '1', '0'},
929 
930  /* Drive cJTAG escape sequence for TAP reset - 8 TMSC edges */
931  /* TCK=1, TMS=1, TDI=0 (rising edge of TCK with TMSC still 0) */
932  {'1', '1', '0'},
933  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
934  {'1', '1', '1'},
935  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
936  {'1', '1', '0'},
937  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
938  {'1', '1', '1'},
939  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
940  {'1', '1', '0'},
941  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
942  {'1', '1', '1'},
943  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
944  {'1', '1', '0'},
945  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
946  {'1', '1', '1'},
947  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
948  {'1', '1', '0'},
949  /* TCK=0, TMS=1, TDI=0 (falling edge TCK with TMSC still 0) */
950  {'0', '1', '0'},
951 
952  /* 3 TCK pulses for padding */
953  /* TCK=1, TMS=1, TDI=0 (drive rising TCK edge) */
954  {'1', '1', '0'},
955  /* TCK=0, TMS=1, TDI=0 (drive falling TCK edge) */
956  {'0', '1', '0'},
957  /* TCK=1, TMS=1, TDI=0 (drive rising TCK edge) */
958  {'1', '1', '0'},
959  /* TCK=0, TMS=1, TDI=0 (drive falling TCK edge) */
960  {'0', '1', '0'},
961  /* TCK=1, TMS=1, TDI=0 (drive rising TCK edge) */
962  {'1', '1', '0'},
963  /* TCK=0, TMS=1, TDI=0 (drive falling TCK edge) */
964  {'0', '1', '0'},
965 
966  /* Drive cJTAG escape sequence for SELECT */
967  /* TCK=1, TMS=1, TDI=0 (rising edge of TCK with TMSC still 0, TAP reset that was just setup occurs here too) */
968  {'1', '1', '0'},
969  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
970  {'1', '1', '1'},
971  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
972  {'1', '1', '0'},
973  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
974  {'1', '1', '1'},
975  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
976  {'1', '1', '0'},
977  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
978  {'1', '1', '1'},
979  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
980  {'1', '1', '0'},
981  /* TCK=0, TMS=1, TDI=0 (falling edge TCK with TMSC still 0) */
982  {'0', '1', '0'},
983 
984  /* Drive cJTAG escape sequence for OScan1 activation -- OAC = 1100 -> 2 wires -- */
985  /* TCK=1, TMS=1, TDI=0 (rising edge TCK with TMSC still 0... online mode activated... also OAC bit0==0) */
986  {'1', '1', '0'},
987  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
988  {'0', '1', '0'},
989  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... OAC bit1==0) */
990  {'1', '1', '0'},
991  /* TCK=0, TMS=1, TDI=1 (falling edge TCK) */
992  {'0', '1', '1'},
993  /* TCK=1, TMS=1, TDI=1 (rising edge TCK... OAC bit2==1) */
994  {'1', '1', '1'},
995  /* TCK=0, TMS=1, TDI=1 (falling edge TCK, TMSC stays high) */
996  {'0', '1', '1'},
997  /* TCK=1, TMS=1, TDI=1 (rising edge TCK... OAC bit3==1) */
998  {'1', '1', '1'},
999  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1000  {'0', '1', '0'},
1001  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... EC bit0==0) */
1002  {'1', '1', '0'},
1003  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1004  {'0', '1', '0'},
1005  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... EC bit1==0) */
1006  {'1', '1', '0'},
1007  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1008  {'0', '1', '0'},
1009  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... EC bit2==0) */
1010  {'1', '1', '0'},
1011  /* TCK=0, TMS=1, TDI=1 (falling edge TCK) */
1012  {'0', '1', '1'},
1013  /* TCK=1, TMS=1, TDI=1 (rising edge TCK... EC bit3==1) */
1014  {'1', '1', '1'},
1015  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1016  {'0', '1', '0'},
1017  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... CP bit0==0) */
1018  {'1', '1', '0'},
1019  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1020  {'0', '1', '0'},
1021  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... CP bit1==0) */
1022  {'1', '1', '0'},
1023  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1024  {'0', '1', '0'},
1025  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... CP bit2==0) */
1026  {'1', '1', '0'},
1027  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1028  {'0', '1', '0'},
1029  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... CP bit3==0) */
1030  {'1', '1', '0'},
1031  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1032  {'0', '1', '0'},
1033  };
1034 
1035  if (!oscan1_mode && !jscan3_mode)
1036  return; /* Nothing to do */
1037 
1038  if (oscan1_mode && jscan3_mode) {
1039  LOG_ERROR("Both oscan1_mode and jscan3_mode are \"on\". At most one of them can be enabled.");
1040  return;
1041  }
1042 
1043  if (!tck) {
1044  LOG_ERROR("Can't run cJTAG online/activate escape sequences: TCK signal is not defined");
1045  return;
1046  }
1047 
1048  if (!tdi) {
1049  LOG_ERROR("Can't run cJTAG online/activate escape sequences: TDI signal is not defined");
1050  return;
1051  }
1052 
1053  if (!tms) {
1054  LOG_ERROR("Can't run cJTAG online/activate escape sequences: TMS signal is not defined");
1055  return;
1056  }
1057 
1058  if (!tdo) {
1059  LOG_ERROR("Can't run cJTAG online/activate escape sequences: TDO signal is not defined");
1060  return;
1061  }
1062 
1063  if (jscan3_mode) {
1064  /* Update the sequence above to enable JScan3 instead of OScan1 */
1065  sequence[ESCAPE_SEQ_OAC_BIT2].tdi = '0';
1066  sequence[ESCAPE_SEQ_OAC_BIT2 + 1].tdi = '0';
1067  }
1068 
1069  /* if defined TMSC_EN, replace tms with it */
1070  if (tmsc_en)
1071  tms = tmsc_en;
1072 
1073  /* Send the sequence to the adapter */
1074  for (size_t i = 0; i < ARRAY_SIZE(sequence); i++)
1075  cjtag_set_tck_tms_tdi(tck, sequence[i].tck, tms, sequence[i].tms, tdi, sequence[i].tdi);
1076 
1077  /* If JScan3 mode, configure cJTAG adapter to 4-wire */
1078  if (jscan3_mode)
1079  ftdi_set_signal(find_signal_by_name("JTAG_SEL"), '1');
1080 
1081  ftdi_get_signal(tdo, &tdovalue); /* Just to force a flush */
1082 }
1083 #endif /* #if BUILD_FTDI_CJTAG == 1 */
1084 
1085 COMMAND_HANDLER(ftdi_handle_device_desc_command)
1086 {
1087  if (CMD_ARGC == 1) {
1088  free(ftdi_device_desc);
1089  ftdi_device_desc = strdup(CMD_ARGV[0]);
1090  } else {
1091  LOG_ERROR("expected exactly one argument to ftdi device_desc <description>");
1092  }
1093 
1094  return ERROR_OK;
1095 }
1096 
1097 COMMAND_HANDLER(ftdi_handle_channel_command)
1098 {
1099  if (CMD_ARGC == 1)
1101  else
1103 
1104  return ERROR_OK;
1105 }
1106 
1107 COMMAND_HANDLER(ftdi_handle_layout_init_command)
1108 {
1109  if (CMD_ARGC != 2)
1111 
1114 
1115  return ERROR_OK;
1116 }
1117 
1118 COMMAND_HANDLER(ftdi_handle_layout_signal_command)
1119 {
1120  if (CMD_ARGC < 1)
1122 
1123  bool invert_data = false;
1124  uint16_t data_mask = 0;
1125  bool invert_input = false;
1126  uint16_t input_mask = 0;
1127  bool invert_oe = false;
1128  uint16_t oe_mask = 0;
1129  for (unsigned int i = 1; i < CMD_ARGC; i += 2) {
1130  if (strcmp("-data", CMD_ARGV[i]) == 0) {
1131  invert_data = false;
1132  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask);
1133  } else if (strcmp("-ndata", CMD_ARGV[i]) == 0) {
1134  invert_data = true;
1135  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask);
1136  } else if (strcmp("-input", CMD_ARGV[i]) == 0) {
1137  invert_input = false;
1138  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], input_mask);
1139  } else if (strcmp("-ninput", CMD_ARGV[i]) == 0) {
1140  invert_input = true;
1141  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], input_mask);
1142  } else if (strcmp("-oe", CMD_ARGV[i]) == 0) {
1143  invert_oe = false;
1144  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask);
1145  } else if (strcmp("-noe", CMD_ARGV[i]) == 0) {
1146  invert_oe = true;
1147  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask);
1148  } else if (!strcmp("-alias", CMD_ARGV[i]) ||
1149  !strcmp("-nalias", CMD_ARGV[i])) {
1150  if (!strcmp("-nalias", CMD_ARGV[i])) {
1151  invert_data = true;
1152  invert_input = true;
1153  }
1154  struct signal *sig = find_signal_by_name(CMD_ARGV[i + 1]);
1155  if (!sig) {
1156  LOG_ERROR("signal %s is not defined", CMD_ARGV[i + 1]);
1157  return ERROR_FAIL;
1158  }
1159  data_mask = sig->data_mask;
1160  input_mask = sig->input_mask;
1161  oe_mask = sig->oe_mask;
1162  invert_input ^= sig->invert_input;
1163  invert_oe = sig->invert_oe;
1164  invert_data ^= sig->invert_data;
1165  } else {
1166  LOG_ERROR("unknown option '%s'", CMD_ARGV[i]);
1168  }
1169  }
1170 
1171  struct signal *sig;
1172  sig = find_signal_by_name(CMD_ARGV[0]);
1173  if (!sig)
1174  sig = create_signal(CMD_ARGV[0]);
1175  if (!sig) {
1176  LOG_ERROR("failed to create signal %s", CMD_ARGV[0]);
1177  return ERROR_FAIL;
1178  }
1179 
1180  sig->invert_data = invert_data;
1181  sig->data_mask = data_mask;
1182  sig->invert_input = invert_input;
1183  sig->input_mask = input_mask;
1184  sig->invert_oe = invert_oe;
1185  sig->oe_mask = oe_mask;
1186 
1187  return ERROR_OK;
1188 }
1189 
1190 COMMAND_HANDLER(ftdi_handle_set_signal_command)
1191 {
1192  if (CMD_ARGC < 2)
1194 
1195  struct signal *sig;
1196  sig = find_signal_by_name(CMD_ARGV[0]);
1197  if (!sig) {
1198  LOG_ERROR("interface configuration doesn't define signal '%s'", CMD_ARGV[0]);
1199  return ERROR_FAIL;
1200  }
1201 
1202  switch (*CMD_ARGV[1]) {
1203  case '0':
1204  case '1':
1205  case 'z':
1206  case 'Z':
1207  /* single character level specifier only */
1208  if (CMD_ARGV[1][1] == '\0') {
1209  ftdi_set_signal(sig, *CMD_ARGV[1]);
1210  break;
1211  }
1212  /* fallthrough */
1213  default:
1214  LOG_ERROR("unknown signal level '%s', use 0, 1 or z", CMD_ARGV[1]);
1216  }
1217 
1218  return mpsse_flush(mpsse_ctx);
1219 }
1220 
1221 COMMAND_HANDLER(ftdi_handle_get_signal_command)
1222 {
1223  if (CMD_ARGC < 1)
1225 
1226  struct signal *sig;
1227  uint16_t sig_data = 0;
1228  sig = find_signal_by_name(CMD_ARGV[0]);
1229  if (!sig) {
1230  command_print(CMD, "interface configuration doesn't define signal '%s'", CMD_ARGV[0]);
1231  return ERROR_FAIL;
1232  }
1233 
1234  int ret = ftdi_get_signal(sig, &sig_data);
1235  if (ret != ERROR_OK)
1236  return ret;
1237 
1238  command_print(CMD, "%#06x", sig_data);
1239 
1240  return ERROR_OK;
1241 }
1242 
1243 COMMAND_HANDLER(ftdi_handle_vid_pid_command)
1244 {
1245  if (CMD_ARGC > MAX_USB_IDS * 2) {
1246  LOG_WARNING("ignoring extra IDs in ftdi vid_pid "
1247  "(maximum is %d pairs)", MAX_USB_IDS);
1248  CMD_ARGC = MAX_USB_IDS * 2;
1249  }
1250  if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
1251  LOG_WARNING("incomplete ftdi vid_pid configuration directive");
1252  if (CMD_ARGC < 2)
1254  /* remove the incomplete trailing id */
1255  CMD_ARGC -= 1;
1256  }
1257 
1258  unsigned int i;
1259  for (i = 0; i < CMD_ARGC; i += 2) {
1260  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ftdi_vid[i >> 1]);
1261  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ftdi_pid[i >> 1]);
1262  }
1263 
1264  /*
1265  * Explicitly terminate, in case there are multiples instances of
1266  * ftdi vid_pid.
1267  */
1268  ftdi_vid[i >> 1] = ftdi_pid[i >> 1] = 0;
1269 
1270  return ERROR_OK;
1271 }
1272 
1273 COMMAND_HANDLER(ftdi_handle_tdo_sample_edge_command)
1274 {
1275  const struct nvp *n;
1276  static const struct nvp nvp_ftdi_jtag_modes[] = {
1277  { .name = "rising", .value = JTAG_MODE },
1278  { .name = "falling", .value = JTAG_MODE_ALT },
1279  { .name = NULL, .value = -1 },
1280  };
1281 
1282  if (CMD_ARGC > 0) {
1283  n = nvp_name2value(nvp_ftdi_jtag_modes, CMD_ARGV[0]);
1284  if (!n->name)
1286  ftdi_jtag_mode = n->value;
1287 
1288  }
1289 
1290  n = nvp_value2name(nvp_ftdi_jtag_modes, ftdi_jtag_mode);
1291  command_print(CMD, "ftdi samples TDO on %s edge of TCK", n->name);
1292 
1293  return ERROR_OK;
1294 }
1295 
1296 #if BUILD_FTDI_CJTAG == 1
1297 COMMAND_HANDLER(ftdi_handle_oscan1_mode_command)
1298 {
1299  if (CMD_ARGC > 1)
1301 
1302  if (CMD_ARGC == 1)
1303  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], oscan1_mode);
1304 
1305  command_print(CMD, "oscan1 mode: %s.", oscan1_mode ? "on" : "off");
1306  return ERROR_OK;
1307 }
1308 
1309 COMMAND_HANDLER(ftdi_handle_jscan3_mode_command)
1310 {
1311  if (CMD_ARGC > 1)
1313 
1314  if (CMD_ARGC == 1)
1315  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], jscan3_mode);
1316 
1317  command_print(CMD, "jscan3 mode: %s.", jscan3_mode ? "on" : "off");
1318  return ERROR_OK;
1319 }
1320 #endif
1321 
1322 static const struct command_registration ftdi_subcommand_handlers[] = {
1323  {
1324  .name = "device_desc",
1325  .handler = &ftdi_handle_device_desc_command,
1326  .mode = COMMAND_CONFIG,
1327  .help = "set the USB device description of the FTDI device",
1328  .usage = "description_string",
1329  },
1330  {
1331  .name = "channel",
1332  .handler = &ftdi_handle_channel_command,
1333  .mode = COMMAND_CONFIG,
1334  .help = "set the channel of the FTDI device that is used as JTAG",
1335  .usage = "(0-3)",
1336  },
1337  {
1338  .name = "layout_init",
1339  .handler = &ftdi_handle_layout_init_command,
1340  .mode = COMMAND_CONFIG,
1341  .help = "initialize the FTDI GPIO signals used "
1342  "to control output-enables and reset signals",
1343  .usage = "data direction",
1344  },
1345  {
1346  .name = "layout_signal",
1347  .handler = &ftdi_handle_layout_signal_command,
1348  .mode = COMMAND_ANY,
1349  .help = "define a signal controlled by one or more FTDI GPIO as data "
1350  "and/or output enable",
1351  .usage = "name [-data mask|-ndata mask] [-oe mask|-noe mask] [-alias|-nalias name]",
1352  },
1353  {
1354  .name = "set_signal",
1355  .handler = &ftdi_handle_set_signal_command,
1356  .mode = COMMAND_EXEC,
1357  .help = "control a layout-specific signal",
1358  .usage = "name (1|0|z)",
1359  },
1360  {
1361  .name = "get_signal",
1362  .handler = &ftdi_handle_get_signal_command,
1363  .mode = COMMAND_EXEC,
1364  .help = "read the value of a layout-specific signal",
1365  .usage = "name",
1366  },
1367  {
1368  .name = "vid_pid",
1369  .handler = &ftdi_handle_vid_pid_command,
1370  .mode = COMMAND_CONFIG,
1371  .help = "the vendor ID and product ID of the FTDI device",
1372  .usage = "(vid pid)*",
1373  },
1374  {
1375  .name = "tdo_sample_edge",
1376  .handler = &ftdi_handle_tdo_sample_edge_command,
1377  .mode = COMMAND_ANY,
1378  .help = "set which TCK clock edge is used for sampling TDO "
1379  "- default is rising-edge (Setting to falling-edge may "
1380  "allow signalling speed increase)",
1381  .usage = "(rising|falling)",
1382  },
1383 #if BUILD_FTDI_CJTAG == 1
1384  {
1385  .name = "oscan1_mode",
1386  .handler = &ftdi_handle_oscan1_mode_command,
1387  .mode = COMMAND_ANY,
1388  .help = "set to 'on' to use OScan1 mode for signaling, otherwise 'off' (default is 'off')",
1389  .usage = "(on|off)",
1390  },
1391  {
1392  .name = "jscan3_mode",
1393  .handler = &ftdi_handle_jscan3_mode_command,
1394  .mode = COMMAND_ANY,
1395  .help = "set to 'on' to use JScan3 mode for signaling, otherwise 'off' (default is 'off')",
1396  .usage = "(on|off)",
1397  },
1398 #endif
1400 };
1401 
1402 static const struct command_registration ftdi_command_handlers[] = {
1403  {
1404  .name = "ftdi",
1405  .mode = COMMAND_ANY,
1406  .help = "perform ftdi management",
1407  .chain = ftdi_subcommand_handlers,
1408  .usage = "",
1409  },
1411 };
1412 
1413 static int create_default_signal(const char *name, uint16_t data_mask)
1414 {
1415  struct signal *sig = create_signal(name);
1416  if (!sig) {
1417  LOG_ERROR("failed to create signal %s", name);
1418  return ERROR_FAIL;
1419  }
1420  sig->invert_data = false;
1421  sig->data_mask = data_mask;
1422  sig->invert_oe = false;
1423  sig->oe_mask = 0;
1424 
1425  return ERROR_OK;
1426 }
1427 
1428 static int create_signals(void)
1429 {
1430  if (create_default_signal("TCK", 0x01) != ERROR_OK)
1431  return ERROR_FAIL;
1432  if (create_default_signal("TDI", 0x02) != ERROR_OK)
1433  return ERROR_FAIL;
1434  if (create_default_signal("TDO", 0x04) != ERROR_OK)
1435  return ERROR_FAIL;
1436  if (create_default_signal("TMS", 0x08) != ERROR_OK)
1437  return ERROR_FAIL;
1438  return ERROR_OK;
1439 }
1440 
1441 static int ftdi_swd_init(void)
1442 {
1443  LOG_INFO("FTDI SWD mode enabled");
1444  swd_mode = true;
1445 
1446  if (create_signals() != ERROR_OK)
1447  return ERROR_FAIL;
1448 
1449  swd_cmd_queue_alloced = 10;
1450  swd_cmd_queue = malloc(swd_cmd_queue_alloced * sizeof(*swd_cmd_queue));
1451 
1452  return swd_cmd_queue ? ERROR_OK : ERROR_FAIL;
1453 }
1454 
1455 static void ftdi_swd_swdio_en(bool enable)
1456 {
1457  struct signal *oe = find_signal_by_name("SWDIO_OE");
1458  if (oe) {
1459  if (oe->data_mask)
1460  ftdi_set_signal(oe, enable ? '1' : '0');
1461  else {
1462  /* Sets TDI/DO pin to input during rx when both pins are connected
1463  to SWDIO */
1464  if (enable)
1465  direction |= jtag_direction_init & 0x0002U;
1466  else
1467  direction &= ~0x0002U;
1469  }
1470  }
1471 }
1472 
1477 static int ftdi_swd_run_queue(void)
1478 {
1479  LOG_DEBUG_IO("Executing %zu queued transactions", swd_cmd_queue_length);
1480  int retval;
1481  struct signal *led = find_signal_by_name("LED");
1482 
1483  if (queued_retval != ERROR_OK) {
1484  LOG_DEBUG_IO("Skipping due to previous errors: %d", queued_retval);
1485  goto skip;
1486  }
1487 
1488  /* A transaction must be followed by another transaction or at least 8 idle cycles to
1489  * ensure that data is clocked through the AP. */
1491 
1492  /* Terminate the "blink", if the current layout has that feature */
1493  if (led)
1494  ftdi_set_signal(led, '0');
1495 
1497  if (queued_retval != ERROR_OK) {
1498  LOG_ERROR("MPSSE failed");
1499  goto skip;
1500  }
1501 
1502  for (size_t i = 0; i < swd_cmd_queue_length; i++) {
1503  int ack = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1, 3);
1504 
1505  /* Devices do not reply to DP_TARGETSEL write cmd, ignore received ack */
1506  bool check_ack = swd_cmd_returns_ack(swd_cmd_queue[i].cmd);
1507 
1508  LOG_CUSTOM_LEVEL((check_ack && ack != SWD_ACK_OK) ? LOG_LVL_DEBUG : LOG_LVL_DEBUG_IO,
1509  "%s%s %s %s reg %X = %08" PRIx32,
1510  check_ack ? "" : "ack ignored ",
1511  ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
1512  swd_cmd_queue[i].cmd & SWD_CMD_APNDP ? "AP" : "DP",
1513  swd_cmd_queue[i].cmd & SWD_CMD_RNW ? "read" : "write",
1514  (swd_cmd_queue[i].cmd & SWD_CMD_A32) >> 1,
1515  buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn,
1516  1 + 3 + (swd_cmd_queue[i].cmd & SWD_CMD_RNW ? 0 : 1), 32));
1517 
1518  if (ack != SWD_ACK_OK && check_ack) {
1520  goto skip;
1521 
1522  } else if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) {
1523  uint32_t data = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3, 32);
1524  int parity = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 32, 1);
1525 
1526  if (parity != parity_u32(data)) {
1527  LOG_ERROR("SWD Read data parity mismatch");
1529  goto skip;
1530  }
1531 
1532  if (swd_cmd_queue[i].dst)
1533  *swd_cmd_queue[i].dst = data;
1534  }
1535  }
1536 
1537 skip:
1539  retval = queued_retval;
1541 
1542  /* Queue a new "blink" */
1543  if (led && retval == ERROR_OK)
1544  ftdi_set_signal(led, '1');
1545 
1546  return retval;
1547 }
1548 
1549 static void ftdi_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
1550 {
1552  /* Not enough room in the queue. Run the queue and increase its size for next time.
1553  * Note that it's not possible to avoid running the queue here, because mpsse contains
1554  * pointers into the queue which may be invalid after the realloc. */
1556  struct swd_cmd_queue_entry *q = realloc(swd_cmd_queue, swd_cmd_queue_alloced * 2 * sizeof(*swd_cmd_queue));
1557  if (q) {
1558  swd_cmd_queue = q;
1559  swd_cmd_queue_alloced *= 2;
1560  LOG_DEBUG("Increased SWD command queue to %zu elements", swd_cmd_queue_alloced);
1561  }
1562  }
1563 
1564  if (queued_retval != ERROR_OK)
1565  return;
1566 
1567  size_t i = swd_cmd_queue_length++;
1569 
1571 
1572  if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) {
1573  /* Queue a read transaction */
1574  swd_cmd_queue[i].dst = dst;
1575 
1576  ftdi_swd_swdio_en(false);
1578  0, 1 + 3 + 32 + 1 + 1, SWD_MODE);
1579  ftdi_swd_swdio_en(true);
1580  } else {
1581  /* Queue a write transaction */
1582  ftdi_swd_swdio_en(false);
1583 
1585  0, 1 + 3 + 1, SWD_MODE);
1586 
1587  ftdi_swd_swdio_en(true);
1588 
1589  buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1, 32, data);
1590  buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1 + 32, 1, parity_u32(data));
1591 
1593  1 + 3 + 1, 32 + 1, SWD_MODE);
1594  }
1595 
1596  /* Insert idle cycles after AP accesses to avoid WAIT */
1597  if (cmd & SWD_CMD_APNDP)
1598  mpsse_clock_data_out(mpsse_ctx, NULL, 0, ap_delay_clk, SWD_MODE);
1599 
1600 }
1601 
1602 static void ftdi_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1603 {
1604  assert(cmd & SWD_CMD_RNW);
1605  ftdi_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
1606 }
1607 
1608 static void ftdi_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1609 {
1610  assert(!(cmd & SWD_CMD_RNW));
1611  ftdi_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
1612 }
1613 
1615 {
1616  switch (seq) {
1617  case LINE_RESET:
1618  LOG_DEBUG("SWD line reset");
1619  ftdi_swd_swdio_en(true);
1621  break;
1622  case JTAG_TO_SWD:
1623  LOG_DEBUG("JTAG-to-SWD");
1624  ftdi_swd_swdio_en(true);
1626  break;
1627  case JTAG_TO_DORMANT:
1628  LOG_DEBUG("JTAG-to-DORMANT");
1629  ftdi_swd_swdio_en(true);
1631  break;
1632  case SWD_TO_JTAG:
1633  LOG_DEBUG("SWD-to-JTAG");
1634  ftdi_swd_swdio_en(true);
1636  break;
1637  case SWD_TO_DORMANT:
1638  LOG_DEBUG("SWD-to-DORMANT");
1639  ftdi_swd_swdio_en(true);
1641  break;
1642  case DORMANT_TO_SWD:
1643  LOG_DEBUG("DORMANT-to-SWD");
1644  ftdi_swd_swdio_en(true);
1646  break;
1647  case DORMANT_TO_JTAG:
1648  LOG_DEBUG("DORMANT-to-JTAG");
1649  ftdi_swd_swdio_en(true);
1651  break;
1652  default:
1653  LOG_ERROR("Sequence %d not supported", seq);
1654  return ERROR_FAIL;
1655  }
1656 
1657  return ERROR_OK;
1658 }
1659 
1660 static const struct swd_driver ftdi_swd = {
1661  .init = ftdi_swd_init,
1662  .switch_seq = ftdi_swd_switch_seq,
1663  .read_reg = ftdi_swd_read_reg,
1664  .write_reg = ftdi_swd_write_reg,
1665  .run = ftdi_swd_run_queue,
1666 };
1667 
1668 static struct jtag_interface ftdi_interface = {
1670  .execute_queue = ftdi_execute_queue,
1671 };
1672 
1674  .name = "ftdi",
1675  .transport_ids = TRANSPORT_JTAG | TRANSPORT_SWD,
1676  .transport_preferred_id = TRANSPORT_JTAG,
1677  .commands = ftdi_command_handlers,
1678 
1679  .init = ftdi_initialize,
1680  .quit = ftdi_quit,
1681  .reset = ftdi_reset,
1682  .speed = ftdi_speed,
1683  .khz = ftdi_khz,
1684  .speed_div = ftdi_speed_div,
1685 
1686  .jtag_ops = &ftdi_interface,
1687  .swd_ops = &ftdi_swd,
1688 };
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Definition: adapter.c:302
unsigned int adapter_get_speed_khz(void)
Retrieves the clock speed of the adapter in kHz.
Definition: adapter.c:210
const char * adapter_usb_get_location(void)
Definition: adapter.c:328
#define SWD_ACK_FAULT
Definition: arm_adi_v5.h:33
swd_special_seq
Definition: arm_adi_v5.h:236
@ DORMANT_TO_JTAG
Definition: arm_adi_v5.h:243
@ JTAG_TO_SWD
Definition: arm_adi_v5.h:238
@ DORMANT_TO_SWD
Definition: arm_adi_v5.h:242
@ LINE_RESET
Definition: arm_adi_v5.h:237
@ JTAG_TO_DORMANT
Definition: arm_adi_v5.h:239
@ SWD_TO_DORMANT
Definition: arm_adi_v5.h:241
@ SWD_TO_JTAG
Definition: arm_adi_v5.h:240
#define SWD_ACK_WAIT
Definition: arm_adi_v5.h:32
#define SWD_ACK_OK
Definition: arm_adi_v5.h:31
enum arm_mode mode
Definition: armv4_5.c:281
const char * name
Definition: armv4_5.c:76
static void bit_copy(uint8_t *dst, unsigned int dst_offset, const uint8_t *src, unsigned int src_offset, unsigned int bit_count)
Definition: binarybuffer.h:218
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:371
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#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 COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:528
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:400
#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:440
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:402
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
enum scan_type jtag_scan_type(const struct scan_command *cmd)
Definition: commands.c:167
@ 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 length
Definition: esp_usb_jtag.c:1
static uint16_t output
Definition: ftdi.c:162
static void move_to_state(enum tap_state goal_state)
Function move_to_state moves the TAP controller from the current state to a goal_state through a path...
Definition: ftdi.c:313
COMMAND_HANDLER(ftdi_handle_device_desc_command)
Definition: ftdi.c:1085
static uint8_t ftdi_channel
Definition: ftdi.c:93
static const struct command_registration ftdi_command_handlers[]
Definition: ftdi.c:1402
static void ftdi_swd_swdio_en(bool enable)
Definition: ftdi.c:1455
#define DO_CLOCK_TMS_CS_OUT
Definition: ftdi.c:85
static int queued_retval
Definition: ftdi.c:159
static void ftdi_execute_sleep(struct jtag_command *cmd)
Definition: ftdi.c:634
static struct signal * create_signal(const char *name)
Definition: ftdi.c:178
static void ftdi_execute_stableclocks(struct jtag_command *cmd)
Definition: ftdi.c:645
static void ftdi_execute_pathmove(struct jtag_command *cmd)
Definition: ftdi.c:443
static int ftdi_swd_init(void)
Definition: ftdi.c:1441
static void ftdi_end_state(enum tap_state state)
Definition: ftdi.c:374
static uint16_t direction
Definition: ftdi.c:163
static char * ftdi_device_desc
Definition: ftdi.c:92
static void ftdi_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: ftdi.c:1602
static struct jtag_interface ftdi_interface
Definition: ftdi.c:1668
#define JTAG_MODE
Definition: ftdi.c:88
static uint16_t jtag_output_init
Definition: ftdi.c:164
static int ftdi_speed(int speed)
Definition: ftdi.c:341
static int ftdi_get_signal(const struct signal *s, uint16_t *value_out)
Definition: ftdi.c:249
static bool swd_mode
Definition: ftdi.c:96
static size_t swd_cmd_queue_length
Definition: ftdi.c:157
struct adapter_driver ftdi_adapter_driver
Definition: ftdi.c:1673
static const struct swd_driver ftdi_swd
Definition: ftdi.c:1660
static struct mpsse_ctx * mpsse_ctx
Definition: ftdi.c:136
static void ftdi_execute_tms(struct jtag_command *cmd)
Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG (or SWD) state machine.
Definition: ftdi.c:430
#define MAX_USB_IDS
Definition: ftdi.c:131
static size_t swd_cmd_queue_alloced
Definition: ftdi.c:158
static int ftdi_reset(int trst, int srst)
Definition: ftdi.c:596
static int ftdi_khz(int khz, int *jtag_speed)
Definition: ftdi.c:363
static int ftdi_speed_div(int speed, int *khz)
Definition: ftdi.c:357
static uint16_t ftdi_pid[MAX_USB_IDS+1]
Definition: ftdi.c:134
static int ftdi_execute_queue(struct jtag_command *cmd_queue)
Definition: ftdi.c:708
static void ftdi_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
Definition: ftdi.c:1549
static int ftdi_swd_run_queue(void)
Flush the MPSSE queue and process the SWD transaction queue.
Definition: ftdi.c:1477
#define JTAG_MODE_ALT
Definition: ftdi.c:89
static uint16_t jtag_direction_init
Definition: ftdi.c:165
static struct signal * find_signal_by_name(const char *name)
Definition: ftdi.c:169
static int ftdi_initialize(void)
Definition: ftdi.c:730
static int ftdi_quit(void)
Definition: ftdi.c:786
#define SWD_MODE
Definition: ftdi.c:90
static void ftdi_execute_statemove(struct jtag_command *cmd)
Definition: ftdi.c:414
static int create_default_signal(const char *name, uint16_t data_mask)
Definition: ftdi.c:1413
static void ftdi_execute_scan(struct jtag_command *cmd)
Definition: ftdi.c:494
#define DO_CLOCK_DATA
Definition: ftdi.c:83
static uint8_t ftdi_jtag_mode
Definition: ftdi.c:94
static void ftdi_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: ftdi.c:1608
static uint16_t ftdi_vid[MAX_USB_IDS+1]
Definition: ftdi.c:133
static int ftdi_swd_switch_seq(enum swd_special_seq seq)
Definition: ftdi.c:1614
static void ftdi_execute_runtest(struct jtag_command *cmd)
Definition: ftdi.c:384
#define DO_CLOCK_TMS_CS
Definition: ftdi.c:84
static int freq
Definition: ftdi.c:160
static int create_signals(void)
Definition: ftdi.c:1428
static const struct command_registration ftdi_subcommand_handlers[]
Definition: ftdi.c:1322
static int ftdi_set_signal(const struct signal *s, char value)
Definition: ftdi.c:196
static struct signal * signals
Definition: ftdi.c:149
static struct swd_cmd_queue_entry * swd_cmd_queue
static void ftdi_execute_command(struct jtag_command *cmd)
Definition: ftdi.c:669
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:1075
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1747
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
@ TAP_IRPAUSE
Definition: jtag.h:52
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:552
@ RESET_HAS_SRST
Definition: jtag.h:218
@ RESET_HAS_TRST
Definition: jtag.h:217
@ RESET_TRST_OPEN_DRAIN
Definition: jtag.h:222
@ RESET_SRST_PUSH_PULL
Definition: jtag.h:223
#define LOG_CUSTOM_LEVEL(level, expr ...)
Definition: log.h:119
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:103
#define LOG_WARNING(expr ...)
Definition: log.h:131
#define ERROR_FAIL
Definition: log.h:175
#define LOG_ERROR(expr ...)
Definition: log.h:134
#define LOG_INFO(expr ...)
Definition: log.h:128
#define LOG_DEBUG(expr ...)
Definition: log.h:111
#define ERROR_OK
Definition: log.h:169
@ LOG_LVL_DEBUG
Definition: log.h:48
@ LOG_LVL_DEBUG_IO
Definition: log.h:49
#define zero
Definition: mips32.c:181
int mpsse_flush(struct mpsse_ctx *ctx)
Definition: mpsse.c:850
void mpsse_read_data_bits_high_byte(struct mpsse_ctx *ctx, uint8_t *data)
Definition: mpsse.c:678
void mpsse_set_data_bits_high_byte(struct mpsse_ctx *ctx, uint8_t data, uint8_t dir)
Definition: mpsse.c:645
int mpsse_set_frequency(struct mpsse_ctx *ctx, int frequency)
Definition: mpsse.c:753
void mpsse_read_data_bits_low_byte(struct mpsse_ctx *ctx, uint8_t *data)
Definition: mpsse.c:662
void mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, unsigned int length, bool tdi, uint8_t mode)
Definition: mpsse.c:571
void mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in, unsigned int in_offset, unsigned int length, uint8_t mode)
Definition: mpsse.c:500
void mpsse_clock_data_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, unsigned int length, uint8_t mode)
Definition: mpsse.c:488
void mpsse_clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in, unsigned int in_offset, unsigned int length, bool tdi, uint8_t mode)
Definition: mpsse.c:577
struct mpsse_ctx * mpsse_open(const uint16_t vids[], const uint16_t pids[], const char *description, const char *serial, const char *location, int channel)
Definition: mpsse.c:332
bool mpsse_is_high_speed(struct mpsse_ctx *ctx)
Definition: mpsse.c:421
void mpsse_close(struct mpsse_ctx *ctx)
Definition: mpsse.c:407
void mpsse_clock_data_in(struct mpsse_ctx *ctx, uint8_t *in, unsigned int in_offset, unsigned int length, uint8_t mode)
Definition: mpsse.c:494
void mpsse_loopback_config(struct mpsse_ctx *ctx, bool enable)
Definition: mpsse.c:708
void mpsse_set_data_bits_low_byte(struct mpsse_ctx *ctx, uint8_t data, uint8_t dir)
Definition: mpsse.c:628
const struct nvp * nvp_name2value(const struct nvp *p, const char *name)
Definition: nvp.c:29
const struct nvp * nvp_value2name(const struct nvp *p, int value)
Definition: nvp.c:39
static uint32_t bit(uint32_t value, unsigned int b)
Definition: opcodes.h:39
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:234
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:239
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
Name Value Pairs, aka: NVP.
Definition: nvp.h:61
int value
Definition: nvp.h:63
const char * name
Definition: nvp.h:62
This structure defines a single scan field in the scan.
Definition: jtag.h:87
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
unsigned int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
Definition: osbdm.c:17
const void * tdi
Definition: osbdm.c:21
Definition: ftdi.c:138
struct signal * next
Definition: ftdi.c:146
bool invert_oe
Definition: ftdi.c:145
const char * name
Definition: ftdi.c:139
bool invert_data
Definition: ftdi.c:143
uint16_t input_mask
Definition: ftdi.c:141
uint16_t data_mask
Definition: ftdi.c:140
bool invert_input
Definition: ftdi.c:144
uint16_t oe_mask
Definition: ftdi.c:142
Definition: ftdi.c:152
uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4+3+32+1+4, 8)]
Definition: ftdi.c:155
uint8_t cmd
Definition: ftdi.c:153
uint32_t * dst
Definition: ftdi.c:154
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
Definition: swd.h:255
static const unsigned int swd_seq_dormant_to_swd_len
Definition: swd.h:190
static const uint8_t swd_seq_dormant_to_jtag[]
Dormant-to-JTAG sequence.
Definition: swd.h:230
#define SWD_CMD_A32
Definition: swd.h:19
static const uint8_t swd_seq_dormant_to_swd[]
Dormant-to-SWD sequence.
Definition: swd.h:171
static const uint8_t swd_seq_jtag_to_dormant[]
JTAG-to-dormant sequence.
Definition: swd.h:199
static bool swd_cmd_returns_ack(uint8_t cmd)
Test if we can rely on ACK returned by SWD command.
Definition: swd.h:58
#define SWD_CMD_PARK
Definition: swd.h:22
static int swd_ack_to_error_code(uint8_t ack)
Convert SWD ACK value returned from DP to OpenOCD error code.
Definition: swd.h:72
static const unsigned int swd_seq_jtag_to_swd_len
Definition: swd.h:125
static const unsigned int swd_seq_line_reset_len
Definition: swd.h:104
static const unsigned int swd_seq_dormant_to_jtag_len
Definition: swd.h:244
#define SWD_CMD_APNDP
Definition: swd.h:17
static const unsigned int swd_seq_swd_to_dormant_len
Definition: swd.h:159
#define SWD_CMD_START
Definition: swd.h:16
#define SWD_CMD_RNW
Definition: swd.h:18
static const uint8_t swd_seq_line_reset[]
SWD Line reset.
Definition: swd.h:98
static const uint8_t swd_seq_jtag_to_swd[]
JTAG-to-SWD sequence.
Definition: swd.h:115
static const uint8_t swd_seq_swd_to_jtag[]
SWD-to-JTAG sequence.
Definition: swd.h:136
static const unsigned int swd_seq_swd_to_jtag_len
Definition: swd.h:144
static const unsigned int swd_seq_jtag_to_dormant_len
Definition: swd.h:211
static const uint8_t swd_seq_swd_to_dormant[]
SWD-to-dormant sequence.
Definition: swd.h:153
#define TRANSPORT_SWD
Definition: transport.h:20
#define TRANSPORT_JTAG
Definition: transport.h:19
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
static int parity_u32(uint32_t x)
Calculate the (even) parity of a 32-bit datum.
Definition: types.h:265
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t state[4]
Definition: vdebug.c:21
static unsigned int parity(unsigned int v)
Definition: xscale.c:624