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(
336  hdev_param,
338  header->length + 1, buffer
339  );
340  if (usb_err < 0)
341  return usb_err;
342 
343  /* Load it into the DTC. */
344  usb_err = ep1_generic_commandl(
345  hdev_param, 3,
347  (DTC_LOAD_BUFFER >> 8),
349  );
350  if (usb_err < 0)
351  return usb_err;
352 
353  break;
354 
355  case DTCLOAD_RUN:
356  usb_err = ep1_generic_commandl(
357  hdev_param, 3,
359  buffer[0],
361  );
362  if (usb_err < 0)
363  return usb_err;
364 
365  break;
366 
367  case DTCLOAD_LUT_START:
368  lut_start = buffer[0];
369  break;
370 
371  case DTCLOAD_LUT:
372  usb_err = ep1_memory_write(
373  hdev_param,
374  ST7_USB_BUF_EP0OUT + lut_start,
375  header->length + 1, buffer
376  );
377  if (usb_err < 0)
378  return usb_err;
379  break;
380 
381  default:
382  LOG_ERROR("Invalid DTC image record type: 0x%02x", header->type);
383  exit(1);
384  break;
385  }
386 
387  buffer += (header->length + 1);
388  length -= (header->length + 1);
389  }
390 
391  return 0;
392 }
393 
394 /*
395  * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
396  */
397 static int dtc_start_download(void)
398 {
399  int usb_err;
400  uint8_t ep2txr;
401  int transferred;
402 
403  /* set up for download mode and make sure EP2 is set up to transmit */
404  usb_err = ep1_generic_commandl(
405  hdev, 7,
406 
410  EP1_CMD_MEMORY_READ, /* read EP2TXR for its data toggle */
411  ST7_EP2TXR >> 8,
412  ST7_EP2TXR,
413  1
414  );
415  if (usb_err < 0)
416  return usb_err;
417 
418  /* read back ep2txr */
419  usb_err = jtag_libusb_bulk_read(
421  (char *)&ep2txr, 1,
423  &transferred
424  );
425  if (usb_err != ERROR_OK)
426  return usb_err;
427 
428  usb_err = ep1_generic_commandl(
429  hdev, 13,
430 
431  EP1_CMD_MEMORY_WRITE, /* preinitialize poll byte */
434  1,
435  0x00,
436  EP1_CMD_MEMORY_WRITE, /* set EP2IN to return data */
437  ST7_EP2TXR >> 8,
438  ST7_EP2TXR,
439  1,
441  EP1_CMD_DTC_CALL, /* start running the DTC */
444  );
445  if (usb_err < 0)
446  return usb_err;
447 
448  /* wait for completion */
449  usb_err = jtag_libusb_bulk_read(
451  (char *)&ep2txr, 1,
453  &transferred
454  );
455 
456  return usb_err;
457 }
458 
459 static int dtc_run_download(
460  struct libusb_device_handle *hdev_param,
461  uint8_t *command_buffer,
462  int command_buffer_size,
463  uint8_t *reply_buffer,
464  int reply_buffer_size
465  )
466 {
467  char dtc_status;
468  int usb_err;
469  int i;
470  int transferred;
471 
472  LOG_DEBUG("%d/%d", command_buffer_size, reply_buffer_size);
473 
474  usb_err = jtag_libusb_bulk_write(
475  hdev_param,
477  (char *)command_buffer, USB_EP2BANK_SIZE,
479  &transferred
480  );
481  if (usb_err < 0)
482  return usb_err;
483 
484 
485  /* Wait for DTC to finish running command buffer */
486  for (i = 50;; ) {
487  usb_err = ep1_generic_commandl(
488  hdev_param, 4,
489 
493  1
494  );
495  if (usb_err < 0)
496  return usb_err;
497 
498  usb_err = jtag_libusb_bulk_read(
499  hdev_param,
501  &dtc_status, 1,
503  &transferred
504  );
505  if (usb_err < 0)
506  return usb_err;
507 
508  if (dtc_status & 0x01)
509  break;
510 
511  if (!--i) {
512  LOG_ERROR("too many retries waiting for DTC status");
513  return LIBUSB_ERROR_TIMEOUT;
514  }
515  }
516 
517 
518  if (reply_buffer && reply_buffer_size) {
519  usb_err = jtag_libusb_bulk_read(
520  hdev_param,
522  (char *)reply_buffer, reply_buffer_size,
524  &transferred
525  );
526 
527  if (usb_err != ERROR_OK || transferred < reply_buffer_size) {
528  LOG_ERROR("Read of endpoint 2 returned %d, expected %d",
529  usb_err, reply_buffer_size
530  );
531  return usb_err;
532  }
533  }
534 
535  return usb_err;
536 }
537 
538 /*
539  * The dtc reply queue is a singly linked list that describes what to do
540  * with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate
541  * these entries.
542  */
543 
546  struct jtag_command *cmd; /* the command that resulted in this entry */
547 
548  struct {
549  uint8_t *buffer; /* the scan buffer */
550  int size; /* size of the scan buffer in bits */
551  int offset; /* how many bits were already done before this? */
552  int length; /* how many bits are processed in this operation? */
553  enum scan_type type; /* SCAN_IN/SCAN_OUT/SCAN_IO */
554  } scan;
555 };
556 
557 
558 /*
559  * The dtc_queue consists of a buffer of pending commands and a reply queue.
560  * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
561  */
562 
563 static struct {
566  uint32_t cmd_index;
567  uint32_t reply_index;
570 
571 /*
572  * The tap state queue is for accumulating TAP state changes without needlessly
573  * flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to
574  * the dtc_queue.
575  */
576 
577 static struct {
578  uint32_t length;
579  uint32_t buffer;
581 
582 static int dtc_queue_init(void)
583 {
584  dtc_queue.rq_head = NULL;
585  dtc_queue.rq_tail = NULL;
586  dtc_queue.cmd_index = 0;
587  dtc_queue.reply_index = 0;
588  return 0;
589 }
590 
592  enum scan_type type, uint8_t *buffer, int size, int offset,
593  int length, struct jtag_command *cmd)
594 {
595  struct dtc_reply_queue_entry *rq_entry;
596 
597  rq_entry = malloc(sizeof(struct dtc_reply_queue_entry));
598  if (rq_entry) {
599  rq_entry->scan.type = type;
600  rq_entry->scan.buffer = buffer;
601  rq_entry->scan.size = size;
602  rq_entry->scan.offset = offset;
603  rq_entry->scan.length = length;
604  rq_entry->cmd = cmd;
605  rq_entry->next = NULL;
606 
607  if (!dtc_queue.rq_head)
608  dtc_queue.rq_head = rq_entry;
609  else
610  dtc_queue.rq_tail->next = rq_entry;
611 
612  dtc_queue.rq_tail = rq_entry;
613  }
614 
615  return rq_entry;
616 }
617 
618 /*
619  * Running the queue means that any pending command buffer is run
620  * and any reply data dealt with. The command buffer is then cleared for subsequent processing.
621  * The queue is automatically run by append when it is necessary to get space for the append.
622  */
623 
624 static int dtc_queue_run(void)
625 {
626  struct dtc_reply_queue_entry *rq_p, *rq_next;
627  int retval;
628  int usb_err;
629  int bit_cnt;
630  int x;
631  uint8_t *dtc_p, *tdo_p;
632  uint8_t dtc_mask, tdo_mask;
633  uint8_t reply_buffer[USB_EP2IN_SIZE];
634 
635  assert((!!dtc_queue.rq_head) == (dtc_queue.reply_index > 0));
636  assert(dtc_queue.cmd_index < USB_EP2BANK_SIZE);
637  assert(dtc_queue.reply_index <= USB_EP2IN_SIZE);
638 
639  retval = ERROR_OK;
640 
641  if (dtc_queue.cmd_index < 1)
642  return retval;
643 
644  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = DTC_CMD_STOP;
645 
646  usb_err = dtc_run_download(hdev,
647  dtc_queue.cmd_buffer, dtc_queue.cmd_index,
648  reply_buffer, sizeof(reply_buffer)
649  );
650  if (usb_err < 0) {
651  LOG_ERROR("dtc_run_download: %s", libusb_error_name(usb_err));
652  exit(1);
653  }
654 
655  if (dtc_queue.rq_head) {
656  /* process the reply, which empties the reply queue and frees its entries */
657  dtc_p = reply_buffer;
658 
659  /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the
660  *scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It
661  *was that or craft a function to do the reversal, and that wouldn't work with
662  *bit-stuffing (supplying extra bits to use mostly byte operations), or any other
663  *scheme which would throw the byte alignment off. */
664 
665  for (
666  rq_p = dtc_queue.rq_head;
667  rq_p;
668  rq_p = rq_next
669  ) {
670  tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8);
671  tdo_mask = 1 << (rq_p->scan.offset % 8);
672 
673 
674  bit_cnt = rq_p->scan.length;
675  if (bit_cnt >= 8) {
676  /* bytes */
677 
678  dtc_mask = 1 << (8 - 1);
679 
680  for (
681  ;
682  bit_cnt;
683  bit_cnt--
684  ) {
685  if (*dtc_p & dtc_mask)
686  *tdo_p |= tdo_mask;
687  else
688  *tdo_p &= ~tdo_mask;
689 
690  dtc_mask >>= 1;
691  if (dtc_mask == 0) {
692  dtc_p++;
693  dtc_mask = 1 << (8 - 1);
694  }
695 
696  tdo_mask <<= 1;
697  if (tdo_mask == 0) {
698  tdo_p++;
699  tdo_mask = 1;
700  }
701  }
702  } else {
703  /* extra bits or last bit */
704 
705  x = *dtc_p++;
706  if ((rq_p->scan.type == SCAN_IN) && (
707  rq_p->scan.offset != rq_p->scan.size - 1
708  )) {
709  /* extra bits were sent as a full byte with padding on the
710  *end */
711  dtc_mask = 1 << (8 - 1);
712  } else
713  dtc_mask = 1 << (bit_cnt - 1);
714 
715  for (
716  ;
717  bit_cnt;
718  bit_cnt--
719  ) {
720  if (x & dtc_mask)
721  *tdo_p |= tdo_mask;
722  else
723  *tdo_p &= ~tdo_mask;
724 
725  dtc_mask >>= 1;
726 
727  tdo_mask <<= 1;
728  if (tdo_mask == 0) {
729  tdo_p++;
730  tdo_mask = 1;
731  }
732 
733  }
734  }
735 
736  if ((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
737  /* feed scan buffer back into openocd and free it */
738  if (jtag_read_buffer(rq_p->scan.buffer,
739  rq_p->cmd->cmd.scan) != ERROR_OK)
740  retval = ERROR_JTAG_QUEUE_FAILED;
741  free(rq_p->scan.buffer);
742  }
743 
744  rq_next = rq_p->next;
745  free(rq_p);
746  }
747  dtc_queue.rq_head = NULL;
748  dtc_queue.rq_tail = NULL;
749  }
750 
751  /* reset state for new appends */
752  dtc_queue.cmd_index = 0;
753  dtc_queue.reply_index = 0;
754 
755  return retval;
756 }
757 
758 /* runs the queue if it cannot take reserved_cmd bytes of command data
759  * or reserved_reply bytes of reply data */
760 static int dtc_queue_run_if_full(int reserved_cmd, int reserved_reply)
761 {
762  /* reserve one additional byte for the STOP cmd appended during run */
763  if (dtc_queue.cmd_index + reserved_cmd + 1 > USB_EP2BANK_SIZE)
764  return dtc_queue_run();
765 
766  if (dtc_queue.reply_index + reserved_reply > USB_EP2IN_SIZE)
767  return dtc_queue_run();
768 
769  return ERROR_OK;
770 }
771 
772 static int tap_state_queue_init(void)
773 {
774  tap_state_queue.length = 0;
775  tap_state_queue.buffer = 0;
776  return 0;
777 }
778 
779 static int tap_state_queue_run(void)
780 {
781  int i;
782  int bits;
783  uint8_t byte_param;
784  int retval;
785 
786  retval = 0;
787  if (!tap_state_queue.length)
788  return retval;
789  bits = 1;
790  byte_param = 0;
791  for (i = tap_state_queue.length; i--; ) {
792 
793  byte_param <<= 1;
794  if (tap_state_queue.buffer & 1)
795  byte_param |= 1;
796  if ((bits >= 8) || !i) {
797  byte_param <<= (8 - bits);
798 
799  /* make sure there's room for two cmd bytes */
800  dtc_queue_run_if_full(2, 0);
801 
802 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
803  if (bits == 8) {
804  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
806  } else {
807 #endif
808  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
810 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
811  }
812 #endif
813 
814  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
815  byte_param;
816 
817  byte_param = 0;
818  bits = 1;
819  } else
820  bits++;
821 
822  tap_state_queue.buffer >>= 1;
823  }
824  retval = tap_state_queue_init();
825  return retval;
826 }
827 
828 static int tap_state_queue_append(uint8_t tms)
829 {
830  int retval;
831 
832  if (tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) {
833  retval = tap_state_queue_run();
834  if (retval != 0)
835  return retval;
836  }
837 
838  if (tms)
839  tap_state_queue.buffer |= (1 << tap_state_queue.length);
840  tap_state_queue.length++;
841 
842  return 0;
843 }
844 
846 {
849  else {
850  LOG_ERROR("BUG: %i is not a valid end state", state);
851  exit(-1);
852  }
853 }
854 
855 static void rlink_state_move(void)
856 {
857 
858  int i = 0, tms = 0;
859  uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
861 
862  for (i = 0; i < tms_count; i++) {
863  tms = (tms_scan >> i) & 1;
865  }
866 
868 }
869 
871 {
872  unsigned int num_states = cmd->num_states;
873  int state_count;
874  int tms = 0;
875 
876  state_count = 0;
877  while (num_states) {
878  if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
879  tms = 0;
880  else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
881  tms = 1;
882  else {
883  LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
885  tap_state_name(cmd->path[state_count]));
886  exit(-1);
887  }
888 
890 
891  tap_set_state(cmd->path[state_count]);
892  state_count++;
893  num_states--;
894  }
895 
897 }
898 
899 static void rlink_runtest(unsigned int num_cycles)
900 {
901  tap_state_t saved_end_state = tap_get_end_state();
902 
903  /* only do a state_move when we're not already in RTI */
904  if (tap_get_state() != TAP_IDLE) {
907  }
908 
909  /* execute num_cycles */
910  for (unsigned int i = 0; i < num_cycles; i++)
912 
913  /* finish in end_state */
914  rlink_end_state(saved_end_state);
915  if (tap_get_state() != tap_get_end_state())
917 }
918 
919 /* (1) assert or (0) deassert reset lines */
920 static void rlink_reset(int trst, int srst)
921 {
922  uint8_t bitmap;
923  int usb_err;
924  int transferred;
925 
926  /* Read port A for bit op */
927  usb_err = ep1_generic_commandl(
928  hdev, 4,
930  ST7_PADR >> 8,
931  ST7_PADR,
932  1
933  );
934  if (usb_err < 0) {
935  LOG_ERROR("%s", libusb_error_name(usb_err));
936  exit(1);
937  }
938 
939  usb_err = jtag_libusb_bulk_read(
941  (char *)&bitmap, 1,
943  &transferred
944  );
945  if (usb_err != ERROR_OK || transferred < 1) {
946  LOG_ERROR("%s", libusb_error_name(usb_err));
947  exit(1);
948  }
949 
950  if (trst)
951  bitmap &= ~ST7_PA_NTRST;
952  else
953  bitmap |= ST7_PA_NTRST;
954 
955  /* Write port A and read port B for bit op
956  * port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0
957  *and assert NSRST by setting DDR to 1. */
958  usb_err = ep1_generic_commandl(
959  hdev, 9,
961  ST7_PADR >> 8,
962  ST7_PADR,
963  1,
964  bitmap,
966  ST7_PBDDR >> 8,
967  ST7_PBDDR,
968  1
969  );
970  if (usb_err < 0) {
971  LOG_ERROR("%s", libusb_error_name(usb_err));
972  exit(1);
973  }
974 
975  usb_err = jtag_libusb_bulk_read(
977  (char *)&bitmap, 1,
979  &transferred
980  );
981  if (usb_err != ERROR_OK || transferred < 1) {
982  LOG_ERROR("%s", libusb_error_name(usb_err));
983  exit(1);
984  }
985 
986  if (srst)
987  bitmap |= ST7_PB_NSRST;
988  else
989  bitmap &= ~ST7_PB_NSRST;
990 
991  /* write port B and read dummy to ensure completion before returning */
992  usb_err = ep1_generic_commandl(
993  hdev, 6,
995  ST7_PBDDR >> 8,
996  ST7_PBDDR,
997  1,
998  bitmap,
1000  );
1001  if (usb_err < 0) {
1002  LOG_ERROR("%s", libusb_error_name(usb_err));
1003  exit(1);
1004  }
1005 
1006  usb_err = jtag_libusb_bulk_read(
1008  (char *)&bitmap, 1,
1010  &transferred
1011  );
1012  if (usb_err != ERROR_OK || transferred < 1) {
1013  LOG_ERROR("%s", libusb_error_name(usb_err));
1014  exit(1);
1015  }
1016 }
1017 
1018 static int rlink_scan(struct jtag_command *cmd, enum scan_type type,
1019  uint8_t *buffer, int scan_size)
1020 {
1021  bool ir_scan;
1022  tap_state_t saved_end_state;
1023  int byte_bits;
1024  int extra_bits;
1025  int chunk_bits;
1026  int chunk_bytes;
1027  int x;
1028 
1029  int tdi_bit_offset;
1030  uint8_t tdi_mask, *tdi_p;
1031  uint8_t dtc_mask;
1032 
1033  if (scan_size < 1) {
1034  LOG_ERROR("scan_size cannot be less than 1 bit");
1035  exit(1);
1036  }
1037 
1038  ir_scan = cmd->cmd.scan->ir_scan;
1039 
1040  /* Move to the proper state before starting to shift TDI/TDO. */
1041  if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) ||
1042  (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) {
1043  saved_end_state = tap_get_end_state();
1045  rlink_state_move();
1046  rlink_end_state(saved_end_state);
1047  }
1048 
1050 
1051 
1052 #if 0
1053  printf("scan_size = %d, type = 0x%x\n", scan_size, type);
1054  {
1055  int i;
1056 
1057  /* clear unused bits in scan buffer for ease of debugging
1058  * (it makes diffing output easier) */
1059  buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
1060 
1061  printf("before scan:");
1062  for (i = 0; i < (scan_size + 7) / 8; i++)
1063  printf(" %02x", buffer[i]);
1064  printf("\n");
1065  }
1066 #endif
1067 
1068  /* The number of bits that can be shifted as complete bytes */
1069  byte_bits = (int)(scan_size - 1) / 8 * 8;
1070  /* The number of bits left over, not counting the last bit */
1071  extra_bits = (scan_size - 1) - byte_bits;
1072 
1073  tdi_bit_offset = 0;
1074  tdi_p = buffer;
1075  tdi_mask = 1;
1076 
1077  if (extra_bits && (type == SCAN_OUT)) {
1078  /* Schedule any extra bits into the DTC command buffer, padding as needed
1079  * For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will
1080  *fall off the end */
1081 
1082  /* make sure there's room for two cmd bytes */
1083  dtc_queue_run_if_full(2, 0);
1084 
1085  x = 0;
1086  dtc_mask = 1 << (extra_bits - 1);
1087 
1088  while (extra_bits--) {
1089  if (*tdi_p & tdi_mask)
1090  x |= dtc_mask;
1091 
1092  dtc_mask >>= 1;
1093 
1094  tdi_mask <<= 1;
1095  if (tdi_mask == 0) {
1096  tdi_p++;
1097  tdi_mask = 1;
1098  }
1099  }
1100 
1101  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1103 
1104  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1105  }
1106 
1107  /* Loop scheduling full bytes into the DTC command buffer */
1108  while (byte_bits) {
1109  /* make sure there's room for one (for in scans) or two cmd bytes and
1110  * at least one reply byte for in or inout scans*/
1111  dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, type != SCAN_OUT ? 1 : 0);
1112 
1113  chunk_bits = byte_bits;
1114  /* we can only use up to 16 bytes at a time */
1115  if (chunk_bits > (16 * 8))
1116  chunk_bits = (16 * 8);
1117 
1118  if (type != SCAN_IN) {
1119  /* how much is there room for, considering stop and byte op? */
1120  x = (sizeof(dtc_queue.cmd_buffer) - (dtc_queue.cmd_index + 1 + 1)) * 8;
1121  if (chunk_bits > x)
1122  chunk_bits = x;
1123  }
1124 
1125  if (type != SCAN_OUT) {
1126  /* how much is there room for in the reply buffer? */
1127  x = (USB_EP2IN_SIZE - dtc_queue.reply_index) * 8;
1128  if (chunk_bits > x)
1129  chunk_bits = x;
1130  }
1131 
1132  /* so the loop will end */
1133  byte_bits -= chunk_bits;
1134 
1135  if (type != SCAN_OUT) {
1136  if (!dtc_queue_enqueue_reply(type, buffer, scan_size, tdi_bit_offset,
1137  chunk_bits, cmd)) {
1138  LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1139  exit(1);
1140  }
1141  dtc_queue.reply_index += (chunk_bits + 7) / 8;
1142 
1143  tdi_bit_offset += chunk_bits;
1144  }
1145 
1146  /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1147  chunk_bytes = chunk_bits / 8;
1148 
1149  switch (type) {
1150  case SCAN_IN:
1151  x = DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes);
1152  break;
1153  case SCAN_OUT:
1154  x = DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes);
1155  break;
1156  default:
1157  x = DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes);
1158  break;
1159  }
1160  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1161 
1162  if (type != SCAN_IN) {
1163  x = 0;
1164  dtc_mask = 1 << (8 - 1);
1165 
1166  while (chunk_bits--) {
1167  if (*tdi_p & tdi_mask)
1168  x |= dtc_mask;
1169 
1170  dtc_mask >>= 1;
1171  if (dtc_mask == 0) {
1172  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1173  x = 0;
1174  dtc_mask = 1 << (8 - 1);
1175  }
1176 
1177  tdi_mask <<= 1;
1178  if (tdi_mask == 0) {
1179  tdi_p++;
1180  tdi_mask = 1;
1181  }
1182  }
1183  }
1184  }
1185 
1186  if (extra_bits && (type != SCAN_OUT)) {
1187  /* Schedule any extra bits into the DTC command buffer */
1188 
1189  /* make sure there's room for one (for in scans) or two cmd bytes
1190  * and one reply byte */
1191  dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, 1);
1192 
1193  if (!dtc_queue_enqueue_reply(type, buffer, scan_size, tdi_bit_offset,
1194  extra_bits, cmd)) {
1195  LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1196  exit(1);
1197  }
1198 
1199  dtc_queue.reply_index++;
1200 
1201  tdi_bit_offset += extra_bits;
1202 
1203  if (type == SCAN_IN) {
1204  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1206 
1207  } else {
1208  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1209  DTC_CMD_SHIFT_TDIO_BITS(extra_bits);
1210 
1211  x = 0;
1212  dtc_mask = 1 << (8 - 1);
1213 
1214  while (extra_bits--) {
1215  if (*tdi_p & tdi_mask)
1216  x |= dtc_mask;
1217 
1218  dtc_mask >>= 1;
1219 
1220  tdi_mask <<= 1;
1221  if (tdi_mask == 0) {
1222  tdi_p++;
1223  tdi_mask = 1;
1224  }
1225  }
1226 
1227  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1228  }
1229  }
1230 
1231  /* Schedule the last bit into the DTC command buffer */
1232 
1233  /* make sure there's room for one cmd byte and one reply byte
1234  * for in or inout scans*/
1235  dtc_queue_run_if_full(1, type == SCAN_OUT ? 0 : 1);
1236 
1237  if (type == SCAN_OUT) {
1238  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1239  DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0);
1240 
1241  } else {
1242  if (!dtc_queue_enqueue_reply(type, buffer, scan_size, tdi_bit_offset,
1243  1, cmd)) {
1244  LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1245  exit(1);
1246  }
1247 
1248  dtc_queue.reply_index++;
1249 
1250  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1251  DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1);
1252  }
1253 
1254  /* Move to pause state */
1256  tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1257  if (tap_get_state() != tap_get_end_state())
1258  rlink_state_move();
1259 
1260  return 0;
1261 }
1262 
1263 static int rlink_execute_queue(struct jtag_command *cmd_queue)
1264 {
1265  struct jtag_command *cmd = cmd_queue; /* currently processed command */
1266  int scan_size;
1267  enum scan_type type;
1268  uint8_t *buffer;
1269  int retval, tmp_retval;
1270 
1271  /* return ERROR_OK, unless something goes wrong */
1272  retval = ERROR_OK;
1273 
1274 #ifndef AUTOMATIC_BUSY_LED
1275  /* turn LED on */
1278  ~(ST7_PD_NBUSY_LED)
1279  );
1280 #endif
1281 
1282  while (cmd) {
1283  switch (cmd->type) {
1284  case JTAG_RUNTEST:
1285  case JTAG_TLR_RESET:
1286  case JTAG_PATHMOVE:
1287  case JTAG_SCAN:
1288  break;
1289 
1290  default:
1291  /* some events, such as resets, need a queue flush to ensure
1292  *consistency */
1294  dtc_queue_run();
1295  break;
1296  }
1297 
1298  switch (cmd->type) {
1299  case JTAG_RESET:
1300  LOG_DEBUG_IO("reset trst: %i srst %i",
1301  cmd->cmd.reset->trst,
1302  cmd->cmd.reset->srst);
1303  if ((cmd->cmd.reset->trst == 1) ||
1304  (cmd->cmd.reset->srst &&
1307  rlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1308  break;
1309  case JTAG_RUNTEST:
1310  LOG_DEBUG_IO("runtest %i cycles, end in %i",
1311  cmd->cmd.runtest->num_cycles,
1312  cmd->cmd.runtest->end_state);
1313  if (cmd->cmd.runtest->end_state != -1)
1314  rlink_end_state(cmd->cmd.runtest->end_state);
1315  rlink_runtest(cmd->cmd.runtest->num_cycles);
1316  break;
1317  case JTAG_TLR_RESET:
1318  LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
1319  if (cmd->cmd.statemove->end_state != -1)
1320  rlink_end_state(cmd->cmd.statemove->end_state);
1321  rlink_state_move();
1322  break;
1323  case JTAG_PATHMOVE:
1324  LOG_DEBUG_IO("pathmove: %u states, end in %i",
1325  cmd->cmd.pathmove->num_states,
1326  cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1327  rlink_path_move(cmd->cmd.pathmove);
1328  break;
1329  case JTAG_SCAN:
1330  LOG_DEBUG_IO("%s scan end in %i",
1331  (cmd->cmd.scan->ir_scan) ? "IR" : "DR",
1332  cmd->cmd.scan->end_state);
1333  if (cmd->cmd.scan->end_state != -1)
1334  rlink_end_state(cmd->cmd.scan->end_state);
1335  scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1336  type = jtag_scan_type(cmd->cmd.scan);
1337  if (rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK)
1338  retval = ERROR_FAIL;
1339  break;
1340  case JTAG_SLEEP:
1341  LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
1342  jtag_sleep(cmd->cmd.sleep->us);
1343  break;
1344  default:
1345  LOG_ERROR("BUG: unknown JTAG command type encountered");
1346  exit(-1);
1347  }
1348  cmd = cmd->next;
1349  }
1350 
1351  /* Flush the DTC queue to make sure any pending reads have been done before exiting this
1352  *function */
1354  tmp_retval = dtc_queue_run();
1355  if (tmp_retval != ERROR_OK)
1356  retval = tmp_retval;
1357 
1358 #ifndef AUTOMATIC_BUSY_LED
1359  /* turn LED off */
1362  ~0
1363  );
1364 #endif
1365 
1366  return retval;
1367 }
1368 
1369 /* Using an unindexed table because it is infrequently accessed and it is short. The table must be
1370  *in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
1371 
1372 static int rlink_speed(int speed)
1373 {
1374  int i;
1375 
1376  if (speed == 0) {
1377  /* fastest speed */
1379  }
1380 
1381  for (i = rlink_speed_table_size; i--; ) {
1382  if (rlink_speed_table[i].prescaler == speed) {
1384  rlink_speed_table[i].dtc_size) != 0) {
1385  LOG_ERROR(
1386  "An error occurred while trying to load DTC code for speed \"%d\".",
1387  speed);
1388  exit(1);
1389  }
1390 
1391  int ret = dtc_start_download();
1392  if (ret < 0) {
1393  LOG_ERROR("starting DTC: %s", libusb_error_name(ret));
1394  exit(1);
1395  }
1396 
1397  return ERROR_OK;
1398  }
1399  }
1400 
1401  LOG_ERROR("%d is not a supported speed", speed);
1402  return ERROR_FAIL;
1403 }
1404 
1405 static int rlink_speed_div(int speed, int *khz)
1406 {
1407  int i;
1408 
1409  for (i = rlink_speed_table_size; i--; ) {
1410  if (rlink_speed_table[i].prescaler == speed) {
1411  *khz = rlink_speed_table[i].khz;
1412  return ERROR_OK;
1413  }
1414  }
1415 
1416  LOG_ERROR("%d is not a supported speed", speed);
1417  return ERROR_FAIL;
1418 }
1419 
1420 static int rlink_khz(int khz, int *speed)
1421 {
1422  int i;
1423 
1424  if (khz == 0) {
1425  LOG_ERROR("RCLK not supported");
1426  return ERROR_FAIL;
1427  }
1428 
1429  for (i = rlink_speed_table_size; i--; ) {
1430  if (rlink_speed_table[i].khz <= khz) {
1431  *speed = rlink_speed_table[i].prescaler;
1432  return ERROR_OK;
1433  }
1434  }
1435 
1436  LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table[0].khz);
1437  *speed = rlink_speed_table[0].prescaler;
1438  return ERROR_OK;
1439 }
1440 
1441 static int rlink_init(void)
1442 {
1443  int i, j, retries;
1444  uint8_t reply_buffer[USB_EP1IN_SIZE];
1445  int transferred;
1446 
1447  const uint16_t vids[] = { USB_IDVENDOR, 0 };
1448  const uint16_t pids[] = { USB_IDPRODUCT, 0 };
1449  if (jtag_libusb_open(vids, pids, NULL, &hdev, NULL) != ERROR_OK)
1450  return ERROR_FAIL;
1451 
1452  struct libusb_device_descriptor descriptor;
1453  struct libusb_device *usb_dev = libusb_get_device(hdev);
1454  int r = libusb_get_device_descriptor(usb_dev, &descriptor);
1455  if (r < 0) {
1456  LOG_ERROR("error %d getting device descriptor", r);
1457  return ERROR_FAIL;
1458  }
1459 
1460  if (descriptor.bNumConfigurations > 1) {
1461  LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...");
1462  return ERROR_FAIL;
1463  }
1464  struct libusb_config_descriptor *config;
1465  libusb_get_config_descriptor(usb_dev, 0, &config);
1466  if (config->bNumInterfaces > 1) {
1467  LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...");
1468  return ERROR_FAIL;
1469  }
1470 
1471  LOG_DEBUG("Opened device, hdev = %p", hdev);
1472 
1473  /* usb_set_configuration required under win32 */
1474  libusb_set_configuration(hdev, config->bConfigurationValue);
1475 
1476  retries = 3;
1477  do {
1478  i = libusb_claim_interface(hdev, 0);
1479  if (i != LIBUSB_SUCCESS) {
1480  LOG_ERROR("usb_claim_interface: %s", libusb_error_name(i));
1481  j = libusb_detach_kernel_driver(hdev, 0);
1482  if (j != LIBUSB_SUCCESS)
1483  LOG_ERROR("detach kernel driver: %s", libusb_error_name(j));
1484  } else {
1485  LOG_DEBUG("interface claimed!");
1486  break;
1487  }
1488  } while (--retries);
1489 
1490  if (i != LIBUSB_SUCCESS) {
1491  LOG_ERROR("Initialisation failed.");
1492  return ERROR_FAIL;
1493  }
1494  if (libusb_set_interface_alt_setting(hdev, 0, 0) != LIBUSB_SUCCESS) {
1495  LOG_ERROR("Failed to set interface.");
1496  return ERROR_FAIL;
1497  }
1498 
1499  /* The device starts out in an unknown state on open. As such,
1500  * result reads time out, and it's not even known whether the
1501  * command was accepted. So, for this first command, we issue
1502  * it repeatedly until its response doesn't time out. Also, if
1503  * sending a command is going to time out, we find that out here.
1504  *
1505  * It must be possible to open the device in such a way that
1506  * this special magic isn't needed, but, so far, it escapes us.
1507  */
1508  for (i = 0; i < 5; i++) {
1510  hdev, 1,
1512  );
1513  if (j < USB_EP1OUT_SIZE) {
1514  LOG_ERROR("USB write error: %s", libusb_error_name(j));
1515  return ERROR_FAIL;
1516  }
1519  (char *)reply_buffer, sizeof(reply_buffer),
1520  200,
1521  &transferred
1522  );
1523  if (j != LIBUSB_ERROR_TIMEOUT)
1524  break;
1525  }
1526 
1527  if (j != ERROR_OK || transferred != (int)sizeof(reply_buffer)) {
1528  LOG_ERROR("USB read error: %s", libusb_error_name(j));
1529  return ERROR_FAIL;
1530  }
1531  LOG_DEBUG(INTERFACE_NAME " firmware version: %d.%d.%d",
1532  reply_buffer[0],
1533  reply_buffer[1],
1534  reply_buffer[2]);
1535 
1536  if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3))
1537  LOG_WARNING(
1538  "The rlink device is not of the version that the developers have played with. It may or may not work.");
1539 
1540  /* Probe port E for adapter presence */
1542  hdev, 16,
1543  EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 0 */
1544  ST7_PEDR >> 8,
1545  ST7_PEDR,
1546  3,
1547  0x00, /* DR */
1548  ST7_PE_ADAPTER_SENSE_OUT, /* DDR */
1549  ST7_PE_ADAPTER_SENSE_OUT, /* OR */
1550  EP1_CMD_MEMORY_READ, /* Read back */
1551  ST7_PEDR >> 8,
1552  ST7_PEDR,
1553  1,
1554  EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 1 */
1555  ST7_PEDR >> 8,
1556  ST7_PEDR,
1557  1,
1559  );
1560 
1563  (char *)reply_buffer, 1,
1565  &transferred
1566  );
1567 
1568  if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0)
1569  LOG_WARNING("target detection problem");
1570 
1572  hdev, 11,
1573  EP1_CMD_MEMORY_READ, /* Read back */
1574  ST7_PEDR >> 8,
1575  ST7_PEDR,
1576  1,
1577  EP1_CMD_MEMORY_WRITE, /* float port E */
1578  ST7_PEDR >> 8,
1579  ST7_PEDR,
1580  3,
1581  0x00, /* DR */
1582  0x00, /* DDR */
1583  0x00 /* OR */
1584  );
1585 
1588  (char *)reply_buffer, 1,
1590  &transferred
1591  );
1592 
1593 
1594  if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0)
1595  LOG_WARNING("target not plugged in");
1596 
1597  /* float ports A and B */
1599  hdev, 11,
1601  ST7_PADDR >> 8,
1602  ST7_PADDR,
1603  2,
1604  0x00,
1605  0x00,
1607  ST7_PBDDR >> 8,
1608  ST7_PBDDR,
1609  1,
1610  0x00
1611  );
1612 
1613  /* make sure DTC is stopped, set VPP control, set up ports A and B */
1615  hdev, 14,
1618  ~(ST7_PD_VPP_SHDN),
1620  ST7_PADR >> 8,
1621  ST7_PADR,
1622  2,
1623  ((~(0)) & (ST7_PA_NTRST)),
1624  (ST7_PA_NTRST),
1625  /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0
1626  *here and later assert NSRST by setting DDR bit to 1. */
1628  ST7_PBDR >> 8,
1629  ST7_PBDR,
1630  1,
1631  0x00
1632  );
1633 
1634  /* set LED updating mode and make sure they're unlit */
1636  hdev, 3,
1637 #ifdef AUTOMATIC_BUSY_LED
1639 #else
1641 #endif
1643  ~0
1644  );
1645 
1647  dtc_queue_init();
1648  rlink_reset(0, 0);
1649 
1650  return ERROR_OK;
1651 }
1652 
1653 static int rlink_quit(void)
1654 {
1655  /* stop DTC and make sure LEDs are off */
1657  hdev, 6,
1661  ~0,
1663  ~0
1664  );
1665 
1666  libusb_release_interface(hdev, 0);
1667  libusb_close(hdev);
1668 
1669  return ERROR_OK;
1670 }
1671 
1672 static struct jtag_interface rlink_interface = {
1674 };
1675 
1677  .name = "rlink",
1678  .transports = jtag_only,
1679 
1680  .init = rlink_init,
1681  .quit = rlink_quit,
1682  .speed = rlink_speed,
1683  .khz = rlink_khz,
1684  .speed_div = rlink_speed_div,
1685 
1686  .jtag_ops = &rlink_interface,
1687 };
const char *const jtag_only[]
Definition: adapter.c:27
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
uint8_t type
Definition: esp_usb_jtag.c:0
bool tap_is_state_stable(tap_state_t astate)
Function tap_is_state_stable returns true if the astate is stable.
Definition: interface.c:200
tap_state_t tap_state_transition(tap_state_t cur_state, bool tms)
Function tap_state_transition takes a current TAP state and returns the next state according to the t...
Definition: interface.c:223
const char * tap_state_name(tap_state_t state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
Definition: interface.c:344
void tap_set_end_state(tap_state_t 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
tap_state_t tap_get_end_state(void)
For more information,.
Definition: interface.c:56
int tap_get_tms_path(tap_state_t from, tap_state_t to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
Definition: interface.c:190
int tap_get_tms_path_len(tap_state_t from, tap_state_t to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
Definition: interface.c:195
tap_state_t tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
Definition: interface.c:37
#define tap_set_state(new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
Definition: interface.h:49
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1068
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1740
@ 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
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
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:101
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
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
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
Represents a driver for a debugging interface.
Definition: interface.h:207
const char *const name
The name of the interface driver.
Definition: interface.h:209
Definition: rlink.c:544
uint8_t * buffer
Definition: rlink.c:549
int offset
Definition: rlink.c:551
int size
Definition: rlink.c:550
enum scan_type type
Definition: rlink.c:553
struct dtc_reply_queue_entry * next
Definition: rlink.c:545
struct jtag_command * cmd
Definition: rlink.c:546
struct dtc_reply_queue_entry::@32 scan
int length
Definition: rlink.c:552
union jtag_command_container cmd
Definition: commands.h:147
Represents a driver for a debugging interface.
Definition: interface.h:182
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
Definition: interface.h:195
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