OpenOCD
buspirate.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2010 by Michal Demin *
5  * based on usbprog.c and arm-jtag-ew.c *
6  * Several fixes by R. Diez in 2013. *
7  ***************************************************************************/
8 
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12 
13 #include <jtag/interface.h>
14 #include <jtag/swd.h>
15 #include <jtag/commands.h>
16 
17 #include <termios.h>
18 #include <fcntl.h>
19 #include <sys/ioctl.h>
20 
21 #undef DEBUG_SERIAL
22 /*#define DEBUG_SERIAL */
23 static int buspirate_execute_queue(struct jtag_command *cmd_queue);
24 static int buspirate_init(void);
25 static int buspirate_quit(void);
26 static int buspirate_reset(int trst, int srst);
27 
28 static void buspirate_end_state(enum tap_state state);
29 static void buspirate_state_move(void);
30 static void buspirate_path_move(unsigned int num_states, enum tap_state *path);
31 static void buspirate_runtest(unsigned int num_cycles);
32 static void buspirate_scan(bool ir_scan, enum scan_type type,
33  uint8_t *buffer, int scan_size, struct scan_command *command);
34 static void buspirate_stableclocks(unsigned int num_cycles);
35 
36 #define CMD_UNKNOWN 0x00
37 #define CMD_PORT_MODE 0x01
38 #define CMD_FEATURE 0x02
39 #define CMD_READ_ADCS 0x03
40 /*#define CMD_TAP_SHIFT 0x04 // old protocol */
41 #define CMD_TAP_SHIFT 0x05
42 #define CMD_ENTER_RWIRE 0x05
43 #define CMD_ENTER_OOCD 0x06
44 #define CMD_UART_SPEED 0x07
45 #define CMD_JTAG_SPEED 0x08
46 #define CMD_RAW_PERIPH 0x40
47 #define CMD_RAW_SPEED 0x60
48 #define CMD_RAW_MODE 0x80
49 
50 #define CMD_TAP_SHIFT_HEADER_LEN 3
51 
52 /* raw-wire mode configuration */
53 #define CMD_RAW_CONFIG_HIZ 0x00
54 #define CMD_RAW_CONFIG_3V3 0x08
55 #define CMD_RAW_CONFIG_2W 0x00
56 #define CMD_RAW_CONFIG_3W 0x04
57 #define CMD_RAW_CONFIG_MSB 0x00
58 #define CMD_RAW_CONFIG_LSB 0x02
59 
60 /* Not all OSes have this speed defined */
61 #if !defined(B1000000)
62 #define B1000000 0010010
63 #endif
64 
65 #define SHORT_TIMEOUT 1 /* Must be at least 1. */
66 #define NORMAL_TIMEOUT 10
67 
68 enum {
69  MODE_HIZ = 0,
70  MODE_JTAG = 1, /* push-pull outputs */
71  MODE_JTAG_OD = 2, /* open-drain outputs */
72 };
73 
74 enum {
75  FEATURE_LED = 0x01,
76  FEATURE_VREG = 0x02,
77  FEATURE_TRST = 0x04,
78  FEATURE_SRST = 0x08,
79  FEATURE_PULLUP = 0x10
80 };
81 
82 enum {
84  ACTION_ENABLE = 1
85 };
86 
87 enum {
89  SERIAL_FAST = 1
90 };
91 
92 enum {
96  SPEED_RAW_400_KHZ = 0x3
97 };
98 
99 /* SWD mode specific */
100 static bool swd_mode;
101 static char swd_features;
102 
103 static int buspirate_fd = -1;
106 static int buspirate_vreg;
107 static int buspirate_pullup;
108 static char *buspirate_port;
109 
110 static enum tap_state last_tap_state = TAP_RESET;
111 
112 /* SWD interface */
113 static int buspirate_swd_init(void);
114 static int buspirate_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk);
115 static int buspirate_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk);
116 static int buspirate_swd_switch_seq(enum swd_special_seq seq);
117 static int buspirate_swd_run_queue(void);
118 
119 /* TAP interface */
120 static void buspirate_tap_init(void);
121 static int buspirate_tap_execute(void);
122 static void buspirate_tap_append(int tms, int tdi);
123 static void buspirate_tap_append_scan(int length, uint8_t *buffer,
124  struct scan_command *command);
125 static void buspirate_tap_make_space(int scan, int bits);
126 
127 static void buspirate_set_feature(int, char, char);
128 static void buspirate_set_mode(int, char);
129 static void buspirate_set_speed(int, char);
130 
131 /* low level interface */
132 static void buspirate_bbio_enable(int);
133 static void buspirate_jtag_reset(int);
134 static unsigned char buspirate_jtag_command(int, uint8_t *, int);
135 static void buspirate_jtag_set_speed(int, char);
136 static void buspirate_jtag_set_mode(int, char);
137 static void buspirate_jtag_set_feature(int, char, char);
138 static void buspirate_jtag_get_adcs(int);
139 
140 /* low level two-wire interface */
141 static void buspirate_swd_set_speed(int, char);
142 static void buspirate_swd_set_feature(int, char, char);
143 static void buspirate_swd_set_mode(int, char);
144 
145 /* low level HW communication interface */
146 static int buspirate_serial_open(char *port);
147 static int buspirate_serial_setspeed(int fd, char speed, cc_t timeout);
148 static int buspirate_serial_write(int fd, uint8_t *buf, int size);
149 static int buspirate_serial_read(int fd, uint8_t *buf, int size);
150 static void buspirate_serial_close(int fd);
151 static void buspirate_print_buffer(uint8_t *buf, int size);
152 
153 static int buspirate_execute_queue(struct jtag_command *cmd_queue)
154 {
155  /* currently processed command */
156  struct jtag_command *cmd = cmd_queue;
157  int scan_size;
158  enum scan_type type;
159  uint8_t *buffer;
160 
161  while (cmd) {
162  switch (cmd->type) {
163  case JTAG_RUNTEST:
164  LOG_DEBUG_IO("runtest %u cycles, end in %s",
165  cmd->cmd.runtest->num_cycles,
166  tap_state_name(cmd->cmd.runtest
167  ->end_state));
168  buspirate_end_state(cmd->cmd.runtest
169  ->end_state);
170  buspirate_runtest(cmd->cmd.runtest
171  ->num_cycles);
172  break;
173  case JTAG_TLR_RESET:
174  LOG_DEBUG_IO("statemove end in %s",
175  tap_state_name(cmd->cmd.statemove
176  ->end_state));
177  buspirate_end_state(cmd->cmd.statemove
178  ->end_state);
180  break;
181  case JTAG_PATHMOVE:
182  LOG_DEBUG_IO("pathmove: %u states, end in %s",
183  cmd->cmd.pathmove->num_states,
184  tap_state_name(cmd->cmd.pathmove
185  ->path[cmd->cmd.pathmove
186  ->num_states - 1]));
187  buspirate_path_move(cmd->cmd.pathmove
188  ->num_states,
189  cmd->cmd.pathmove->path);
190  break;
191  case JTAG_SCAN:
192  LOG_DEBUG_IO("scan end in %s",
193  tap_state_name(cmd->cmd.scan
194  ->end_state));
195 
196  buspirate_end_state(cmd->cmd.scan
197  ->end_state);
198 
199  scan_size = jtag_build_buffer(cmd->cmd.scan,
200  &buffer);
201  type = jtag_scan_type(cmd->cmd.scan);
202  buspirate_scan(cmd->cmd.scan->ir_scan, type,
203  buffer, scan_size, cmd->cmd.scan);
204 
205  break;
206  case JTAG_SLEEP:
207  LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
209  jtag_sleep(cmd->cmd.sleep->us);
210  break;
211  case JTAG_STABLECLOCKS:
212  LOG_DEBUG_IO("stable clock %u cycles", cmd->cmd.stableclocks->num_cycles);
213  buspirate_stableclocks(cmd->cmd.stableclocks->num_cycles);
214  break;
215  default:
216  LOG_ERROR("BUG: unknown JTAG command type encountered");
217  exit(-1);
218  }
219 
220  cmd = cmd->next;
221  }
222 
223  return buspirate_tap_execute();
224 }
225 
226 
227 /* Returns true if successful, false if error. */
228 
229 static bool read_and_discard_all_data(const int fd)
230 {
231  /* LOG_INFO("Discarding any stale data from a previous connection..."); */
232 
233  bool was_msg_already_printed = false;
234 
235  for ( ; ; ) {
236  uint8_t buffer[1024]; /* Any size will do, it's a trade-off between stack size and performance. */
237 
238  const ssize_t read_count = read(fd, buffer, sizeof(buffer));
239 
240  if (read_count == 0) {
241  /* This is the "end of file" or "connection closed at the other end" condition. */
242  return true;
243  }
244 
245  if (read_count > 0) {
246  if (!was_msg_already_printed) {
247  LOG_INFO("Some stale data from a previous connection was discarded.");
248  was_msg_already_printed = true;
249  }
250 
251  continue;
252  }
253 
254  assert(read_count == -1); /* According to the specification. */
255 
256  const int errno_code = errno;
257 
258  if (errno_code == EINTR)
259  continue;
260 
261  if (errno_code == EAGAIN ||
262  errno_code == EWOULDBLOCK) {
263  /* We know that the file descriptor has been opened with O_NONBLOCK or O_NDELAY,
264  and these codes mean that there is no data to read at present. */
265  return true;
266  }
267 
268  /* Some other error has occurred. */
269  return false;
270  }
271 }
272 
273 
274 static int buspirate_init(void)
275 {
276  if (!buspirate_port) {
277  LOG_ERROR("You need to specify the serial port!");
278  return ERROR_JTAG_INIT_FAILED;
279  }
280 
282  if (buspirate_fd == -1) {
283  LOG_ERROR("Could not open serial port");
284  return ERROR_JTAG_INIT_FAILED;
285  }
286 
287  /* The Operating System or the device itself may deliver stale data from the last connection,
288  so discard all available bytes right after the new connection has been established.
289  After all, we are implementing here a master/slave protocol, so the slave should have nothing
290  to say until the master sends the first command.
291 
292  In the past, there was a tcflush() call in buspirate_serial_setspeed(), but that
293  was not enough. I guess you must actively read from the serial port to trigger any
294  data collection from the device and/or lower USB layers. If you disable the serial port
295  read timeout (if you set SHORT_TIMEOUT to 0), then the discarding does not work any more.
296 
297  Note that we are lowering the serial port timeout for this first read operation,
298  otherwise the normal initialisation would be delayed for too long. */
299 
301  LOG_ERROR("Error configuring the serial port.");
302  return ERROR_JTAG_INIT_FAILED;
303  }
304 
306  LOG_ERROR("Error while attempting to discard any stale data right after establishing the connection.");
307  return ERROR_JTAG_INIT_FAILED;
308  }
309 
311  LOG_ERROR("Error configuring the serial port.");
312  return ERROR_JTAG_INIT_FAILED;
313  }
314 
316 
319 
320  LOG_INFO("Buspirate %s Interface ready!", swd_mode ? "SWD" : "JTAG");
321 
322  if (!swd_mode)
324 
330  buspirate_reset(0, 0);
331 
332  return ERROR_OK;
333 }
334 
335 static int buspirate_quit(void)
336 {
337  LOG_INFO("Shutting down buspirate.");
340 
342 
344 
345  free(buspirate_port);
347  return ERROR_OK;
348 }
349 
350 /* openocd command interface */
351 COMMAND_HANDLER(buspirate_handle_adc_command)
352 {
353  if (buspirate_fd == -1)
354  return ERROR_OK;
355 
356  /* unavailable in SWD mode */
357  if (swd_mode)
358  return ERROR_OK;
359 
360  /* send the command */
362 
363  return ERROR_OK;
364 
365 }
366 
367 COMMAND_HANDLER(buspirate_handle_vreg_command)
368 {
369  if (CMD_ARGC < 1)
371 
372  if (atoi(CMD_ARGV[0]) == 1)
373  buspirate_vreg = 1;
374  else if (atoi(CMD_ARGV[0]) == 0)
375  buspirate_vreg = 0;
376  else
377  LOG_ERROR("usage: buspirate_vreg <1|0>");
378 
379  return ERROR_OK;
380 
381 }
382 
383 COMMAND_HANDLER(buspirate_handle_pullup_command)
384 {
385  if (CMD_ARGC < 1)
387 
388  if (atoi(CMD_ARGV[0]) == 1)
389  buspirate_pullup = 1;
390  else if (atoi(CMD_ARGV[0]) == 0)
391  buspirate_pullup = 0;
392  else
393  LOG_ERROR("usage: buspirate_pullup <1|0>");
394 
395  return ERROR_OK;
396 
397 }
398 
399 COMMAND_HANDLER(buspirate_handle_led_command)
400 {
401  if (CMD_ARGC < 1)
403 
404  if (atoi(CMD_ARGV[0]) == 1) {
405  /* enable led */
407  ACTION_ENABLE);
408  } else if (atoi(CMD_ARGV[0]) == 0) {
409  /* disable led */
412  } else {
413  LOG_ERROR("usage: buspirate_led <1|0>");
414  }
415 
416  return ERROR_OK;
417 
418 }
419 
420 COMMAND_HANDLER(buspirate_handle_mode_command)
421 {
422  if (CMD_ARGC < 1)
424 
425  if (CMD_ARGV[0][0] == 'n')
427  else if (CMD_ARGV[0][0] == 'o')
429  else
430  LOG_ERROR("usage: buspirate_mode <normal|open-drain>");
431 
432  return ERROR_OK;
433 
434 }
435 
436 COMMAND_HANDLER(buspirate_handle_speed_command)
437 {
438  if (CMD_ARGC < 1)
440 
441  if (CMD_ARGV[0][0] == 'n')
443  else if (CMD_ARGV[0][0] == 'f')
445  else
446  LOG_ERROR("usage: buspirate_speed <normal|fast>");
447 
448  return ERROR_OK;
449 
450 }
451 
452 COMMAND_HANDLER(buspirate_handle_port_command)
453 {
454  if (CMD_ARGC < 1)
456 
457  if (!buspirate_port)
458  buspirate_port = strdup(CMD_ARGV[0]);
459 
460  return ERROR_OK;
461 
462 }
463 
464 static const struct command_registration buspirate_subcommand_handlers[] = {
465  {
466  .name = "adc",
467  .handler = &buspirate_handle_adc_command,
468  .mode = COMMAND_EXEC,
469  .help = "reads voltages on adc pins",
470  .usage = "",
471  },
472  {
473  .name = "vreg",
474  .usage = "<1|0>",
475  .handler = &buspirate_handle_vreg_command,
476  .mode = COMMAND_CONFIG,
477  .help = "changes the state of voltage regulators",
478  },
479  {
480  .name = "pullup",
481  .usage = "<1|0>",
482  .handler = &buspirate_handle_pullup_command,
483  .mode = COMMAND_CONFIG,
484  .help = "changes the state of pullup",
485  },
486  {
487  .name = "led",
488  .usage = "<1|0>",
489  .handler = &buspirate_handle_led_command,
490  .mode = COMMAND_EXEC,
491  .help = "changes the state of led",
492  },
493  {
494  .name = "speed",
495  .usage = "<normal|fast>",
496  .handler = &buspirate_handle_speed_command,
497  .mode = COMMAND_CONFIG,
498  .help = "speed of the interface",
499  },
500  {
501  .name = "mode",
502  .usage = "<normal|open-drain>",
503  .handler = &buspirate_handle_mode_command,
504  .mode = COMMAND_CONFIG,
505  .help = "pin mode of the interface",
506  },
507  {
508  .name = "port",
509  .usage = "/dev/ttyUSB0",
510  .handler = &buspirate_handle_port_command,
511  .mode = COMMAND_CONFIG,
512  .help = "name of the serial port to open",
513  },
515 };
516 
517 static const struct command_registration buspirate_command_handlers[] = {
518  {
519  .name = "buspirate",
520  .mode = COMMAND_ANY,
521  .help = "perform buspirate management",
523  .usage = "",
524  },
526 };
527 
528 static const struct swd_driver buspirate_swd = {
530  .switch_seq = buspirate_swd_switch_seq,
531  .read_reg = buspirate_swd_read_reg,
532  .write_reg = buspirate_swd_write_reg,
534 };
535 
536 static struct jtag_interface buspirate_interface = {
538 };
539 
541  .name = "buspirate",
542  .transport_ids = TRANSPORT_JTAG | TRANSPORT_SWD,
543  .transport_preferred_id = TRANSPORT_JTAG,
544  .commands = buspirate_command_handlers,
545 
546  .init = buspirate_init,
547  .quit = buspirate_quit,
548  .reset = buspirate_reset,
549 
550  .jtag_ops = &buspirate_interface,
551  .swd_ops = &buspirate_swd,
552 };
553 
554 /*************** jtag execute commands **********************/
556 {
559  else {
560  LOG_ERROR("BUG: %i is not a valid end state", state);
561  exit(-1);
562  }
563 }
564 
565 static void buspirate_state_move(void)
566 {
567  int i = 0, tms = 0;
568  uint8_t tms_scan = tap_get_tms_path(tap_get_state(),
570  int tms_count = tap_get_tms_path_len(tap_get_state(),
572 
573  for (i = 0; i < tms_count; i++) {
574  tms = (tms_scan >> i) & 1;
575  buspirate_tap_append(tms, 0);
576  }
577 
579 }
580 
581 static void buspirate_path_move(unsigned int num_states, enum tap_state *path)
582 {
583  for (unsigned int i = 0; i < num_states; i++) {
584  if (tap_state_transition(tap_get_state(), false) == path[i]) {
585  buspirate_tap_append(0, 0);
586  } else if (tap_state_transition(tap_get_state(), true)
587  == path[i]) {
588  buspirate_tap_append(1, 0);
589  } else {
590  LOG_ERROR("BUG: %s -> %s isn't a valid "
591  "TAP transition",
593  tap_state_name(path[i]));
594  exit(-1);
595  }
596 
597  tap_set_state(path[i]);
598  }
599 
601 }
602 
603 static void buspirate_runtest(unsigned int num_cycles)
604 {
605  enum tap_state saved_end_state = tap_get_end_state();
606 
607  /* only do a state_move when we're not already in IDLE */
608  if (tap_get_state() != TAP_IDLE) {
611  }
612 
613  for (unsigned int i = 0; i < num_cycles; i++)
614  buspirate_tap_append(0, 0);
615 
616  LOG_DEBUG_IO("runtest: cur_state %s end_state %s",
619 
620  /* finish in end_state */
621  buspirate_end_state(saved_end_state);
622  if (tap_get_state() != tap_get_end_state())
624 }
625 
626 static void buspirate_scan(bool ir_scan, enum scan_type type,
627  uint8_t *buffer, int scan_size, struct scan_command *command)
628 {
629  enum tap_state saved_end_state;
630 
631  buspirate_tap_make_space(1, scan_size+8);
632  /* is 8 correct ? (2 moves = 16) */
633 
634  saved_end_state = tap_get_end_state();
635 
637 
638  /* Only move if we're not already there */
639  if (tap_get_state() != tap_get_end_state())
641 
643 
644  /* move to PAUSE */
645  buspirate_tap_append(0, 0);
646 
647  /* restore the saved state */
648  buspirate_end_state(saved_end_state);
650 
651  if (tap_get_state() != tap_get_end_state())
653 }
654 
655 static void buspirate_stableclocks(unsigned int num_cycles)
656 {
657  int tms = (tap_get_state() == TAP_RESET ? 1 : 0);
658 
659  buspirate_tap_make_space(0, num_cycles);
660 
661  for (unsigned int i = 0; i < num_cycles; i++)
662  buspirate_tap_append(tms, 0);
663 }
664 
665 /************************* TAP related stuff **********/
666 
667 /* This buffer size matches the maximum CMD_TAP_SHIFT bit length in the Bus Pirate firmware,
668  look for constant 0x2000 in OpenOCD.c . */
669 #define BUSPIRATE_BUFFER_SIZE 1024
670 
671 /* The old value of 32 scans was not enough to achieve near 100% utilisation ratio
672  for the current BUSPIRATE_BUFFER_SIZE value of 1024.
673  With 128 scans I am getting full USB 2.0 high speed packets (512 bytes long) when
674  using the JtagDue firmware on the Arduino Due instead of the Bus Pirate, which
675  amounts approximately to a 10% overall speed gain. Bigger packets should also
676  benefit the Bus Pirate, but the speed difference is much smaller.
677  Unfortunately, each 512-byte packet is followed by a 329-byte one, which is not ideal.
678  However, increasing BUSPIRATE_BUFFER_SIZE for the benefit of the JtagDue would
679  make it incompatible with the Bus Pirate firmware. */
680 #define BUSPIRATE_MAX_PENDING_SCANS 128
681 
682 static uint8_t tms_chain[BUSPIRATE_BUFFER_SIZE]; /* send */
683 static uint8_t tdi_chain[BUSPIRATE_BUFFER_SIZE]; /* send */
684 static int tap_chain_index;
685 
686 struct pending_scan_result /* this was stolen from arm-jtag-ew */
687 {
688  int first; /* First bit position in tdo_buffer to read */
689  int length; /* Number of bits to read */
690  struct scan_command *command; /* Corresponding scan command */
691  uint8_t *buffer;
692 };
693 
694 static struct pending_scan_result
697 
698 static void buspirate_tap_init(void)
699 {
700  tap_chain_index = 0;
702 }
703 
704 static int buspirate_tap_execute(void)
705 {
706  uint8_t tmp[4096];
707  uint8_t *in_buf;
708  int i;
709  int fill_index = 0;
710  int ret;
711  int bytes_to_send;
712 
713  if (tap_chain_index <= 0)
714  return ERROR_OK;
715 
716  LOG_DEBUG("executing tap num bits = %i scans = %i",
718 
719  bytes_to_send = DIV_ROUND_UP(tap_chain_index, 8);
720 
721  tmp[0] = CMD_TAP_SHIFT; /* this command expects number of bits */
722  tmp[1] = tap_chain_index >> 8; /* high */
723  tmp[2] = tap_chain_index; /* low */
724 
725  fill_index = CMD_TAP_SHIFT_HEADER_LEN;
726  for (i = 0; i < bytes_to_send; i++) {
727  tmp[fill_index] = tdi_chain[i];
728  fill_index++;
729  tmp[fill_index] = tms_chain[i];
730  fill_index++;
731  }
732 
733  /* jlink.c calls the routine below, which may be useful for debugging purposes.
734  For example, enabling this allows you to compare the log outputs from jlink.c
735  and from this module for JTAG development or troubleshooting purposes. */
736  if (false) {
739  }
740 
741  ret = buspirate_serial_write(buspirate_fd, tmp, CMD_TAP_SHIFT_HEADER_LEN + bytes_to_send*2);
742  if (ret != bytes_to_send*2+CMD_TAP_SHIFT_HEADER_LEN) {
743  LOG_ERROR("error writing :(");
745  }
746 
747  ret = buspirate_serial_read(buspirate_fd, tmp, bytes_to_send + CMD_TAP_SHIFT_HEADER_LEN);
748  if (ret != bytes_to_send + CMD_TAP_SHIFT_HEADER_LEN) {
749  LOG_ERROR("error reading");
750  return ERROR_FAIL;
751  }
752  in_buf = (uint8_t *)(&tmp[CMD_TAP_SHIFT_HEADER_LEN]);
753 
754  /* parse the scans */
755  for (i = 0; i < tap_pending_scans_num; i++) {
756  uint8_t *buffer = tap_pending_scans[i].buffer;
758  int first = tap_pending_scans[i].first;
760 
761  /* copy bits from buffer */
762  buf_set_buf(in_buf, first, buffer, 0, length);
763 
764  /* return buffer to higher level */
768  }
769 
770  free(buffer);
771  }
773  return ERROR_OK;
774 }
775 
776 static void buspirate_tap_make_space(int scans, int bits)
777 {
779  int have_bits = BUSPIRATE_BUFFER_SIZE * 8 - tap_chain_index;
780 
781  if ((have_scans < scans) || (have_bits < bits))
783 }
784 
785 static void buspirate_tap_append(int tms, int tdi)
786 {
787  int chain_index;
788 
790  chain_index = tap_chain_index / 8;
791 
792  if (chain_index < BUSPIRATE_BUFFER_SIZE) {
793  int bit_index = tap_chain_index % 8;
794  uint8_t bit = 1 << bit_index;
795 
796  if (bit_index == 0) {
797  /* Let's say that the TAP shift operation wants to shift 9 bits,
798  so we will be sending to the Bus Pirate a bit count of 9 but still
799  full 16 bits (2 bytes) of shift data.
800  If we don't clear all bits at this point, the last 7 bits will contain
801  random data from the last buffer contents, which is not pleasant to the eye.
802  Besides, the Bus Pirate (or some clone) may want to assert in debug builds
803  that, after consuming all significant data bits, the rest of them are zero.
804  Therefore, for aesthetic and for assert purposes, we clear all bits below. */
805  tms_chain[chain_index] = 0;
806  tdi_chain[chain_index] = 0;
807  }
808 
809  if (tms)
810  tms_chain[chain_index] |= bit;
811  else
812  tms_chain[chain_index] &= ~bit;
813 
814  if (tdi)
815  tdi_chain[chain_index] |= bit;
816  else
817  tdi_chain[chain_index] &= ~bit;
818 
819  tap_chain_index++;
820  } else {
821  LOG_ERROR("tap_chain overflow, bad things will happen");
822  /* Exit abruptly, like jlink.c does. After a buffer overflow we don't want
823  to carry on, as data will be corrupt. Another option would be to return
824  some error code at this point. */
825  exit(-1);
826  }
827 }
828 
829 static void buspirate_tap_append_scan(int length, uint8_t *buffer,
830  struct scan_command *command)
831 {
832  int i;
837 
838  for (i = 0; i < length; i++) {
839  int tms = (i < length-1 ? 0 : 1);
840  int tdi = (buffer[i/8] >> (i%8)) & 1;
841  buspirate_tap_append(tms, tdi);
842  }
844 }
845 
846 /*************** wrapper functions *********************/
847 
848 /* (1) assert or (0) deassert reset lines */
849 static int buspirate_reset(int trst, int srst)
850 {
851  LOG_DEBUG("trst: %i, srst: %i", trst, srst);
852 
853  if (trst)
855  else
857 
858  if (srst)
860  else
862 
863  return ERROR_OK;
864 }
865 
866 static void buspirate_set_feature(int fd, char feat, char action)
867 {
868  if (swd_mode)
869  buspirate_swd_set_feature(fd, feat, action);
870  else
871  buspirate_jtag_set_feature(fd, feat, action);
872 }
873 
874 static void buspirate_set_mode(int fd, char mode)
875 {
876  if (swd_mode)
878  else
880 }
881 
882 static void buspirate_set_speed(int fd, char speed)
883 {
884  if (swd_mode)
885  buspirate_swd_set_speed(fd, speed);
886  else
887  buspirate_jtag_set_speed(fd, speed);
888 }
889 
890 
891 /*************** swd lowlevel functions ********************/
892 
893 static void buspirate_swd_set_speed(int fd, char speed)
894 {
895  int ret;
896  uint8_t tmp[1];
897 
898  LOG_DEBUG("Buspirate speed setting in SWD mode defaults to 400 kHz");
899 
900  /* speed settings */
901  tmp[0] = CMD_RAW_SPEED | SPEED_RAW_400_KHZ;
902  buspirate_serial_write(fd, tmp, 1);
903  ret = buspirate_serial_read(fd, tmp, 1);
904  if (ret != 1) {
905  LOG_ERROR("Buspirate did not answer correctly");
906  exit(-1);
907  }
908  if (tmp[0] != 1) {
909  LOG_ERROR("Buspirate did not reply as expected to the speed change command");
910  exit(-1);
911  }
912 }
913 
914 static void buspirate_swd_set_mode(int fd, char mode)
915 {
916  int ret;
917  uint8_t tmp[1];
918 
919  /* raw-wire mode configuration */
920  if (mode == MODE_HIZ)
921  tmp[0] = CMD_RAW_MODE | CMD_RAW_CONFIG_LSB;
922  else
924 
925  buspirate_serial_write(fd, tmp, 1);
926  ret = buspirate_serial_read(fd, tmp, 1);
927  if (ret != 1) {
928  LOG_ERROR("Buspirate did not answer correctly");
929  exit(-1);
930  }
931  if (tmp[0] != 1) {
932  LOG_ERROR("Buspirate did not reply as expected to the configure command");
933  exit(-1);
934  }
935 }
936 
937 static void buspirate_swd_set_feature(int fd, char feat, char action)
938 {
939  int ret;
940  uint8_t tmp[1];
941 
942  switch (feat) {
943  case FEATURE_TRST:
944  LOG_DEBUG("Buspirate TRST feature not available in SWD mode");
945  return;
946  case FEATURE_LED:
947  LOG_ERROR("Buspirate LED feature not available in SWD mode");
948  return;
949  case FEATURE_SRST:
950  swd_features = (action == ACTION_ENABLE) ? swd_features | 0x02 : swd_features & 0x0D;
951  break;
952  case FEATURE_PULLUP:
953  swd_features = (action == ACTION_ENABLE) ? swd_features | 0x04 : swd_features & 0x0B;
954  break;
955  case FEATURE_VREG:
956  swd_features = (action == ACTION_ENABLE) ? swd_features | 0x08 : swd_features & 0x07;
957  break;
958  default:
959  LOG_DEBUG("Buspirate unknown feature %d", feat);
960  return;
961  }
962 
963  tmp[0] = CMD_RAW_PERIPH | swd_features;
964  buspirate_serial_write(fd, tmp, 1);
965  ret = buspirate_serial_read(fd, tmp, 1);
966  if (ret != 1) {
967  LOG_DEBUG("Buspirate feature %d not supported in SWD mode", feat);
968  } else if (tmp[0] != 1) {
969  LOG_ERROR("Buspirate did not reply as expected to the configure command");
970  exit(-1);
971  }
972 }
973 
974 /*************** jtag lowlevel functions ********************/
975 static void buspirate_bbio_enable(int fd)
976 {
977  int ret;
978  char command;
979  const char *mode_answers[2] = { "OCD1", "RAW1" };
980  const char *correct_ans = NULL;
981  uint8_t tmp[21] = { [0 ... 20] = 0x00 };
982  int done = 0;
983  int cmd_sent = 0;
984 
985  if (swd_mode) {
987  correct_ans = mode_answers[1];
988  } else {
990  correct_ans = mode_answers[0];
991  }
992 
993  LOG_DEBUG("Entering binary mode, that is %s", correct_ans);
994  buspirate_serial_write(fd, tmp, 20);
995  usleep(10000);
996 
997  /* reads 1 to n "BBIO1"s and one "OCD1" or "RAW1" */
998  while (!done) {
999  ret = buspirate_serial_read(fd, tmp, 4);
1000  if (ret != 4) {
1001  LOG_ERROR("Buspirate error. Is binary"
1002  "/OpenOCD support enabled?");
1003  exit(-1);
1004  }
1005  if (strncmp((char *)tmp, "BBIO", 4) == 0) {
1006  ret = buspirate_serial_read(fd, tmp, 1);
1007  if (ret != 1) {
1008  LOG_ERROR("Buspirate did not answer correctly! "
1009  "Do you have correct firmware?");
1010  exit(-1);
1011  }
1012  if (tmp[0] != '1') {
1013  LOG_ERROR("Unsupported binary protocol");
1014  exit(-1);
1015  }
1016  if (cmd_sent == 0) {
1017  cmd_sent = 1;
1018  tmp[0] = command;
1019  ret = buspirate_serial_write(fd, tmp, 1);
1020  if (ret != 1) {
1021  LOG_ERROR("error reading");
1022  exit(-1);
1023  }
1024  }
1025  } else if (strncmp((char *)tmp, correct_ans, 4) == 0)
1026  done = 1;
1027  else {
1028  LOG_ERROR("Buspirate did not answer correctly! "
1029  "Do you have correct firmware?");
1030  exit(-1);
1031  }
1032  }
1033 
1034 }
1035 
1036 static void buspirate_jtag_reset(int fd)
1037 {
1038  uint8_t tmp[5];
1039 
1040  tmp[0] = 0x00; /* exit OCD1 mode */
1041  buspirate_serial_write(fd, tmp, 1);
1042  usleep(10000);
1043  /* We ignore the return value here on purpose, nothing we can do */
1044  buspirate_serial_read(fd, tmp, 5);
1045  if (strncmp((char *)tmp, "BBIO1", 5) == 0) {
1046  tmp[0] = 0x0F; /* reset BP */
1047  buspirate_serial_write(fd, tmp, 1);
1048  } else
1049  LOG_ERROR("Unable to restart buspirate!");
1050 }
1051 
1052 static void buspirate_jtag_set_speed(int fd, char speed)
1053 {
1054  int ret;
1055  uint8_t tmp[2];
1056  uint8_t ack[2];
1057 
1058  ack[0] = 0xAA;
1059  ack[1] = 0x55;
1060 
1061  tmp[0] = CMD_UART_SPEED;
1062  tmp[1] = speed;
1063  buspirate_jtag_command(fd, tmp, 2);
1064 
1065  /* here the adapter changes speed, we need follow */
1066  if (-1 == buspirate_serial_setspeed(fd, speed, NORMAL_TIMEOUT)) {
1067  LOG_ERROR("Error configuring the serial port.");
1068  exit(-1);
1069  }
1070 
1071  buspirate_serial_write(fd, ack, 2);
1072  ret = buspirate_serial_read(fd, tmp, 2);
1073  if (ret != 2) {
1074  LOG_ERROR("Buspirate did not ack speed change");
1075  exit(-1);
1076  }
1077  if ((tmp[0] != CMD_UART_SPEED) || (tmp[1] != speed)) {
1078  LOG_ERROR("Buspirate did not reply as expected to the speed change command");
1079  exit(-1);
1080  }
1081  LOG_INFO("Buspirate switched to %s mode",
1082  (speed == SERIAL_NORMAL) ? "normal" : "FAST");
1083 }
1084 
1085 
1086 static void buspirate_jtag_set_mode(int fd, char mode)
1087 {
1088  uint8_t tmp[2];
1089  tmp[0] = CMD_PORT_MODE;
1090  tmp[1] = mode;
1091  buspirate_jtag_command(fd, tmp, 2);
1092 }
1093 
1094 static void buspirate_jtag_set_feature(int fd, char feat, char action)
1095 {
1096  uint8_t tmp[3];
1097  tmp[0] = CMD_FEATURE;
1098  tmp[1] = feat; /* what */
1099  tmp[2] = action; /* action */
1100  buspirate_jtag_command(fd, tmp, 3);
1101 }
1102 
1103 static void buspirate_jtag_get_adcs(int fd)
1104 {
1105  uint8_t tmp[10];
1106  uint16_t a, b, c, d;
1107  tmp[0] = CMD_READ_ADCS;
1108  buspirate_jtag_command(fd, tmp, 1);
1109  a = tmp[2] << 8 | tmp[3];
1110  b = tmp[4] << 8 | tmp[5];
1111  c = tmp[6] << 8 | tmp[7];
1112  d = tmp[8] << 8 | tmp[9];
1113 
1114  LOG_INFO("ADC: ADC_Pin = %.02f VPullup = %.02f V33 = %.02f "
1115  "V50 = %.02f",
1116  ((float)a)/155.1515, ((float)b)/155.1515,
1117  ((float)c)/155.1515, ((float)d)/155.1515);
1118 }
1119 
1120 static unsigned char buspirate_jtag_command(int fd,
1121  uint8_t *cmd, int cmdlen)
1122 {
1123  int res;
1124  int len = 0;
1125 
1126  res = buspirate_serial_write(fd, cmd, cmdlen);
1127 
1128  if ((cmd[0] == CMD_UART_SPEED)
1129  || (cmd[0] == CMD_PORT_MODE)
1130  || (cmd[0] == CMD_FEATURE)
1131  || (cmd[0] == CMD_JTAG_SPEED))
1132  return 1;
1133 
1134  if (res == cmdlen) {
1135  switch (cmd[0]) {
1136  case CMD_READ_ADCS:
1137  len = 10; /* 2*sizeof(char)+4*sizeof(uint16_t) */
1138  break;
1139  case CMD_TAP_SHIFT:
1140  len = cmdlen;
1141  break;
1142  default:
1143  LOG_INFO("Wrong !");
1144  }
1145  res = buspirate_serial_read(fd, cmd, len);
1146  if (res > 0)
1147  return (unsigned char)cmd[1];
1148  else
1149  return -1;
1150  } else
1151  return -1;
1152  return 0;
1153 }
1154 
1155 /* low level serial port */
1156 /* TODO add support for WIN32 and others ! */
1157 static int buspirate_serial_open(char *port)
1158 {
1159  return open(buspirate_port, O_RDWR | O_NOCTTY | O_NDELAY);
1160 }
1161 
1162 
1163 /* Returns -1 on error. */
1164 
1165 static int buspirate_serial_setspeed(int fd, char speed, cc_t timeout)
1166 {
1167  struct termios t_opt;
1168  speed_t baud = (speed == SERIAL_FAST) ? B1000000 : B115200;
1169 
1170  /* set the serial port parameters */
1171  fcntl(fd, F_SETFL, 0);
1172  if (tcgetattr(fd, &t_opt) != 0)
1173  return -1;
1174 
1175  if (cfsetispeed(&t_opt, baud) != 0)
1176  return -1;
1177 
1178  if (cfsetospeed(&t_opt, baud) != 0)
1179  return -1;
1180 
1181  t_opt.c_cflag |= (CLOCAL | CREAD);
1182  t_opt.c_cflag &= ~PARENB;
1183  t_opt.c_cflag &= ~CSTOPB;
1184  t_opt.c_cflag &= ~CSIZE;
1185  t_opt.c_cflag |= CS8;
1186  t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
1187 
1188  /* The serial port may have been configured for human interaction with
1189  the Bus Pirate console, but OpenOCD is going to use a binary protocol,
1190  so make sure to turn off any CR/LF translation and the like. */
1191  t_opt.c_iflag &= ~(IXON | IXOFF | IXANY | INLCR | ICRNL);
1192 
1193  t_opt.c_oflag &= ~OPOST;
1194  t_opt.c_cc[VMIN] = 0;
1195  t_opt.c_cc[VTIME] = timeout;
1196 
1197  /* Note that, in the past, TCSANOW was used below instead of TCSADRAIN,
1198  and CMD_UART_SPEED did not work properly then, at least with
1199  the Bus Pirate v3.5 (USB). */
1200  if (tcsetattr(fd, TCSADRAIN, &t_opt) != 0) {
1201  /* According to the Linux documentation, this is actually not enough
1202  to detect errors, you need to call tcgetattr() and check that
1203  all changes have been performed successfully. */
1204  return -1;
1205  }
1206 
1207  return 0;
1208 }
1209 
1210 static int buspirate_serial_write(int fd, uint8_t *buf, int size)
1211 {
1212  int ret = 0;
1213 
1214  ret = write(fd, buf, size);
1215 
1216  LOG_DEBUG("size = %d ret = %d", size, ret);
1218 
1219  if (ret != size)
1220  LOG_ERROR("Error sending data");
1221 
1222  return ret;
1223 }
1224 
1225 static int buspirate_serial_read(int fd, uint8_t *buf, int size)
1226 {
1227  int len = 0;
1228  int ret = 0;
1229  int timeout = 0;
1230 
1231  while (len < size) {
1232  ret = read(fd, buf+len, size-len);
1233  if (ret == -1)
1234  return -1;
1235 
1236  if (ret == 0) {
1237  timeout++;
1238 
1239  if (timeout >= 10)
1240  break;
1241 
1242  continue;
1243  }
1244 
1245  len += ret;
1246  }
1247 
1248  LOG_DEBUG("should have read = %d actual size = %d", size, len);
1249  buspirate_print_buffer(buf, len);
1250 
1251  if (len != size)
1252  LOG_ERROR("Error reading data");
1253 
1254  return len;
1255 }
1256 
1257 static void buspirate_serial_close(int fd)
1258 {
1259  close(fd);
1260 }
1261 
1262 #define LINE_SIZE 81
1263 #define BYTES_PER_LINE 16
1264 static void buspirate_print_buffer(uint8_t *buf, int size)
1265 {
1266  char line[LINE_SIZE];
1267  char tmp[10];
1268  int offset = 0;
1269 
1270  line[0] = 0;
1271  while (offset < size) {
1272  snprintf(tmp, 5, "%02x ", (uint8_t)buf[offset]);
1273  offset++;
1274 
1275  strcat(line, tmp);
1276 
1277  if (offset % BYTES_PER_LINE == 0) {
1278  LOG_DEBUG("%s", line);
1279  line[0] = 0;
1280  }
1281  }
1282 
1283  if (line[0] != 0)
1284  LOG_DEBUG("%s", line);
1285 }
1286 
1287 /************************* SWD related stuff **********/
1288 
1289 static int buspirate_swd_init(void)
1290 {
1291  LOG_INFO("Buspirate SWD mode enabled");
1292  swd_mode = true;
1293 
1294  return ERROR_OK;
1295 }
1296 
1298 {
1299  const uint8_t *sequence;
1300  int sequence_len;
1301  uint32_t no_bytes, sequence_offset;
1302 
1303  switch (seq) {
1304  case LINE_RESET:
1305  LOG_DEBUG("SWD line reset");
1307  sequence_len = DIV_ROUND_UP(swd_seq_line_reset_len, 8);
1308  break;
1309  case JTAG_TO_SWD:
1310  LOG_DEBUG("JTAG-to-SWD");
1312  sequence_len = DIV_ROUND_UP(swd_seq_jtag_to_swd_len, 8);
1313  break;
1314  case SWD_TO_JTAG:
1315  LOG_DEBUG("SWD-to-JTAG");
1317  sequence_len = DIV_ROUND_UP(swd_seq_swd_to_jtag_len, 8);
1318  break;
1319  default:
1320  LOG_ERROR("Sequence %d not supported", seq);
1321  return ERROR_FAIL;
1322  }
1323 
1324  no_bytes = sequence_len;
1325  sequence_offset = 0;
1326 
1327  while (no_bytes) {
1328  uint8_t tmp[17];
1329  uint32_t to_send;
1330 
1331  to_send = no_bytes > 16 ? 16 : no_bytes;
1332 
1333  tmp[0] = 0x10 + ((to_send - 1) & 0x0F);
1334  memcpy(tmp + 1, &sequence[sequence_offset], to_send);
1335 
1336  buspirate_serial_write(buspirate_fd, tmp, to_send + 1);
1337  buspirate_serial_read(buspirate_fd, tmp, to_send + 1);
1338 
1339  no_bytes -= to_send;
1340  sequence_offset += to_send;
1341  }
1342 
1343  return ERROR_OK;
1344 }
1345 
1346 static uint8_t buspirate_swd_write_header(uint8_t cmd)
1347 {
1348  uint8_t tmp[8];
1349  int to_send;
1350 
1351  tmp[0] = 0x10; /* bus pirate: send 1 byte */
1352  tmp[1] = cmd; /* swd cmd */
1353  tmp[2] = 0x07; /* ack __x */
1354  tmp[3] = 0x07; /* ack _x_ */
1355  tmp[4] = 0x07; /* ack x__ */
1356  tmp[5] = 0x07; /* write mode trn_1 */
1357  tmp[6] = 0x07; /* write mode trn_2 */
1358 
1359  to_send = ((cmd & SWD_CMD_RNW) == 0) ? 7 : 5;
1360  buspirate_serial_write(buspirate_fd, tmp, to_send);
1361 
1362  /* read ack */
1363  buspirate_serial_read(buspirate_fd, tmp, 2); /* drop pirate command ret vals */
1364  buspirate_serial_read(buspirate_fd, tmp, to_send - 2); /* ack bits */
1365 
1366  return tmp[2] << 2 | tmp[1] << 1 | tmp[0];
1367 }
1368 
1369 static void buspirate_swd_idle_clocks(uint32_t no_bits)
1370 {
1371  uint32_t no_bytes;
1372  uint8_t tmp[20];
1373 
1374  no_bytes = (no_bits + 7) / 8;
1375  memset(tmp + 1, 0x00, sizeof(tmp) - 1);
1376 
1377  /* unfortunately bus pirate misbehaves when clocks are sent in parts
1378  * so we need to limit at 128 clock cycles
1379  */
1380  if (no_bytes > 16)
1381  no_bytes = 16;
1382 
1383  while (no_bytes) {
1384  uint8_t to_send = no_bytes > 16 ? 16 : no_bytes;
1385  tmp[0] = 0x10 + ((to_send - 1) & 0x0F);
1386 
1387  buspirate_serial_write(buspirate_fd, tmp, to_send + 1);
1388  buspirate_serial_read(buspirate_fd, tmp, to_send + 1);
1389 
1390  no_bytes -= to_send;
1391  }
1392 }
1393 
1395 {
1396  buspirate_swd_write_reg(swd_cmd(false, false, DP_ABORT),
1398 }
1399 
1400 static int buspirate_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1401 {
1402  uint8_t tmp[16];
1403 
1404  LOG_DEBUG("buspirate_swd_read_reg");
1405  assert(cmd & SWD_CMD_RNW);
1406 
1408  uint8_t ack = buspirate_swd_write_header(cmd);
1409 
1410  /* do a read transaction */
1411  tmp[0] = 0x06; /* 4 data bytes */
1412  tmp[1] = 0x06;
1413  tmp[2] = 0x06;
1414  tmp[3] = 0x06;
1415  tmp[4] = 0x07; /* parity bit */
1416  tmp[5] = 0x21; /* 2 turnaround clocks */
1417 
1420 
1421  /* store the data and parity */
1422  uint32_t data = (uint8_t) tmp[0];
1423  data |= (uint8_t) tmp[1] << 8;
1424  data |= (uint8_t) tmp[2] << 16;
1425  data |= (uint8_t) tmp[3] << 24;
1426  int parity = tmp[4] ? 0x01 : 0x00;
1427 
1428  LOG_DEBUG("%s %s %s reg %X = %08"PRIx32,
1429  ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
1430  cmd & SWD_CMD_APNDP ? "AP" : "DP",
1431  cmd & SWD_CMD_RNW ? "read" : "write",
1432  (cmd & SWD_CMD_A32) >> 1,
1433  data);
1434 
1435  switch (ack) {
1436  case SWD_ACK_OK:
1437  if (parity != parity_u32(data)) {
1438  LOG_DEBUG("Read data parity mismatch %x %x", parity, parity_u32(data));
1439  return ERROR_SWD_FAIL;
1440  }
1441  if (value)
1442  *value = data;
1443  if (cmd & SWD_CMD_APNDP)
1444  buspirate_swd_idle_clocks(ap_delay_clk);
1445  return ERROR_OK;
1446  case SWD_ACK_WAIT:
1447  LOG_DEBUG("SWD_ACK_WAIT");
1449  return ERROR_WAIT;
1450  case SWD_ACK_FAULT:
1451  LOG_DEBUG("SWD_ACK_FAULT");
1452  return ERROR_SWD_FAULT;
1453  default:
1454  LOG_DEBUG("No valid acknowledge: ack=%d", ack);
1455  return ERROR_SWD_FAIL;
1456  }
1457 }
1458 
1459 static int buspirate_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1460 {
1461  uint8_t tmp[16];
1462 
1463  LOG_DEBUG("buspirate_swd_write_reg");
1464  assert(!(cmd & SWD_CMD_RNW));
1465 
1467  uint8_t ack = buspirate_swd_write_header(cmd);
1468 
1469  /* do a write transaction */
1470  tmp[0] = 0x10 + ((4 + 1 - 1) & 0xF); /* bus pirate: send 4+1 bytes */
1471  buf_set_u32((uint8_t *) tmp + 1, 0, 32, value);
1472  /* write sequence ends with parity bit and 7 idle ticks */
1473  tmp[5] = parity_u32(value) ? 0x01 : 0x00;
1474 
1477 
1478  LOG_DEBUG("%s %s %s reg %X = %08"PRIx32,
1479  ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
1480  cmd & SWD_CMD_APNDP ? "AP" : "DP",
1481  cmd & SWD_CMD_RNW ? "read" : "write",
1482  (cmd & SWD_CMD_A32) >> 1,
1483  value);
1484 
1485  switch (ack) {
1486  case SWD_ACK_OK:
1487  if (cmd & SWD_CMD_APNDP)
1488  buspirate_swd_idle_clocks(ap_delay_clk);
1489  return ERROR_OK;
1490  case SWD_ACK_WAIT:
1491  LOG_DEBUG("SWD_ACK_WAIT");
1493  return ERROR_WAIT;
1494  case SWD_ACK_FAULT:
1495  LOG_DEBUG("SWD_ACK_FAULT");
1496  return ERROR_SWD_FAULT;
1497  default:
1498  LOG_DEBUG("No valid acknowledge: ack=%d", ack);
1499  return ERROR_SWD_FAIL;
1500  }
1501 }
1502 
1503 static int buspirate_swd_run_queue(void)
1504 {
1505  LOG_DEBUG("buspirate_swd_run_queue");
1506  /* A transaction must be followed by another transaction or at least 8 idle cycles to
1507  * ensure that data is clocked through the AP. */
1509 
1510  return ERROR_OK;
1511 }
#define SWD_ACK_FAULT
Definition: arm_adi_v5.h:33
#define DP_ABORT
Definition: arm_adi_v5.h:46
#define WDERRCLR
Definition: arm_adi_v5.h:71
#define STKERRCLR
Definition: arm_adi_v5.h:70
#define ORUNERRCLR
Definition: arm_adi_v5.h:72
#define STKCMPCLR
Definition: arm_adi_v5.h:69
swd_special_seq
Definition: arm_adi_v5.h:236
@ JTAG_TO_SWD
Definition: arm_adi_v5.h:238
@ LINE_RESET
Definition: arm_adi_v5.h:237
@ SWD_TO_JTAG
Definition: arm_adi_v5.h:240
#define SWD_ACK_WAIT
Definition: arm_adi_v5.h:32
#define SWD_ACK_OK
Definition: arm_adi_v5.h:31
enum arm_mode mode
Definition: armv4_5.c:280
void * buf_set_buf(const void *_src, unsigned int src_start, void *_dst, unsigned int dst_start, unsigned int len)
Definition: binarybuffer.c:120
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
static void buspirate_tap_init(void)
Definition: buspirate.c:698
static void buspirate_end_state(enum tap_state state)
Definition: buspirate.c:555
static const struct command_registration buspirate_subcommand_handlers[]
Definition: buspirate.c:464
static enum tap_state last_tap_state
Definition: buspirate.c:110
static int buspirate_tap_execute(void)
Definition: buspirate.c:704
#define BUSPIRATE_MAX_PENDING_SCANS
Definition: buspirate.c:680
static void buspirate_path_move(unsigned int num_states, enum tap_state *path)
Definition: buspirate.c:581
#define SHORT_TIMEOUT
Definition: buspirate.c:65
static void buspirate_swd_clear_sticky_errors(void)
Definition: buspirate.c:1394
static void buspirate_state_move(void)
Definition: buspirate.c:565
static int buspirate_pinmode
Definition: buspirate.c:104
#define CMD_ENTER_RWIRE
Definition: buspirate.c:42
#define B1000000
Definition: buspirate.c:62
static void buspirate_tap_make_space(int scan, int bits)
Definition: buspirate.c:776
static void buspirate_runtest(unsigned int num_cycles)
Definition: buspirate.c:603
static const struct swd_driver buspirate_swd
Definition: buspirate.c:528
@ SPEED_RAW_50_KHZ
Definition: buspirate.c:94
@ SPEED_RAW_400_KHZ
Definition: buspirate.c:96
@ SPEED_RAW_100_KHZ
Definition: buspirate.c:95
@ SPEED_RAW_5_KHZ
Definition: buspirate.c:93
static void buspirate_swd_set_feature(int, char, char)
Definition: buspirate.c:937
static const struct command_registration buspirate_command_handlers[]
Definition: buspirate.c:517
static char * buspirate_port
Definition: buspirate.c:108
struct adapter_driver buspirate_adapter_driver
Definition: buspirate.c:540
static int buspirate_fd
Definition: buspirate.c:103
static void buspirate_jtag_set_feature(int, char, char)
Definition: buspirate.c:1094
static int buspirate_baudrate
Definition: buspirate.c:105
#define CMD_TAP_SHIFT_HEADER_LEN
Definition: buspirate.c:50
static void buspirate_print_buffer(uint8_t *buf, int size)
Definition: buspirate.c:1264
static int buspirate_pullup
Definition: buspirate.c:107
static int buspirate_swd_init(void)
Definition: buspirate.c:1289
static void buspirate_jtag_reset(int)
Definition: buspirate.c:1036
static struct pending_scan_result tap_pending_scans[BUSPIRATE_MAX_PENDING_SCANS]
Definition: buspirate.c:694
static bool swd_mode
Definition: buspirate.c:100
static unsigned char buspirate_jtag_command(int, uint8_t *, int)
Definition: buspirate.c:1120
@ MODE_HIZ
Definition: buspirate.c:69
@ MODE_JTAG
Definition: buspirate.c:70
@ MODE_JTAG_OD
Definition: buspirate.c:71
static int buspirate_vreg
Definition: buspirate.c:106
static uint8_t buspirate_swd_write_header(uint8_t cmd)
Definition: buspirate.c:1346
static uint8_t tms_chain[BUSPIRATE_BUFFER_SIZE]
Definition: buspirate.c:682
#define CMD_RAW_PERIPH
Definition: buspirate.c:46
static void buspirate_set_speed(int, char)
Definition: buspirate.c:882
#define CMD_RAW_MODE
Definition: buspirate.c:48
static void buspirate_set_mode(int, char)
Definition: buspirate.c:874
static void buspirate_jtag_get_adcs(int)
Definition: buspirate.c:1103
#define CMD_RAW_CONFIG_LSB
Definition: buspirate.c:58
static int buspirate_serial_read(int fd, uint8_t *buf, int size)
Definition: buspirate.c:1225
static void buspirate_set_feature(int, char, char)
Definition: buspirate.c:866
static int buspirate_execute_queue(struct jtag_command *cmd_queue)
Definition: buspirate.c:153
#define CMD_FEATURE
Definition: buspirate.c:38
#define CMD_TAP_SHIFT
Definition: buspirate.c:41
static void buspirate_stableclocks(unsigned int num_cycles)
Definition: buspirate.c:655
static void buspirate_swd_idle_clocks(uint32_t no_bits)
Definition: buspirate.c:1369
#define CMD_UART_SPEED
Definition: buspirate.c:44
static uint8_t tdi_chain[BUSPIRATE_BUFFER_SIZE]
Definition: buspirate.c:683
static int tap_pending_scans_num
Definition: buspirate.c:696
static int buspirate_serial_write(int fd, uint8_t *buf, int size)
Definition: buspirate.c:1210
static int buspirate_swd_run_queue(void)
Definition: buspirate.c:1503
static char swd_features
Definition: buspirate.c:101
@ SERIAL_NORMAL
Definition: buspirate.c:88
@ SERIAL_FAST
Definition: buspirate.c:89
#define NORMAL_TIMEOUT
Definition: buspirate.c:66
static void buspirate_swd_set_speed(int, char)
Definition: buspirate.c:893
static int buspirate_serial_setspeed(int fd, char speed, cc_t timeout)
Definition: buspirate.c:1165
static void buspirate_swd_set_mode(int, char)
Definition: buspirate.c:914
#define BYTES_PER_LINE
Definition: buspirate.c:1263
#define LINE_SIZE
Definition: buspirate.c:1262
static bool read_and_discard_all_data(const int fd)
Definition: buspirate.c:229
static void buspirate_jtag_set_speed(int, char)
Definition: buspirate.c:1052
#define CMD_RAW_CONFIG_3V3
Definition: buspirate.c:54
static void buspirate_serial_close(int fd)
Definition: buspirate.c:1257
COMMAND_HANDLER(buspirate_handle_adc_command)
Definition: buspirate.c:351
static struct jtag_interface buspirate_interface
Definition: buspirate.c:536
static int buspirate_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: buspirate.c:1459
static void buspirate_bbio_enable(int)
Definition: buspirate.c:975
static int buspirate_quit(void)
Definition: buspirate.c:335
static int buspirate_serial_open(char *port)
Definition: buspirate.c:1157
#define CMD_PORT_MODE
Definition: buspirate.c:37
static void buspirate_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command)
Definition: buspirate.c:829
@ ACTION_ENABLE
Definition: buspirate.c:84
@ ACTION_DISABLE
Definition: buspirate.c:83
static int buspirate_init(void)
Definition: buspirate.c:274
static int buspirate_reset(int trst, int srst)
Definition: buspirate.c:849
static void buspirate_jtag_set_mode(int, char)
Definition: buspirate.c:1086
#define CMD_JTAG_SPEED
Definition: buspirate.c:45
static int tap_chain_index
Definition: buspirate.c:684
static int buspirate_swd_switch_seq(enum swd_special_seq seq)
Definition: buspirate.c:1297
#define CMD_ENTER_OOCD
Definition: buspirate.c:43
static void buspirate_tap_append(int tms, int tdi)
Definition: buspirate.c:785
static void buspirate_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command)
Definition: buspirate.c:626
#define CMD_RAW_SPEED
Definition: buspirate.c:47
#define BUSPIRATE_BUFFER_SIZE
Definition: buspirate.c:669
static int buspirate_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: buspirate.c:1400
@ FEATURE_SRST
Definition: buspirate.c:78
@ FEATURE_PULLUP
Definition: buspirate.c:79
@ FEATURE_LED
Definition: buspirate.c:75
@ FEATURE_VREG
Definition: buspirate.c:76
@ FEATURE_TRST
Definition: buspirate.c:77
#define CMD_READ_ADCS
Definition: buspirate.c:39
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
int jtag_build_buffer(const struct scan_command *cmd, uint8_t **buffer)
Definition: commands.c:192
enum scan_type jtag_scan_type(const struct scan_command *cmd)
Definition: commands.c:167
int jtag_read_buffer(uint8_t *buffer, const struct scan_command *cmd)
Definition: commands.c:230
scan_type
The inferred type of a scan_command structure, indicating whether the command has the host scan in fr...
Definition: commands.h:22
@ JTAG_TLR_RESET
Definition: commands.h:137
@ JTAG_SCAN
Definition: commands.h:129
@ JTAG_PATHMOVE
Definition: commands.h:140
@ JTAG_STABLECLOCKS
Definition: commands.h:142
@ JTAG_RUNTEST
Definition: commands.h:138
@ JTAG_SLEEP
Definition: commands.h:141
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint8_t type
Definition: esp_usb_jtag.c:0
uint8_t length
Definition: esp_usb_jtag.c:1
enum tap_state tap_get_end_state(void)
For more information,.
Definition: interface.c:56
enum tap_state tap_state_transition(enum tap_state cur_state, bool tms)
Function tap_state_transition takes a current TAP state and returns the next state according to the t...
Definition: interface.c:223
const char * tap_state_name(enum tap_state state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
Definition: interface.c:344
int tap_get_tms_path_len(enum tap_state from, enum tap_state to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
Definition: interface.c:195
void tap_set_end_state(enum tap_state new_end_state)
This function sets the state of an "end state follower" which tracks the state that any cable driver ...
Definition: interface.c:48
enum tap_state tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
Definition: interface.c:37
bool tap_is_state_stable(enum tap_state astate)
Function tap_is_state_stable returns true if the astate is stable.
Definition: interface.c:200
int tap_get_tms_path(enum tap_state from, enum tap_state to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
Definition: interface.c:190
#define tap_set_state(new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
Definition: interface.h:50
static enum tap_state jtag_debug_state_machine(const void *tms_buf, const void *tdi_buf, unsigned int tap_len, enum tap_state start_tap_state)
Prints verbose TAP state transitions for the given TMS/TDI buffers.
Definition: interface.h:168
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1058
#define ERROR_JTAG_DEVICE_ERROR
Definition: jtag.h:558
tap_state
Defines JTAG Test Access Port states.
Definition: jtag.h:37
@ TAP_RESET
Definition: jtag.h:56
@ TAP_DRPAUSE
Definition: jtag.h:44
@ TAP_IRSHIFT
Definition: jtag.h:51
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_DRSHIFT
Definition: jtag.h:43
@ TAP_IRPAUSE
Definition: jtag.h:52
#define ERROR_JTAG_QUEUE_FAILED
Definition: jtag.h:556
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:552
static struct scan_blk scan
Definition: lakemont.c:60
#define ERROR_WAIT
Definition: log.h:189
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:116
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
static uint32_t bit(uint32_t value, unsigned int b)
Definition: opcodes.h:39
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
Represents a driver for a debugging interface.
Definition: interface.h:214
const char *const name
The name of the interface driver.
Definition: interface.h:216
const char * name
Definition: command.h:239
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
Represents a driver for a debugging interface.
Definition: interface.h:189
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
Definition: interface.h:202
int first
First bit position in tdo_buffer to read.
Definition: arm-jtag-ew.c:511
int length
Number of bits to read.
Definition: arm-jtag-ew.c:512
uint8_t * buffer
Location to store the result.
Definition: arm-jtag-ew.c:514
struct scan_command * command
Definition: arm-jtag-ew.c:513
The scan_command provide a means of encapsulating a set of scan_field structures that should be scann...
Definition: commands.h:35
Definition: osbdm.c:17
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
Definition: swd.h:255
Definition: psoc6.c:83
#define SWD_CMD_A32
Definition: swd.h:19
#define SWD_CMD_PARK
Definition: swd.h:22
static uint8_t swd_cmd(bool is_read, bool is_ap, uint8_t regnum)
Construct a "cmd" byte, in lSB bit order, which swd_driver.read_reg() and swd_driver....
Definition: swd.h:35
static const unsigned int swd_seq_jtag_to_swd_len
Definition: swd.h:125
#define ERROR_SWD_FAIL
Definition: swd.h:28
static const unsigned int swd_seq_line_reset_len
Definition: swd.h:104
#define SWD_CMD_APNDP
Definition: swd.h:17
#define SWD_CMD_START
Definition: swd.h:16
#define SWD_CMD_RNW
Definition: swd.h:18
#define ERROR_SWD_FAULT
Definition: swd.h:29
static const uint8_t swd_seq_line_reset[]
SWD Line reset.
Definition: swd.h:98
static const uint8_t swd_seq_jtag_to_swd[]
JTAG-to-SWD sequence.
Definition: swd.h:115
static const uint8_t swd_seq_swd_to_jtag[]
SWD-to-JTAG sequence.
Definition: swd.h:136
static const unsigned int swd_seq_swd_to_jtag_len
Definition: swd.h:144
#define TRANSPORT_SWD
Definition: transport.h:20
#define TRANSPORT_JTAG
Definition: transport.h:19
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
static int parity_u32(uint32_t x)
Calculate the (even) parity of a 32-bit datum.
Definition: types.h:265
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t state[4]
Definition: vdebug.c:21
static unsigned int parity(unsigned int v)
Definition: xscale.c:619