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, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
24 #include "helper/log.h"
27 /* Compatibility define for older libusb-1.0 */
32 #ifdef _DEBUG_JTAG_IO_
33 #define DEBUG_IO(expr...) LOG_DEBUG(expr)
34 #define DEBUG_PRINT_BUF(buf, len) \
36 char buf_string[32 * 3 + 1]; \
37 int buf_string_pos = 0; \
38 for (int i = 0; i < len; i++) { \
39 buf_string_pos += sprintf(buf_string + buf_string_pos, " %02x", buf[i]); \
40 if (i % 32 == 32 - 1) { \
41 LOG_DEBUG("%s", buf_string); \
45 if (buf_string_pos > 0) \
46 LOG_DEBUG("%s", buf_string);\
49 #define DEBUG_IO(expr...) do {} while (0)
50 #define DEBUG_PRINT_BUF(buf, len) do {} while (0)
53 #define FTDI_DEVICE_OUT_REQTYPE (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE)
54 #define FTDI_DEVICE_IN_REQTYPE (0x80 | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE)
56 #define BITMODE_MPSSE 0x02
58 #define SIO_RESET_REQUEST 0x00
59 #define SIO_SET_LATENCY_TIMER_REQUEST 0x09
60 #define SIO_GET_LATENCY_TIMER_REQUEST 0x0A
61 #define SIO_SET_BITMODE_REQUEST 0x0B
63 #define SIO_RESET_SIO 0
64 #define SIO_RESET_PURGE_RX 1
65 #define SIO_RESET_PURGE_TX 2
68 libusb_context
*usb_ctx
;
69 libusb_device_handle
*usb_dev
;
70 unsigned int usb_write_timeout
;
71 unsigned int usb_read_timeout
;
74 uint16_t max_packet_size
;
77 enum ftdi_chip_type type
;
78 uint8_t *write_buffer
;
85 unsigned read_chunk_size
;
86 struct bit_copy_queue read_queue
;
90 /* Returns true if the string descriptor indexed by str_index in device matches string */
91 static bool string_descriptor_equal(libusb_device_handle
*device
, uint8_t str_index
,
95 char desc_string
[256]; /* Max size of string descriptor */
96 retval
= libusb_get_string_descriptor_ascii(device
, str_index
, (unsigned char *)desc_string
,
99 LOG_ERROR("libusb_get_string_descriptor_ascii() failed with %s", libusb_error_name(retval
));
102 return strncmp(string
, desc_string
, sizeof(desc_string
)) == 0;
105 static bool device_location_equal(libusb_device
*device
, const char *location
)
108 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
109 char *loc
= strdup(location
);
110 uint8_t port_path
[7];
111 int path_step
, path_len
;
112 uint8_t dev_bus
= libusb_get_bus_number(device
);
115 path_len
= libusb_get_port_numbers(device
, port_path
, 7);
116 if (path_len
== LIBUSB_ERROR_OVERFLOW
) {
117 LOG_ERROR("cannot determine path to usb device! (more than 7 ports in path)");
121 LOG_DEBUG("device path has %i steps", path_len
);
123 ptr
= strtok(loc
, ":");
125 LOG_DEBUG("no ':' in path");
128 if (atoi(ptr
) != dev_bus
) {
129 LOG_DEBUG("bus mismatch");
134 while (path_step
< 7) {
135 ptr
= strtok(NULL
, ",");
137 LOG_DEBUG("no more tokens in path at step %i", path_step
);
141 if (path_step
< path_len
142 && atoi(ptr
) != port_path
[path_step
]) {
143 LOG_DEBUG("path mismatch at step %i", path_step
);
150 /* walked the full path, all elements match */
151 if (path_step
== path_len
)
160 /* Helper to open a libusb device that matches vid, pid, product string and/or serial string.
161 * Set any field to 0 as a wildcard. If the device is found true is returned, with ctx containing
162 * the already opened handle. ctx->interface must be set to the desired interface (channel) number
163 * prior to calling this function. */
164 static bool open_matching_device(struct mpsse_ctx
*ctx
, const uint16_t *vid
, const uint16_t *pid
,
165 const char *product
, const char *serial
, const char *location
)
167 libusb_device
**list
;
168 struct libusb_device_descriptor desc
;
169 struct libusb_config_descriptor
*config0
;
172 ssize_t cnt
= libusb_get_device_list(ctx
->usb_ctx
, &list
);
174 LOG_ERROR("libusb_get_device_list() failed with %s", libusb_error_name(cnt
));
176 for (ssize_t i
= 0; i
< cnt
; i
++) {
177 libusb_device
*device
= list
[i
];
179 err
= libusb_get_device_descriptor(device
, &desc
);
180 if (err
!= LIBUSB_SUCCESS
) {
181 LOG_ERROR("libusb_get_device_descriptor() failed with %s", libusb_error_name(err
));
185 if (vid
&& *vid
!= desc
.idVendor
)
187 if (pid
&& *pid
!= desc
.idProduct
)
190 err
= libusb_open(device
, &ctx
->usb_dev
);
191 if (err
!= LIBUSB_SUCCESS
) {
192 LOG_ERROR("libusb_open() failed with %s",
193 libusb_error_name(err
));
197 if (location
&& !device_location_equal(device
, location
)) {
198 libusb_close(ctx
->usb_dev
);
202 if (product
&& !string_descriptor_equal(ctx
->usb_dev
, desc
.iProduct
, product
)) {
203 libusb_close(ctx
->usb_dev
);
207 if (serial
&& !string_descriptor_equal(ctx
->usb_dev
, desc
.iSerialNumber
, serial
)) {
208 libusb_close(ctx
->usb_dev
);
216 libusb_free_device_list(list
, 1);
219 LOG_ERROR("no device found");
223 err
= libusb_get_config_descriptor(libusb_get_device(ctx
->usb_dev
), 0, &config0
);
224 if (err
!= LIBUSB_SUCCESS
) {
225 LOG_ERROR("libusb_get_config_descriptor() failed with %s", libusb_error_name(err
));
226 libusb_close(ctx
->usb_dev
);
230 /* Make sure the first configuration is selected */
232 err
= libusb_get_configuration(ctx
->usb_dev
, &cfg
);
233 if (err
!= LIBUSB_SUCCESS
) {
234 LOG_ERROR("libusb_get_configuration() failed with %s", libusb_error_name(err
));
238 if (desc
.bNumConfigurations
> 0 && cfg
!= config0
->bConfigurationValue
) {
239 err
= libusb_set_configuration(ctx
->usb_dev
, config0
->bConfigurationValue
);
240 if (err
!= LIBUSB_SUCCESS
) {
241 LOG_ERROR("libusb_set_configuration() failed with %s", libusb_error_name(err
));
246 /* Try to detach ftdi_sio kernel module */
247 err
= libusb_detach_kernel_driver(ctx
->usb_dev
, ctx
->interface
);
248 if (err
!= LIBUSB_SUCCESS
&& err
!= LIBUSB_ERROR_NOT_FOUND
249 && err
!= LIBUSB_ERROR_NOT_SUPPORTED
) {
250 LOG_ERROR("libusb_detach_kernel_driver() failed with %s", libusb_error_name(err
));
254 err
= libusb_claim_interface(ctx
->usb_dev
, ctx
->interface
);
255 if (err
!= LIBUSB_SUCCESS
) {
256 LOG_ERROR("libusb_claim_interface() failed with %s", libusb_error_name(err
));
260 /* Reset FTDI device */
261 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
,
262 SIO_RESET_REQUEST
, SIO_RESET_SIO
,
263 ctx
->index
, NULL
, 0, ctx
->usb_write_timeout
);
265 LOG_ERROR("failed to reset FTDI device: %s", libusb_error_name(err
));
269 switch (desc
.bcdDevice
) {
271 ctx
->type
= TYPE_FT2232C
;
274 ctx
->type
= TYPE_FT2232H
;
277 ctx
->type
= TYPE_FT4232H
;
280 ctx
->type
= TYPE_FT232H
;
283 LOG_ERROR("unsupported FTDI chip type: 0x%04x", desc
.bcdDevice
);
287 /* Determine maximum packet size and endpoint addresses */
288 if (!(desc
.bNumConfigurations
> 0 && ctx
->interface
< config0
->bNumInterfaces
289 && config0
->interface
[ctx
->interface
].num_altsetting
> 0))
292 const struct libusb_interface_descriptor
*descriptor
;
293 descriptor
= &config0
->interface
[ctx
->interface
].altsetting
[0];
294 if (descriptor
->bNumEndpoints
!= 2)
299 for (int i
= 0; i
< descriptor
->bNumEndpoints
; i
++) {
300 if (descriptor
->endpoint
[i
].bEndpointAddress
& 0x80) {
301 ctx
->in_ep
= descriptor
->endpoint
[i
].bEndpointAddress
;
302 ctx
->max_packet_size
=
303 descriptor
->endpoint
[i
].wMaxPacketSize
;
305 ctx
->out_ep
= descriptor
->endpoint
[i
].bEndpointAddress
;
309 if (ctx
->in_ep
== 0 || ctx
->out_ep
== 0)
312 libusb_free_config_descriptor(config0
);
316 LOG_ERROR("unrecognized USB device descriptor");
318 libusb_free_config_descriptor(config0
);
319 libusb_close(ctx
->usb_dev
);
323 struct mpsse_ctx
*mpsse_open(const uint16_t *vid
, const uint16_t *pid
, const char *description
,
324 const char *serial
, const char *location
, int channel
)
326 struct mpsse_ctx
*ctx
= calloc(1, sizeof(*ctx
));
332 bit_copy_queue_init(&ctx
->read_queue
);
333 ctx
->read_chunk_size
= 16384;
334 ctx
->read_size
= 16384;
335 ctx
->write_size
= 16384;
336 ctx
->read_chunk
= malloc(ctx
->read_chunk_size
);
337 ctx
->read_buffer
= malloc(ctx
->read_size
);
338 ctx
->write_buffer
= malloc(ctx
->write_size
);
339 if (!ctx
->read_chunk
|| !ctx
->read_buffer
|| !ctx
->write_buffer
)
342 ctx
->interface
= channel
;
343 ctx
->index
= channel
+ 1;
344 ctx
->usb_read_timeout
= 5000;
345 ctx
->usb_write_timeout
= 5000;
347 err
= libusb_init(&ctx
->usb_ctx
);
348 if (err
!= LIBUSB_SUCCESS
) {
349 LOG_ERROR("libusb_init() failed with %s", libusb_error_name(err
));
353 if (!open_matching_device(ctx
, vid
, pid
, description
, serial
, location
)) {
354 /* Four hex digits plus terminating zero each */
357 LOG_ERROR("unable to open ftdi device with vid %s, pid %s, description '%s', "
358 "serial '%s' at bus location '%s'",
359 vid
? sprintf(vidstr
, "%04x", *vid
), vidstr
: "*",
360 pid
? sprintf(pidstr
, "%04x", *pid
), pidstr
: "*",
361 description
? description
: "*",
362 serial
? serial
: "*",
363 location
? location
: "*");
368 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
,
369 SIO_SET_LATENCY_TIMER_REQUEST
, 255, ctx
->index
, NULL
, 0,
370 ctx
->usb_write_timeout
);
372 LOG_ERROR("unable to set latency timer: %s", libusb_error_name(err
));
376 err
= libusb_control_transfer(ctx
->usb_dev
,
377 FTDI_DEVICE_OUT_REQTYPE
,
378 SIO_SET_BITMODE_REQUEST
,
379 0x0b | (BITMODE_MPSSE
<< 8),
383 ctx
->usb_write_timeout
);
385 LOG_ERROR("unable to set MPSSE bitmode: %s", libusb_error_name(err
));
397 void mpsse_close(struct mpsse_ctx
*ctx
)
400 libusb_close(ctx
->usb_dev
);
402 libusb_exit(ctx
->usb_ctx
);
403 bit_copy_discard(&ctx
->read_queue
);
404 if (ctx
->write_buffer
)
405 free(ctx
->write_buffer
);
406 if (ctx
->read_buffer
)
407 free(ctx
->read_buffer
);
409 free(ctx
->read_chunk
);
414 bool mpsse_is_high_speed(struct mpsse_ctx
*ctx
)
416 return ctx
->type
!= TYPE_FT2232C
;
419 void mpsse_purge(struct mpsse_ctx
*ctx
)
423 ctx
->write_count
= 0;
425 ctx
->retval
= ERROR_OK
;
426 bit_copy_discard(&ctx
->read_queue
);
427 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
, SIO_RESET_REQUEST
,
428 SIO_RESET_PURGE_RX
, ctx
->index
, NULL
, 0, ctx
->usb_write_timeout
);
430 LOG_ERROR("unable to purge ftdi rx buffers: %s", libusb_error_name(err
));
434 err
= libusb_control_transfer(ctx
->usb_dev
, FTDI_DEVICE_OUT_REQTYPE
, SIO_RESET_REQUEST
,
435 SIO_RESET_PURGE_TX
, ctx
->index
, NULL
, 0, ctx
->usb_write_timeout
);
437 LOG_ERROR("unable to purge ftdi tx buffers: %s", libusb_error_name(err
));
442 static unsigned buffer_write_space(struct mpsse_ctx
*ctx
)
444 /* Reserve one byte for SEND_IMMEDIATE */
445 return ctx
->write_size
- ctx
->write_count
- 1;
448 static unsigned buffer_read_space(struct mpsse_ctx
*ctx
)
450 return ctx
->read_size
- ctx
->read_count
;
453 static void buffer_write_byte(struct mpsse_ctx
*ctx
, uint8_t data
)
455 DEBUG_IO("%02x", data
);
456 assert(ctx
->write_count
< ctx
->write_size
);
457 ctx
->write_buffer
[ctx
->write_count
++] = data
;
460 static unsigned buffer_write(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
,
463 DEBUG_IO("%d bits", bit_count
);
464 assert(ctx
->write_count
+ DIV_ROUND_UP(bit_count
, 8) <= ctx
->write_size
);
465 bit_copy(ctx
->write_buffer
+ ctx
->write_count
, 0, out
, out_offset
, bit_count
);
466 ctx
->write_count
+= DIV_ROUND_UP(bit_count
, 8);
470 static unsigned buffer_add_read(struct mpsse_ctx
*ctx
, uint8_t *in
, unsigned in_offset
,
471 unsigned bit_count
, unsigned offset
)
473 DEBUG_IO("%d bits, offset %d", bit_count
, offset
);
474 assert(ctx
->read_count
+ DIV_ROUND_UP(bit_count
, 8) <= ctx
->read_size
);
475 bit_copy_queued(&ctx
->read_queue
, in
, in_offset
, ctx
->read_buffer
+ ctx
->read_count
, offset
,
477 ctx
->read_count
+= DIV_ROUND_UP(bit_count
, 8);
481 void mpsse_clock_data_out(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
,
482 unsigned length
, uint8_t mode
)
484 mpsse_clock_data(ctx
, out
, out_offset
, 0, 0, length
, mode
);
487 void mpsse_clock_data_in(struct mpsse_ctx
*ctx
, uint8_t *in
, unsigned in_offset
, unsigned length
,
490 mpsse_clock_data(ctx
, 0, 0, in
, in_offset
, length
, mode
);
493 void mpsse_clock_data(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
, uint8_t *in
,
494 unsigned in_offset
, unsigned length
, uint8_t mode
)
496 /* TODO: Fix MSB first modes */
497 DEBUG_IO("%s%s %d bits", in
? "in" : "", out
? "out" : "", length
);
499 if (ctx
->retval
!= ERROR_OK
) {
500 DEBUG_IO("Ignoring command due to previous error");
504 /* TODO: On H chips, use command 0x8E/0x8F if in and out are both 0 */
505 if (out
|| (!out
&& !in
))
511 /* Guarantee buffer space enough for a minimum size transfer */
512 if (buffer_write_space(ctx
) + (length
< 8) < (out
|| (!out
&& !in
) ? 4 : 3)
513 || (in
&& buffer_read_space(ctx
) < 1))
514 ctx
->retval
= mpsse_flush(ctx
);
517 /* Transfer remaining bits in bit mode */
518 buffer_write_byte(ctx
, 0x02 | mode
);
519 buffer_write_byte(ctx
, length
- 1);
521 out_offset
+= buffer_write(ctx
, out
, out_offset
, length
);
523 in_offset
+= buffer_add_read(ctx
, in
, in_offset
, length
, 8 - length
);
525 buffer_write_byte(ctx
, 0x00);
529 unsigned this_bytes
= length
/ 8;
530 /* MPSSE command limit */
531 if (this_bytes
> 65536)
533 /* Buffer space limit. We already made sure there's space for the minimum
535 if ((out
|| (!out
&& !in
)) && this_bytes
+ 3 > buffer_write_space(ctx
))
536 this_bytes
= buffer_write_space(ctx
) - 3;
537 if (in
&& this_bytes
> buffer_read_space(ctx
))
538 this_bytes
= buffer_read_space(ctx
);
540 if (this_bytes
> 0) {
541 buffer_write_byte(ctx
, mode
);
542 buffer_write_byte(ctx
, (this_bytes
- 1) & 0xff);
543 buffer_write_byte(ctx
, (this_bytes
- 1) >> 8);
545 out_offset
+= buffer_write(ctx
,
550 in_offset
+= buffer_add_read(ctx
,
556 for (unsigned n
= 0; n
< this_bytes
; n
++)
557 buffer_write_byte(ctx
, 0x00);
558 length
-= this_bytes
* 8;
564 void mpsse_clock_tms_cs_out(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
,
565 unsigned length
, bool tdi
, uint8_t mode
)
567 mpsse_clock_tms_cs(ctx
, out
, out_offset
, 0, 0, length
, tdi
, mode
);
570 void mpsse_clock_tms_cs(struct mpsse_ctx
*ctx
, const uint8_t *out
, unsigned out_offset
, uint8_t *in
,
571 unsigned in_offset
, unsigned length
, bool tdi
, uint8_t mode
)
573 DEBUG_IO("%sout %d bits, tdi=%d", in
? "in" : "", length
, tdi
);
576 if (ctx
->retval
!= ERROR_OK
) {
577 DEBUG_IO("Ignoring command due to previous error");
586 /* Guarantee buffer space enough for a minimum size transfer */
587 if (buffer_write_space(ctx
) < 3 || (in
&& buffer_read_space(ctx
) < 1))
588 ctx
->retval
= mpsse_flush(ctx
);
591 unsigned this_bits
= length
;
592 /* MPSSE command limit */
593 /* NOTE: there's a report of an FT2232 bug in this area, where shifting
594 * exactly 7 bits can make problems with TMS signaling for the last
597 * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
603 buffer_write_byte(ctx
, mode
);
604 buffer_write_byte(ctx
, this_bits
- 1);
606 /* TODO: Fix MSB first, if allowed in MPSSE */
607 bit_copy(&data
, 0, out
, out_offset
, this_bits
);
608 out_offset
+= this_bits
;
609 buffer_write_byte(ctx
, data
| (tdi
? 0x80 : 0x00));
611 in_offset
+= buffer_add_read(ctx
,
621 void mpsse_set_data_bits_low_byte(struct mpsse_ctx
*ctx
, uint8_t data
, uint8_t dir
)
625 if (ctx
->retval
!= ERROR_OK
) {
626 DEBUG_IO("Ignoring command due to previous error");
630 if (buffer_write_space(ctx
) < 3)
631 ctx
->retval
= mpsse_flush(ctx
);
633 buffer_write_byte(ctx
, 0x80);
634 buffer_write_byte(ctx
, data
);
635 buffer_write_byte(ctx
, dir
);
638 void mpsse_set_data_bits_high_byte(struct mpsse_ctx
*ctx
, uint8_t data
, uint8_t dir
)
642 if (ctx
->retval
!= ERROR_OK
) {
643 DEBUG_IO("Ignoring command due to previous error");
647 if (buffer_write_space(ctx
) < 3)
648 ctx
->retval
= mpsse_flush(ctx
);
650 buffer_write_byte(ctx
, 0x82);
651 buffer_write_byte(ctx
, data
);
652 buffer_write_byte(ctx
, dir
);
655 void mpsse_read_data_bits_low_byte(struct mpsse_ctx
*ctx
, uint8_t *data
)
659 if (ctx
->retval
!= ERROR_OK
) {
660 DEBUG_IO("Ignoring command due to previous error");
664 if (buffer_write_space(ctx
) < 1 || buffer_read_space(ctx
) < 1)
665 ctx
->retval
= mpsse_flush(ctx
);
667 buffer_write_byte(ctx
, 0x81);
668 buffer_add_read(ctx
, data
, 0, 8, 0);
671 void mpsse_read_data_bits_high_byte(struct mpsse_ctx
*ctx
, uint8_t *data
)
675 if (ctx
->retval
!= ERROR_OK
) {
676 DEBUG_IO("Ignoring command due to previous error");
680 if (buffer_write_space(ctx
) < 1 || buffer_read_space(ctx
) < 1)
681 ctx
->retval
= mpsse_flush(ctx
);
683 buffer_write_byte(ctx
, 0x83);
684 buffer_add_read(ctx
, data
, 0, 8, 0);
687 static void single_byte_boolean_helper(struct mpsse_ctx
*ctx
, bool var
, uint8_t val_if_true
,
688 uint8_t val_if_false
)
690 if (ctx
->retval
!= ERROR_OK
) {
691 DEBUG_IO("Ignoring command due to previous error");
695 if (buffer_write_space(ctx
) < 1)
696 ctx
->retval
= mpsse_flush(ctx
);
698 buffer_write_byte(ctx
, var
? val_if_true
: val_if_false
);
701 void mpsse_loopback_config(struct mpsse_ctx
*ctx
, bool enable
)
703 LOG_DEBUG("%s", enable
? "on" : "off");
704 single_byte_boolean_helper(ctx
, enable
, 0x84, 0x85);
707 void mpsse_set_divisor(struct mpsse_ctx
*ctx
, uint16_t divisor
)
709 LOG_DEBUG("%d", divisor
);
711 if (ctx
->retval
!= ERROR_OK
) {
712 DEBUG_IO("Ignoring command due to previous error");
716 if (buffer_write_space(ctx
) < 3)
717 ctx
->retval
= mpsse_flush(ctx
);
719 buffer_write_byte(ctx
, 0x86);
720 buffer_write_byte(ctx
, divisor
& 0xff);
721 buffer_write_byte(ctx
, divisor
>> 8);
724 int mpsse_divide_by_5_config(struct mpsse_ctx
*ctx
, bool enable
)
726 if (!mpsse_is_high_speed(ctx
))
729 LOG_DEBUG("%s", enable
? "on" : "off");
730 single_byte_boolean_helper(ctx
, enable
, 0x8b, 0x8a);
735 int mpsse_rtck_config(struct mpsse_ctx
*ctx
, bool enable
)
737 if (!mpsse_is_high_speed(ctx
))
740 LOG_DEBUG("%s", enable
? "on" : "off");
741 single_byte_boolean_helper(ctx
, enable
, 0x96, 0x97);
746 int mpsse_set_frequency(struct mpsse_ctx
*ctx
, int frequency
)
748 LOG_DEBUG("target %d Hz", frequency
);
749 assert(frequency
>= 0);
753 return mpsse_rtck_config(ctx
, true);
755 mpsse_rtck_config(ctx
, false); /* just try */
757 if (frequency
> 60000000 / 2 / 65536 && mpsse_divide_by_5_config(ctx
, false) == ERROR_OK
) {
758 base_clock
= 60000000;
760 mpsse_divide_by_5_config(ctx
, true); /* just try */
761 base_clock
= 12000000;
764 int divisor
= (base_clock
/ 2 + frequency
- 1) / frequency
- 1;
767 assert(divisor
>= 0);
769 mpsse_set_divisor(ctx
, divisor
);
771 frequency
= base_clock
/ 2 / (1 + divisor
);
772 LOG_DEBUG("actually %d Hz", frequency
);
777 /* Context needed by the callbacks */
778 struct transfer_result
{
779 struct mpsse_ctx
*ctx
;
781 unsigned transferred
;
784 static LIBUSB_CALL
void read_cb(struct libusb_transfer
*transfer
)
786 struct transfer_result
*res
= transfer
->user_data
;
787 struct mpsse_ctx
*ctx
= res
->ctx
;
789 unsigned packet_size
= ctx
->max_packet_size
;
791 DEBUG_PRINT_BUF(transfer
->buffer
, transfer
->actual_length
);
793 /* Strip the two status bytes sent at the beginning of each USB packet
794 * while copying the chunk buffer to the read buffer */
795 unsigned num_packets
= DIV_ROUND_UP(transfer
->actual_length
, packet_size
);
796 unsigned chunk_remains
= transfer
->actual_length
;
797 for (unsigned i
= 0; i
< num_packets
&& chunk_remains
> 2; i
++) {
798 unsigned this_size
= packet_size
- 2;
799 if (this_size
> chunk_remains
- 2)
800 this_size
= chunk_remains
- 2;
801 if (this_size
> ctx
->read_count
- res
->transferred
)
802 this_size
= ctx
->read_count
- res
->transferred
;
803 memcpy(ctx
->read_buffer
+ res
->transferred
,
804 ctx
->read_chunk
+ packet_size
* i
+ 2,
806 res
->transferred
+= this_size
;
807 chunk_remains
-= this_size
+ 2;
808 if (res
->transferred
== ctx
->read_count
) {
814 DEBUG_IO("raw chunk %d, transferred %d of %d", transfer
->actual_length
, res
->transferred
,
818 if (libusb_submit_transfer(transfer
) != LIBUSB_SUCCESS
)
822 static LIBUSB_CALL
void write_cb(struct libusb_transfer
*transfer
)
824 struct transfer_result
*res
= transfer
->user_data
;
825 struct mpsse_ctx
*ctx
= res
->ctx
;
827 res
->transferred
+= transfer
->actual_length
;
829 DEBUG_IO("transferred %d of %d", res
->transferred
, ctx
->write_count
);
831 DEBUG_PRINT_BUF(transfer
->buffer
, transfer
->actual_length
);
833 if (res
->transferred
== ctx
->write_count
)
836 transfer
->length
= ctx
->write_count
- res
->transferred
;
837 transfer
->buffer
= ctx
->write_buffer
+ res
->transferred
;
838 if (libusb_submit_transfer(transfer
) != LIBUSB_SUCCESS
)
843 int mpsse_flush(struct mpsse_ctx
*ctx
)
845 int retval
= ctx
->retval
;
847 if (retval
!= ERROR_OK
) {
848 DEBUG_IO("Ignoring flush due to previous error");
849 assert(ctx
->write_count
== 0 && ctx
->read_count
== 0);
850 ctx
->retval
= ERROR_OK
;
854 DEBUG_IO("write %d%s, read %d", ctx
->write_count
, ctx
->read_count
? "+1" : "",
856 assert(ctx
->write_count
> 0 || ctx
->read_count
== 0); /* No read data without write data */
858 if (ctx
->write_count
== 0)
861 struct libusb_transfer
*read_transfer
= 0;
862 struct transfer_result read_result
= { .ctx
= ctx
, .done
= true };
863 if (ctx
->read_count
) {
864 buffer_write_byte(ctx
, 0x87); /* SEND_IMMEDIATE */
865 read_result
.done
= false;
866 /* delay read transaction to ensure the FTDI chip can support us with data
867 immediately after processing the MPSSE commands in the write transaction */
870 struct transfer_result write_result
= { .ctx
= ctx
, .done
= false };
871 struct libusb_transfer
*write_transfer
= libusb_alloc_transfer(0);
872 libusb_fill_bulk_transfer(write_transfer
, ctx
->usb_dev
, ctx
->out_ep
, ctx
->write_buffer
,
873 ctx
->write_count
, write_cb
, &write_result
, ctx
->usb_write_timeout
);
874 retval
= libusb_submit_transfer(write_transfer
);
876 if (ctx
->read_count
) {
877 read_transfer
= libusb_alloc_transfer(0);
878 libusb_fill_bulk_transfer(read_transfer
, ctx
->usb_dev
, ctx
->in_ep
, ctx
->read_chunk
,
879 ctx
->read_chunk_size
, read_cb
, &read_result
,
880 ctx
->usb_read_timeout
);
881 retval
= libusb_submit_transfer(read_transfer
);
884 /* Polling loop, more or less taken from libftdi */
885 while (!write_result
.done
|| !read_result
.done
) {
886 retval
= libusb_handle_events(ctx
->usb_ctx
);
888 if (retval
!= LIBUSB_SUCCESS
&& retval
!= LIBUSB_ERROR_INTERRUPTED
) {
889 libusb_cancel_transfer(write_transfer
);
891 libusb_cancel_transfer(read_transfer
);
892 while (!write_result
.done
|| !read_result
.done
)
893 if (libusb_handle_events(ctx
->usb_ctx
) != LIBUSB_SUCCESS
)
898 if (retval
!= LIBUSB_SUCCESS
) {
899 LOG_ERROR("libusb_handle_events() failed with %s", libusb_error_name(retval
));
901 } else if (write_result
.transferred
< ctx
->write_count
) {
902 LOG_ERROR("ftdi device did not accept all data: %d, tried %d",
903 write_result
.transferred
,
906 } else if (read_result
.transferred
< ctx
->read_count
) {
907 LOG_ERROR("ftdi device did not return all data: %d, expected %d",
908 read_result
.transferred
,
911 } else if (ctx
->read_count
) {
912 ctx
->write_count
= 0;
914 bit_copy_execute(&ctx
->read_queue
);
917 ctx
->write_count
= 0;
918 bit_copy_discard(&ctx
->read_queue
);
922 libusb_free_transfer(write_transfer
);
924 libusb_free_transfer(read_transfer
);
926 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)