libusb: idProduct of USB device may be zero
[openocd.git] / src / jtag / drivers / buspirate.c
1 /***************************************************************************
2 * Copyright (C) 2010 by Michal Demin *
3 * based on usbprog.c and arm-jtag-ew.c *
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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <jtag/interface.h>
26 #include <jtag/commands.h>
27
28 #include <termios.h>
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31
32 #undef DEBUG_SERIAL
33 /*#define DEBUG_SERIAL */
34 static int buspirate_execute_queue(void);
35 static int buspirate_speed(int speed);
36 static int buspirate_khz(int khz, int *jtag_speed);
37 static int buspirate_init(void);
38 static int buspirate_quit(void);
39
40 static void buspirate_end_state(tap_state_t state);
41 static void buspirate_state_move(void);
42 static void buspirate_path_move(int num_states, tap_state_t *path);
43 static void buspirate_runtest(int num_cycles);
44 static void buspirate_scan(bool ir_scan, enum scan_type type,
45 uint8_t *buffer, int scan_size, struct scan_command *command);
46
47 #define CMD_UNKNOWN 0x00
48 #define CMD_PORT_MODE 0x01
49 #define CMD_FEATURE 0x02
50 #define CMD_READ_ADCS 0x03
51 /*#define CMD_TAP_SHIFT 0x04 // old protocol */
52 #define CMD_TAP_SHIFT 0x05
53 #define CMD_ENTER_OOCD 0x06
54 #define CMD_UART_SPEED 0x07
55 #define CMD_JTAG_SPEED 0x08
56
57 /* Not all OSes have this speed defined */
58 #if !defined(B1000000)
59 #define B1000000 0010010
60 #endif
61
62 enum {
63 MODE_HIZ = 0,
64 MODE_JTAG = 1, /* push-pull outputs */
65 MODE_JTAG_OD = 2, /* open-drain outputs */
66 };
67
68 enum {
69 FEATURE_LED = 0x01,
70 FEATURE_VREG = 0x02,
71 FEATURE_TRST = 0x04,
72 FEATURE_SRST = 0x08,
73 FEATURE_PULLUP = 0x10
74 };
75
76 enum {
77 ACTION_DISABLE = 0,
78 ACTION_ENABLE = 1
79 };
80
81 enum {
82 SERIAL_NORMAL = 0,
83 SERIAL_FAST = 1
84 };
85
86 static int buspirate_fd = -1;
87 static int buspirate_pinmode = MODE_JTAG_OD;
88 static int buspirate_baudrate = SERIAL_NORMAL;
89 static int buspirate_vreg;
90 static int buspirate_pullup;
91 static char *buspirate_port;
92
93 /* TAP interface */
94 static void buspirate_tap_init(void);
95 static int buspirate_tap_execute(void);
96 static void buspirate_tap_append(int tms, int tdi);
97 static void buspirate_tap_append_scan(int length, uint8_t *buffer,
98 struct scan_command *command);
99 static void buspirate_tap_make_space(int scan, int bits);
100
101 static void buspirate_reset(int trst, int srst);
102
103 /* low level interface */
104 static void buspirate_jtag_reset(int);
105 static void buspirate_jtag_enable(int);
106 static unsigned char buspirate_jtag_command(int, char *, int);
107 static void buspirate_jtag_set_speed(int, char);
108 static void buspirate_jtag_set_mode(int, char);
109 static void buspirate_jtag_set_feature(int, char, char);
110 static void buspirate_jtag_get_adcs(int);
111
112 /* low level HW communication interface */
113 static int buspirate_serial_open(char *port);
114 static int buspirate_serial_setspeed(int fd, char speed);
115 static int buspirate_serial_write(int fd, char *buf, int size);
116 static int buspirate_serial_read(int fd, char *buf, int size);
117 static void buspirate_serial_close(int fd);
118 static void buspirate_print_buffer(char *buf, int size);
119
120 static int buspirate_speed(int speed)
121 {
122 /* TODO */
123 LOG_INFO("Want to set speed to %dkHz, but not implemented yet", speed);
124 return ERROR_OK;
125 }
126
127 static int buspirate_khz(int khz, int *jtag_speed)
128 {
129 *jtag_speed = khz;
130 return ERROR_OK;
131 }
132
133 static int buspirate_execute_queue(void)
134 {
135 /* currently processed command */
136 struct jtag_command *cmd = jtag_command_queue;
137 int scan_size;
138 enum scan_type type;
139 uint8_t *buffer;
140
141 while (cmd) {
142 switch (cmd->type) {
143 case JTAG_RUNTEST:
144 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
145 cmd->cmd.runtest->num_cycles,
146 tap_state_name(cmd->cmd.runtest
147 ->end_state));
148 buspirate_end_state(cmd->cmd.runtest
149 ->end_state);
150 buspirate_runtest(cmd->cmd.runtest
151 ->num_cycles);
152 break;
153 case JTAG_TLR_RESET:
154 DEBUG_JTAG_IO("statemove end in %s",
155 tap_state_name(cmd->cmd.statemove
156 ->end_state));
157 buspirate_end_state(cmd->cmd.statemove
158 ->end_state);
159 buspirate_state_move();
160 break;
161 case JTAG_PATHMOVE:
162 DEBUG_JTAG_IO("pathmove: %i states, end in %s",
163 cmd->cmd.pathmove->num_states,
164 tap_state_name(cmd->cmd.pathmove
165 ->path[cmd->cmd.pathmove
166 ->num_states - 1]));
167 buspirate_path_move(cmd->cmd.pathmove
168 ->num_states,
169 cmd->cmd.pathmove->path);
170 break;
171 case JTAG_SCAN:
172 DEBUG_JTAG_IO("scan end in %s",
173 tap_state_name(cmd->cmd.scan
174 ->end_state));
175
176 buspirate_end_state(cmd->cmd.scan
177 ->end_state);
178
179 scan_size = jtag_build_buffer(cmd->cmd.scan,
180 &buffer);
181 type = jtag_scan_type(cmd->cmd.scan);
182 buspirate_scan(cmd->cmd.scan->ir_scan, type,
183 buffer, scan_size, cmd->cmd.scan);
184
185 break;
186 case JTAG_RESET:
187 DEBUG_JTAG_IO("reset trst: %i srst %i",
188 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
189
190 /* flush buffers, so we can reset */
191 buspirate_tap_execute();
192
193 if (cmd->cmd.reset->trst == 1)
194 tap_set_state(TAP_RESET);
195 buspirate_reset(cmd->cmd.reset->trst,
196 cmd->cmd.reset->srst);
197 break;
198 case JTAG_SLEEP:
199 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
200 buspirate_tap_execute();
201 jtag_sleep(cmd->cmd.sleep->us);
202 break;
203 default:
204 LOG_ERROR("BUG: unknown JTAG command type encountered");
205 exit(-1);
206 }
207
208 cmd = cmd->next;
209 }
210
211 return buspirate_tap_execute();
212 }
213
214 static int buspirate_init(void)
215 {
216 if (buspirate_port == NULL) {
217 LOG_ERROR("You need to specify the serial port!");
218 return ERROR_JTAG_INIT_FAILED;
219 }
220
221 buspirate_fd = buspirate_serial_open(buspirate_port);
222 if (buspirate_fd == -1) {
223 LOG_ERROR("Could not open serial port");
224 return ERROR_JTAG_INIT_FAILED;
225 }
226
227 buspirate_serial_setspeed(buspirate_fd, SERIAL_NORMAL);
228
229 buspirate_jtag_enable(buspirate_fd);
230
231 if (buspirate_baudrate != SERIAL_NORMAL)
232 buspirate_jtag_set_speed(buspirate_fd, SERIAL_FAST);
233
234 LOG_INFO("Buspirate Interface ready!");
235
236 buspirate_tap_init();
237 buspirate_jtag_set_mode(buspirate_fd, buspirate_pinmode);
238 buspirate_jtag_set_feature(buspirate_fd, FEATURE_VREG,
239 (buspirate_vreg == 1) ? ACTION_ENABLE : ACTION_DISABLE);
240 buspirate_jtag_set_feature(buspirate_fd, FEATURE_PULLUP,
241 (buspirate_pullup == 1) ? ACTION_ENABLE : ACTION_DISABLE);
242 buspirate_reset(0, 0);
243
244 return ERROR_OK;
245 }
246
247 static int buspirate_quit(void)
248 {
249 LOG_INFO("Shutting down buspirate.");
250 buspirate_jtag_set_mode(buspirate_fd, MODE_HIZ);
251
252 buspirate_jtag_set_speed(buspirate_fd, SERIAL_NORMAL);
253 buspirate_jtag_reset(buspirate_fd);
254
255 buspirate_serial_close(buspirate_fd);
256
257 if (buspirate_port) {
258 free(buspirate_port);
259 buspirate_port = NULL;
260 }
261 return ERROR_OK;
262 }
263
264 /* openocd command interface */
265 COMMAND_HANDLER(buspirate_handle_adc_command)
266 {
267 if (buspirate_fd == -1)
268 return ERROR_OK;
269
270 /* send the command */
271 buspirate_jtag_get_adcs(buspirate_fd);
272
273 return ERROR_OK;
274
275 }
276
277 COMMAND_HANDLER(buspirate_handle_vreg_command)
278 {
279 if (CMD_ARGC < 1)
280 return ERROR_COMMAND_SYNTAX_ERROR;
281
282 if (atoi(CMD_ARGV[0]) == 1)
283 buspirate_vreg = 1;
284 else if (atoi(CMD_ARGV[0]) == 0)
285 buspirate_vreg = 0;
286 else
287 LOG_ERROR("usage: buspirate_vreg <1|0>");
288
289 return ERROR_OK;
290
291 }
292
293 COMMAND_HANDLER(buspirate_handle_pullup_command)
294 {
295 if (CMD_ARGC < 1)
296 return ERROR_COMMAND_SYNTAX_ERROR;
297
298 if (atoi(CMD_ARGV[0]) == 1)
299 buspirate_pullup = 1;
300 else if (atoi(CMD_ARGV[0]) == 0)
301 buspirate_pullup = 0;
302 else
303 LOG_ERROR("usage: buspirate_pullup <1|0>");
304
305 return ERROR_OK;
306
307 }
308
309 COMMAND_HANDLER(buspirate_handle_led_command)
310 {
311 if (CMD_ARGC < 1)
312 return ERROR_COMMAND_SYNTAX_ERROR;
313
314 if (atoi(CMD_ARGV[0]) == 1) {
315 /* enable led */
316 buspirate_jtag_set_feature(buspirate_fd, FEATURE_LED,
317 ACTION_ENABLE);
318 } else if (atoi(CMD_ARGV[0]) == 0) {
319 /* disable led */
320 buspirate_jtag_set_feature(buspirate_fd, FEATURE_LED,
321 ACTION_DISABLE);
322 } else {
323 LOG_ERROR("usage: buspirate_led <1|0>");
324 }
325
326 return ERROR_OK;
327
328 }
329
330 COMMAND_HANDLER(buspirate_handle_mode_command)
331 {
332 if (CMD_ARGC < 1)
333 return ERROR_COMMAND_SYNTAX_ERROR;
334
335 if (CMD_ARGV[0][0] == 'n')
336 buspirate_pinmode = MODE_JTAG;
337 else if (CMD_ARGV[0][0] == 'o')
338 buspirate_pinmode = MODE_JTAG_OD;
339 else
340 LOG_ERROR("usage: buspirate_mode <normal|open-drain>");
341
342 return ERROR_OK;
343
344 }
345
346 COMMAND_HANDLER(buspirate_handle_speed_command)
347 {
348 if (CMD_ARGC < 1)
349 return ERROR_COMMAND_SYNTAX_ERROR;
350
351 if (CMD_ARGV[0][0] == 'n')
352 buspirate_baudrate = SERIAL_NORMAL;
353 else if (CMD_ARGV[0][0] == 'f')
354 buspirate_baudrate = SERIAL_FAST;
355 else
356 LOG_ERROR("usage: buspirate_speed <normal|fast>");
357
358 return ERROR_OK;
359
360 }
361
362 COMMAND_HANDLER(buspirate_handle_port_command)
363 {
364 if (CMD_ARGC < 1)
365 return ERROR_COMMAND_SYNTAX_ERROR;
366
367 if (buspirate_port == NULL)
368 buspirate_port = strdup(CMD_ARGV[0]);
369
370 return ERROR_OK;
371
372 }
373
374 static const struct command_registration buspirate_command_handlers[] = {
375 {
376 .name = "buspirate_adc",
377 .handler = &buspirate_handle_adc_command,
378 .mode = COMMAND_EXEC,
379 .help = "reads voltages on adc pins",
380 },
381 {
382 .name = "buspirate_vreg",
383 .usage = "<1|0>",
384 .handler = &buspirate_handle_vreg_command,
385 .mode = COMMAND_CONFIG,
386 .help = "changes the state of voltage regulators",
387 },
388 {
389 .name = "buspirate_pullup",
390 .usage = "<1|0>",
391 .handler = &buspirate_handle_pullup_command,
392 .mode = COMMAND_CONFIG,
393 .help = "changes the state of pullup",
394 },
395 {
396 .name = "buspirate_led",
397 .usage = "<1|0>",
398 .handler = &buspirate_handle_led_command,
399 .mode = COMMAND_EXEC,
400 .help = "changes the state of led",
401 },
402 {
403 .name = "buspirate_speed",
404 .usage = "<normal|fast>",
405 .handler = &buspirate_handle_speed_command,
406 .mode = COMMAND_CONFIG,
407 .help = "speed of the interface",
408 },
409 {
410 .name = "buspirate_mode",
411 .usage = "<normal|open-drain>",
412 .handler = &buspirate_handle_mode_command,
413 .mode = COMMAND_CONFIG,
414 .help = "pin mode of the interface",
415 },
416 {
417 .name = "buspirate_port",
418 .usage = "/dev/ttyUSB0",
419 .handler = &buspirate_handle_port_command,
420 .mode = COMMAND_CONFIG,
421 .help = "name of the serial port to open",
422 },
423 COMMAND_REGISTRATION_DONE
424 };
425
426 struct jtag_interface buspirate_interface = {
427 .name = "buspirate",
428 .execute_queue = buspirate_execute_queue,
429 .speed = buspirate_speed,
430 .khz = buspirate_khz,
431 .commands = buspirate_command_handlers,
432 .init = buspirate_init,
433 .quit = buspirate_quit
434 };
435
436 /*************** jtag execute commands **********************/
437 static void buspirate_end_state(tap_state_t state)
438 {
439 if (tap_is_state_stable(state))
440 tap_set_end_state(state);
441 else {
442 LOG_ERROR("BUG: %i is not a valid end state", state);
443 exit(-1);
444 }
445 }
446
447 static void buspirate_state_move(void)
448 {
449 int i = 0, tms = 0;
450 uint8_t tms_scan = tap_get_tms_path(tap_get_state(),
451 tap_get_end_state());
452 int tms_count = tap_get_tms_path_len(tap_get_state(),
453 tap_get_end_state());
454
455 for (i = 0; i < tms_count; i++) {
456 tms = (tms_scan >> i) & 1;
457 buspirate_tap_append(tms, 0);
458 }
459
460 tap_set_state(tap_get_end_state());
461 }
462
463 static void buspirate_path_move(int num_states, tap_state_t *path)
464 {
465 int i;
466
467 for (i = 0; i < num_states; i++) {
468 if (tap_state_transition(tap_get_state(), false) == path[i]) {
469 buspirate_tap_append(0, 0);
470 } else if (tap_state_transition(tap_get_state(), true)
471 == path[i]) {
472 buspirate_tap_append(1, 0);
473 } else {
474 LOG_ERROR("BUG: %s -> %s isn't a valid "
475 "TAP transition",
476 tap_state_name(tap_get_state()),
477 tap_state_name(path[i]));
478 exit(-1);
479 }
480
481 tap_set_state(path[i]);
482 }
483
484 tap_set_end_state(tap_get_state());
485 }
486
487 static void buspirate_runtest(int num_cycles)
488 {
489 int i;
490
491 tap_state_t saved_end_state = tap_get_end_state();
492
493 /* only do a state_move when we're not already in IDLE */
494 if (tap_get_state() != TAP_IDLE) {
495 buspirate_end_state(TAP_IDLE);
496 buspirate_state_move();
497 }
498
499 for (i = 0; i < num_cycles; i++)
500 buspirate_tap_append(0, 0);
501
502 DEBUG_JTAG_IO("runtest: cur_state %s end_state %s",
503 tap_state_name(tap_get_state()),
504 tap_state_name(tap_get_end_state()));
505
506 /* finish in end_state */
507 buspirate_end_state(saved_end_state);
508 if (tap_get_state() != tap_get_end_state())
509 buspirate_state_move();
510 }
511
512 static void buspirate_scan(bool ir_scan, enum scan_type type,
513 uint8_t *buffer, int scan_size, struct scan_command *command)
514 {
515 tap_state_t saved_end_state;
516
517 buspirate_tap_make_space(1, scan_size+8);
518 /* is 8 correct ? (2 moves = 16) */
519
520 saved_end_state = tap_get_end_state();
521
522 buspirate_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
523 buspirate_state_move();
524
525 buspirate_tap_append_scan(scan_size, buffer, command);
526
527 /* move to PAUSE */
528 buspirate_tap_append(0, 0);
529
530 /* restore the saved state */
531 buspirate_end_state(saved_end_state);
532 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
533
534 if (tap_get_state() != tap_get_end_state())
535 buspirate_state_move();
536 }
537
538
539 /************************* TAP related stuff **********/
540
541 #define BUSPIRATE_BUFFER_SIZE 1024
542 #define BUSPIRATE_MAX_PENDING_SCANS 32
543
544 static char tms_chain[BUSPIRATE_BUFFER_SIZE]; /* send */
545 static char tdi_chain[BUSPIRATE_BUFFER_SIZE]; /* send */
546 static int tap_chain_index;
547
548 struct pending_scan_result /* this was stolen from arm-jtag-ew */
549 {
550 int first; /* First bit position in tdo_buffer to read */
551 int length; /* Number of bits to read */
552 struct scan_command *command; /* Corresponding scan command */
553 uint8_t *buffer;
554 };
555
556 static struct pending_scan_result
557 tap_pending_scans[BUSPIRATE_MAX_PENDING_SCANS];
558 static int tap_pending_scans_num;
559
560 static void buspirate_tap_init(void)
561 {
562 tap_chain_index = 0;
563 tap_pending_scans_num = 0;
564 }
565
566 static int buspirate_tap_execute(void)
567 {
568 char tmp[4096];
569 uint8_t *in_buf;
570 int i;
571 int fill_index = 0;
572 int ret;
573 int bytes_to_send;
574
575 if (tap_chain_index <= 0)
576 return ERROR_OK;
577
578 LOG_DEBUG("executing tap num bits = %i scans = %i",
579 tap_chain_index, tap_pending_scans_num);
580
581 bytes_to_send = (tap_chain_index+7) / 8;
582
583 tmp[0] = CMD_TAP_SHIFT; /* this command expects number of bits */
584 tmp[1] = (char)(tap_chain_index >> 8); /* high */
585 tmp[2] = (char)(tap_chain_index); /* low */
586
587 fill_index = 3;
588 for (i = 0; i < bytes_to_send; i++) {
589 tmp[fill_index] = tdi_chain[i];
590 fill_index++;
591 tmp[fill_index] = tms_chain[i];
592 fill_index++;
593 }
594
595 ret = buspirate_serial_write(buspirate_fd, tmp, 3 + bytes_to_send*2);
596 if (ret != bytes_to_send*2+3) {
597 LOG_ERROR("error writing :(");
598 return ERROR_JTAG_DEVICE_ERROR;
599 }
600
601 ret = buspirate_serial_read(buspirate_fd, tmp, bytes_to_send + 3);
602 if (ret != bytes_to_send + 3) {
603 LOG_ERROR("error reading");
604 return ERROR_FAIL;
605 }
606 in_buf = (uint8_t *)(&tmp[3]);
607
608 /* parse the scans */
609 for (i = 0; i < tap_pending_scans_num; i++) {
610 uint8_t *buffer = tap_pending_scans[i].buffer;
611 int length = tap_pending_scans[i].length;
612 int first = tap_pending_scans[i].first;
613 struct scan_command *command = tap_pending_scans[i].command;
614
615 /* copy bits from buffer */
616 buf_set_buf(in_buf, first, buffer, 0, length);
617
618 /* return buffer to higher level */
619 if (jtag_read_buffer(buffer, command) != ERROR_OK) {
620 buspirate_tap_init();
621 return ERROR_JTAG_QUEUE_FAILED;
622 }
623
624 free(buffer);
625 }
626 tap_pending_scans_num = 0;
627 tap_chain_index = 0;
628 return ERROR_OK;
629 }
630
631 static void buspirate_tap_make_space(int scans, int bits)
632 {
633 int have_scans = BUSPIRATE_MAX_PENDING_SCANS - tap_pending_scans_num;
634 int have_bits = BUSPIRATE_BUFFER_SIZE * 8 - tap_chain_index;
635
636 if ((have_scans < scans) || (have_bits < bits))
637 buspirate_tap_execute();
638 }
639
640 static void buspirate_tap_append(int tms, int tdi)
641 {
642 int chain_index;
643
644 buspirate_tap_make_space(0, 1);
645 chain_index = tap_chain_index / 8;
646
647 if (chain_index < BUSPIRATE_BUFFER_SIZE) {
648 int bit_index = tap_chain_index % 8;
649 uint8_t bit = 1 << bit_index;
650
651 if (tms)
652 tms_chain[chain_index] |= bit;
653 else
654 tms_chain[chain_index] &= ~bit;
655
656 if (tdi)
657 tdi_chain[chain_index] |= bit;
658 else
659 tdi_chain[chain_index] &= ~bit;
660
661 tap_chain_index++;
662 } else
663 LOG_ERROR("tap_chain overflow, bad things will happen");
664
665 }
666
667 static void buspirate_tap_append_scan(int length, uint8_t *buffer,
668 struct scan_command *command)
669 {
670 int i;
671 tap_pending_scans[tap_pending_scans_num].length = length;
672 tap_pending_scans[tap_pending_scans_num].buffer = buffer;
673 tap_pending_scans[tap_pending_scans_num].command = command;
674 tap_pending_scans[tap_pending_scans_num].first = tap_chain_index;
675
676 for (i = 0; i < length; i++) {
677 int tms = (i < length-1 ? 0 : 1);
678 int tdi = (buffer[i/8] >> (i%8)) & 1;
679 buspirate_tap_append(tms, tdi);
680 }
681 tap_pending_scans_num++;
682 }
683
684 /*************** jtag wrapper functions *********************/
685
686 /* (1) assert or (0) deassert reset lines */
687 static void buspirate_reset(int trst, int srst)
688 {
689 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
690
691 if (trst)
692 buspirate_jtag_set_feature(buspirate_fd,
693 FEATURE_TRST, ACTION_DISABLE);
694 else
695 buspirate_jtag_set_feature(buspirate_fd,
696 FEATURE_TRST, ACTION_ENABLE);
697
698 if (srst)
699 buspirate_jtag_set_feature(buspirate_fd,
700 FEATURE_SRST, ACTION_DISABLE);
701 else
702 buspirate_jtag_set_feature(buspirate_fd,
703 FEATURE_SRST, ACTION_ENABLE);
704 }
705
706 /*************** jtag lowlevel functions ********************/
707 static void buspirate_jtag_enable(int fd)
708 {
709 int ret;
710 char tmp[21] = { [0 ... 20] = 0x00 };
711 int done = 0;
712 int cmd_sent = 0;
713
714 LOG_DEBUG("Entering binary mode");
715 buspirate_serial_write(fd, tmp, 20);
716 usleep(10000);
717
718 /* reads 1 to n "BBIO1"s and one "OCD1" */
719 while (!done) {
720 ret = buspirate_serial_read(fd, tmp, 4);
721 if (ret != 4) {
722 LOG_ERROR("Buspirate error. Is binary"
723 "/OpenOCD support enabled?");
724 exit(-1);
725 }
726 if (strncmp(tmp, "BBIO", 4) == 0) {
727 ret = buspirate_serial_read(fd, tmp, 1);
728 if (ret != 1) {
729 LOG_ERROR("Buspirate did not answer correctly! "
730 "Do you have correct firmware?");
731 exit(-1);
732 }
733 if (tmp[0] != '1') {
734 LOG_ERROR("Unsupported binary protocol");
735 exit(-1);
736 }
737 if (cmd_sent == 0) {
738 cmd_sent = 1;
739 tmp[0] = CMD_ENTER_OOCD;
740 ret = buspirate_serial_write(fd, tmp, 1);
741 if (ret != 1) {
742 LOG_ERROR("error reading");
743 exit(-1);
744 }
745 }
746 } else if (strncmp(tmp, "OCD1", 4) == 0)
747 done = 1;
748 else {
749 LOG_ERROR("Buspirate did not answer correctly! "
750 "Do you have correct firmware?");
751 exit(-1);
752 }
753 }
754
755 }
756
757 static void buspirate_jtag_reset(int fd)
758 {
759 char tmp[5];
760
761 tmp[0] = 0x00; /* exit OCD1 mode */
762 buspirate_serial_write(fd, tmp, 1);
763 usleep(10000);
764 /* We ignore the return value here purposly, nothing we can do */
765 buspirate_serial_read(fd, tmp, 5);
766 if (strncmp(tmp, "BBIO1", 5) == 0) {
767 tmp[0] = 0x0F; /* reset BP */
768 buspirate_serial_write(fd, tmp, 1);
769 } else
770 LOG_ERROR("Unable to restart buspirate!");
771 }
772
773 static void buspirate_jtag_set_speed(int fd, char speed)
774 {
775 int ret;
776 char tmp[2];
777 char ack[2];
778
779 ack[0] = 0xAA;
780 ack[1] = 0x55;
781
782 tmp[0] = CMD_UART_SPEED;
783 tmp[1] = speed;
784 buspirate_jtag_command(fd, tmp, 2);
785
786 /* here the adapter changes speed, we need follow */
787 buspirate_serial_setspeed(fd, speed);
788
789 buspirate_serial_write(fd, ack, 2);
790 ret = buspirate_serial_read(fd, tmp, 2);
791 if (ret != 2) {
792 LOG_ERROR("Buspirate did not ack speed change");
793 exit(-1);
794 }
795 if ((tmp[0] != CMD_UART_SPEED) || (tmp[1] != speed)) {
796 LOG_ERROR("Buspirate did not reply as expected");
797 exit(-1);
798 }
799 LOG_INFO("Buspirate switched to %s mode",
800 (speed == SERIAL_NORMAL) ? "normal" : "FAST");
801 }
802
803
804 static void buspirate_jtag_set_mode(int fd, char mode)
805 {
806 char tmp[2];
807 tmp[0] = CMD_PORT_MODE;
808 tmp[1] = mode;
809 buspirate_jtag_command(fd, tmp, 2);
810 }
811
812 static void buspirate_jtag_set_feature(int fd, char feat, char action)
813 {
814 char tmp[3];
815 tmp[0] = CMD_FEATURE;
816 tmp[1] = feat; /* what */
817 tmp[2] = action; /* action */
818 buspirate_jtag_command(fd, tmp, 3);
819 }
820
821 static void buspirate_jtag_get_adcs(int fd)
822 {
823 uint8_t tmp[10];
824 uint16_t a, b, c, d;
825 tmp[0] = CMD_READ_ADCS;
826 buspirate_jtag_command(fd, (char *)tmp, 1);
827 a = tmp[2] << 8 | tmp[3];
828 b = tmp[4] << 8 | tmp[5];
829 c = tmp[6] << 8 | tmp[7];
830 d = tmp[8] << 8 | tmp[9];
831
832 LOG_INFO("ADC: ADC_Pin = %.02f VPullup = %.02f V33 = %.02f "
833 "V50 = %.02f",
834 ((float)a)/155.1515, ((float)b)/155.1515,
835 ((float)c)/155.1515, ((float)d)/155.1515);
836 }
837
838 static unsigned char buspirate_jtag_command(int fd,
839 char *cmd, int cmdlen)
840 {
841 int res;
842 int len = 0;
843
844 res = buspirate_serial_write(fd, cmd, cmdlen);
845
846 if ((cmd[0] == CMD_UART_SPEED)
847 || (cmd[0] == CMD_PORT_MODE)
848 || (cmd[0] == CMD_FEATURE)
849 || (cmd[0] == CMD_JTAG_SPEED))
850 return 1;
851
852 if (res == cmdlen) {
853 switch (cmd[0]) {
854 case CMD_READ_ADCS:
855 len = 10; /* 2*sizeof(char)+4*sizeof(uint16_t) */
856 break;
857 case CMD_TAP_SHIFT:
858 len = cmdlen;
859 break;
860 default:
861 LOG_INFO("Wrong !");
862 }
863 res = buspirate_serial_read(fd, cmd, len);
864 if (res > 0)
865 return (unsigned char)cmd[1];
866 else
867 return -1;
868 } else
869 return -1;
870 return 0;
871 }
872
873 /* low level serial port */
874 /* TODO add support for WIN32 and others ! */
875 static int buspirate_serial_open(char *port)
876 {
877 int fd;
878 fd = open(buspirate_port, O_RDWR | O_NOCTTY | O_NDELAY);
879 return fd;
880 }
881
882 static int buspirate_serial_setspeed(int fd, char speed)
883 {
884 struct termios t_opt;
885 speed_t baud = (speed == SERIAL_FAST) ? B1000000 : B115200;
886
887 /* set the serial port parameters */
888 fcntl(fd, F_SETFL, 0);
889 tcgetattr(fd, &t_opt);
890 cfsetispeed(&t_opt, baud);
891 cfsetospeed(&t_opt, baud);
892 t_opt.c_cflag |= (CLOCAL | CREAD);
893 t_opt.c_cflag &= ~PARENB;
894 t_opt.c_cflag &= ~CSTOPB;
895 t_opt.c_cflag &= ~CSIZE;
896 t_opt.c_cflag |= CS8;
897 t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
898 t_opt.c_iflag &= ~(IXON | IXOFF | IXANY);
899 t_opt.c_oflag &= ~OPOST;
900 t_opt.c_cc[VMIN] = 0;
901 t_opt.c_cc[VTIME] = 10;
902 tcflush(fd, TCIFLUSH);
903 tcsetattr(fd, TCSANOW, &t_opt);
904
905 return 0;
906 }
907
908 static int buspirate_serial_write(int fd, char *buf, int size)
909 {
910 int ret = 0;
911
912 ret = write(fd, buf, size);
913
914 LOG_DEBUG("size = %d ret = %d", size, ret);
915 buspirate_print_buffer(buf, size);
916
917 if (ret != size)
918 LOG_ERROR("Error sending data");
919
920 return ret;
921 }
922
923 static int buspirate_serial_read(int fd, char *buf, int size)
924 {
925 int len = 0;
926 int ret = 0;
927 int timeout = 0;
928
929 while (len < size) {
930 ret = read(fd, buf+len, size-len);
931 if (ret == -1)
932 return -1;
933
934 if (ret == 0) {
935 timeout++;
936
937 if (timeout >= 10)
938 break;
939
940 continue;
941 }
942
943 len += ret;
944 }
945
946 LOG_DEBUG("should have read = %d actual size = %d", size, len);
947 buspirate_print_buffer(buf, len);
948
949 if (len != size)
950 LOG_ERROR("Error reading data");
951
952 return len;
953 }
954
955 static void buspirate_serial_close(int fd)
956 {
957 close(fd);
958 }
959
960 #define LINE_SIZE 81
961 #define BYTES_PER_LINE 16
962 static void buspirate_print_buffer(char *buf, int size)
963 {
964 char line[LINE_SIZE];
965 char tmp[10];
966 int offset = 0;
967
968 line[0] = 0;
969 while (offset < size) {
970 snprintf(tmp, 5, "%02x ", (uint8_t)buf[offset]);
971 offset++;
972
973 strcat(line, tmp);
974
975 if (offset % BYTES_PER_LINE == 0) {
976 LOG_DEBUG("%s", line);
977 line[0] = 0;
978 }
979 }
980
981 if (line[0] != 0)
982 LOG_DEBUG("%s", line);
983 }

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)