jtag_interface: .speed can be NULL when not needed
[openocd.git] / src / jtag / drivers / usbprog.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Benedikt Sauter *
3 * sauter@ixbat.de *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 /*
22 * This file is based on Dominic Rath's amt_jtagaccel.c.
23 *
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).
29 *
30 * http://www.embedded-projects.net/usbprog
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <jtag/interface.h>
38 #include <jtag/commands.h>
39 #include "usb_common.h"
40
41 #define VID 0x1781
42 #define PID 0x0c63
43
44 /* Pins at usbprog */
45 #define TDO_BIT 0
46 #define TDI_BIT 3
47 #define TCK_BIT 2
48 #define TMS_BIT 1
49
50 static void usbprog_end_state(tap_state_t state);
51 static void usbprog_state_move(void);
52 static void usbprog_path_move(struct pathmove_command *cmd);
53 static void usbprog_runtest(int num_cycles);
54 static void usbprog_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size);
55
56 #define UNKNOWN_COMMAND 0x00
57 #define PORT_DIRECTION 0x01
58 #define PORT_SET 0x02
59 #define PORT_GET 0x03
60 #define PORT_SETBIT 0x04
61 #define PORT_GETBIT 0x05
62 #define WRITE_TDI 0x06
63 #define READ_TDO 0x07
64 #define WRITE_AND_READ 0x08
65 #define WRITE_TMS 0x09
66 #define WRITE_TMS_CHAIN 0x0A
67
68 struct usbprog_jtag {
69 struct usb_dev_handle *usb_handle;
70 };
71
72 static struct usbprog_jtag *usbprog_jtag_handle;
73
74 static struct usbprog_jtag *usbprog_jtag_open(void);
75 /* static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag); */
76 static void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag);
77 static unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen);
78
79 static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char *buffer, int size);
80 static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char *buffer, int size);
81 static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char *buffer, int size);
82 static void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan);
83
84 static char tms_chain[64];
85 static int tms_chain_index;
86
87 static void usbprog_jtag_tms_collect(char tms_scan);
88 static void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag);
89
90 static void usbprog_write(int tck, int tms, int tdi);
91 static void usbprog_reset(int trst, int srst);
92
93 static void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction);
94 static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag, unsigned char value);
95 /* static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag); */
96 static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag, int bit, int value);
97 /* static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit); */
98
99 static int usbprog_execute_queue(void)
100 {
101 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
102 int scan_size;
103 enum scan_type type;
104 uint8_t *buffer;
105
106 while (cmd) {
107 switch (cmd->type) {
108 case JTAG_RESET:
109 #ifdef _DEBUG_JTAG_IO_
110 LOG_DEBUG("reset trst: %i srst %i",
111 cmd->cmd.reset->trst,
112 cmd->cmd.reset->srst);
113 #endif
114 if (cmd->cmd.reset->trst == 1)
115 tap_set_state(TAP_RESET);
116 usbprog_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
117 break;
118 case JTAG_RUNTEST:
119 #ifdef _DEBUG_JTAG_IO_
120 LOG_DEBUG("runtest %i cycles, end in %i",
121 cmd->cmd.runtest->num_cycles,
122 cmd->cmd.runtest->end_state);
123 #endif
124 usbprog_end_state(cmd->cmd.runtest->end_state);
125 usbprog_runtest(cmd->cmd.runtest->num_cycles);
126 break;
127 case JTAG_TLR_RESET:
128 #ifdef _DEBUG_JTAG_IO_
129 LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
130 #endif
131 usbprog_end_state(cmd->cmd.statemove->end_state);
132 usbprog_state_move();
133 break;
134 case JTAG_PATHMOVE:
135 #ifdef _DEBUG_JTAG_IO_
136 LOG_DEBUG("pathmove: %i states, end in %i",
137 cmd->cmd.pathmove->num_states,
138 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
139 #endif
140 usbprog_path_move(cmd->cmd.pathmove);
141 break;
142 case JTAG_SCAN:
143 #ifdef _DEBUG_JTAG_IO_
144 LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
145 #endif
146 usbprog_end_state(cmd->cmd.scan->end_state);
147 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
148 type = jtag_scan_type(cmd->cmd.scan);
149 usbprog_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
150 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
151 return ERROR_JTAG_QUEUE_FAILED;
152 if (buffer)
153 free(buffer);
154 break;
155 case JTAG_SLEEP:
156 #ifdef _DEBUG_JTAG_IO_
157 LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
158 #endif
159 jtag_sleep(cmd->cmd.sleep->us);
160 break;
161 default:
162 LOG_ERROR("BUG: unknown JTAG command type encountered");
163 exit(-1);
164 }
165
166 cmd = cmd->next;
167 }
168
169 return ERROR_OK;
170 }
171
172 static int usbprog_init(void)
173 {
174 usbprog_jtag_handle = usbprog_jtag_open();
175
176 tms_chain_index = 0;
177 if (usbprog_jtag_handle == 0) {
178 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
179 return ERROR_JTAG_INIT_FAILED;
180 }
181
182 LOG_INFO("USB JTAG Interface ready!");
183
184 usbprog_jtag_init(usbprog_jtag_handle);
185 usbprog_reset(0, 0);
186 usbprog_write(0, 0, 0);
187
188 return ERROR_OK;
189 }
190
191 static int usbprog_quit(void)
192 {
193 return ERROR_OK;
194 }
195
196 /*************** jtag execute commands **********************/
197 static void usbprog_end_state(tap_state_t state)
198 {
199 if (tap_is_state_stable(state))
200 tap_set_end_state(state);
201 else {
202 LOG_ERROR("BUG: %i is not a valid end state", state);
203 exit(-1);
204 }
205 }
206
207 static void usbprog_state_move(void)
208 {
209 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
210
211 usbprog_jtag_write_tms(usbprog_jtag_handle, (char)tms_scan);
212
213 tap_set_state(tap_get_end_state());
214 }
215
216 static void usbprog_path_move(struct pathmove_command *cmd)
217 {
218 int num_states = cmd->num_states;
219 int state_count;
220
221 /* There may be queued transitions, and before following a specified
222 path, we must flush those queued transitions */
223 usbprog_jtag_tms_send(usbprog_jtag_handle);
224
225 state_count = 0;
226 while (num_states) {
227 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count]) {
228 /* LOG_INFO("1"); */
229 usbprog_write(0, 0, 0);
230 usbprog_write(1, 0, 0);
231 } else if (tap_state_transition(tap_get_state(),
232 true) == cmd->path[state_count]) {
233 /* LOG_INFO("2"); */
234 usbprog_write(0, 1, 0);
235 usbprog_write(1, 1, 0);
236 } else {
237 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
238 tap_state_name(tap_get_state()),
239 tap_state_name(cmd->path[state_count]));
240 exit(-1);
241 }
242
243 tap_set_state(cmd->path[state_count]);
244 state_count++;
245 num_states--;
246 }
247
248 tap_set_end_state(tap_get_state());
249 }
250
251 static void usbprog_runtest(int num_cycles)
252 {
253 int i;
254
255 /* only do a state_move when we're not already in IDLE */
256 if (tap_get_state() != TAP_IDLE) {
257 usbprog_end_state(TAP_IDLE);
258 usbprog_state_move();
259 }
260
261 /* execute num_cycles */
262 if (num_cycles > 0) {
263 usbprog_jtag_tms_send(usbprog_jtag_handle);
264 usbprog_write(0, 0, 0);
265 } else {
266 usbprog_jtag_tms_send(usbprog_jtag_handle);
267 /* LOG_INFO("NUM CYCLES %i",num_cycles); */
268 }
269
270 for (i = 0; i < num_cycles; i++) {
271 usbprog_write(1, 0, 0);
272 usbprog_write(0, 0, 0);
273 }
274
275 #ifdef _DEBUG_JTAG_IO_
276 LOG_DEBUG("runtest: cur_state %s end_state %s", tap_state_name(
277 tap_get_state()), tap_state_name(tap_get_end_state()));
278 #endif
279
280 /* finish in end_state */
281 /*
282 usbprog_end_state(saved_end_state);
283 if (tap_get_state() != tap_get_end_state())
284 usbprog_state_move();
285 */
286 }
287
288 static void usbprog_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
289 {
290 tap_state_t saved_end_state = tap_get_end_state();
291
292 if (ir_scan)
293 usbprog_end_state(TAP_IRSHIFT);
294 else
295 usbprog_end_state(TAP_DRSHIFT);
296
297 /* Only move if we're not already there */
298 if (tap_get_state() != tap_get_end_state())
299 usbprog_state_move();
300
301 usbprog_end_state(saved_end_state);
302
303 usbprog_jtag_tms_send(usbprog_jtag_handle);
304
305 void (*f)(struct usbprog_jtag *usbprog_jtag, char *buffer_local, int size);
306 switch (type) {
307 case SCAN_OUT:
308 f = &usbprog_jtag_write_tdi;
309 break;
310 case SCAN_IN:
311 f = &usbprog_jtag_read_tdo;
312 break;
313 case SCAN_IO:
314 f = &usbprog_jtag_write_and_read;
315 break;
316 default:
317 LOG_ERROR("unknown scan type: %i", type);
318 exit(-1);
319 }
320 f(usbprog_jtag_handle, (char *)buffer, scan_size);
321
322 /* The adapter does the transition to PAUSE internally */
323 if (ir_scan)
324 tap_set_state(TAP_IRPAUSE);
325 else
326 tap_set_state(TAP_DRPAUSE);
327
328 if (tap_get_state() != tap_get_end_state())
329 usbprog_state_move();
330 }
331
332 /*************** jtag wrapper functions *********************/
333
334 static void usbprog_write(int tck, int tms, int tdi)
335 {
336 unsigned char output_value = 0x00;
337
338 if (tms)
339 output_value |= (1 << TMS_BIT);
340 if (tdi)
341 output_value |= (1 << TDI_BIT);
342 if (tck)
343 output_value |= (1 << TCK_BIT);
344
345 usbprog_jtag_write_slice(usbprog_jtag_handle, output_value);
346 }
347
348 /* (1) assert or (0) deassert reset lines */
349 static void usbprog_reset(int trst, int srst)
350 {
351 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
352
353 if (trst)
354 usbprog_jtag_set_bit(usbprog_jtag_handle, 5, 0);
355 else
356 usbprog_jtag_set_bit(usbprog_jtag_handle, 5, 1);
357
358 if (srst)
359 usbprog_jtag_set_bit(usbprog_jtag_handle, 4, 0);
360 else
361 usbprog_jtag_set_bit(usbprog_jtag_handle, 4, 1);
362 }
363
364 /*************** jtag lowlevel functions ********************/
365
366 struct usb_bus *busses;
367
368 struct usbprog_jtag *usbprog_jtag_open(void)
369 {
370 usb_set_debug(10);
371 usb_init();
372
373 const uint16_t vids[] = { VID, 0 };
374 const uint16_t pids[] = { PID, 0 };
375 struct usb_dev_handle *dev;
376 if (jtag_usb_open(vids, pids, &dev) != ERROR_OK)
377 return NULL;
378
379 struct usbprog_jtag *tmp = malloc(sizeof(struct usbprog_jtag));
380 tmp->usb_handle = dev;
381
382 usb_set_configuration(dev, 1);
383 usb_claim_interface(dev, 0);
384 usb_set_altinterface(dev, 0);
385
386 return tmp;
387 }
388
389 #if 0
390 static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag)
391 {
392 usb_close(usbprog_jtag->usb_handle);
393 free(usbprog_jtag);
394 }
395 #endif
396
397 static unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen)
398 {
399 int res = usb_bulk_write(usbprog_jtag->usb_handle, 3, msg, msglen, 100);
400 if ((msg[0] == 2) || (msg[0] == 1) || (msg[0] == 4) || (msg[0] == 0) || \
401 (msg[0] == 6) || (msg[0] == 0x0A) || (msg[0] == 9))
402 return 1;
403 if (res == msglen) {
404 /* LOG_INFO("HALLLLOOO %i",(int)msg[0]); */
405 res = usb_bulk_read(usbprog_jtag->usb_handle, 0x82, msg, 2, 100);
406 if (res > 0)
407 return (unsigned char)msg[1];
408 else
409 return -1;
410 } else
411 return -1;
412 return 0;
413 }
414
415 static void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag)
416 {
417 usbprog_jtag_set_direction(usbprog_jtag, 0xFE);
418 }
419
420 static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char *buffer, int size)
421 {
422 char tmp[64]; /* fastes packet size for usb controller */
423 int send_bits, bufindex = 0, fillindex = 0, i, loops;
424
425 char swap;
426 /* 61 byte can be transfered (488 bit) */
427
428 while (size > 0) {
429 if (size > 488) {
430 send_bits = 488;
431 size = size - 488;
432 loops = 61;
433 } else {
434 send_bits = size;
435 loops = size / 8;
436 loops++;
437 size = 0;
438 }
439 tmp[0] = WRITE_AND_READ;
440 tmp[1] = (char)(send_bits >> 8); /* high */
441 tmp[2] = (char)(send_bits); /* low */
442
443 for (i = 0; i < loops; i++) {
444 tmp[3 + i] = buffer[bufindex];
445 bufindex++;
446 }
447
448 if (usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000) == 64) {
449 /* LOG_INFO("HALLLLOOO2 %i",(int)tmp[0]); */
450 usleep(1);
451 int timeout = 0;
452 while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 1000) < 1) {
453 timeout++;
454 if (timeout > 10)
455 break;
456 }
457
458 for (i = 0; i < loops; i++) {
459 swap = tmp[3 + i];
460 buffer[fillindex++] = swap;
461 }
462 }
463 }
464 }
465
466 static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char *buffer, int size)
467 {
468 char tmp[64]; /* fastes packet size for usb controller */
469 int send_bits, fillindex = 0, i, loops;
470
471 char swap;
472 /* 61 byte can be transfered (488 bit) */
473
474 while (size > 0) {
475 if (size > 488) {
476 send_bits = 488;
477 size = size - 488;
478 loops = 61;
479 } else {
480 send_bits = size;
481 loops = size / 8;
482 loops++;
483 size = 0;
484 }
485 tmp[0] = WRITE_AND_READ;
486 tmp[1] = (char)(send_bits >> 8); /* high */
487 tmp[2] = (char)(send_bits); /* low */
488
489 usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 3, 1000);
490
491 /* LOG_INFO("HALLLLOOO3 %i",(int)tmp[0]); */
492 int timeout = 0;
493 usleep(1);
494 while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 10) < 1) {
495 timeout++;
496 if (timeout > 10)
497 break;
498 }
499
500 for (i = 0; i < loops; i++) {
501 swap = tmp[3 + i];
502 buffer[fillindex++] = swap;
503 }
504 }
505 }
506
507 static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char *buffer, int size)
508 {
509 char tmp[64]; /* fastes packet size for usb controller */
510 int send_bits, bufindex = 0, i, loops;
511
512 /* 61 byte can be transfered (488 bit) */
513 while (size > 0) {
514 if (size > 488) {
515 send_bits = 488;
516 size = size - 488;
517 loops = 61;
518 } else {
519 send_bits = size;
520 loops = size/8;
521 /* if (loops == 0) */
522 loops++;
523 size = 0;
524 }
525 tmp[0] = WRITE_TDI;
526 tmp[1] = (char)(send_bits >> 8); /* high */
527 tmp[2] = (char)(send_bits); /* low */
528
529 for (i = 0; i < loops; i++) {
530 tmp[3 + i] = buffer[bufindex];
531 bufindex++;
532 }
533 usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000);
534 }
535 }
536
537 static void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan)
538 {
539 usbprog_jtag_tms_collect(tms_scan);
540 }
541
542 static void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction)
543 {
544 char tmp[2];
545 tmp[0] = PORT_DIRECTION;
546 tmp[1] = (char)direction;
547 usbprog_jtag_message(usbprog_jtag, tmp, 2);
548 }
549
550 static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag, unsigned char value)
551 {
552 char tmp[2];
553 tmp[0] = PORT_SET;
554 tmp[1] = (char)value;
555 usbprog_jtag_message(usbprog_jtag, tmp, 2);
556 }
557
558 #if 0
559 static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag)
560 {
561 char tmp[2];
562 tmp[0] = PORT_GET;
563 tmp[1] = 0x00;
564 return usbprog_jtag_message(usbprog_jtag, tmp, 2);
565 }
566 #endif
567
568 static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag, int bit, int value)
569 {
570 char tmp[3];
571 tmp[0] = PORT_SETBIT;
572 tmp[1] = (char)bit;
573 if (value == 1)
574 tmp[2] = 0x01;
575 else
576 tmp[2] = 0x00;
577 usbprog_jtag_message(usbprog_jtag, tmp, 3);
578 }
579
580 #if 0
581 static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit)
582 {
583 char tmp[2];
584 tmp[0] = PORT_GETBIT;
585 tmp[1] = (char)bit;
586
587 if (usbprog_jtag_message(usbprog_jtag, tmp, 2) > 0)
588 return 1;
589 else
590 return 0;
591 }
592 #endif
593
594 static void usbprog_jtag_tms_collect(char tms_scan)
595 {
596 tms_chain[tms_chain_index] = tms_scan;
597 tms_chain_index++;
598 }
599
600 static void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag)
601 {
602 int i;
603 /* LOG_INFO("TMS SEND"); */
604 if (tms_chain_index > 0) {
605 char tmp[tms_chain_index + 2];
606 tmp[0] = WRITE_TMS_CHAIN;
607 tmp[1] = (char)(tms_chain_index);
608 for (i = 0; i < tms_chain_index + 1; i++)
609 tmp[2 + i] = tms_chain[i];
610 usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, tms_chain_index + 2, 1000);
611 tms_chain_index = 0;
612 }
613 }
614
615 struct jtag_interface usbprog_interface = {
616 .name = "usbprog",
617
618 .execute_queue = usbprog_execute_queue,
619 .init = usbprog_init,
620 .quit = usbprog_quit
621 };

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)