1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 Rob Brown, Lou Deluxe *
9 * rob@cobbleware.com, lou.openocd012@fixit.nospammail.net *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
29 /* project specific includes */
30 #include <jtag/interface.h>
31 #include <jtag/commands.h>
33 #include "rlink_st7.h"
34 #include "rlink_ep1_cmd.h"
35 #include "rlink_dtc_cmd.h"
36 #include "usb_common.h"
38 /* This feature is made useless by running the DTC all the time. When automatic, the LED is on
39 *whenever the DTC is running. Otherwise, USB messages are sent to turn it on and off. */
40 #undef AUTOMATIC_BUSY_LED
42 /* This feature may require derating the speed due to reduced hold time. */
43 #undef USE_HARDWARE_SHIFTER_FOR_TMS
45 #define INTERFACE_NAME "RLink"
47 #define USB_IDVENDOR (0x138e)
48 #define USB_IDPRODUCT (0x9000)
50 #define USB_EP1OUT_ADDR (0x01)
51 #define USB_EP1OUT_SIZE (16)
52 #define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80)
53 #define USB_EP1IN_SIZE (USB_EP1OUT_SIZE)
55 #define USB_EP2OUT_ADDR (0x02)
56 #define USB_EP2OUT_SIZE (64)
57 #define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80)
58 #define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
59 #define USB_EP2BANK_SIZE (512)
61 #define USB_TIMEOUT_MS (3 * 1000)
63 #define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
65 #define ST7_PD_NBUSY_LED ST7_PD0
66 #define ST7_PD_NRUN_LED ST7_PD1
67 /* low enables VPP at adapter header, high connects it to GND instead */
68 #define ST7_PD_VPP_SEL ST7_PD6
69 /* low: VPP = 12v, high: VPP <= 5v */
70 #define ST7_PD_VPP_SHDN ST7_PD7
72 /* These pins are connected together */
73 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
74 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
76 /* Symbolic mapping between port pins and numbered IO lines */
77 #define ST7_PA_IO1 ST7_PA1
78 #define ST7_PA_IO2 ST7_PA2
79 #define ST7_PA_IO4 ST7_PA4
80 #define ST7_PA_IO8 ST7_PA6
81 #define ST7_PA_IO10 ST7_PA7
82 #define ST7_PB_IO5 ST7_PB5
83 #define ST7_PC_IO9 ST7_PC1
84 #define ST7_PC_IO3 ST7_PC2
85 #define ST7_PC_IO7 ST7_PC3
86 #define ST7_PE_IO6 ST7_PE5
88 /* Symbolic mapping between numbered IO lines and adapter signals */
89 #define ST7_PA_RTCK ST7_PA_IO0
90 #define ST7_PA_NTRST ST7_PA_IO1
91 #define ST7_PC_TDI ST7_PC_IO3
92 #define ST7_PA_DBGRQ ST7_PA_IO4
93 #define ST7_PB_NSRST ST7_PB_IO5
94 #define ST7_PE_TMS ST7_PE_IO6
95 #define ST7_PC_TCK ST7_PC_IO7
96 #define ST7_PC_TDO ST7_PC_IO9
97 #define ST7_PA_DBGACK ST7_PA_IO10
99 static usb_dev_handle
*pHDev
;
102 * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
103 * This function takes care of zeroing the unused bytes before sending the packet.
104 * Any reply packet is not handled by this function.
106 static int ep1_generic_commandl(usb_dev_handle
*pHDev_param
, size_t length
, ...)
108 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
109 uint8_t *usb_buffer_p
;
113 if (length
> sizeof(usb_buffer
))
114 length
= sizeof(usb_buffer
);
116 usb_buffer_p
= usb_buffer
;
118 va_start(ap
, length
);
120 *usb_buffer_p
++ = va_arg(ap
, int);
127 sizeof(usb_buffer
) - (usb_buffer_p
- usb_buffer
)
130 usb_ret
= usb_bulk_write(
133 (char *)usb_buffer
, sizeof(usb_buffer
),
141 static ssize_t
ep1_memory_read(
142 usb_dev_handle
*pHDev
, uint16_t addr
,
143 size_t length
, uint8_t *buffer
)
145 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
150 usb_buffer
[0] = EP1_CMD_MEMORY_READ
;
154 sizeof(usb_buffer
) - 4
161 if (remain
> sizeof(usb_buffer
))
162 length
= sizeof(usb_buffer
);
166 usb_buffer
[1] = addr
>> 8;
167 usb_buffer
[2] = addr
;
168 usb_buffer
[3] = length
;
170 usb_ret
= usb_bulk_write(
171 pHDev
, USB_EP1OUT_ADDR
,
172 usb_buffer
, sizeof(usb_buffer
),
176 if (usb_ret
< sizeof(usb_buffer
))
179 usb_ret
= usb_bulk_read(
180 pHDev
, USB_EP1IN_ADDR
,
185 if (usb_ret
< length
)
198 static ssize_t
ep1_memory_write(usb_dev_handle
*pHDev_param
, uint16_t addr
,
199 size_t length
, uint8_t const *buffer
)
201 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
206 usb_buffer
[0] = EP1_CMD_MEMORY_WRITE
;
212 if (remain
> (sizeof(usb_buffer
) - 4))
213 length
= (sizeof(usb_buffer
) - 4);
217 usb_buffer
[1] = addr
>> 8;
218 usb_buffer
[2] = addr
;
219 usb_buffer
[3] = length
;
226 usb_buffer
+ 4 + length
,
228 sizeof(usb_buffer
) - 4 - length
231 usb_ret
= usb_bulk_write(
232 pHDev_param
, USB_EP1OUT_ADDR
,
233 (char *)usb_buffer
, sizeof(usb_buffer
),
237 if ((size_t)usb_ret
< sizeof(usb_buffer
))
251 static ssize_t
ep1_memory_writel(usb_dev_handle
*pHDev
, uint16_t addr
,
254 uint8_t buffer
[USB_EP1OUT_SIZE
- 4];
259 if (length
> sizeof(buffer
))
260 length
= sizeof(buffer
);
265 va_start(ap
, length
);
267 *buffer_p
++ = va_arg(ap
, int);
271 return ep1_memory_write(pHDev
, addr
, length
, buffer
);
275 #define DTCLOAD_COMMENT (0)
276 #define DTCLOAD_ENTRY (1)
277 #define DTCLOAD_LOAD (2)
278 #define DTCLOAD_RUN (3)
279 #define DTCLOAD_LUT_START (4)
280 #define DTCLOAD_LUT (5)
282 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
284 /* This gets set by the DTC loader */
285 static uint8_t dtc_entry_download
;
287 /* The buffer is specially formatted to represent a valid image to load into the DTC. */
288 static int dtc_load_from_buffer(usb_dev_handle
*pHDev_param
, const uint8_t *buffer
,
297 struct header_s
*header
;
298 uint8_t lut_start
= 0xc0;
300 dtc_entry_download
= 0;
302 /* Stop the DTC before loading anything. */
303 usb_err
= ep1_generic_commandl(
311 if (length
< sizeof(*header
)) {
312 LOG_ERROR("Malformed DTC image");
316 header
= (struct header_s
*)buffer
;
317 buffer
+= sizeof(*header
);
318 length
-= sizeof(*header
);
320 if (length
< (size_t)header
->length
+ 1) {
321 LOG_ERROR("Malformed DTC image");
325 switch (header
->type
) {
326 case DTCLOAD_COMMENT
:
330 /* store entry addresses somewhere */
331 if (!strncmp("download", (char *)buffer
+ 1, 8))
332 dtc_entry_download
= buffer
[0];
336 /* Send the DTC program to ST7 RAM. */
337 usb_err
= ep1_memory_write(
340 header
->length
+ 1, buffer
345 /* Load it into the DTC. */
346 usb_err
= ep1_generic_commandl(
349 (DTC_LOAD_BUFFER
>> 8),
358 usb_err
= ep1_generic_commandl(
369 case DTCLOAD_LUT_START
:
370 lut_start
= buffer
[0];
374 usb_err
= ep1_memory_write(
376 ST7_USB_BUF_EP0OUT
+ lut_start
,
377 header
->length
+ 1, buffer
384 LOG_ERROR("Invalid DTC image record type: 0x%02x", header
->type
);
389 buffer
+= (header
->length
+ 1);
390 length
-= (header
->length
+ 1);
397 * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
399 static int dtc_start_download(void)
404 /* set up for download mode and make sure EP2 is set up to transmit */
405 usb_err
= ep1_generic_commandl(
410 EP1_CMD_SET_DOWNLOAD
,
411 EP1_CMD_MEMORY_READ
, /* read EP2TXR for its data toggle */
419 /* read back ep2txr */
420 usb_err
= usb_bulk_read(
421 pHDev
, USB_EP1IN_ADDR
,
428 usb_err
= ep1_generic_commandl(
431 EP1_CMD_MEMORY_WRITE
, /* preinitialize poll byte */
432 DTC_STATUS_POLL_BYTE
>> 8,
433 DTC_STATUS_POLL_BYTE
,
436 EP1_CMD_MEMORY_WRITE
, /* set EP2IN to return data */
440 (ep2txr
& ST7_EP2TXR_DTOG_TX
) | ST7_EP2TXR_STAT_VALID
,
441 EP1_CMD_DTC_CALL
, /* start running the DTC */
443 EP1_CMD_DTC_GET_CACHED_STATUS
448 /* wait for completion */
449 usb_err
= usb_bulk_read(
450 pHDev
, USB_EP1IN_ADDR
,
458 static int dtc_run_download(
459 usb_dev_handle
*pHDev_param
,
460 uint8_t *command_buffer
,
461 int command_buffer_size
,
462 uint8_t *reply_buffer
,
463 int reply_buffer_size
470 LOG_DEBUG("%d/%d", command_buffer_size
, reply_buffer_size
);
472 usb_err
= usb_bulk_write(
475 (char *)command_buffer
, USB_EP2BANK_SIZE
,
482 /* Wait for DTC to finish running command buffer */
484 usb_err
= ep1_generic_commandl(
488 DTC_STATUS_POLL_BYTE
>> 8,
489 DTC_STATUS_POLL_BYTE
,
495 usb_err
= usb_bulk_read(
504 if (dtc_status
& 0x01)
508 LOG_ERROR("too many retries waiting for DTC status");
514 if (reply_buffer
&& reply_buffer_size
) {
515 usb_err
= usb_bulk_read(
518 (char *)reply_buffer
, reply_buffer_size
,
522 if (usb_err
< reply_buffer_size
) {
523 LOG_ERROR("Read of endpoint 2 returned %d, expected %d",
524 usb_err
, reply_buffer_size
534 * The dtc reply queue is a singly linked list that describes what to do
535 * with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate
539 struct dtc_reply_queue_entry
{
540 struct dtc_reply_queue_entry
*next
;
541 struct jtag_command
*cmd
; /* the command that resulted in this entry */
544 uint8_t *buffer
; /* the scan buffer */
545 int size
; /* size of the scan buffer in bits */
546 int offset
; /* how many bits were already done before this? */
547 int length
; /* how many bits are processed in this operation? */
548 enum scan_type type
; /* SCAN_IN/SCAN_OUT/SCAN_IO */
554 * The dtc_queue consists of a buffer of pending commands and a reply queue.
555 * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
559 struct dtc_reply_queue_entry
*rq_head
;
560 struct dtc_reply_queue_entry
*rq_tail
;
562 uint32_t reply_index
;
563 uint8_t cmd_buffer
[USB_EP2BANK_SIZE
];
567 * The tap state queue is for accumulating TAP state changes wiithout needlessly
568 * flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to
577 static int dtc_queue_init(void)
579 dtc_queue
.rq_head
= NULL
;
580 dtc_queue
.rq_tail
= NULL
;
581 dtc_queue
.cmd_index
= 0;
582 dtc_queue
.reply_index
= 0;
586 static inline struct dtc_reply_queue_entry
*dtc_queue_enqueue_reply(
587 enum scan_type type
, uint8_t *buffer
, int size
, int offset
,
588 int length
, struct jtag_command
*cmd
)
590 struct dtc_reply_queue_entry
*rq_entry
;
592 rq_entry
= malloc(sizeof(struct dtc_reply_queue_entry
));
593 if (rq_entry
!= NULL
) {
594 rq_entry
->scan
.type
= type
;
595 rq_entry
->scan
.buffer
= buffer
;
596 rq_entry
->scan
.size
= size
;
597 rq_entry
->scan
.offset
= offset
;
598 rq_entry
->scan
.length
= length
;
600 rq_entry
->next
= NULL
;
602 if (dtc_queue
.rq_head
== NULL
)
603 dtc_queue
.rq_head
= rq_entry
;
605 dtc_queue
.rq_tail
->next
= rq_entry
;
607 dtc_queue
.rq_tail
= rq_entry
;
614 * Running the queue means that any pending command buffer is run
615 * and any reply data dealt with. The command buffer is then cleared for subsequent processing.
616 * The queue is automatically run by append when it is necessary to get space for the append.
619 static int dtc_queue_run(void)
621 struct dtc_reply_queue_entry
*rq_p
, *rq_next
;
626 uint8_t *dtc_p
, *tdo_p
;
627 uint8_t dtc_mask
, tdo_mask
;
628 uint8_t reply_buffer
[USB_EP2IN_SIZE
];
630 assert((dtc_queue
.rq_head
!= 0) == (dtc_queue
.reply_index
> 0));
631 assert(dtc_queue
.cmd_index
< USB_EP2BANK_SIZE
);
632 assert(dtc_queue
.reply_index
<= USB_EP2IN_SIZE
);
636 if (dtc_queue
.cmd_index
< 1)
639 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = DTC_CMD_STOP
;
641 usb_err
= dtc_run_download(pHDev
,
642 dtc_queue
.cmd_buffer
, dtc_queue
.cmd_index
,
643 reply_buffer
, sizeof(reply_buffer
)
646 LOG_ERROR("dtc_run_download: %s", usb_strerror());
650 if (dtc_queue
.rq_head
!= NULL
) {
651 /* process the reply, which empties the reply queue and frees its entries */
652 dtc_p
= reply_buffer
;
654 /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the
655 *scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It
656 *was that or craft a function to do the reversal, and that wouldn't work with
657 *bit-stuffing (supplying extra bits to use mostly byte operations), or any other
658 *scheme which would throw the byte alignment off. */
661 rq_p
= dtc_queue
.rq_head
;
665 tdo_p
= rq_p
->scan
.buffer
+ (rq_p
->scan
.offset
/ 8);
666 tdo_mask
= 1 << (rq_p
->scan
.offset
% 8);
669 bit_cnt
= rq_p
->scan
.length
;
673 dtc_mask
= 1 << (8 - 1);
680 if (*dtc_p
& dtc_mask
)
688 dtc_mask
= 1 << (8 - 1);
698 /* extra bits or last bit */
701 if ((rq_p
->scan
.type
== SCAN_IN
) && (
702 rq_p
->scan
.offset
!= rq_p
->scan
.size
- 1
704 /* extra bits were sent as a full byte with padding on the
706 dtc_mask
= 1 << (8 - 1);
708 dtc_mask
= 1 << (bit_cnt
- 1);
731 if ((rq_p
->scan
.offset
+ rq_p
->scan
.length
) >= rq_p
->scan
.size
) {
732 /* feed scan buffer back into openocd and free it */
733 if (jtag_read_buffer(rq_p
->scan
.buffer
,
734 rq_p
->cmd
->cmd
.scan
) != ERROR_OK
)
735 retval
= ERROR_JTAG_QUEUE_FAILED
;
736 free(rq_p
->scan
.buffer
);
739 rq_next
= rq_p
->next
;
742 dtc_queue
.rq_head
= NULL
;
743 dtc_queue
.rq_tail
= NULL
;
746 /* reset state for new appends */
747 dtc_queue
.cmd_index
= 0;
748 dtc_queue
.reply_index
= 0;
753 /* runs the queue if it cannot take reserved_cmd bytes of command data
754 * or reserved_reply bytes of reply data */
755 static int dtc_queue_run_if_full(int reserved_cmd
, int reserved_reply
)
757 /* reserve one additional byte for the STOP cmd appended during run */
758 if (dtc_queue
.cmd_index
+ reserved_cmd
+ 1 > USB_EP2BANK_SIZE
)
759 return dtc_queue_run();
761 if (dtc_queue
.reply_index
+ reserved_reply
> USB_EP2IN_SIZE
)
762 return dtc_queue_run();
767 static int tap_state_queue_init(void)
769 tap_state_queue
.length
= 0;
770 tap_state_queue
.buffer
= 0;
774 static int tap_state_queue_run(void)
782 if (!tap_state_queue
.length
)
786 for (i
= tap_state_queue
.length
; i
--; ) {
789 if (tap_state_queue
.buffer
& 1)
791 if ((bits
>= 8) || !i
) {
792 byte_param
<<= (8 - bits
);
794 /* make sure there's room for two cmd bytes */
795 dtc_queue_run_if_full(2, 0);
797 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
799 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
800 DTC_CMD_SHIFT_TMS_BYTES(1);
803 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
804 DTC_CMD_SHIFT_TMS_BITS(bits
);
805 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
809 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
817 tap_state_queue
.buffer
>>= 1;
819 retval
= tap_state_queue_init();
823 static int tap_state_queue_append(uint8_t tms
)
827 if (tap_state_queue
.length
>= sizeof(tap_state_queue
.buffer
) * 8) {
828 retval
= tap_state_queue_run();
834 tap_state_queue
.buffer
|= (1 << tap_state_queue
.length
);
835 tap_state_queue
.length
++;
840 static void rlink_end_state(tap_state_t state
)
842 if (tap_is_state_stable(state
))
843 tap_set_end_state(state
);
845 LOG_ERROR("BUG: %i is not a valid end state", state
);
850 static void rlink_state_move(void)
854 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
855 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
857 for (i
= 0; i
< tms_count
; i
++) {
858 tms
= (tms_scan
>> i
) & 1;
859 tap_state_queue_append(tms
);
862 tap_set_state(tap_get_end_state());
865 static void rlink_path_move(struct pathmove_command
*cmd
)
867 int num_states
= cmd
->num_states
;
873 if (tap_state_transition(tap_get_state(), false) == cmd
->path
[state_count
])
875 else if (tap_state_transition(tap_get_state(), true) == cmd
->path
[state_count
])
878 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
879 tap_state_name(tap_get_state()),
880 tap_state_name(cmd
->path
[state_count
]));
884 tap_state_queue_append(tms
);
886 tap_set_state(cmd
->path
[state_count
]);
891 tap_set_end_state(tap_get_state());
894 static void rlink_runtest(int num_cycles
)
898 tap_state_t saved_end_state
= tap_get_end_state();
900 /* only do a state_move when we're not already in RTI */
901 if (tap_get_state() != TAP_IDLE
) {
902 rlink_end_state(TAP_IDLE
);
906 /* execute num_cycles */
907 for (i
= 0; i
< num_cycles
; i
++)
908 tap_state_queue_append(0);
910 /* finish in end_state */
911 rlink_end_state(saved_end_state
);
912 if (tap_get_state() != tap_get_end_state())
916 /* (1) assert or (0) deassert reset lines */
917 static void rlink_reset(int trst
, int srst
)
922 /* Read port A for bit op */
923 usb_err
= ep1_generic_commandl(
931 LOG_ERROR("%s", usb_strerror());
935 usb_err
= usb_bulk_read(
936 pHDev
, USB_EP1IN_ADDR
,
941 LOG_ERROR("%s", usb_strerror());
946 bitmap
&= ~ST7_PA_NTRST
;
948 bitmap
|= ST7_PA_NTRST
;
950 /* Write port A and read port B for bit op
951 * port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0
952 *and assert NSRST by setting DDR to 1. */
953 usb_err
= ep1_generic_commandl(
955 EP1_CMD_MEMORY_WRITE
,
966 LOG_ERROR("%s", usb_strerror());
970 usb_err
= usb_bulk_read(
971 pHDev
, USB_EP1IN_ADDR
,
976 LOG_ERROR("%s", usb_strerror());
981 bitmap
|= ST7_PB_NSRST
;
983 bitmap
&= ~ST7_PB_NSRST
;
985 /* write port B and read dummy to ensure completion before returning */
986 usb_err
= ep1_generic_commandl(
988 EP1_CMD_MEMORY_WRITE
,
993 EP1_CMD_DTC_GET_CACHED_STATUS
996 LOG_ERROR("%s", usb_strerror());
1000 usb_err
= usb_bulk_read(
1001 pHDev
, USB_EP1IN_ADDR
,
1006 LOG_ERROR("%s", usb_strerror());
1011 static int rlink_scan(struct jtag_command
*cmd
, enum scan_type type
,
1012 uint8_t *buffer
, int scan_size
)
1015 tap_state_t saved_end_state
;
1023 uint8_t tdi_mask
, *tdi_p
;
1026 if (scan_size
< 1) {
1027 LOG_ERROR("scan_size cannot be less than 1 bit");
1031 ir_scan
= cmd
->cmd
.scan
->ir_scan
;
1033 /* Move to the proper state before starting to shift TDI/TDO. */
1034 if (!((!ir_scan
&& (tap_get_state() == TAP_DRSHIFT
)) ||
1035 (ir_scan
&& (tap_get_state() == TAP_IRSHIFT
)))) {
1036 saved_end_state
= tap_get_end_state();
1037 rlink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1039 rlink_end_state(saved_end_state
);
1042 tap_state_queue_run();
1046 printf("scan_size = %d, type = 0x%x\n", scan_size
, type
);
1050 /* clear unused bits in scan buffer for ease of debugging
1051 * (it makes diffing output easier) */
1052 buffer
[scan_size
/ 8] &= ((1 << ((scan_size
- 1) % 8) + 1) - 1);
1054 printf("before scan:");
1055 for (i
= 0; i
< (scan_size
+ 7) / 8; i
++)
1056 printf(" %02x", buffer
[i
]);
1061 /* The number of bits that can be shifted as complete bytes */
1062 byte_bits
= (int)(scan_size
- 1) / 8 * 8;
1063 /* The number of bits left over, not counting the last bit */
1064 extra_bits
= (scan_size
- 1) - byte_bits
;
1070 if (extra_bits
&& (type
== SCAN_OUT
)) {
1071 /* Schedule any extra bits into the DTC command buffer, padding as needed
1072 * For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will
1073 *fall off the end */
1075 /* make sure there's room for two cmd bytes */
1076 dtc_queue_run_if_full(2, 0);
1079 dtc_mask
= 1 << (extra_bits
- 1);
1081 while (extra_bits
--) {
1082 if (*tdi_p
& tdi_mask
)
1088 if (tdi_mask
== 0) {
1094 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1095 DTC_CMD_SHIFT_TDI_BYTES(1);
1097 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1100 /* Loop scheduling full bytes into the DTC command buffer */
1102 /* make sure there's room for one (for in scans) or two cmd bytes and
1103 * at least one reply byte for in or inout scans*/
1104 dtc_queue_run_if_full(type
== SCAN_IN
? 1 : 2, type
!= SCAN_OUT
? 1 : 0);
1106 chunk_bits
= byte_bits
;
1107 /* we can only use up to 16 bytes at a time */
1108 if (chunk_bits
> (16 * 8))
1109 chunk_bits
= (16 * 8);
1111 if (type
!= SCAN_IN
) {
1112 /* how much is there room for, considering stop and byte op? */
1113 x
= (sizeof(dtc_queue
.cmd_buffer
) - (dtc_queue
.cmd_index
+ 1 + 1)) * 8;
1118 if (type
!= SCAN_OUT
) {
1119 /* how much is there room for in the reply buffer? */
1120 x
= (USB_EP2IN_SIZE
- dtc_queue
.reply_index
) * 8;
1125 /* so the loop will end */
1126 byte_bits
-= chunk_bits
;
1128 if (type
!= SCAN_OUT
) {
1129 if (dtc_queue_enqueue_reply(
1130 type
, buffer
, scan_size
, tdi_bit_offset
,
1134 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno
));
1137 dtc_queue
.reply_index
+= (chunk_bits
+ 7) / 8;
1139 tdi_bit_offset
+= chunk_bits
;
1142 /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1143 chunk_bytes
= chunk_bits
/ 8;
1147 x
= DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes
);
1150 x
= DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes
);
1153 x
= DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes
);
1156 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1158 if (type
!= SCAN_IN
) {
1160 dtc_mask
= 1 << (8 - 1);
1162 while (chunk_bits
--) {
1163 if (*tdi_p
& tdi_mask
)
1167 if (dtc_mask
== 0) {
1168 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1170 dtc_mask
= 1 << (8 - 1);
1174 if (tdi_mask
== 0) {
1182 if (extra_bits
&& (type
!= SCAN_OUT
)) {
1183 /* Schedule any extra bits into the DTC command buffer */
1185 /* make sure there's room for one (for in scans) or two cmd bytes
1186 * and one reply byte */
1187 dtc_queue_run_if_full(type
== SCAN_IN
? 1 : 2, 1);
1189 if (dtc_queue_enqueue_reply(
1190 type
, buffer
, scan_size
, tdi_bit_offset
,
1194 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno
));
1198 dtc_queue
.reply_index
++;
1200 tdi_bit_offset
+= extra_bits
;
1202 if (type
== SCAN_IN
) {
1203 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1204 DTC_CMD_SHIFT_TDO_BYTES(1);
1207 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1208 DTC_CMD_SHIFT_TDIO_BITS(extra_bits
);
1211 dtc_mask
= 1 << (8 - 1);
1213 while (extra_bits
--) {
1214 if (*tdi_p
& tdi_mask
)
1220 if (tdi_mask
== 0) {
1226 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1230 /* Schedule the last bit into the DTC command buffer */
1232 /* make sure there's room for one cmd byte and one reply byte
1233 * for in or inout scans*/
1234 dtc_queue_run_if_full(1, type
== SCAN_OUT
? 0 : 1);
1236 if (type
== SCAN_OUT
) {
1237 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1238 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p
& tdi_mask
), 0);
1241 if (dtc_queue_enqueue_reply(
1242 type
, buffer
, scan_size
, tdi_bit_offset
,
1246 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno
));
1250 dtc_queue
.reply_index
++;
1252 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1253 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p
& tdi_mask
), 1);
1256 /* Move to pause state */
1257 tap_state_queue_append(0);
1258 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1259 if (tap_get_state() != tap_get_end_state())
1265 static int rlink_execute_queue(void)
1267 struct jtag_command
*cmd
= jtag_command_queue
; /* currently processed command */
1269 enum scan_type type
;
1271 int retval
, tmp_retval
;
1273 /* return ERROR_OK, unless something goes wrong */
1276 #ifndef AUTOMATIC_BUSY_LED
1278 ep1_generic_commandl(pHDev
, 2,
1279 EP1_CMD_SET_PORTD_LEDS
,
1285 switch (cmd
->type
) {
1287 case JTAG_TLR_RESET
:
1293 /* some events, such as resets, need a queue flush to ensure
1295 tap_state_queue_run();
1300 switch (cmd
->type
) {
1302 LOG_DEBUG_IO("reset trst: %i srst %i",
1303 cmd
->cmd
.reset
->trst
,
1304 cmd
->cmd
.reset
->srst
);
1305 if ((cmd
->cmd
.reset
->trst
== 1) ||
1306 (cmd
->cmd
.reset
->srst
&&
1307 (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1308 tap_set_state(TAP_RESET
);
1309 rlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1312 LOG_DEBUG_IO("runtest %i cycles, end in %i",
1313 cmd
->cmd
.runtest
->num_cycles
,
1314 cmd
->cmd
.runtest
->end_state
);
1315 if (cmd
->cmd
.runtest
->end_state
!= -1)
1316 rlink_end_state(cmd
->cmd
.runtest
->end_state
);
1317 rlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
1319 case JTAG_TLR_RESET
:
1320 LOG_DEBUG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
1321 if (cmd
->cmd
.statemove
->end_state
!= -1)
1322 rlink_end_state(cmd
->cmd
.statemove
->end_state
);
1326 LOG_DEBUG_IO("pathmove: %i states, end in %i",
1327 cmd
->cmd
.pathmove
->num_states
,
1328 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
1329 rlink_path_move(cmd
->cmd
.pathmove
);
1332 LOG_DEBUG_IO("%s scan end in %i",
1333 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR",
1334 cmd
->cmd
.scan
->end_state
);
1335 if (cmd
->cmd
.scan
->end_state
!= -1)
1336 rlink_end_state(cmd
->cmd
.scan
->end_state
);
1337 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1338 type
= jtag_scan_type(cmd
->cmd
.scan
);
1339 if (rlink_scan(cmd
, type
, buffer
, scan_size
) != ERROR_OK
)
1340 retval
= ERROR_FAIL
;
1343 LOG_DEBUG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
1344 jtag_sleep(cmd
->cmd
.sleep
->us
);
1347 LOG_ERROR("BUG: unknown JTAG command type encountered");
1353 /* Flush the DTC queue to make sure any pending reads have been done before exiting this
1355 tap_state_queue_run();
1356 tmp_retval
= dtc_queue_run();
1357 if (tmp_retval
!= ERROR_OK
)
1358 retval
= tmp_retval
;
1360 #ifndef AUTOMATIC_BUSY_LED
1362 ep1_generic_commandl(pHDev
, 2,
1363 EP1_CMD_SET_PORTD_LEDS
,
1371 /* Using an unindexed table because it is infrequently accessed and it is short. The table must be
1372 *in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
1374 static int rlink_speed(int speed
)
1380 speed
= rlink_speed_table
[rlink_speed_table_size
- 1].prescaler
;
1383 for (i
= rlink_speed_table_size
; i
--; ) {
1384 if (rlink_speed_table
[i
].prescaler
== speed
) {
1385 if (dtc_load_from_buffer(pHDev
, rlink_speed_table
[i
].dtc
,
1386 rlink_speed_table
[i
].dtc_size
) != 0) {
1388 "An error occurred while trying to load DTC code for speed \"%d\".",
1393 if (dtc_start_download() < 0) {
1394 LOG_ERROR("starting DTC: %s", usb_strerror());
1402 LOG_ERROR("%d is not a supported speed", speed
);
1406 static int rlink_speed_div(int speed
, int *khz
)
1410 for (i
= rlink_speed_table_size
; i
--; ) {
1411 if (rlink_speed_table
[i
].prescaler
== speed
) {
1412 *khz
= rlink_speed_table
[i
].khz
;
1417 LOG_ERROR("%d is not a supported speed", speed
);
1421 static int rlink_khz(int khz
, int *speed
)
1426 LOG_ERROR("RCLK not supported");
1430 for (i
= rlink_speed_table_size
; i
--; ) {
1431 if (rlink_speed_table
[i
].khz
<= khz
) {
1432 *speed
= rlink_speed_table
[i
].prescaler
;
1437 LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table
[0].khz
);
1438 *speed
= rlink_speed_table
[0].prescaler
;
1442 static int rlink_init(void)
1445 uint8_t reply_buffer
[USB_EP1IN_SIZE
];
1448 const uint16_t vids
[] = { USB_IDVENDOR
, 0 };
1449 const uint16_t pids
[] = { USB_IDPRODUCT
, 0 };
1450 if (jtag_usb_open(vids
, pids
, &pHDev
) != ERROR_OK
)
1453 struct usb_device
*dev
= usb_device(pHDev
);
1454 if (dev
->descriptor
.bNumConfigurations
> 1) {
1455 LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...");
1458 if (dev
->config
->bNumInterfaces
> 1) {
1459 LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...");
1463 LOG_DEBUG("Opened device, pHDev = %p", pHDev
);
1465 /* usb_set_configuration required under win32 */
1466 usb_set_configuration(pHDev
, dev
->config
[0].bConfigurationValue
);
1470 i
= usb_claim_interface(pHDev
, 0);
1472 LOG_ERROR("usb_claim_interface: %s", usb_strerror());
1473 #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1474 j
= usb_detach_kernel_driver_np(pHDev
, 0);
1476 LOG_ERROR("detach kernel driver: %s", usb_strerror());
1479 LOG_DEBUG("interface claimed!");
1482 } while (--retries
);
1485 LOG_ERROR("Initialisation failed.");
1488 if (usb_set_altinterface(pHDev
, 0) != 0) {
1489 LOG_ERROR("Failed to set interface.");
1493 /* The device starts out in an unknown state on open. As such,
1494 * result reads time out, and it's not even known whether the
1495 * command was accepted. So, for this first command, we issue
1496 * it repeatedly until its response doesn't time out. Also, if
1497 * sending a command is going to time out, we find that out here.
1499 * It must be possible to open the device in such a way that
1500 * this special magic isn't needed, but, so far, it escapes us.
1502 for (i
= 0; i
< 5; i
++) {
1503 j
= ep1_generic_commandl(
1507 if (j
< USB_EP1OUT_SIZE
) {
1508 LOG_ERROR("USB write error: %s", usb_strerror());
1512 pHDev
, USB_EP1IN_ADDR
,
1513 (char *)reply_buffer
, sizeof(reply_buffer
),
1516 if (j
!= -ETIMEDOUT
)
1520 if (j
< (int)sizeof(reply_buffer
)) {
1521 LOG_ERROR("USB read error: %s", usb_strerror());
1524 LOG_DEBUG(INTERFACE_NAME
" firmware version: %d.%d.%d",
1529 if ((reply_buffer
[0] != 0) || (reply_buffer
[1] != 0) || (reply_buffer
[2] != 3))
1531 "The rlink device is not of the version that the developers have played with. It may or may not work.");
1533 /* Probe port E for adapter presence */
1534 ep1_generic_commandl(
1536 EP1_CMD_MEMORY_WRITE
, /* Drive sense pin with 0 */
1541 ST7_PE_ADAPTER_SENSE_OUT
, /* DDR */
1542 ST7_PE_ADAPTER_SENSE_OUT
, /* OR */
1543 EP1_CMD_MEMORY_READ
, /* Read back */
1547 EP1_CMD_MEMORY_WRITE
, /* Drive sense pin with 1 */
1551 ST7_PE_ADAPTER_SENSE_OUT
1555 pHDev
, USB_EP1IN_ADDR
,
1556 (char *)reply_buffer
, 1,
1560 if ((reply_buffer
[0] & ST7_PE_ADAPTER_SENSE_IN
) != 0)
1561 LOG_WARNING("target detection problem");
1563 ep1_generic_commandl(
1565 EP1_CMD_MEMORY_READ
, /* Read back */
1569 EP1_CMD_MEMORY_WRITE
, /* float port E */
1579 pHDev
, USB_EP1IN_ADDR
,
1580 (char *)reply_buffer
, 1,
1585 if ((reply_buffer
[0] & ST7_PE_ADAPTER_SENSE_IN
) == 0)
1586 LOG_WARNING("target not plugged in");
1588 /* float ports A and B */
1589 ep1_generic_commandl(
1591 EP1_CMD_MEMORY_WRITE
,
1597 EP1_CMD_MEMORY_WRITE
,
1604 /* make sure DTC is stopped, set VPP control, set up ports A and B */
1605 ep1_generic_commandl(
1608 EP1_CMD_SET_PORTD_VPP
,
1610 EP1_CMD_MEMORY_WRITE
,
1614 ((~(0)) & (ST7_PA_NTRST
)),
1616 /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0
1617 *here and later assert NSRST by setting DDR bit to 1. */
1618 EP1_CMD_MEMORY_WRITE
,
1625 /* set LED updating mode and make sure they're unlit */
1626 ep1_generic_commandl(
1628 #ifdef AUTOMATIC_BUSY_LED
1633 EP1_CMD_SET_PORTD_LEDS
,
1637 tap_state_queue_init();
1644 static int rlink_quit(void)
1646 /* stop DTC and make sure LEDs are off */
1647 ep1_generic_commandl(
1651 EP1_CMD_SET_PORTD_LEDS
,
1653 EP1_CMD_SET_PORTD_VPP
,
1657 usb_release_interface(pHDev
, 0);
1663 struct jtag_interface rlink_interface
= {
1665 .transports
= jtag_only
,
1668 .speed
= rlink_speed
,
1669 .speed_div
= rlink_speed_div
,
1671 .execute_queue
= rlink_execute_queue
,
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)