1 /***************************************************************************
2 * Copyright (C) 2010 by Michal Demin *
3 * based on usbprog.c and arm-jtag-ew.c *
4 * Several fixes by R. Diez in 2013. *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 ***************************************************************************/
24 #include <jtag/interface.h>
25 #include <jtag/commands.h>
29 #include <sys/ioctl.h>
32 /*#define DEBUG_SERIAL */
33 static int buspirate_execute_queue(void);
34 static int buspirate_init(void);
35 static int buspirate_quit(void);
37 static void buspirate_end_state(tap_state_t state);
38 static void buspirate_state_move(void);
39 static void buspirate_path_move(int num_states, tap_state_t *path);
40 static void buspirate_runtest(int num_cycles);
41 static void buspirate_scan(bool ir_scan, enum scan_type type,
42 uint8_t *buffer, int scan_size, struct scan_command *command);
43 static void buspirate_stableclocks(int num_cycles);
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
55 /* Not all OSes have this speed defined */
56 #if !defined(B1000000)
57 #define B1000000 0010010
62 MODE_JTAG = 1, /* push-pull outputs */
63 MODE_JTAG_OD = 2, /* open-drain outputs */
84 static const cc_t SHORT_TIMEOUT = 1; /* Must be at least 1. */
85 static const cc_t NORMAL_TIMEOUT = 10;
87 static int buspirate_fd = -1;
88 static int buspirate_pinmode = MODE_JTAG_OD;
89 static int buspirate_baudrate = SERIAL_NORMAL;
90 static int buspirate_vreg;
91 static int buspirate_pullup;
92 static char *buspirate_port;
94 static enum tap_state last_tap_state = TAP_RESET;
98 static void buspirate_tap_init(void);
99 static int buspirate_tap_execute(void);
100 static void buspirate_tap_append(int tms, int tdi);
101 static void buspirate_tap_append_scan(int length, uint8_t *buffer,
102 struct scan_command *command);
103 static void buspirate_tap_make_space(int scan, int bits);
105 static void buspirate_reset(int trst, int srst);
107 /* low level interface */
108 static void buspirate_jtag_reset(int);
109 static void buspirate_jtag_enable(int);
110 static unsigned char buspirate_jtag_command(int, char *, int);
111 static void buspirate_jtag_set_speed(int, char);
112 static void buspirate_jtag_set_mode(int, char);
113 static void buspirate_jtag_set_feature(int, char, char);
114 static void buspirate_jtag_get_adcs(int);
116 /* low level HW communication interface */
117 static int buspirate_serial_open(char *port);
118 static int buspirate_serial_setspeed(int fd, char speed, cc_t timeout);
119 static int buspirate_serial_write(int fd, char *buf, int size);
120 static int buspirate_serial_read(int fd, char *buf, int size);
121 static void buspirate_serial_close(int fd);
122 static void buspirate_print_buffer(char *buf, int size);
124 static int buspirate_execute_queue(void)
126 /* currently processed command */
127 struct jtag_command *cmd = jtag_command_queue;
135 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
136 cmd->cmd.runtest->num_cycles,
137 tap_state_name(cmd->cmd.runtest
139 buspirate_end_state(cmd->cmd.runtest
141 buspirate_runtest(cmd->cmd.runtest
145 DEBUG_JTAG_IO("statemove end in %s",
146 tap_state_name(cmd->cmd.statemove
148 buspirate_end_state(cmd->cmd.statemove
150 buspirate_state_move();
153 DEBUG_JTAG_IO("pathmove: %i states, end in %s",
154 cmd->cmd.pathmove->num_states,
155 tap_state_name(cmd->cmd.pathmove
156 ->path[cmd->cmd.pathmove
158 buspirate_path_move(cmd->cmd.pathmove
160 cmd->cmd.pathmove->path);
163 DEBUG_JTAG_IO("scan end in %s",
164 tap_state_name(cmd->cmd.scan
167 buspirate_end_state(cmd->cmd.scan
170 scan_size = jtag_build_buffer(cmd->cmd.scan,
172 type = jtag_scan_type(cmd->cmd.scan);
173 buspirate_scan(cmd->cmd.scan->ir_scan, type,
174 buffer, scan_size, cmd->cmd.scan);
178 DEBUG_JTAG_IO("reset trst: %i srst %i",
179 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
181 /* flush buffers, so we can reset */
182 buspirate_tap_execute();
184 if (cmd->cmd.reset->trst == 1)
185 tap_set_state(TAP_RESET);
186 buspirate_reset(cmd->cmd.reset->trst,
187 cmd->cmd.reset->srst);
190 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
191 buspirate_tap_execute();
192 jtag_sleep(cmd->cmd.sleep->us);
194 case JTAG_STABLECLOCKS:
195 DEBUG_JTAG_IO("stable clock %i cycles", cmd->cmd.stableclocks->num_cycles);
196 buspirate_stableclocks(cmd->cmd.stableclocks->num_cycles);
199 LOG_ERROR("BUG: unknown JTAG command type encountered");
206 return buspirate_tap_execute();
210 /* Returns true if successful, false if error. */
212 static bool read_and_discard_all_data(const int fd)
214 /* LOG_INFO("Discarding any stale data from a previous connection..."); */
216 bool was_msg_already_printed = false;
219 char buffer[1024]; /* Any size will do, it's a trade-off between stack size and performance. */
221 const ssize_t read_count = read(fd, buffer, sizeof(buffer));
223 if (read_count == 0) {
224 /* This is the "end of file" or "connection closed at the other end" condition. */
228 if (read_count > 0) {
229 if (!was_msg_already_printed) {
230 LOG_INFO("Some stale data from a previous connection was discarded.");
231 was_msg_already_printed = true;
237 assert(read_count == -1); /* According to the specification. */
239 const int errno_code = errno;
241 if (errno_code == EINTR)
244 if (errno_code == EAGAIN ||
245 errno_code == EWOULDBLOCK) {
246 /* We know that the file descriptor has been opened with O_NONBLOCK or O_NDELAY,
247 and these codes mean that there is no data to read at present. */
251 /* Some other error has occurred. */
257 static int buspirate_init(void)
259 if (buspirate_port == NULL) {
260 LOG_ERROR("You need to specify the serial port!");
261 return ERROR_JTAG_INIT_FAILED;
264 buspirate_fd = buspirate_serial_open(buspirate_port);
265 if (buspirate_fd == -1) {
266 LOG_ERROR("Could not open serial port");
267 return ERROR_JTAG_INIT_FAILED;
270 /* The Operating System or the device itself may deliver stale data from the last connection,
271 so discard all available bytes right after the new connection has been established.
272 After all, we are implementing here a master/slave protocol, so the slave should have nothing
273 to say until the master sends the first command.
275 In the past, there was a tcflush() call in buspirate_serial_setspeed(), but that
276 was not enough. I guess you must actively read from the serial port to trigger any
277 data collection from the device and/or lower USB layers. If you disable the serial port
278 read timeout (if you set SHORT_TIMEOUT to 0), then the discarding does not work any more.
280 Note that we are lowering the serial port timeout for this first read operation,
281 otherwise the normal initialisation would be delayed for too long. */
283 if (-1 == buspirate_serial_setspeed(buspirate_fd, SERIAL_NORMAL, SHORT_TIMEOUT)) {
284 LOG_ERROR("Error configuring the serial port.");
285 return ERROR_JTAG_INIT_FAILED;
288 if (!read_and_discard_all_data(buspirate_fd)) {
289 LOG_ERROR("Error while attempting to discard any stale data right after establishing the connection.");
290 return ERROR_JTAG_INIT_FAILED;
293 if (-1 == buspirate_serial_setspeed(buspirate_fd, SERIAL_NORMAL, NORMAL_TIMEOUT)) {
294 LOG_ERROR("Error configuring the serial port.");
295 return ERROR_JTAG_INIT_FAILED;
298 buspirate_jtag_enable(buspirate_fd);
300 if (buspirate_baudrate != SERIAL_NORMAL)
301 buspirate_jtag_set_speed(buspirate_fd, SERIAL_FAST);
303 LOG_INFO("Buspirate Interface ready!");
305 buspirate_tap_init();
306 buspirate_jtag_set_mode(buspirate_fd, buspirate_pinmode);
307 buspirate_jtag_set_feature(buspirate_fd, FEATURE_VREG,
308 (buspirate_vreg == 1) ? ACTION_ENABLE : ACTION_DISABLE);
309 buspirate_jtag_set_feature(buspirate_fd, FEATURE_PULLUP,
310 (buspirate_pullup == 1) ? ACTION_ENABLE : ACTION_DISABLE);
311 buspirate_reset(0, 0);
316 static int buspirate_quit(void)
318 LOG_INFO("Shutting down buspirate.");
319 buspirate_jtag_set_mode(buspirate_fd, MODE_HIZ);
321 buspirate_jtag_set_speed(buspirate_fd, SERIAL_NORMAL);
322 buspirate_jtag_reset(buspirate_fd);
324 buspirate_serial_close(buspirate_fd);
326 if (buspirate_port) {
327 free(buspirate_port);
328 buspirate_port = NULL;
333 /* openocd command interface */
334 COMMAND_HANDLER(buspirate_handle_adc_command)
336 if (buspirate_fd == -1)
339 /* send the command */
340 buspirate_jtag_get_adcs(buspirate_fd);
346 COMMAND_HANDLER(buspirate_handle_vreg_command)
349 return ERROR_COMMAND_SYNTAX_ERROR;
351 if (atoi(CMD_ARGV[0]) == 1)
353 else if (atoi(CMD_ARGV[0]) == 0)
356 LOG_ERROR("usage: buspirate_vreg <1|0>");
362 COMMAND_HANDLER(buspirate_handle_pullup_command)
365 return ERROR_COMMAND_SYNTAX_ERROR;
367 if (atoi(CMD_ARGV[0]) == 1)
368 buspirate_pullup = 1;
369 else if (atoi(CMD_ARGV[0]) == 0)
370 buspirate_pullup = 0;
372 LOG_ERROR("usage: buspirate_pullup <1|0>");
378 COMMAND_HANDLER(buspirate_handle_led_command)
381 return ERROR_COMMAND_SYNTAX_ERROR;
383 if (atoi(CMD_ARGV[0]) == 1) {
385 buspirate_jtag_set_feature(buspirate_fd, FEATURE_LED,
387 } else if (atoi(CMD_ARGV[0]) == 0) {
389 buspirate_jtag_set_feature(buspirate_fd, FEATURE_LED,
392 LOG_ERROR("usage: buspirate_led <1|0>");
399 COMMAND_HANDLER(buspirate_handle_mode_command)
402 return ERROR_COMMAND_SYNTAX_ERROR;
404 if (CMD_ARGV[0][0] == 'n')
405 buspirate_pinmode = MODE_JTAG;
406 else if (CMD_ARGV[0][0] == 'o')
407 buspirate_pinmode = MODE_JTAG_OD;
409 LOG_ERROR("usage: buspirate_mode <normal|open-drain>");
415 COMMAND_HANDLER(buspirate_handle_speed_command)
418 return ERROR_COMMAND_SYNTAX_ERROR;
420 if (CMD_ARGV[0][0] == 'n')
421 buspirate_baudrate = SERIAL_NORMAL;
422 else if (CMD_ARGV[0][0] == 'f')
423 buspirate_baudrate = SERIAL_FAST;
425 LOG_ERROR("usage: buspirate_speed <normal|fast>");
431 COMMAND_HANDLER(buspirate_handle_port_command)
434 return ERROR_COMMAND_SYNTAX_ERROR;
436 if (buspirate_port == NULL)
437 buspirate_port = strdup(CMD_ARGV[0]);
443 static const struct command_registration buspirate_command_handlers[] = {
445 .name = "buspirate_adc",
446 .handler = &buspirate_handle_adc_command,
447 .mode = COMMAND_EXEC,
448 .help = "reads voltages on adc pins",
451 .name = "buspirate_vreg",
453 .handler = &buspirate_handle_vreg_command,
454 .mode = COMMAND_CONFIG,
455 .help = "changes the state of voltage regulators",
458 .name = "buspirate_pullup",
460 .handler = &buspirate_handle_pullup_command,
461 .mode = COMMAND_CONFIG,
462 .help = "changes the state of pullup",
465 .name = "buspirate_led",
467 .handler = &buspirate_handle_led_command,
468 .mode = COMMAND_EXEC,
469 .help = "changes the state of led",
472 .name = "buspirate_speed",
473 .usage = "<normal|fast>",
474 .handler = &buspirate_handle_speed_command,
475 .mode = COMMAND_CONFIG,
476 .help = "speed of the interface",
479 .name = "buspirate_mode",
480 .usage = "<normal|open-drain>",
481 .handler = &buspirate_handle_mode_command,
482 .mode = COMMAND_CONFIG,
483 .help = "pin mode of the interface",
486 .name = "buspirate_port",
487 .usage = "/dev/ttyUSB0",
488 .handler = &buspirate_handle_port_command,
489 .mode = COMMAND_CONFIG,
490 .help = "name of the serial port to open",
492 COMMAND_REGISTRATION_DONE
495 struct jtag_interface buspirate_interface = {
497 .execute_queue = buspirate_execute_queue,
498 .commands = buspirate_command_handlers,
499 .init = buspirate_init,
500 .quit = buspirate_quit
503 /*************** jtag execute commands **********************/
504 static void buspirate_end_state(tap_state_t state)
506 if (tap_is_state_stable(state))
507 tap_set_end_state(state);
509 LOG_ERROR("BUG: %i is not a valid end state", state);
514 static void buspirate_state_move(void)
517 uint8_t tms_scan = tap_get_tms_path(tap_get_state(),
518 tap_get_end_state());
519 int tms_count = tap_get_tms_path_len(tap_get_state(),
520 tap_get_end_state());
522 for (i = 0; i < tms_count; i++) {
523 tms = (tms_scan >> i) & 1;
524 buspirate_tap_append(tms, 0);
527 tap_set_state(tap_get_end_state());
530 static void buspirate_path_move(int num_states, tap_state_t *path)
534 for (i = 0; i < num_states; i++) {
535 if (tap_state_transition(tap_get_state(), false) == path[i]) {
536 buspirate_tap_append(0, 0);
537 } else if (tap_state_transition(tap_get_state(), true)
539 buspirate_tap_append(1, 0);
541 LOG_ERROR("BUG: %s -> %s isn't a valid "
543 tap_state_name(tap_get_state()),
544 tap_state_name(path[i]));
548 tap_set_state(path[i]);
551 tap_set_end_state(tap_get_state());
554 static void buspirate_runtest(int num_cycles)
558 tap_state_t saved_end_state = tap_get_end_state();
560 /* only do a state_move when we're not already in IDLE */
561 if (tap_get_state() != TAP_IDLE) {
562 buspirate_end_state(TAP_IDLE);
563 buspirate_state_move();
566 for (i = 0; i < num_cycles; i++)
567 buspirate_tap_append(0, 0);
569 DEBUG_JTAG_IO("runtest: cur_state %s end_state %s",
570 tap_state_name(tap_get_state()),
571 tap_state_name(tap_get_end_state()));
573 /* finish in end_state */
574 buspirate_end_state(saved_end_state);
575 if (tap_get_state() != tap_get_end_state())
576 buspirate_state_move();
579 static void buspirate_scan(bool ir_scan, enum scan_type type,
580 uint8_t *buffer, int scan_size, struct scan_command *command)
582 tap_state_t saved_end_state;
584 buspirate_tap_make_space(1, scan_size+8);
585 /* is 8 correct ? (2 moves = 16) */
587 saved_end_state = tap_get_end_state();
589 buspirate_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
591 /* Only move if we're not already there */
592 if (tap_get_state() != tap_get_end_state())
593 buspirate_state_move();
595 buspirate_tap_append_scan(scan_size, buffer, command);
598 buspirate_tap_append(0, 0);
600 /* restore the saved state */
601 buspirate_end_state(saved_end_state);
602 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
604 if (tap_get_state() != tap_get_end_state())
605 buspirate_state_move();
608 static void buspirate_stableclocks(int num_cycles)
611 int tms = (tap_get_state() == TAP_RESET ? 1 : 0);
613 buspirate_tap_make_space(0, num_cycles);
615 for (i = 0; i < num_cycles; i++)
616 buspirate_tap_append(tms, 0);
619 /************************* TAP related stuff **********/
621 /* This buffer size matches the maximum CMD_TAP_SHIFT bit length in the Bus Pirate firmware,
622 look for constant 0x2000 in OpenOCD.c . */
623 #define BUSPIRATE_BUFFER_SIZE 1024
625 /* The old value of 32 scans was not enough to achieve near 100% utilisation ratio
626 for the current BUSPIRATE_BUFFER_SIZE value of 1024.
627 With 128 scans I am getting full USB 2.0 high speed packets (512 bytes long) when
628 using the JtagDue firmware on the Arduino Due instead of the Bus Pirate, which
629 amounts approximately to a 10% overall speed gain. Bigger packets should also
630 benefit the Bus Pirate, but the speed difference is much smaller.
631 Unfortunately, each 512-byte packet is followed by a 329-byte one, which is not ideal.
632 However, increasing BUSPIRATE_BUFFER_SIZE for the benefit of the JtagDue would
633 make it incompatible with the Bus Pirate firmware. */
634 #define BUSPIRATE_MAX_PENDING_SCANS 128
636 static char tms_chain[BUSPIRATE_BUFFER_SIZE]; /* send */
637 static char tdi_chain[BUSPIRATE_BUFFER_SIZE]; /* send */
638 static int tap_chain_index;
640 struct pending_scan_result /* this was stolen from arm-jtag-ew */
642 int first; /* First bit position in tdo_buffer to read */
643 int length; /* Number of bits to read */
644 struct scan_command *command; /* Corresponding scan command */
648 static struct pending_scan_result
649 tap_pending_scans[BUSPIRATE_MAX_PENDING_SCANS];
650 static int tap_pending_scans_num;
652 static void buspirate_tap_init(void)
655 tap_pending_scans_num = 0;
658 static int buspirate_tap_execute(void)
660 static const int CMD_TAP_SHIFT_HEADER_LEN = 3;
669 if (tap_chain_index <= 0)
672 LOG_DEBUG("executing tap num bits = %i scans = %i",
673 tap_chain_index, tap_pending_scans_num);
675 bytes_to_send = DIV_ROUND_UP(tap_chain_index, 8);
677 tmp[0] = CMD_TAP_SHIFT; /* this command expects number of bits */
678 tmp[1] = (char)(tap_chain_index >> 8); /* high */
679 tmp[2] = (char)(tap_chain_index); /* low */
681 fill_index = CMD_TAP_SHIFT_HEADER_LEN;
682 for (i = 0; i < bytes_to_send; i++) {
683 tmp[fill_index] = tdi_chain[i];
685 tmp[fill_index] = tms_chain[i];
689 /* jlink.c calls the routine below, which may be useful for debugging purposes.
690 For example, enabling this allows you to compare the log outputs from jlink.c
691 and from this module for JTAG development or troubleshooting purposes. */
693 last_tap_state = jtag_debug_state_machine(tms_chain, tdi_chain,
694 tap_chain_index, last_tap_state);
697 ret = buspirate_serial_write(buspirate_fd, tmp, CMD_TAP_SHIFT_HEADER_LEN + bytes_to_send*2);
698 if (ret != bytes_to_send*2+CMD_TAP_SHIFT_HEADER_LEN) {
699 LOG_ERROR("error writing :(");
700 return ERROR_JTAG_DEVICE_ERROR;
703 ret = buspirate_serial_read(buspirate_fd, tmp, bytes_to_send + CMD_TAP_SHIFT_HEADER_LEN);
704 if (ret != bytes_to_send + CMD_TAP_SHIFT_HEADER_LEN) {
705 LOG_ERROR("error reading");
708 in_buf = (uint8_t *)(&tmp[CMD_TAP_SHIFT_HEADER_LEN]);
710 /* parse the scans */
711 for (i = 0; i < tap_pending_scans_num; i++) {
712 uint8_t *buffer = tap_pending_scans[i].buffer;
713 int length = tap_pending_scans[i].length;
714 int first = tap_pending_scans[i].first;
715 struct scan_command *command = tap_pending_scans[i].command;
717 /* copy bits from buffer */
718 buf_set_buf(in_buf, first, buffer, 0, length);
720 /* return buffer to higher level */
721 if (jtag_read_buffer(buffer, command) != ERROR_OK) {
722 buspirate_tap_init();
723 return ERROR_JTAG_QUEUE_FAILED;
728 buspirate_tap_init();
732 static void buspirate_tap_make_space(int scans, int bits)
734 int have_scans = BUSPIRATE_MAX_PENDING_SCANS - tap_pending_scans_num;
735 int have_bits = BUSPIRATE_BUFFER_SIZE * 8 - tap_chain_index;
737 if ((have_scans < scans) || (have_bits < bits))
738 buspirate_tap_execute();
741 static void buspirate_tap_append(int tms, int tdi)
745 buspirate_tap_make_space(0, 1);
746 chain_index = tap_chain_index / 8;
748 if (chain_index < BUSPIRATE_BUFFER_SIZE) {
749 int bit_index = tap_chain_index % 8;
750 uint8_t bit = 1 << bit_index;
752 if (0 == bit_index) {
753 /* Let's say that the TAP shift operation wants to shift 9 bits,
754 so we will be sending to the Bus Pirate a bit count of 9 but still
755 full 16 bits (2 bytes) of shift data.
756 If we don't clear all bits at this point, the last 7 bits will contain
757 random data from the last buffer contents, which is not pleasant to the eye.
758 Besides, the Bus Pirate (or some clone) may want to assert in debug builds
759 that, after consuming all significant data bits, the rest of them are zero.
760 Therefore, for aesthetic and for assert purposes, we clear all bits below. */
761 tms_chain[chain_index] = 0;
762 tdi_chain[chain_index] = 0;
766 tms_chain[chain_index] |= bit;
768 tms_chain[chain_index] &= ~bit;
771 tdi_chain[chain_index] |= bit;
773 tdi_chain[chain_index] &= ~bit;
777 LOG_ERROR("tap_chain overflow, bad things will happen");
778 /* Exit abruptly, like jlink.c does. After a buffer overflow we don't want
779 to carry on, as data will be corrupt. Another option would be to return
780 some error code at this point. */
785 static void buspirate_tap_append_scan(int length, uint8_t *buffer,
786 struct scan_command *command)
789 tap_pending_scans[tap_pending_scans_num].length = length;
790 tap_pending_scans[tap_pending_scans_num].buffer = buffer;
791 tap_pending_scans[tap_pending_scans_num].command = command;
792 tap_pending_scans[tap_pending_scans_num].first = tap_chain_index;
794 for (i = 0; i < length; i++) {
795 int tms = (i < length-1 ? 0 : 1);
796 int tdi = (buffer[i/8] >> (i%8)) & 1;
797 buspirate_tap_append(tms, tdi);
799 tap_pending_scans_num++;
802 /*************** jtag wrapper functions *********************/
804 /* (1) assert or (0) deassert reset lines */
805 static void buspirate_reset(int trst, int srst)
807 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
810 buspirate_jtag_set_feature(buspirate_fd,
811 FEATURE_TRST, ACTION_DISABLE);
813 buspirate_jtag_set_feature(buspirate_fd,
814 FEATURE_TRST, ACTION_ENABLE);
817 buspirate_jtag_set_feature(buspirate_fd,
818 FEATURE_SRST, ACTION_DISABLE);
820 buspirate_jtag_set_feature(buspirate_fd,
821 FEATURE_SRST, ACTION_ENABLE);
824 /*************** jtag lowlevel functions ********************/
825 static void buspirate_jtag_enable(int fd)
828 char tmp[21] = { [0 ... 20] = 0x00 };
832 LOG_DEBUG("Entering binary mode");
833 buspirate_serial_write(fd, tmp, 20);
836 /* reads 1 to n "BBIO1"s and one "OCD1" */
838 ret = buspirate_serial_read(fd, tmp, 4);
840 LOG_ERROR("Buspirate error. Is binary"
841 "/OpenOCD support enabled?");
844 if (strncmp(tmp, "BBIO", 4) == 0) {
845 ret = buspirate_serial_read(fd, tmp, 1);
847 LOG_ERROR("Buspirate did not answer correctly! "
848 "Do you have correct firmware?");
852 LOG_ERROR("Unsupported binary protocol");
857 tmp[0] = CMD_ENTER_OOCD;
858 ret = buspirate_serial_write(fd, tmp, 1);
860 LOG_ERROR("error reading");
864 } else if (strncmp(tmp, "OCD1", 4) == 0)
867 LOG_ERROR("Buspirate did not answer correctly! "
868 "Do you have correct firmware?");
875 static void buspirate_jtag_reset(int fd)
879 tmp[0] = 0x00; /* exit OCD1 mode */
880 buspirate_serial_write(fd, tmp, 1);
882 /* We ignore the return value here purposly, nothing we can do */
883 buspirate_serial_read(fd, tmp, 5);
884 if (strncmp(tmp, "BBIO1", 5) == 0) {
885 tmp[0] = 0x0F; /* reset BP */
886 buspirate_serial_write(fd, tmp, 1);
888 LOG_ERROR("Unable to restart buspirate!");
891 static void buspirate_jtag_set_speed(int fd, char speed)
900 tmp[0] = CMD_UART_SPEED;
902 buspirate_jtag_command(fd, tmp, 2);
904 /* here the adapter changes speed, we need follow */
905 if (-1 == buspirate_serial_setspeed(fd, speed, NORMAL_TIMEOUT)) {
906 LOG_ERROR("Error configuring the serial port.");
910 buspirate_serial_write(fd, ack, 2);
911 ret = buspirate_serial_read(fd, tmp, 2);
913 LOG_ERROR("Buspirate did not ack speed change");
916 if ((tmp[0] != CMD_UART_SPEED) || (tmp[1] != speed)) {
917 LOG_ERROR("Buspirate did not reply as expected to the speed change command");
920 LOG_INFO("Buspirate switched to %s mode",
921 (speed == SERIAL_NORMAL) ? "normal" : "FAST");
925 static void buspirate_jtag_set_mode(int fd, char mode)
928 tmp[0] = CMD_PORT_MODE;
930 buspirate_jtag_command(fd, tmp, 2);
933 static void buspirate_jtag_set_feature(int fd, char feat, char action)
936 tmp[0] = CMD_FEATURE;
937 tmp[1] = feat; /* what */
938 tmp[2] = action; /* action */
939 buspirate_jtag_command(fd, tmp, 3);
942 static void buspirate_jtag_get_adcs(int fd)
946 tmp[0] = CMD_READ_ADCS;
947 buspirate_jtag_command(fd, (char *)tmp, 1);
948 a = tmp[2] << 8 | tmp[3];
949 b = tmp[4] << 8 | tmp[5];
950 c = tmp[6] << 8 | tmp[7];
951 d = tmp[8] << 8 | tmp[9];
953 LOG_INFO("ADC: ADC_Pin = %.02f VPullup = %.02f V33 = %.02f "
955 ((float)a)/155.1515, ((float)b)/155.1515,
956 ((float)c)/155.1515, ((float)d)/155.1515);
959 static unsigned char buspirate_jtag_command(int fd,
960 char *cmd, int cmdlen)
965 res = buspirate_serial_write(fd, cmd, cmdlen);
967 if ((cmd[0] == CMD_UART_SPEED)
968 || (cmd[0] == CMD_PORT_MODE)
969 || (cmd[0] == CMD_FEATURE)
970 || (cmd[0] == CMD_JTAG_SPEED))
976 len = 10; /* 2*sizeof(char)+4*sizeof(uint16_t) */
984 res = buspirate_serial_read(fd, cmd, len);
986 return (unsigned char)cmd[1];
994 /* low level serial port */
995 /* TODO add support for WIN32 and others ! */
996 static int buspirate_serial_open(char *port)
999 fd = open(buspirate_port, O_RDWR | O_NOCTTY | O_NDELAY);
1004 /* Returns -1 on error. */
1006 static int buspirate_serial_setspeed(int fd, char speed, cc_t timeout)
1008 struct termios t_opt;
1009 speed_t baud = (speed == SERIAL_FAST) ? B1000000 : B115200;
1011 /* set the serial port parameters */
1012 fcntl(fd, F_SETFL, 0);
1013 if (0 != tcgetattr(fd, &t_opt))
1016 if (0 != cfsetispeed(&t_opt, baud))
1019 if (0 != cfsetospeed(&t_opt, baud))
1022 t_opt.c_cflag |= (CLOCAL | CREAD);
1023 t_opt.c_cflag &= ~PARENB;
1024 t_opt.c_cflag &= ~CSTOPB;
1025 t_opt.c_cflag &= ~CSIZE;
1026 t_opt.c_cflag |= CS8;
1027 t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
1029 /* The serial port may have been configured for human interaction with
1030 the Bus Pirate console, but OpenOCD is going to use a binary protocol,
1031 so make sure to turn off any CR/LF translation and the like. */
1032 t_opt.c_iflag &= ~(IXON | IXOFF | IXANY | INLCR | ICRNL);
1034 t_opt.c_oflag &= ~OPOST;
1035 t_opt.c_cc[VMIN] = 0;
1036 t_opt.c_cc[VTIME] = timeout;
1038 /* Note that, in the past, TCSANOW was used below instead of TCSADRAIN,
1039 and CMD_UART_SPEED did not work properly then, at least with
1040 the Bus Pirate v3.5 (USB). */
1041 if (0 != tcsetattr(fd, TCSADRAIN, &t_opt)) {
1042 /* According to the Linux documentation, this is actually not enough
1043 to detect errors, you need to call tcgetattr() and check that
1044 all changes have been performed successfully. */
1051 static int buspirate_serial_write(int fd, char *buf, int size)
1055 ret = write(fd, buf, size);
1057 LOG_DEBUG("size = %d ret = %d", size, ret);
1058 buspirate_print_buffer(buf, size);
1061 LOG_ERROR("Error sending data");
1066 static int buspirate_serial_read(int fd, char *buf, int size)
1072 while (len < size) {
1073 ret = read(fd, buf+len, size-len);
1089 LOG_DEBUG("should have read = %d actual size = %d", size, len);
1090 buspirate_print_buffer(buf, len);
1093 LOG_ERROR("Error reading data");
1098 static void buspirate_serial_close(int fd)
1103 #define LINE_SIZE 81
1104 #define BYTES_PER_LINE 16
1105 static void buspirate_print_buffer(char *buf, int size)
1107 char line[LINE_SIZE];
1112 while (offset < size) {
1113 snprintf(tmp, 5, "%02x ", (uint8_t)buf[offset]);
1118 if (offset % BYTES_PER_LINE == 0) {
1119 LOG_DEBUG("%s", line);
1125 LOG_DEBUG("%s", line);