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 "interface.h"
52 static int usbprog_execute_queue(void);
53 static int usbprog_speed(int speed
);
54 static int usbprog_register_commands(struct command_context_s
*cmd_ctx
);
55 static int usbprog_init(void);
56 static int usbprog_quit(void);
58 static void usbprog_end_state(tap_state_t state
);
59 static void usbprog_state_move(void);
60 static void usbprog_path_move(pathmove_command_t
*cmd
);
61 static void usbprog_runtest(int num_cycles
);
62 static void usbprog_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
);
64 jtag_interface_t usbprog_interface
=
67 .execute_queue
= usbprog_execute_queue
,
68 .speed
= usbprog_speed
,
69 .register_commands
= usbprog_register_commands
,
74 #define UNKOWN_COMMAND 0x00
75 #define PORT_DIRECTION 0x01
78 #define PORT_SETBIT 0x04
79 #define PORT_GETBIT 0x05
80 #define WRITE_TDI 0x06
82 #define WRITE_AND_READ 0x08
83 #define WRITE_TMS 0x09
84 #define WRITE_TMS_CHAIN 0x0A
88 struct usb_dev_handle
* usb_handle
;
91 static struct usbprog_jtag
* usbprog_jtag_handle
;
93 static struct usbprog_jtag
* usbprog_jtag_open(void);
94 //static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag);
95 static void usbprog_jtag_init(struct usbprog_jtag
*usbprog_jtag
);
96 static unsigned char usbprog_jtag_message(struct usbprog_jtag
*usbprog_jtag
, char *msg
, int msglen
);
98 static void usbprog_jtag_read_tdo(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
);
99 static void usbprog_jtag_write_tdi(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
);
100 static void usbprog_jtag_write_and_read(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
);
101 static void usbprog_jtag_write_tms(struct usbprog_jtag
*usbprog_jtag
, char tms_scan
);
103 static char tms_chain
[64];
104 static int tms_chain_index
;
106 static void usbprog_jtag_tms_collect(char tms_scan
);
107 static void usbprog_jtag_tms_send(struct usbprog_jtag
*usbprog_jtag
);
109 static void usbprog_write(int tck
, int tms
, int tdi
);
110 static void usbprog_reset(int trst
, int srst
);
112 static void usbprog_jtag_set_direction(struct usbprog_jtag
*usbprog_jtag
, unsigned char direction
);
113 static void usbprog_jtag_write_slice(struct usbprog_jtag
*usbprog_jtag
,unsigned char value
);
114 //static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag);
115 static void usbprog_jtag_set_bit(struct usbprog_jtag
*usbprog_jtag
,int bit
, int value
);
116 //static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit);
118 static int usbprog_speed(int speed
)
123 static int usbprog_register_commands(struct command_context_s
*cmd_ctx
)
128 static int usbprog_execute_queue(void)
130 jtag_command_t
*cmd
= jtag_command_queue
; /* currently processed command */
140 #ifdef _DEBUG_JTAG_IO_
141 LOG_DEBUG("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
143 if (cmd
->cmd
.reset
->trst
== 1)
145 tap_set_state(TAP_RESET
);
147 usbprog_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
150 #ifdef _DEBUG_JTAG_IO_
151 LOG_DEBUG("runtest %i cycles, end in %i", cmd
->cmd
.runtest
->num_cycles
, cmd
->cmd
.runtest
->end_state
);
153 usbprog_end_state(cmd
->cmd
.runtest
->end_state
);
154 usbprog_runtest(cmd
->cmd
.runtest
->num_cycles
);
157 #ifdef _DEBUG_JTAG_IO_
158 LOG_DEBUG("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
160 usbprog_end_state(cmd
->cmd
.statemove
->end_state
);
161 usbprog_state_move();
164 #ifdef _DEBUG_JTAG_IO_
165 LOG_DEBUG("pathmove: %i states, end in %i", cmd
->cmd
.pathmove
->num_states
,
166 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
168 usbprog_path_move(cmd
->cmd
.pathmove
);
171 #ifdef _DEBUG_JTAG_IO_
172 LOG_DEBUG("scan end in %i", cmd
->cmd
.scan
->end_state
);
174 usbprog_end_state(cmd
->cmd
.scan
->end_state
);
175 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
176 type
= jtag_scan_type(cmd
->cmd
.scan
);
177 usbprog_scan(cmd
->cmd
.scan
->ir_scan
, type
, buffer
, scan_size
);
178 if (jtag_read_buffer(buffer
, cmd
->cmd
.scan
) != ERROR_OK
)
179 return ERROR_JTAG_QUEUE_FAILED
;
184 #ifdef _DEBUG_JTAG_IO_
185 LOG_DEBUG("sleep %i", cmd
->cmd
.sleep
->us
);
187 jtag_sleep(cmd
->cmd
.sleep
->us
);
190 LOG_ERROR("BUG: unknown JTAG command type encountered");
200 static int usbprog_init(void)
202 usbprog_jtag_handle
= usbprog_jtag_open();
205 if (usbprog_jtag_handle
== 0)
207 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
208 return ERROR_JTAG_INIT_FAILED
;
211 LOG_INFO("USB JTAG Interface ready!");
213 usbprog_jtag_init(usbprog_jtag_handle
);
215 usbprog_write(0, 0, 0);
220 static int usbprog_quit(void)
225 /*************** jtag execute commands **********************/
226 static void usbprog_end_state(tap_state_t state
)
228 if (tap_is_state_stable(state
))
229 tap_set_end_state(state
);
232 LOG_ERROR("BUG: %i is not a valid end state", state
);
237 static void usbprog_state_move(void)
240 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
241 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
243 usbprog_jtag_write_tms(usbprog_jtag_handle
, (char)tms_scan
);
244 for (i
= 0; i
< tms_count
; i
++)
246 tms
= (tms_scan
>> i
) & 1;
249 tap_set_state(tap_get_end_state());
252 static void usbprog_path_move(pathmove_command_t
*cmd
)
254 int num_states
= cmd
->num_states
;
257 /* There may be queued transitions, and before following a specified
258 path, we must flush those queued transitions */
259 usbprog_jtag_tms_send(usbprog_jtag_handle
);
264 if (tap_state_transition(tap_get_state(), false) == cmd
->path
[state_count
])
267 usbprog_write(0, 0, 0);
268 usbprog_write(1, 0, 0);
270 else if (tap_state_transition(tap_get_state(), true) == cmd
->path
[state_count
])
273 usbprog_write(0, 1, 0);
274 usbprog_write(1, 1, 0);
278 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd
->path
[state_count
]));
282 tap_set_state(cmd
->path
[state_count
]);
287 tap_set_end_state(tap_get_state());
290 static void usbprog_runtest(int num_cycles
)
294 /* only do a state_move when we're not already in IDLE */
295 if (tap_get_state() != TAP_IDLE
)
297 usbprog_end_state(TAP_IDLE
);
298 usbprog_state_move();
301 /* execute num_cycles */
304 usbprog_jtag_tms_send(usbprog_jtag_handle
);
305 usbprog_write(0, 0, 0);
309 usbprog_jtag_tms_send(usbprog_jtag_handle
);
310 /* LOG_INFO("NUM CYCLES %i",num_cycles); */
313 for (i
= 0; i
< num_cycles
; i
++)
315 usbprog_write(1, 0, 0);
316 usbprog_write(0, 0, 0);
319 #ifdef _DEBUG_JTAG_IO_
320 LOG_DEBUG("runtest: cur_state %s end_state %s", tap_state_name(tap_get_state()), tap_state_name(tap_get_end_state()));
323 /* finish in end_state */
325 usbprog_end_state(saved_end_state);
326 if (tap_get_state() != tap_get_end_state())
327 usbprog_state_move();
331 static void usbprog_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
, int scan_size
)
333 tap_state_t saved_end_state
= tap_get_end_state();
336 usbprog_end_state(TAP_IRSHIFT
);
338 usbprog_end_state(TAP_DRSHIFT
);
340 /* Only move if we're not already there */
341 if (tap_get_state() != tap_get_end_state())
342 usbprog_state_move();
344 usbprog_end_state(saved_end_state
);
346 usbprog_jtag_tms_send(usbprog_jtag_handle
);
348 void (*f
)(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
);
350 case SCAN_OUT
: f
= &usbprog_jtag_write_tdi
; break;
351 case SCAN_IN
: f
= &usbprog_jtag_read_tdo
; break;
352 case SCAN_IO
: f
= &usbprog_jtag_write_and_read
; break;
354 LOG_ERROR("unknown scan type: %i", type
);
357 f(usbprog_jtag_handle
, (char *)buffer
, scan_size
);
359 /* The adapter does the transition to PAUSE internally */
361 tap_set_state(TAP_IRPAUSE
);
363 tap_set_state(TAP_DRPAUSE
);
365 if (tap_get_state() != tap_get_end_state())
366 usbprog_state_move();
369 /*************** jtag wrapper functions *********************/
371 static void usbprog_write(int tck
, int tms
, int tdi
)
373 unsigned char output_value
= 0x00;
376 output_value
|= (1 << TMS_BIT
);
378 output_value
|= (1 << TDI_BIT
);
380 output_value
|= (1 << TCK_BIT
);
382 usbprog_jtag_write_slice(usbprog_jtag_handle
,output_value
);
385 /* (1) assert or (0) deassert reset lines */
386 static void usbprog_reset(int trst
, int srst
)
388 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
391 usbprog_jtag_set_bit(usbprog_jtag_handle
, 5, 0);
393 usbprog_jtag_set_bit(usbprog_jtag_handle
, 5, 1);
396 usbprog_jtag_set_bit(usbprog_jtag_handle
, 4, 0);
398 usbprog_jtag_set_bit(usbprog_jtag_handle
, 4, 1);
401 /*************** jtag lowlevel functions ********************/
403 struct usb_bus
*busses
;
405 struct usbprog_jtag
* usbprog_jtag_open(void)
408 struct usb_device
*dev
;
410 struct usbprog_jtag
*tmp
;
412 tmp
= (struct usbprog_jtag
*)malloc(sizeof(struct usbprog_jtag
));
419 busses
= usb_get_busses();
421 /* find usbprog_jtag device in usb bus */
423 for (bus
= busses
; bus
; bus
= bus
->next
)
425 for (dev
= bus
->devices
; dev
; dev
= dev
->next
)
427 /* condition for sucessfully hit (too bad, I only check the vendor id)*/
428 if (dev
->descriptor
.idVendor
== VID
&& dev
->descriptor
.idProduct
== PID
)
430 tmp
->usb_handle
= usb_open(dev
);
431 usb_set_configuration(tmp
->usb_handle
, 1);
432 usb_claim_interface(tmp
->usb_handle
, 0);
433 usb_set_altinterface(tmp
->usb_handle
, 0);
443 static void usbprog_jtag_close(struct usbprog_jtag
*usbprog_jtag
)
445 usb_close(usbprog_jtag
->usb_handle
);
450 static unsigned char usbprog_jtag_message(struct usbprog_jtag
*usbprog_jtag
, char *msg
, int msglen
)
452 int res
= usb_bulk_write(usbprog_jtag
->usb_handle
, 3, msg
,msglen
, 100);
453 if ((msg
[0] == 2) || (msg
[0] == 1) || (msg
[0] == 4) || (msg
[0] == 0) || \
454 (msg
[0] == 6) || (msg
[0] == 0x0A) || (msg
[0] == 9))
458 /* LOG_INFO("HALLLLOOO %i",(int)msg[0]); */
459 res
= usb_bulk_read(usbprog_jtag
->usb_handle
, 0x82, msg
, 2, 100);
461 return (unsigned char)msg
[1];
470 static void usbprog_jtag_init(struct usbprog_jtag
*usbprog_jtag
)
472 usbprog_jtag_set_direction(usbprog_jtag
, 0xFE);
475 static void usbprog_jtag_write_and_read(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
)
477 char tmp
[64]; /* fastes packet size for usb controller */
478 int send_bits
, bufindex
= 0, fillindex
= 0, i
, loops
;
481 /* 61 byte can be transfered (488 bit) */
498 tmp
[0] = WRITE_AND_READ
;
499 tmp
[1] = (char)(send_bits
>> 8); /* high */
500 tmp
[2] = (char)(send_bits
); /* low */
503 for (i
= 0; i
< loops
; i
++)
505 tmp
[3 + i
] = buffer
[bufindex
];
509 if (usb_bulk_write(usbprog_jtag
->usb_handle
, 3, tmp
, 64, 1000) == 64)
511 /* LOG_INFO("HALLLLOOO2 %i",(int)tmp[0]); */
514 while (usb_bulk_read(usbprog_jtag
->usb_handle
, 0x82, tmp
, 64, 1000) < 1)
521 for (i
= 0; i
< loops
; i
++)
524 buffer
[fillindex
++] = swap
;
530 static void usbprog_jtag_read_tdo(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
)
532 char tmp
[64]; /* fastes packet size for usb controller */
533 int send_bits
, fillindex
= 0, i
, loops
;
536 /* 61 byte can be transfered (488 bit) */
553 tmp
[0] = WRITE_AND_READ
;
554 tmp
[1] = (char)(send_bits
>> 8); /* high */
555 tmp
[2] = (char)(send_bits
); /* low */
557 usb_bulk_write(usbprog_jtag
->usb_handle
, 3, tmp
, 3, 1000);
559 /* LOG_INFO("HALLLLOOO3 %i",(int)tmp[0]); */
562 while (usb_bulk_read(usbprog_jtag
->usb_handle
, 0x82, tmp
, 64, 10) < 1)
569 for (i
= 0; i
< loops
; i
++)
572 buffer
[fillindex
++] = swap
;
577 static void usbprog_jtag_write_tdi(struct usbprog_jtag
*usbprog_jtag
, char * buffer
, int size
)
579 char tmp
[64]; /* fastes packet size for usb controller */
580 int send_bits
, bufindex
= 0, i
, loops
;
582 /* 61 byte can be transfered (488 bit) */
595 /* if (loops == 0) */
600 tmp
[1] = (char)(send_bits
>> 8); /* high */
601 tmp
[2] = (char)(send_bits
); /* low */
604 for (i
= 0; i
< loops
; i
++)
606 tmp
[3 + i
] = buffer
[bufindex
];
609 usb_bulk_write(usbprog_jtag
->usb_handle
, 3, tmp
, 64, 1000);
613 static void usbprog_jtag_write_tms(struct usbprog_jtag
*usbprog_jtag
, char tms_scan
)
615 usbprog_jtag_tms_collect(tms_scan
);
618 static void usbprog_jtag_set_direction(struct usbprog_jtag
*usbprog_jtag
, unsigned char direction
)
621 tmp
[0] = PORT_DIRECTION
;
622 tmp
[1] = (char)direction
;
623 usbprog_jtag_message(usbprog_jtag
, tmp
, 2);
626 static void usbprog_jtag_write_slice(struct usbprog_jtag
*usbprog_jtag
,unsigned char value
)
630 tmp
[1] = (char)value
;
631 usbprog_jtag_message(usbprog_jtag
, tmp
, 2);
635 static unsigned char usbprog_jtag_get_port(struct usbprog_jtag
*usbprog_jtag
)
640 return usbprog_jtag_message(usbprog_jtag
, tmp
, 2);
644 static void usbprog_jtag_set_bit(struct usbprog_jtag
*usbprog_jtag
,int bit
, int value
)
647 tmp
[0] = PORT_SETBIT
;
653 usbprog_jtag_message(usbprog_jtag
, tmp
, 3);
657 static int usbprog_jtag_get_bit(struct usbprog_jtag
*usbprog_jtag
, int bit
)
660 tmp
[0] = PORT_GETBIT
;
663 if (usbprog_jtag_message(usbprog_jtag
, tmp
, 2) > 0)
670 static void usbprog_jtag_tms_collect(char tms_scan
)
672 tms_chain
[tms_chain_index
] = tms_scan
;
676 static void usbprog_jtag_tms_send(struct usbprog_jtag
*usbprog_jtag
)
679 /* LOG_INFO("TMS SEND"); */
680 if (tms_chain_index
> 0)
682 char tmp
[tms_chain_index
+ 2];
683 tmp
[0] = WRITE_TMS_CHAIN
;
684 tmp
[1] = (char)(tms_chain_index
);
685 for (i
= 0; i
< tms_chain_index
+ 1; i
++)
686 tmp
[2 + i
] = tms_chain
[i
];
687 usb_bulk_write(usbprog_jtag
->usb_handle
, 3, tmp
, tms_chain_index
+ 2, 1000);