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 #define ST7_PD_NBUSY_LED ST7_PD0
76 #define ST7_PD_NRUN_LED ST7_PD1
77 /* low enables VPP at adapter header, high connects it to GND instead */
78 #define ST7_PD_VPP_SEL ST7_PD6
79 /* low: VPP = 12v, high: VPP <= 5v */
80 #define ST7_PD_VPP_SHDN ST7_PD7
82 /* These pins are connected together */
83 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
84 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
86 /* Symbolic mapping between port pins and numbered IO lines */
87 #define ST7_PA_IO1 ST7_PA1
88 #define ST7_PA_IO2 ST7_PA2
89 #define ST7_PA_IO4 ST7_PA4
90 #define ST7_PA_IO8 ST7_PA6
91 #define ST7_PA_IO10 ST7_PA7
92 #define ST7_PB_IO5 ST7_PB5
93 #define ST7_PC_IO9 ST7_PC1
94 #define ST7_PC_IO3 ST7_PC2
95 #define ST7_PC_IO7 ST7_PC3
96 #define ST7_PE_IO6 ST7_PE5
98 /* Symbolic mapping between numbered IO lines and adapter signals */
99 #define ST7_PA_RTCK ST7_PA_IO0
100 #define ST7_PA_NTRST ST7_PA_IO1
101 #define ST7_PC_TDI ST7_PC_IO3
102 #define ST7_PA_DBGRQ ST7_PA_IO4
103 #define ST7_PB_NSRST ST7_PB_IO5
104 #define ST7_PE_TMS ST7_PE_IO6
105 #define ST7_PC_TCK ST7_PC_IO7
106 #define ST7_PC_TDO ST7_PC_IO9
107 #define ST7_PA_DBGACK ST7_PA_IO10
109 static usb_dev_handle
*pHDev
;
113 * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
114 * This function takes care of zeroing the unused bytes before sending the packet.
115 * Any reply packet is not handled by this function.
119 ep1_generic_commandl(
120 usb_dev_handle
*pHDev
,
124 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
125 uint8_t *usb_buffer_p
;
129 if(length
> sizeof(usb_buffer
)) {
130 length
= sizeof(usb_buffer
);
133 usb_buffer_p
= usb_buffer
;
135 va_start(ap
, length
);
137 *usb_buffer_p
++ = va_arg(ap
, int);
144 sizeof(usb_buffer
) - (usb_buffer_p
- usb_buffer
)
147 usb_ret
= usb_bulk_write(
150 (char *)usb_buffer
, sizeof(usb_buffer
),
163 usb_dev_handle
*pHDev
,
168 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
173 usb_buffer
[0] = EP1_CMD_MEMORY_READ
;
177 sizeof(usb_buffer
) - 4
184 if(remain
> sizeof(usb_buffer
)) {
185 length
= sizeof(usb_buffer
);
190 usb_buffer
[1] = addr
>> 8;
191 usb_buffer
[2] = addr
;
192 usb_buffer
[3] = length
;
194 usb_ret
= usb_bulk_write(
195 pHDev
, USB_EP1OUT_ADDR
,
196 usb_buffer
, sizeof(usb_buffer
),
200 if(usb_ret
< sizeof(usb_buffer
)) {
204 usb_ret
= usb_bulk_read(
205 pHDev
, USB_EP1IN_ADDR
,
210 if(usb_ret
< length
) {
229 usb_dev_handle
*pHDev
,
232 uint8_t const *buffer
234 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
239 usb_buffer
[0] = EP1_CMD_MEMORY_WRITE
;
245 if(remain
> (sizeof(usb_buffer
) - 4)) {
246 length
= (sizeof(usb_buffer
) - 4);
251 usb_buffer
[1] = addr
>> 8;
252 usb_buffer
[2] = addr
;
253 usb_buffer
[3] = length
;
260 usb_buffer
+ 4 + length
,
262 sizeof(usb_buffer
) - 4 - length
265 usb_ret
= usb_bulk_write(
266 pHDev
, USB_EP1OUT_ADDR
,
267 (char *)usb_buffer
, sizeof(usb_buffer
),
271 if((size_t)usb_ret
< sizeof(usb_buffer
)) {
289 usb_dev_handle
*pHDev
,
294 uint8_t buffer
[USB_EP1OUT_SIZE
- 4];
299 if(length
> sizeof(buffer
)) {
300 length
= sizeof(buffer
);
306 va_start(ap
, length
);
308 *buffer_p
++ = va_arg(ap
, int);
312 return(ep1_memory_write(pHDev
, addr
, length
, buffer
));
317 #define DTCLOAD_COMMENT (0)
318 #define DTCLOAD_ENTRY (1)
319 #define DTCLOAD_LOAD (2)
320 #define DTCLOAD_RUN (3)
321 #define DTCLOAD_LUT_START (4)
322 #define DTCLOAD_LUT (5)
324 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
326 /* This gets set by the DTC loader */
327 static uint8_t dtc_entry_download
;
330 /* The buffer is specially formatted to represent a valid image to load into the DTC. */
333 dtc_load_from_buffer(
334 usb_dev_handle
*pHDev
,
344 struct header_s
*header
;
347 dtc_entry_download
= 0;
349 /* Stop the DTC before loading anything. */
350 usb_err
= ep1_generic_commandl(
354 if(usb_err
< 0) return(usb_err
);
357 if(length
< sizeof(*header
)) {
358 LOG_ERROR("Malformed DTC image\n");
362 header
= (struct header_s
*)buffer
;
363 buffer
+= sizeof(*header
);
364 length
-= sizeof(*header
);
366 if(length
< (size_t)header
->length
+ 1) {
367 LOG_ERROR("Malformed DTC image\n");
371 switch(header
->type
) {
372 case DTCLOAD_COMMENT
:
376 /* store entry addresses somewhere */
377 if(!strncmp("download", (char *)buffer
+ 1, 8)) {
378 dtc_entry_download
= buffer
[0];
383 /* Send the DTC program to ST7 RAM. */
384 usb_err
= ep1_memory_write(
387 header
->length
+ 1, buffer
389 if(usb_err
< 0) return(usb_err
);
391 /* Load it into the DTC. */
392 usb_err
= ep1_generic_commandl(
395 (DTC_LOAD_BUFFER
>> 8),
398 if(usb_err
< 0) return(usb_err
);
403 usb_err
= ep1_generic_commandl(
409 if(usb_err
< 0) return(usb_err
);
413 case DTCLOAD_LUT_START
:
414 lut_start
= buffer
[0];
418 usb_err
= ep1_memory_write(
420 ST7_USB_BUF_EP0OUT
+ lut_start
,
421 header
->length
+ 1, buffer
423 if(usb_err
< 0) return(usb_err
);
427 LOG_ERROR("Invalid DTC image record type: 0x%02x\n", header
->type
);
432 buffer
+= (header
->length
+ 1);
433 length
-= (header
->length
+ 1);
441 * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
445 dtc_start_download(void) {
449 /* set up for download mode and make sure EP2 is set up to transmit */
450 usb_err
= ep1_generic_commandl(
455 EP1_CMD_SET_DOWNLOAD
,
456 EP1_CMD_MEMORY_READ
, /* read EP2TXR for its data toggle */
461 if(usb_err
< 0) return(usb_err
);
463 /* read back ep2txr */
464 usb_err
= usb_bulk_read(
465 pHDev
, USB_EP1IN_ADDR
,
469 if(usb_err
< 0) return(usb_err
);
471 usb_err
= ep1_generic_commandl(
474 EP1_CMD_MEMORY_WRITE
, /* preinitialize poll byte */
475 DTC_STATUS_POLL_BYTE
>> 8,
476 DTC_STATUS_POLL_BYTE
,
479 EP1_CMD_MEMORY_WRITE
, /* set EP2IN to return data */
483 (ep2txr
& ST7_EP2TXR_DTOG_TX
) | ST7_EP2TXR_STAT_VALID
,
484 EP1_CMD_DTC_CALL
, /* start running the DTC */
486 EP1_CMD_DTC_GET_CACHED_STATUS
488 if(usb_err
< 0) return(usb_err
);
490 /* wait for completion */
491 usb_err
= usb_bulk_read(
492 pHDev
, USB_EP1IN_ADDR
,
504 usb_dev_handle
*pHDev
,
506 int command_buffer_size
,
508 int reply_buffer_size
510 u8 ep2_buffer
[USB_EP2IN_SIZE
];
514 LOG_DEBUG(": %d/%d\n", command_buffer_size
, reply_buffer_size
);
516 usb_err
= usb_bulk_write(
519 (char *)command_buffer
, USB_EP2BANK_SIZE
,
522 if(usb_err
< 0) return(usb_err
);
525 /* Wait for DTC to finish running command buffer */
527 usb_err
= ep1_generic_commandl(
531 DTC_STATUS_POLL_BYTE
>> 8,
532 DTC_STATUS_POLL_BYTE
,
535 if(usb_err
< 0) return(usb_err
);
537 usb_err
= usb_bulk_read(
540 (char *)ep2_buffer
, 1,
543 if(usb_err
< 0) return(usb_err
);
545 if(ep2_buffer
[0] & 0x01) break;
548 LOG_ERROR("%s, %d: too many retries waiting for DTC status\n",
556 if(!reply_buffer
) reply_buffer_size
= 0;
557 if(reply_buffer_size
) {
558 usb_err
= usb_bulk_read(
561 (char *)ep2_buffer
, sizeof(ep2_buffer
),
565 if(usb_err
< (int)sizeof(ep2_buffer
)) {
566 LOG_ERROR("%s, %d: Read of endpoint 2 returned %d\n",
567 __FILE__
, __LINE__
, usb_err
572 memcpy(reply_buffer
, ep2_buffer
, reply_buffer_size
);
581 * 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.
585 struct dtc_reply_queue_entry_s
{
586 struct dtc_reply_queue_entry_s
*next
;
587 jtag_command_t
*cmd
; /* the command that resulted in this entry */
590 u8
*buffer
; /* the scan buffer */
591 int size
; /* size of the scan buffer in bits */
592 int offset
; /* how many bits were already done before this? */
593 int length
; /* how many bits are processed in this operation? */
594 enum scan_type type
; /* SCAN_IN/SCAN_OUT/SCAN_IO */
596 } dtc_reply_queue_entry_t
;
600 * The dtc_queue consists of a buffer of pending commands and a reply queue.
601 * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
606 dtc_reply_queue_entry_t
*rq_head
;
607 dtc_reply_queue_entry_t
*rq_tail
;
610 u8 cmd_buffer
[USB_EP2BANK_SIZE
];
615 * 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.
628 dtc_queue_init(void) {
629 dtc_queue
.rq_head
= NULL
;
630 dtc_queue
.rq_tail
= NULL
;
631 dtc_queue
.cmd_index
= 0;
632 dtc_queue
.reply_index
= 0;
639 dtc_reply_queue_entry_t
*
640 dtc_queue_enqueue_reply(
648 dtc_reply_queue_entry_t
*rq_entry
;
650 rq_entry
= malloc(sizeof(dtc_reply_queue_entry_t
));
651 if(rq_entry
!= NULL
) {
652 rq_entry
->scan
.type
= type
;
653 rq_entry
->scan
.buffer
= buffer
;
654 rq_entry
->scan
.size
= size
;
655 rq_entry
->scan
.offset
= offset
;
656 rq_entry
->scan
.length
= length
;
658 rq_entry
->next
= NULL
;
660 if(dtc_queue
.rq_head
== NULL
)
661 dtc_queue
.rq_head
= rq_entry
;
663 dtc_queue
.rq_tail
->next
= rq_entry
;
665 dtc_queue
.rq_tail
= rq_entry
;
673 * 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.
674 * The queue is automatically run by append when it is necessary to get space for the append.
679 dtc_queue_run(void) {
680 dtc_reply_queue_entry_t
*rq_p
, *rq_next
;
686 u8 dtc_mask
, tdo_mask
;
687 u8 reply_buffer
[USB_EP2IN_SIZE
];
691 if(dtc_queue
.cmd_index
< 1) return(retval
);
693 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = DTC_CMD_STOP
;
696 if(dtc_queue
.rq_head
== NULL
) {
697 usb_err
= dtc_run_download(pHDev
,
698 dtc_queue
.cmd_buffer
, dtc_queue
.cmd_index
,
702 LOG_ERROR("dtc_run_download: %s\n", usb_strerror());
706 usb_err
= dtc_run_download(pHDev
,
707 dtc_queue
.cmd_buffer
, dtc_queue
.cmd_index
,
708 reply_buffer
, dtc_queue
.reply_index
711 LOG_ERROR("dtc_run_download: %s\n", usb_strerror());
714 /* process the reply, which empties the reply queue and frees its entries */
715 dtc_p
= reply_buffer
;
717 /* 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. */
720 rq_p
= dtc_queue
.rq_head
;
724 tdo_p
= rq_p
->scan
.buffer
+ (rq_p
->scan
.offset
/ 8);
725 tdo_mask
= 1 << (rq_p
->scan
.offset
% 8);
728 bit_cnt
= rq_p
->scan
.length
;
732 dtc_mask
= 1 << (8 - 1);
739 if(*dtc_p
& dtc_mask
) {
748 dtc_mask
= 1 << (8 - 1);
758 /* extra bits or last bit */
762 rq_p
->scan
.type
== SCAN_IN
764 rq_p
->scan
.offset
!= rq_p
->scan
.size
- 1
766 /* extra bits were sent as a full byte with padding on the end */
767 dtc_mask
= 1 << (8 - 1);
769 dtc_mask
= 1 << (bit_cnt
- 1);
794 if((rq_p
->scan
.offset
+ rq_p
->scan
.length
) >= rq_p
->scan
.size
) {
795 /* feed scan buffer back into openocd and free it */
796 if(jtag_read_buffer(rq_p
->scan
.buffer
, rq_p
->cmd
->cmd
.scan
) != ERROR_OK
) {
797 retval
= ERROR_JTAG_QUEUE_FAILED
;
799 free(rq_p
->scan
.buffer
);
802 rq_next
= rq_p
->next
;
805 dtc_queue
.rq_head
= NULL
;
806 dtc_queue
.rq_tail
= NULL
;
812 /* reset state for new appends */
813 dtc_queue
.cmd_index
= 0;
814 dtc_queue
.reply_index
= 0;
823 tap_state_queue_init(void) {
824 tap_state_queue
.length
= 0;
825 tap_state_queue
.buffer
= 0;
832 tap_state_queue_run(void) {
839 if(!tap_state_queue
.length
) return(retval
);
842 for(i
= tap_state_queue
.length
; i
--;) {
845 if(tap_state_queue
.buffer
& 1) {
848 if((bits
>= 8) || !i
) {
851 /* make sure there's room for stop, byte op, and one byte */
852 if(dtc_queue
.cmd_index
>= (sizeof(dtc_queue
.cmd_buffer
) - (1 + 1 + 1))) {
853 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
858 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
860 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
861 DTC_CMD_SHIFT_TMS_BYTES(1);
864 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
865 DTC_CMD_SHIFT_TMS_BITS(bits
);
866 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
870 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
879 tap_state_queue
.buffer
>>= 1;
881 retval
= tap_state_queue_init();
888 tap_state_queue_append(
893 if(tap_state_queue
.length
>= sizeof(tap_state_queue
.buffer
) * 8) {
894 retval
= tap_state_queue_run();
895 if(retval
!= 0) return(retval
);
899 tap_state_queue
.buffer
|= (1 << tap_state_queue
.length
);
901 tap_state_queue
.length
++;
908 void rlink_end_state(tap_state_t state
)
910 if (tap_is_state_stable(state
))
911 tap_set_end_state(state
);
914 LOG_ERROR("BUG: %i is not a valid end state", state
);
921 void rlink_state_move(void) {
924 u8 tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
926 for (i
= 0; i
< 7; i
++)
928 tms
= (tms_scan
>> i
) & 1;
929 tap_state_queue_append(tms
);
932 tap_set_state(tap_get_end_state());
936 void rlink_path_move(pathmove_command_t
*cmd
)
938 int num_states
= cmd
->num_states
;
945 if (tap_state_transition(tap_get_state(), false) == cmd
->path
[state_count
])
949 else if (tap_state_transition(tap_get_state(), true) == cmd
->path
[state_count
])
955 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd
->path
[state_count
]));
959 tap_state_queue_append(tms
);
961 tap_set_state(cmd
->path
[state_count
]);
966 tap_set_end_state(tap_get_state());
971 void rlink_runtest(int num_cycles
)
975 tap_state_t saved_end_state
= tap_get_end_state();
977 /* only do a state_move when we're not already in RTI */
978 if (tap_get_state() != TAP_IDLE
)
980 rlink_end_state(TAP_IDLE
);
984 /* execute num_cycles */
985 for (i
= 0; i
< num_cycles
; i
++)
987 tap_state_queue_append(0);
990 /* finish in end_state */
991 rlink_end_state(saved_end_state
);
992 if (tap_get_state() != tap_get_end_state())
997 /* (1) assert or (0) deassert reset lines */
999 void rlink_reset(int trst
, int srst
)
1004 /* Read port A for bit op */
1005 usb_err
= ep1_generic_commandl(
1007 EP1_CMD_MEMORY_READ
,
1013 LOG_ERROR("%s", usb_strerror());
1017 usb_err
= usb_bulk_read(
1018 pHDev
, USB_EP1IN_ADDR
,
1023 LOG_ERROR("%s", usb_strerror());
1028 bitmap
&= ~ST7_PA_NTRST
;
1030 bitmap
|= ST7_PA_NTRST
;
1033 /* Write port A and read port B for bit op */
1034 /* port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0 and assert NSRST by setting DDR to 1. */
1035 usb_err
= ep1_generic_commandl(
1037 EP1_CMD_MEMORY_WRITE
,
1042 EP1_CMD_MEMORY_READ
,
1048 LOG_ERROR("%s", usb_strerror());
1052 usb_err
= usb_bulk_read(
1053 pHDev
, USB_EP1IN_ADDR
,
1058 LOG_ERROR("%s", usb_strerror());
1063 bitmap
|= ST7_PB_NSRST
;
1065 bitmap
&= ~ST7_PB_NSRST
;
1068 /* write port B and read dummy to ensure completion before returning */
1069 usb_err
= ep1_generic_commandl(
1071 EP1_CMD_MEMORY_WRITE
,
1076 EP1_CMD_DTC_GET_CACHED_STATUS
1079 LOG_ERROR("%s", usb_strerror());
1083 usb_err
= usb_bulk_read(
1084 pHDev
, USB_EP1IN_ADDR
,
1089 LOG_ERROR("%s", usb_strerror());
1098 jtag_command_t
*cmd
,
1099 enum scan_type type
,
1104 tap_state_t saved_end_state
;
1112 u8 tdi_mask
, *tdi_p
;
1116 LOG_ERROR("scan_size cannot be less than 1 bit\n");
1120 ir_scan
= cmd
->cmd
.scan
->ir_scan
;
1122 /* Move to the proper state before starting to shift TDI/TDO. */
1124 (!ir_scan
&& (tap_get_state() == TAP_DRSHIFT
))
1126 (ir_scan
&& (tap_get_state() == TAP_IRSHIFT
))
1128 saved_end_state
= tap_get_end_state();
1129 rlink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1131 rlink_end_state(saved_end_state
);
1134 tap_state_queue_run();
1138 printf("scan_size = %d, type=0x%x\n", scan_size
, type
);
1142 /* clear unused bits in scan buffer for ease of debugging */
1143 /* (it makes diffing output easier) */
1144 buffer
[scan_size
/ 8] &= ((1 << ((scan_size
- 1) % 8) + 1) - 1);
1146 printf("before scan:");
1147 for(i
= 0; i
< (scan_size
+ 7) / 8; i
++) {
1148 printf(" %02x", buffer
[i
]);
1154 /* The number of bits that can be shifted as complete bytes */
1155 byte_bits
= (int)(scan_size
- 1) / 8 * 8;
1156 /* The number of bits left over, not counting the last bit */
1157 extra_bits
= (scan_size
- 1) - byte_bits
;
1163 if(extra_bits
&& (type
== SCAN_OUT
)) {
1164 /* Schedule any extra bits into the DTC command buffer, padding as needed */
1165 /* For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will fall off the end */
1166 /* make sure there's room for stop, byte op, and one byte */
1168 (dtc_queue
.cmd_index
>= sizeof(dtc_queue
.cmd_buffer
) - (1 + 1 + 1))
1174 dtc_mask
= 1 << (extra_bits
- 1);
1176 while(extra_bits
--) {
1177 if(*tdi_p
& tdi_mask
) {
1190 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1191 DTC_CMD_SHIFT_TDI_BYTES(1);
1193 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1196 /* Loop scheduling full bytes into the DTC command buffer */
1198 if(type
== SCAN_IN
) {
1199 /* make sure there's room for stop and byte op */
1200 x
= (dtc_queue
.cmd_index
>= sizeof(dtc_queue
.cmd_buffer
) - (1 + 1));
1202 /* make sure there's room for stop, byte op, and at least one byte */
1203 x
= (dtc_queue
.cmd_index
>= sizeof(dtc_queue
.cmd_buffer
) - (1 + 1 + 1));
1206 if(type
!= SCAN_OUT
) {
1207 /* make sure there's room for at least one reply byte */
1208 x
|= (dtc_queue
.reply_index
>= USB_EP2IN_SIZE
- (1));
1215 chunk_bits
= byte_bits
;
1216 /* we can only use up to 16 bytes at a time */
1217 if(chunk_bits
> (16 * 8)) chunk_bits
= (16 * 8);
1219 if(type
!= SCAN_IN
) {
1220 /* how much is there room for, considering stop and byte op? */
1221 x
= (sizeof(dtc_queue
.cmd_buffer
) - (dtc_queue
.cmd_index
+ 1 + 1)) * 8;
1222 if(chunk_bits
> x
) chunk_bits
= x
;
1225 if(type
!= SCAN_OUT
) {
1226 /* how much is there room for in the reply buffer? */
1227 x
= (USB_EP2IN_SIZE
- dtc_queue
.reply_index
) * 8;
1228 if(chunk_bits
> x
) chunk_bits
= x
;
1231 /* so the loop will end */
1232 byte_bits
-= chunk_bits
;
1234 if(type
!= SCAN_OUT
) {
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
+= chunk_bits
;
1247 /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1248 chunk_bytes
= chunk_bits
/ 8;
1252 x
= DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes
);
1255 x
= DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes
);
1258 x
= DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes
);
1261 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1263 if(type
!= SCAN_IN
) {
1265 dtc_mask
= 1 << (8 - 1);
1267 while(chunk_bits
--) {
1268 if(*tdi_p
& tdi_mask
) {
1274 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1275 dtc_queue
.reply_index
++;
1277 dtc_mask
= 1 << (8 - 1);
1289 if(extra_bits
&& (type
!= SCAN_OUT
)) {
1290 /* Schedule any extra bits into the DTC command buffer */
1291 /* make sure there's room for stop, byte op, and one byte */
1293 (dtc_queue
.cmd_index
>= sizeof(dtc_queue
.cmd_buffer
) - (1 + 1 + 1))
1295 (dtc_queue
.reply_index
>= USB_EP2IN_SIZE
- (1))
1300 if(dtc_queue_enqueue_reply(
1301 type
, buffer
, scan_size
, tdi_bit_offset
,
1305 LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno
));
1309 tdi_bit_offset
+= extra_bits
;
1311 if(type
== SCAN_IN
) {
1312 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1313 DTC_CMD_SHIFT_TDO_BYTES(1);
1316 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1317 DTC_CMD_SHIFT_TDIO_BITS(extra_bits
);
1320 dtc_mask
= 1 << (8 - 1);
1322 while(extra_bits
--) {
1323 if(*tdi_p
& tdi_mask
) {
1336 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1339 dtc_queue
.reply_index
++;
1342 /* Schedule the last bit into the DTC command buffer */
1344 /* make sure there's room for stop, and bit pair command */
1346 (dtc_queue
.cmd_index
>= sizeof(dtc_queue
.cmd_buffer
) - (1 + 1))
1348 (dtc_queue
.reply_index
>= USB_EP2IN_SIZE
- (1))
1353 if(type
== SCAN_OUT
) {
1354 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1355 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p
& tdi_mask
), 0);
1358 if(dtc_queue_enqueue_reply(
1359 type
, buffer
, scan_size
, tdi_bit_offset
,
1363 LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno
));
1367 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1368 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p
& tdi_mask
), 1);
1370 dtc_queue
.reply_index
++;
1374 /* Move to pause state */
1375 tap_state_queue_append(0);
1376 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1377 if (tap_get_state() != tap_get_end_state()) rlink_state_move();
1384 int rlink_execute_queue(void)
1386 jtag_command_t
*cmd
= jtag_command_queue
; /* currently processed command */
1388 enum scan_type type
;
1390 int retval
, tmp_retval
;
1392 /* return ERROR_OK, unless something goes wrong */
1395 #ifndef AUTOMATIC_BUSY_LED
1397 ep1_generic_commandl(pHDev
, 2,
1398 EP1_CMD_SET_PORTD_LEDS
,
1407 case JTAG_END_STATE
:
1409 case JTAG_STATEMOVE
:
1415 /* some events, such as resets, need a queue flush to ensure consistency */
1416 tap_state_queue_run();
1423 case JTAG_END_STATE
:
1424 #ifdef _DEBUG_JTAG_IO_
1425 LOG_DEBUG("end_state: %i", cmd
->cmd
.end_state
->end_state
);
1427 if (cmd
->cmd
.end_state
->end_state
!= -1)
1428 rlink_end_state(cmd
->cmd
.end_state
->end_state
);
1431 #ifdef _DEBUG_JTAG_IO_
1432 LOG_DEBUG("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1434 if ((cmd
->cmd
.reset
->trst
== 1) || (cmd
->cmd
.reset
->srst
&& (jtag_reset_config
& RESET_SRST_PULLS_TRST
)))
1436 tap_set_state(TAP_RESET
);
1438 rlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1441 #ifdef _DEBUG_JTAG_IO_
1442 LOG_DEBUG("runtest %i cycles, end in %i", cmd
->cmd
.runtest
->num_cycles
, cmd
->cmd
.runtest
->end_state
);
1444 if (cmd
->cmd
.runtest
->end_state
!= -1)
1445 rlink_end_state(cmd
->cmd
.runtest
->end_state
);
1446 rlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
1448 case JTAG_STATEMOVE
:
1449 #ifdef _DEBUG_JTAG_IO_
1450 LOG_DEBUG("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
1452 if (cmd
->cmd
.statemove
->end_state
!= -1)
1453 rlink_end_state(cmd
->cmd
.statemove
->end_state
);
1457 #ifdef _DEBUG_JTAG_IO_
1458 LOG_DEBUG("pathmove: %i states, end in %i", cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
1460 rlink_path_move(cmd
->cmd
.pathmove
);
1463 #ifdef _DEBUG_JTAG_IO_
1464 LOG_DEBUG("%s scan end in %i", (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", cmd
->cmd
.scan
->end_state
);
1466 if (cmd
->cmd
.scan
->end_state
!= -1)
1467 rlink_end_state(cmd
->cmd
.scan
->end_state
);
1468 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1469 type
= jtag_scan_type(cmd
->cmd
.scan
);
1470 if(rlink_scan(cmd
, type
, buffer
, scan_size
) != ERROR_OK
) {
1471 retval
= ERROR_FAIL
;
1475 #ifdef _DEBUG_JTAG_IO_
1476 LOG_DEBUG("sleep %i", cmd
->cmd
.sleep
->us
);
1478 jtag_sleep(cmd
->cmd
.sleep
->us
);
1481 LOG_ERROR("BUG: unknown JTAG command type encountered");
1487 /* Flush the DTC queue to make sure any pending reads have been done before exiting this function */
1488 tap_state_queue_run();
1489 tmp_retval
= dtc_queue_run();
1490 if(tmp_retval
!= ERROR_OK
) {
1491 retval
= tmp_retval
;
1494 #ifndef AUTOMATIC_BUSY_LED
1496 ep1_generic_commandl(pHDev
, 2,
1497 EP1_CMD_SET_PORTD_LEDS
,
1506 /* 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. */
1509 int rlink_speed(int speed
)
1515 speed
= rlink_speed_table
[rlink_speed_table_size
- 1].prescaler
;
1518 for(i
= rlink_speed_table_size
; i
--; ) {
1519 if(rlink_speed_table
[i
].prescaler
== speed
) {
1520 if(dtc_load_from_buffer(pHDev
, rlink_speed_table
[i
].dtc
, rlink_speed_table
[i
].dtc_size
) != 0) {
1521 LOG_ERROR("An error occurred while trying to load DTC code for speed \"%d\".\n", speed
);
1525 if(dtc_start_download() < 0) {
1526 LOG_ERROR("%s, %d: starting DTC: %s",
1537 LOG_ERROR("%d is not a supported speed", speed
);
1543 int rlink_speed_div(
1549 for(i
= rlink_speed_table_size
; i
--; ) {
1550 if(rlink_speed_table
[i
].prescaler
== speed
) {
1551 *khz
= rlink_speed_table
[i
].khz
;
1556 LOG_ERROR("%d is not a supported speed", speed
);
1569 LOG_ERROR("RCLK not supported");
1573 for(i
= rlink_speed_table_size
; i
--; ) {
1574 if(rlink_speed_table
[i
].khz
<= khz
) {
1575 *speed
= rlink_speed_table
[i
].prescaler
;
1580 LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table
[0].khz
);
1581 *speed
= rlink_speed_table
[0].prescaler
;
1589 handle_dtc_directory_command(
1590 struct command_context_s
*cmd_ctx
,
1596 LOG_ERROR("expected exactly one argument to rlink_dtc_directory <directory-path>");
1597 return(ERROR_INVALID_ARGUMENTS
);
1600 printf("handle_dtc_directory_command called with \"%s\"\n", args
[0]);
1608 int rlink_register_commands(struct command_context_s
*cmd_ctx
)
1611 #ifdef _DEBUG_JTAG_IO_
1612 LOG_DEBUG("rlink_register_commands called with cmd_ctx=%p\n", cmd_ctx
);
1618 "rlink_dtc_directory",
1619 handle_dtc_directory_command
,
1621 "The directory in which to search for DTC load images"
1630 int rlink_init(void)
1632 struct usb_bus
*busses
;
1633 struct usb_bus
*bus
;
1637 u8 reply_buffer
[USB_EP1IN_SIZE
];
1643 busses
= usb_get_busses();
1645 for(bus
= busses
; bus
; bus
= bus
->next
)
1647 struct usb_device
*dev
;
1649 for(dev
= bus
->devices
; dev
; dev
= dev
->next
)
1651 if( (dev
->descriptor
.idVendor
== USB_IDVENDOR
) && (dev
->descriptor
.idProduct
== USB_IDPRODUCT
) )
1654 LOG_DEBUG("Found device on bus.\n");
1658 if( dev
->descriptor
.bNumConfigurations
> 1 )
1660 LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...\n");
1663 if( dev
->config
->bNumInterfaces
> 1 )
1665 LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...\n");
1669 pHDev
=usb_open(dev
);
1671 LOG_ERROR ("Failed to open device.\n");
1674 LOG_DEBUG("Opened device, pHDev = %p\n",pHDev
);
1676 /* usb_set_configuration required under win32 */
1677 usb_set_configuration(pHDev
, dev
->config
[0].bConfigurationValue
);
1682 i
= usb_claim_interface(pHDev
,0);
1685 LOG_ERROR("usb_claim_interface: %s", usb_strerror());
1686 #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1687 j
= usb_detach_kernel_driver_np(pHDev
, 0);
1689 LOG_ERROR("detach kernel driver: %s", usb_strerror());
1694 LOG_DEBUG("interface claimed!\n");
1701 if( usb_set_altinterface(pHDev
,0) )
1703 LOG_ERROR("Failed to set interface.\n");
1717 LOG_ERROR("No device found on bus.\n");
1723 LOG_ERROR("Initialisation failed.");
1728 /* 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. */
1729 /* 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. */
1730 for(i
= 0; i
< 5; i
++) {
1731 j
= ep1_generic_commandl(
1735 if(j
< USB_EP1OUT_SIZE
) {
1736 LOG_ERROR("USB write error: %s", usb_strerror());
1740 pHDev
, USB_EP1IN_ADDR
,
1741 (char *)reply_buffer
, sizeof(reply_buffer
),
1744 if(j
!= -ETIMEDOUT
) break;
1747 if(j
< (int)sizeof(reply_buffer
)) {
1748 LOG_ERROR("USB read error: %s", usb_strerror());
1751 LOG_DEBUG(INTERFACE_NAME
" firmware version: %d.%d.%d\n", reply_buffer
[0], reply_buffer
[1], reply_buffer
[2]);
1753 if((reply_buffer
[0] != 0) || (reply_buffer
[1] != 0) || (reply_buffer
[2] != 3)) {
1754 LOG_WARNING("The rlink device is not of the version that the developers have played with. It may or may not work.\n");
1757 /* Probe port E for adapter presence */
1758 ep1_generic_commandl(
1760 EP1_CMD_MEMORY_WRITE
, /* Drive sense pin with 0 */
1765 ST7_PE_ADAPTER_SENSE_OUT
, /* DDR */
1766 ST7_PE_ADAPTER_SENSE_OUT
, /* OR */
1767 EP1_CMD_MEMORY_READ
, /* Read back */
1771 EP1_CMD_MEMORY_WRITE
, /* Drive sense pin with 1 */
1775 ST7_PE_ADAPTER_SENSE_OUT
1779 pHDev
, USB_EP1IN_ADDR
,
1780 (char *)reply_buffer
, 1,
1784 if((reply_buffer
[0] & ST7_PE_ADAPTER_SENSE_IN
) != 0) {
1785 LOG_WARNING("target detection problem\n");
1788 ep1_generic_commandl(
1790 EP1_CMD_MEMORY_READ
, /* Read back */
1794 EP1_CMD_MEMORY_WRITE
, /* float port E */
1804 pHDev
, USB_EP1IN_ADDR
,
1805 (char *)reply_buffer
, 1,
1810 if((reply_buffer
[0] & ST7_PE_ADAPTER_SENSE_IN
) == 0) {
1811 LOG_WARNING("target not plugged in\n");
1814 /* float ports A and B */
1815 ep1_generic_commandl(
1817 EP1_CMD_MEMORY_WRITE
,
1823 EP1_CMD_MEMORY_WRITE
,
1830 /* make sure DTC is stopped, set VPP control, set up ports A and B */
1831 ep1_generic_commandl(
1834 EP1_CMD_SET_PORTD_VPP
,
1836 EP1_CMD_MEMORY_WRITE
,
1840 ((~(0)) & (ST7_PA_NTRST
)),
1842 /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0 here and later assert NSRST by setting DDR bit to 1. */
1843 EP1_CMD_MEMORY_WRITE
,
1850 /* set LED updating mode and make sure they're unlit */
1851 ep1_generic_commandl(
1853 #ifdef AUTOMATIC_BUSY_LED
1858 EP1_CMD_SET_PORTD_LEDS
,
1862 tap_state_queue_init();
1864 rlink_speed(jtag_speed
);
1872 int rlink_quit(void)
1874 /* stop DTC and make sure LEDs are off */
1875 ep1_generic_commandl(
1879 EP1_CMD_SET_PORTD_LEDS
,
1881 EP1_CMD_SET_PORTD_VPP
,
1885 usb_release_interface(pHDev
,0);
1893 jtag_interface_t rlink_interface
=
1898 .register_commands
= rlink_register_commands
,
1899 .speed
= rlink_speed
,
1900 .speed_div
= rlink_speed_div
,
1902 .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)