1 /***************************************************************************
2 * Copyright (C) 2007 by Benedikt Sauter sauter@ixbat.de *
3 * based on Dominic Rath's amt_jtagaccel.c *
5 * usbprog is a free programming adapter. You can easily install *
6 * different firmware versions from an "online pool" over USB. *
7 * The adapter can be used for programming and debugging AVR and ARM *
8 * processors, as USB to RS232 converter, as JTAG interface or as *
9 * simple I/O interface (5 lines). *
11 * http://www.embedded-projects.net/usbprog *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
18 * This program is distributed in the hope that it will be useful, *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21 * GNU General Public License for more details. *
23 * You should have received a copy of the GNU General Public License *
24 * along with this program; if not, write to the *
25 * Free Software Foundation, Inc., *
26 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
27 ***************************************************************************/
33 #include "replacements.h"
51 int usbprog_execute_queue(void);
52 int usbprog_speed(int speed);
53 int usbprog_register_commands(struct command_context_s *cmd_ctx);
54 int usbprog_init(void);
55 int usbprog_quit(void);
58 void usbprog_end_state(enum tap_state state);
59 void usbprog_state_move(void);
60 void usbprog_path_move(pathmove_command_t *cmd);
61 void usbprog_runtest(int num_cycles);
62 void usbprog_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size);
64 jtag_interface_t usbprog_interface =
67 .execute_queue = usbprog_execute_queue,
68 .support_pathmove = 0,
69 .speed = usbprog_speed,
70 .register_commands = usbprog_register_commands,
75 #define UNKOWN_COMMAND 0x00
76 #define PORT_DIRECTION 0x01
79 #define PORT_SETBIT 0x04
80 #define PORT_GETBIT 0x05
81 #define WRITE_TDI 0x06
83 #define WRITE_AND_READ 0x08
84 #define WRITE_TMS 0x09
85 #define WRITE_TMS_CHAIN 0x0A
89 struct usb_dev_handle* usb_handle;
92 struct usbprog_jtag * usbprog_jtag_handle;
94 struct usbprog_jtag* usbprog_jtag_open();
95 void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag);
96 void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag);
97 unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen);
100 void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
101 void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
102 void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
103 void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan);
107 void usbprog_jtag_tms_collect(char tms_scan);
108 void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag);
110 void usbprog_write(int tck, int tms, int tdi);
111 void usbprog_reset(int trst, int srst);
113 void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction);
114 void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag,unsigned char value);
115 unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag);
116 void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag,int bit, int value);
117 int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit);
119 int usbprog_speed(int speed)
124 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 DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
145 if (cmd->cmd.end_state->end_state != -1)
146 usbprog_end_state(cmd->cmd.end_state->end_state);
149 #ifdef _DEBUG_JTAG_IO_
150 DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
152 if (cmd->cmd.reset->trst == 1)
156 usbprog_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
159 #ifdef _DEBUG_JTAG_IO_
160 DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
162 if (cmd->cmd.runtest->end_state != -1)
163 usbprog_end_state(cmd->cmd.runtest->end_state);
164 usbprog_runtest(cmd->cmd.runtest->num_cycles);
167 #ifdef _DEBUG_JTAG_IO_
168 DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
170 if (cmd->cmd.statemove->end_state != -1)
171 usbprog_end_state(cmd->cmd.statemove->end_state);
172 usbprog_state_move();
175 #ifdef _DEBUG_JTAG_IO_
176 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 DEBUG("scan end in %i", cmd->cmd.scan->end_state);
185 if (cmd->cmd.scan->end_state != -1)
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 DEBUG("sleep %i", cmd->cmd.sleep->us);
199 jtag_sleep(cmd->cmd.sleep->us);
202 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){
218 ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
219 return ERROR_JTAG_INIT_FAILED;
222 INFO("USB JTAG Interface ready!");
224 usbprog_jtag_init(usbprog_jtag_handle);
226 usbprog_write(0, 0, 0);
231 int usbprog_quit(void)
237 /*************** jtag execute commands **********************/
238 void usbprog_end_state(enum tap_state state)
240 if (tap_move_map[state] != -1)
244 ERROR("BUG: %i is not a valid end state", state);
250 void usbprog_state_move(void) {
253 u8 tms_scan = TAP_MOVE(cur_state, end_state);
255 usbprog_jtag_write_tms(usbprog_jtag_handle,(char)tms_scan);
256 for (i = 0; i < 7; i++)
258 tms = (tms_scan >> i) & 1;
261 cur_state = end_state;
265 void usbprog_path_move(pathmove_command_t *cmd)
267 int num_states = cmd->num_states;
273 if (tap_transitions[cur_state].low == cmd->path[state_count])
276 usbprog_write(0, 0, 0);
277 usbprog_write(1, 0, 0);
279 else if (tap_transitions[cur_state].high == cmd->path[state_count])
282 usbprog_write(0, 1, 0);
283 usbprog_write(1, 1, 0);
287 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
291 cur_state = cmd->path[state_count];
296 end_state = cur_state;
300 void usbprog_runtest(int num_cycles)
305 enum tap_state saved_end_state = end_state;
308 /* only do a state_move when we're not already in RTI */
309 if (cur_state != TAP_RTI)
311 usbprog_end_state(TAP_RTI);
312 usbprog_state_move();
315 /* execute num_cycles */
318 usbprog_write(0, 0, 0);
321 usbprog_jtag_tms_send(usbprog_jtag_handle);
324 for (i = 0; i < num_cycles; i++)
326 usbprog_write(1, 0, 0);
327 usbprog_write(0, 0, 0);
330 /* finish in end_state */
332 usbprog_end_state(saved_end_state);
333 if (cur_state != end_state)
334 usbprog_state_move();
340 void usbprog_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
342 enum tap_state saved_end_state = end_state;
345 usbprog_end_state(TAP_SI);
347 usbprog_end_state(TAP_SD);
349 usbprog_state_move();
350 usbprog_end_state(saved_end_state);
352 usbprog_jtag_tms_send(usbprog_jtag_handle);
354 if (type == SCAN_OUT) {
355 usbprog_jtag_write_tdi(usbprog_jtag_handle, (char*)buffer, scan_size);
357 if (type == SCAN_IN) {
358 usbprog_jtag_read_tdo(usbprog_jtag_handle, (char*)buffer, scan_size);
360 if (type == SCAN_IO) {
361 usbprog_jtag_write_and_read(usbprog_jtag_handle, (char*)buffer, scan_size);
369 if (cur_state != end_state)
370 usbprog_state_move();
373 /*************** jtag wrapper functions *********************/
375 void usbprog_write(int tck, int tms, int tdi)
377 unsigned char output_value=0x00;
380 output_value |= (1<<TMS_BIT);
382 output_value |= (1<<TDI_BIT);
384 output_value |= (1<<TCK_BIT);
386 usbprog_jtag_write_slice(usbprog_jtag_handle,output_value);
389 /* (1) assert or (0) deassert reset lines */
390 void usbprog_reset(int trst, int srst)
392 DEBUG("trst: %i, srst: %i", trst, srst);
395 usbprog_jtag_set_bit(usbprog_jtag_handle,5,0);
397 usbprog_jtag_set_bit(usbprog_jtag_handle,5,1);
400 usbprog_jtag_set_bit(usbprog_jtag_handle,4,0);
402 usbprog_jtag_set_bit(usbprog_jtag_handle,4,1);
407 /*************** jtag lowlevel functions ********************/
410 struct usbprog_jtag* usbprog_jtag_open()
412 struct usb_bus *busses;
414 struct usb_device *dev;
416 struct usbprog_jtag * tmp;
418 tmp = (struct usbprog_jtag*)malloc(sizeof(struct usbprog_jtag));
425 busses = usb_get_busses();
427 /* find usbprog_jtag device in usb bus */
429 for (bus = busses; bus; bus = bus->next){
430 for (dev = bus->devices; dev; dev = dev->next){
431 /* condition for sucessfully hit (too bad, I only check the vendor id)*/
432 if (dev->descriptor.idVendor == VID && dev->descriptor.idProduct == PID) {
433 tmp->usb_handle = usb_open(dev);
434 usb_set_configuration (tmp->usb_handle,dev->config[0].bConfigurationValue);
435 usb_claim_interface(tmp->usb_handle, 0);
436 usb_set_altinterface(tmp->usb_handle,0);
445 void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag)
447 usb_close(usbprog_jtag->usb_handle);
452 unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen)
454 int res = usb_bulk_write(usbprog_jtag->usb_handle,3,msg,msglen,100);
458 res = usb_bulk_read(usbprog_jtag->usb_handle,0x82, msg, 2, 100);
460 return (unsigned char)msg[1];
469 void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag)
471 usbprog_jtag_set_direction(usbprog_jtag, 0xFE);
475 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)
494 tmp[0] = WRITE_AND_READ;
495 tmp[1] = (char)(send_bits>>8); // high
496 tmp[2] = (char)(send_bits); // low
499 for(i=0;i < loops ;i++) {
500 tmp[3+i]=buffer[bufindex];
504 usb_bulk_write(usbprog_jtag->usb_handle,3,tmp,64,1000);
506 while(usb_bulk_read(usbprog_jtag->usb_handle,0x82, tmp, 64, 1000) < 1);
508 for(i=0;i<loops ;i++) {
510 buffer[fillindex++] = swap;
516 void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
518 char tmp[64]; // fastes packet size for usb controller
519 int send_bits, fillindex = 0, i, loops;
522 // 61 byte can be transfered (488 bit)
535 tmp[0] = WRITE_AND_READ;
536 tmp[1] = (char)(send_bits>>8); // high
537 tmp[2] = (char)(send_bits); // low
539 usb_bulk_write(usbprog_jtag->usb_handle,3,tmp,3,1000);
541 while(usb_bulk_read(usbprog_jtag->usb_handle,0x82, tmp, 64, 10) < 1);
543 for(i=0;i<loops ;i++) {
545 buffer[fillindex++] = swap;
550 void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
552 char tmp[64]; /* fastes packet size for usb controller */
553 int send_bits, bufindex = 0, i, loops;
555 /* 61 byte can be transfered (488 bit) */
569 tmp[1] = (char)(send_bits>>8); /* high */
570 tmp[2] = (char)(send_bits); /* low */
573 for(i=0;i < loops ;i++) {
574 tmp[3+i]=buffer[bufindex];
577 usb_bulk_write(usbprog_jtag->usb_handle,3,tmp,64,1000);
582 void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan)
584 usbprog_jtag_tms_collect(tms_scan);
588 void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction)
591 tmp[0] = PORT_DIRECTION;
592 tmp[1] = (char)direction;
593 usbprog_jtag_message(usbprog_jtag,tmp,2);
596 void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag,unsigned char value)
600 tmp[1] = (char)value;
601 usbprog_jtag_message(usbprog_jtag,tmp,2);
604 unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag)
609 return usbprog_jtag_message(usbprog_jtag,tmp,2);
613 void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag,int bit, int value)
616 tmp[0] = PORT_SETBIT;
622 usbprog_jtag_message(usbprog_jtag,tmp,3);
625 int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit)
628 tmp[0] = PORT_GETBIT;
631 if(usbprog_jtag_message(usbprog_jtag,tmp,2)>0)
637 void usbprog_jtag_tms_collect(char tms_scan){
638 tms_chain[tms_chain_index]=tms_scan;
642 void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag){
644 if(tms_chain_index>0) {
645 char tmp[tms_chain_index+2];
646 tmp[0] = WRITE_TMS_CHAIN;
647 tmp[1] = (char)(tms_chain_index);
648 for(i=0;i<tms_chain_index+1;i++)
649 tmp[2+i] = tms_chain[i];
650 usb_bulk_write(usbprog_jtag->usb_handle,3,tmp,tms_chain_index+2,1000);