1 /***************************************************************************
2 * Copyright (C) 2009 by Simon Qian <SimonQian@SimonQian.com> *
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. *
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. *
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 ***************************************************************************/
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.
29 #include "replacements.h"
38 //#define _VSLLINK_IN_DEBUG_MODE_
40 /* enable this to view USB communication
43 #define _DEBUG_USB_COMMS_
46 #ifdef _DEBUG_JTAG_IO_
47 #define DEBUG_JTAG_IO(expr ...) LOG_DEBUG(expr)
49 #define DEBUG_JTAG_IO(expr ...)
57 #define VSLLINK_USB_TIMEOUT 10000
59 static int VSLLINK_BufferSize
= 1024;
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
;
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
78 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
79 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
80 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
82 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
83 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
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)
95 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_map[from]][tap_move_map[to]]
97 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
105 * SD->SD and SI->SI have to be caught in interface specific code
107 u8 VSLLINK_tap_move
[6][6] =
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 */
118 typedef struct insert_insignificant_operation
120 unsigned char insert_value
;
121 unsigned char insert_position
;
122 }insert_insignificant_operation_t
;
124 insert_insignificant_operation_t VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT
[6][6] =
171 u8 VSLLINK_BIT_MSK
[8] =
173 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
178 int length
; /* Number of bits to read */
180 scan_command_t
*command
; /* Corresponding scan command */
182 } pending_scan_result_t
;
184 #define MAX_PENDING_SCAN_RESULTS 256
186 static int pending_scan_results_length
;
187 static pending_scan_result_t pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
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);
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
);
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
);
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
);
220 /* VSLLink lowlevel functions */
221 typedef struct vsllink_jtag
223 struct usb_dev_handle
* usb_handle
;
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
);
232 void vsllink_debug_buffer(u8
*buffer
, int length
);
234 static int vsllink_tms_data_len
= 0;
235 static u8
* vsllink_tms_cmd_pos
;
237 vsllink_jtag_t
* vsllink_jtag_handle
;
239 /***************************************************************************/
240 /* External interface implementation */
242 jtag_interface_t vsllink_interface
=
245 .execute_queue
= vsllink_execute_queue
,
246 .speed
= vsllink_speed
,
248 .speed_div
= vsllink_speed_div
,
249 .register_commands
= vsllink_register_commands
,
250 .init
= vsllink_init
,
254 int vsllink_execute_queue(void)
256 jtag_command_t
*cmd
= jtag_command_queue
;
261 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
263 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGSEQCMD
;
264 vsllink_usb_out_buffer_idx
= 3;
270 DEBUG_JTAG_IO("end_state: %s", jtag_state_name(cmd
->cmd
.end_state
->end_state
));
272 if (cmd
->cmd
.end_state
->end_state
!= -1)
274 vsllink_end_state(cmd
->cmd
.end_state
->end_state
);
279 DEBUG_JTAG_IO( "runtest %i cycles, end in %s", cmd
->cmd
.runtest
->num_cycles
, \
280 jtag_state_name(cmd
->cmd
.runtest
->end_state
));
282 if (cmd
->cmd
.runtest
->end_state
!= -1)
284 vsllink_end_state(cmd
->cmd
.runtest
->end_state
);
286 vsllink_runtest(cmd
->cmd
.runtest
->num_cycles
);
290 DEBUG_JTAG_IO("statemove end in %s", jtag_state_name(cmd
->cmd
.statemove
->end_state
));
292 if (cmd
->cmd
.statemove
->end_state
!= -1)
294 vsllink_end_state(cmd
->cmd
.statemove
->end_state
);
296 vsllink_state_move();
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]));
304 vsllink_path_move(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
308 if (cmd
->cmd
.scan
->end_state
!= -1)
310 vsllink_end_state(cmd
->cmd
.scan
->end_state
);
313 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
314 if (cmd
->cmd
.scan
->ir_scan
)
316 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size
, jtag_state_name(cmd
->cmd
.scan
->end_state
));
320 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size
, jtag_state_name(cmd
->cmd
.scan
->end_state
));
323 #ifdef _DEBUG_JTAG_IO_
324 vsllink_debug_buffer(buffer
, (scan_size
+ 7) >> 3);
327 type
= jtag_scan_type(cmd
->cmd
.scan
);
329 vsllink_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
, cmd
->cmd
.scan
);
333 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
335 vsllink_tap_execute();
337 if (cmd
->cmd
.reset
->trst
== 1)
339 cur_state
= TAP_RESET
;
341 vsllink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
343 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGSEQCMD
;
344 vsllink_usb_out_buffer_idx
= 3;
348 DEBUG_JTAG_IO("sleep %i", cmd
->cmd
.sleep
->us
);
349 vsllink_tap_execute();
350 jtag_sleep(cmd
->cmd
.sleep
->us
);
353 case JTAG_STABLECLOCKS
:
354 DEBUG_JTAG_IO("add %d clocks", cmd
->cmd
.stableclocks
->num_cycles
);
358 // tms should be '1' to stay in TAP_RESET mode
366 // in other mode, tms should be '0'
368 break; /* above stable states are OK */
370 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
371 jtag_state_name(cur_state
) );
374 vsllink_stableclocks(cmd
->cmd
.stableclocks
->num_cycles
, scan_size
);
378 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd
->type
);
384 return vsllink_tap_execute();
387 int vsllink_speed(int speed
)
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;
395 result
= vsllink_usb_write(vsllink_jtag_handle
, 3);
403 LOG_ERROR("VSLLink setting speed failed (%d)", result
);
404 return ERROR_JTAG_DEVICE_ERROR
;
410 int vsllink_khz(int khz
, int *jtag_speed
)
417 int vsllink_speed_div(int jtag_speed
, int *khz
)
424 int vsllink_register_commands(struct command_context_s
*cmd_ctx
)
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
);
438 int vsllink_init(void)
442 char version_str
[100];
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
))
448 LOG_ERROR("Not enough memory");
452 vsllink_jtag_handle
= vsllink_usb_open();
454 if (vsllink_jtag_handle
== 0)
456 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
457 return ERROR_JTAG_INIT_FAILED
;
461 while (check_cnt
< 3)
463 vsllink_simple_command(VSLLINK_CMD_CONN
);
464 result
= vsllink_usb_read(vsllink_jtag_handle
);
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
);
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
;
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
))
484 LOG_ERROR("Not enough memory");
489 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize
);
493 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
500 // It's dangerout to proced
501 LOG_ERROR("VSLLink initial failed");
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)
511 LOG_ERROR("VSLLink USB send data error");
517 LOG_INFO("VSLLink JTAG Interface ready");
524 int vsllink_quit(void)
526 if ((vsllink_usb_in_buffer
!= NULL
) && (vsllink_usb_out_buffer
!= NULL
))
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)
534 LOG_ERROR("VSLLink USB send data error");
539 vsllink_simple_command(VSLLINK_CMD_DISCONN
);
540 vsllink_usb_close(vsllink_jtag_handle
);
543 if (vsllink_usb_in_buffer
!= NULL
)
545 free(vsllink_usb_in_buffer
);
547 if (vsllink_usb_out_buffer
!= NULL
)
549 free(vsllink_usb_out_buffer
);
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)
558 u8 tms_scan
= VSLLINK_TAP_MOVE(cur_state
, end_state
);
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
))
565 LOG_ERROR("There MUST be some bugs in the driver");
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)
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
);
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
);
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;
582 vsllink_tms_data_len
= 0;
583 vsllink_tms_cmd_pos
= NULL
;
586 /***************************************************************************/
587 /* Queue command implementations */
589 void vsllink_end_state(enum tap_state state
)
591 if (tap_move_map
[state
] != -1)
597 LOG_ERROR("BUG: %i is not a valid end state", state
);
602 /* Goes to the end state. */
603 void vsllink_state_move(void)
605 if (vsllink_tms_data_len
> 0)
607 vsllink_append_tms();
611 vsllink_tap_ensure_space(0, 2);
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
);
617 cur_state
= end_state
;
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
)
625 for (i
= start
; i
< (start
+ num
); i
++)
631 vsllink_usb_out_buffer_idx
++;
633 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0;
636 if (path
[i
- start
] == tap_transitions
[cur_state
].high
)
638 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] |= 1 << (i
& 7);
640 else if (path
[i
- start
] == tap_transitions
[cur_state
].low
)
646 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", jtag_state_name(cur_state
), jtag_state_name(path
[i
]));
649 cur_state
= path
[i
- start
];
651 if ((i
> 0) && ((i
& 7) == 0))
653 vsllink_usb_out_buffer_idx
++;
654 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0;
657 end_state
= cur_state
;
660 void vsllink_path_move(int num_states
, enum tap_state
*path
)
662 int i
, tms_len
, tms_cmd_pos
, path_idx
= 0;
664 if (vsllink_tms_data_len
> 0)
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
;
671 // merge into last tms shift
674 // just append tms data to the last tms byte
675 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
677 else if (tms_len
== 8)
679 // end last tms shift command
680 (*vsllink_tms_cmd_pos
)--;
681 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
683 else if (tms_len
< 16)
685 if ((*vsllink_tms_cmd_pos
& VSLLINK_CMDJTAGSEQ_LENMSK
) < VSLLINK_CMDJTAGSEQ_LENMSK
)
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
);
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
);
709 else if (tms_len
== 16)
711 // end last tms shift command
712 vsllink_add_path(vsllink_tms_data_len
, num_states
, path
);
715 vsllink_tms_data_len
= (vsllink_tms_data_len
+ num_states
) & 7;
716 if (vsllink_tms_data_len
== 0)
718 vsllink_tms_cmd_pos
= NULL
;
724 vsllink_add_path(vsllink_tms_data_len
, 16 - vsllink_tms_data_len
, path
);
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
;
735 // Normal operation, don't need to append tms data
736 vsllink_tms_data_len
= num_states
& 7;
738 while (num_states
> 0)
740 if (num_states
> ((VSLLINK_CMDJTAGSEQ_LENMSK
+ 1) * 8))
742 i
= (VSLLINK_CMDJTAGSEQ_LENMSK
+ 1) * 8;
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);
753 vsllink_add_path(0, i
, path
+ path_idx
);
759 if (vsllink_tms_data_len
> 0)
761 if (tms_len
< (VSLLINK_CMDJTAGSEQ_LENMSK
+ 1))
763 vsllink_tms_cmd_pos
= &vsllink_usb_out_buffer
[tms_cmd_pos
];
764 (*vsllink_tms_cmd_pos
)++;
768 vsllink_usb_out_buffer
[tms_cmd_pos
]--;
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
;
780 void vsllink_stableclocks(int num_cycles
, int tms
)
785 if (vsllink_tms_data_len
> 0)
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
;
792 // append '1' for tms
793 tms_append_byte
= (u16
)((((1 << num_cycles
) - 1) << vsllink_tms_data_len
) & 0xFFFF);
797 // append '0' for tms
802 // merge into last tms shift
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);
810 else if (tms_len
== 8)
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);
817 else if (tms_len
< 16)
819 if ((*vsllink_tms_cmd_pos
& VSLLINK_CMDJTAGSEQ_LENMSK
) < VSLLINK_CMDJTAGSEQ_LENMSK
)
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);
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);
844 else if (tms_len
== 16)
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);
851 vsllink_tms_data_len
= tms_len
& 7;
852 if (vsllink_tms_data_len
== 0)
854 vsllink_tms_cmd_pos
= NULL
;
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);
864 num_cycles
-= 16 - vsllink_tms_data_len
;
865 vsllink_tms_data_len
= 0;
866 vsllink_tms_cmd_pos
= NULL
;
869 // from here vsllink_tms_data_len == 0 or num_cycles == 0
871 if (vsllink_tms_data_len
> 0)
877 LOG_ERROR("There MUST be some bugs in the driver");
883 // get number of bytes left to be sent
884 tms_len
= num_cycles
>> 3;
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
893 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK
| 1;
897 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK
;
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;
904 vsllink_usb_in_want_length
+= 1;
905 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= NULL
;
906 pending_scan_results_length
++;
908 if (tms_len
> 0xFFFF)
910 vsllink_tap_execute();
915 vsllink_tms_data_len
= num_cycles
& 7;
916 if (vsllink_tms_data_len
> 0)
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;
923 // append '1' for tms
924 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = (1 << vsllink_tms_data_len
) - 1;
928 // append '0' for tms
929 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] = 0x00;
935 void vsllink_runtest(int num_cycles
)
937 enum tap_state saved_end_state
= end_state
;
939 if (cur_state
!= TAP_IDLE
)
941 // enter into IDLE state
942 vsllink_end_state(TAP_IDLE
);
943 vsllink_state_move();
946 vsllink_stableclocks(num_cycles
, 0);
950 vsllink_end_state(saved_end_state
);
951 cur_state
= TAP_IDLE
;
952 if (end_state
!= TAP_IDLE
)
954 vsllink_state_move();
958 void vsllink_scan(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
, scan_command_t
*command
)
960 enum tap_state saved_end_state
;
961 u8 bits_left
, tms_tmp
, tdi_len
;
969 tdi_len
= ((scan_size
+ 7) >> 3);
970 if ((tdi_len
+ 7) > VSLLINK_BufferSize
)
972 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
976 saved_end_state
= end_state
;
978 /* Move to appropriate scan state */
979 vsllink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
981 if (vsllink_tms_data_len
> 0)
983 if (cur_state
== end_state
)
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)
989 LOG_ERROR("There MUST be some bugs in the driver");
992 else if(*vsllink_tms_cmd_pos
< 2)
994 tms_tmp
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
995 vsllink_usb_out_buffer_idx
--;
999 tms_tmp
= vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
];
1000 *vsllink_tms_cmd_pos
-= 2;
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
);
1012 for (i
= 0; i
< tdi_len
; i
++)
1014 buffer
[i
] >>= 8 - vsllink_tms_data_len
;
1017 buffer
[i
] += buffer
[i
+ 1] << vsllink_tms_data_len
;
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;
1027 vsllink_append_tms();
1028 vsllink_tap_ensure_space(1, tdi_len
+ 5);
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;
1034 vsllink_tap_append_scan(scan_size
, buffer
, command
, 0);
1039 vsllink_tap_ensure_space(1, tdi_len
+ 7);
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;
1047 vsllink_tap_append_scan(scan_size
, buffer
, command
, 8);
1049 vsllink_end_state(saved_end_state
);
1051 bits_left
= scan_size
& 0x07;
1052 cur_state
= ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
;
1056 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 1 << (bits_left
- 1);
1060 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 1 << 7;
1063 if (cur_state
!= end_state
)
1065 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = VSLLINK_TAP_MOVE(cur_state
, end_state
);
1069 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = 0;
1072 cur_state
= end_state
;
1075 void vsllink_reset(int trst
, int srst
)
1079 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
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;
1087 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_SRST
;
1091 vsllink_usb_out_buffer
[2] |= JTAG_PINMSK_TRST
;
1094 result
= vsllink_usb_write(vsllink_jtag_handle
, 3);
1097 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result
);
1101 void vsllink_simple_command(u8 command
)
1105 DEBUG_JTAG_IO("0x%02x", command
);
1107 vsllink_usb_out_buffer
[0] = command
;
1108 result
= vsllink_usb_write(vsllink_jtag_handle
, 1);
1112 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command
, result
);
1116 int vsllink_handle_usb_vid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1119 LOG_ERROR("parameter error, should be one parameter for VID");
1123 vsllink_vid
= strtol(args
[0], NULL
, 0);
1128 int vsllink_handle_usb_pid_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1131 LOG_ERROR("parameter error, should be one parameter for PID");
1135 vsllink_pid
= strtol(args
[0], NULL
, 0);
1140 int vsllink_handle_usb_bulkin_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1143 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1147 vsllink_bulkin
= strtol(args
[0], NULL
, 0) | 0x80;
1152 int vsllink_handle_usb_bulkout_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1155 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1159 vsllink_bulkout
= strtol(args
[0], NULL
, 0);
1164 /***************************************************************************/
1165 /* VSLLink tap functions */
1167 void vsllink_tap_init(void)
1169 vsllink_usb_out_buffer_idx
= 0;
1170 vsllink_usb_in_want_length
= 0;
1171 pending_scan_results_length
= 0;
1174 void vsllink_tap_ensure_space(int scans
, int bytes
)
1176 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1177 int available_bytes
= VSLLINK_BufferSize
- vsllink_usb_out_buffer_idx
;
1179 if (scans
> available_scans
|| bytes
> available_bytes
)
1181 vsllink_tap_execute();
1185 void vsllink_tap_append_scan(int length
, u8
*buffer
, scan_command_t
*command
, int offset
)
1187 pending_scan_result_t
*pending_scan_result
= &pending_scan_results_buffer
[pending_scan_results_length
];
1192 vsllink_usb_in_want_length
+= ((length
+ 7) >> 3) + 1;
1196 vsllink_usb_in_want_length
+= (length
+ 7) >> 3;
1198 pending_scan_result
->length
= length
;
1199 pending_scan_result
->offset
= offset
;
1200 pending_scan_result
->command
= command
;
1201 pending_scan_result
->buffer
= buffer
;
1203 for (i
= 0; i
< ((length
+ 7) >> 3); i
++)
1205 vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
++] = buffer
[i
];
1208 pending_scan_results_length
++;
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)
1219 if (vsllink_tms_data_len
> 0)
1221 if((cur_state
!= TAP_RESET
) && (cur_state
!= TAP_IDLE
) && (cur_state
!= TAP_IRPAUSE
) && (cur_state
!= TAP_DRPAUSE
))
1223 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", jtag_state_name(cur_state
));
1226 if (vsllink_usb_out_buffer
[vsllink_usb_out_buffer_idx
] & (1 << (vsllink_tms_data_len
- 1)))
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;
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;
1242 if (vsllink_usb_out_buffer_idx
> 3)
1244 if (vsllink_usb_out_buffer
[0] == VSLLINK_CMD_HW_JTAGSEQCMD
)
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;
1250 result
= vsllink_usb_message(vsllink_jtag_handle
, vsllink_usb_out_buffer_idx
, vsllink_usb_in_want_length
);
1252 if (result
== vsllink_usb_in_want_length
)
1254 for (i
= 0; i
< pending_scan_results_length
; i
++)
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
;
1264 // IRSHIFT or DRSHIFT
1265 buf_set_buf(vsllink_usb_in_buffer
, first
* 8 + offset
, buffer
, 0, length
);
1266 first
+= (length
+ offset
+ 7) >> 3;
1268 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length
);
1269 #ifdef _DEBUG_JTAG_IO_
1270 vsllink_debug_buffer(buffer
, (length
+ 7) >> 3);
1273 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
)
1276 return ERROR_JTAG_QUEUE_FAILED
;
1279 free(pending_scan_result
->buffer
);
1280 pending_scan_result
->buffer
= NULL
;
1290 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result
, vsllink_usb_in_want_length
);
1291 return ERROR_JTAG_QUEUE_FAILED
;
1297 vsllink_usb_out_buffer
[0] = VSLLINK_CMD_HW_JTAGSEQCMD
;
1298 vsllink_usb_out_buffer_idx
= 3;
1303 /*****************************************************************************/
1304 /* VSLLink USB low-level functions */
1306 vsllink_jtag_t
* vsllink_usb_open(void)
1308 struct usb_bus
*busses
;
1309 struct usb_bus
*bus
;
1310 struct usb_device
*dev
;
1312 vsllink_jtag_t
*result
;
1314 result
= (vsllink_jtag_t
*) malloc(sizeof(vsllink_jtag_t
));
1320 busses
= usb_get_busses();
1322 /* find vsllink_jtag device in usb bus */
1324 for (bus
= busses
; bus
; bus
= bus
->next
)
1326 for (dev
= bus
->devices
; dev
; dev
= dev
->next
)
1328 if ((dev
->descriptor
.idVendor
== vsllink_vid
) && (dev
->descriptor
.idProduct
== vsllink_pid
))
1330 result
->usb_handle
= usb_open(dev
);
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);
1338 * This makes problems under Mac OS X. And is not needed
1339 * under Windows. Hopefully this will not break a linux build
1341 usb_set_altinterface(result
->usb_handle
, 0);
1352 void vsllink_usb_close(vsllink_jtag_t
*vsllink_jtag
)
1354 usb_close(vsllink_jtag
->usb_handle
);
1358 /* Send a message and receive the reply. */
1359 int vsllink_usb_message(vsllink_jtag_t
*vsllink_jtag
, int out_length
, int in_length
)
1363 result
= vsllink_usb_write(vsllink_jtag
, out_length
);
1364 if (result
== out_length
)
1368 result
= vsllink_usb_read(vsllink_jtag
);
1369 if (result
== in_length
)
1375 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length
, result
);
1383 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length
, result
);
1388 /* Write data from out_buffer to USB. */
1389 int vsllink_usb_write(vsllink_jtag_t
*vsllink_jtag
, int out_length
)
1393 if (out_length
> VSLLINK_BufferSize
)
1395 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length
, VSLLINK_BufferSize
);
1399 result
= usb_bulk_write(vsllink_jtag
->usb_handle
, vsllink_bulkout
, \
1400 (char *)vsllink_usb_out_buffer
, out_length
, VSLLINK_USB_TIMEOUT
);
1402 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length
, result
);
1404 #ifdef _DEBUG_USB_COMMS_
1405 LOG_DEBUG("USB out:");
1406 vsllink_debug_buffer(vsllink_usb_out_buffer
, out_length
);
1409 #ifdef _VSLLINK_IN_DEBUG_MODE_
1416 /* Read data from USB into in_buffer. */
1417 int vsllink_usb_read(vsllink_jtag_t
*vsllink_jtag
)
1419 int result
= usb_bulk_read(vsllink_jtag
->usb_handle
, vsllink_bulkin
, \
1420 (char *)vsllink_usb_in_buffer
, VSLLINK_BufferSize
, VSLLINK_USB_TIMEOUT
);
1422 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result
);
1424 #ifdef _DEBUG_USB_COMMS_
1425 LOG_DEBUG("USB in:");
1426 vsllink_debug_buffer(vsllink_usb_in_buffer
, result
);
1431 #define BYTES_PER_LINE 16
1433 void vsllink_debug_buffer(u8
*buffer
, int length
)
1440 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
)
1442 snprintf(line
, 5, "%04x", i
);
1443 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++)
1445 snprintf(s
, 4, " %02x", buffer
[j
]);