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, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
36 /* project specific includes */
40 #include "configuration.h"
47 /* This feature is made useless by running the DTC all the time. When automatic, the LED is on whenever the DTC is running. Otherwise, USB messages are sent to turn it on and off. */
48 #undef AUTOMATIC_BUSY_LED
50 /* This feature may require derating the speed due to reduced hold time. */
51 #undef USE_HARDWARE_SHIFTER_FOR_TMS
54 #define INTERFACE_NAME "RLink"
56 #define USB_IDVENDOR (0x138e)
57 #define USB_IDPRODUCT (0x9000)
59 #define USB_EP1OUT_ADDR (0x01)
60 #define USB_EP1OUT_SIZE (16)
61 #define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80)
62 #define USB_EP1IN_SIZE (USB_EP1OUT_SIZE)
64 #define USB_EP2OUT_ADDR (0x02)
65 #define USB_EP2OUT_SIZE (64)
66 #define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80)
67 #define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
68 #define USB_EP2BANK_SIZE (512)
70 #define USB_TIMEOUT_MS (3 * 1000)
72 #define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
75 /* Symbolic names for some pins */
76 #define ST7_PA_NJTAG_TRST ST7_PA1
77 #define ST7_PA_NRLINK_RST ST7_PA3
78 #define ST7_PA_NLINE_DRIVER_ENABLE ST7_PA5
80 /* mask for negative-logic pins */
81 #define ST7_PA_NUNASSERTED (0 \
84 | ST7_PA_NLINE_DRIVER_ENABLE \
87 #define ST7_PD_NBUSY_LED ST7_PD0
88 #define ST7_PD_NERROR_LED ST7_PD1
89 #define ST7_PD_NRUN_LED ST7_PD7
91 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
92 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
94 static usb_dev_handle
*pHDev
;
98 * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
99 * This function takes care of zeroing the unused bytes before sending the packet.
100 * Any reply packet is not handled by this function.
104 ep1_generic_commandl(
105 usb_dev_handle
*pHDev
,
109 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
110 uint8_t *usb_buffer_p
;
114 if(length
> sizeof(usb_buffer
)) {
115 length
= sizeof(usb_buffer
);
118 usb_buffer_p
= usb_buffer
;
120 va_start(ap
, length
);
122 *usb_buffer_p
++ = va_arg(ap
, int);
129 sizeof(usb_buffer
) - (usb_buffer_p
- usb_buffer
)
132 usb_ret
= usb_bulk_write(
135 (char *)usb_buffer
, sizeof(usb_buffer
),
148 usb_dev_handle
*pHDev
,
153 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
158 usb_buffer
[0] = EP1_CMD_MEMORY_READ
;
162 sizeof(usb_buffer
) - 4
169 if(remain
> sizeof(usb_buffer
)) {
170 length
= sizeof(usb_buffer
);
175 usb_buffer
[1] = addr
>> 8;
176 usb_buffer
[2] = addr
;
177 usb_buffer
[3] = length
;
179 usb_ret
= usb_bulk_write(
180 pHDev
, USB_EP1OUT_ADDR
,
181 usb_buffer
, sizeof(usb_buffer
),
185 if(usb_ret
< sizeof(usb_buffer
)) {
189 usb_ret
= usb_bulk_read(
190 pHDev
, USB_EP1IN_ADDR
,
195 if(usb_ret
< length
) {
214 usb_dev_handle
*pHDev
,
217 uint8_t const *buffer
219 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
224 usb_buffer
[0] = EP1_CMD_MEMORY_WRITE
;
230 if(remain
> (sizeof(usb_buffer
) - 4)) {
231 length
= (sizeof(usb_buffer
) - 4);
236 usb_buffer
[1] = addr
>> 8;
237 usb_buffer
[2] = addr
;
238 usb_buffer
[3] = length
;
245 usb_buffer
+ 4 + length
,
247 sizeof(usb_buffer
) - 4 - length
250 usb_ret
= usb_bulk_write(
251 pHDev
, USB_EP1OUT_ADDR
,
252 (char *)usb_buffer
, sizeof(usb_buffer
),
256 if(usb_ret
< sizeof(usb_buffer
)) {
274 usb_dev_handle
*pHDev
,
279 uint8_t buffer
[USB_EP1OUT_SIZE
- 4];
284 if(length
> sizeof(buffer
)) {
285 length
= sizeof(buffer
);
291 va_start(ap
, length
);
293 *buffer_p
++ = va_arg(ap
, int);
297 return(ep1_memory_write(pHDev
, addr
, length
, buffer
));
302 #define DTCLOAD_COMMENT (0)
303 #define DTCLOAD_ENTRY (1)
304 #define DTCLOAD_LOAD (2)
305 #define DTCLOAD_RUN (3)
306 #define DTCLOAD_LUT_START (4)
307 #define DTCLOAD_LUT (5)
309 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
311 /* This gets set by the DTC loader */
312 static uint8_t dtc_entry_download
;
315 /* The buffer is specially formatted to represent a valid image to load into the DTC. */
318 dtc_load_from_buffer(
319 usb_dev_handle
*pHDev
,
329 struct header_s
*header
;
332 dtc_entry_download
= 0;
334 /* Stop the DTC before loading anything. */
335 usb_err
= ep1_generic_commandl(
339 if(usb_err
< 0) return(usb_err
);
342 if(length
< sizeof(*header
)) {
343 LOG_ERROR("Malformed DTC image\n");
347 header
= (struct header_s
*)buffer
;
348 buffer
+= sizeof(*header
);
349 length
-= sizeof(*header
);
351 if(length
< header
->length
+ 1) {
352 LOG_ERROR("Malformed DTC image\n");
356 switch(header
->type
) {
357 case DTCLOAD_COMMENT
:
361 /* store entry addresses somewhere */
362 if(!strncmp("download", (char *)buffer
+ 1, 8)) {
363 dtc_entry_download
= buffer
[0];
368 /* Send the DTC program to ST7 RAM. */
369 usb_err
= ep1_memory_write(
372 header
->length
+ 1, buffer
374 if(usb_err
< 0) return(usb_err
);
376 /* Load it into the DTC. */
377 usb_err
= ep1_generic_commandl(
380 (DTC_LOAD_BUFFER
>> 8),
383 if(usb_err
< 0) return(usb_err
);
388 usb_err
= ep1_generic_commandl(
394 if(usb_err
< 0) return(usb_err
);
398 case DTCLOAD_LUT_START
:
399 lut_start
= buffer
[0];
403 usb_err
= ep1_memory_write(
405 ST7_USB_BUF_EP0OUT
+ lut_start
,
406 header
->length
+ 1, buffer
408 if(usb_err
< 0) return(usb_err
);
412 LOG_ERROR("Invalid DTC image record type: 0x%02x\n", header
->type
);
417 buffer
+= (header
->length
+ 1);
418 length
-= (header
->length
+ 1);
426 * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
435 /* set up for download mode and make sure EP2 is set up to transmit */
436 usb_err
= ep1_generic_commandl(
441 EP1_CMD_SET_DOWNLOAD
,
442 EP1_CMD_MEMORY_READ
, /* read EP2TXR for its data toggle */
447 if(usb_err
< 0) return(usb_err
);
449 /* read back ep2txr */
450 usb_err
= usb_bulk_read(
451 pHDev
, USB_EP1IN_ADDR
,
455 if(usb_err
< 0) return(usb_err
);
457 usb_err
= ep1_generic_commandl(
460 EP1_CMD_MEMORY_WRITE
, /* preinitialize poll byte */
461 DTC_STATUS_POLL_BYTE
>> 8,
462 DTC_STATUS_POLL_BYTE
,
465 EP1_CMD_MEMORY_WRITE
, /* set EP2IN to return data */
469 (ep2txr
& ST7_EP2TXR_DTOG_TX
) | ST7_EP2TXR_STAT_VALID
,
470 EP1_CMD_DTC_CALL
, /* start running the DTC */
472 EP1_CMD_DTC_GET_CACHED_STATUS
474 if(usb_err
< 0) return(usb_err
);
476 /* wait for completion */
477 usb_err
= usb_bulk_read(
478 pHDev
, USB_EP1IN_ADDR
,
490 usb_dev_handle
*pHDev
,
492 int command_buffer_size
,
494 int reply_buffer_size
496 u8 ep2_buffer
[USB_EP2IN_SIZE
];
500 LOG_DEBUG(": %d/%d\n", command_buffer_size
, reply_buffer_size
);
502 usb_err
= usb_bulk_write(
505 (char *)command_buffer
, USB_EP2BANK_SIZE
,
508 if(usb_err
< 0) return(usb_err
);
511 /* Wait for DTC to finish running command buffer */
513 usb_err
= ep1_generic_commandl(
517 DTC_STATUS_POLL_BYTE
>> 8,
518 DTC_STATUS_POLL_BYTE
,
521 if(usb_err
< 0) return(usb_err
);
523 usb_err
= usb_bulk_read(
526 (char *)ep2_buffer
, 1,
529 if(usb_err
< 0) return(usb_err
);
531 if(ep2_buffer
[0] & 0x01) break;
534 LOG_ERROR("%s, %d: too many retries waiting for DTC status\n",
542 if(!reply_buffer
) reply_buffer_size
= 0;
543 if(reply_buffer_size
) {
544 usb_err
= usb_bulk_read(
547 (char *)ep2_buffer
, sizeof(ep2_buffer
),
551 if(usb_err
< (int)sizeof(ep2_buffer
)) {
552 LOG_ERROR("%s, %d: Read of endpoint 2 returned %d\n",
553 __FILE__
, __LINE__
, usb_err
558 memcpy(reply_buffer
, ep2_buffer
, reply_buffer_size
);
567 * The dtc reply queue is a singly linked list that describes what to do with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate these entries.
571 struct dtc_reply_queue_entry_s
{
572 struct dtc_reply_queue_entry_s
*next
;
573 jtag_command_t
*cmd
; /* the command that resulted in this entry */
576 u8
*buffer
; /* the scan buffer */
577 int size
; /* size of the scan buffer in bits */
578 int offset
; /* how many bits were already done before this? */
579 int length
; /* how many bits are processed in this operation? */
580 enum scan_type type
; /* SCAN_IN/SCAN_OUT/SCAN_IO */
582 } dtc_reply_queue_entry_t
;
586 * The dtc_queue consists of a buffer of pending commands and a reply queue.
587 * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
592 dtc_reply_queue_entry_t
*rq_head
;
593 dtc_reply_queue_entry_t
*rq_tail
;
596 u8 cmd_buffer
[USB_EP2BANK_SIZE
];
601 * The tap state queue is for accumulating TAP state changes wiithout needlessly flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to the dtc_queue.
616 dtc_queue
.rq_head
= NULL
;
617 dtc_queue
.rq_tail
= NULL
;
618 dtc_queue
.cmd_index
= 0;
619 dtc_queue
.reply_index
= 0;
626 dtc_reply_queue_entry_t
*
627 dtc_queue_enqueue_reply(
635 dtc_reply_queue_entry_t
*rq_entry
;
637 rq_entry
= malloc(sizeof(dtc_reply_queue_entry_t
));
638 if(rq_entry
!= NULL
) {
639 rq_entry
->scan
.type
= type
;
640 rq_entry
->scan
.buffer
= buffer
;
641 rq_entry
->scan
.size
= size
;
642 rq_entry
->scan
.offset
= offset
;
643 rq_entry
->scan
.length
= length
;
645 rq_entry
->next
= NULL
;
647 if(dtc_queue
.rq_head
== NULL
)
648 dtc_queue
.rq_head
= rq_entry
;
650 dtc_queue
.rq_tail
->next
= rq_entry
;
652 dtc_queue
.rq_tail
= rq_entry
;
660 * Running the queue means that any pending command buffer is run and any reply data dealt with. The command buffer is then cleared for subsequent processing.
661 * The queue is automatically run by append when it is necessary to get space for the append.
668 dtc_reply_queue_entry_t
*rq_p
, *rq_next
;
674 u8 dtc_mask
, tdo_mask
;
675 u8 reply_buffer
[USB_EP2IN_SIZE
];
679 if(dtc_queue
.cmd_index
< 1) return(retval
);
681 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = DTC_CMD_STOP
;
684 if(dtc_queue
.rq_head
== NULL
) {
685 usb_err
= dtc_run_download(pHDev
,
686 dtc_queue
.cmd_buffer
, dtc_queue
.cmd_index
,
690 LOG_ERROR("dtc_run_download: %s\n", usb_strerror());
694 usb_err
= dtc_run_download(pHDev
,
695 dtc_queue
.cmd_buffer
, dtc_queue
.cmd_index
,
696 reply_buffer
, dtc_queue
.reply_index
699 LOG_ERROR("dtc_run_download: %s\n", usb_strerror());
702 /* process the reply, which empties the reply queue and frees its entries */
703 dtc_p
= reply_buffer
;
705 /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It was that or craft a function to do the reversal, and that wouldn't work with bit-stuffing (supplying extra bits to use mostly byte operations), or any other scheme which would throw the byte alignment off. */
708 rq_p
= dtc_queue
.rq_head
;
712 tdo_p
= rq_p
->scan
.buffer
+ (rq_p
->scan
.offset
/ 8);
713 tdo_mask
= 1 << (rq_p
->scan
.offset
% 8);
716 bit_cnt
= rq_p
->scan
.length
;
720 dtc_mask
= 1 << (8 - 1);
727 if(*dtc_p
& dtc_mask
) {
736 dtc_mask
= 1 << (8 - 1);
746 /* extra bits or last bit */
750 rq_p
->scan
.type
== SCAN_IN
752 rq_p
->scan
.offset
!= rq_p
->scan
.size
- 1
754 /* extra bits were sent as a full byte with padding on the end */
755 dtc_mask
= 1 << (8 - 1);
757 dtc_mask
= 1 << (bit_cnt
- 1);
782 if((rq_p
->scan
.offset
+ rq_p
->scan
.length
) >= rq_p
->scan
.size
) {
783 /* feed scan buffer back into openocd and free it */
784 if(jtag_read_buffer(rq_p
->scan
.buffer
, rq_p
->cmd
->cmd
.scan
) != ERROR_OK
) {
785 retval
= ERROR_JTAG_QUEUE_FAILED
;
787 free(rq_p
->scan
.buffer
);
790 rq_next
= rq_p
->next
;
793 dtc_queue
.rq_head
= NULL
;
794 dtc_queue
.rq_tail
= NULL
;
800 /* reset state for new appends */
801 dtc_queue
.cmd_index
= 0;
802 dtc_queue
.reply_index
= 0;
811 tap_state_queue_init(
813 tap_state_queue
.length
= 0;
814 tap_state_queue
.buffer
= 0;
829 if(!tap_state_queue
.length
) return(retval
);
832 for(i
= tap_state_queue
.length
; i
--;) {
835 if(tap_state_queue
.buffer
& 1) {
838 if((bits
>= 8) || !i
) {
841 /* make sure there's room for stop, byte op, and one byte */
842 if(dtc_queue
.cmd_index
>= (sizeof(dtc_queue
.cmd_buffer
) - (1 + 1 + 1))) {
843 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
848 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
850 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
851 DTC_CMD_SHIFT_TMS_BYTES(1);
854 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
855 DTC_CMD_SHIFT_TMS_BITS(bits
);
856 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
860 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
869 tap_state_queue
.buffer
>>= 1;
871 retval
= tap_state_queue_init();
878 tap_state_queue_append(
883 if(tap_state_queue
.length
>= sizeof(tap_state_queue
.buffer
) * 8) {
884 retval
= tap_state_queue_run();
885 if(retval
!= 0) return(retval
);
889 tap_state_queue
.buffer
|= (1 << tap_state_queue
.length
);
891 tap_state_queue
.length
++;
898 void rlink_end_state(enum tap_state state
)
900 if (tap_move_map
[state
] != -1)
904 LOG_ERROR("BUG: %i is not a valid end state", state
);
911 void rlink_state_move(void) {
914 u8 tms_scan
= TAP_MOVE(cur_state
, end_state
);
916 for (i
= 0; i
< 7; i
++)
918 tms
= (tms_scan
>> i
) & 1;
919 tap_state_queue_append(tms
);
922 cur_state
= end_state
;
926 void rlink_path_move(pathmove_command_t
*cmd
)
928 int num_states
= cmd
->num_states
;
935 if (tap_transitions
[cur_state
].low
== cmd
->path
[state_count
])
939 else if (tap_transitions
[cur_state
].high
== cmd
->path
[state_count
])
945 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", jtag_state_name(cur_state
), jtag_state_name(cmd
->path
[state_count
]));
949 tap_state_queue_append(tms
);
951 cur_state
= cmd
->path
[state_count
];
956 end_state
= cur_state
;
961 void rlink_runtest(int num_cycles
)
965 enum tap_state saved_end_state
= end_state
;
967 /* only do a state_move when we're not already in RTI */
968 if (cur_state
!= TAP_IDLE
)
970 rlink_end_state(TAP_IDLE
);
974 /* execute num_cycles */
975 for (i
= 0; i
< num_cycles
; i
++)
977 tap_state_queue_append(0);
980 /* finish in end_state */
981 rlink_end_state(saved_end_state
);
982 if (cur_state
!= end_state
)
987 /* (1) assert or (0) deassert reset lines */
989 void rlink_reset(int trst
, int srst
)
994 bitmap
= ((~(ST7_PA_NLINE_DRIVER_ENABLE
)) & ST7_PA_NUNASSERTED
);
997 bitmap
&= ~ST7_PA_NJTAG_TRST
;
1000 bitmap
&= ~ST7_PA_NRLINK_RST
;
1003 usb_err
= ep1_generic_commandl(
1006 EP1_CMD_MEMORY_WRITE
,
1011 EP1_CMD_DTC_GET_CACHED_STATUS
1014 LOG_ERROR("%s: %s\n", __func__
, usb_strerror());
1018 usb_err
= usb_bulk_read(
1019 pHDev
, USB_EP1IN_ADDR
,
1024 LOG_ERROR("%s: %s\n", __func__
, usb_strerror());
1033 jtag_command_t
*cmd
,
1034 enum scan_type type
,
1039 enum tap_state saved_end_state
;
1047 u8 tdi_mask
, *tdi_p
;
1051 LOG_ERROR("scan_size cannot be less than 1 bit\n");
1055 ir_scan
= cmd
->cmd
.scan
->ir_scan
;
1057 /* Move to the proper state before starting to shift TDI/TDO. */
1059 (!ir_scan
&& (cur_state
== TAP_DRSHIFT
))
1061 (ir_scan
&& (cur_state
== TAP_IRSHIFT
))
1063 saved_end_state
= end_state
;
1064 rlink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1066 rlink_end_state(saved_end_state
);
1069 tap_state_queue_run();
1073 printf("scan_size = %d, type=0x%x\n", scan_size
, type
);
1077 /* clear unused bits in scan buffer for ease of debugging */
1078 /* (it makes diffing output easier) */
1079 buffer
[scan_size
/ 8] &= ((1 << ((scan_size
- 1) % 8) + 1) - 1);
1081 printf("before scan:");
1082 for(i
= 0; i
< (scan_size
+ 7) / 8; i
++) {
1083 printf(" %02x", buffer
[i
]);
1089 /* The number of bits that can be shifted as complete bytes */
1090 byte_bits
= (int)(scan_size
- 1) / 8 * 8;
1091 /* The number of bits left over, not counting the last bit */
1092 extra_bits
= (scan_size
- 1) - byte_bits
;
1098 if(extra_bits
&& (type
== SCAN_OUT
)) {
1099 /* Schedule any extra bits into the DTC command buffer, padding as needed */
1100 /* For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will fall off the end */
1101 /* make sure there's room for stop, byte op, and one byte */
1103 (dtc_queue
.cmd_index
>= sizeof(dtc_queue
.cmd_buffer
) - (1 + 1 + 1))
1109 dtc_mask
= 1 << (extra_bits
- 1);
1111 while(extra_bits
--) {
1112 if(*tdi_p
& tdi_mask
) {
1125 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1126 DTC_CMD_SHIFT_TDI_BYTES(1);
1128 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1131 /* Loop scheduling full bytes into the DTC command buffer */
1133 if(type
== SCAN_IN
) {
1134 /* make sure there's room for stop and byte op */
1135 x
= (dtc_queue
.cmd_index
>= sizeof(dtc_queue
.cmd_buffer
) - (1 + 1));
1137 /* make sure there's room for stop, byte op, and at least one byte */
1138 x
= (dtc_queue
.cmd_index
>= sizeof(dtc_queue
.cmd_buffer
) - (1 + 1 + 1));
1141 if(type
!= SCAN_OUT
) {
1142 /* make sure there's room for at least one reply byte */
1143 x
|= (dtc_queue
.reply_index
>= USB_EP2IN_SIZE
- (1));
1150 chunk_bits
= byte_bits
;
1151 /* we can only use up to 16 bytes at a time */
1152 if(chunk_bits
> (16 * 8)) chunk_bits
= (16 * 8);
1154 if(type
!= SCAN_IN
) {
1155 /* how much is there room for, considering stop and byte op? */
1156 x
= (sizeof(dtc_queue
.cmd_buffer
) - (dtc_queue
.cmd_index
+ 1 + 1)) * 8;
1157 if(chunk_bits
> x
) chunk_bits
= x
;
1160 if(type
!= SCAN_OUT
) {
1161 /* how much is there room for in the reply buffer? */
1162 x
= (USB_EP2IN_SIZE
- dtc_queue
.reply_index
) * 8;
1163 if(chunk_bits
> x
) chunk_bits
= x
;
1166 /* so the loop will end */
1167 byte_bits
-= chunk_bits
;
1169 if(type
!= SCAN_OUT
) {
1170 if(dtc_queue_enqueue_reply(
1171 type
, buffer
, scan_size
, tdi_bit_offset
,
1175 LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno
));
1179 tdi_bit_offset
+= chunk_bits
;
1182 /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1183 chunk_bytes
= chunk_bits
/ 8;
1187 x
= DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes
);
1190 x
= DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes
);
1193 x
= DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes
);
1196 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1198 if(type
!= SCAN_IN
) {
1200 dtc_mask
= 1 << (8 - 1);
1202 while(chunk_bits
--) {
1203 if(*tdi_p
& tdi_mask
) {
1209 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1210 dtc_queue
.reply_index
++;
1212 dtc_mask
= 1 << (8 - 1);
1224 if(extra_bits
&& (type
!= SCAN_OUT
)) {
1225 /* Schedule any extra bits into the DTC command buffer */
1226 /* make sure there's room for stop, byte op, and one byte */
1228 (dtc_queue
.cmd_index
>= sizeof(dtc_queue
.cmd_buffer
) - (1 + 1 + 1))
1230 (dtc_queue
.reply_index
>= USB_EP2IN_SIZE
- (1))
1235 if(dtc_queue_enqueue_reply(
1236 type
, buffer
, scan_size
, tdi_bit_offset
,
1240 LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno
));
1244 tdi_bit_offset
+= extra_bits
;
1246 if(type
== SCAN_IN
) {
1247 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1248 DTC_CMD_SHIFT_TDO_BYTES(1);
1251 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1252 DTC_CMD_SHIFT_TDIO_BITS(extra_bits
);
1255 dtc_mask
= 1 << (8 - 1);
1257 while(extra_bits
--) {
1258 if(*tdi_p
& tdi_mask
) {
1271 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1274 dtc_queue
.reply_index
++;
1277 /* Schedule the last bit into the DTC command buffer */
1279 /* make sure there's room for stop, and bit pair command */
1281 (dtc_queue
.cmd_index
>= sizeof(dtc_queue
.cmd_buffer
) - (1 + 1))
1283 (dtc_queue
.reply_index
>= USB_EP2IN_SIZE
- (1))
1288 if(type
== SCAN_OUT
) {
1289 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1290 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p
& tdi_mask
), 0);
1293 if(dtc_queue_enqueue_reply(
1294 type
, buffer
, scan_size
, tdi_bit_offset
,
1298 LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno
));
1302 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1303 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p
& tdi_mask
), 1);
1305 dtc_queue
.reply_index
++;
1309 /* Move to pause state */
1310 tap_state_queue_append(0);
1311 cur_state
= ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
;
1312 if (cur_state
!= end_state
) rlink_state_move();
1319 int rlink_execute_queue(void)
1321 jtag_command_t
*cmd
= jtag_command_queue
; /* currently processed command */
1323 enum scan_type type
;
1325 int retval
, tmp_retval
;
1327 /* return ERROR_OK, unless something goes wrong */
1330 #ifndef AUTOMATIC_BUSY_LED
1332 ep1_generic_commandl(pHDev
, 2,
1333 EP1_CMD_SET_PORTD_LEDS
,
1342 case JTAG_END_STATE
:
1344 case JTAG_STATEMOVE
:
1350 /* some events, such as resets, need a queue flush to ensure consistency */
1351 tap_state_queue_run();
1358 case JTAG_END_STATE
:
1359 #ifdef _DEBUG_JTAG_IO_
1360 LOG_DEBUG("end_state: %i", cmd
->cmd
.end_state
->end_state
);
1362 if (cmd
->cmd
.end_state
->end_state
!= -1)
1363 rlink_end_state(cmd
->cmd
.end_state
->end_state
);
1366 #ifdef _DEBUG_JTAG_IO_
1367 LOG_DEBUG("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1369 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_reset_config
& RESET_SRST_PULLS_TRST
)))
1371 cur_state
= TAP_RESET
;
1373 rlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1376 #ifdef _DEBUG_JTAG_IO_
1377 LOG_DEBUG("runtest %i cycles, end in %i", cmd
->cmd
.runtest
->num_cycles
, cmd
->cmd
.runtest
->end_state
);
1379 if (cmd
->cmd
.runtest
->end_state
!= -1)
1380 rlink_end_state(cmd
->cmd
.runtest
->end_state
);
1381 rlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
1383 case JTAG_STATEMOVE
:
1384 #ifdef _DEBUG_JTAG_IO_
1385 LOG_DEBUG("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
1387 if (cmd
->cmd
.statemove
->end_state
!= -1)
1388 rlink_end_state(cmd
->cmd
.statemove
->end_state
);
1392 #ifdef _DEBUG_JTAG_IO_
1393 LOG_DEBUG("pathmove: %i states, end in %i", cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
1395 rlink_path_move(cmd
->cmd
.pathmove
);
1398 #ifdef _DEBUG_JTAG_IO_
1399 LOG_DEBUG("%s scan end in %i", (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", cmd
->cmd
.scan
->end_state
);
1401 if (cmd
->cmd
.scan
->end_state
!= -1)
1402 rlink_end_state(cmd
->cmd
.scan
->end_state
);
1403 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1404 type
= jtag_scan_type(cmd
->cmd
.scan
);
1405 if(rlink_scan(cmd
, type
, buffer
, scan_size
) != ERROR_OK
) {
1406 retval
= ERROR_FAIL
;
1410 #ifdef _DEBUG_JTAG_IO_
1411 LOG_DEBUG("sleep %i", cmd
->cmd
.sleep
->us
);
1413 jtag_sleep(cmd
->cmd
.sleep
->us
);
1416 LOG_ERROR("BUG: unknown JTAG command type encountered");
1422 /* Flush the DTC queue to make sure any pending reads have been done before exiting this function */
1423 tap_state_queue_run();
1424 tmp_retval
= dtc_queue_run();
1425 if(tmp_retval
!= ERROR_OK
) {
1426 retval
= tmp_retval
;
1429 #ifndef AUTOMATIC_BUSY_LED
1431 ep1_generic_commandl(pHDev
, 2,
1432 EP1_CMD_SET_PORTD_LEDS
,
1441 /* Using an unindexed table because it is infrequently accessed and it is short. The table must be in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
1444 int rlink_speed(int speed
)
1450 speed
= rlink_speed_table
[rlink_speed_table_size
- 1].prescaler
;
1453 for(i
= rlink_speed_table_size
; i
--; ) {
1454 if(rlink_speed_table
[i
].prescaler
== speed
) {
1455 if(dtc_load_from_buffer(pHDev
, rlink_speed_table
[i
].dtc
, rlink_speed_table
[i
].dtc_size
) != 0) {
1456 LOG_ERROR("An error occurred while trying to load DTC code for speed \"%d\".\n", speed
);
1460 if(dtc_start_download() < 0) {
1461 LOG_ERROR("%s, %d: starting DTC: %s",
1472 LOG_ERROR("%d is not a supported speed", speed
);
1478 int rlink_speed_div(
1484 for(i
= rlink_speed_table_size
; i
--; ) {
1485 if(rlink_speed_table
[i
].prescaler
== speed
) {
1486 *khz
= rlink_speed_table
[i
].khz
;
1491 LOG_ERROR("%d is not a supported speed", speed
);
1504 LOG_ERROR("RCLK not supported");
1508 for(i
= rlink_speed_table_size
; i
--; ) {
1509 if(rlink_speed_table
[i
].khz
<= khz
) {
1510 *speed
= rlink_speed_table
[i
].prescaler
;
1515 LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table
[0].khz
);
1516 *speed
= rlink_speed_table
[0].prescaler
;
1524 handle_dtc_directory_command(
1525 struct command_context_s
*cmd_ctx
,
1531 LOG_ERROR("expected exactly one argument to rlink_dtc_directory <directory-path>");
1532 return(ERROR_INVALID_ARGUMENTS
);
1535 printf("handle_dtc_directory_command called with \"%s\"\n", args
[0]);
1543 int rlink_register_commands(struct command_context_s
*cmd_ctx
)
1546 #ifdef _DEBUG_JTAG_IO_
1547 LOG_DEBUG("rlink_register_commands called with cmd_ctx=%p\n", cmd_ctx
);
1553 "rlink_dtc_directory",
1554 handle_dtc_directory_command
,
1556 "The directory in which to search for DTC load images"
1565 int rlink_init(void)
1567 struct usb_bus
*busses
;
1568 struct usb_bus
*bus
;
1572 u8 reply_buffer
[USB_EP1IN_SIZE
];
1578 busses
= usb_get_busses();
1580 for(bus
= busses
; bus
; bus
= bus
->next
)
1582 struct usb_device
*dev
;
1584 for(dev
= bus
->devices
; dev
; dev
= dev
->next
)
1586 if( (dev
->descriptor
.idVendor
== USB_IDVENDOR
) && (dev
->descriptor
.idProduct
== USB_IDPRODUCT
) )
1589 LOG_DEBUG("Found device on bus.\n");
1593 if( dev
->descriptor
.bNumConfigurations
> 1 )
1595 LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...\n");
1598 if( dev
->config
->bNumInterfaces
> 1 )
1600 LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...\n");
1604 pHDev
=usb_open(dev
);
1606 LOG_ERROR ("Failed to open device.\n");
1609 LOG_DEBUG("Opened device, pHDev = %p\n",pHDev
);
1611 /* usb_set_configuration required under win32 */
1612 usb_set_configuration(pHDev
, dev
->config
[0].bConfigurationValue
);
1617 i
= usb_claim_interface(pHDev
,0);
1620 LOG_ERROR("usb_claim_interface: %s", usb_strerror());
1621 #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1622 j
= usb_detach_kernel_driver_np(pHDev
, 0);
1624 LOG_ERROR("detach kernel driver: %s", usb_strerror());
1629 LOG_DEBUG("interface claimed!\n");
1636 if( usb_set_altinterface(pHDev
,0) )
1638 LOG_ERROR("Failed to set interface.\n");
1652 LOG_ERROR("No device found on bus.\n");
1658 LOG_ERROR("Initialisation failed.");
1663 /* The device starts out in an unknown state on open. As such, result reads time out, and it's not even known whether the command was accepted. So, for this first command, we issue it repeatedly until its response doesn't time out. Also, if sending a command is going to time out, we'll find that out here. */
1664 /* It must be possible to open the device in such a way that this special magic isn't needed, but, so far, it escapes us. */
1665 for(i
= 0; i
< 5; i
++) {
1666 j
= ep1_generic_commandl(
1670 if(j
< USB_EP1OUT_SIZE
) {
1671 LOG_ERROR("USB write error: %s", usb_strerror());
1675 pHDev
, USB_EP1IN_ADDR
,
1676 (char *)reply_buffer
, sizeof(reply_buffer
),
1679 if(j
!= -ETIMEDOUT
) break;
1682 if(j
< (int)sizeof(reply_buffer
)) {
1683 LOG_ERROR("USB read error: %s", usb_strerror());
1686 LOG_DEBUG(INTERFACE_NAME
" firmware version: %d.%d.%d\n", reply_buffer
[0], reply_buffer
[1], reply_buffer
[2]);
1688 if((reply_buffer
[0] != 0) || (reply_buffer
[1] != 0) || (reply_buffer
[2] != 3)) {
1689 LOG_WARNING("The rlink device is not of the version that the developers have played with. It may or may not work.\n");
1692 /* Probe port E for adapter presence */
1693 ep1_generic_commandl(
1695 EP1_CMD_MEMORY_WRITE
, /* Drive sense pin with 0 */
1700 ST7_PE_ADAPTER_SENSE_OUT
,
1701 ST7_PE_ADAPTER_SENSE_OUT
,
1702 EP1_CMD_MEMORY_READ
, /* Read back */
1706 EP1_CMD_MEMORY_WRITE
, /* Drive sense pin with 1 */
1710 ST7_PE_ADAPTER_SENSE_OUT
1714 pHDev
, USB_EP1IN_ADDR
,
1715 (char *)reply_buffer
, 1,
1719 if((reply_buffer
[0] & ST7_PE_ADAPTER_SENSE_IN
) != 0) {
1720 LOG_WARNING("target detection problem\n");
1723 ep1_generic_commandl(
1725 EP1_CMD_MEMORY_READ
, /* Read back */
1729 EP1_CMD_MEMORY_WRITE
, /* float port E */
1739 pHDev
, USB_EP1IN_ADDR
,
1740 (char *)reply_buffer
, 1,
1745 if((reply_buffer
[0] & ST7_PE_ADAPTER_SENSE_IN
) == 0) {
1746 LOG_WARNING("target not plugged in\n");
1749 /* float port A, make sure DTC is stopped, set upper 2 bits of port D, and set up port A */
1750 ep1_generic_commandl(
1752 EP1_CMD_MEMORY_WRITE
,
1759 EP1_CMD_SET_PORTD_UPPER
,
1761 EP1_CMD_MEMORY_WRITE
,
1765 ((~(ST7_PA_NLINE_DRIVER_ENABLE
)) & ST7_PA_NUNASSERTED
),
1766 (ST7_PA_NLINE_DRIVER_ENABLE
| ST7_PA_NRLINK_RST
| ST7_PA_NJTAG_TRST
)
1769 /* set LED updating mode and make sure they're unlit */
1770 ep1_generic_commandl(
1772 #ifdef AUTOMATIC_BUSY_LED
1777 EP1_CMD_SET_PORTD_LEDS
,
1781 tap_state_queue_init();
1783 rlink_speed(jtag_speed
);
1791 int rlink_quit(void)
1793 /* stop DTC and make sure LEDs are off */
1794 ep1_generic_commandl(
1798 EP1_CMD_SET_PORTD_LEDS
,
1800 EP1_CMD_SET_PORTD_UPPER
,
1804 usb_release_interface(pHDev
,0);
1812 jtag_interface_t rlink_interface
=
1817 .register_commands
= rlink_register_commands
,
1818 .speed
= rlink_speed
,
1819 .speed_div
= rlink_speed_div
,
1821 .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)