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

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)