usb_blaster: fix warning: array subscript has type ‘char’
[openocd.git] / src / jtag / drivers / usb_blaster / usb_blaster.c
1 /*
2 * Driver for USB-JTAG, Altera USB-Blaster and compatibles
3 *
4 * Inspired from original code from Kolja Waschk's USB-JTAG project
5 * (http://www.ixo.de/info/usb_jtag/), and from openocd project.
6 *
7 * Copyright (C) 2013 Franck Jullien franck.jullien@gmail.com
8 * Copyright (C) 2012 Robert Jarzmik robert.jarzmik@free.fr
9 * Copyright (C) 2011 Ali Lown ali@lown.me.uk
10 * Copyright (C) 2009 Catalin Patulea cat@vv.carleton.ca
11 * Copyright (C) 2006 Kolja Waschk usbjtag@ixo.de
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 */
24
25 /*
26 * The following information is originally from Kolja Waschk's USB-JTAG,
27 * where it was obtained by reverse engineering an Altera USB-Blaster.
28 * See http://www.ixo.de/info/usb_jtag/ for USB-Blaster block diagram and
29 * usb_jtag-20080705-1200.zip#usb_jtag/host/openocd for protocol.
30 *
31 * The same information is also on the UrJTAG mediawiki, with some additional
32 * notes on bits marked as "unknown" by usb_jtag.
33 * (http://sourceforge.net/apps/mediawiki/urjtag/index.php?
34 * title=Cable_Altera_USB-Blaster)
35 *
36 * USB-JTAG, Altera USB-Blaster and compatibles are typically implemented as
37 * an FTDIChip FT245 followed by a CPLD which handles a two-mode protocol:
38 *
39 * _________
40 * | |
41 * | AT93C46 |
42 * |_________|
43 * __|__________ _________
44 * | | | |
45 * USB__| FTDI 245BM |__| EPM7064 |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
46 * |_____________| |_________|
47 * __|__________ _|___________
48 * | | | |
49 * | 6 MHz XTAL | | 24 MHz Osc. |
50 * |_____________| |_____________|
51 *
52 * USB-JTAG, Altera USB-Blaster II are typically implemented as a Cypress
53 * EZ-USB FX2LP followed by a CPLD.
54 * _____________ _________
55 * | | | |
56 * USB__| EZ-USB FX2 |__| EPM570 |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
57 * |_____________| |_________|
58 * __|__________
59 * | |
60 * | 24 MHz XTAL |
61 * |_____________|
62 */
63
64 #ifdef HAVE_CONFIG_H
65 #include "config.h"
66 #endif
67
68 #if IS_CYGWIN == 1
69 #include "windows.h"
70 #undef LOG_ERROR
71 #endif
72
73 /* project specific includes */
74 #include <jtag/interface.h>
75 #include <jtag/commands.h>
76 #include <helper/time_support.h>
77 #include "ublast_access.h"
78
79 /* system includes */
80 #include <string.h>
81 #include <stdlib.h>
82 #include <unistd.h>
83 #include <sys/time.h>
84 #include <time.h>
85
86 /* Size of USB endpoint max packet size, ie. 64 bytes */
87 #define MAX_PACKET_SIZE 64
88 /*
89 * Size of data buffer that holds bytes in byte-shift mode.
90 * This buffer can hold multiple USB packets aligned to
91 * MAX_PACKET_SIZE bytes boundaries.
92 * BUF_LEN must be grater than or equal MAX_PACKET_SIZE.
93 */
94 #define BUF_LEN 4096
95
96 /* USB-Blaster II specific command */
97 #define CMD_COPY_TDO_BUFFER 0x5F
98
99 enum gpio_steer {
100 FIXED_0 = 0,
101 FIXED_1,
102 SRST,
103 TRST,
104 };
105
106 struct ublast_info {
107 enum gpio_steer pin6;
108 enum gpio_steer pin8;
109 int tms;
110 int tdi;
111 bool trst_asserted;
112 bool srst_asserted;
113 uint8_t buf[BUF_LEN];
114 int bufidx;
115
116 char *lowlevel_name;
117 struct ublast_lowlevel *drv;
118 char *ublast_device_desc;
119 uint16_t ublast_vid, ublast_pid;
120 uint16_t ublast_vid_uninit, ublast_pid_uninit;
121 int flags;
122 char *firmware_path;
123 };
124
125 /*
126 * Global device control
127 */
128 static struct ublast_info info = {
129 .ublast_vid = 0x09fb, /* Altera */
130 .ublast_pid = 0x6001, /* USB-Blaster */
131 .lowlevel_name = NULL,
132 .srst_asserted = false,
133 .trst_asserted = false,
134 .pin6 = FIXED_1,
135 .pin8 = FIXED_1,
136 };
137
138 /*
139 * Available lowlevel drivers (FTDI, FTD2xx, ...)
140 */
141 struct drvs_map {
142 char *name;
143 struct ublast_lowlevel *(*drv_register)(void);
144 };
145
146 static struct drvs_map lowlevel_drivers_map[] = {
147 #if BUILD_USB_BLASTER_LIBFTDI
148 { .name = "ftdi", .drv_register = ublast_register_ftdi },
149 #endif
150 #if BUILD_USB_BLASTER_FTD2XX
151 { .name = "ftd2xx", .drv_register = ublast_register_ftd2xx },
152 #endif
153 #if BUILD_USB_BLASTER_2
154 { .name = "ublast2", .drv_register = ublast2_register_libusb },
155 #endif
156 { NULL, NULL },
157 };
158
159 /*
160 * Access functions to lowlevel driver, agnostic of libftdi/libftdxx
161 */
162 static char *hexdump(uint8_t *buf, unsigned int size)
163 {
164 unsigned int i;
165 char *str = calloc(size * 2 + 1, 1);
166
167 for (i = 0; i < size; i++)
168 sprintf(str + 2*i, "%02x", buf[i]);
169 return str;
170 }
171
172 static int ublast_buf_read(uint8_t *buf, unsigned size, uint32_t *bytes_read)
173 {
174 int ret = info.drv->read(info.drv, buf, size, bytes_read);
175 char *str = hexdump(buf, *bytes_read);
176
177 DEBUG_JTAG_IO("(size=%d, buf=[%s]) -> %u", size, str,
178 *bytes_read);
179 free(str);
180 return ret;
181 }
182
183 static int ublast_buf_write(uint8_t *buf, int size, uint32_t *bytes_written)
184 {
185 int ret = info.drv->write(info.drv, buf, size, bytes_written);
186 char *str = hexdump(buf, *bytes_written);
187
188 DEBUG_JTAG_IO("(size=%d, buf=[%s]) -> %u", size, str,
189 *bytes_written);
190 free(str);
191 return ret;
192 }
193
194 static int nb_buf_remaining(void)
195 {
196 return BUF_LEN - info.bufidx;
197 }
198
199 static void ublast_flush_buffer(void)
200 {
201 unsigned int retlen;
202 int nb = info.bufidx, ret = ERROR_OK;
203
204 while (ret == ERROR_OK && nb > 0) {
205 ret = ublast_buf_write(info.buf, nb, &retlen);
206 nb -= retlen;
207 }
208 info.bufidx = 0;
209 }
210
211 /*
212 * Actually, the USB-Blaster offers a byte-shift mode to transmit up to 504 data
213 * bits (bidirectional) in a single USB packet. A header byte has to be sent as
214 * the first byte in a packet with the following meaning:
215 *
216 * Bit 7 (0x80): Must be set to indicate byte-shift mode.
217 * Bit 6 (0x40): If set, the USB-Blaster will also read data, not just write.
218 * Bit 5..0: Define the number N of following bytes
219 *
220 * All N following bytes will then be clocked out serially on TDI. If Bit 6 was
221 * set, it will afterwards return N bytes with TDO data read while clocking out
222 * the TDI data. LSB of the first byte after the header byte will appear first
223 * on TDI.
224 */
225
226 /* Simple bit banging mode:
227 *
228 * Bit 7 (0x80): Must be zero (see byte-shift mode above)
229 * Bit 6 (0x40): If set, you will receive a byte indicating the state of TDO
230 * in return.
231 * Bit 5 (0x20): Output Enable/LED.
232 * Bit 4 (0x10): TDI Output.
233 * Bit 3 (0x08): nCS Output (not used in JTAG mode).
234 * Bit 2 (0x04): nCE Output (not used in JTAG mode).
235 * Bit 1 (0x02): TMS Output.
236 * Bit 0 (0x01): TCK Output.
237 *
238 * For transmitting a single data bit, you need to write two bytes (one for
239 * setting up TDI/TMS/TCK=0, and one to trigger TCK high with same TDI/TMS
240 * held). Up to 64 bytes can be combined in a single USB packet.
241 * It isn't possible to read a data without transmitting data.
242 */
243
244 #define TCK (1 << 0)
245 #define TMS (1 << 1)
246 #define NCE (1 << 2)
247 #define NCS (1 << 3)
248 #define TDI (1 << 4)
249 #define LED (1 << 5)
250 #define READ (1 << 6)
251 #define SHMODE (1 << 7)
252 #define READ_TDO (1 << 0)
253
254 /**
255 * ublast_queue_byte - queue one 'bitbang mode' byte for USB Blaster
256 * @abyte: the byte to queue
257 *
258 * Queues one byte in 'bitbang mode' to the USB Blaster. The byte is not
259 * actually sent, but stored in a buffer. The write is performed once
260 * the buffer is filled, or if an explicit ublast_flush_buffer() is called.
261 */
262 static void ublast_queue_byte(uint8_t abyte)
263 {
264 if (nb_buf_remaining() < 1)
265 ublast_flush_buffer();
266 info.buf[info.bufidx++] = abyte;
267 if (nb_buf_remaining() == 0)
268 ublast_flush_buffer();
269 DEBUG_JTAG_IO("(byte=0x%02x)", abyte);
270 }
271
272 /**
273 * ublast_compute_pin - compute if gpio should be asserted
274 * @steer: control (ie. TRST driven, SRST driven, of fixed)
275 *
276 * Returns pin value (1 means driven high, 0 mean driven low)
277 */
278 bool ublast_compute_pin(enum gpio_steer steer)
279 {
280 switch (steer) {
281 case FIXED_0:
282 return 0;
283 case FIXED_1:
284 return 1;
285 case SRST:
286 return !info.srst_asserted;
287 case TRST:
288 return !info.trst_asserted;
289 default:
290 return 1;
291 }
292 }
293
294 /**
295 * ublast_build_out - build bitbang mode output byte
296 * @type: says if reading back TDO is required
297 *
298 * Returns the compute bitbang mode byte
299 */
300 static uint8_t ublast_build_out(enum scan_type type)
301 {
302 uint8_t abyte = 0;
303
304 abyte |= info.tms ? TMS : 0;
305 abyte |= ublast_compute_pin(info.pin6) ? NCE : 0;
306 abyte |= ublast_compute_pin(info.pin8) ? NCS : 0;
307 abyte |= info.tdi ? TDI : 0;
308 abyte |= LED;
309 if (type == SCAN_IN || type == SCAN_IO)
310 abyte |= READ;
311 return abyte;
312 }
313
314 /**
315 * ublast_reset - reset the JTAG device is possible
316 * @trst: 1 if TRST is to be asserted
317 * @srst: 1 if SRST is to be asserted
318 */
319 static void ublast_reset(int trst, int srst)
320 {
321 uint8_t out_value;
322
323 info.trst_asserted = trst;
324 info.srst_asserted = srst;
325 out_value = ublast_build_out(SCAN_OUT);
326 ublast_queue_byte(out_value);
327 ublast_flush_buffer();
328 }
329
330 /**
331 * ublast_clock_tms - clock a TMS transition
332 * @tms: the TMS to be sent
333 *
334 * Triggers a TMS transition (ie. one JTAG TAP state move).
335 */
336 static void ublast_clock_tms(int tms)
337 {
338 uint8_t out;
339
340 DEBUG_JTAG_IO("(tms=%d)", !!tms);
341 info.tms = !!tms;
342 info.tdi = 0;
343 out = ublast_build_out(SCAN_OUT);
344 ublast_queue_byte(out);
345 ublast_queue_byte(out | TCK);
346 }
347
348 /**
349 * ublast_idle_clock - put back TCK to low level
350 *
351 * See ublast_queue_tdi() comment for the usage of this function.
352 */
353 static void ublast_idle_clock(void)
354 {
355 uint8_t out = ublast_build_out(SCAN_OUT);
356
357 DEBUG_JTAG_IO(".");
358 ublast_queue_byte(out);
359 }
360
361 /**
362 * ublast_clock_tdi - Output a TDI with bitbang mode
363 * @tdi: the TDI bit to be shifted out
364 * @type: scan type (ie. does a readback of TDO is required)
365 *
366 * Output a TDI bit and assert clock to push it into the JTAG device :
367 * - writing out TCK=0, TMS=<old_state>=0, TDI=<tdi>
368 * - writing out TCK=1, TMS=<new_state>, TDI=<tdi> which triggers the JTAG
369 * device aquiring the data.
370 *
371 * If a TDO is to be read back, the required read is requested (bitbang mode),
372 * and the USB Blaster will send back a byte with bit0 reprensenting the TDO.
373 */
374 static void ublast_clock_tdi(int tdi, enum scan_type type)
375 {
376 uint8_t out;
377
378 DEBUG_JTAG_IO("(tdi=%d)", !!tdi);
379 info.tdi = !!tdi;
380
381 out = ublast_build_out(SCAN_OUT);
382 ublast_queue_byte(out);
383
384 out = ublast_build_out(type);
385 ublast_queue_byte(out | TCK);
386 }
387
388 /**
389 * ublast_clock_tdi_flip_tms - Output a TDI with bitbang mode, change JTAG state
390 * @tdi: the TDI bit to be shifted out
391 * @type: scan type (ie. does a readback of TDO is required)
392 *
393 * This function is the same as ublast_clock_tdi(), but it changes also the TMS
394 * while outputing the TDI. This should be the last TDI output of a TDI
395 * sequence, which will change state from :
396 * - IRSHIFT -> IREXIT1
397 * - or DRSHIFT -> DREXIT1
398 */
399 static void ublast_clock_tdi_flip_tms(int tdi, enum scan_type type)
400 {
401 uint8_t out;
402
403 DEBUG_JTAG_IO("(tdi=%d)", !!tdi);
404 info.tdi = !!tdi;
405 info.tms = !info.tms;
406
407 out = ublast_build_out(SCAN_OUT);
408 ublast_queue_byte(out);
409
410 out = ublast_build_out(type);
411 ublast_queue_byte(out | TCK);
412
413 out = ublast_build_out(SCAN_OUT);
414 ublast_queue_byte(out);
415 }
416
417 /**
418 * ublast_queue_bytes - queue bytes for the USB Blaster
419 * @bytes: byte array
420 * @nb_bytes: number of bytes
421 *
422 * Queues bytes to be sent to the USB Blaster. The bytes are not
423 * actually sent, but stored in a buffer. The write is performed once
424 * the buffer is filled, or if an explicit ublast_flush_buffer() is called.
425 */
426 static void ublast_queue_bytes(uint8_t *bytes, int nb_bytes)
427 {
428 if (info.bufidx + nb_bytes > BUF_LEN) {
429 LOG_ERROR("buggy code, should never queue more that %d bytes",
430 info.bufidx + nb_bytes);
431 exit(-1);
432 }
433 DEBUG_JTAG_IO("(nb_bytes=%d, bytes=[0x%02x, ...])", nb_bytes,
434 bytes ? bytes[0] : 0);
435 if (bytes)
436 memcpy(&info.buf[info.bufidx], bytes, nb_bytes);
437 else
438 memset(&info.buf[info.bufidx], 0, nb_bytes);
439 info.bufidx += nb_bytes;
440 if (nb_buf_remaining() == 0)
441 ublast_flush_buffer();
442 }
443
444 /**
445 * ublast_tms_seq - write a TMS sequence transition to JTAG
446 * @bits: TMS bits to be written (bit0, bit1 .. bitN)
447 * @nb_bits: number of TMS bits (between 1 and 8)
448 *
449 * Write a serie of TMS transitions, where each transition consists in :
450 * - writing out TCK=0, TMS=<new_state>, TDI=<???>
451 * - writing out TCK=1, TMS=<new_state>, TDI=<???> which triggers the transition
452 * The function ensures that at the end of the sequence, the clock (TCK) is put
453 * low.
454 */
455 static void ublast_tms_seq(const uint8_t *bits, int nb_bits)
456 {
457 int i;
458
459 DEBUG_JTAG_IO("(bits=%02x..., nb_bits=%d)", bits[0], nb_bits);
460 for (i = 0; i < nb_bits; i++)
461 ublast_clock_tms((bits[i / 8] >> (i % 8)) & 0x01);
462 ublast_idle_clock();
463 }
464
465 /**
466 * ublast_tms - write a tms command
467 * @cmd: tms command
468 */
469 static void ublast_tms(struct tms_command *cmd)
470 {
471 DEBUG_JTAG_IO("(num_bits=%d)", cmd->num_bits);
472 ublast_tms_seq(cmd->bits, cmd->num_bits);
473 }
474
475 /**
476 * ublast_path_move - write a TMS sequence transition to JTAG
477 * @cmd: path transition
478 *
479 * Write a serie of TMS transitions, where each transition consists in :
480 * - writing out TCK=0, TMS=<new_state>, TDI=<???>
481 * - writing out TCK=1, TMS=<new_state>, TDI=<???> which triggers the transition
482 * The function ensures that at the end of the sequence, the clock (TCK) is put
483 * low.
484 */
485 static void ublast_path_move(struct pathmove_command *cmd)
486 {
487 int i;
488
489 DEBUG_JTAG_IO("(num_states=%d, last_state=%d)",
490 cmd->num_states, cmd->path[cmd->num_states - 1]);
491 for (i = 0; i < cmd->num_states; i++) {
492 if (tap_state_transition(tap_get_state(), false) == cmd->path[i])
493 ublast_clock_tms(0);
494 if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
495 ublast_clock_tms(1);
496 tap_set_state(cmd->path[i]);
497 }
498 ublast_idle_clock();
499 }
500
501 /**
502 * ublast_state_move - move JTAG state to the target state
503 * @state: the target state
504 *
505 * Input the correct TMS sequence to the JTAG TAP so that we end up in the
506 * target state. This assumes the current state (tap_get_state()) is correct.
507 */
508 static void ublast_state_move(tap_state_t state)
509 {
510 uint8_t tms_scan;
511 int tms_len;
512
513 DEBUG_JTAG_IO("(from %s to %s)", tap_state_name(tap_get_state()),
514 tap_state_name(state));
515 if (tap_get_state() == state)
516 return;
517 tms_scan = tap_get_tms_path(tap_get_state(), state);
518 tms_len = tap_get_tms_path_len(tap_get_state(), state);
519 ublast_tms_seq(&tms_scan, tms_len);
520 tap_set_state(state);
521 }
522
523 /**
524 * ublast_read_byteshifted_tdos - read TDO of byteshift writes
525 * @buf: the buffer to store the bits
526 * @nb_bits: the number of bits
527 *
528 * Reads back from USB Blaster TDO bits, triggered by a 'byteshift write', ie. eight
529 * bits per received byte from USB interface, and store them in buffer.
530 *
531 * As the USB blaster stores the TDO bits in LSB (ie. first bit in (byte0,
532 * bit0), second bit in (byte0, bit1), ...), which is what we want to return,
533 * simply read bytes from USB interface and store them.
534 *
535 * Returns ERROR_OK if OK, ERROR_xxx if a read error occured
536 */
537 static int ublast_read_byteshifted_tdos(uint8_t *buf, int nb_bytes)
538 {
539 unsigned int retlen;
540 int ret = ERROR_OK;
541
542 DEBUG_JTAG_IO("%s(buf=%p, num_bits=%d)", __func__, buf, nb_bytes * 8);
543 ublast_flush_buffer();
544 while (ret == ERROR_OK && nb_bytes > 0) {
545 ret = ublast_buf_read(buf, nb_bytes, &retlen);
546 nb_bytes -= retlen;
547 }
548 return ret;
549 }
550
551 /**
552 * ublast_read_bitbang_tdos - read TDO of bitbang writes
553 * @buf: the buffer to store the bits
554 * @nb_bits: the number of bits
555 *
556 * Reads back from USB Blaster TDO bits, triggered by a 'bitbang write', ie. one
557 * bit per received byte from USB interface, and store them in buffer, where :
558 * - first bit is stored in byte0, bit0 (LSB)
559 * - second bit is stored in byte0, bit 1
560 * ...
561 * - eight bit is sotred in byte0, bit 7
562 * - ninth bit is sotred in byte1, bit 0
563 * - etc ...
564 *
565 * Returns ERROR_OK if OK, ERROR_xxx if a read error occured
566 */
567 static int ublast_read_bitbang_tdos(uint8_t *buf, int nb_bits)
568 {
569 int nb1 = nb_bits;
570 int i, ret = ERROR_OK;
571 unsigned int retlen;
572 uint8_t tmp[8];
573
574 DEBUG_JTAG_IO("%s(buf=%p, num_bits=%d)", __func__, buf, nb_bits);
575
576 /*
577 * Ensure all previous bitbang writes were issued to the dongle, so that
578 * it returns back the read values.
579 */
580 ublast_flush_buffer();
581
582 ret = ublast_buf_read(tmp, nb1, &retlen);
583 for (i = 0; ret == ERROR_OK && i < nb1; i++)
584 if (tmp[i] & READ_TDO)
585 *buf |= (1 << i);
586 else
587 *buf &= ~(1 << i);
588 return ret;
589 }
590
591 /**
592 * ublast_queue_tdi - short description
593 * @bits: bits to be queued on TDI (or NULL if 0 are to be queued)
594 * @nb_bits: number of bits
595 * @scan: scan type (ie. if TDO read back is required or not)
596 *
597 * Outputs a serie of TDI bits on TDI.
598 * As a side effect, the last TDI bit is sent along a TMS=1, and triggers a JTAG
599 * TAP state shift if input bits were non NULL.
600 *
601 * In order to not saturate the USB Blaster queues, this method reads back TDO
602 * if the scan type requests it, and stores them back in bits.
603 *
604 * As a side note, the state of TCK when entering this function *must* be
605 * low. This is because byteshift mode outputs TDI on rising TCK and reads TDO
606 * on falling TCK if and only if TCK is low before queuing byteshift mode bytes.
607 * If TCK was high, the USB blaster will queue TDI on falling edge, and read TDO
608 * on rising edge !!!
609 */
610 static void ublast_queue_tdi(uint8_t *bits, int nb_bits, enum scan_type scan)
611 {
612 int nb8 = nb_bits / 8;
613 int nb1 = nb_bits % 8;
614 int nbfree_in_packet, i, trans = 0, read_tdos;
615 uint8_t *tdos = calloc(1, nb_bits / 8 + 1);
616 static uint8_t byte0[BUF_LEN];
617
618 /*
619 * As the last TDI bit should always be output in bitbang mode in order
620 * to activate the TMS=1 transition to EXIT_?R state. Therefore a
621 * situation where nb_bits is a multiple of 8 is handled as follows:
622 * - the number of TDI shifted out in "byteshift mode" is 8 less than
623 * nb_bits
624 * - nb1 = 8
625 * This ensures that nb1 is never 0, and allows the TMS transition.
626 */
627 if (nb8 > 0 && nb1 == 0) {
628 nb8--;
629 nb1 = 8;
630 }
631
632 read_tdos = (scan == SCAN_IN || scan == SCAN_IO);
633 for (i = 0; i < nb8; i += trans) {
634 /*
635 * Calculate number of bytes to fill USB packet of size MAX_PACKET_SIZE
636 */
637 nbfree_in_packet = (MAX_PACKET_SIZE - (info.bufidx%MAX_PACKET_SIZE));
638 trans = MIN(nbfree_in_packet - 1, nb8 - i);
639
640 /*
641 * Queue a byte-shift mode transmission, with as many bytes as
642 * is possible with regard to :
643 * - current filling level of write buffer
644 * - remaining bytes to write in byte-shift mode
645 */
646 if (read_tdos)
647 ublast_queue_byte(SHMODE | READ | trans);
648 else
649 ublast_queue_byte(SHMODE | trans);
650 if (bits)
651 ublast_queue_bytes(&bits[i], trans);
652 else
653 ublast_queue_bytes(byte0, trans);
654 if (read_tdos) {
655 if (info.flags & COPY_TDO_BUFFER)
656 ublast_queue_byte(CMD_COPY_TDO_BUFFER);
657 ublast_read_byteshifted_tdos(&tdos[i], trans);
658 }
659 }
660
661 /*
662 * Queue the remaining TDI bits in bitbang mode.
663 */
664 for (i = 0; i < nb1; i++) {
665 int tdi = bits ? bits[nb8 + i / 8] & (1 << i) : 0;
666 if (bits && i == nb1 - 1)
667 ublast_clock_tdi_flip_tms(tdi, scan);
668 else
669 ublast_clock_tdi(tdi, scan);
670 }
671 if (nb1 && read_tdos) {
672 if (info.flags & COPY_TDO_BUFFER)
673 ublast_queue_byte(CMD_COPY_TDO_BUFFER);
674 ublast_read_bitbang_tdos(&tdos[nb8], nb1);
675 }
676
677 if (bits)
678 memcpy(bits, tdos, DIV_ROUND_UP(nb_bits, 8));
679 free(tdos);
680
681 /*
682 * Ensure clock is in lower state
683 */
684 ublast_idle_clock();
685 }
686
687 static void ublast_runtest(int cycles, tap_state_t state)
688 {
689 DEBUG_JTAG_IO("%s(cycles=%i, end_state=%d)", __func__, cycles, state);
690
691 ublast_state_move(TAP_IDLE);
692 ublast_queue_tdi(NULL, cycles, SCAN_OUT);
693 ublast_state_move(state);
694 }
695
696 static void ublast_stableclocks(int cycles)
697 {
698 DEBUG_JTAG_IO("%s(cycles=%i)", __func__, cycles);
699 ublast_queue_tdi(NULL, cycles, SCAN_OUT);
700 }
701
702 /**
703 * ublast_scan - launches a DR-scan or IR-scan
704 * @cmd: the command to launch
705 *
706 * Launch a JTAG IR-scan or DR-scan
707 *
708 * Returns ERROR_OK if OK, ERROR_xxx if a read/write error occured.
709 */
710 static int ublast_scan(struct scan_command *cmd)
711 {
712 int scan_bits;
713 uint8_t *buf = NULL;
714 enum scan_type type;
715 int ret = ERROR_OK;
716 static const char * const type2str[] = { "", "SCAN_IN", "SCAN_OUT", "SCAN_IO" };
717 char *log_buf = NULL;
718
719 type = jtag_scan_type(cmd);
720 scan_bits = jtag_build_buffer(cmd, &buf);
721
722 if (cmd->ir_scan)
723 ublast_state_move(TAP_IRSHIFT);
724 else
725 ublast_state_move(TAP_DRSHIFT);
726
727 log_buf = hexdump(buf, DIV_ROUND_UP(scan_bits, 8));
728 DEBUG_JTAG_IO("%s(scan=%s, type=%s, bits=%d, buf=[%s], end_state=%d)", __func__,
729 cmd->ir_scan ? "IRSCAN" : "DRSCAN",
730 type2str[type],
731 scan_bits, log_buf, cmd->end_state);
732 free(log_buf);
733
734 ublast_queue_tdi(buf, scan_bits, type);
735
736 /*
737 * As our JTAG is in an unstable state (IREXIT1 or DREXIT1), move it
738 * forward to a stable IRPAUSE or DRPAUSE.
739 */
740 ublast_clock_tms(0);
741 if (cmd->ir_scan)
742 tap_set_state(TAP_IRPAUSE);
743 else
744 tap_set_state(TAP_DRPAUSE);
745
746 ret = jtag_read_buffer(buf, cmd);
747 if (buf)
748 free(buf);
749 ublast_state_move(cmd->end_state);
750 return ret;
751 }
752
753 static void ublast_usleep(int us)
754 {
755 DEBUG_JTAG_IO("%s(us=%d)", __func__, us);
756 jtag_sleep(us);
757 }
758
759 static int ublast_execute_queue(void)
760 {
761 struct jtag_command *cmd;
762 int ret = ERROR_OK;
763
764 for (cmd = jtag_command_queue; ret == ERROR_OK && cmd != NULL;
765 cmd = cmd->next) {
766 switch (cmd->type) {
767 case JTAG_RESET:
768 ublast_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
769 break;
770 case JTAG_RUNTEST:
771 ublast_runtest(cmd->cmd.runtest->num_cycles,
772 cmd->cmd.runtest->end_state);
773 break;
774 case JTAG_STABLECLOCKS:
775 ublast_stableclocks(cmd->cmd.stableclocks->num_cycles);
776 break;
777 case JTAG_TLR_RESET:
778 ublast_state_move(cmd->cmd.statemove->end_state);
779 break;
780 case JTAG_PATHMOVE:
781 ublast_path_move(cmd->cmd.pathmove);
782 break;
783 case JTAG_TMS:
784 ublast_tms(cmd->cmd.tms);
785 break;
786 case JTAG_SLEEP:
787 ublast_usleep(cmd->cmd.sleep->us);
788 break;
789 case JTAG_SCAN:
790 ret = ublast_scan(cmd->cmd.scan);
791 break;
792 }
793 }
794
795 ublast_flush_buffer();
796 return ret;
797 }
798
799 /**
800 * ublast_init - Initialize the Altera device
801 *
802 * Initialize the device :
803 * - open the USB device
804 * - empty the write FIFO (128 bytes)
805 * - empty the read FIFO (384 bytes)
806 *
807 * Returns ERROR_OK if USB device found, error if not.
808 */
809 static int ublast_init(void)
810 {
811 static uint8_t tms_reset = 0xff;
812 int ret, i;
813
814 if (info.lowlevel_name) {
815 for (i = 0; lowlevel_drivers_map[i].name; i++)
816 if (!strcmp(lowlevel_drivers_map[i].name, info.lowlevel_name))
817 break;
818 if (lowlevel_drivers_map[i].name)
819 info.drv = lowlevel_drivers_map[i].drv_register();
820 if (!info.drv) {
821 LOG_ERROR("no lowlevel driver found for %s or lowlevel driver opening error",
822 info.lowlevel_name);
823 return ERROR_JTAG_DEVICE_ERROR;
824 }
825 } else {
826 LOG_INFO("No lowlevel driver configured, will try them all");
827 for (i = 0; !info.drv && lowlevel_drivers_map[i].name; i++)
828 info.drv = lowlevel_drivers_map[i].drv_register();
829 if (!info.drv) {
830 LOG_ERROR("no lowlevel driver found");
831 return ERROR_JTAG_DEVICE_ERROR;
832 }
833 info.lowlevel_name = strdup(lowlevel_drivers_map[i-1].name);
834 }
835
836 /*
837 * Register the lowlevel driver
838 */
839 info.drv->ublast_vid = info.ublast_vid;
840 info.drv->ublast_pid = info.ublast_pid;
841 info.drv->ublast_vid_uninit = info.ublast_vid_uninit;
842 info.drv->ublast_pid_uninit = info.ublast_pid_uninit;
843 info.drv->ublast_device_desc = info.ublast_device_desc;
844 info.drv->firmware_path = info.firmware_path;
845
846 info.flags |= info.drv->flags;
847
848 ret = info.drv->open(info.drv);
849 if (ret == ERROR_OK) {
850 /*
851 * Flush USB-Blaster queue fifos
852 */
853 uint32_t retlen;
854 ublast_buf_write(info.buf, BUF_LEN, &retlen);
855 /*
856 * Put JTAG in RESET state (five 1 on TMS)
857 */
858 ublast_tms_seq(&tms_reset, 5);
859 tap_set_state(TAP_RESET);
860 }
861 return ret;
862 }
863
864 /**
865 * ublast_quit - Release the Altera device
866 *
867 * Releases the device :
868 * - put the device pins in 'high impedance' mode
869 * - close the USB device
870 *
871 * Returns always ERROR_OK
872 */
873 static int ublast_quit(void)
874 {
875 uint8_t byte0 = 0;
876 unsigned int retlen;
877
878 ublast_buf_write(&byte0, 1, &retlen);
879 return info.drv->close(info.drv);
880 }
881
882 COMMAND_HANDLER(ublast_handle_device_desc_command)
883 {
884 if (CMD_ARGC != 1)
885 return ERROR_COMMAND_SYNTAX_ERROR;
886
887 info.ublast_device_desc = strdup(CMD_ARGV[0]);
888
889 return ERROR_OK;
890 }
891
892 COMMAND_HANDLER(ublast_handle_vid_pid_command)
893 {
894 if (CMD_ARGC > 4) {
895 LOG_WARNING("ignoring extra IDs in ublast_vid_pid "
896 "(maximum is 2 pairs)");
897 CMD_ARGC = 4;
898 }
899
900 if (CMD_ARGC >= 2) {
901 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], info.ublast_vid);
902 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], info.ublast_pid);
903 } else {
904 LOG_WARNING("incomplete ublast_vid_pid configuration");
905 }
906
907 if (CMD_ARGC == 4) {
908 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[2], info.ublast_vid_uninit);
909 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[3], info.ublast_pid_uninit);
910 } else {
911 LOG_WARNING("incomplete ublast_vid_pid configuration");
912 }
913
914 return ERROR_OK;
915 }
916
917 COMMAND_HANDLER(ublast_handle_pin_command)
918 {
919 uint8_t out_value;
920 const char * const pin_name = CMD_ARGV[0];
921 enum gpio_steer *steer = NULL;
922 static const char * const pin_val_str[] = {
923 [FIXED_0] = "0",
924 [FIXED_1] = "1",
925 [SRST] = "SRST driven",
926 [TRST] = "TRST driven",
927 };
928
929 if (CMD_ARGC > 2) {
930 LOG_ERROR("%s takes exactly one or two arguments", CMD_NAME);
931 return ERROR_COMMAND_SYNTAX_ERROR;
932 }
933
934 if (!strcmp(pin_name, "pin6"))
935 steer = &info.pin6;
936 if (!strcmp(pin_name, "pin8"))
937 steer = &info.pin8;
938 if (!steer) {
939 LOG_ERROR("%s: pin name must be \"pin6\" or \"pin8\"",
940 CMD_NAME);
941 return ERROR_COMMAND_SYNTAX_ERROR;
942 }
943
944 if (CMD_ARGC == 1) {
945 LOG_INFO("%s: %s is set as %s\n", CMD_NAME, pin_name,
946 pin_val_str[*steer]);
947 }
948
949 if (CMD_ARGC == 2) {
950 const char * const pin_value = CMD_ARGV[1];
951 char val = pin_value[0];
952
953 if (strlen(pin_value) > 1)
954 val = '?';
955 switch (tolower((unsigned char)val)) {
956 case '0':
957 *steer = FIXED_0;
958 break;
959 case '1':
960 *steer = FIXED_1;
961 break;
962 case 't':
963 *steer = TRST;
964 break;
965 case 's':
966 *steer = SRST;
967 break;
968 default:
969 LOG_ERROR("%s: pin value must be 0, 1, s (SRST) or t (TRST)",
970 pin_value);
971 return ERROR_COMMAND_SYNTAX_ERROR;
972 }
973
974 if (info.drv) {
975 out_value = ublast_build_out(SCAN_OUT);
976 ublast_queue_byte(out_value);
977 ublast_flush_buffer();
978 }
979 }
980 return ERROR_OK;
981 }
982
983 COMMAND_HANDLER(ublast_handle_lowlevel_drv_command)
984 {
985 if (CMD_ARGC != 1)
986 return ERROR_COMMAND_SYNTAX_ERROR;
987
988 info.lowlevel_name = strdup(CMD_ARGV[0]);
989
990 return ERROR_OK;
991 }
992
993 COMMAND_HANDLER(ublast_firmware_command)
994 {
995 if (CMD_ARGC != 1)
996 return ERROR_COMMAND_SYNTAX_ERROR;
997
998 info.firmware_path = strdup(CMD_ARGV[0]);
999
1000 return ERROR_OK;
1001 }
1002
1003
1004 static const struct command_registration ublast_command_handlers[] = {
1005 {
1006 .name = "usb_blaster_device_desc",
1007 .handler = ublast_handle_device_desc_command,
1008 .mode = COMMAND_CONFIG,
1009 .help = "set the USB device description of the USB-Blaster",
1010 .usage = "description-string",
1011 },
1012 {
1013 .name = "usb_blaster_vid_pid",
1014 .handler = ublast_handle_vid_pid_command,
1015 .mode = COMMAND_CONFIG,
1016 .help = "the vendor ID and product ID of the USB-Blaster and " \
1017 "vendor ID and product ID of the uninitialized device " \
1018 "for USB-Blaster II",
1019 .usage = "vid pid vid_uninit pid_uninit",
1020 },
1021 {
1022 .name = "usb_blaster_lowlevel_driver",
1023 .handler = ublast_handle_lowlevel_drv_command,
1024 .mode = COMMAND_CONFIG,
1025 .help = "set the lowlevel access for the USB Blaster (ftdi, ftd2xx, ublast2)",
1026 .usage = "(ftdi|ftd2xx|ublast2)",
1027 },
1028 {
1029 .name = "usb_blaster_pin",
1030 .handler = ublast_handle_pin_command,
1031 .mode = COMMAND_ANY,
1032 .help = "show or set pin state for the unused GPIO pins",
1033 .usage = "(pin6|pin8) (0|1|s|t)",
1034 },
1035 {
1036 .name = "usb_blaster_firmware",
1037 .handler = &ublast_firmware_command,
1038 .mode = COMMAND_CONFIG,
1039 .help = "configure the USB-Blaster II firmware location",
1040 .usage = "path/to/blaster_xxxx.hex",
1041 },
1042 COMMAND_REGISTRATION_DONE
1043 };
1044
1045 struct jtag_interface usb_blaster_interface = {
1046 .name = "usb_blaster",
1047 .commands = ublast_command_handlers,
1048 .supported = DEBUG_CAP_TMS_SEQ,
1049
1050 .execute_queue = ublast_execute_queue,
1051 .init = ublast_init,
1052 .quit = ublast_quit,
1053 };

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)