OpenOCD
bitbang.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007,2008 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  ***************************************************************************/
10 
11 /* 2014-12: Addition of the SWD protocol support is based on the initial work
12  * by Paul Fertser and modifications by Jean-Christian de Rivaz. */
13 
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 #include <jtag/jtag.h> /* Added to avoid include loop in commands.h */
19 #include "bitbang.h"
20 #include <jtag/interface.h>
21 #include <jtag/commands.h>
22 
23 #include <helper/time_support.h>
24 
25 /* Timeout for retrying on SWD WAIT in msec */
26 #define SWD_WAIT_TIMEOUT 500
27 
36 static int bitbang_stableclocks(unsigned int num_cycles);
37 
38 static void bitbang_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk);
39 
41 
42 /* DANGER!!!! clock absolutely *MUST* be 0 in idle or reset won't work!
43  *
44  * Set this to 1 and str912 reset halt will fail.
45  *
46  * If someone can submit a patch with an explanation it will be greatly
47  * appreciated, but as far as I can tell (ØH) DCLK is generated upon
48  * clk = 0 in TAP_IDLE. Good luck deducing that from the ARM documentation!
49  * The ARM documentation uses the term "DCLK is asserted while in the TAP_IDLE
50  * state". With hardware there is no such thing as *while* in a state. There
51  * are only edges. So clk => 0 is in fact a very subtle state transition that
52  * happens *while* in the TAP_IDLE state. "#&¤"#¤&"#&"#&
53  *
54  * For "reset halt" the last thing that happens before srst is asserted
55  * is that the breakpoint is set up. If DCLK is not wiggled one last
56  * time before the reset, then the breakpoint is not set up and
57  * "reset halt" will fail to halt.
58  *
59  */
60 #define CLOCK_IDLE() 0
61 
62 /* The bitbang driver leaves the TCK 0 when in idle */
64 {
65  assert(tap_is_state_stable(state));
67 }
68 
69 static int bitbang_state_move(int skip)
70 {
71  int i = 0, tms = 0;
72  uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
74 
75  for (i = skip; i < tms_count; i++) {
76  tms = (tms_scan >> i) & 1;
77  if (bitbang_interface->write(0, tms, 0) != ERROR_OK)
78  return ERROR_FAIL;
79  if (bitbang_interface->write(1, tms, 0) != ERROR_OK)
80  return ERROR_FAIL;
81  }
82  if (bitbang_interface->write(CLOCK_IDLE(), tms, 0) != ERROR_OK)
83  return ERROR_FAIL;
84 
86  return ERROR_OK;
87 }
88 
94 {
95  unsigned int num_bits = cmd->cmd.tms->num_bits;
96  const uint8_t *bits = cmd->cmd.tms->bits;
97 
98  LOG_DEBUG_IO("TMS: %u bits", num_bits);
99 
100  int tms = 0;
101  for (unsigned int i = 0; i < num_bits; i++) {
102  tms = ((bits[i/8] >> (i % 8)) & 1);
103  if (bitbang_interface->write(0, tms, 0) != ERROR_OK)
104  return ERROR_FAIL;
105  if (bitbang_interface->write(1, tms, 0) != ERROR_OK)
106  return ERROR_FAIL;
107  }
108  if (bitbang_interface->write(CLOCK_IDLE(), tms, 0) != ERROR_OK)
109  return ERROR_FAIL;
110 
111  return ERROR_OK;
112 }
113 
115 {
116  unsigned int num_states = cmd->num_states;
117  int state_count;
118  int tms = 0;
119 
120  state_count = 0;
121  while (num_states) {
122  if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
123  tms = 0;
124  else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
125  tms = 1;
126  else {
127  LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
129  tap_state_name(cmd->path[state_count]));
130  exit(-1);
131  }
132 
133  if (bitbang_interface->write(0, tms, 0) != ERROR_OK)
134  return ERROR_FAIL;
135  if (bitbang_interface->write(1, tms, 0) != ERROR_OK)
136  return ERROR_FAIL;
137 
138  tap_set_state(cmd->path[state_count]);
139  state_count++;
140  num_states--;
141  }
142 
143  if (bitbang_interface->write(CLOCK_IDLE(), tms, 0) != ERROR_OK)
144  return ERROR_FAIL;
145 
147  return ERROR_OK;
148 }
149 
150 static int bitbang_runtest(unsigned int num_cycles)
151 {
152  tap_state_t saved_end_state = tap_get_end_state();
153 
154  /* only do a state_move when we're not already in IDLE */
155  if (tap_get_state() != TAP_IDLE) {
157  if (bitbang_state_move(0) != ERROR_OK)
158  return ERROR_FAIL;
159  }
160 
161  /* execute num_cycles */
162  for (unsigned int i = 0; i < num_cycles; i++) {
163  if (bitbang_interface->write(0, 0, 0) != ERROR_OK)
164  return ERROR_FAIL;
165  if (bitbang_interface->write(1, 0, 0) != ERROR_OK)
166  return ERROR_FAIL;
167  }
168  if (bitbang_interface->write(CLOCK_IDLE(), 0, 0) != ERROR_OK)
169  return ERROR_FAIL;
170 
171  /* finish in end_state */
172  bitbang_end_state(saved_end_state);
173  if (tap_get_state() != tap_get_end_state())
174  if (bitbang_state_move(0) != ERROR_OK)
175  return ERROR_FAIL;
176 
177  return ERROR_OK;
178 }
179 
180 static int bitbang_stableclocks(unsigned int num_cycles)
181 {
182  int tms = (tap_get_state() == TAP_RESET ? 1 : 0);
183 
184  /* send num_cycles clocks onto the cable */
185  for (unsigned int i = 0; i < num_cycles; i++) {
186  if (bitbang_interface->write(1, tms, 0) != ERROR_OK)
187  return ERROR_FAIL;
188  if (bitbang_interface->write(0, tms, 0) != ERROR_OK)
189  return ERROR_FAIL;
190  }
191 
192  return ERROR_OK;
193 }
194 
195 static int bitbang_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
196  unsigned int scan_size)
197 {
198  tap_state_t saved_end_state = tap_get_end_state();
199  unsigned int bit_cnt;
200 
201  if (!((!ir_scan &&
202  (tap_get_state() == TAP_DRSHIFT)) ||
203  (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) {
204  if (ir_scan)
206  else
208 
209  if (bitbang_state_move(0) != ERROR_OK)
210  return ERROR_FAIL;
211  bitbang_end_state(saved_end_state);
212  }
213 
214  size_t buffered = 0;
215  for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++) {
216  int tms = (bit_cnt == scan_size-1) ? 1 : 0;
217  int tdi;
218  int bytec = bit_cnt/8;
219  int bcval = 1 << (bit_cnt % 8);
220 
221  /* if we're just reading the scan, but don't care about the output
222  * default to outputting 'low', this also makes valgrind traces more readable,
223  * as it removes the dependency on an uninitialised value
224  */
225  tdi = 0;
226  if ((type != SCAN_IN) && (buffer[bytec] & bcval))
227  tdi = 1;
228 
229  if (bitbang_interface->write(0, tms, tdi) != ERROR_OK)
230  return ERROR_FAIL;
231 
232  if (type != SCAN_OUT) {
235  return ERROR_FAIL;
236  buffered++;
237  } else {
238  switch (bitbang_interface->read()) {
239  case BB_LOW:
240  buffer[bytec] &= ~bcval;
241  break;
242  case BB_HIGH:
243  buffer[bytec] |= bcval;
244  break;
245  default:
246  return ERROR_FAIL;
247  }
248  }
249  }
250 
251  if (bitbang_interface->write(1, tms, tdi) != ERROR_OK)
252  return ERROR_FAIL;
253 
255  (buffered == bitbang_interface->buf_size ||
256  bit_cnt == scan_size - 1)) {
257  for (unsigned int i = bit_cnt + 1 - buffered; i <= bit_cnt; i++) {
258  switch (bitbang_interface->read_sample()) {
259  case BB_LOW:
260  buffer[i/8] &= ~(1 << (i % 8));
261  break;
262  case BB_HIGH:
263  buffer[i/8] |= 1 << (i % 8);
264  break;
265  default:
266  return ERROR_FAIL;
267  }
268  }
269  buffered = 0;
270  }
271  }
272 
273  if (tap_get_state() != tap_get_end_state()) {
274  /* we *KNOW* the above loop transitioned out of
275  * the shift state, so we skip the first state
276  * and move directly to the end state.
277  */
278  if (bitbang_state_move(1) != ERROR_OK)
279  return ERROR_FAIL;
280  }
281  return ERROR_OK;
282 }
283 
284 static void bitbang_sleep(unsigned int microseconds)
285 {
286  if (bitbang_interface->sleep) {
287  bitbang_interface->sleep(microseconds);
288  } else {
289  jtag_sleep(microseconds);
290  }
291 }
292 
293 int bitbang_execute_queue(struct jtag_command *cmd_queue)
294 {
295  struct jtag_command *cmd = cmd_queue; /* currently processed command */
296  int scan_size;
297  enum scan_type type;
298  uint8_t *buffer;
299  int retval;
300 
301  if (!bitbang_interface) {
302  LOG_ERROR("BUG: Bitbang interface called, but not yet initialized");
303  exit(-1);
304  }
305 
306  /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
307  * that wasn't handled by a caller-provided error handler
308  */
309  retval = ERROR_OK;
310 
311  if (bitbang_interface->blink) {
312  if (bitbang_interface->blink(true) != ERROR_OK)
313  return ERROR_FAIL;
314  }
315 
316  while (cmd) {
317  switch (cmd->type) {
318  case JTAG_RUNTEST:
319  LOG_DEBUG_IO("runtest %u cycles, end in %s",
320  cmd->cmd.runtest->num_cycles,
321  tap_state_name(cmd->cmd.runtest->end_state));
322  bitbang_end_state(cmd->cmd.runtest->end_state);
323  if (bitbang_runtest(cmd->cmd.runtest->num_cycles) != ERROR_OK)
324  return ERROR_FAIL;
325  break;
326 
327  case JTAG_STABLECLOCKS:
328  /* this is only allowed while in a stable state. A check for a stable
329  * state was done in jtag_add_clocks()
330  */
331  if (bitbang_stableclocks(cmd->cmd.stableclocks->num_cycles) != ERROR_OK)
332  return ERROR_FAIL;
333  break;
334 
335  case JTAG_TLR_RESET:
336  LOG_DEBUG_IO("statemove end in %s",
337  tap_state_name(cmd->cmd.statemove->end_state));
338  bitbang_end_state(cmd->cmd.statemove->end_state);
339  if (bitbang_state_move(0) != ERROR_OK)
340  return ERROR_FAIL;
341  break;
342  case JTAG_PATHMOVE:
343  LOG_DEBUG_IO("pathmove: %u states, end in %s",
344  cmd->cmd.pathmove->num_states,
345  tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
346  if (bitbang_path_move(cmd->cmd.pathmove) != ERROR_OK)
347  return ERROR_FAIL;
348  break;
349  case JTAG_SCAN:
350  bitbang_end_state(cmd->cmd.scan->end_state);
351  scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
352  LOG_DEBUG_IO("%s scan %d bits; end in %s",
353  (cmd->cmd.scan->ir_scan) ? "IR" : "DR",
354  scan_size,
355  tap_state_name(cmd->cmd.scan->end_state));
356  type = jtag_scan_type(cmd->cmd.scan);
357  if (bitbang_scan(cmd->cmd.scan->ir_scan, type, buffer,
358  scan_size) != ERROR_OK)
359  return ERROR_FAIL;
360  if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
361  retval = ERROR_JTAG_QUEUE_FAILED;
362  free(buffer);
363  break;
364  case JTAG_SLEEP:
365  LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
367  return ERROR_FAIL;
368  bitbang_sleep(cmd->cmd.sleep->us);
369  break;
370  case JTAG_TMS:
371  retval = bitbang_execute_tms(cmd);
372  break;
373  default:
374  LOG_ERROR("BUG: unknown JTAG command type encountered");
375  exit(-1);
376  }
377  cmd = cmd->next;
378  }
379  if (bitbang_interface->blink) {
380  if (bitbang_interface->blink(false) != ERROR_OK)
381  return ERROR_FAIL;
382  }
383 
384  return retval;
385 }
386 
387 static int queued_retval;
388 
389 static int bitbang_swd_init(void)
390 {
391  LOG_DEBUG("bitbang_swd_init");
392  return ERROR_OK;
393 }
394 
395 static void bitbang_swd_exchange(bool rnw, uint8_t buf[], unsigned int offset, unsigned int bit_cnt)
396 {
397  if (bitbang_interface->blink) {
398  /* FIXME: we should manage errors */
399  bitbang_interface->blink(true);
400  }
401 
402  for (unsigned int i = offset; i < bit_cnt + offset; i++) {
403  int bytec = i/8;
404  int bcval = 1 << (i % 8);
405  int swdio = !rnw && (buf[bytec] & bcval);
406 
407  bitbang_interface->swd_write(0, swdio);
408 
409  if (rnw && buf) {
411  buf[bytec] |= bcval;
412  else
413  buf[bytec] &= ~bcval;
414  }
415 
416  bitbang_interface->swd_write(1, swdio);
417  }
418 
419  if (bitbang_interface->blink) {
420  /* FIXME: we should manage errors */
421  bitbang_interface->blink(false);
422  }
423 }
424 
426 {
427  switch (seq) {
428  case LINE_RESET:
429  LOG_DEBUG_IO("SWD line reset");
431  break;
432  case JTAG_TO_SWD:
433  LOG_DEBUG("JTAG-to-SWD");
435  break;
436  case JTAG_TO_DORMANT:
437  LOG_DEBUG("JTAG-to-DORMANT");
439  break;
440  case SWD_TO_JTAG:
441  LOG_DEBUG("SWD-to-JTAG");
443  break;
444  case SWD_TO_DORMANT:
445  LOG_DEBUG("SWD-to-DORMANT");
447  break;
448  case DORMANT_TO_SWD:
449  LOG_DEBUG("DORMANT-to-SWD");
451  break;
452  case DORMANT_TO_JTAG:
453  LOG_DEBUG("DORMANT-to-JTAG");
455  break;
456  default:
457  LOG_ERROR("Sequence %d not supported", seq);
458  return ERROR_FAIL;
459  }
460 
461  return ERROR_OK;
462 }
463 
464 static void swd_clear_sticky_errors(void)
465 {
466  bitbang_swd_write_reg(swd_cmd(false, false, DP_ABORT),
468 }
469 
470 static void bitbang_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
471 {
472  assert(cmd & SWD_CMD_RNW);
473 
474  if (queued_retval != ERROR_OK) {
475  LOG_DEBUG("Skip bitbang_swd_read_reg because queued_retval=%d", queued_retval);
476  return;
477  }
478 
479  int64_t timeout = timeval_ms() + SWD_WAIT_TIMEOUT;
480  for (unsigned int retry = 0;; retry++) {
481  uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)];
482 
484  bitbang_swd_exchange(false, &cmd, 0, 8);
485 
487  bitbang_swd_exchange(true, trn_ack_data_parity_trn, 0, 1 + 3 + 32 + 1 + 1);
489 
490  int ack = buf_get_u32(trn_ack_data_parity_trn, 1, 3);
491  uint32_t data = buf_get_u32(trn_ack_data_parity_trn, 1 + 3, 32);
492  int parity = buf_get_u32(trn_ack_data_parity_trn, 1 + 3 + 32, 1);
493 
494  LOG_CUSTOM_LEVEL((ack != SWD_ACK_OK && (retry == 0 || ack != SWD_ACK_WAIT))
496  "%s %s read reg %X = %08" PRIx32,
497  ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
498  cmd & SWD_CMD_APNDP ? "AP" : "DP",
499  (cmd & SWD_CMD_A32) >> 1,
500  data);
501 
502  if (ack == SWD_ACK_WAIT && timeval_ms() <= timeout) {
504  if (retry > 20)
505  alive_sleep(1);
506 
507  continue;
508  }
509  if (retry > 1)
510  LOG_DEBUG("SWD WAIT: retried %u times", retry);
511 
512  if (ack != SWD_ACK_OK) {
514  return;
515  }
516 
517  if (parity != parity_u32(data)) {
518  LOG_ERROR("Wrong parity detected");
520  return;
521  }
522  if (value)
523  *value = data;
524  if (cmd & SWD_CMD_APNDP)
525  bitbang_swd_exchange(true, NULL, 0, ap_delay_clk);
526  return;
527  }
528 }
529 
530 static void bitbang_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
531 {
532  assert(!(cmd & SWD_CMD_RNW));
533 
534  if (queued_retval != ERROR_OK) {
535  LOG_DEBUG("Skip bitbang_swd_write_reg because queued_retval=%d", queued_retval);
536  return;
537  }
538 
539  int64_t timeout = timeval_ms() + SWD_WAIT_TIMEOUT;
540 
541  /* Devices do not reply to DP_TARGETSEL write cmd, ignore received ack */
542  bool check_ack = swd_cmd_returns_ack(cmd);
543 
544  /* init the array to silence scan-build */
545  uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)] = {0};
546  for (unsigned int retry = 0;; retry++) {
547  buf_set_u32(trn_ack_data_parity_trn, 1 + 3 + 1, 32, value);
548  buf_set_u32(trn_ack_data_parity_trn, 1 + 3 + 1 + 32, 1, parity_u32(value));
549 
551  bitbang_swd_exchange(false, &cmd, 0, 8);
552 
554  bitbang_swd_exchange(true, trn_ack_data_parity_trn, 0, 1 + 3);
555 
556  /* Avoid a glitch on SWDIO when changing the direction to output.
557  * To keep performance penalty minimal, pre-write the first data
558  * bit to SWDIO GPIO output buffer while clocking the turnaround bit.
559  * Following swdio_drive(true) outputs the pre-written value
560  * and the same value is rewritten by the next swd_write()
561  * instead of glitching SWDIO
562  * HiZ/pull-up --------------> 0 -------------> 1
563  * swdio_drive(true) swd_write(0,1)
564  * in case of data bit 0 = 1
565  */
566  bitbang_swd_exchange(false, trn_ack_data_parity_trn, 1 + 3 + 1, 1);
568  bitbang_swd_exchange(false, trn_ack_data_parity_trn, 1 + 3 + 1, 32 + 1);
569 
570  int ack = buf_get_u32(trn_ack_data_parity_trn, 1, 3);
571  LOG_CUSTOM_LEVEL((check_ack && ack != SWD_ACK_OK && (retry == 0 || ack != SWD_ACK_WAIT))
573  "%s%s %s write reg %X = %08" PRIx32,
574  check_ack ? "" : "ack ignored ",
575  ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
576  cmd & SWD_CMD_APNDP ? "AP" : "DP",
577  (cmd & SWD_CMD_A32) >> 1,
578  buf_get_u32(trn_ack_data_parity_trn, 1 + 3 + 1, 32));
579 
580  if (check_ack && ack == SWD_ACK_WAIT && timeval_ms() <= timeout) {
582  if (retry > 20)
583  alive_sleep(1);
584 
585  continue;
586  }
587 
588  if (retry > 1)
589  LOG_DEBUG("SWD WAIT: retried %u times", retry);
590 
591  if (check_ack && ack != SWD_ACK_OK) {
593  return;
594  }
595 
596  if (cmd & SWD_CMD_APNDP)
597  bitbang_swd_exchange(true, NULL, 0, ap_delay_clk);
598  return;
599  }
600 }
601 
602 static int bitbang_swd_run_queue(void)
603 {
604  /* A transaction must be followed by another transaction or at least 8 idle cycles to
605  * ensure that data is clocked through the AP. */
606  bitbang_swd_exchange(true, NULL, 0, 8);
607 
608  int retval = queued_retval;
610  LOG_DEBUG_IO("SWD queue return value: %02x", retval);
611  return retval;
612 }
613 
614 const struct swd_driver bitbang_swd = {
616  .switch_seq = bitbang_swd_switch_seq,
617  .read_reg = bitbang_swd_read_reg,
618  .write_reg = bitbang_swd_write_reg,
619  .run = bitbang_swd_run_queue,
620 };
#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
@ DORMANT_TO_JTAG
Definition: arm_adi_v5.h:243
@ JTAG_TO_SWD
Definition: arm_adi_v5.h:238
@ DORMANT_TO_SWD
Definition: arm_adi_v5.h:242
@ LINE_RESET
Definition: arm_adi_v5.h:237
@ JTAG_TO_DORMANT
Definition: arm_adi_v5.h:239
@ SWD_TO_DORMANT
Definition: arm_adi_v5.h:241
@ 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
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
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 int bitbang_execute_tms(struct jtag_command *cmd)
Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG (or SWD) state machine.
Definition: bitbang.c:93
static int queued_retval
Definition: bitbang.c:387
struct bitbang_interface * bitbang_interface
Definition: bitbang.c:40
static int bitbang_stableclocks(unsigned int num_cycles)
Function bitbang_stableclocks issues a number of clock cycles while staying in a stable state.
Definition: bitbang.c:180
static int bitbang_path_move(struct pathmove_command *cmd)
Definition: bitbang.c:114
static int bitbang_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, unsigned int scan_size)
Definition: bitbang.c:195
static int bitbang_swd_init(void)
Definition: bitbang.c:389
static void bitbang_swd_exchange(bool rnw, uint8_t buf[], unsigned int offset, unsigned int bit_cnt)
Definition: bitbang.c:395
static int bitbang_runtest(unsigned int num_cycles)
Definition: bitbang.c:150
static void bitbang_end_state(tap_state_t state)
Definition: bitbang.c:63
static void bitbang_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: bitbang.c:530
static int bitbang_swd_switch_seq(enum swd_special_seq seq)
Definition: bitbang.c:425
static void swd_clear_sticky_errors(void)
Definition: bitbang.c:464
#define CLOCK_IDLE()
Definition: bitbang.c:60
int bitbang_execute_queue(struct jtag_command *cmd_queue)
Definition: bitbang.c:293
#define SWD_WAIT_TIMEOUT
Definition: bitbang.c:26
const struct swd_driver bitbang_swd
Definition: bitbang.c:614
static void bitbang_sleep(unsigned int microseconds)
Definition: bitbang.c:284
static int bitbang_state_move(int skip)
Definition: bitbang.c:69
static void bitbang_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: bitbang.c:470
static int bitbang_swd_run_queue(void)
Definition: bitbang.c:602
@ BB_LOW
Definition: bitbang.h:18
@ BB_HIGH
Definition: bitbang.h:19
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
@ SCAN_IN
From device to host,.
Definition: commands.h:24
@ SCAN_OUT
From host to device,.
Definition: commands.h:26
@ 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
@ JTAG_TMS
Definition: commands.h:143
uint8_t type
Definition: esp_usb_jtag.c:0
bool tap_is_state_stable(tap_state_t astate)
Function tap_is_state_stable returns true if the astate is stable.
Definition: interface.c:200
tap_state_t tap_state_transition(tap_state_t 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(tap_state_t state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
Definition: interface.c:344
void tap_set_end_state(tap_state_t 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
tap_state_t tap_get_end_state(void)
For more information,.
Definition: interface.c:56
int tap_get_tms_path(tap_state_t from, tap_state_t to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
Definition: interface.c:190
int tap_get_tms_path_len(tap_state_t from, tap_state_t to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
Definition: interface.c:195
tap_state_t 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
#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:49
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1068
The JTAG interface can be implemented with a software or hardware fifo.
@ TAP_RESET
Definition: jtag.h:56
@ TAP_IRSHIFT
Definition: jtag.h:51
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_DRSHIFT
Definition: jtag.h:43
#define ERROR_JTAG_QUEUE_FAILED
Definition: jtag.h:556
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
void alive_sleep(uint64_t ms)
Definition: log.c:456
#define LOG_CUSTOM_LEVEL(level, expr ...)
Definition: log.h:117
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:101
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
@ LOG_LVL_DEBUG
Definition: log.h:47
@ LOG_LVL_DEBUG_IO
Definition: log.h:48
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
Low level callbacks (for bitbang).
Definition: bitbang.h:30
int(* sleep)(unsigned int microseconds)
Sleep for some number of microseconds.
Definition: bitbang.h:60
int(* swdio_read)(void)
Sample SWDIO and return the value.
Definition: bitbang.h:51
int(* swd_write)(int swclk, int swdio)
Set SWCLK and SWDIO to the given value.
Definition: bitbang.h:57
int(* sample)(void)
Sample TDO and put the result in a buffer.
Definition: bitbang.h:39
bb_value_t(* read)(void)
Sample TDO and return the value.
Definition: bitbang.h:32
bb_value_t(* read_sample)(void)
Return the next unread value from the buffer.
Definition: bitbang.h:42
int(* flush)(void)
Force a flush.
Definition: bitbang.h:63
int(* write)(int tck, int tms, int tdi)
Set TCK, TMS, and TDI to the given values.
Definition: bitbang.h:45
void(* swdio_drive)(bool on)
Set direction of SWDIO.
Definition: bitbang.h:54
int(* blink)(bool on)
Blink led (optional).
Definition: bitbang.h:48
size_t buf_size
The number of TDO samples that can be buffered up before the caller has to call read_sample.
Definition: bitbang.h:36
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
Definition: swd.h:255
Definition: psoc6.c:83
static const unsigned int swd_seq_dormant_to_swd_len
Definition: swd.h:190
static const uint8_t swd_seq_dormant_to_jtag[]
Dormant-to-JTAG sequence.
Definition: swd.h:230
#define SWD_CMD_A32
Definition: swd.h:19
static const uint8_t swd_seq_dormant_to_swd[]
Dormant-to-SWD sequence.
Definition: swd.h:171
static const uint8_t swd_seq_jtag_to_dormant[]
JTAG-to-dormant sequence.
Definition: swd.h:199
static bool swd_cmd_returns_ack(uint8_t cmd)
Test if we can rely on ACK returned by SWD command.
Definition: swd.h:58
#define SWD_CMD_PARK
Definition: swd.h:22
static int swd_ack_to_error_code(uint8_t ack)
Convert SWD ACK value returned from DP to OpenOCD error code.
Definition: swd.h:72
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
static const unsigned int swd_seq_dormant_to_jtag_len
Definition: swd.h:244
#define SWD_CMD_APNDP
Definition: swd.h:17
static const unsigned int swd_seq_swd_to_dormant_len
Definition: swd.h:159
#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
static const unsigned int swd_seq_jtag_to_dormant_len
Definition: swd.h:211
static const uint8_t swd_seq_swd_to_dormant[]
SWD-to-dormant sequence.
Definition: swd.h:153
int64_t timeval_ms(void)
#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