- vsllink update from SimonQian [simonqian@SimonQian.com]
[openocd.git] / src / jtag / vsllink.c
1 /***************************************************************************
2 * Copyright (C) 2009 by Simon Qian <SimonQian@SimonQian.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20 /* Versaloon is a programming tool for multiple MCUs.
21 * OpenOCD and MSP430 supports are distributed under GPLv2.
22 * You can find it at http://www.SimonQian.com/en/Versaloon.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "replacements.h"
30
31 #include "jtag.h"
32
33 #include <usb.h>
34 #include <string.h>
35
36 #include "log.h"
37
38 //#define _VSLLINK_IN_DEBUG_MODE_
39
40 /* enable this to view USB communication
41 */
42 #if 0
43 #define _DEBUG_USB_COMMS_
44 #endif
45
46 #ifdef _DEBUG_JTAG_IO_
47 #define DEBUG_JTAG_IO(expr ...) LOG_DEBUG(expr)
48 #else
49 #define DEBUG_JTAG_IO(expr ...)
50 #endif
51
52 #define VSLLINK_MODE_NORMAL 0
53 #define VSLLINK_MODE_DMA 1
54
55 static u16 vsllink_usb_vid;
56 static u16 vsllink_usb_pid;
57 static u8 vsllink_usb_bulkout;
58 static u8 vsllink_usb_bulkin;
59 static u8 vsllink_usb_interface;
60 static u8 vsllink_mode = VSLLINK_MODE_NORMAL;
61 static int VSLLINK_USB_TIMEOUT = 10000;
62
63 static int VSLLINK_BufferSize = 1024;
64
65 /* Global USB buffers */
66 static int vsllink_usb_out_buffer_idx;
67 static int vsllink_usb_in_want_length;
68 static u8* vsllink_usb_in_buffer = NULL;
69 static u8* vsllink_usb_out_buffer = NULL;
70
71 /* Constants for VSLLink command */
72 #define VSLLINK_CMD_CONN 0x80
73 #define VSLLINK_CMD_DISCONN 0x81
74 #define VSLLINK_CMD_SET_SPEED 0x82
75 #define VSLLINK_CMD_SET_PORT 0x90
76 #define VSLLINK_CMD_GET_PORT 0x91
77 #define VSLLINK_CMD_SET_PORTDIR 0x92
78 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
79 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
80 #define VSLLINK_CMD_HW_SWDCMD 0xA2
81 #define VSLLINK_CMD_HW_JTAGRAWCMD 0xA3
82
83 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
84 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
85 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
86
87 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
88 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
89
90 #define JTAG_PINMSK_SRST (1 << 0)
91 #define JTAG_PINMSK_TRST (1 << 1)
92 #define JTAG_PINMSK_USR1 (1 << 2)
93 #define JTAG_PINMSK_USR2 (1 << 3)
94 #define JTAG_PINMSK_TCK (1 << 4)
95 #define JTAG_PINMSK_TMS (1 << 5)
96 #define JTAG_PINMSK_TDI (1 << 6)
97 #define JTAG_PINMSK_TDO (1 << 7)
98
99
100 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_ndx(from)][tap_move_ndx(to)]
101
102 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
103 * 0: Test-Logic-Reset
104 * 1: Run-Test/Idle
105 * 2: Shift-DR
106 * 3: Pause-DR
107 * 4: Shift-IR
108 * 5: Pause-IR
109 *
110 * SD->SD and SI->SI have to be caught in interface specific code
111 */
112 static u8 VSLLINK_tap_move[6][6] =
113 {
114 /* TLR RTI SD PD SI PI */
115 {0xff, 0x7f, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
116 {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
117 {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
118 {0xfe, 0x60, 0x40, 0x5c, 0x3c, 0x5e}, /* PD */
119 {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
120 {0xfe, 0x60, 0x38, 0x5c, 0x40, 0x5e} /* PI */
121 };
122
123 typedef struct insert_insignificant_operation
124 {
125 unsigned char insert_value;
126 unsigned char insert_position;
127 }insert_insignificant_operation_t;
128
129 static insert_insignificant_operation_t VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[6][6] =
130 {
131 /* stuff offset */
132 {/* TLR */
133 {1, 0,}, /* TLR */
134 {1, 0,}, /* RTI */
135 {1, 0,}, /* SD */
136 {1, 0,}, /* PD */
137 {1, 0,}, /* SI */
138 {1, 0,}}, /* PI */
139 {/* RTI */
140 {1, 0,}, /* TLR */
141 {0, 0,}, /* RTI */
142 {0, 4,}, /* SD */
143 {0, 7,}, /* PD */
144 {0, 5,}, /* SI */
145 {0, 7,}}, /* PI */
146 {/* SD */
147 {0, 0,}, /* TLR */
148 {0, 0,}, /* RTI */
149 {0, 0,}, /* SD */
150 {0, 0,}, /* PD */
151 {0, 0,}, /* SI */
152 {0, 0,}}, /* PI */
153 {/* PD */
154 {0, 0,}, /* TLR */
155 {0, 0,}, /* RTI */
156 {0, 0,}, /* SD */
157 {0, 0,}, /* PD */
158 {0, 0,}, /* SI */
159 {0, 0,}}, /* PI */
160 {/* SI */
161 {0, 0,}, /* TLR */
162 {0, 0,}, /* RTI */
163 {0, 0,}, /* SD */
164 {0, 0,}, /* PD */
165 {0, 0,}, /* SI */
166 {0, 0,}}, /* PI */
167 {/* PI */
168 {0, 0,}, /* TLR */
169 {0, 0,}, /* RTI */
170 {0, 0,}, /* SD */
171 {0, 0,}, /* PD */
172 {0, 0,}, /* SI */
173 {0, 0,}}, /* PI */
174 };
175
176 static u8 VSLLINK_BIT_MSK[8] =
177 {
178 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
179 };
180
181 typedef struct
182 {
183 int offset;
184 int length; /* Number of bits to read */
185 scan_command_t *command; /* Corresponding scan command */
186 u8 *buffer;
187 } pending_scan_result_t;
188
189 #define MAX_PENDING_SCAN_RESULTS 256
190
191 static int pending_scan_results_length;
192 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
193
194 /* External interface functions */
195 static int vsllink_execute_queue(void);
196 static int vsllink_speed(int speed);
197 static int vsllink_khz(int khz, int *jtag_speed);
198 static int vsllink_speed_div(int jtag_speed, int *khz);
199 static int vsllink_register_commands(struct command_context_s *cmd_ctx);
200 static int vsllink_init(void);
201 static int vsllink_quit(void);
202
203 /* CLI command handler functions */
204 static int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
205 static int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
206 static int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
207 static int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
208 static int vsllink_handle_usb_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
209 static int vsllink_handle_mode_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
210
211 /* Queue command functions */
212 static void vsllink_end_state(tap_state_t state);
213 static void vsllink_state_move_dma(void);
214 static void vsllink_state_move_normal(void);
215 static void (*vsllink_state_move)(void);
216 static void vsllink_path_move_dma(int num_states, tap_state_t *path);
217 static void vsllink_path_move_normal(int num_states, tap_state_t *path);
218 static void (*vsllink_path_move)(int num_states, tap_state_t *path);
219 static void vsllink_runtest(int num_cycles);
220 static void vsllink_stableclocks_dma(int num_cycles, int tms);
221 static void vsllink_stableclocks_normal(int num_cycles, int tms);
222 static void (*vsllink_stableclocks)(int num_cycles, int tms);
223 static void vsllink_scan_dma(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
224 static void vsllink_scan_normal(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
225 static void (*vsllink_scan)(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
226 static void vsllink_reset(int trst, int srst);
227 static void vsllink_simple_command(u8 command);
228 static int vsllink_connect(void);
229 static int vsllink_disconnect(void);
230
231 /* VSLLink tap buffer functions */
232 static void vsllink_tap_append_step(int tms, int tdi);
233 static void vsllink_tap_init_dma(void);
234 static void vsllink_tap_init_normal(void);
235 static void (*vsllink_tap_init)(void);
236 static int vsllink_tap_execute_dma(void);
237 static int vsllink_tap_execute_normal(void);
238 static int (*vsllink_tap_execute)(void);
239 static void vsllink_tap_ensure_space_dma(int scans, int length);
240 static void vsllink_tap_ensure_space_normal(int scans, int length);
241 static void (*vsllink_tap_ensure_space)(int scans, int length);
242 static void vsllink_tap_append_scan_dma(int length, u8 *buffer, scan_command_t *command);
243 static void vsllink_tap_append_scan_normal(int length, u8 *buffer, scan_command_t *command, int offset);
244
245 /* VSLLink lowlevel functions */
246 typedef struct vsllink_jtag
247 {
248 struct usb_dev_handle* usb_handle;
249 } vsllink_jtag_t;
250
251 static vsllink_jtag_t *vsllink_usb_open(void);
252 static void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag);
253 static int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length);
254 static int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length);
255 static int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag);
256
257 #ifdef _DEBUG_USB_COMMS_
258 static void vsllink_debug_buffer(u8 *buffer, int length);
259 #endif
260
261 static int vsllink_tms_data_len = 0;
262 static u8* vsllink_tms_cmd_pos;
263
264 static int tap_length = 0;
265 static int tap_buffer_size = 0;
266 static u8 *tms_buffer = NULL;
267 static u8 *tdi_buffer = NULL;
268 static u8 *tdo_buffer = NULL;
269 static int last_tms;
270
271 static vsllink_jtag_t* vsllink_jtag_handle = NULL;
272
273 /***************************************************************************/
274 /* External interface implementation */
275
276 jtag_interface_t vsllink_interface =
277 {
278 .name = "vsllink",
279 .execute_queue = vsllink_execute_queue,
280 .speed = vsllink_speed,
281 .khz = vsllink_khz,
282 .speed_div = vsllink_speed_div,
283 .register_commands = vsllink_register_commands,
284 .init = vsllink_init,
285 .quit = vsllink_quit
286 };
287
288 static void reset_command_pointer(void)
289 {
290 if (vsllink_mode == VSLLINK_MODE_NORMAL)
291 {
292 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
293 vsllink_usb_out_buffer_idx = 3;
294 }
295 else
296 {
297 tap_length = 0;
298 }
299 }
300
301 static int vsllink_execute_queue(void)
302 {
303 jtag_command_t *cmd = jtag_command_queue;
304 int scan_size;
305 enum scan_type type;
306 u8 *buffer;
307
308 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
309
310 reset_command_pointer();
311 while (cmd != NULL)
312 {
313 switch (cmd->type)
314 {
315 case JTAG_END_STATE:
316 DEBUG_JTAG_IO("end_state: %s", tap_state_name(cmd->cmd.end_state->end_state));
317
318 if (cmd->cmd.end_state->end_state != TAP_INVALID)
319 {
320 vsllink_end_state(cmd->cmd.end_state->end_state);
321 }
322 break;
323
324 case JTAG_RUNTEST:
325 DEBUG_JTAG_IO( "runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, \
326 tap_state_name(cmd->cmd.runtest->end_state));
327
328 if (cmd->cmd.runtest->end_state != TAP_INVALID)
329 {
330 vsllink_end_state(cmd->cmd.runtest->end_state);
331 }
332 vsllink_runtest(cmd->cmd.runtest->num_cycles);
333 break;
334
335 case JTAG_STATEMOVE:
336 DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state));
337
338 if (cmd->cmd.statemove->end_state != TAP_INVALID)
339 {
340 vsllink_end_state(cmd->cmd.statemove->end_state);
341 }
342 vsllink_state_move();
343 break;
344
345 case JTAG_PATHMOVE:
346 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
347 cmd->cmd.pathmove->num_states, \
348 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
349
350 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
351 break;
352
353 case JTAG_SCAN:
354 if (cmd->cmd.scan->end_state != TAP_INVALID)
355 {
356 vsllink_end_state(cmd->cmd.scan->end_state);
357 }
358
359 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
360 if (cmd->cmd.scan->ir_scan)
361 {
362 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
363 }
364 else
365 {
366 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
367 }
368
369 #ifdef _DEBUG_JTAG_IO_
370 vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
371 #endif
372
373 type = jtag_scan_type(cmd->cmd.scan);
374
375 vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
376 break;
377
378 case JTAG_RESET:
379 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
380
381 vsllink_tap_execute();
382
383 if (cmd->cmd.reset->trst == 1)
384 {
385 tap_set_state(TAP_RESET);
386 }
387 vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
388 break;
389
390 case JTAG_SLEEP:
391 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
392 vsllink_tap_execute();
393 jtag_sleep(cmd->cmd.sleep->us);
394 break;
395
396 case JTAG_STABLECLOCKS:
397 DEBUG_JTAG_IO("add %d clocks", cmd->cmd.stableclocks->num_cycles);
398 switch(tap_get_state())
399 {
400 case TAP_RESET:
401 // tms should be '1' to stay in TAP_RESET mode
402 scan_size = 1;
403 break;
404 case TAP_DRSHIFT:
405 case TAP_IDLE:
406 case TAP_DRPAUSE:
407 case TAP_IRSHIFT:
408 case TAP_IRPAUSE:
409 // in other mode, tms should be '0'
410 scan_size = 0;
411 break; /* above stable states are OK */
412 default:
413 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
414 tap_state_name(tap_get_state()) );
415 exit(-1);
416 }
417 vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
418 break;
419
420 default:
421 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
422 exit(-1);
423 }
424 cmd = cmd->next;
425 }
426
427 return vsllink_tap_execute();
428 }
429
430 static int vsllink_speed(int speed)
431 {
432 int result;
433
434 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
435 vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
436 vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
437
438 result = vsllink_usb_write(vsllink_jtag_handle, 3);
439
440 if (result == 3)
441 {
442 return ERROR_OK;
443 }
444 else
445 {
446 LOG_ERROR("VSLLink setting speed failed (%d)", result);
447 return ERROR_JTAG_DEVICE_ERROR;
448 }
449
450 return ERROR_OK;
451 }
452
453 static int vsllink_khz(int khz, int *jtag_speed)
454 {
455 *jtag_speed = khz;
456
457 return ERROR_OK;
458 }
459
460 static int vsllink_speed_div(int jtag_speed, int *khz)
461 {
462 *khz = jtag_speed;
463
464 return ERROR_OK;
465 }
466
467 static int vsllink_init(void)
468 {
469 int check_cnt, to_tmp;
470 int result;
471 char version_str[100];
472
473 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
474 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
475 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
476 {
477 LOG_ERROR("Not enough memory");
478 exit(-1);
479 }
480
481 vsllink_jtag_handle = vsllink_usb_open();
482
483 if (vsllink_jtag_handle == 0)
484 {
485 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
486 return ERROR_JTAG_INIT_FAILED;
487 }
488 LOG_DEBUG("vsllink found on %04X:%04X", vsllink_usb_vid, vsllink_usb_pid);
489
490 to_tmp = VSLLINK_USB_TIMEOUT;
491 VSLLINK_USB_TIMEOUT = 100;
492 check_cnt = 0;
493 while (check_cnt < 5)
494 {
495 vsllink_simple_command(0x00);
496 result = vsllink_usb_read(vsllink_jtag_handle);
497
498 if (result > 2)
499 {
500 vsllink_usb_in_buffer[result] = 0;
501 VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
502 strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
503 LOG_INFO("%s", version_str);
504
505 // free the pre-alloc memroy
506 free(vsllink_usb_in_buffer);
507 free(vsllink_usb_out_buffer);
508 vsllink_usb_in_buffer = NULL;
509 vsllink_usb_out_buffer = NULL;
510
511 // alloc new memory
512 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
513 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
514 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
515 {
516 LOG_ERROR("Not enough memory");
517 exit(-1);
518 }
519 else
520 {
521 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
522 }
523 // alloc memory for dma mode
524 if (vsllink_mode == VSLLINK_MODE_DMA)
525 {
526 tap_buffer_size = (VSLLINK_BufferSize - 3) / 2;
527 tms_buffer = (u8*)malloc(tap_buffer_size);
528 tdi_buffer = (u8*)malloc(tap_buffer_size);
529 tdo_buffer = (u8*)malloc(tap_buffer_size);
530 if ((tms_buffer == NULL) || (tdi_buffer == NULL) || (tdo_buffer == NULL))
531 {
532 LOG_ERROR("Not enough memory");
533 exit(-1);
534 }
535 }
536 break;
537 }
538 vsllink_simple_command(VSLLINK_CMD_DISCONN);
539 check_cnt++;
540 }
541 if (check_cnt == 3)
542 {
543 // It's dangerout to proced
544 LOG_ERROR("VSLLink initial failed");
545 exit(-1);
546 }
547 VSLLINK_USB_TIMEOUT = to_tmp;
548
549 // connect to vsllink
550 vsllink_connect();
551 // initialize function pointers
552 if (vsllink_mode == VSLLINK_MODE_NORMAL)
553 {
554 // normal mode
555 vsllink_state_move = vsllink_state_move_normal;
556 vsllink_path_move = vsllink_path_move_normal;
557 vsllink_stableclocks = vsllink_stableclocks_normal;
558 vsllink_scan = vsllink_scan_normal;
559
560 vsllink_tap_init = vsllink_tap_init_normal;
561 vsllink_tap_execute = vsllink_tap_execute_normal;
562 vsllink_tap_ensure_space = vsllink_tap_ensure_space_normal;
563
564 LOG_INFO("vsllink run in NORMAL mode");
565 }
566 else
567 {
568 // dma mode
569 vsllink_state_move = vsllink_state_move_dma;
570 vsllink_path_move = vsllink_path_move_dma;
571 vsllink_stableclocks = vsllink_stableclocks_dma;
572 vsllink_scan = vsllink_scan_dma;
573
574 vsllink_tap_init = vsllink_tap_init_dma;
575 vsllink_tap_execute = vsllink_tap_execute_dma;
576 vsllink_tap_ensure_space = vsllink_tap_ensure_space_dma;
577
578 LOG_INFO("vsllink run in DMA mode");
579 }
580
581 // Set SRST and TRST to output, Set USR1 and USR2 to input
582 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
583 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
584 vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
585 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
586 {
587 LOG_ERROR("VSLLink USB send data error");
588 exit(-1);
589 }
590
591 vsllink_reset(0, 0);
592
593 LOG_INFO("VSLLink JTAG Interface ready");
594
595 vsllink_tap_init();
596
597 return ERROR_OK;
598 }
599
600 static int vsllink_quit(void)
601 {
602 if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
603 {
604 // Set all pins to input
605 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
606 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
607 vsllink_usb_out_buffer[2] = 0;
608 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
609 {
610 LOG_ERROR("VSLLink USB send data error");
611 exit(-1);
612 }
613
614 // disconnect
615 vsllink_disconnect();
616 vsllink_usb_close(vsllink_jtag_handle);
617 vsllink_jtag_handle = NULL;
618 }
619
620 if (vsllink_usb_in_buffer != NULL)
621 {
622 free(vsllink_usb_in_buffer);
623 vsllink_usb_in_buffer = NULL;
624 }
625 if (vsllink_usb_out_buffer != NULL)
626 {
627 free(vsllink_usb_out_buffer);
628 vsllink_usb_out_buffer = NULL;
629 }
630
631 return ERROR_OK;
632 }
633
634 /***************************************************************************/
635 /* Queue command implementations */
636 static int vsllink_disconnect(void)
637 {
638 vsllink_simple_command(VSLLINK_CMD_DISCONN);
639 return ERROR_OK;
640 }
641
642 static int vsllink_connect(void)
643 {
644 char vsllink_str[100];
645
646 vsllink_usb_out_buffer[0] = VSLLINK_CMD_CONN;
647 vsllink_usb_out_buffer[1] = vsllink_mode;
648 vsllink_usb_message(vsllink_jtag_handle, 2, 0);
649 if (vsllink_usb_read(vsllink_jtag_handle) > 2)
650 {
651 strncpy(vsllink_str, (char *)vsllink_usb_in_buffer + 2, sizeof(vsllink_str));
652 LOG_INFO("%s", vsllink_str);
653 }
654
655 return ERROR_OK;
656 }
657
658 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
659 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
660 static void vsllink_append_tms(void)
661 {
662 u8 tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
663 u16 tms2;
664 insert_insignificant_operation_t *insert = \
665 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
666
667 if (((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_DRPAUSE) && (tap_get_state() != TAP_IRPAUSE)) || \
668 (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || \
669 (vsllink_tms_cmd_pos == NULL))
670 {
671 LOG_ERROR("There MUST be some bugs in the driver");
672 exit(-1);
673 }
674
675 tms2 = (tms_scan & VSLLINK_BIT_MSK[insert->insert_position]) << \
676 vsllink_tms_data_len;
677 if (insert->insert_value == 1)
678 {
679 tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << \
680 (vsllink_tms_data_len + insert->insert_position);
681 }
682 tms2 |= (tms_scan >> insert->insert_position) << \
683 (8 + insert->insert_position);
684
685 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
686 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
687
688 vsllink_tms_data_len = 0;
689 vsllink_tms_cmd_pos = NULL;
690 }
691
692 static void vsllink_end_state(tap_state_t state)
693 {
694 if (tap_is_state_stable(state))
695 {
696 tap_set_end_state(state);
697 }
698 else
699 {
700 LOG_ERROR("BUG: %i is not a valid end state", state);
701 exit(-1);
702 }
703 }
704
705 /* Goes to the end state. */
706 static void vsllink_state_move_normal(void)
707 {
708 if (vsllink_tms_data_len > 0)
709 {
710 vsllink_append_tms();
711 }
712 else
713 {
714 vsllink_tap_ensure_space(0, 2);
715
716 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
717 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
718 }
719
720 tap_set_state(tap_get_end_state());
721 }
722 static void vsllink_state_move_dma(void)
723 {
724 int i, insert_length = (tap_length % 8) ? (8 - (tap_length % 8)) : 0;
725 insert_insignificant_operation_t *insert = \
726 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
727 u8 tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
728
729 vsllink_tap_ensure_space(0, 8);
730
731 if (tap_get_state() == TAP_RESET)
732 {
733 for (i = 0; i < 8; i++)
734 {
735 vsllink_tap_append_step(1, 0);
736 }
737 }
738
739 if (insert_length > 0)
740 {
741 vsllink_tap_ensure_space(0, 16);
742
743 for (i = 0; i < insert->insert_position; i++)
744 {
745 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
746 }
747 for (i = 0; i < insert_length; i++)
748 {
749 vsllink_tap_append_step(insert->insert_value, 0);
750 }
751 for (i = insert->insert_position; i < 8; i++)
752 {
753 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
754 }
755 }
756 else
757 {
758 vsllink_tap_ensure_space(0, 8);
759
760 for (i = 0; i < 8; i++)
761 {
762 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
763 }
764 }
765
766 tap_set_state(tap_get_end_state());
767 }
768
769 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
770 static void vsllink_add_path(int start, int num, tap_state_t *path)
771 {
772 int i;
773
774 for (i = start; i < (start + num); i++)
775 {
776 if ((i & 7) == 0)
777 {
778 if (i > 0)
779 {
780 vsllink_usb_out_buffer_idx++;
781 }
782 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
783 }
784
785 if (path[i - start] == tap_state_transition(tap_get_state(), true))
786 {
787 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
788 }
789 else if (path[i - start] == tap_state_transition(tap_get_state(), false))
790 {
791 // nothing to do
792 }
793 else
794 {
795 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
796 exit(-1);
797 }
798 tap_set_state(path[i - start]);
799 }
800 if ((i > 0) && ((i & 7) == 0))
801 {
802 vsllink_usb_out_buffer_idx++;
803 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
804 }
805
806 tap_set_end_state(tap_get_state());
807 }
808
809 static void vsllink_path_move_normal(int num_states, tap_state_t *path)
810 {
811 int i, tms_len, tms_cmd_pos, path_idx = 0;
812
813 if (vsllink_tms_data_len > 0)
814 {
815 // there are vsllink_tms_data_len more tms bits to be shifted
816 // so there are vsllink_tms_data_len + num_states tms bits in all
817 tms_len = vsllink_tms_data_len + num_states;
818 if (tms_len <= 16)
819 {
820 // merge into last tms shift
821 if (tms_len < 8)
822 {
823 // just append tms data to the last tms byte
824 vsllink_add_path(vsllink_tms_data_len, num_states, path);
825 }
826 else if (tms_len == 8)
827 {
828 // end last tms shift command
829 (*vsllink_tms_cmd_pos)--;
830 vsllink_add_path(vsllink_tms_data_len, num_states, path);
831 }
832 else if (tms_len < 16)
833 {
834 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
835 {
836 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
837 // there is enought tms length in the current tms shift command
838 (*vsllink_tms_cmd_pos)++;
839 vsllink_add_path(vsllink_tms_data_len, num_states, path);
840 }
841 else
842 {
843 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
844 // not enough tms length in the current tms shift command
845 // so a new command should be added
846 // first decrease byte length of last tms shift command
847 (*vsllink_tms_cmd_pos)--;
848 // append tms data to the last tms byte
849 vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
850 path += 8 - vsllink_tms_data_len;
851 // add new command(3 bytes)
852 vsllink_tap_ensure_space(0, 3);
853 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
854 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
855 vsllink_add_path(0, num_states - (8 - vsllink_tms_data_len), path);
856 }
857 }
858 else if (tms_len == 16)
859 {
860 // end last tms shift command
861 vsllink_add_path(vsllink_tms_data_len, num_states, path);
862 }
863
864 vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
865 if (vsllink_tms_data_len == 0)
866 {
867 vsllink_tms_cmd_pos = NULL;
868 }
869 num_states = 0;
870 }
871 else
872 {
873 vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
874
875 path += 16 - vsllink_tms_data_len;
876 num_states -= 16 - vsllink_tms_data_len;
877 vsllink_tms_data_len = 0;
878 vsllink_tms_cmd_pos = NULL;
879 }
880 }
881
882 if (num_states > 0)
883 {
884 // Normal operation, don't need to append tms data
885 vsllink_tms_data_len = num_states & 7;
886
887 while (num_states > 0)
888 {
889 if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
890 {
891 i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
892 }
893 else
894 {
895 i = num_states;
896 }
897 tms_len = (i + 7) >> 3;
898 vsllink_tap_ensure_space(0, tms_len + 2);
899 tms_cmd_pos = vsllink_usb_out_buffer_idx;
900 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
901
902 vsllink_add_path(0, i, path + path_idx);
903
904 path_idx += i;
905 num_states -= i;
906 }
907
908 if (vsllink_tms_data_len > 0)
909 {
910 if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
911 {
912 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[tms_cmd_pos];
913 (*vsllink_tms_cmd_pos)++;
914 }
915 else
916 {
917 vsllink_usb_out_buffer[tms_cmd_pos]--;
918
919 tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
920 vsllink_tap_ensure_space(0, 3);
921 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
922 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
923 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
924 }
925 }
926 }
927 }
928 static void vsllink_path_move_dma(int num_states, tap_state_t *path)
929 {
930 int i, j = 0;
931
932 if (tap_length & 7)
933 {
934 if ((8 - (tap_length & 7)) < num_states)
935 {
936 j = 8 - (tap_length & 7);
937 }
938 else
939 {
940 j = num_states;
941 }
942 for (i = 0; i < j; i++)
943 {
944 if (path[i] == tap_state_transition(tap_get_state(), false))
945 {
946 vsllink_tap_append_step(0, 0);
947 }
948 else if (path[i] == tap_state_transition(tap_get_state(), true))
949 {
950 vsllink_tap_append_step(1, 0);
951 }
952 else
953 {
954 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
955 exit(-1);
956 }
957 tap_set_state(path[i]);
958 }
959 num_states -= j;
960 }
961
962 if (num_states > 0)
963 {
964 vsllink_tap_ensure_space(0, num_states);
965
966 for (i = 0; i < num_states; i++)
967 {
968 if (path[j + i] == tap_state_transition(tap_get_state(), false))
969 {
970 vsllink_tap_append_step(0, 0);
971 }
972 else if (path[j + i] == tap_state_transition(tap_get_state(), true))
973 {
974 vsllink_tap_append_step(1, 0);
975 }
976 else
977 {
978 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
979 exit(-1);
980 }
981 tap_set_state(path[j + i]);
982 }
983 }
984
985 tap_set_end_state(tap_get_state());
986 }
987
988 static void vsllink_stableclocks_normal(int num_cycles, int tms)
989 {
990 int tms_len;
991 u16 tms_append_byte;
992
993 if (vsllink_tms_data_len > 0)
994 {
995 // there are vsllink_tms_data_len more tms bits to be shifted
996 // so there are vsllink_tms_data_len + num_cycles tms bits in all
997 tms_len = vsllink_tms_data_len + num_cycles;
998 if (tms > 0)
999 {
1000 // append '1' for tms
1001 tms_append_byte = (u16)((((1 << num_cycles) - 1) << vsllink_tms_data_len) & 0xFFFF);
1002 }
1003 else
1004 {
1005 // append '0' for tms
1006 tms_append_byte = 0;
1007 }
1008 if (tms_len <= 16)
1009 {
1010 // merge into last tms shift
1011 if (tms_len < 8)
1012 {
1013 // just add to vsllink_tms_data_len
1014 // same result if tun through
1015 //vsllink_tms_data_len += num_cycles;
1016 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= (u8)(tms_append_byte & 0xFF);
1017 }
1018 else if (tms_len == 8)
1019 {
1020 // end last tms shift command
1021 // just reduce it, and append last tms byte
1022 (*vsllink_tms_cmd_pos)--;
1023 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
1024 }
1025 else if (tms_len < 16)
1026 {
1027 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
1028 {
1029 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
1030 // there is enought tms length in the current tms shift command
1031 // increase the tms byte length by 1 and set the last byte to 0
1032 (*vsllink_tms_cmd_pos)++;
1033 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
1034 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (u8)(tms_append_byte >> 8);
1035 }
1036 else
1037 {
1038 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
1039 // not enough tms length in the current tms shift command
1040 // so a new command should be added
1041 // first decrease byte length of last tms shift command
1042 (*vsllink_tms_cmd_pos)--;
1043 // append last tms byte and move the command pointer to the next empty position
1044 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
1045 // add new command(3 bytes)
1046 vsllink_tap_ensure_space(0, 3);
1047 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1048 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
1049 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (u8)(tms_append_byte >> 8);
1050 }
1051 }
1052 else if (tms_len == 16)
1053 {
1054 // end last tms shift command
1055 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
1056 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (u8)(tms_append_byte >> 8);
1057 }
1058
1059 vsllink_tms_data_len = tms_len & 7;
1060 if (vsllink_tms_data_len == 0)
1061 {
1062 vsllink_tms_cmd_pos = NULL;
1063 }
1064 num_cycles = 0;
1065 }
1066 else
1067 {
1068 // more shifts will be needed
1069 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
1070 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (u8)(tms_append_byte >> 8);
1071
1072 num_cycles -= 16 - vsllink_tms_data_len;
1073 vsllink_tms_data_len = 0;
1074 vsllink_tms_cmd_pos = NULL;
1075 }
1076 }
1077 // from here vsllink_tms_data_len == 0 or num_cycles == 0
1078
1079 if (vsllink_tms_data_len > 0)
1080 {
1081 // num_cycles == 0
1082 // no need to shift
1083 if (num_cycles > 0)
1084 {
1085 LOG_ERROR("There MUST be some bugs in the driver");
1086 exit(-1);
1087 }
1088 }
1089 else
1090 {
1091 // get number of bytes left to be sent
1092 tms_len = num_cycles >> 3;
1093 if (tms_len > 0)
1094 {
1095 vsllink_tap_ensure_space(1, 5);
1096 // if tms_len > 0, vsllink_tms_data_len == 0
1097 // so just add new command
1098 // LSB of the command byte is the tms value when do the shifting
1099 if (tms > 0)
1100 {
1101 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK | 1;
1102 }
1103 else
1104 {
1105 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK;
1106 }
1107 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 0) & 0xff;
1108 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 8) & 0xff;
1109 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 16) & 0xff;
1110 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 24) & 0xff;
1111
1112 vsllink_usb_in_want_length += 1;
1113 pending_scan_results_buffer[pending_scan_results_length].buffer = NULL;
1114 pending_scan_results_length++;
1115
1116 if (tms_len > 0xFFFF)
1117 {
1118 vsllink_tap_execute();
1119 }
1120 }
1121
1122 // post-process
1123 vsllink_tms_data_len = num_cycles & 7;
1124 if (vsllink_tms_data_len > 0)
1125 {
1126 vsllink_tap_ensure_space(0, 3);
1127 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1128 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
1129 if (tms > 0)
1130 {
1131 // append '1' for tms
1132 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (1 << vsllink_tms_data_len) - 1;
1133 }
1134 else
1135 {
1136 // append '0' for tms
1137 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0x00;
1138 }
1139 }
1140 }
1141 }
1142 static void vsllink_stableclocks_dma(int num_cycles, int tms)
1143 {
1144 int i, cur_cycles;
1145
1146 if (tap_length & 7)
1147 {
1148 if ((8 - (tap_length & 7)) < num_cycles)
1149 {
1150 cur_cycles = 8 - (tap_length & 7);
1151 }
1152 else
1153 {
1154 cur_cycles = num_cycles;
1155 }
1156 for (i = 0; i < cur_cycles; i++)
1157 {
1158 vsllink_tap_append_step(tms, 0);
1159 }
1160 num_cycles -= cur_cycles;
1161 }
1162
1163 while (num_cycles > 0)
1164 {
1165 if (num_cycles > 8 * tap_buffer_size)
1166 {
1167 cur_cycles = 8 * tap_buffer_size;
1168 }
1169 else
1170 {
1171 cur_cycles = num_cycles;
1172 }
1173
1174 vsllink_tap_ensure_space(0, cur_cycles);
1175
1176 for (i = 0; i < cur_cycles; i++)
1177 {
1178 vsllink_tap_append_step(tms, 0);
1179 }
1180
1181 num_cycles -= cur_cycles;
1182 }
1183 }
1184
1185 static void vsllink_runtest(int num_cycles)
1186 {
1187 tap_state_t saved_end_state = tap_get_end_state();
1188
1189 if (tap_get_state() != TAP_IDLE)
1190 {
1191 // enter into IDLE state
1192 vsllink_end_state(TAP_IDLE);
1193 vsllink_state_move();
1194 }
1195
1196 vsllink_stableclocks(num_cycles, 0);
1197
1198 // post-process
1199 // set end_state
1200 vsllink_end_state(saved_end_state);
1201 tap_set_state(TAP_IDLE);
1202 if (tap_get_end_state() != TAP_IDLE)
1203 {
1204 vsllink_state_move();
1205 }
1206 }
1207
1208 static void vsllink_scan_normal(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
1209 {
1210 tap_state_t saved_end_state;
1211 u8 bits_left, tms_tmp, tdi_len;
1212 int i;
1213
1214 if (0 == scan_size )
1215 {
1216 return;
1217 }
1218
1219 tdi_len = ((scan_size + 7) >> 3);
1220 if ((tdi_len + 7) > VSLLINK_BufferSize)
1221 {
1222 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
1223 exit(-1);
1224 }
1225
1226 saved_end_state = tap_get_end_state();
1227
1228 /* Move to appropriate scan state */
1229 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1230
1231 if (vsllink_tms_data_len > 0)
1232 {
1233 if (tap_get_state() == tap_get_end_state())
1234 {
1235 // already in IRSHIFT or DRSHIFT state
1236 // merge tms data in the last tms shift command into next scan command
1237 if(*vsllink_tms_cmd_pos < 1)
1238 {
1239 LOG_ERROR("There MUST be some bugs in the driver");
1240 exit(-1);
1241 }
1242 else if(*vsllink_tms_cmd_pos < 2)
1243 {
1244 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1245 vsllink_usb_out_buffer_idx--;
1246 }
1247 else
1248 {
1249 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1250 *vsllink_tms_cmd_pos -= 2;
1251 }
1252
1253 vsllink_tap_ensure_space(1, tdi_len + 7);
1254 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1255 // which is merged from the last tms shift command
1256 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1257 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1258 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
1259 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
1260 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
1261
1262 for (i = 0; i < tdi_len; i++)
1263 {
1264 buffer[i] >>= 8 - vsllink_tms_data_len;
1265 if (i != tdi_len)
1266 {
1267 buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
1268 }
1269 }
1270
1271 vsllink_tap_append_scan_normal(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
1272 scan_size -= 8 - vsllink_tms_data_len;
1273 vsllink_tms_data_len = 0;
1274 }
1275 else
1276 {
1277 vsllink_state_move();
1278 vsllink_tap_ensure_space(1, tdi_len + 5);
1279
1280 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
1281 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
1282 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
1283
1284 vsllink_tap_append_scan_normal(scan_size, buffer, command, 0);
1285 }
1286 }
1287 else
1288 {
1289 vsllink_tap_ensure_space(1, tdi_len + 7);
1290
1291 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1292 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1293 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
1294 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1295 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1296
1297 vsllink_tap_append_scan_normal(scan_size, buffer, command, 8);
1298 }
1299 vsllink_end_state(saved_end_state);
1300
1301 bits_left = scan_size & 0x07;
1302 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1303
1304 if (bits_left > 0)
1305 {
1306 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
1307 }
1308 else
1309 {
1310 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
1311 }
1312
1313 if (tap_get_state() != tap_get_end_state())
1314 {
1315 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1316 }
1317 else
1318 {
1319 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1320 }
1321
1322 tap_set_state(tap_get_end_state());
1323 }
1324 static void vsllink_scan_dma(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
1325 {
1326 tap_state_t saved_end_state;
1327
1328 saved_end_state = tap_get_end_state();
1329
1330 /* Move to appropriate scan state */
1331 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1332
1333 vsllink_state_move();
1334 vsllink_end_state(saved_end_state);
1335
1336 /* Scan */
1337 vsllink_tap_ensure_space(1, (scan_size + 7) & ~0x00000007);
1338 vsllink_tap_append_scan_dma(scan_size, buffer, command);
1339
1340 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1341 while (tap_length % 8 != 0)
1342 {
1343 // more 0s in Pause
1344 vsllink_tap_append_step(0, 0);
1345 }
1346
1347 if (tap_get_state() != tap_get_end_state())
1348 {
1349 vsllink_state_move();
1350 }
1351 }
1352
1353 static void vsllink_reset(int trst, int srst)
1354 {
1355 int result;
1356
1357 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
1358
1359 /* Signals are active low */
1360 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
1361 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
1362 vsllink_usb_out_buffer[2] = 0;
1363 if (srst == 0)
1364 {
1365 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
1366 }
1367 if (trst == 0)
1368 {
1369 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
1370 }
1371
1372 result = vsllink_usb_write(vsllink_jtag_handle, 3);
1373 if (result != 3)
1374 {
1375 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
1376 }
1377 }
1378
1379 static void vsllink_simple_command(u8 command)
1380 {
1381 int result;
1382
1383 DEBUG_JTAG_IO("0x%02x", command);
1384
1385 vsllink_usb_out_buffer[0] = command;
1386 result = vsllink_usb_write(vsllink_jtag_handle, 1);
1387
1388 if (result != 1)
1389 {
1390 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
1391 }
1392 }
1393
1394 static int vsllink_register_commands(struct command_context_s *cmd_ctx)
1395 {
1396 register_command(cmd_ctx, NULL, "vsllink_usb_vid", vsllink_handle_usb_vid_command,
1397 COMMAND_CONFIG, NULL);
1398 register_command(cmd_ctx, NULL, "vsllink_usb_pid", vsllink_handle_usb_pid_command,
1399 COMMAND_CONFIG, NULL);
1400 register_command(cmd_ctx, NULL, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command,
1401 COMMAND_CONFIG, NULL);
1402 register_command(cmd_ctx, NULL, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command,
1403 COMMAND_CONFIG, NULL);
1404 register_command(cmd_ctx, NULL, "vsllink_usb_interface", vsllink_handle_usb_interface_command,
1405 COMMAND_CONFIG, NULL);
1406 register_command(cmd_ctx, NULL, "vsllink_mode", vsllink_handle_mode_command,
1407 COMMAND_CONFIG, NULL);
1408
1409 return ERROR_OK;
1410 }
1411
1412 static int vsllink_handle_mode_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1413 {
1414 if (argc != 1) {
1415 LOG_ERROR("parameter error, should be one parameter for VID");
1416 return ERROR_FAIL;
1417 }
1418
1419 if (!strcmp(args[0], "normal"))
1420 {
1421 vsllink_mode = VSLLINK_MODE_NORMAL;
1422 }
1423 else if (!strcmp(args[0], "dma"))
1424 {
1425 vsllink_mode = VSLLINK_MODE_DMA;
1426 }
1427 else
1428 {
1429 LOG_ERROR("invalid vsllink_mode: %s", args[0]);
1430 return ERROR_FAIL;
1431 }
1432
1433 return ERROR_OK;
1434 }
1435
1436 static int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1437 {
1438 if (argc != 1)
1439 {
1440 LOG_ERROR("parameter error, should be one parameter for VID");
1441 return ERROR_OK;
1442 }
1443
1444 vsllink_usb_vid = strtol(args[0], NULL, 0);
1445
1446 return ERROR_OK;
1447 }
1448
1449 static int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1450 {
1451 if (argc != 1)
1452 {
1453 LOG_ERROR("parameter error, should be one parameter for PID");
1454 return ERROR_OK;
1455 }
1456
1457 vsllink_usb_pid = strtol(args[0], NULL, 0);
1458
1459 return ERROR_OK;
1460 }
1461
1462 static int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1463 {
1464 if (argc != 1)
1465 {
1466 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1467 return ERROR_OK;
1468 }
1469
1470 vsllink_usb_bulkin = strtol(args[0], NULL, 0) | 0x80;
1471
1472 return ERROR_OK;
1473 }
1474
1475 static int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1476 {
1477 if (argc != 1)
1478 {
1479 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1480 return ERROR_OK;
1481 }
1482
1483 vsllink_usb_bulkout = strtol(args[0], NULL, 0);
1484
1485 return ERROR_OK;
1486 }
1487
1488 static int vsllink_handle_usb_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1489 {
1490 if (argc != 1)
1491 {
1492 LOG_ERROR("parameter error, should be one parameter for interface number");
1493 return ERROR_OK;
1494 }
1495
1496 vsllink_usb_interface = strtol(args[0], NULL, 0);
1497
1498 return ERROR_OK;
1499 }
1500
1501 /***************************************************************************/
1502 /* VSLLink tap functions */
1503
1504 static void vsllink_tap_init_normal(void)
1505 {
1506 vsllink_usb_out_buffer_idx = 0;
1507 vsllink_usb_in_want_length = 0;
1508 pending_scan_results_length = 0;
1509 }
1510 static void vsllink_tap_init_dma(void)
1511 {
1512 tap_length = 0;
1513 pending_scan_results_length = 0;
1514 }
1515
1516 static void vsllink_tap_ensure_space_normal(int scans, int length)
1517 {
1518 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1519 int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
1520
1521 if (scans > available_scans || length > available_bytes)
1522 {
1523 vsllink_tap_execute();
1524 }
1525 }
1526 static void vsllink_tap_ensure_space_dma(int scans, int length)
1527 {
1528 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1529 int available_bytes = tap_buffer_size * 8 - tap_length;
1530
1531 if (scans > available_scans || length > available_bytes)
1532 {
1533 vsllink_tap_execute();
1534 }
1535 }
1536
1537 static void vsllink_tap_append_step(int tms, int tdi)
1538 {
1539 last_tms = tms;
1540 int index = tap_length / 8;
1541
1542 if (index < tap_buffer_size)
1543 {
1544 int bit_index = tap_length % 8;
1545 u8 bit = 1 << bit_index;
1546
1547 if (tms)
1548 {
1549 tms_buffer[index] |= bit;
1550 }
1551 else
1552 {
1553 tms_buffer[index] &= ~bit;
1554 }
1555
1556 if (tdi)
1557 {
1558 tdi_buffer[index] |= bit;
1559 }
1560 else
1561 {
1562 tdi_buffer[index] &= ~bit;
1563 }
1564
1565 tap_length++;
1566 }
1567 else
1568 {
1569 LOG_ERROR("buffer overflow, tap_length=%d", tap_length);
1570 }
1571 }
1572
1573 static void vsllink_tap_append_scan_normal(int length, u8 *buffer, scan_command_t *command, int offset)
1574 {
1575 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1576 int i;
1577
1578 if (offset > 0)
1579 {
1580 vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
1581 }
1582 else
1583 {
1584 vsllink_usb_in_want_length += (length + 7) >> 3;
1585 }
1586 pending_scan_result->length = length;
1587 pending_scan_result->offset = offset;
1588 pending_scan_result->command = command;
1589 pending_scan_result->buffer = buffer;
1590
1591 for (i = 0; i < ((length + 7) >> 3); i++)
1592 {
1593 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
1594 }
1595
1596 pending_scan_results_length++;
1597 }
1598 static void vsllink_tap_append_scan_dma(int length, u8 *buffer, scan_command_t *command)
1599 {
1600 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1601 int i;
1602
1603 pending_scan_result->offset = tap_length;
1604 pending_scan_result->length = length;
1605 pending_scan_result->command = command;
1606 pending_scan_result->buffer = buffer;
1607
1608 for (i = 0; i < length; i++)
1609 {
1610 vsllink_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
1611 }
1612
1613 pending_scan_results_length++;
1614 }
1615
1616 /* Pad and send a tap sequence to the device, and receive the answer.
1617 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1618 static int vsllink_tap_execute_normal(void)
1619 {
1620 int i;
1621 int result;
1622 int first = 0;
1623
1624 if (vsllink_tms_data_len > 0)
1625 {
1626 if((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_IRPAUSE) && (tap_get_state() != TAP_DRPAUSE))
1627 {
1628 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
1629 }
1630
1631 if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
1632 {
1633 // last tms bit is '1'
1634 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= 0xFF << vsllink_tms_data_len;
1635 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0xFF;
1636 vsllink_tms_data_len = 0;
1637 }
1638 else
1639 {
1640 // last tms bit is '0'
1641 vsllink_usb_out_buffer_idx++;
1642 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1643 vsllink_tms_data_len = 0;
1644 }
1645 }
1646
1647 if (vsllink_usb_out_buffer_idx > 3)
1648 {
1649 if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
1650 {
1651 vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
1652 vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
1653 }
1654
1655 result = vsllink_usb_message(vsllink_jtag_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
1656
1657 if (result == vsllink_usb_in_want_length)
1658 {
1659 for (i = 0; i < pending_scan_results_length; i++)
1660 {
1661 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1662 u8 *buffer = pending_scan_result->buffer;
1663 int length = pending_scan_result->length;
1664 int offset = pending_scan_result->offset;
1665 scan_command_t *command = pending_scan_result->command;
1666
1667 if (buffer != NULL)
1668 {
1669 // IRSHIFT or DRSHIFT
1670 buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
1671 first += (length + offset + 7) >> 3;
1672
1673 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
1674 #ifdef _DEBUG_JTAG_IO_
1675 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1676 #endif
1677
1678 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1679 {
1680 vsllink_tap_init();
1681 return ERROR_JTAG_QUEUE_FAILED;
1682 }
1683
1684 free(pending_scan_result->buffer);
1685 pending_scan_result->buffer = NULL;
1686 }
1687 else
1688 {
1689 first++;
1690 }
1691 }
1692 }
1693 else
1694 {
1695 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
1696 return ERROR_JTAG_QUEUE_FAILED;
1697 }
1698
1699 vsllink_tap_init();
1700 }
1701 reset_command_pointer();
1702
1703 return ERROR_OK;
1704 }
1705 static int vsllink_tap_execute_dma(void)
1706 {
1707 int byte_length;
1708 int i;
1709 int result;
1710
1711 if (tap_length > 0)
1712 {
1713 /* Pad last byte so that tap_length is divisible by 8 */
1714 while (tap_length % 8 != 0)
1715 {
1716 /* More of the last TMS value keeps us in the same state,
1717 * analogous to free-running JTAG interfaces. */
1718 vsllink_tap_append_step(last_tms, 0);
1719 }
1720 byte_length = tap_length / 8;
1721
1722 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGRAWCMD;
1723 vsllink_usb_out_buffer[1] = ((byte_length * 2 + 3) >> 0) & 0xff; // package size
1724 vsllink_usb_out_buffer[2] = ((byte_length * 2 + 3) >> 8) & 0xff;
1725
1726 memcpy(&vsllink_usb_out_buffer[3], tdi_buffer, byte_length);
1727 memcpy(&vsllink_usb_out_buffer[3 + byte_length], tms_buffer, byte_length);
1728
1729 result = vsllink_usb_message(vsllink_jtag_handle, 3 + 2 * byte_length, byte_length);
1730 if (result == byte_length)
1731 {
1732 for (i = 0; i < pending_scan_results_length; i++)
1733 {
1734 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1735 u8 *buffer = pending_scan_result->buffer;
1736 int length = pending_scan_result->length;
1737 int first = pending_scan_result->offset;
1738
1739 scan_command_t *command = pending_scan_result->command;
1740 buf_set_buf(vsllink_usb_in_buffer, first, buffer, 0, length);
1741
1742 DEBUG_JTAG_IO("JTAG scan read(%d bits, from %d bits):", length, first);
1743 #ifdef _DEBUG_JTAG_IO_
1744 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1745 #endif
1746
1747 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1748 {
1749 vsllink_tap_init();
1750 return ERROR_JTAG_QUEUE_FAILED;
1751 }
1752
1753 if (pending_scan_result->buffer != NULL)
1754 {
1755 free(pending_scan_result->buffer);
1756 }
1757 }
1758 }
1759 else
1760 {
1761 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, byte_length);
1762 return ERROR_JTAG_QUEUE_FAILED;
1763 }
1764
1765 vsllink_tap_init();
1766 }
1767
1768 return ERROR_OK;
1769 }
1770
1771 /*****************************************************************************/
1772 /* VSLLink USB low-level functions */
1773
1774 static vsllink_jtag_t* vsllink_usb_open(void)
1775 {
1776 struct usb_bus *busses;
1777 struct usb_bus *bus;
1778 struct usb_device *dev;
1779 int ret;
1780
1781 vsllink_jtag_t *result;
1782
1783 result = (vsllink_jtag_t*) malloc(sizeof(vsllink_jtag_t));
1784
1785 usb_init();
1786 usb_find_busses();
1787 usb_find_devices();
1788
1789 busses = usb_get_busses();
1790
1791 /* find vsllink_jtag device in usb bus */
1792
1793 for (bus = busses; bus; bus = bus->next)
1794 {
1795 for (dev = bus->devices; dev; dev = dev->next)
1796 {
1797 if ((dev->descriptor.idVendor == vsllink_usb_vid) && (dev->descriptor.idProduct == vsllink_usb_pid))
1798 {
1799 result->usb_handle = usb_open(dev);
1800 if (NULL == result->usb_handle)
1801 {
1802 LOG_ERROR("failed to open %04X:%04X, not enough permissions?", vsllink_usb_vid, vsllink_usb_pid);
1803 exit(-1);
1804 }
1805
1806 /* usb_set_configuration required under win32 */
1807 ret = usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
1808 if (ret != 0)
1809 {
1810 LOG_ERROR("fail to set configuration to %d, %d returned, not enough permissions?", dev->config[0].bConfigurationValue, ret);
1811 exit(-1);
1812 }
1813 ret = usb_claim_interface(result->usb_handle, vsllink_usb_interface);
1814 if (ret != 0)
1815 {
1816 LOG_ERROR("fail to claim interface %d, %d returned", vsllink_usb_interface, ret);
1817 exit(-1);
1818 }
1819
1820 #if 0
1821 /*
1822 * This makes problems under Mac OS X. And is not needed
1823 * under Windows. Hopefully this will not break a linux build
1824 */
1825 usb_set_altinterface(result->usb_handle, 0);
1826 #endif
1827 return result;
1828 }
1829 }
1830 }
1831
1832 free(result);
1833 return NULL;
1834 }
1835
1836 static void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag)
1837 {
1838 int ret;
1839
1840 ret = usb_release_interface(vsllink_jtag->usb_handle, vsllink_usb_interface);
1841 if (ret != 0)
1842 {
1843 LOG_ERROR("fail to release interface %d, %d returned", vsllink_usb_interface, ret);
1844 exit(-1);
1845 }
1846
1847 ret = usb_close(vsllink_jtag->usb_handle);
1848 if (ret != 0)
1849 {
1850 LOG_ERROR("fail to close usb, %d returned", ret);
1851 exit(-1);
1852 }
1853
1854 free(vsllink_jtag);
1855 }
1856
1857 /* Send a message and receive the reply. */
1858 static int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length)
1859 {
1860 int result;
1861
1862 result = vsllink_usb_write(vsllink_jtag, out_length);
1863 if (result == out_length)
1864 {
1865 if (in_length > 0)
1866 {
1867 result = vsllink_usb_read(vsllink_jtag);
1868 if (result == in_length )
1869 {
1870 return result;
1871 }
1872 else
1873 {
1874 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
1875 return -1;
1876 }
1877 }
1878 return 0;
1879 }
1880 else
1881 {
1882 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
1883 return -1;
1884 }
1885 }
1886
1887 /* Write data from out_buffer to USB. */
1888 static int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length)
1889 {
1890 int result;
1891
1892 if (out_length > VSLLINK_BufferSize)
1893 {
1894 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
1895 return -1;
1896 }
1897
1898 result = usb_bulk_write(vsllink_jtag->usb_handle, vsllink_usb_bulkout, \
1899 (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
1900
1901 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
1902
1903 #ifdef _DEBUG_USB_COMMS_
1904 LOG_DEBUG("USB out:");
1905 vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
1906 #endif
1907
1908 #ifdef _VSLLINK_IN_DEBUG_MODE_
1909 usleep(100000);
1910 #endif
1911
1912 return result;
1913 }
1914
1915 /* Read data from USB into in_buffer. */
1916 static int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag)
1917 {
1918 int result = usb_bulk_read(vsllink_jtag->usb_handle, vsllink_usb_bulkin, \
1919 (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
1920
1921 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
1922
1923 #ifdef _DEBUG_USB_COMMS_
1924 LOG_DEBUG("USB in:");
1925 vsllink_debug_buffer(vsllink_usb_in_buffer, result);
1926 #endif
1927 return result;
1928 }
1929
1930 #define BYTES_PER_LINE 16
1931
1932 #ifdef _DEBUG_USB_COMMS_
1933 static void vsllink_debug_buffer(u8 *buffer, int length)
1934 {
1935 char line[81];
1936 char s[4];
1937 int i;
1938 int j;
1939
1940 for (i = 0; i < length; i += BYTES_PER_LINE)
1941 {
1942 snprintf(line, 5, "%04x", i);
1943 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1944 {
1945 snprintf(s, 4, " %02x", buffer[j]);
1946 strcat(line, s);
1947 }
1948 LOG_DEBUG("%s", line);
1949 }
1950 }
1951 #endif // _DEBUG_USB_COMMS_

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)