OpenOCD
rlink.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007,2008 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  * *
10  * Copyright (C) 2008 Rob Brown, Lou Deluxe *
11  * rob@cobbleware.com, lou.openocd012@fixit.nospammail.net *
12  ***************************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 /* project specific includes */
19 #include <jtag/interface.h>
20 #include <jtag/commands.h>
21 #include "helper/replacements.h"
22 #include "rlink.h"
23 #include "rlink_st7.h"
24 #include "rlink_ep1_cmd.h"
25 #include "rlink_dtc_cmd.h"
26 #include "libusb_helper.h"
27 
28 /* This feature is made useless by running the DTC all the time. When automatic, the LED is on
29  *whenever the DTC is running. Otherwise, USB messages are sent to turn it on and off. */
30 #undef AUTOMATIC_BUSY_LED
31 
32 /* This feature may require derating the speed due to reduced hold time. */
33 #undef USE_HARDWARE_SHIFTER_FOR_TMS
34 
35 #define INTERFACE_NAME "RLink"
36 
37 #define USB_IDVENDOR (0x138e)
38 #define USB_IDPRODUCT (0x9000)
39 
40 #define USB_EP1OUT_ADDR (0x01)
41 #define USB_EP1OUT_SIZE (16)
42 #define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80)
43 #define USB_EP1IN_SIZE (USB_EP1OUT_SIZE)
44 
45 #define USB_EP2OUT_ADDR (0x02)
46 #define USB_EP2OUT_SIZE (64)
47 #define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80)
48 #define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
49 #define USB_EP2BANK_SIZE (512)
50 
51 #define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
52 
53 #define ST7_PD_NBUSY_LED ST7_PD0
54 #define ST7_PD_NRUN_LED ST7_PD1
55 /* low enables VPP at adapter header, high connects it to GND instead */
56 #define ST7_PD_VPP_SEL ST7_PD6
57 /* low: VPP = 12v, high: VPP <= 5v */
58 #define ST7_PD_VPP_SHDN ST7_PD7
59 
60 /* These pins are connected together */
61 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
62 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
63 
64 /* Symbolic mapping between port pins and numbered IO lines */
65 #define ST7_PA_IO1 ST7_PA1
66 #define ST7_PA_IO2 ST7_PA2
67 #define ST7_PA_IO4 ST7_PA4
68 #define ST7_PA_IO8 ST7_PA6
69 #define ST7_PA_IO10 ST7_PA7
70 #define ST7_PB_IO5 ST7_PB5
71 #define ST7_PC_IO9 ST7_PC1
72 #define ST7_PC_IO3 ST7_PC2
73 #define ST7_PC_IO7 ST7_PC3
74 #define ST7_PE_IO6 ST7_PE5
75 
76 /* Symbolic mapping between numbered IO lines and adapter signals */
77 #define ST7_PA_RTCK ST7_PA_IO0
78 #define ST7_PA_NTRST ST7_PA_IO1
79 #define ST7_PC_TDI ST7_PC_IO3
80 #define ST7_PA_DBGRQ ST7_PA_IO4
81 #define ST7_PB_NSRST ST7_PB_IO5
82 #define ST7_PE_TMS ST7_PE_IO6
83 #define ST7_PC_TCK ST7_PC_IO7
84 #define ST7_PC_TDO ST7_PC_IO9
85 #define ST7_PA_DBGACK ST7_PA_IO10
86 
87 static struct libusb_device_handle *hdev;
88 
89 /*
90  * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
91  * This function takes care of zeroing the unused bytes before sending the packet.
92  * Any reply packet is not handled by this function.
93  */
94 static int ep1_generic_commandl(struct libusb_device_handle *hdev_param, size_t length, ...)
95 {
96  uint8_t usb_buffer[USB_EP1OUT_SIZE];
97  uint8_t *usb_buffer_p;
98  va_list ap;
99  int usb_ret;
100  int transferred;
101 
102  if (length > sizeof(usb_buffer))
103  length = sizeof(usb_buffer);
104 
105  usb_buffer_p = usb_buffer;
106 
107  va_start(ap, length);
108  while (length > 0) {
109  *usb_buffer_p++ = va_arg(ap, int);
110  length--;
111  }
112 
113  memset(
114  usb_buffer_p,
115  0,
116  sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
117  );
118 
119  usb_ret = jtag_libusb_bulk_write(
120  hdev_param,
122  (char *)usb_buffer, sizeof(usb_buffer),
124  &transferred
125  );
126 
127  if (usb_ret != ERROR_OK)
128  return usb_ret;
129  return transferred;
130 }
131 
132 #if 0
133 static ssize_t ep1_memory_read(
134  struct libusb_device_handle *hdev_param, uint16_t addr,
135  size_t length, uint8_t *buffer)
136 {
137  uint8_t usb_buffer[USB_EP1OUT_SIZE];
138  int usb_ret;
139  size_t remain;
140  ssize_t count;
141  int transferred;
142 
143  usb_buffer[0] = EP1_CMD_MEMORY_READ;
144  memset(
145  usb_buffer + 4,
146  0,
147  sizeof(usb_buffer) - 4
148  );
149 
150  remain = length;
151  count = 0;
152 
153  while (remain) {
154  if (remain > sizeof(usb_buffer))
155  length = sizeof(usb_buffer);
156  else
157  length = remain;
158 
159  usb_buffer[1] = addr >> 8;
160  usb_buffer[2] = addr;
161  usb_buffer[3] = length;
162 
163  usb_ret = jtag_libusb_bulk_write(
164  hdev_param, USB_EP1OUT_ADDR,
165  (char *)usb_buffer, sizeof(usb_buffer),
167  &transferred
168  );
169 
170  if (usb_ret != ERROR_OK || transferred < (int)sizeof(usb_buffer))
171  break;
172 
173  usb_ret = jtag_libusb_bulk_read(
174  hdev_param, USB_EP1IN_ADDR,
175  (char *)buffer, length,
177  &transferred
178  );
179 
180  if (usb_ret != ERROR_OK || transferred < (int)length)
181  break;
182 
183  addr += length;
184  buffer += length;
185  count += length;
186  remain -= length;
187  }
188 
189  return count;
190 }
191 #endif
192 
193 static ssize_t ep1_memory_write(struct libusb_device_handle *hdev_param, uint16_t addr,
194  size_t length, uint8_t const *buffer)
195 {
196  uint8_t usb_buffer[USB_EP1OUT_SIZE];
197  int usb_ret;
198  size_t remain;
199  ssize_t count;
200 
201  usb_buffer[0] = EP1_CMD_MEMORY_WRITE;
202 
203  remain = length;
204  count = 0;
205 
206  while (remain) {
207  if (remain > (sizeof(usb_buffer) - 4))
208  length = (sizeof(usb_buffer) - 4);
209  else
210  length = remain;
211 
212  usb_buffer[1] = addr >> 8;
213  usb_buffer[2] = addr;
214  usb_buffer[3] = length;
215  memcpy(
216  usb_buffer + 4,
217  buffer,
218  length
219  );
220  memset(
221  usb_buffer + 4 + length,
222  0,
223  sizeof(usb_buffer) - 4 - length
224  );
225 
226  int transferred;
227 
228  usb_ret = jtag_libusb_bulk_write(
229  hdev_param, USB_EP1OUT_ADDR,
230  (char *)usb_buffer, sizeof(usb_buffer),
232  &transferred
233  );
234 
235  if (usb_ret != ERROR_OK || transferred < (int)sizeof(usb_buffer))
236  break;
237 
238  addr += length;
239  buffer += length;
240  count += length;
241  remain -= length;
242  }
243 
244  return count;
245 }
246 
247 
248 #if 0
249 static ssize_t ep1_memory_writel(struct libusb_device_handle *hdev_param, uint16_t addr,
250  size_t length, ...)
251 {
252  uint8_t buffer[USB_EP1OUT_SIZE - 4];
253  uint8_t *buffer_p;
254  va_list ap;
255  size_t remain;
256 
257  if (length > sizeof(buffer))
258  length = sizeof(buffer);
259 
260  remain = length;
261  buffer_p = buffer;
262 
263  va_start(ap, length);
264  while (remain > 0) {
265  *buffer_p++ = va_arg(ap, int);
266  remain--;
267  }
268 
269  return ep1_memory_write(hdev_param, addr, length, buffer);
270 }
271 #endif
272 
273 #define DTCLOAD_COMMENT (0)
274 #define DTCLOAD_ENTRY (1)
275 #define DTCLOAD_LOAD (2)
276 #define DTCLOAD_RUN (3)
277 #define DTCLOAD_LUT_START (4)
278 #define DTCLOAD_LUT (5)
279 
280 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
281 
282 /* This gets set by the DTC loader */
283 static uint8_t dtc_entry_download;
284 
285 /* The buffer is specially formatted to represent a valid image to load into the DTC. */
286 static int dtc_load_from_buffer(struct libusb_device_handle *hdev_param, const uint8_t *buffer,
287  size_t length)
288 {
289  struct header {
290  uint8_t type;
291  uint8_t length;
292  };
293 
294  int usb_err;
295  struct header *header;
296  uint8_t lut_start = 0xc0;
297 
298  dtc_entry_download = 0;
299 
300  /* Stop the DTC before loading anything. */
301  usb_err = ep1_generic_commandl(
302  hdev_param, 1,
304  );
305  if (usb_err < 0)
306  return usb_err;
307 
308  while (length) {
309  if (length < sizeof(*header)) {
310  LOG_ERROR("Malformed DTC image");
311  exit(1);
312  }
313 
314  header = (struct header *)buffer;
315  buffer += sizeof(*header);
316  length -= sizeof(*header);
317 
318  if (length < (size_t)header->length + 1) {
319  LOG_ERROR("Malformed DTC image");
320  exit(1);
321  }
322 
323  switch (header->type) {
324  case DTCLOAD_COMMENT:
325  break;
326 
327  case DTCLOAD_ENTRY:
328  /* store entry addresses somewhere */
329  if (!strncmp("download", (char *)buffer + 1, 8))
331  break;
332 
333  case DTCLOAD_LOAD:
334  /* Send the DTC program to ST7 RAM. */
335  usb_err = ep1_memory_write(hdev_param, DTC_LOAD_BUFFER,
336  header->length + 1, buffer);
337  if (usb_err < 0)
338  return usb_err;
339 
340  /* Load it into the DTC. */
341  usb_err = ep1_generic_commandl(hdev_param, 3, EP1_CMD_DTC_LOAD,
343  if (usb_err < 0)
344  return usb_err;
345 
346  break;
347 
348  case DTCLOAD_RUN:
349  usb_err = ep1_generic_commandl(hdev_param, 3, EP1_CMD_DTC_CALL,
351  if (usb_err < 0)
352  return usb_err;
353 
354  break;
355 
356  case DTCLOAD_LUT_START:
357  lut_start = buffer[0];
358  break;
359 
360  case DTCLOAD_LUT:
361  usb_err = ep1_memory_write(hdev_param,
362  ST7_USB_BUF_EP0OUT + lut_start, header->length + 1, buffer);
363  if (usb_err < 0)
364  return usb_err;
365  break;
366 
367  default:
368  LOG_ERROR("Invalid DTC image record type: 0x%02x", header->type);
369  exit(1);
370  break;
371  }
372 
373  buffer += (header->length + 1);
374  length -= (header->length + 1);
375  }
376 
377  return 0;
378 }
379 
380 /*
381  * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
382  */
383 static int dtc_start_download(void)
384 {
385  int usb_err;
386  uint8_t ep2txr;
387  int transferred;
388 
389  /* set up for download mode and make sure EP2 is set up to transmit */
390  usb_err = ep1_generic_commandl(
391  hdev, 7,
392 
396  EP1_CMD_MEMORY_READ, /* read EP2TXR for its data toggle */
397  ST7_EP2TXR >> 8,
398  ST7_EP2TXR,
399  1
400  );
401  if (usb_err < 0)
402  return usb_err;
403 
404  /* read back ep2txr */
405  usb_err = jtag_libusb_bulk_read(
407  (char *)&ep2txr, 1,
409  &transferred
410  );
411  if (usb_err != ERROR_OK)
412  return usb_err;
413 
414  usb_err = ep1_generic_commandl(
415  hdev, 13,
416 
417  EP1_CMD_MEMORY_WRITE, /* preinitialize poll byte */
420  1,
421  0x00,
422  EP1_CMD_MEMORY_WRITE, /* set EP2IN to return data */
423  ST7_EP2TXR >> 8,
424  ST7_EP2TXR,
425  1,
427  EP1_CMD_DTC_CALL, /* start running the DTC */
430  );
431  if (usb_err < 0)
432  return usb_err;
433 
434  /* wait for completion */
435  usb_err = jtag_libusb_bulk_read(
437  (char *)&ep2txr, 1,
439  &transferred
440  );
441 
442  return usb_err;
443 }
444 
445 static int dtc_run_download(
446  struct libusb_device_handle *hdev_param,
447  uint8_t *command_buffer,
448  int command_buffer_size,
449  uint8_t *reply_buffer,
450  int reply_buffer_size
451  )
452 {
453  char dtc_status;
454  int usb_err;
455  int i;
456  int transferred;
457 
458  LOG_DEBUG("%d/%d", command_buffer_size, reply_buffer_size);
459 
460  usb_err = jtag_libusb_bulk_write(
461  hdev_param,
463  (char *)command_buffer, USB_EP2BANK_SIZE,
465  &transferred
466  );
467  if (usb_err < 0)
468  return usb_err;
469 
470 
471  /* Wait for DTC to finish running command buffer */
472  for (i = 50;; ) {
473  usb_err = ep1_generic_commandl(
474  hdev_param, 4,
475 
479  1
480  );
481  if (usb_err < 0)
482  return usb_err;
483 
484  usb_err = jtag_libusb_bulk_read(
485  hdev_param,
487  &dtc_status, 1,
489  &transferred
490  );
491  if (usb_err < 0)
492  return usb_err;
493 
494  if (dtc_status & 0x01)
495  break;
496 
497  if (!--i) {
498  LOG_ERROR("too many retries waiting for DTC status");
499  return LIBUSB_ERROR_TIMEOUT;
500  }
501  }
502 
503 
504  if (reply_buffer && reply_buffer_size) {
505  usb_err = jtag_libusb_bulk_read(
506  hdev_param,
508  (char *)reply_buffer, reply_buffer_size,
510  &transferred
511  );
512 
513  if (usb_err != ERROR_OK || transferred < reply_buffer_size) {
514  LOG_ERROR("Read of endpoint 2 returned %d, expected %d",
515  usb_err, reply_buffer_size
516  );
517  return usb_err;
518  }
519  }
520 
521  return usb_err;
522 }
523 
524 /*
525  * The dtc reply queue is a singly linked list that describes what to do
526  * with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate
527  * these entries.
528  */
529 
532  struct jtag_command *cmd; /* the command that resulted in this entry */
533 
534  struct {
535  uint8_t *buffer; /* the scan buffer */
536  int size; /* size of the scan buffer in bits */
537  int offset; /* how many bits were already done before this? */
538  int length; /* how many bits are processed in this operation? */
539  enum scan_type type; /* SCAN_IN/SCAN_OUT/SCAN_IO */
540  } scan;
541 };
542 
543 
544 /*
545  * The dtc_queue consists of a buffer of pending commands and a reply queue.
546  * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
547  */
548 
549 static struct {
552  uint32_t cmd_index;
553  uint32_t reply_index;
556 
557 /*
558  * The tap state queue is for accumulating TAP state changes without needlessly
559  * flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to
560  * the dtc_queue.
561  */
562 
563 static struct {
564  uint32_t length;
565  uint32_t buffer;
567 
568 static int dtc_queue_init(void)
569 {
570  dtc_queue.rq_head = NULL;
571  dtc_queue.rq_tail = NULL;
572  dtc_queue.cmd_index = 0;
573  dtc_queue.reply_index = 0;
574  return 0;
575 }
576 
578  enum scan_type type, uint8_t *buffer, int size, int offset,
579  int length, struct jtag_command *cmd)
580 {
581  struct dtc_reply_queue_entry *rq_entry;
582 
583  rq_entry = malloc(sizeof(struct dtc_reply_queue_entry));
584  if (rq_entry) {
585  rq_entry->scan.type = type;
586  rq_entry->scan.buffer = buffer;
587  rq_entry->scan.size = size;
588  rq_entry->scan.offset = offset;
589  rq_entry->scan.length = length;
590  rq_entry->cmd = cmd;
591  rq_entry->next = NULL;
592 
593  if (!dtc_queue.rq_head)
594  dtc_queue.rq_head = rq_entry;
595  else
596  dtc_queue.rq_tail->next = rq_entry;
597 
598  dtc_queue.rq_tail = rq_entry;
599  }
600 
601  return rq_entry;
602 }
603 
604 /*
605  * Running the queue means that any pending command buffer is run
606  * and any reply data dealt with. The command buffer is then cleared for subsequent processing.
607  * The queue is automatically run by append when it is necessary to get space for the append.
608  */
609 
610 static int dtc_queue_run(void)
611 {
612  struct dtc_reply_queue_entry *rq_p, *rq_next;
613  int retval;
614  int usb_err;
615  int bit_cnt;
616  int x;
617  uint8_t *dtc_p, *tdo_p;
618  uint8_t dtc_mask, tdo_mask;
619  uint8_t reply_buffer[USB_EP2IN_SIZE];
620 
621  assert((!!dtc_queue.rq_head) == (dtc_queue.reply_index > 0));
622  assert(dtc_queue.cmd_index < USB_EP2BANK_SIZE);
623  assert(dtc_queue.reply_index <= USB_EP2IN_SIZE);
624 
625  retval = ERROR_OK;
626 
627  if (dtc_queue.cmd_index < 1)
628  return retval;
629 
630  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = DTC_CMD_STOP;
631 
632  usb_err = dtc_run_download(hdev,
633  dtc_queue.cmd_buffer, dtc_queue.cmd_index,
634  reply_buffer, sizeof(reply_buffer)
635  );
636  if (usb_err < 0) {
637  LOG_ERROR("dtc_run_download: %s", libusb_error_name(usb_err));
638  exit(1);
639  }
640 
641  if (dtc_queue.rq_head) {
642  /* process the reply, which empties the reply queue and frees its entries */
643  dtc_p = reply_buffer;
644 
645  /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the
646  *scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It
647  *was that or craft a function to do the reversal, and that wouldn't work with
648  *bit-stuffing (supplying extra bits to use mostly byte operations), or any other
649  *scheme which would throw the byte alignment off. */
650 
651  for (
652  rq_p = dtc_queue.rq_head;
653  rq_p;
654  rq_p = rq_next
655  ) {
656  tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8);
657  tdo_mask = 1 << (rq_p->scan.offset % 8);
658 
659 
660  bit_cnt = rq_p->scan.length;
661  if (bit_cnt >= 8) {
662  /* bytes */
663 
664  dtc_mask = 1 << (8 - 1);
665 
666  for (
667  ;
668  bit_cnt;
669  bit_cnt--
670  ) {
671  if (*dtc_p & dtc_mask)
672  *tdo_p |= tdo_mask;
673  else
674  *tdo_p &= ~tdo_mask;
675 
676  dtc_mask >>= 1;
677  if (dtc_mask == 0) {
678  dtc_p++;
679  dtc_mask = 1 << (8 - 1);
680  }
681 
682  tdo_mask <<= 1;
683  if (tdo_mask == 0) {
684  tdo_p++;
685  tdo_mask = 1;
686  }
687  }
688  } else {
689  /* extra bits or last bit */
690 
691  x = *dtc_p++;
692  if ((rq_p->scan.type == SCAN_IN) && (
693  rq_p->scan.offset != rq_p->scan.size - 1
694  )) {
695  /* extra bits were sent as a full byte with padding on the
696  *end */
697  dtc_mask = 1 << (8 - 1);
698  } else
699  dtc_mask = 1 << (bit_cnt - 1);
700 
701  for (
702  ;
703  bit_cnt;
704  bit_cnt--
705  ) {
706  if (x & dtc_mask)
707  *tdo_p |= tdo_mask;
708  else
709  *tdo_p &= ~tdo_mask;
710 
711  dtc_mask >>= 1;
712 
713  tdo_mask <<= 1;
714  if (tdo_mask == 0) {
715  tdo_p++;
716  tdo_mask = 1;
717  }
718 
719  }
720  }
721 
722  if ((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
723  /* feed scan buffer back into openocd and free it */
724  if (jtag_read_buffer(rq_p->scan.buffer,
725  rq_p->cmd->cmd.scan) != ERROR_OK)
726  retval = ERROR_JTAG_QUEUE_FAILED;
727  free(rq_p->scan.buffer);
728  }
729 
730  rq_next = rq_p->next;
731  free(rq_p);
732  }
733  dtc_queue.rq_head = NULL;
734  dtc_queue.rq_tail = NULL;
735  }
736 
737  /* reset state for new appends */
738  dtc_queue.cmd_index = 0;
739  dtc_queue.reply_index = 0;
740 
741  return retval;
742 }
743 
744 /* runs the queue if it cannot take reserved_cmd bytes of command data
745  * or reserved_reply bytes of reply data */
746 static int dtc_queue_run_if_full(int reserved_cmd, int reserved_reply)
747 {
748  /* reserve one additional byte for the STOP cmd appended during run */
749  if (dtc_queue.cmd_index + reserved_cmd + 1 > USB_EP2BANK_SIZE)
750  return dtc_queue_run();
751 
752  if (dtc_queue.reply_index + reserved_reply > USB_EP2IN_SIZE)
753  return dtc_queue_run();
754 
755  return ERROR_OK;
756 }
757 
758 static int tap_state_queue_init(void)
759 {
760  tap_state_queue.length = 0;
761  tap_state_queue.buffer = 0;
762  return 0;
763 }
764 
765 static int tap_state_queue_run(void)
766 {
767  int i;
768  int bits;
769  uint8_t byte_param;
770  int retval;
771 
772  retval = 0;
773  if (!tap_state_queue.length)
774  return retval;
775  bits = 1;
776  byte_param = 0;
777  for (i = tap_state_queue.length; i--; ) {
778 
779  byte_param <<= 1;
780  if (tap_state_queue.buffer & 1)
781  byte_param |= 1;
782  if ((bits >= 8) || !i) {
783  byte_param <<= (8 - bits);
784 
785  /* make sure there's room for two cmd bytes */
786  dtc_queue_run_if_full(2, 0);
787 
788 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
789  if (bits == 8) {
790  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
792  } else {
793 #endif
794  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
796 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
797  }
798 #endif
799 
800  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
801  byte_param;
802 
803  byte_param = 0;
804  bits = 1;
805  } else
806  bits++;
807 
808  tap_state_queue.buffer >>= 1;
809  }
810  retval = tap_state_queue_init();
811  return retval;
812 }
813 
814 static int tap_state_queue_append(uint8_t tms)
815 {
816  int retval;
817 
818  if (tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) {
819  retval = tap_state_queue_run();
820  if (retval != 0)
821  return retval;
822  }
823 
824  if (tms)
825  tap_state_queue.buffer |= (1 << tap_state_queue.length);
826  tap_state_queue.length++;
827 
828  return 0;
829 }
830 
831 static void rlink_end_state(enum tap_state state)
832 {
835  else {
836  LOG_ERROR("BUG: %i is not a valid end state", state);
837  exit(-1);
838  }
839 }
840 
841 static void rlink_state_move(void)
842 {
843 
844  int i = 0, tms = 0;
845  uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
847 
848  for (i = 0; i < tms_count; i++) {
849  tms = (tms_scan >> i) & 1;
851  }
852 
854 }
855 
857 {
858  unsigned int num_states = cmd->num_states;
859  int state_count;
860  int tms = 0;
861 
862  state_count = 0;
863  while (num_states) {
864  if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
865  tms = 0;
866  else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
867  tms = 1;
868  else {
869  LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
871  tap_state_name(cmd->path[state_count]));
872  exit(-1);
873  }
874 
876 
877  tap_set_state(cmd->path[state_count]);
878  state_count++;
879  num_states--;
880  }
881 
883 }
884 
885 static void rlink_runtest(unsigned int num_cycles)
886 {
887  enum tap_state saved_end_state = tap_get_end_state();
888 
889  /* only do a state_move when we're not already in RTI */
890  if (tap_get_state() != TAP_IDLE) {
893  }
894 
895  /* execute num_cycles */
896  for (unsigned int i = 0; i < num_cycles; i++)
898 
899  /* finish in end_state */
900  rlink_end_state(saved_end_state);
901  if (tap_get_state() != tap_get_end_state())
903 }
904 
905 /* (1) assert or (0) deassert reset lines */
906 static void rlink_reset(int trst, int srst)
907 {
908  uint8_t bitmap;
909  int usb_err;
910  int transferred;
911 
912  /* Read port A for bit op */
913  usb_err = ep1_generic_commandl(
914  hdev, 4,
916  ST7_PADR >> 8,
917  ST7_PADR,
918  1
919  );
920  if (usb_err < 0) {
921  LOG_ERROR("%s", libusb_error_name(usb_err));
922  exit(1);
923  }
924 
925  usb_err = jtag_libusb_bulk_read(
927  (char *)&bitmap, 1,
929  &transferred
930  );
931  if (usb_err != ERROR_OK || transferred < 1) {
932  LOG_ERROR("%s", libusb_error_name(usb_err));
933  exit(1);
934  }
935 
936  if (trst)
937  bitmap &= ~ST7_PA_NTRST;
938  else
939  bitmap |= ST7_PA_NTRST;
940 
941  /* Write port A and read port B for bit op
942  * port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0
943  *and assert NSRST by setting DDR to 1. */
944  usb_err = ep1_generic_commandl(
945  hdev, 9,
947  ST7_PADR >> 8,
948  ST7_PADR,
949  1,
950  bitmap,
952  ST7_PBDDR >> 8,
953  ST7_PBDDR,
954  1
955  );
956  if (usb_err < 0) {
957  LOG_ERROR("%s", libusb_error_name(usb_err));
958  exit(1);
959  }
960 
961  usb_err = jtag_libusb_bulk_read(
963  (char *)&bitmap, 1,
965  &transferred
966  );
967  if (usb_err != ERROR_OK || transferred < 1) {
968  LOG_ERROR("%s", libusb_error_name(usb_err));
969  exit(1);
970  }
971 
972  if (srst)
973  bitmap |= ST7_PB_NSRST;
974  else
975  bitmap &= ~ST7_PB_NSRST;
976 
977  /* write port B and read dummy to ensure completion before returning */
978  usb_err = ep1_generic_commandl(
979  hdev, 6,
981  ST7_PBDDR >> 8,
982  ST7_PBDDR,
983  1,
984  bitmap,
986  );
987  if (usb_err < 0) {
988  LOG_ERROR("%s", libusb_error_name(usb_err));
989  exit(1);
990  }
991 
992  usb_err = jtag_libusb_bulk_read(
994  (char *)&bitmap, 1,
996  &transferred
997  );
998  if (usb_err != ERROR_OK || transferred < 1) {
999  LOG_ERROR("%s", libusb_error_name(usb_err));
1000  exit(1);
1001  }
1002 }
1003 
1004 static int rlink_scan(struct jtag_command *cmd, enum scan_type type,
1005  uint8_t *buffer, int scan_size)
1006 {
1007  bool ir_scan;
1008  enum tap_state saved_end_state;
1009  int byte_bits;
1010  int extra_bits;
1011  int chunk_bits;
1012  int chunk_bytes;
1013  int x;
1014 
1015  int tdi_bit_offset;
1016  uint8_t tdi_mask, *tdi_p;
1017  uint8_t dtc_mask;
1018 
1019  if (scan_size < 1) {
1020  LOG_ERROR("scan_size cannot be less than 1 bit");
1021  exit(1);
1022  }
1023 
1024  ir_scan = cmd->cmd.scan->ir_scan;
1025 
1026  /* Move to the proper state before starting to shift TDI/TDO. */
1027  if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) ||
1028  (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) {
1029  saved_end_state = tap_get_end_state();
1031  rlink_state_move();
1032  rlink_end_state(saved_end_state);
1033  }
1034 
1036 
1037 
1038 #if 0
1039  printf("scan_size = %d, type = 0x%x\n", scan_size, type);
1040  {
1041  int i;
1042 
1043  /* clear unused bits in scan buffer for ease of debugging
1044  * (it makes diffing output easier) */
1045  buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
1046 
1047  printf("before scan:");
1048  for (i = 0; i < (scan_size + 7) / 8; i++)
1049  printf(" %02x", buffer[i]);
1050  printf("\n");
1051  }
1052 #endif
1053 
1054  /* The number of bits that can be shifted as complete bytes */
1055  byte_bits = (int)(scan_size - 1) / 8 * 8;
1056  /* The number of bits left over, not counting the last bit */
1057  extra_bits = (scan_size - 1) - byte_bits;
1058 
1059  tdi_bit_offset = 0;
1060  tdi_p = buffer;
1061  tdi_mask = 1;
1062 
1063  if (extra_bits && (type == SCAN_OUT)) {
1064  /* Schedule any extra bits into the DTC command buffer, padding as needed
1065  * For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will
1066  *fall off the end */
1067 
1068  /* make sure there's room for two cmd bytes */
1069  dtc_queue_run_if_full(2, 0);
1070 
1071  x = 0;
1072  dtc_mask = 1 << (extra_bits - 1);
1073 
1074  while (extra_bits--) {
1075  if (*tdi_p & tdi_mask)
1076  x |= dtc_mask;
1077 
1078  dtc_mask >>= 1;
1079 
1080  tdi_mask <<= 1;
1081  if (tdi_mask == 0) {
1082  tdi_p++;
1083  tdi_mask = 1;
1084  }
1085  }
1086 
1087  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1089 
1090  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1091  }
1092 
1093  /* Loop scheduling full bytes into the DTC command buffer */
1094  while (byte_bits) {
1095  /* make sure there's room for one (for in scans) or two cmd bytes and
1096  * at least one reply byte for in or inout scans*/
1097  dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, type != SCAN_OUT ? 1 : 0);
1098 
1099  chunk_bits = byte_bits;
1100  /* we can only use up to 16 bytes at a time */
1101  if (chunk_bits > (16 * 8))
1102  chunk_bits = (16 * 8);
1103 
1104  if (type != SCAN_IN) {
1105  /* how much is there room for, considering stop and byte op? */
1106  x = (sizeof(dtc_queue.cmd_buffer) - (dtc_queue.cmd_index + 1 + 1)) * 8;
1107  if (chunk_bits > x)
1108  chunk_bits = x;
1109  }
1110 
1111  if (type != SCAN_OUT) {
1112  /* how much is there room for in the reply buffer? */
1113  x = (USB_EP2IN_SIZE - dtc_queue.reply_index) * 8;
1114  if (chunk_bits > x)
1115  chunk_bits = x;
1116  }
1117 
1118  /* so the loop will end */
1119  byte_bits -= chunk_bits;
1120 
1121  if (type != SCAN_OUT) {
1122  if (!dtc_queue_enqueue_reply(type, buffer, scan_size, tdi_bit_offset,
1123  chunk_bits, cmd)) {
1124  LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1125  exit(1);
1126  }
1127  dtc_queue.reply_index += (chunk_bits + 7) / 8;
1128 
1129  tdi_bit_offset += chunk_bits;
1130  }
1131 
1132  /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1133  chunk_bytes = chunk_bits / 8;
1134 
1135  switch (type) {
1136  case SCAN_IN:
1137  x = DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes);
1138  break;
1139  case SCAN_OUT:
1140  x = DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes);
1141  break;
1142  default:
1143  x = DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes);
1144  break;
1145  }
1146  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1147 
1148  if (type != SCAN_IN) {
1149  x = 0;
1150  dtc_mask = 1 << (8 - 1);
1151 
1152  while (chunk_bits--) {
1153  if (*tdi_p & tdi_mask)
1154  x |= dtc_mask;
1155 
1156  dtc_mask >>= 1;
1157  if (dtc_mask == 0) {
1158  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1159  x = 0;
1160  dtc_mask = 1 << (8 - 1);
1161  }
1162 
1163  tdi_mask <<= 1;
1164  if (tdi_mask == 0) {
1165  tdi_p++;
1166  tdi_mask = 1;
1167  }
1168  }
1169  }
1170  }
1171 
1172  if (extra_bits && (type != SCAN_OUT)) {
1173  /* Schedule any extra bits into the DTC command buffer */
1174 
1175  /* make sure there's room for one (for in scans) or two cmd bytes
1176  * and one reply byte */
1177  dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, 1);
1178 
1179  if (!dtc_queue_enqueue_reply(type, buffer, scan_size, tdi_bit_offset,
1180  extra_bits, cmd)) {
1181  LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1182  exit(1);
1183  }
1184 
1185  dtc_queue.reply_index++;
1186 
1187  tdi_bit_offset += extra_bits;
1188 
1189  if (type == SCAN_IN) {
1190  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1192 
1193  } else {
1194  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1195  DTC_CMD_SHIFT_TDIO_BITS(extra_bits);
1196 
1197  x = 0;
1198  dtc_mask = 1 << (8 - 1);
1199 
1200  while (extra_bits--) {
1201  if (*tdi_p & tdi_mask)
1202  x |= dtc_mask;
1203 
1204  dtc_mask >>= 1;
1205 
1206  tdi_mask <<= 1;
1207  if (tdi_mask == 0) {
1208  tdi_p++;
1209  tdi_mask = 1;
1210  }
1211  }
1212 
1213  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1214  }
1215  }
1216 
1217  /* Schedule the last bit into the DTC command buffer */
1218 
1219  /* make sure there's room for one cmd byte and one reply byte
1220  * for in or inout scans*/
1221  dtc_queue_run_if_full(1, type == SCAN_OUT ? 0 : 1);
1222 
1223  if (type == SCAN_OUT) {
1224  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1225  DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0);
1226 
1227  } else {
1228  if (!dtc_queue_enqueue_reply(type, buffer, scan_size, tdi_bit_offset,
1229  1, cmd)) {
1230  LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1231  exit(1);
1232  }
1233 
1234  dtc_queue.reply_index++;
1235 
1236  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1237  DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1);
1238  }
1239 
1240  /* Move to pause state */
1242  tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1243  if (tap_get_state() != tap_get_end_state())
1244  rlink_state_move();
1245 
1246  return 0;
1247 }
1248 
1249 static int rlink_execute_queue(struct jtag_command *cmd_queue)
1250 {
1251  struct jtag_command *cmd = cmd_queue; /* currently processed command */
1252  int scan_size;
1253  enum scan_type type;
1254  uint8_t *buffer;
1255  int retval, tmp_retval;
1256 
1257  /* return ERROR_OK, unless something goes wrong */
1258  retval = ERROR_OK;
1259 
1260 #ifndef AUTOMATIC_BUSY_LED
1261  /* turn LED on */
1264  ~(ST7_PD_NBUSY_LED)
1265  );
1266 #endif
1267 
1268  while (cmd) {
1269  switch (cmd->type) {
1270  case JTAG_RUNTEST:
1271  case JTAG_TLR_RESET:
1272  case JTAG_PATHMOVE:
1273  case JTAG_SCAN:
1274  break;
1275 
1276  default:
1277  /* some events, such as resets, need a queue flush to ensure
1278  *consistency */
1280  dtc_queue_run();
1281  break;
1282  }
1283 
1284  switch (cmd->type) {
1285  case JTAG_RESET:
1286  LOG_DEBUG_IO("reset trst: %i srst %i",
1287  cmd->cmd.reset->trst,
1288  cmd->cmd.reset->srst);
1289  if (cmd->cmd.reset->trst == 1 ||
1290  (cmd->cmd.reset->srst &&
1293  rlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1294  break;
1295  case JTAG_RUNTEST:
1296  LOG_DEBUG_IO("runtest %i cycles, end in %i",
1297  cmd->cmd.runtest->num_cycles,
1298  cmd->cmd.runtest->end_state);
1299  if (cmd->cmd.runtest->end_state != -1)
1300  rlink_end_state(cmd->cmd.runtest->end_state);
1301  rlink_runtest(cmd->cmd.runtest->num_cycles);
1302  break;
1303  case JTAG_TLR_RESET:
1304  LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
1305  if (cmd->cmd.statemove->end_state != -1)
1306  rlink_end_state(cmd->cmd.statemove->end_state);
1307  rlink_state_move();
1308  break;
1309  case JTAG_PATHMOVE:
1310  LOG_DEBUG_IO("pathmove: %u states, end in %i",
1311  cmd->cmd.pathmove->num_states,
1312  cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1313  rlink_path_move(cmd->cmd.pathmove);
1314  break;
1315  case JTAG_SCAN:
1316  LOG_DEBUG_IO("%s scan end in %i",
1317  (cmd->cmd.scan->ir_scan) ? "IR" : "DR",
1318  cmd->cmd.scan->end_state);
1319  if (cmd->cmd.scan->end_state != -1)
1320  rlink_end_state(cmd->cmd.scan->end_state);
1321  scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1322  type = jtag_scan_type(cmd->cmd.scan);
1323  if (rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK)
1324  retval = ERROR_FAIL;
1325  break;
1326  case JTAG_SLEEP:
1327  LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
1328  jtag_sleep(cmd->cmd.sleep->us);
1329  break;
1330  default:
1331  LOG_ERROR("BUG: unknown JTAG command type encountered");
1332  exit(-1);
1333  }
1334  cmd = cmd->next;
1335  }
1336 
1337  /* Flush the DTC queue to make sure any pending reads have been done before exiting this
1338  *function */
1340  tmp_retval = dtc_queue_run();
1341  if (tmp_retval != ERROR_OK)
1342  retval = tmp_retval;
1343 
1344 #ifndef AUTOMATIC_BUSY_LED
1345  /* turn LED off */
1348  ~0
1349  );
1350 #endif
1351 
1352  return retval;
1353 }
1354 
1355 /* Using an unindexed table because it is infrequently accessed and it is short. The table must be
1356  *in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
1357 
1358 static int rlink_speed(int speed)
1359 {
1360  int i;
1361 
1362  if (speed == 0) {
1363  /* fastest speed */
1365  }
1366 
1367  for (i = rlink_speed_table_size; i--; ) {
1368  if (rlink_speed_table[i].prescaler == speed) {
1370  rlink_speed_table[i].dtc_size) != 0) {
1371  LOG_ERROR(
1372  "An error occurred while trying to load DTC code for speed \"%d\".",
1373  speed);
1374  exit(1);
1375  }
1376 
1377  int ret = dtc_start_download();
1378  if (ret < 0) {
1379  LOG_ERROR("starting DTC: %s", libusb_error_name(ret));
1380  exit(1);
1381  }
1382 
1383  return ERROR_OK;
1384  }
1385  }
1386 
1387  LOG_ERROR("%d is not a supported speed", speed);
1388  return ERROR_FAIL;
1389 }
1390 
1391 static int rlink_speed_div(int speed, int *khz)
1392 {
1393  int i;
1394 
1395  for (i = rlink_speed_table_size; i--; ) {
1396  if (rlink_speed_table[i].prescaler == speed) {
1397  *khz = rlink_speed_table[i].khz;
1398  return ERROR_OK;
1399  }
1400  }
1401 
1402  LOG_ERROR("%d is not a supported speed", speed);
1403  return ERROR_FAIL;
1404 }
1405 
1406 static int rlink_khz(int khz, int *speed)
1407 {
1408  int i;
1409 
1410  if (khz == 0) {
1411  LOG_ERROR("RCLK not supported");
1412  return ERROR_FAIL;
1413  }
1414 
1415  for (i = rlink_speed_table_size; i--; ) {
1416  if (rlink_speed_table[i].khz <= khz) {
1417  *speed = rlink_speed_table[i].prescaler;
1418  return ERROR_OK;
1419  }
1420  }
1421 
1422  LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table[0].khz);
1423  *speed = rlink_speed_table[0].prescaler;
1424  return ERROR_OK;
1425 }
1426 
1427 static int rlink_init(void)
1428 {
1429  int i, j, retries;
1430  uint8_t reply_buffer[USB_EP1IN_SIZE];
1431  int transferred;
1432 
1433  const uint16_t vids[] = { USB_IDVENDOR, 0 };
1434  const uint16_t pids[] = { USB_IDPRODUCT, 0 };
1435  if (jtag_libusb_open(vids, pids, NULL, &hdev, NULL) != ERROR_OK)
1436  return ERROR_FAIL;
1437 
1438  struct libusb_device_descriptor descriptor;
1439  struct libusb_device *usb_dev = libusb_get_device(hdev);
1440  int r = libusb_get_device_descriptor(usb_dev, &descriptor);
1441  if (r < 0) {
1442  LOG_ERROR("error %d getting device descriptor", r);
1443  return ERROR_FAIL;
1444  }
1445 
1446  if (descriptor.bNumConfigurations > 1) {
1447  LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...");
1448  return ERROR_FAIL;
1449  }
1450  struct libusb_config_descriptor *config;
1451  libusb_get_config_descriptor(usb_dev, 0, &config);
1452  if (config->bNumInterfaces > 1) {
1453  LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...");
1454  return ERROR_FAIL;
1455  }
1456 
1457  LOG_DEBUG("Opened device, hdev = %p", hdev);
1458 
1459  /* usb_set_configuration required under win32 */
1460  libusb_set_configuration(hdev, config->bConfigurationValue);
1461 
1462  retries = 3;
1463  do {
1464  i = libusb_claim_interface(hdev, 0);
1465  if (i != LIBUSB_SUCCESS) {
1466  LOG_ERROR("usb_claim_interface: %s", libusb_error_name(i));
1467  j = libusb_detach_kernel_driver(hdev, 0);
1468  if (j != LIBUSB_SUCCESS)
1469  LOG_ERROR("detach kernel driver: %s", libusb_error_name(j));
1470  } else {
1471  LOG_DEBUG("interface claimed!");
1472  break;
1473  }
1474  } while (--retries);
1475 
1476  if (i != LIBUSB_SUCCESS) {
1477  LOG_ERROR("Initialisation failed.");
1478  return ERROR_FAIL;
1479  }
1480  if (libusb_set_interface_alt_setting(hdev, 0, 0) != LIBUSB_SUCCESS) {
1481  LOG_ERROR("Failed to set interface.");
1482  return ERROR_FAIL;
1483  }
1484 
1485  /* The device starts out in an unknown state on open. As such,
1486  * result reads time out, and it's not even known whether the
1487  * command was accepted. So, for this first command, we issue
1488  * it repeatedly until its response doesn't time out. Also, if
1489  * sending a command is going to time out, we find that out here.
1490  *
1491  * It must be possible to open the device in such a way that
1492  * this special magic isn't needed, but, so far, it escapes us.
1493  */
1494  for (i = 0; i < 5; i++) {
1496  hdev, 1,
1498  );
1499  if (j < USB_EP1OUT_SIZE) {
1500  LOG_ERROR("USB write error: %s", libusb_error_name(j));
1501  return ERROR_FAIL;
1502  }
1505  (char *)reply_buffer, sizeof(reply_buffer),
1506  200,
1507  &transferred
1508  );
1509  if (j != LIBUSB_ERROR_TIMEOUT)
1510  break;
1511  }
1512 
1513  if (j != ERROR_OK || transferred != (int)sizeof(reply_buffer)) {
1514  LOG_ERROR("USB read error: %s", libusb_error_name(j));
1515  return ERROR_FAIL;
1516  }
1517  LOG_DEBUG(INTERFACE_NAME " firmware version: %d.%d.%d",
1518  reply_buffer[0],
1519  reply_buffer[1],
1520  reply_buffer[2]);
1521 
1522  if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3))
1523  LOG_WARNING(
1524  "The rlink device is not of the version that the developers have played with. It may or may not work.");
1525 
1526  /* Probe port E for adapter presence */
1528  hdev, 16,
1529  EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 0 */
1530  ST7_PEDR >> 8,
1531  ST7_PEDR,
1532  3,
1533  0x00, /* DR */
1534  ST7_PE_ADAPTER_SENSE_OUT, /* DDR */
1535  ST7_PE_ADAPTER_SENSE_OUT, /* OR */
1536  EP1_CMD_MEMORY_READ, /* Read back */
1537  ST7_PEDR >> 8,
1538  ST7_PEDR,
1539  1,
1540  EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 1 */
1541  ST7_PEDR >> 8,
1542  ST7_PEDR,
1543  1,
1545  );
1546 
1549  (char *)reply_buffer, 1,
1551  &transferred
1552  );
1553 
1554  if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0)
1555  LOG_WARNING("target detection problem");
1556 
1558  hdev, 11,
1559  EP1_CMD_MEMORY_READ, /* Read back */
1560  ST7_PEDR >> 8,
1561  ST7_PEDR,
1562  1,
1563  EP1_CMD_MEMORY_WRITE, /* float port E */
1564  ST7_PEDR >> 8,
1565  ST7_PEDR,
1566  3,
1567  0x00, /* DR */
1568  0x00, /* DDR */
1569  0x00 /* OR */
1570  );
1571 
1574  (char *)reply_buffer, 1,
1576  &transferred
1577  );
1578 
1579 
1580  if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0)
1581  LOG_WARNING("target not plugged in");
1582 
1583  /* float ports A and B */
1585  hdev, 11,
1587  ST7_PADDR >> 8,
1588  ST7_PADDR,
1589  2,
1590  0x00,
1591  0x00,
1593  ST7_PBDDR >> 8,
1594  ST7_PBDDR,
1595  1,
1596  0x00
1597  );
1598 
1599  /* make sure DTC is stopped, set VPP control, set up ports A and B */
1601  hdev, 14,
1604  ~(ST7_PD_VPP_SHDN),
1606  ST7_PADR >> 8,
1607  ST7_PADR,
1608  2,
1609  ((~(0)) & (ST7_PA_NTRST)),
1610  (ST7_PA_NTRST),
1611  /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0
1612  *here and later assert NSRST by setting DDR bit to 1. */
1614  ST7_PBDR >> 8,
1615  ST7_PBDR,
1616  1,
1617  0x00
1618  );
1619 
1620  /* set LED updating mode and make sure they're unlit */
1622  hdev, 3,
1623 #ifdef AUTOMATIC_BUSY_LED
1625 #else
1627 #endif
1629  ~0
1630  );
1631 
1633  dtc_queue_init();
1634  rlink_reset(0, 0);
1635 
1636  return ERROR_OK;
1637 }
1638 
1639 static int rlink_quit(void)
1640 {
1641  /* stop DTC and make sure LEDs are off */
1643  hdev, 6,
1647  ~0,
1649  ~0
1650  );
1651 
1652  libusb_release_interface(hdev, 0);
1653  libusb_close(hdev);
1654 
1655  return ERROR_OK;
1656 }
1657 
1658 static struct jtag_interface rlink_interface = {
1660 };
1661 
1663  .name = "rlink",
1664  .transport_ids = TRANSPORT_JTAG,
1665  .transport_preferred_id = TRANSPORT_JTAG,
1666 
1667  .init = rlink_init,
1668  .quit = rlink_quit,
1669  .speed = rlink_speed,
1670  .khz = rlink_khz,
1671  .speed_div = rlink_speed_div,
1672 
1673  .jtag_ops = &rlink_interface,
1674 };
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_RUNTEST
Definition: commands.h:138
@ JTAG_SLEEP
Definition: commands.h:141
@ JTAG_RESET
Definition: commands.h:139
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint8_t type
Definition: esp_usb_jtag.c:0
enum tap_state tap_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 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_DRPAUSE
Definition: jtag.h:44
@ 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_QUEUE_FAILED
Definition: jtag.h:556
@ 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_bulk_read(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
#define LIBUSB_TIMEOUT_MS
Definition: libusb_helper.h:26
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:102
#define LOG_WARNING(expr ...)
Definition: log.h:130
#define ERROR_FAIL
Definition: log.h:174
#define LOG_ERROR(expr ...)
Definition: log.h:133
#define LOG_DEBUG(expr ...)
Definition: log.h:110
#define ERROR_OK
Definition: log.h:168
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
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
Definition: rlink.c:530
uint8_t * buffer
Definition: rlink.c:535
struct dtc_reply_queue_entry::@35 scan
int offset
Definition: rlink.c:537
int size
Definition: rlink.c:536
enum scan_type type
Definition: rlink.c:539
struct dtc_reply_queue_entry * next
Definition: rlink.c:531
struct jtag_command * cmd
Definition: rlink.c:532
int length
Definition: rlink.c:538
union jtag_command_container cmd
Definition: commands.h:147
Represents a driver for a debugging interface.
Definition: interface.h:183
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
Definition: interface.h:196
#define TRANSPORT_JTAG
Definition: transport.h:19
struct scan_command * scan
Definition: commands.h:113
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22