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

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)