1 /**************************************************************************
2 * Copyright (C) 2012 by Andreas Fritiofson *
3 * andreas.fritiofson@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
19 ***************************************************************************/
26 #include "helper/log.h"
27 #include <libusb-1.0/libusb.h>
29 /* Compatibility define for older libusb-1.0 */
34 #ifdef _DEBUG_JTAG_IO_
35 #define DEBUG_IO(expr...) LOG_DEBUG(expr)
36 #define DEBUG_PRINT_BUF(buf, len) \
38 char buf_string[32 * 3 + 1]; \
39 int buf_string_pos = 0; \
40 for (int i = 0; i < len; i++) { \
41 buf_string_pos += sprintf(buf_string + buf_string_pos, " %02x", buf[i]); \
42 if (i % 32 == 32 - 1) { \
43 LOG_DEBUG("%s", buf_string); \
47 if (buf_string_pos > 0) \
48 LOG_DEBUG("%s", buf_string);\
51 #define DEBUG_IO(expr...) do {} while (0)
52 #define DEBUG_PRINT_BUF(buf, len) do {} while (0)
55 #define FTDI_DEVICE_OUT_REQTYPE (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE)
56 #define FTDI_DEVICE_IN_REQTYPE (0x80 | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE)
58 #define BITMODE_MPSSE 0x02
60 #define SIO_RESET_REQUEST 0x00
61 #define SIO_SET_LATENCY_TIMER_REQUEST 0x09
62 #define SIO_GET_LATENCY_TIMER_REQUEST 0x0A
63 #define SIO_SET_BITMODE_REQUEST 0x0B
65 #define SIO_RESET_SIO 0
66 #define SIO_RESET_PURGE_RX 1
67 #define SIO_RESET_PURGE_TX 2
70 libusb_context
*usb_ctx
;
71 libusb_device_handle
*usb_dev
;
72 unsigned int usb_write_timeout
;
73 unsigned int usb_read_timeout
;
76 uint16_t max_packet_size
;
79 enum ftdi_chip_type type
;
80 uint8_t *write_buffer
;
87 unsigned read_chunk_size
;
88 struct bit_copy_queue read_queue
;
91 /* Returns true if the string descriptor indexed by str_index in device matches string */
92 static bool string_descriptor_equal(libusb_device_handle
*device
, uint8_t str_index
,
96 char desc_string
[256]; /* Max size of string descriptor */
97 retval
= libusb_get_string_descriptor_ascii(device
, str_index
, (unsigned char *)desc_string
,
100 LOG_ERROR("libusb_get_string_descriptor_ascii() failed with %d", retval
);
103 return strncmp(string
, desc_string
, sizeof(desc_string
)) == 0;
106 /* Helper to open a libusb device that matches vid, pid, product string and/or serial string.
107 * Set any field to 0 as a wildcard. If the device is found true is returned, with ctx containing
108 * the already opened handle. ctx->interface must be set to the desired interface (channel) number
109 * prior to calling this function. */
110 static bool open_matching_device(struct mpsse_ctx
*ctx
, const uint16_t *vid
, const uint16_t *pid
,
111 const char *product
, const char *serial
)
113 libusb_device
**list
;
114 struct libusb_device_descriptor desc
;
115 struct libusb_config_descriptor
*config0
;
118 ssize_t cnt
= libusb_get_device_list(ctx
->usb_ctx
, &list
);
120 LOG_ERROR("libusb_get_device_list() failed with %zi", cnt
);
122 for (ssize_t i
= 0; i
< cnt
; i
++) {
123 libusb_device
*device
= list
[i
];
125 err
= libusb_get_device_descriptor(device
, &desc
);
126 if (err
!= LIBUSB_SUCCESS
) {
127 LOG_ERROR("libusb_get_device_descriptor() failed with %d", err
);
131 if (vid
&& *vid
!= desc
.idVendor
)
133 if (pid
&& *pid
!= desc
.idProduct
)
136 err
= libusb_open(device
, &ctx
->usb_dev
);
137 if (err
!= LIBUSB_SUCCESS
) {
138 LOG_ERROR("libusb_open() failed with %s",
139 libusb_error_name(err
));
143 if (product
&& !string_descriptor_equal(ctx
->usb_dev
, desc
.iProduct
, product
)) {
144 libusb_close(ctx
->usb_dev
);
148 if (serial
&& !string_descriptor_equal(ctx
->usb_dev
, desc
.iSerialNumber
, serial
)) {
149 libusb_close(ctx
->usb_dev
);
157 libusb_free_device_list(list
, 1);
160 LOG_ERROR("no device found");
164 err
= libusb_get_config_descriptor(libusb_get_device(ctx
->usb_dev
), 0, &config0
);
165 if (err
!= LIBUSB_SUCCESS
) {
166 LOG_ERROR("libusb_get_config_descriptor() failed with %d", err
);
167 libusb_close(ctx
->usb_dev
);
171 /* Make sure the first configuration is selected */
173 err
= libusb_get_configuration(ctx
->usb_dev
, &cfg
);
174 if (err
!= LIBUSB_SUCCESS
) {
175 LOG_ERROR("libusb_get_configuration() failed with %d", err
);
179 if (desc
.bNumConfigurations
> 0 && cfg
!= config0
->bConfigurationValue
) {
180 err
= libusb_set_configuration(ctx
->usb_dev
, config0
->bConfigurationValue
);
181 if (err
!= LIBUSB_SUCCESS
) {
182 LOG_ERROR("libusb_set_configuration() failed with %d", err
);
187 /* Try to detach ftdi_sio kernel module */
188 err
= libusb_detach_kernel_driver(ctx
->usb_dev
, ctx
->interface
);
189 if (err
!= LIBUSB_SUCCESS
&& err
!= LIBUSB_ERROR_NOT_FOUND
190 && err
!= LIBUSB_ERROR_NOT_SUPPORTED
) {
191 LOG_ERROR("libusb_detach_kernel_driver() failed with %d", err
);
195 err
= libusb_claim_interface(ctx
->usb_dev
, ctx
->interface
);
196 if (err
!= LIBUSB_SUCCESS
) {
197 LOG_ERROR("libusb_claim_interface() failed with %d", err
);
201 /* Reset FTDI device */
202 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
,
203 SIO_RESET_REQUEST
, SIO_RESET_SIO
,
204 ctx
->index
, NULL
, 0, ctx
->usb_write_timeout
);
206 LOG_ERROR("failed to reset FTDI device: %d", err
);
210 switch (desc
.bcdDevice
) {
212 ctx
->type
= TYPE_FT2232C
;
215 ctx
->type
= TYPE_FT2232H
;
218 ctx
->type
= TYPE_FT4232H
;
221 ctx
->type
= TYPE_FT232H
;
224 LOG_ERROR("unsupported FTDI chip type: 0x%04x", desc
.bcdDevice
);
228 /* Determine maximum packet size and endpoint addresses */
229 if (!(desc
.bNumConfigurations
> 0 && ctx
->interface
< config0
->bNumInterfaces
230 && config0
->interface
[ctx
->interface
].num_altsetting
> 0))
233 const struct libusb_interface_descriptor
*descriptor
;
234 descriptor
= &config0
->interface
[ctx
->interface
].altsetting
[0];
235 if (descriptor
->bNumEndpoints
!= 2)
240 for (int i
= 0; i
< descriptor
->bNumEndpoints
; i
++) {
241 if (descriptor
->endpoint
[i
].bEndpointAddress
& 0x80) {
242 ctx
->in_ep
= descriptor
->endpoint
[i
].bEndpointAddress
;
243 ctx
->max_packet_size
=
244 descriptor
->endpoint
[i
].wMaxPacketSize
;
246 ctx
->out_ep
= descriptor
->endpoint
[i
].bEndpointAddress
;
250 if (ctx
->in_ep
== 0 || ctx
->out_ep
== 0)
253 libusb_free_config_descriptor(config0
);
257 LOG_ERROR("unrecognized USB device descriptor");
259 libusb_free_config_descriptor(config0
);
260 libusb_close(ctx
->usb_dev
);
264 struct mpsse_ctx
*mpsse_open(const uint16_t *vid
, const uint16_t *pid
, const char *description
,
265 const char *serial
, int channel
)
267 struct mpsse_ctx
*ctx
= calloc(1, sizeof(*ctx
));
273 bit_copy_queue_init(&ctx
->read_queue
);
274 ctx
->read_chunk_size
= 16384;
275 ctx
->read_size
= 16384;
276 ctx
->write_size
= 16384;
277 ctx
->read_chunk
= malloc(ctx
->read_chunk_size
);
278 ctx
->read_buffer
= malloc(ctx
->read_size
);
279 ctx
->write_buffer
= malloc(ctx
->write_size
);
280 if (!ctx
->read_chunk
|| !ctx
->read_buffer
|| !ctx
->write_buffer
)
283 ctx
->interface
= channel
;
284 ctx
->index
= channel
+ 1;
285 ctx
->usb_read_timeout
= 5000;
286 ctx
->usb_write_timeout
= 5000;
288 err
= libusb_init(&ctx
->usb_ctx
);
289 if (err
!= LIBUSB_SUCCESS
) {
290 LOG_ERROR("libusb_init() failed with %d", err
);
294 if (!open_matching_device(ctx
, vid
, pid
, description
, serial
)) {
295 /* Four hex digits plus terminating zero each */
298 LOG_ERROR("unable to open ftdi device with vid %s, pid %s, description '%s' and "
300 vid
? sprintf(vidstr
, "%04x", *vid
), vidstr
: "*",
301 pid
? sprintf(pidstr
, "%04x", *pid
), pidstr
: "*",
302 description
? description
: "*",
303 serial
? serial
: "*");
308 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
,
309 SIO_SET_LATENCY_TIMER_REQUEST
, 255, ctx
->index
, NULL
, 0,
310 ctx
->usb_write_timeout
);
312 LOG_ERROR("unable to set latency timer: %d", err
);
316 err
= libusb_control_transfer(ctx
->usb_dev
,
317 FTDI_DEVICE_OUT_REQTYPE
,
318 SIO_SET_BITMODE_REQUEST
,
319 0x0b | (BITMODE_MPSSE
<< 8),
323 ctx
->usb_write_timeout
);
325 LOG_ERROR("unable to set MPSSE bitmode: %d", err
);
337 void mpsse_close(struct mpsse_ctx
*ctx
)
340 libusb_close(ctx
->usb_dev
);
342 libusb_exit(ctx
->usb_ctx
);
343 bit_copy_discard(&ctx
->read_queue
);
344 if (ctx
->write_buffer
)
345 free(ctx
->write_buffer
);
346 if (ctx
->read_buffer
)
347 free(ctx
->read_buffer
);
349 free(ctx
->read_chunk
);
354 bool mpsse_is_high_speed(struct mpsse_ctx
*ctx
)
356 return ctx
->type
!= TYPE_FT2232C
;
359 void mpsse_purge(struct mpsse_ctx
*ctx
)
363 ctx
->write_count
= 0;
365 bit_copy_discard(&ctx
->read_queue
);
366 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
, SIO_RESET_REQUEST
,
367 SIO_RESET_PURGE_RX
, ctx
->index
, NULL
, 0, ctx
->usb_write_timeout
);
369 LOG_ERROR("unable to purge ftdi rx buffers: %d", err
);
373 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
, SIO_RESET_REQUEST
,
374 SIO_RESET_PURGE_TX
, ctx
->index
, NULL
, 0, ctx
->usb_write_timeout
);
376 LOG_ERROR("unable to purge ftdi tx buffers: %d", err
);
381 static unsigned buffer_write_space(struct mpsse_ctx
*ctx
)
383 /* Reserve one byte for SEND_IMMEDIATE */
384 return ctx
->write_size
- ctx
->write_count
- 1;
387 static unsigned buffer_read_space(struct mpsse_ctx
*ctx
)
389 return ctx
->read_size
- ctx
->read_count
;
392 static void buffer_write_byte(struct mpsse_ctx
*ctx
, uint8_t data
)
394 DEBUG_IO("%02x", data
);
395 assert(ctx
->write_count
< ctx
->write_size
);
396 ctx
->write_buffer
[ctx
->write_count
++] = data
;
399 static unsigned buffer_write(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
,
402 DEBUG_IO("%d bits", bit_count
);
403 assert(ctx
->write_count
+ DIV_ROUND_UP(bit_count
, 8) <= ctx
->write_size
);
404 bit_copy(ctx
->write_buffer
+ ctx
->write_count
, 0, out
, out_offset
, bit_count
);
405 ctx
->write_count
+= DIV_ROUND_UP(bit_count
, 8);
409 static unsigned buffer_add_read(struct mpsse_ctx
*ctx
, uint8_t *in
, unsigned in_offset
,
410 unsigned bit_count
, unsigned offset
)
412 DEBUG_IO("%d bits, offset %d", bit_count
, offset
);
413 assert(ctx
->read_count
+ DIV_ROUND_UP(bit_count
, 8) <= ctx
->read_size
);
414 bit_copy_queued(&ctx
->read_queue
, in
, in_offset
, ctx
->read_buffer
+ ctx
->read_count
, offset
,
416 ctx
->read_count
+= DIV_ROUND_UP(bit_count
, 8);
420 int mpsse_clock_data_out(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
,
421 unsigned length
, uint8_t mode
)
423 return mpsse_clock_data(ctx
, out
, out_offset
, 0, 0, length
, mode
);
426 int mpsse_clock_data_in(struct mpsse_ctx
*ctx
, uint8_t *in
, unsigned in_offset
, unsigned length
,
429 return mpsse_clock_data(ctx
, 0, 0, in
, in_offset
, length
, mode
);
432 int mpsse_clock_data(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
, uint8_t *in
,
433 unsigned in_offset
, unsigned length
, uint8_t mode
)
435 /* TODO: Fix MSB first modes */
436 DEBUG_IO("%s%s %d bits", in
? "in" : "", out
? "out" : "", length
);
437 int retval
= ERROR_OK
;
439 /* TODO: On H chips, use command 0x8E/0x8F if in and out are both 0 */
440 if (out
|| (!out
&& !in
))
446 /* Guarantee buffer space enough for a minimum size transfer */
447 if (buffer_write_space(ctx
) + (length
< 8) < (out
|| (!out
&& !in
) ? 4 : 3)
448 || (in
&& buffer_read_space(ctx
) < 1))
449 retval
= mpsse_flush(ctx
);
452 /* Transfer remaining bits in bit mode */
453 buffer_write_byte(ctx
, 0x02 | mode
);
454 buffer_write_byte(ctx
, length
- 1);
456 out_offset
+= buffer_write(ctx
, out
, out_offset
, length
);
458 in_offset
+= buffer_add_read(ctx
, in
, in_offset
, length
, 8 - length
);
460 buffer_write_byte(ctx
, 0x00);
464 unsigned this_bytes
= length
/ 8;
465 /* MPSSE command limit */
466 if (this_bytes
> 65536)
468 /* Buffer space limit. We already made sure there's space for the minimum
470 if ((out
|| (!out
&& !in
)) && this_bytes
+ 3 > buffer_write_space(ctx
))
471 this_bytes
= buffer_write_space(ctx
) - 3;
472 if (in
&& this_bytes
> buffer_read_space(ctx
))
473 this_bytes
= buffer_read_space(ctx
);
475 if (this_bytes
> 0) {
476 buffer_write_byte(ctx
, mode
);
477 buffer_write_byte(ctx
, (this_bytes
- 1) & 0xff);
478 buffer_write_byte(ctx
, (this_bytes
- 1) >> 8);
480 out_offset
+= buffer_write(ctx
,
485 in_offset
+= buffer_add_read(ctx
,
491 for (unsigned n
= 0; n
< this_bytes
; n
++)
492 buffer_write_byte(ctx
, 0x00);
493 length
-= this_bytes
* 8;
500 int mpsse_clock_tms_cs_out(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
,
501 unsigned length
, bool tdi
, uint8_t mode
)
503 return mpsse_clock_tms_cs(ctx
, out
, out_offset
, 0, 0, length
, tdi
, mode
);
506 int mpsse_clock_tms_cs(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
, uint8_t *in
,
507 unsigned in_offset
, unsigned length
, bool tdi
, uint8_t mode
)
509 DEBUG_IO("%sout %d bits, tdi=%d", in
? "in" : "", length
, tdi
);
511 int retval
= ERROR_OK
;
518 /* Guarantee buffer space enough for a minimum size transfer */
519 if (buffer_write_space(ctx
) < 3 || (in
&& buffer_read_space(ctx
) < 1))
520 retval
= mpsse_flush(ctx
);
523 unsigned this_bits
= length
;
524 /* MPSSE command limit */
525 /* NOTE: there's a report of an FT2232 bug in this area, where shifting
526 * exactly 7 bits can make problems with TMS signaling for the last
529 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
535 buffer_write_byte(ctx
, mode
);
536 buffer_write_byte(ctx
, this_bits
- 1);
538 /* TODO: Fix MSB first, if allowed in MPSSE */
539 bit_copy(&data
, 0, out
, out_offset
, this_bits
);
540 out_offset
+= this_bits
;
541 buffer_write_byte(ctx
, data
| (tdi
? 0x80 : 0x00));
543 in_offset
+= buffer_add_read(ctx
,
554 int mpsse_set_data_bits_low_byte(struct mpsse_ctx
*ctx
, uint8_t data
, uint8_t dir
)
557 int retval
= ERROR_OK
;
559 if (buffer_write_space(ctx
) < 3)
560 retval
= mpsse_flush(ctx
);
562 buffer_write_byte(ctx
, 0x80);
563 buffer_write_byte(ctx
, data
);
564 buffer_write_byte(ctx
, dir
);
569 int mpsse_set_data_bits_high_byte(struct mpsse_ctx
*ctx
, uint8_t data
, uint8_t dir
)
572 int retval
= ERROR_OK
;
574 if (buffer_write_space(ctx
) < 3)
575 retval
= mpsse_flush(ctx
);
577 buffer_write_byte(ctx
, 0x82);
578 buffer_write_byte(ctx
, data
);
579 buffer_write_byte(ctx
, dir
);
584 int mpsse_read_data_bits_low_byte(struct mpsse_ctx
*ctx
, uint8_t *data
)
587 int retval
= ERROR_OK
;
589 if (buffer_write_space(ctx
) < 1)
590 retval
= mpsse_flush(ctx
);
592 buffer_write_byte(ctx
, 0x81);
593 buffer_add_read(ctx
, data
, 0, 8, 0);
598 int mpsse_read_data_bits_high_byte(struct mpsse_ctx
*ctx
, uint8_t *data
)
601 int retval
= ERROR_OK
;
603 if (buffer_write_space(ctx
) < 1)
604 retval
= mpsse_flush(ctx
);
606 buffer_write_byte(ctx
, 0x83);
607 buffer_add_read(ctx
, data
, 0, 8, 0);
612 static int single_byte_boolean_helper(struct mpsse_ctx
*ctx
, bool var
, uint8_t val_if_true
,
613 uint8_t val_if_false
)
615 int retval
= ERROR_OK
;
617 if (buffer_write_space(ctx
) < 1)
618 retval
= mpsse_flush(ctx
);
620 buffer_write_byte(ctx
, var
? val_if_true
: val_if_false
);
625 int mpsse_loopback_config(struct mpsse_ctx
*ctx
, bool enable
)
627 LOG_DEBUG("%s", enable
? "on" : "off");
628 return single_byte_boolean_helper(ctx
, enable
, 0x84, 0x85);
631 int mpsse_set_divisor(struct mpsse_ctx
*ctx
, uint16_t divisor
)
633 LOG_DEBUG("%d", divisor
);
634 int retval
= ERROR_OK
;
636 if (buffer_write_space(ctx
) < 3)
637 retval
= mpsse_flush(ctx
);
639 buffer_write_byte(ctx
, 0x86);
640 buffer_write_byte(ctx
, divisor
& 0xff);
641 buffer_write_byte(ctx
, divisor
>> 8);
646 int mpsse_divide_by_5_config(struct mpsse_ctx
*ctx
, bool enable
)
648 if (!mpsse_is_high_speed(ctx
))
651 LOG_DEBUG("%s", enable
? "on" : "off");
653 return single_byte_boolean_helper(ctx
, enable
, 0x8b, 0x8a);
656 int mpsse_rtck_config(struct mpsse_ctx
*ctx
, bool enable
)
658 if (!mpsse_is_high_speed(ctx
))
661 LOG_DEBUG("%s", enable
? "on" : "off");
663 return single_byte_boolean_helper(ctx
, enable
, 0x96, 0x97);
666 int mpsse_set_frequency(struct mpsse_ctx
*ctx
, int frequency
)
668 LOG_DEBUG("target %d Hz", frequency
);
669 assert(frequency
>= 0);
673 return mpsse_rtck_config(ctx
, true);
675 mpsse_rtck_config(ctx
, false); /* just try */
677 if (frequency
> 60000000 / 2 / 65536 && mpsse_is_high_speed(ctx
)) {
678 int retval
= mpsse_divide_by_5_config(ctx
, false);
679 if (retval
!= ERROR_OK
)
681 base_clock
= 60000000;
683 mpsse_divide_by_5_config(ctx
, true); /* just try */
684 base_clock
= 12000000;
687 int divisor
= (base_clock
/ 2 + frequency
- 1) / frequency
- 1;
690 assert(divisor
>= 0);
692 int retval
= mpsse_set_divisor(ctx
, divisor
);
693 if (retval
!= ERROR_OK
)
696 frequency
= base_clock
/ 2 / (1 + divisor
);
697 LOG_DEBUG("actually %d Hz", frequency
);
702 /* Context needed by the callbacks */
703 struct transfer_result
{
704 struct mpsse_ctx
*ctx
;
706 unsigned transferred
;
709 static LIBUSB_CALL
void read_cb(struct libusb_transfer
*transfer
)
711 struct transfer_result
*res
= (struct transfer_result
*)transfer
->user_data
;
712 struct mpsse_ctx
*ctx
= res
->ctx
;
714 unsigned packet_size
= ctx
->max_packet_size
;
716 DEBUG_PRINT_BUF(transfer
->buffer
, transfer
->actual_length
);
718 /* Strip the two status bytes sent at the beginning of each USB packet
719 * while copying the chunk buffer to the read buffer */
720 unsigned num_packets
= DIV_ROUND_UP(transfer
->actual_length
, packet_size
);
721 unsigned chunk_remains
= transfer
->actual_length
;
722 for (unsigned i
= 0; i
< num_packets
&& chunk_remains
> 2; i
++) {
723 unsigned this_size
= packet_size
- 2;
724 if (this_size
> chunk_remains
- 2)
725 this_size
= chunk_remains
- 2;
726 if (this_size
> ctx
->read_count
- res
->transferred
)
727 this_size
= ctx
->read_count
- res
->transferred
;
728 memcpy(ctx
->read_buffer
+ res
->transferred
,
729 ctx
->read_chunk
+ packet_size
* i
+ 2,
731 res
->transferred
+= this_size
;
732 chunk_remains
-= this_size
+ 2;
733 if (res
->transferred
== ctx
->read_count
) {
739 DEBUG_IO("raw chunk %d, transferred %d of %d", transfer
->actual_length
, res
->transferred
,
743 if (libusb_submit_transfer(transfer
) != LIBUSB_SUCCESS
)
747 static LIBUSB_CALL
void write_cb(struct libusb_transfer
*transfer
)
749 struct transfer_result
*res
= (struct transfer_result
*)transfer
->user_data
;
750 struct mpsse_ctx
*ctx
= res
->ctx
;
752 res
->transferred
+= transfer
->actual_length
;
754 DEBUG_IO("transferred %d of %d", res
->transferred
, ctx
->write_count
);
756 DEBUG_PRINT_BUF(transfer
->buffer
, transfer
->actual_length
);
758 if (res
->transferred
== ctx
->write_count
)
761 transfer
->length
= ctx
->write_count
- res
->transferred
;
762 transfer
->buffer
= ctx
->write_buffer
+ res
->transferred
;
763 if (libusb_submit_transfer(transfer
) != LIBUSB_SUCCESS
)
768 int mpsse_flush(struct mpsse_ctx
*ctx
)
770 DEBUG_IO("write %d%s, read %d", ctx
->write_count
, ctx
->read_count
? "+1" : "",
772 assert(ctx
->write_count
> 0 || ctx
->read_count
== 0); /* No read data without write data */
773 int retval
= ERROR_OK
;
775 if (ctx
->write_count
== 0)
778 struct libusb_transfer
*read_transfer
= 0;
779 struct transfer_result read_result
= { .ctx
= ctx
, .done
= true };
780 if (ctx
->read_count
) {
781 buffer_write_byte(ctx
, 0x87); /* SEND_IMMEDIATE */
782 read_result
.done
= false;
783 /* delay read transaction to ensure the FTDI chip can support us with data
784 immediately after processing the MPSSE commands in the write transaction */
787 struct transfer_result write_result
= { .ctx
= ctx
, .done
= false };
788 struct libusb_transfer
*write_transfer
= libusb_alloc_transfer(0);
789 libusb_fill_bulk_transfer(write_transfer
, ctx
->usb_dev
, ctx
->out_ep
, ctx
->write_buffer
,
790 ctx
->write_count
, write_cb
, &write_result
, ctx
->usb_write_timeout
);
791 retval
= libusb_submit_transfer(write_transfer
);
793 if (ctx
->read_count
) {
794 read_transfer
= libusb_alloc_transfer(0);
795 libusb_fill_bulk_transfer(read_transfer
, ctx
->usb_dev
, ctx
->in_ep
, ctx
->read_chunk
,
796 ctx
->read_chunk_size
, read_cb
, &read_result
,
797 ctx
->usb_read_timeout
);
798 retval
= libusb_submit_transfer(read_transfer
);
801 /* Polling loop, more or less taken from libftdi */
802 while (!write_result
.done
|| !read_result
.done
) {
803 retval
= libusb_handle_events(ctx
->usb_ctx
);
805 if (retval
!= LIBUSB_SUCCESS
&& retval
!= LIBUSB_ERROR_INTERRUPTED
) {
806 libusb_cancel_transfer(write_transfer
);
808 libusb_cancel_transfer(read_transfer
);
809 while (!write_result
.done
|| !read_result
.done
)
810 if (libusb_handle_events(ctx
->usb_ctx
) != LIBUSB_SUCCESS
)
815 if (retval
!= LIBUSB_SUCCESS
) {
816 LOG_ERROR("libusb_handle_events() failed with %d", retval
);
818 } else if (write_result
.transferred
< ctx
->write_count
) {
819 LOG_ERROR("ftdi device did not accept all data: %d, tried %d",
820 write_result
.transferred
,
823 } else if (read_result
.transferred
< ctx
->read_count
) {
824 LOG_ERROR("ftdi device did not return all data: %d, expected %d",
825 read_result
.transferred
,
828 } else if (ctx
->read_count
) {
829 ctx
->write_count
= 0;
831 bit_copy_execute(&ctx
->read_queue
);
834 ctx
->write_count
= 0;
835 bit_copy_discard(&ctx
->read_queue
);
839 libusb_free_transfer(write_transfer
);
841 libusb_free_transfer(read_transfer
);
843 if (retval
!= ERROR_OK
)
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)