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