1 /***************************************************************************
2 * Copyright (C) 2007 by Benedikt Sauter *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 * This file is based on Dominic Rath's amt_jtagaccel.c.
24 * usbprog is a free programming adapter. You can easily install
25 * different firmware versions from an "online pool" over USB.
26 * The adapter can be used for programming and debugging AVR and ARM
27 * processors, as USB to RS232 converter, as JTAG interface or as
28 * simple I/O interface (5 lines).
30 * http://www.embedded-projects.net/usbprog
37 #include "replacements.h"
55 int usbprog_execute_queue(void);
56 int usbprog_speed(int speed
);
57 int usbprog_register_commands(struct command_context_s
*cmd_ctx
);
58 int usbprog_init(void);
59 int usbprog_quit(void);
61 void usbprog_end_state(tap_state_t state
);
62 void usbprog_state_move(void);
63 void usbprog_path_move(pathmove_command_t
*cmd
);
64 void usbprog_runtest(int num_cycles
);
65 void usbprog_scan(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
);
67 jtag_interface_t usbprog_interface
=
70 .execute_queue
= usbprog_execute_queue
,
71 .speed
= usbprog_speed
,
72 .register_commands
= usbprog_register_commands
,
77 #define UNKOWN_COMMAND 0x00
78 #define PORT_DIRECTION 0x01
81 #define PORT_SETBIT 0x04
82 #define PORT_GETBIT 0x05
83 #define WRITE_TDI 0x06
85 #define WRITE_AND_READ 0x08
86 #define WRITE_TMS 0x09
87 #define WRITE_TMS_CHAIN 0x0A
91 struct usb_dev_handle
* usb_handle
;
94 struct usbprog_jtag
* usbprog_jtag_handle
;
96 struct usbprog_jtag
* usbprog_jtag_open(void);
97 void usbprog_jtag_close(struct usbprog_jtag
*usbprog_jtag
);
98 void usbprog_jtag_init(struct usbprog_jtag
*usbprog_jtag
);
99 unsigned char usbprog_jtag_message(struct usbprog_jtag
*usbprog_jtag
, char *msg
, int msglen
);
101 void usbprog_jtag_read_tdo(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
);
102 void usbprog_jtag_write_tdi(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
);
103 void usbprog_jtag_write_and_read(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
);
104 void usbprog_jtag_write_tms(struct usbprog_jtag
*usbprog_jtag
, char tms_scan
);
108 void usbprog_jtag_tms_collect(char tms_scan
);
109 void usbprog_jtag_tms_send(struct usbprog_jtag
*usbprog_jtag
);
111 void usbprog_write(int tck
, int tms
, int tdi
);
112 void usbprog_reset(int trst
, int srst
);
114 void usbprog_jtag_set_direction(struct usbprog_jtag
*usbprog_jtag
, unsigned char direction
);
115 void usbprog_jtag_write_slice(struct usbprog_jtag
*usbprog_jtag
,unsigned char value
);
116 unsigned char usbprog_jtag_get_port(struct usbprog_jtag
*usbprog_jtag
);
117 void usbprog_jtag_set_bit(struct usbprog_jtag
*usbprog_jtag
,int bit
, int value
);
118 int usbprog_jtag_get_bit(struct usbprog_jtag
*usbprog_jtag
, int bit
);
120 int usbprog_speed(int speed
)
125 int usbprog_register_commands(struct command_context_s
*cmd_ctx
)
130 int usbprog_execute_queue(void)
132 jtag_command_t
*cmd
= jtag_command_queue
; /* currently processed command */
142 #ifdef _DEBUG_JTAG_IO_
143 LOG_DEBUG("end_state: %i", cmd
->cmd
.end_state
->end_state
);
145 if (cmd
->cmd
.end_state
->end_state
!= TAP_INVALID
)
146 usbprog_end_state(cmd
->cmd
.end_state
->end_state
);
149 #ifdef _DEBUG_JTAG_IO_
150 LOG_DEBUG("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
152 if (cmd
->cmd
.reset
->trst
== 1)
154 tap_set_state(TAP_RESET
);
156 usbprog_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
159 #ifdef _DEBUG_JTAG_IO_
160 LOG_DEBUG("runtest %i cycles, end in %i", cmd
->cmd
.runtest
->num_cycles
, cmd
->cmd
.runtest
->end_state
);
162 if (cmd
->cmd
.runtest
->end_state
!= TAP_INVALID
)
163 usbprog_end_state(cmd
->cmd
.runtest
->end_state
);
164 usbprog_runtest(cmd
->cmd
.runtest
->num_cycles
);
167 #ifdef _DEBUG_JTAG_IO_
168 LOG_DEBUG("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
170 if (cmd
->cmd
.statemove
->end_state
!= TAP_INVALID
)
171 usbprog_end_state(cmd
->cmd
.statemove
->end_state
);
172 usbprog_state_move();
175 #ifdef _DEBUG_JTAG_IO_
176 LOG_DEBUG("pathmove: %i states, end in %i", cmd
->cmd
.pathmove
->num_states
,
177 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
179 usbprog_path_move(cmd
->cmd
.pathmove
);
182 #ifdef _DEBUG_JTAG_IO_
183 LOG_DEBUG("scan end in %i", cmd
->cmd
.scan
->end_state
);
185 if (cmd
->cmd
.scan
->end_state
!= TAP_INVALID
)
186 usbprog_end_state(cmd
->cmd
.scan
->end_state
);
187 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
188 type
= jtag_scan_type(cmd
->cmd
.scan
);
189 usbprog_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
190 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
191 return ERROR_JTAG_QUEUE_FAILED
;
196 #ifdef _DEBUG_JTAG_IO_
197 LOG_DEBUG("sleep %i", cmd
->cmd
.sleep
->us
);
199 jtag_sleep(cmd
->cmd
.sleep
->us
);
202 LOG_ERROR("BUG: unknown JTAG command type encountered");
212 int usbprog_init(void)
214 usbprog_jtag_handle
= usbprog_jtag_open();
217 if (usbprog_jtag_handle
== 0)
219 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
220 return ERROR_JTAG_INIT_FAILED
;
223 LOG_INFO("USB JTAG Interface ready!");
225 usbprog_jtag_init(usbprog_jtag_handle
);
227 usbprog_write(0, 0, 0);
232 int usbprog_quit(void)
237 /*************** jtag execute commands **********************/
238 void usbprog_end_state(tap_state_t state
)
240 if (tap_is_state_stable(state
))
241 tap_set_end_state(state
);
244 LOG_ERROR("BUG: %i is not a valid end state", state
);
249 void usbprog_state_move(void)
252 u8 tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
254 usbprog_jtag_write_tms(usbprog_jtag_handle
, (char)tms_scan
);
255 for (i
= 0; i
< 7; i
++)
257 tms
= (tms_scan
>> i
) & 1;
260 tap_set_state(tap_get_end_state());
263 void usbprog_path_move(pathmove_command_t
*cmd
)
265 int num_states
= cmd
->num_states
;
268 /* There may be queued transitions, and before following a specified
269 path, we must flush those queued transitions */
270 usbprog_jtag_tms_send(usbprog_jtag_handle
);
275 if (tap_state_transition(tap_get_state(), false) == cmd
->path
[state_count
])
278 usbprog_write(0, 0, 0);
279 usbprog_write(1, 0, 0);
281 else if (tap_state_transition(tap_get_state(), true) == cmd
->path
[state_count
])
284 usbprog_write(0, 1, 0);
285 usbprog_write(1, 1, 0);
289 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd
->path
[state_count
]));
293 tap_set_state(cmd
->path
[state_count
]);
298 tap_set_end_state(tap_get_state());
301 void usbprog_runtest(int num_cycles
)
305 /* only do a state_move when we're not already in IDLE */
306 if (tap_get_state() != TAP_IDLE
)
308 usbprog_end_state(TAP_IDLE
);
309 usbprog_state_move();
312 /* execute num_cycles */
315 usbprog_jtag_tms_send(usbprog_jtag_handle
);
316 usbprog_write(0, 0, 0);
320 usbprog_jtag_tms_send(usbprog_jtag_handle
);
321 /* LOG_INFO("NUM CYCLES %i",num_cycles); */
324 for (i
= 0; i
< num_cycles
; i
++)
326 usbprog_write(1, 0, 0);
327 usbprog_write(0, 0, 0);
330 #ifdef _DEBUG_JTAG_IO_
331 LOG_DEBUG("runtest: cur_state %s end_state %s", tap_state_name(tap_get_state()), tap_state_name(tap_get_end_state()));
334 /* finish in end_state */
336 usbprog_end_state(saved_end_state);
337 if (tap_get_state() != tap_get_end_state())
338 usbprog_state_move();
342 void usbprog_scan(int ir_scan
, enum scan_type type
, u8
*buffer
, int scan_size
)
344 tap_state_t saved_end_state
= tap_get_end_state();
347 usbprog_end_state(TAP_IRSHIFT
);
349 usbprog_end_state(TAP_DRSHIFT
);
351 /* Only move if we're not already there */
352 if (tap_get_state() != tap_get_end_state())
353 usbprog_state_move();
355 usbprog_end_state(saved_end_state
);
357 usbprog_jtag_tms_send(usbprog_jtag_handle
);
359 void (*f
)(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
);
361 case SCAN_OUT
: f
= &usbprog_jtag_write_tdi
; break;
362 case SCAN_IN
: f
= &usbprog_jtag_read_tdo
; break;
363 case SCAN_IO
: f
= &usbprog_jtag_write_and_read
; break;
365 LOG_ERROR("unknown scan type: %i", type
);
368 f(usbprog_jtag_handle
, (char *)buffer
, scan_size
);
370 /* The adapter does the transition to PAUSE internally */
372 tap_set_state(TAP_IRPAUSE
);
374 tap_set_state(TAP_DRPAUSE
);
376 if (tap_get_state() != tap_get_end_state())
377 usbprog_state_move();
380 /*************** jtag wrapper functions *********************/
382 void usbprog_write(int tck
, int tms
, int tdi
)
384 unsigned char output_value
=0x00;
387 output_value
|= (1<<TMS_BIT
);
389 output_value
|= (1<<TDI_BIT
);
391 output_value
|= (1<<TCK_BIT
);
393 usbprog_jtag_write_slice(usbprog_jtag_handle
,output_value
);
396 /* (1) assert or (0) deassert reset lines */
397 void usbprog_reset(int trst
, int srst
)
399 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
402 usbprog_jtag_set_bit(usbprog_jtag_handle
, 5, 0);
404 usbprog_jtag_set_bit(usbprog_jtag_handle
, 5, 1);
407 usbprog_jtag_set_bit(usbprog_jtag_handle
, 4, 0);
409 usbprog_jtag_set_bit(usbprog_jtag_handle
, 4, 1);
412 /*************** jtag lowlevel functions ********************/
414 struct usb_bus
*busses
;
416 struct usbprog_jtag
* usbprog_jtag_open(void)
419 struct usb_device
*dev
;
421 struct usbprog_jtag
*tmp
;
423 tmp
= (struct usbprog_jtag
*)malloc(sizeof(struct usbprog_jtag
));
430 busses
= usb_get_busses();
432 /* find usbprog_jtag device in usb bus */
434 for (bus
= busses
; bus
; bus
= bus
->next
)
436 for (dev
= bus
->devices
; dev
; dev
= dev
->next
)
438 /* condition for sucessfully hit (too bad, I only check the vendor id)*/
439 if (dev
->descriptor
.idVendor
== VID
&& dev
->descriptor
.idProduct
== PID
)
441 tmp
->usb_handle
= usb_open(dev
);
442 usb_set_configuration(tmp
->usb_handle
, 1);
443 usb_claim_interface(tmp
->usb_handle
, 0);
444 usb_set_altinterface(tmp
->usb_handle
, 0);
452 void usbprog_jtag_close(struct usbprog_jtag
*usbprog_jtag
)
454 usb_close(usbprog_jtag
->usb_handle
);
458 unsigned char usbprog_jtag_message(struct usbprog_jtag
*usbprog_jtag
, char *msg
, int msglen
)
460 int res
= usb_bulk_write(usbprog_jtag
->usb_handle
, 3, msg
,msglen
, 100);
461 if ((msg
[0] == 2) || (msg
[0] == 1) || (msg
[0] == 4) || (msg
[0] == 0) || \
462 (msg
[0] == 6) || (msg
[0] == 0x0A) || (msg
[0] == 9))
466 /* LOG_INFO("HALLLLOOO %i",(int)msg[0]); */
467 res
= usb_bulk_read(usbprog_jtag
->usb_handle
, 0x82, msg
, 2, 100);
469 return (unsigned char)msg
[1];
478 void usbprog_jtag_init(struct usbprog_jtag
*usbprog_jtag
)
480 usbprog_jtag_set_direction(usbprog_jtag
, 0xFE);
483 void usbprog_jtag_write_and_read(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
)
485 char tmp
[64]; /* fastes packet size for usb controller */
486 int send_bits
, bufindex
= 0, fillindex
= 0, i
, loops
;
489 /* 61 byte can be transfered (488 bit) */
506 tmp
[0] = WRITE_AND_READ
;
507 tmp
[1] = (char)(send_bits
>> 8); /* high */
508 tmp
[2] = (char)(send_bits
); /* low */
511 for (i
= 0; i
< loops
; i
++)
513 tmp
[3 + i
] = buffer
[bufindex
];
517 if (usb_bulk_write(usbprog_jtag
->usb_handle
, 3, tmp
, 64, 1000) == 64)
519 /* LOG_INFO("HALLLLOOO2 %i",(int)tmp[0]); */
522 while (usb_bulk_read(usbprog_jtag
->usb_handle
, 0x82, tmp
, 64, 1000) < 1)
529 for (i
= 0; i
< loops
; i
++)
532 buffer
[fillindex
++] = swap
;
538 void usbprog_jtag_read_tdo(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
)
540 char tmp
[64]; /* fastes packet size for usb controller */
541 int send_bits
, fillindex
= 0, i
, loops
;
544 /* 61 byte can be transfered (488 bit) */
561 tmp
[0] = WRITE_AND_READ
;
562 tmp
[1] = (char)(send_bits
>> 8); /* high */
563 tmp
[2] = (char)(send_bits
); /* low */
565 usb_bulk_write(usbprog_jtag
->usb_handle
, 3, tmp
, 3, 1000);
567 /* LOG_INFO("HALLLLOOO3 %i",(int)tmp[0]); */
570 while (usb_bulk_read(usbprog_jtag
->usb_handle
, 0x82, tmp
, 64, 10) < 1)
577 for (i
= 0; i
< loops
; i
++)
580 buffer
[fillindex
++] = swap
;
585 void usbprog_jtag_write_tdi(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
)
587 char tmp
[64]; /* fastes packet size for usb controller */
588 int send_bits
, bufindex
= 0, i
, loops
;
590 /* 61 byte can be transfered (488 bit) */
608 tmp
[1] = (char)(send_bits
>> 8); /* high */
609 tmp
[2] = (char)(send_bits
); /* low */
612 for (i
= 0; i
< loops
; i
++)
614 tmp
[3 + i
] = buffer
[bufindex
];
617 usb_bulk_write(usbprog_jtag
->usb_handle
, 3, tmp
, 64, 1000);
621 void usbprog_jtag_write_tms(struct usbprog_jtag
*usbprog_jtag
, char tms_scan
)
623 usbprog_jtag_tms_collect(tms_scan
);
626 void usbprog_jtag_set_direction(struct usbprog_jtag
*usbprog_jtag
, unsigned char direction
)
629 tmp
[0] = PORT_DIRECTION
;
630 tmp
[1] = (char)direction
;
631 usbprog_jtag_message(usbprog_jtag
, tmp
, 2);
634 void usbprog_jtag_write_slice(struct usbprog_jtag
*usbprog_jtag
,unsigned char value
)
638 tmp
[1] = (char)value
;
639 usbprog_jtag_message(usbprog_jtag
, tmp
, 2);
642 unsigned char usbprog_jtag_get_port(struct usbprog_jtag
*usbprog_jtag
)
647 return usbprog_jtag_message(usbprog_jtag
, tmp
, 2);
650 void usbprog_jtag_set_bit(struct usbprog_jtag
*usbprog_jtag
,int bit
, int value
)
653 tmp
[0] = PORT_SETBIT
;
659 usbprog_jtag_message(usbprog_jtag
, tmp
, 3);
662 int usbprog_jtag_get_bit(struct usbprog_jtag
*usbprog_jtag
, int bit
)
665 tmp
[0] = PORT_GETBIT
;
668 if (usbprog_jtag_message(usbprog_jtag
, tmp
, 2) > 0)
674 void usbprog_jtag_tms_collect(char tms_scan
)
676 tms_chain
[tms_chain_index
] = tms_scan
;
680 void usbprog_jtag_tms_send(struct usbprog_jtag
*usbprog_jtag
)
683 /* LOG_INFO("TMS SEND"); */
684 if (tms_chain_index
> 0)
686 char tmp
[tms_chain_index
+ 2];
687 tmp
[0] = WRITE_TMS_CHAIN
;
688 tmp
[1] = (char)(tms_chain_index
);
689 for (i
= 0; i
< tms_chain_index
+ 1; i
++)
690 tmp
[2 + i
] = tms_chain
[i
];
691 usb_bulk_write(usbprog_jtag
->usb_handle
, 3, tmp
, tms_chain_index
+ 2, 1000);
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)