vsllink support for stable clocks by Simon Qian <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 u16 vsllink_vid;
53 u16 vsllink_pid;
54 u8 vsllink_bulkout;
55 u8 vsllink_bulkin;
56
57 #define VSLLINK_USB_TIMEOUT 10000
58
59 static int VSLLINK_BufferSize = 1024;
60
61 /* Global USB buffers */
62 static int vsllink_usb_out_buffer_idx;
63 static int vsllink_usb_in_want_length;
64 static u8* vsllink_usb_in_buffer = NULL;
65 static u8* vsllink_usb_out_buffer = NULL;
66
67 /* Constants for VSLLink command */
68 #define VSLLINK_CMD_CONN 0x80
69 #define VSLLINK_CMD_DISCONN 0x81
70 #define VSLLINK_CMD_SET_SPEED 0x82
71 #define VSLLINK_CMD_SET_PORT 0x90
72 #define VSLLINK_CMD_GET_PORT 0x91
73 #define VSLLINK_CMD_SET_PORTDIR 0x92
74 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
75 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
76 #define VSLLINK_CMD_HW_SWDCMD 0xA2
77
78 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
79 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
80 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
81
82 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
83 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
84
85 #define JTAG_PINMSK_SRST (1 << 0)
86 #define JTAG_PINMSK_TRST (1 << 1)
87 #define JTAG_PINMSK_USR1 (1 << 2)
88 #define JTAG_PINMSK_USR2 (1 << 3)
89 #define JTAG_PINMSK_TCK (1 << 4)
90 #define JTAG_PINMSK_TMS (1 << 5)
91 #define JTAG_PINMSK_TDI (1 << 6)
92 #define JTAG_PINMSK_TDO (1 << 7)
93
94
95 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_map[from]][tap_move_map[to]]
96
97 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
98 * 0: Test-Logic-Reset
99 * 1: Run-Test/Idle
100 * 2: Shift-DR
101 * 3: Pause-DR
102 * 4: Shift-IR
103 * 5: Pause-IR
104 *
105 * SD->SD and SI->SI have to be caught in interface specific code
106 */
107 u8 VSLLINK_tap_move[6][6] =
108 {
109 /* TLR RTI SD PD SI PI */
110 {0xff, 0x7f, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
111 {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
112 {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
113 {0xff, 0x60, 0x40, 0x5c, 0x3c, 0x5e}, /* PD */
114 {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
115 {0xff, 0x60, 0x38, 0x5c, 0x40, 0x5e} /* PI */
116 };
117
118 typedef struct insert_insignificant_operation
119 {
120 unsigned char insert_value;
121 unsigned char insert_position;
122 }insert_insignificant_operation_t;
123
124 insert_insignificant_operation_t VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[6][6] =
125 {
126 /* stuff offset */
127 {/* TLR */
128 {1, 0,}, /* TLR */
129 {1, 0,}, /* RTI */
130 {1, 0,}, /* SD */
131 {1, 0,}, /* PD */
132 {1, 0,}, /* SI */
133 {1, 0,}}, /* PI */
134 {/* RTI */
135 {1, 0,}, /* TLR */
136 {0, 0,}, /* RTI */
137 {0, 4,}, /* SD */
138 {0, 7,}, /* PD */
139 {0, 5,}, /* SI */
140 {0, 7,}}, /* PI */
141 {/* SD */
142 {0, 0,}, /* TLR */
143 {0, 0,}, /* RTI */
144 {0, 0,}, /* SD */
145 {0, 0,}, /* PD */
146 {0, 0,}, /* SI */
147 {0, 0,}}, /* PI */
148 {/* PD */
149 {0, 0,}, /* TLR */
150 {0, 0,}, /* RTI */
151 {0, 0,}, /* SD */
152 {0, 0,}, /* PD */
153 {0, 0,}, /* SI */
154 {0, 0,}}, /* PI */
155 {/* SI */
156 {0, 0,}, /* TLR */
157 {0, 0,}, /* RTI */
158 {0, 0,}, /* SD */
159 {0, 0,}, /* PD */
160 {0, 0,}, /* SI */
161 {0, 0,}}, /* PI */
162 {/* PI */
163 {0, 0,}, /* TLR */
164 {0, 0,}, /* RTI */
165 {0, 0,}, /* SD */
166 {0, 0,}, /* PD */
167 {0, 0,}, /* SI */
168 {0, 0,}}, /* PI */
169 };
170
171 u8 VSLLINK_BIT_MSK[8] =
172 {
173 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
174 };
175
176 typedef struct
177 {
178 int length; /* Number of bits to read */
179 int offset;
180 scan_command_t *command; /* Corresponding scan command */
181 u8 *buffer;
182 } pending_scan_result_t;
183
184 #define MAX_PENDING_SCAN_RESULTS 256
185
186 static int pending_scan_results_length;
187 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
188
189 /* External interface functions */
190 int vsllink_execute_queue(void);
191 int vsllink_speed(int speed);
192 int vsllink_khz(int khz, int *jtag_speed);
193 int vsllink_speed_div(int jtag_speed, int *khz);
194 int vsllink_register_commands(struct command_context_s *cmd_ctx);
195 int vsllink_init(void);
196 int vsllink_quit(void);
197
198 /* CLI command handler functions */
199 int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
200 int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
201 int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
202 int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
203
204 /* Queue command functions */
205 void vsllink_end_state(enum tap_state state);
206 void vsllink_state_move(void);
207 void vsllink_path_move(int num_states, enum tap_state *path);
208 void vsllink_runtest(int num_cycles);
209 void vsllink_stableclocks(int num_cycles, int tms);
210 void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
211 void vsllink_reset(int trst, int srst);
212 void vsllink_simple_command(u8 command);
213
214 /* VSLLink tap buffer functions */
215 void vsllink_tap_init(void);
216 int vsllink_tap_execute(void);
217 void vsllink_tap_ensure_space(int scans, int bytes);
218 void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset);
219
220 /* VSLLink lowlevel functions */
221 typedef struct vsllink_jtag
222 {
223 struct usb_dev_handle* usb_handle;
224 } vsllink_jtag_t;
225
226 vsllink_jtag_t *vsllink_usb_open(void);
227 void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag);
228 int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length);
229 int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length);
230 int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag);
231
232 void vsllink_debug_buffer(u8 *buffer, int length);
233
234 static int vsllink_tms_data_len = 0;
235 static u8* vsllink_tms_cmd_pos;
236
237 vsllink_jtag_t* vsllink_jtag_handle;
238
239 /***************************************************************************/
240 /* External interface implementation */
241
242 jtag_interface_t vsllink_interface =
243 {
244 .name = "vsllink",
245 .execute_queue = vsllink_execute_queue,
246 .speed = vsllink_speed,
247 .khz = vsllink_khz,
248 .speed_div = vsllink_speed_div,
249 .register_commands = vsllink_register_commands,
250 .init = vsllink_init,
251 .quit = vsllink_quit
252 };
253
254 int vsllink_execute_queue(void)
255 {
256 jtag_command_t *cmd = jtag_command_queue;
257 int scan_size;
258 enum scan_type type;
259 u8 *buffer;
260
261 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
262
263 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
264 vsllink_usb_out_buffer_idx = 3;
265 while (cmd != NULL)
266 {
267 switch (cmd->type)
268 {
269 case JTAG_END_STATE:
270 DEBUG_JTAG_IO("end_state: %s", jtag_state_name(cmd->cmd.end_state->end_state));
271
272 if (cmd->cmd.end_state->end_state != -1)
273 {
274 vsllink_end_state(cmd->cmd.end_state->end_state);
275 }
276 break;
277
278 case JTAG_RUNTEST:
279 DEBUG_JTAG_IO( "runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, \
280 jtag_state_name(cmd->cmd.runtest->end_state));
281
282 if (cmd->cmd.runtest->end_state != -1)
283 {
284 vsllink_end_state(cmd->cmd.runtest->end_state);
285 }
286 vsllink_runtest(cmd->cmd.runtest->num_cycles);
287 break;
288
289 case JTAG_STATEMOVE:
290 DEBUG_JTAG_IO("statemove end in %s", jtag_state_name(cmd->cmd.statemove->end_state));
291
292 if (cmd->cmd.statemove->end_state != -1)
293 {
294 vsllink_end_state(cmd->cmd.statemove->end_state);
295 }
296 vsllink_state_move();
297 break;
298
299 case JTAG_PATHMOVE:
300 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
301 cmd->cmd.pathmove->num_states, \
302 jtag_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
303
304 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
305 break;
306
307 case JTAG_SCAN:
308 if (cmd->cmd.scan->end_state != -1)
309 {
310 vsllink_end_state(cmd->cmd.scan->end_state);
311 }
312
313 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
314 if (cmd->cmd.scan->ir_scan)
315 {
316 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size, jtag_state_name(cmd->cmd.scan->end_state));
317 }
318 else
319 {
320 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size, jtag_state_name(cmd->cmd.scan->end_state));
321 }
322
323 #ifdef _DEBUG_JTAG_IO_
324 vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
325 #endif
326
327 type = jtag_scan_type(cmd->cmd.scan);
328
329 vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
330 break;
331
332 case JTAG_RESET:
333 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
334
335 vsllink_tap_execute();
336
337 if (cmd->cmd.reset->trst == 1)
338 {
339 cur_state = TAP_RESET;
340 }
341 vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
342
343 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
344 vsllink_usb_out_buffer_idx = 3;
345 break;
346
347 case JTAG_SLEEP:
348 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
349 vsllink_tap_execute();
350 jtag_sleep(cmd->cmd.sleep->us);
351 break;
352
353 case JTAG_STABLECLOCKS:
354 DEBUG_JTAG_IO("add %d clocks", cmd->cmd.stableclocks->num_cycles);
355 switch(cur_state)
356 {
357 case TAP_RESET:
358 // tms should be '1' to stay in TAP_RESET mode
359 scan_size = 1;
360 break;
361 case TAP_DRSHIFT:
362 case TAP_IDLE:
363 case TAP_DRPAUSE:
364 case TAP_IRSHIFT:
365 case TAP_IRPAUSE:
366 // in other mode, tms should be '0'
367 scan_size = 0;
368 break; /* above stable states are OK */
369 default:
370 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
371 jtag_state_name(cur_state) );
372 exit(-1);
373 }
374 vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
375 break;
376
377 default:
378 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
379 exit(-1);
380 }
381 cmd = cmd->next;
382 }
383
384 return vsllink_tap_execute();
385 }
386
387 int vsllink_speed(int speed)
388 {
389 int result;
390
391 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
392 vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
393 vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
394
395 result = vsllink_usb_write(vsllink_jtag_handle, 3);
396
397 if (result == 3)
398 {
399 return ERROR_OK;
400 }
401 else
402 {
403 LOG_ERROR("VSLLink setting speed failed (%d)", result);
404 return ERROR_JTAG_DEVICE_ERROR;
405 }
406
407 return ERROR_OK;
408 }
409
410 int vsllink_khz(int khz, int *jtag_speed)
411 {
412 *jtag_speed = khz;
413
414 return ERROR_OK;
415 }
416
417 int vsllink_speed_div(int jtag_speed, int *khz)
418 {
419 *khz = jtag_speed;
420
421 return ERROR_OK;
422 }
423
424 int vsllink_register_commands(struct command_context_s *cmd_ctx)
425 {
426 register_command(cmd_ctx, NULL, "vsllink_usb_vid", vsllink_handle_usb_vid_command,
427 COMMAND_CONFIG, NULL);
428 register_command(cmd_ctx, NULL, "vsllink_usb_pid", vsllink_handle_usb_pid_command,
429 COMMAND_CONFIG, NULL);
430 register_command(cmd_ctx, NULL, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command,
431 COMMAND_CONFIG, NULL);
432 register_command(cmd_ctx, NULL, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command,
433 COMMAND_CONFIG, NULL);
434
435 return ERROR_OK;
436 }
437
438 int vsllink_init(void)
439 {
440 int check_cnt;
441 int result;
442 char version_str[100];
443
444 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
445 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
446 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
447 {
448 LOG_ERROR("Not enough memory");
449 exit(-1);
450 }
451
452 vsllink_jtag_handle = vsllink_usb_open();
453
454 if (vsllink_jtag_handle == 0)
455 {
456 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
457 return ERROR_JTAG_INIT_FAILED;
458 }
459
460 check_cnt = 0;
461 while (check_cnt < 3)
462 {
463 vsllink_simple_command(VSLLINK_CMD_CONN);
464 result = vsllink_usb_read(vsllink_jtag_handle);
465
466 if (result > 2)
467 {
468 vsllink_usb_in_buffer[result] = 0;
469 VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
470 strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
471 LOG_INFO(version_str);
472
473 // free the pre-alloc memroy
474 free(vsllink_usb_in_buffer);
475 free(vsllink_usb_out_buffer);
476 vsllink_usb_in_buffer = NULL;
477 vsllink_usb_out_buffer = NULL;
478
479 // alloc new memory
480 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
481 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
482 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
483 {
484 LOG_ERROR("Not enough memory");
485 exit(-1);
486 }
487 else
488 {
489 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
490 }
491 break;
492 }
493 vsllink_simple_command(VSLLINK_CMD_DISCONN);
494
495 check_cnt++;
496 }
497
498 if (check_cnt == 3)
499 {
500 // It's dangerout to proced
501 LOG_ERROR("VSLLink initial failed");
502 exit(-1);
503 }
504
505 // Set SRST and TRST to output, Set USR1 and USR2 to input
506 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
507 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
508 vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
509 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
510 {
511 LOG_ERROR("VSLLink USB send data error");
512 exit(-1);
513 }
514
515 vsllink_reset(0, 0);
516
517 LOG_INFO("VSLLink JTAG Interface ready");
518
519 vsllink_tap_init();
520
521 return ERROR_OK;
522 }
523
524 int vsllink_quit(void)
525 {
526 if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
527 {
528 // Set all pins to input
529 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
530 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
531 vsllink_usb_out_buffer[2] = 0;
532 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
533 {
534 LOG_ERROR("VSLLink USB send data error");
535 exit(-1);
536 }
537
538 // disconnect
539 vsllink_simple_command(VSLLINK_CMD_DISCONN);
540 vsllink_usb_close(vsllink_jtag_handle);
541 }
542
543 if (vsllink_usb_in_buffer != NULL)
544 {
545 free(vsllink_usb_in_buffer);
546 }
547 if (vsllink_usb_out_buffer != NULL)
548 {
549 free(vsllink_usb_out_buffer);
550 }
551 return ERROR_OK;
552 }
553
554 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
555 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
556 void vsllink_append_tms(void)
557 {
558 u8 tms_scan = VSLLINK_TAP_MOVE(cur_state, end_state);
559 u16 tms2;
560
561 if (((cur_state != TAP_RESET) && (cur_state != TAP_IDLE) && (cur_state != TAP_DRPAUSE) && (cur_state != TAP_IRPAUSE)) || \
562 (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || \
563 (vsllink_tms_cmd_pos == NULL))
564 {
565 LOG_ERROR("There MUST be some bugs in the driver");
566 exit(-1);
567 }
568
569 tms2 = (tms_scan & VSLLINK_BIT_MSK[VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_position]) << \
570 vsllink_tms_data_len;
571 if (VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_value == 1)
572 {
573 tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << \
574 (vsllink_tms_data_len + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_position);
575 }
576 tms2 |= (tms_scan >> VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_position) << \
577 (8 + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_map[cur_state]][tap_move_map[end_state]].insert_position);
578
579 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
580 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
581
582 vsllink_tms_data_len = 0;
583 vsllink_tms_cmd_pos = NULL;
584 }
585
586 /***************************************************************************/
587 /* Queue command implementations */
588
589 void vsllink_end_state(enum tap_state state)
590 {
591 if (tap_move_map[state] != -1)
592 {
593 end_state = state;
594 }
595 else
596 {
597 LOG_ERROR("BUG: %i is not a valid end state", state);
598 exit(-1);
599 }
600 }
601
602 /* Goes to the end state. */
603 void vsllink_state_move(void)
604 {
605 if (vsllink_tms_data_len > 0)
606 {
607 vsllink_append_tms();
608 }
609 else
610 {
611 vsllink_tap_ensure_space(0, 2);
612
613 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
614 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
615 }
616
617 cur_state = end_state;
618 }
619
620 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
621 void vsllink_add_path(int start, int num, enum tap_state *path)
622 {
623 int i;
624
625 for (i = start; i < (start + num); i++)
626 {
627 if ((i & 7) == 0)
628 {
629 if (i > 0)
630 {
631 vsllink_usb_out_buffer_idx++;
632 }
633 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
634 }
635
636 if (path[i - start] == tap_transitions[cur_state].high)
637 {
638 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
639 }
640 else if (path[i - start] == tap_transitions[cur_state].low)
641 {
642 // nothing to do
643 }
644 else
645 {
646 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", jtag_state_name(cur_state), jtag_state_name(path[i]));
647 exit(-1);
648 }
649 cur_state = path[i - start];
650 }
651 if ((i > 0) && ((i & 7) == 0))
652 {
653 vsllink_usb_out_buffer_idx++;
654 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
655 }
656
657 end_state = cur_state;
658 }
659
660 void vsllink_path_move(int num_states, enum tap_state *path)
661 {
662 int i, tms_len, tms_cmd_pos, path_idx = 0;
663
664 if (vsllink_tms_data_len > 0)
665 {
666 // there are vsllink_tms_data_len more tms bits to be shifted
667 // so there are vsllink_tms_data_len + num_states tms bits in all
668 tms_len = vsllink_tms_data_len + num_states;
669 if (tms_len <= 16)
670 {
671 // merge into last tms shift
672 if (tms_len < 8)
673 {
674 // just append tms data to the last tms byte
675 vsllink_add_path(vsllink_tms_data_len, num_states, path);
676 }
677 else if (tms_len == 8)
678 {
679 // end last tms shift command
680 (*vsllink_tms_cmd_pos)--;
681 vsllink_add_path(vsllink_tms_data_len, num_states, path);
682 }
683 else if (tms_len < 16)
684 {
685 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
686 {
687 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
688 // there is enought tms length in the current tms shift command
689 (*vsllink_tms_cmd_pos)++;
690 vsllink_add_path(vsllink_tms_data_len, num_states, path);
691 }
692 else
693 {
694 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
695 // not enough tms length in the current tms shift command
696 // so a new command should be added
697 // first decrease byte length of last tms shift command
698 (*vsllink_tms_cmd_pos)--;
699 // append tms data to the last tms byte
700 vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
701 path += 8 - vsllink_tms_data_len;
702 // add new command(3 bytes)
703 vsllink_tap_ensure_space(0, 3);
704 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
705 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
706 vsllink_add_path(0, num_states - (8 - vsllink_tms_data_len), path);
707 }
708 }
709 else if (tms_len == 16)
710 {
711 // end last tms shift command
712 vsllink_add_path(vsllink_tms_data_len, num_states, path);
713 }
714
715 vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
716 if (vsllink_tms_data_len == 0)
717 {
718 vsllink_tms_cmd_pos = NULL;
719 }
720 num_states = 0;
721 }
722 else
723 {
724 vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
725
726 path += 16 - vsllink_tms_data_len;
727 num_states -= 16 - vsllink_tms_data_len;
728 vsllink_tms_data_len = 0;
729 vsllink_tms_cmd_pos = NULL;
730 }
731 }
732
733 if (num_states > 0)
734 {
735 // Normal operation, don't need to append tms data
736 vsllink_tms_data_len = num_states & 7;
737
738 while (num_states > 0)
739 {
740 if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
741 {
742 i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
743 }
744 else
745 {
746 i = num_states;
747 }
748 tms_len = (i + 7) >> 3;
749 vsllink_tap_ensure_space(0, tms_len + 2);
750 tms_cmd_pos = vsllink_usb_out_buffer_idx;
751 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
752
753 vsllink_add_path(0, i, path + path_idx);
754
755 path_idx += i;
756 num_states -= i;
757 }
758
759 if (vsllink_tms_data_len > 0)
760 {
761 if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
762 {
763 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[tms_cmd_pos];
764 (*vsllink_tms_cmd_pos)++;
765 }
766 else
767 {
768 vsllink_usb_out_buffer[tms_cmd_pos]--;
769
770 tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
771 vsllink_tap_ensure_space(0, 3);
772 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
773 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
774 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
775 }
776 }
777 }
778 }
779
780 void vsllink_stableclocks(int num_cycles, int tms)
781 {
782 int tms_len;
783 u16 tms_append_byte;
784
785 if (vsllink_tms_data_len > 0)
786 {
787 // there are vsllink_tms_data_len more tms bits to be shifted
788 // so there are vsllink_tms_data_len + num_cycles tms bits in all
789 tms_len = vsllink_tms_data_len + num_cycles;
790 if (tms > 0)
791 {
792 // append '1' for tms
793 tms_append_byte = (u16)((((1 << num_cycles) - 1) << vsllink_tms_data_len) & 0xFFFF);
794 }
795 else
796 {
797 // append '0' for tms
798 tms_append_byte = 0;
799 }
800 if (tms_len <= 16)
801 {
802 // merge into last tms shift
803 if (tms_len < 8)
804 {
805 // just add to vsllink_tms_data_len
806 // same result if tun through
807 //vsllink_tms_data_len += num_cycles;
808 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= (u8)(tms_append_byte & 0xFF);
809 }
810 else if (tms_len == 8)
811 {
812 // end last tms shift command
813 // just reduce it, and append last tms byte
814 (*vsllink_tms_cmd_pos)--;
815 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
816 }
817 else if (tms_len < 16)
818 {
819 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
820 {
821 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
822 // there is enought tms length in the current tms shift command
823 // increase the tms byte length by 1 and set the last byte to 0
824 (*vsllink_tms_cmd_pos)++;
825 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
826 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (u8)(tms_append_byte >> 8);
827 }
828 else
829 {
830 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
831 // not enough tms length in the current tms shift command
832 // so a new command should be added
833 // first decrease byte length of last tms shift command
834 (*vsllink_tms_cmd_pos)--;
835 // append last tms byte and move the command pointer to the next empty position
836 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
837 // add new command(3 bytes)
838 vsllink_tap_ensure_space(0, 3);
839 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
840 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
841 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (u8)(tms_append_byte >> 8);
842 }
843 }
844 else if (tms_len == 16)
845 {
846 // end last tms shift command
847 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
848 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (u8)(tms_append_byte >> 8);
849 }
850
851 vsllink_tms_data_len = tms_len & 7;
852 if (vsllink_tms_data_len == 0)
853 {
854 vsllink_tms_cmd_pos = NULL;
855 }
856 num_cycles = 0;
857 }
858 else
859 {
860 // more shifts will be needed
861 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
862 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (u8)(tms_append_byte >> 8);
863
864 num_cycles -= 16 - vsllink_tms_data_len;
865 vsllink_tms_data_len = 0;
866 vsllink_tms_cmd_pos = NULL;
867 }
868 }
869 // from here vsllink_tms_data_len == 0 or num_cycles == 0
870
871 if (vsllink_tms_data_len > 0)
872 {
873 // num_cycles == 0
874 // no need to shift
875 if (num_cycles > 0)
876 {
877 LOG_ERROR("There MUST be some bugs in the driver");
878 exit(-1);
879 }
880 }
881 else
882 {
883 // get number of bytes left to be sent
884 tms_len = num_cycles >> 3;
885 if (tms_len > 0)
886 {
887 vsllink_tap_ensure_space(1, 5);
888 // if tms_len > 0, vsllink_tms_data_len == 0
889 // so just add new command
890 // LSB of the command byte is the tms value when do the shifting
891 if (tms > 0)
892 {
893 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK | 1;
894 }
895 else
896 {
897 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK;
898 }
899 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 0) & 0xff;
900 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 8) & 0xff;
901 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 16) & 0xff;
902 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 24) & 0xff;
903
904 vsllink_usb_in_want_length += 1;
905 pending_scan_results_buffer[pending_scan_results_length].buffer = NULL;
906 pending_scan_results_length++;
907
908 if (tms_len > 0xFFFF)
909 {
910 vsllink_tap_execute();
911 }
912 }
913
914 // post-process
915 vsllink_tms_data_len = num_cycles & 7;
916 if (vsllink_tms_data_len > 0)
917 {
918 vsllink_tap_ensure_space(0, 3);
919 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
920 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
921 if (tms > 0)
922 {
923 // append '1' for tms
924 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (1 << vsllink_tms_data_len) - 1;
925 }
926 else
927 {
928 // append '0' for tms
929 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0x00;
930 }
931 }
932 }
933 }
934
935 void vsllink_runtest(int num_cycles)
936 {
937 enum tap_state saved_end_state = end_state;
938
939 if (cur_state != TAP_IDLE)
940 {
941 // enter into IDLE state
942 vsllink_end_state(TAP_IDLE);
943 vsllink_state_move();
944 }
945
946 vsllink_stableclocks(num_cycles, 0);
947
948 // post-process
949 // set end_state
950 vsllink_end_state(saved_end_state);
951 cur_state = TAP_IDLE;
952 if (end_state != TAP_IDLE)
953 {
954 vsllink_state_move();
955 }
956 }
957
958 void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
959 {
960 enum tap_state saved_end_state;
961 u8 bits_left, tms_tmp, tdi_len;
962 int i;
963
964 if (0 == scan_size )
965 {
966 return;
967 }
968
969 tdi_len = ((scan_size + 7) >> 3);
970 if ((tdi_len + 7) > VSLLINK_BufferSize)
971 {
972 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
973 exit(-1);
974 }
975
976 saved_end_state = end_state;
977
978 /* Move to appropriate scan state */
979 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
980
981 if (vsllink_tms_data_len > 0)
982 {
983 if (cur_state == end_state)
984 {
985 // already in IRSHIFT or DRSHIFT state
986 // merge tms data in the last tms shift command into next scan command
987 if(*vsllink_tms_cmd_pos < 1)
988 {
989 LOG_ERROR("There MUST be some bugs in the driver");
990 exit(-1);
991 }
992 else if(*vsllink_tms_cmd_pos < 2)
993 {
994 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
995 vsllink_usb_out_buffer_idx--;
996 }
997 else
998 {
999 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1000 *vsllink_tms_cmd_pos -= 2;
1001 }
1002
1003 vsllink_tap_ensure_space(1, tdi_len + 7);
1004 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1005 // which is merged from the last tms shift command
1006 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1007 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1008 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
1009 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
1010 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
1011
1012 for (i = 0; i < tdi_len; i++)
1013 {
1014 buffer[i] >>= 8 - vsllink_tms_data_len;
1015 if (i != tdi_len)
1016 {
1017 buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
1018 }
1019 }
1020
1021 vsllink_tap_append_scan(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
1022 scan_size -= 8 - vsllink_tms_data_len;
1023 vsllink_tms_data_len = 0;
1024 }
1025 else
1026 {
1027 vsllink_append_tms();
1028 vsllink_tap_ensure_space(1, tdi_len + 5);
1029
1030 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
1031 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
1032 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
1033
1034 vsllink_tap_append_scan(scan_size, buffer, command, 0);
1035 }
1036 }
1037 else
1038 {
1039 vsllink_tap_ensure_space(1, tdi_len + 7);
1040
1041 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1042 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1043 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
1044 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
1045 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1046
1047 vsllink_tap_append_scan(scan_size, buffer, command, 8);
1048 }
1049 vsllink_end_state(saved_end_state);
1050
1051 bits_left = scan_size & 0x07;
1052 cur_state = ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE;
1053
1054 if (bits_left > 0)
1055 {
1056 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
1057 }
1058 else
1059 {
1060 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
1061 }
1062
1063 if (cur_state != end_state)
1064 {
1065 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
1066 }
1067 else
1068 {
1069 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1070 }
1071
1072 cur_state = end_state;
1073 }
1074
1075 void vsllink_reset(int trst, int srst)
1076 {
1077 int result;
1078
1079 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
1080
1081 /* Signals are active low */
1082 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
1083 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
1084 vsllink_usb_out_buffer[2] = 0;
1085 if (srst == 0)
1086 {
1087 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
1088 }
1089 if (trst == 0)
1090 {
1091 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
1092 }
1093
1094 result = vsllink_usb_write(vsllink_jtag_handle, 3);
1095 if (result != 3)
1096 {
1097 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
1098 }
1099 }
1100
1101 void vsllink_simple_command(u8 command)
1102 {
1103 int result;
1104
1105 DEBUG_JTAG_IO("0x%02x", command);
1106
1107 vsllink_usb_out_buffer[0] = command;
1108 result = vsllink_usb_write(vsllink_jtag_handle, 1);
1109
1110 if (result != 1)
1111 {
1112 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
1113 }
1114 }
1115
1116 int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1117 {
1118 if (argc != 1) {
1119 LOG_ERROR("parameter error, should be one parameter for VID");
1120 return ERROR_OK;
1121 }
1122
1123 vsllink_vid = strtol(args[0], NULL, 0);
1124
1125 return ERROR_OK;
1126 }
1127
1128 int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1129 {
1130 if (argc != 1) {
1131 LOG_ERROR("parameter error, should be one parameter for PID");
1132 return ERROR_OK;
1133 }
1134
1135 vsllink_pid = strtol(args[0], NULL, 0);
1136
1137 return ERROR_OK;
1138 }
1139
1140 int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1141 {
1142 if (argc != 1) {
1143 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1144 return ERROR_OK;
1145 }
1146
1147 vsllink_bulkin = strtol(args[0], NULL, 0) | 0x80;
1148
1149 return ERROR_OK;
1150 }
1151
1152 int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1153 {
1154 if (argc != 1) {
1155 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1156 return ERROR_OK;
1157 }
1158
1159 vsllink_bulkout = strtol(args[0], NULL, 0);
1160
1161 return ERROR_OK;
1162 }
1163
1164 /***************************************************************************/
1165 /* VSLLink tap functions */
1166
1167 void vsllink_tap_init(void)
1168 {
1169 vsllink_usb_out_buffer_idx = 0;
1170 vsllink_usb_in_want_length = 0;
1171 pending_scan_results_length = 0;
1172 }
1173
1174 void vsllink_tap_ensure_space(int scans, int bytes)
1175 {
1176 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1177 int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
1178
1179 if (scans > available_scans || bytes > available_bytes)
1180 {
1181 vsllink_tap_execute();
1182 }
1183 }
1184
1185 void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset)
1186 {
1187 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1188 int i;
1189
1190 if (offset > 0)
1191 {
1192 vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
1193 }
1194 else
1195 {
1196 vsllink_usb_in_want_length += (length + 7) >> 3;
1197 }
1198 pending_scan_result->length = length;
1199 pending_scan_result->offset = offset;
1200 pending_scan_result->command = command;
1201 pending_scan_result->buffer = buffer;
1202
1203 for (i = 0; i < ((length + 7) >> 3); i++)
1204 {
1205 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
1206 }
1207
1208 pending_scan_results_length++;
1209 }
1210
1211 /* Pad and send a tap sequence to the device, and receive the answer.
1212 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1213 int vsllink_tap_execute(void)
1214 {
1215 int i;
1216 int result;
1217 int first = 0;
1218
1219 if (vsllink_tms_data_len > 0)
1220 {
1221 if((cur_state != TAP_RESET) && (cur_state != TAP_IDLE) && (cur_state != TAP_IRPAUSE) && (cur_state != TAP_DRPAUSE))
1222 {
1223 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", jtag_state_name(cur_state));
1224 }
1225
1226 if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
1227 {
1228 // last tms bit is '1'
1229 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= 0xFF << vsllink_tms_data_len;
1230 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0xFF;
1231 vsllink_tms_data_len = 0;
1232 }
1233 else
1234 {
1235 // last tms bit is '0'
1236 vsllink_usb_out_buffer_idx++;
1237 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1238 vsllink_tms_data_len = 0;
1239 }
1240 }
1241
1242 if (vsllink_usb_out_buffer_idx > 3)
1243 {
1244 if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
1245 {
1246 vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
1247 vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
1248 }
1249
1250 result = vsllink_usb_message(vsllink_jtag_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
1251
1252 if (result == vsllink_usb_in_want_length)
1253 {
1254 for (i = 0; i < pending_scan_results_length; i++)
1255 {
1256 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1257 u8 *buffer = pending_scan_result->buffer;
1258 int length = pending_scan_result->length;
1259 int offset = pending_scan_result->offset;
1260 scan_command_t *command = pending_scan_result->command;
1261
1262 if (buffer != NULL)
1263 {
1264 // IRSHIFT or DRSHIFT
1265 buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
1266 first += (length + offset + 7) >> 3;
1267
1268 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
1269 #ifdef _DEBUG_JTAG_IO_
1270 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1271 #endif
1272
1273 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1274 {
1275 vsllink_tap_init();
1276 return ERROR_JTAG_QUEUE_FAILED;
1277 }
1278
1279 free(pending_scan_result->buffer);
1280 pending_scan_result->buffer = NULL;
1281 }
1282 else
1283 {
1284 first++;
1285 }
1286 }
1287 }
1288 else
1289 {
1290 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
1291 return ERROR_JTAG_QUEUE_FAILED;
1292 }
1293
1294 vsllink_tap_init();
1295 }
1296
1297 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
1298 vsllink_usb_out_buffer_idx = 3;
1299
1300 return ERROR_OK;
1301 }
1302
1303 /*****************************************************************************/
1304 /* VSLLink USB low-level functions */
1305
1306 vsllink_jtag_t* vsllink_usb_open(void)
1307 {
1308 struct usb_bus *busses;
1309 struct usb_bus *bus;
1310 struct usb_device *dev;
1311
1312 vsllink_jtag_t *result;
1313
1314 result = (vsllink_jtag_t*) malloc(sizeof(vsllink_jtag_t));
1315
1316 usb_init();
1317 usb_find_busses();
1318 usb_find_devices();
1319
1320 busses = usb_get_busses();
1321
1322 /* find vsllink_jtag device in usb bus */
1323
1324 for (bus = busses; bus; bus = bus->next)
1325 {
1326 for (dev = bus->devices; dev; dev = dev->next)
1327 {
1328 if ((dev->descriptor.idVendor == vsllink_vid) && (dev->descriptor.idProduct == vsllink_pid))
1329 {
1330 result->usb_handle = usb_open(dev);
1331
1332 /* usb_set_configuration required under win32 */
1333 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
1334 usb_claim_interface(result->usb_handle, 0);
1335
1336 #if 0
1337 /*
1338 * This makes problems under Mac OS X. And is not needed
1339 * under Windows. Hopefully this will not break a linux build
1340 */
1341 usb_set_altinterface(result->usb_handle, 0);
1342 #endif
1343 return result;
1344 }
1345 }
1346 }
1347
1348 free(result);
1349 return NULL;
1350 }
1351
1352 void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag)
1353 {
1354 usb_close(vsllink_jtag->usb_handle);
1355 free(vsllink_jtag);
1356 }
1357
1358 /* Send a message and receive the reply. */
1359 int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length)
1360 {
1361 int result;
1362
1363 result = vsllink_usb_write(vsllink_jtag, out_length);
1364 if (result == out_length)
1365 {
1366 if (in_length > 0)
1367 {
1368 result = vsllink_usb_read(vsllink_jtag);
1369 if (result == in_length )
1370 {
1371 return result;
1372 }
1373 else
1374 {
1375 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
1376 return -1;
1377 }
1378 }
1379 return 0;
1380 }
1381 else
1382 {
1383 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
1384 return -1;
1385 }
1386 }
1387
1388 /* Write data from out_buffer to USB. */
1389 int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length)
1390 {
1391 int result;
1392
1393 if (out_length > VSLLINK_BufferSize)
1394 {
1395 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
1396 return -1;
1397 }
1398
1399 result = usb_bulk_write(vsllink_jtag->usb_handle, vsllink_bulkout, \
1400 (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
1401
1402 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
1403
1404 #ifdef _DEBUG_USB_COMMS_
1405 LOG_DEBUG("USB out:");
1406 vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
1407 #endif
1408
1409 #ifdef _VSLLINK_IN_DEBUG_MODE_
1410 usleep(100000);
1411 #endif
1412
1413 return result;
1414 }
1415
1416 /* Read data from USB into in_buffer. */
1417 int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag)
1418 {
1419 int result = usb_bulk_read(vsllink_jtag->usb_handle, vsllink_bulkin, \
1420 (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
1421
1422 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
1423
1424 #ifdef _DEBUG_USB_COMMS_
1425 LOG_DEBUG("USB in:");
1426 vsllink_debug_buffer(vsllink_usb_in_buffer, result);
1427 #endif
1428 return result;
1429 }
1430
1431 #define BYTES_PER_LINE 16
1432
1433 void vsllink_debug_buffer(u8 *buffer, int length)
1434 {
1435 char line[81];
1436 char s[4];
1437 int i;
1438 int j;
1439
1440 for (i = 0; i < length; i += BYTES_PER_LINE)
1441 {
1442 snprintf(line, 5, "%04x", i);
1443 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1444 {
1445 snprintf(s, 4, " %02x", buffer[j]);
1446 strcat(line, s);
1447 }
1448 LOG_DEBUG(line);
1449 }
1450 }

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)