OpenOCD
jlink.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
5  * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
6  * *
7  * Copyright (C) 2008 by Spencer Oliver *
8  * spen@spen-soft.co.uk *
9  * *
10  * Copyright (C) 2011 by Jean-Christophe PLAGNIOL-VIILARD *
11  * plagnioj@jcrosoft.com *
12  * *
13  * Copyright (C) 2015 by Marc Schink *
14  * openocd-dev@marcschink.de *
15  * *
16  * Copyright (C) 2015 by Paul Fertser *
17  * fercerpav@gmail.com *
18  ***************************************************************************/
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include <stdint.h>
25 #include <math.h>
26 #include <string.h>
27 
28 #include <jtag/interface.h>
29 #include <jtag/swd.h>
30 #include <jtag/commands.h>
31 #include <jtag/adapter.h>
32 #include <helper/replacements.h>
33 #include <target/cortex_m.h>
34 
35 #include <libjaylink/libjaylink.h>
36 
37 static struct jaylink_context *jayctx;
38 static struct jaylink_device_handle *devh;
39 static struct jaylink_connection conn;
40 static struct jaylink_connection connlist[JAYLINK_MAX_CONNECTIONS];
41 static enum jaylink_jtag_version jtag_command_version;
42 static uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE];
43 
44 static bool use_usb_location;
45 static enum jaylink_usb_address usb_address;
46 static bool use_usb_address;
47 static enum jaylink_target_interface iface = JAYLINK_TIF_JTAG;
48 static bool trace_enabled;
49 
50 #define JLINK_MAX_SPEED 12000
51 #define JLINK_TAP_BUFFER_SIZE 2048
52 
53 static unsigned int swd_buffer_size = JLINK_TAP_BUFFER_SIZE;
54 
55 /* Maximum SWO frequency deviation. */
56 #define SWO_MAX_FREQ_DEV 0.03
57 
58 /* 256 byte non-volatile memory */
59 struct device_config {
60  uint8_t usb_address;
61  /* 0ffset 0x01 to 0x03 */
62  uint8_t reserved_1[3];
63  uint32_t target_power;
64  /* 0ffset 0x08 to 0x1f */
65  uint8_t reserved_2[24];
66  /* IP only for J-Link Pro */
67  uint8_t ip_address[4];
68  uint8_t subnet_mask[4];
69  /* 0ffset 0x28 to 0x2f */
70  uint8_t reserved_3[8];
71  uint8_t mac_address[6];
72  /* 0ffset 0x36 to 0xff */
73  uint8_t reserved_4[202];
74 } __attribute__ ((packed));
75 
76 static struct device_config config;
77 static struct device_config tmp_config;
78 
79 /* Queue command functions */
80 static void jlink_end_state(enum tap_state state);
81 static void jlink_state_move(void);
82 static void jlink_path_move(unsigned int num_states, enum tap_state *path);
83 static void jlink_stableclocks(unsigned int num_cycles);
84 static void jlink_runtest(unsigned int num_cycles);
85 static void jlink_reset(int trst, int srst);
86 static int jlink_reset_safe(int trst, int srst);
87 static int jlink_swd_run_queue(void);
88 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk);
89 static int jlink_swd_switch_seq(enum swd_special_seq seq);
90 
91 /* J-Link tap buffer functions */
92 static void jlink_tap_init(void);
93 static int jlink_flush(void);
106 static void jlink_clock_data(const uint8_t *out, unsigned int out_offset,
107  const uint8_t *tms_out, unsigned int tms_offset,
108  uint8_t *in, unsigned int in_offset,
109  unsigned int length);
110 
111 static enum tap_state jlink_last_state = TAP_RESET;
112 static int queued_retval;
113 
114 /***************************************************************************/
115 /* External interface implementation */
116 
118 {
119  LOG_DEBUG_IO("stableclocks %i cycles", cmd->cmd.runtest->num_cycles);
120  jlink_stableclocks(cmd->cmd.runtest->num_cycles);
121 }
122 
124 {
125  LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles,
126  cmd->cmd.runtest->end_state);
127 
128  jlink_end_state(cmd->cmd.runtest->end_state);
129  jlink_runtest(cmd->cmd.runtest->num_cycles);
130 }
131 
133 {
134  LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
135 
136  jlink_end_state(cmd->cmd.statemove->end_state);
138 }
139 
141 {
142  LOG_DEBUG_IO("pathmove: %u states, end in %i",
143  cmd->cmd.pathmove->num_states,
144  cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
145 
146  jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
147 }
148 
149 static void jlink_execute_scan(struct jtag_command *cmd)
150 {
151  LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
152  jtag_scan_type(cmd->cmd.scan));
153 
154  /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
155  while (cmd->cmd.scan->num_fields > 0
156  && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
157  cmd->cmd.scan->num_fields--;
158  LOG_DEBUG("discarding trailing empty field");
159  }
160 
161  if (!cmd->cmd.scan->num_fields) {
162  LOG_DEBUG("empty scan, doing nothing");
163  return;
164  }
165 
166  if (cmd->cmd.scan->ir_scan) {
167  if (tap_get_state() != TAP_IRSHIFT) {
170  }
171  } else {
172  if (tap_get_state() != TAP_DRSHIFT) {
175  }
176  }
177 
178  jlink_end_state(cmd->cmd.scan->end_state);
179 
180  struct scan_field *field = cmd->cmd.scan->fields;
181  unsigned int scan_size = 0;
182 
183  for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
184  scan_size += field->num_bits;
185  LOG_DEBUG_IO("%s%s field %u/%u %u bits",
186  field->in_value ? "in" : "",
187  field->out_value ? "out" : "",
188  i,
189  cmd->cmd.scan->num_fields,
190  field->num_bits);
191 
192  if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
193  /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
194  * movement. This last field can't have length zero, it was checked above. */
196  0,
197  NULL,
198  0,
199  field->in_value,
200  0,
201  field->num_bits - 1);
202  uint8_t last_bit = 0;
203  if (field->out_value)
204  bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
205  uint8_t tms_bits = 0x01;
206  jlink_clock_data(&last_bit,
207  0,
208  &tms_bits,
209  0,
210  field->in_value,
211  field->num_bits - 1,
212  1);
215  0,
216  &tms_bits,
217  1,
218  NULL,
219  0,
220  1);
222  } else
224  0,
225  NULL,
226  0,
227  field->in_value,
228  0,
229  field->num_bits);
230  }
231 
232  if (tap_get_state() != tap_get_end_state()) {
235  }
236 
237  LOG_DEBUG_IO("%s scan, %i bits, end in %s",
238  (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
240 }
241 
243 {
244  LOG_DEBUG_IO("sleep %" PRIu32 "", cmd->cmd.sleep->us);
245  jlink_flush();
246  jtag_sleep(cmd->cmd.sleep->us);
247 }
248 
250 {
251  switch (cmd->type) {
252  case JTAG_STABLECLOCKS:
254  break;
255  case JTAG_RUNTEST:
257  break;
258  case JTAG_TLR_RESET:
260  break;
261  case JTAG_PATHMOVE:
263  break;
264  case JTAG_SCAN:
266  break;
267  case JTAG_SLEEP:
269  break;
270  default:
271  LOG_ERROR("BUG: Unknown JTAG command type encountered");
273  }
274 
275  return ERROR_OK;
276 }
277 
278 static int jlink_execute_queue(struct jtag_command *cmd_queue)
279 {
280  int ret;
281  struct jtag_command *cmd = cmd_queue;
282 
283  while (cmd) {
284  ret = jlink_execute_command(cmd);
285 
286  if (ret != ERROR_OK)
287  return ret;
288 
289  cmd = cmd->next;
290  }
291 
292  return jlink_flush();
293 }
294 
295 static int jlink_speed(int speed)
296 {
297  int ret;
298  struct jaylink_speed tmp;
299  int max_speed;
300 
301  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) {
302  ret = jaylink_get_speeds(devh, &tmp);
303 
304  if (ret != JAYLINK_OK) {
305  LOG_ERROR("jaylink_get_speeds() failed: %s",
306  jaylink_strerror(ret));
308  }
309 
310  tmp.freq /= 1000;
311  max_speed = tmp.freq / tmp.div;
312  } else {
313  max_speed = JLINK_MAX_SPEED;
314  }
315 
316  if (!speed) {
317  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING)) {
318  LOG_ERROR("Adaptive clocking is not supported by the device");
320  }
321 
322  speed = JAYLINK_SPEED_ADAPTIVE_CLOCKING;
323  } else if (speed > max_speed) {
324  LOG_INFO("Reduced speed from %d kHz to %d kHz (maximum)", speed,
325  max_speed);
326  speed = max_speed;
327  }
328 
329  ret = jaylink_set_speed(devh, speed);
330 
331  if (ret != JAYLINK_OK) {
332  LOG_ERROR("jaylink_set_speed() failed: %s",
333  jaylink_strerror(ret));
335  }
336 
337  return ERROR_OK;
338 }
339 
340 static int jlink_speed_div(int speed, int *khz)
341 {
342  *khz = speed;
343 
344  return ERROR_OK;
345 }
346 
347 static int jlink_khz(int khz, int *jtag_speed)
348 {
349  *jtag_speed = khz;
350 
351  return ERROR_OK;
352 }
353 
354 static bool read_device_config(struct device_config *cfg)
355 {
356  int ret;
357 
358  ret = jaylink_read_raw_config(devh, (uint8_t *)cfg);
359 
360  if (ret != JAYLINK_OK) {
361  LOG_ERROR("jaylink_read_raw_config() failed: %s",
362  jaylink_strerror(ret));
363  return false;
364  }
365 
366  if (cfg->usb_address == 0xff)
367  cfg->usb_address = 0x00;
368 
369  if (cfg->target_power == 0xffffffff)
370  cfg->target_power = 0;
371 
372  return true;
373 }
374 
375 static int select_interface(void)
376 {
377  int ret;
378  uint32_t interfaces;
379 
380  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SELECT_TIF)) {
381  if (iface != JAYLINK_TIF_JTAG) {
382  LOG_ERROR("Device supports JTAG transport only");
383  return ERROR_JTAG_INIT_FAILED;
384  }
385 
386  return ERROR_OK;
387  }
388 
389  ret = jaylink_get_available_interfaces(devh, &interfaces);
390 
391  if (ret != JAYLINK_OK) {
392  LOG_ERROR("jaylink_get_available_interfaces() failed: %s",
393  jaylink_strerror(ret));
394  return ERROR_JTAG_INIT_FAILED;
395  }
396 
397  if (!(interfaces & (1 << iface))) {
398  LOG_ERROR("Selected transport is not supported by the device");
399  return ERROR_JTAG_INIT_FAILED;
400  }
401 
402  ret = jaylink_select_interface(devh, iface, NULL);
403 
404  if (ret < 0) {
405  LOG_ERROR("jaylink_select_interface() failed: %s",
406  jaylink_strerror(ret));
407  return ERROR_JTAG_INIT_FAILED;
408  }
409 
410  return ERROR_OK;
411 }
412 
413 static int jlink_register(void)
414 {
415  int ret;
416  size_t i;
417  bool handle_found;
418  size_t count;
419 
420  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER))
421  return ERROR_OK;
422 
423  ret = jaylink_register(devh, &conn, connlist, &count);
424 
425  if (ret != JAYLINK_OK) {
426  LOG_ERROR("jaylink_register() failed: %s", jaylink_strerror(ret));
427  return ERROR_FAIL;
428  }
429 
430  handle_found = false;
431 
432  for (i = 0; i < count; i++) {
433  if (connlist[i].handle == conn.handle) {
434  handle_found = true;
435  break;
436  }
437  }
438 
439  if (!handle_found) {
440  LOG_ERROR("Registration failed: maximum number of connections on the "
441  "device reached");
442  return ERROR_FAIL;
443  }
444 
445  return ERROR_OK;
446 }
447 
448 /*
449  * Adjust the SWD transaction buffer size depending on the free device internal
450  * memory. This ensures that the SWD transactions sent to the device do not
451  * exceed the internal memory of the device.
452  */
453 static bool adjust_swd_buffer_size(void)
454 {
455  int ret;
456  uint32_t tmp;
457 
458  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
459  return true;
460 
461  ret = jaylink_get_free_memory(devh, &tmp);
462 
463  if (ret != JAYLINK_OK) {
464  LOG_ERROR("jaylink_get_free_memory() failed: %s",
465  jaylink_strerror(ret));
466  return false;
467  }
468 
469  if (tmp < 143) {
470  LOG_ERROR("Not enough free device internal memory: %" PRIu32 " bytes", tmp);
471  return false;
472  }
473 
474  tmp = MIN(JLINK_TAP_BUFFER_SIZE, (tmp - 16) / 2);
475 
476  if (tmp != swd_buffer_size) {
477  swd_buffer_size = tmp;
478  LOG_DEBUG("Adjusted SWD transaction buffer size to %u bytes",
480  }
481 
482  return true;
483 }
484 
485 static int jaylink_log_handler(const struct jaylink_context *ctx,
486  enum jaylink_log_level level, const char *format, va_list args,
487  void *user_data)
488 {
489  enum log_levels tmp;
490 
491  switch (level) {
492  case JAYLINK_LOG_LEVEL_ERROR:
493  tmp = LOG_LVL_ERROR;
494  break;
495  case JAYLINK_LOG_LEVEL_WARNING:
496  tmp = LOG_LVL_WARNING;
497  break;
498  /*
499  * Forward info messages to the debug output because they are more verbose
500  * than info messages of OpenOCD.
501  */
502  case JAYLINK_LOG_LEVEL_INFO:
503  case JAYLINK_LOG_LEVEL_DEBUG:
504  tmp = LOG_LVL_DEBUG;
505  break;
506  case JAYLINK_LOG_LEVEL_DEBUG_IO:
507  tmp = LOG_LVL_DEBUG_IO;
508  break;
509  default:
510  tmp = LOG_LVL_WARNING;
511  }
512 
513  log_vprintf_lf(tmp, __FILE__, __LINE__, __func__, format, args);
514 
515  return 0;
516 }
517 
518 static bool jlink_usb_location_equal(struct jaylink_device *dev)
519 {
520  int retval;
521  uint8_t bus;
522  uint8_t *ports;
523  size_t num_ports;
524  bool equal = false;
525 
526  retval = jaylink_device_get_usb_bus_ports(dev, &bus, &ports, &num_ports);
527 
528  if (retval == JAYLINK_ERR_NOT_SUPPORTED) {
529  return false;
530  } else if (retval != JAYLINK_OK) {
531  LOG_WARNING("jaylink_device_get_usb_bus_ports() failed: %s",
532  jaylink_strerror(retval));
533  return false;
534  }
535 
536  equal = adapter_usb_location_equal(bus, ports, num_ports);
537  free(ports);
538 
539  return equal;
540 }
541 
542 
543 static int jlink_open_device(uint32_t ifaces, bool *found_device)
544 {
545  int ret = jaylink_discovery_scan(jayctx, ifaces);
546  if (ret != JAYLINK_OK) {
547  LOG_ERROR("jaylink_discovery_scan() failed: %s", jaylink_strerror(ret));
548  jaylink_exit(jayctx);
549  return ERROR_JTAG_INIT_FAILED;
550  }
551 
552  size_t num_devices;
553  struct jaylink_device **devs;
554  ret = jaylink_get_devices(jayctx, &devs, &num_devices);
555 
556  if (ret != JAYLINK_OK) {
557  LOG_ERROR("jaylink_get_devices() failed: %s", jaylink_strerror(ret));
558  jaylink_exit(jayctx);
559  return ERROR_JTAG_INIT_FAILED;
560  }
561 
563  const char *adapter_serial = adapter_get_required_serial();
564 
565  if (!adapter_serial && !use_usb_address && !use_usb_location && num_devices > 1) {
566  LOG_ERROR("Multiple devices found, specify the desired device");
567  LOG_INFO("Found devices:");
568  for (size_t i = 0; devs[i]; i++) {
569  uint32_t serial;
570  ret = jaylink_device_get_serial_number(devs[i], &serial);
571  if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
572  continue;
573  } else if (ret != JAYLINK_OK) {
574  LOG_WARNING("jaylink_device_get_serial_number() failed: %s",
575  jaylink_strerror(ret));
576  continue;
577  }
578  char name[JAYLINK_NICKNAME_MAX_LENGTH];
579  int name_ret = jaylink_device_get_nickname(devs[i], name);
580  if (name_ret == JAYLINK_OK)
581  LOG_INFO("Device %zu serial: %" PRIu32 ", nickname %s", i, serial, name);
582  else
583  LOG_INFO("Device %zu serial: %" PRIu32, i, serial);
584  }
585 
586  jaylink_free_devices(devs, true);
587  jaylink_exit(jayctx);
588  return ERROR_JTAG_INIT_FAILED;
589  }
590 
591  *found_device = false;
592 
593  uint32_t serial_number;
594  ret = jaylink_parse_serial_number(adapter_serial, &serial_number);
595  if (ret != JAYLINK_OK)
596  serial_number = 0;
597 
598  for (size_t i = 0; devs[i]; i++) {
599  struct jaylink_device *dev = devs[i];
600 
601  if (adapter_serial) {
602  /*
603  * Treat adapter serial as a nickname first as it can also be numeric.
604  * If it fails to match (optional) device nickname try to compare
605  * adapter serial with the actual device serial number.
606  */
607  char nickname[JAYLINK_NICKNAME_MAX_LENGTH];
608  ret = jaylink_device_get_nickname(dev, nickname);
609  if (ret != JAYLINK_OK || strcmp(nickname, adapter_serial) != 0) {
610  if (!serial_number)
611  continue;
612 
613  uint32_t tmp;
614  ret = jaylink_device_get_serial_number(dev, &tmp);
615  if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
616  continue;
617  } else if (ret != JAYLINK_OK) {
618  LOG_WARNING("jaylink_device_get_serial_number() failed: %s",
619  jaylink_strerror(ret));
620  continue;
621  }
622 
623  if (serial_number != tmp)
624  continue;
625  }
626  }
627 
628  if (use_usb_address) {
629  enum jaylink_usb_address address;
630  ret = jaylink_device_get_usb_address(dev, &address);
631 
632  if (ret == JAYLINK_ERR_NOT_SUPPORTED) {
633  continue;
634  } else if (ret != JAYLINK_OK) {
635  LOG_WARNING("jaylink_device_get_usb_address() failed: %s",
636  jaylink_strerror(ret));
637  continue;
638  }
639 
640  if (usb_address != address)
641  continue;
642  }
643 
645  continue;
646 
647  ret = jaylink_open(dev, &devh);
648 
649  if (ret == JAYLINK_OK) {
650  *found_device = true;
651  break;
652  }
653 
654  LOG_ERROR("Failed to open device: %s", jaylink_strerror(ret));
655  }
656 
657  jaylink_free_devices(devs, true);
658  return ERROR_OK;
659 }
660 
661 
662 static int jlink_init(void)
663 {
664  int ret;
665  char *firmware_version;
666  struct jaylink_hardware_version hwver;
667  struct jaylink_hardware_status hwstatus;
668  size_t length;
669 
670  LOG_DEBUG("Using libjaylink %s (compiled with %s)",
671  jaylink_version_package_get_string(), JAYLINK_VERSION_PACKAGE_STRING);
672 
673  if (!jaylink_library_has_cap(JAYLINK_CAP_HIF_USB) && use_usb_address) {
674  LOG_ERROR("J-Link driver does not support USB devices");
675  return ERROR_JTAG_INIT_FAILED;
676  }
677 
678  ret = jaylink_init(&jayctx);
679 
680  if (ret != JAYLINK_OK) {
681  LOG_ERROR("jaylink_init() failed: %s", jaylink_strerror(ret));
682  return ERROR_JTAG_INIT_FAILED;
683  }
684 
685  ret = jaylink_log_set_callback(jayctx, &jaylink_log_handler, NULL);
686 
687  if (ret != JAYLINK_OK) {
688  LOG_ERROR("jaylink_log_set_callback() failed: %s",
689  jaylink_strerror(ret));
690  jaylink_exit(jayctx);
691  return ERROR_JTAG_INIT_FAILED;
692  }
693 
695  use_usb_address = false;
696 
697  bool found_device;
698  ret = jlink_open_device(JAYLINK_HIF_USB, &found_device);
699  if (ret != ERROR_OK)
700  return ret;
701 
702  if (!found_device && adapter_get_required_serial()) {
703  ret = jlink_open_device(JAYLINK_HIF_TCP, &found_device);
704  if (ret != ERROR_OK)
705  return ret;
706  }
707 
708  if (!found_device) {
709  LOG_ERROR("No J-Link device found");
710  jaylink_exit(jayctx);
711  return ERROR_JTAG_INIT_FAILED;
712  }
713 
714  /*
715  * Be careful with changing the following initialization sequence because
716  * some devices are known to be sensitive regarding the order.
717  */
718 
719  ret = jaylink_get_firmware_version(devh, &firmware_version, &length);
720 
721  if (ret != JAYLINK_OK) {
722  LOG_ERROR("jaylink_get_firmware_version() failed: %s",
723  jaylink_strerror(ret));
724  jaylink_close(devh);
725  jaylink_exit(jayctx);
726  return ERROR_JTAG_INIT_FAILED;
727  } else if (length > 0) {
728  LOG_INFO("%s", firmware_version);
729  free(firmware_version);
730  } else {
731  LOG_WARNING("Device responds empty firmware version string");
732  }
733 
734  memset(caps, 0, JAYLINK_DEV_EXT_CAPS_SIZE);
735  ret = jaylink_get_caps(devh, caps);
736 
737  if (ret != JAYLINK_OK) {
738  LOG_ERROR("jaylink_get_caps() failed: %s", jaylink_strerror(ret));
739  jaylink_close(devh);
740  jaylink_exit(jayctx);
741  return ERROR_JTAG_INIT_FAILED;
742  }
743 
744  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
745  ret = jaylink_get_extended_caps(devh, caps);
746 
747  if (ret != JAYLINK_OK) {
748  LOG_ERROR("jaylink_get_extended_caps() failed: %s",
749  jaylink_strerror(ret));
750  jaylink_close(devh);
751  jaylink_exit(jayctx);
752  return ERROR_JTAG_INIT_FAILED;
753  }
754  }
755 
756  jtag_command_version = JAYLINK_JTAG_VERSION_2;
757 
758  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_HW_VERSION)) {
759  ret = jaylink_get_hardware_version(devh, &hwver);
760 
761  if (ret != JAYLINK_OK) {
762  LOG_ERROR("Failed to retrieve hardware version: %s",
763  jaylink_strerror(ret));
764  jaylink_close(devh);
765  jaylink_exit(jayctx);
766  return ERROR_JTAG_INIT_FAILED;
767  }
768 
769  LOG_INFO("Hardware version: %u.%02u", hwver.major, hwver.minor);
770 
771  if (hwver.major >= 5)
772  jtag_command_version = JAYLINK_JTAG_VERSION_3;
773  }
774 
775  if (iface == JAYLINK_TIF_SWD) {
776  /*
777  * Adjust the SWD transaction buffer size in case there is already
778  * allocated memory on the device. This happens for example if the
779  * memory for SWO capturing is still allocated because the software
780  * which used the device before has not been shut down properly.
781  */
782  if (!adjust_swd_buffer_size()) {
783  jaylink_close(devh);
784  jaylink_exit(jayctx);
785  return ERROR_JTAG_INIT_FAILED;
786  }
787  }
788 
789  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
790  if (!read_device_config(&config)) {
791  LOG_ERROR("Failed to read device configuration data");
792  jaylink_close(devh);
793  jaylink_exit(jayctx);
794  return ERROR_JTAG_INIT_FAILED;
795  }
796 
797  memcpy(&tmp_config, &config, sizeof(struct device_config));
798  }
799 
800  ret = jaylink_get_hardware_status(devh, &hwstatus);
801 
802  if (ret != JAYLINK_OK) {
803  LOG_ERROR("jaylink_get_hardware_status() failed: %s",
804  jaylink_strerror(ret));
805  jaylink_close(devh);
806  jaylink_exit(jayctx);
807  return ERROR_JTAG_INIT_FAILED;
808  }
809 
810  LOG_INFO("VTarget = %u.%03u V", hwstatus.target_voltage / 1000,
811  hwstatus.target_voltage % 1000);
812 
813  conn.handle = 0;
814  conn.pid = 0;
815  strcpy(conn.hid, "0.0.0.0");
816  conn.iid = 0;
817  conn.cid = 0;
818 
819  ret = jlink_register();
820 
821  if (ret != ERROR_OK) {
822  jaylink_close(devh);
823  jaylink_exit(jayctx);
824  return ERROR_JTAG_INIT_FAILED;
825  }
826 
827  ret = select_interface();
828 
829  if (ret != ERROR_OK) {
830  jaylink_close(devh);
831  jaylink_exit(jayctx);
832  return ret;
833  }
834 
835  jlink_reset(0, 0);
836  jtag_sleep(3000);
837  jlink_tap_init();
838 
840 
841  if (iface == JAYLINK_TIF_JTAG) {
842  /*
843  * J-Link devices with firmware version v5 and v6 seems to have an issue
844  * if the first tap move is not divisible by 8, so we send a TLR on
845  * first power up.
846  */
847  uint8_t tms = 0xff;
848  jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 8);
849 
850  jlink_flush();
851  }
852 
853  return ERROR_OK;
854 }
855 
856 static int jlink_quit(void)
857 {
858  int ret;
859  size_t count;
860 
861  if (trace_enabled) {
862  ret = jaylink_swo_stop(devh);
863 
864  if (ret != JAYLINK_OK)
865  LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
866  }
867 
868  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER)) {
869  ret = jaylink_unregister(devh, &conn, connlist, &count);
870 
871  if (ret != JAYLINK_OK)
872  LOG_ERROR("jaylink_unregister() failed: %s",
873  jaylink_strerror(ret));
874  }
875 
876  jaylink_close(devh);
877  jaylink_exit(jayctx);
878 
879  return ERROR_OK;
880 }
881 
882 /***************************************************************************/
883 /* Queue command implementations */
884 
885 static void jlink_end_state(enum tap_state state)
886 {
889  else {
890  LOG_ERROR("BUG: %i is not a valid end state", state);
891  exit(-1);
892  }
893 }
894 
895 /* Goes to the end state. */
896 static void jlink_state_move(void)
897 {
898  uint8_t tms_scan;
899  uint8_t tms_scan_bits;
900 
903 
904  jlink_clock_data(NULL, 0, &tms_scan, 0, NULL, 0, tms_scan_bits);
905 
907 }
908 
909 static void jlink_path_move(unsigned int num_states, enum tap_state *path)
910 {
911  uint8_t tms = 0xff;
912 
913  for (unsigned int i = 0; i < num_states; i++) {
914  if (path[i] == tap_state_transition(tap_get_state(), false))
915  jlink_clock_data(NULL, 0, NULL, 0, NULL, 0, 1);
916  else if (path[i] == tap_state_transition(tap_get_state(), true))
917  jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
918  else {
919  LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
921  exit(-1);
922  }
923 
924  tap_set_state(path[i]);
925  }
926 
928 }
929 
930 static void jlink_stableclocks(unsigned int num_cycles)
931 {
932  uint8_t tms = tap_get_state() == TAP_RESET;
933  /* Execute num_cycles. */
934  for (unsigned int i = 0; i < num_cycles; i++)
935  jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
936 }
937 
938 static void jlink_runtest(unsigned int num_cycles)
939 {
940  enum tap_state saved_end_state = tap_get_end_state();
941 
942  /* Only do a state_move when we're not already in IDLE. */
943  if (tap_get_state() != TAP_IDLE) {
946  /* num_cycles--; */
947  }
948 
949  jlink_stableclocks(num_cycles);
950 
951  /* Finish in end_state. */
952  jlink_end_state(saved_end_state);
953 
954  if (tap_get_state() != tap_get_end_state())
956 }
957 
958 static void jlink_reset(int trst, int srst)
959 {
960  LOG_DEBUG("TRST: %i, SRST: %i", trst, srst);
961 
962  /* Signals are active low. */
963  if (srst == 0)
964  jaylink_set_reset(devh);
965 
966  if (srst == 1)
967  jaylink_clear_reset(devh);
968 
969  if (trst == 1)
970  jaylink_jtag_clear_trst(devh);
971 
972  if (trst == 0)
973  jaylink_jtag_set_trst(devh);
974 }
975 
976 static int jlink_reset_safe(int trst, int srst)
977 {
978  jlink_flush();
979  jlink_reset(trst, srst);
980  return jlink_flush();
981 }
982 
983 COMMAND_HANDLER(jlink_usb_command)
984 {
985  if (CMD_ARGC != 1)
987 
988  unsigned int tmp;
989  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], tmp);
990 
991  if (tmp > JAYLINK_USB_ADDRESS_3) {
992  command_print(CMD, "Invalid USB address: %s", CMD_ARGV[0]);
994  }
995 
996  usb_address = tmp;
997  use_usb_address = true;
998 
999  return ERROR_OK;
1000 }
1001 
1002 COMMAND_HANDLER(jlink_handle_hwstatus_command)
1003 {
1004  int ret;
1005  struct jaylink_hardware_status status;
1006 
1007  ret = jaylink_get_hardware_status(devh, &status);
1008 
1009  if (ret != JAYLINK_OK) {
1010  command_print(CMD, "jaylink_get_hardware_status() failed: %s",
1011  jaylink_strerror(ret));
1012  return ERROR_FAIL;
1013  }
1014 
1015  command_print(CMD, "VTarget = %u.%03u V",
1016  status.target_voltage / 1000, status.target_voltage % 1000);
1017 
1018  command_print(CMD, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
1019  "TRST = %u", status.tck, status.tdi, status.tdo, status.tms,
1020  status.tres, status.trst);
1021 
1022  if (status.target_voltage < 1500)
1023  command_print(CMD, "Target voltage too low. Check target power");
1024 
1025  return ERROR_OK;
1026 }
1027 
1028 COMMAND_HANDLER(jlink_handle_free_memory_command)
1029 {
1030  int ret;
1031  uint32_t tmp;
1032 
1033  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY)) {
1034  command_print(CMD, "Retrieval of free memory is not supported by "
1035  "the device");
1036  return ERROR_OK;
1037  }
1038 
1039  ret = jaylink_get_free_memory(devh, &tmp);
1040 
1041  if (ret != JAYLINK_OK) {
1042  command_print(CMD, "jaylink_get_free_memory() failed: %s",
1043  jaylink_strerror(ret));
1044  return ERROR_FAIL;
1045  }
1046 
1047  command_print(CMD, "Device has %" PRIu32 " bytes of free memory", tmp);
1048 
1049  return ERROR_OK;
1050 }
1051 
1052 COMMAND_HANDLER(jlink_handle_jlink_jtag_command)
1053 {
1054  if (!CMD_ARGC) {
1055  unsigned int version;
1056 
1057  switch (jtag_command_version) {
1058  case JAYLINK_JTAG_VERSION_2:
1059  version = 2;
1060  break;
1061  case JAYLINK_JTAG_VERSION_3:
1062  version = 3;
1063  break;
1064  default:
1065  return ERROR_FAIL;
1066  }
1067 
1068  command_print(CMD, "JTAG command version: %u", version);
1069  } else if (CMD_ARGC == 1) {
1070  uint8_t tmp;
1071  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], tmp);
1072 
1073  switch (tmp) {
1074  case 2:
1075  jtag_command_version = JAYLINK_JTAG_VERSION_2;
1076  break;
1077  case 3:
1078  jtag_command_version = JAYLINK_JTAG_VERSION_3;
1079  break;
1080  default:
1081  command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1083  }
1084  } else {
1086  }
1087 
1088  return ERROR_OK;
1089 }
1090 
1091 COMMAND_HANDLER(jlink_handle_target_power_command)
1092 {
1093  if (CMD_ARGC > 1)
1095 
1096  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1097  command_print(CMD, "Target power supply is not supported by the "
1098  "device");
1099  return ERROR_OK;
1100  }
1101 
1102  if (!CMD_ARGC) {
1103  uint32_t state;
1104  int ret = jaylink_get_hardware_info(devh, JAYLINK_HW_INFO_TARGET_POWER,
1105  &state);
1106 
1107  if (ret != JAYLINK_OK) {
1108  command_print(CMD, "Failed to retrieve target power state");
1109  return ERROR_FAIL;
1110  }
1111 
1112  command_print(CMD, "%d", (bool)state);
1113  return ERROR_OK;
1114  }
1115 
1116  bool enable;
1117  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
1118 
1119  int ret = jaylink_set_target_power(devh, enable);
1120 
1121  if (ret != JAYLINK_OK) {
1122  command_print(CMD, "jaylink_set_target_power() failed: %s",
1123  jaylink_strerror(ret));
1124  return ERROR_FAIL;
1125  }
1126 
1127  return ERROR_OK;
1128 }
1129 
1131 {
1133  command_print(cmd, "USB address: %u [%u]", config.usb_address,
1135  else
1136  command_print(cmd, "USB address: %u", config.usb_address);
1137 }
1138 
1140 {
1141  if (!memcmp(config.ip_address, tmp_config.ip_address, 4))
1142  command_print(cmd, "IP address: %d.%d.%d.%d",
1145  else
1146  command_print(cmd, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1151 
1152  if (!memcmp(config.subnet_mask, tmp_config.subnet_mask, 4))
1153  command_print(cmd, "Subnet mask: %d.%d.%d.%d",
1156  else
1157  command_print(cmd, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1162 }
1163 
1165 {
1166  if (!memcmp(config.mac_address, tmp_config.mac_address, 6))
1167  command_print(cmd, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1171  else
1172  command_print(cmd, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1173  "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1180 }
1181 
1183 {
1184  const char *target_power;
1185  const char *current_target_power;
1186 
1187  if (!config.target_power)
1188  target_power = "off";
1189  else
1190  target_power = "on";
1191 
1192  if (!tmp_config.target_power)
1193  current_target_power = "off";
1194  else
1195  current_target_power = "on";
1196 
1198  command_print(cmd, "Target power supply: %s [%s]", target_power,
1199  current_target_power);
1200  else
1201  command_print(cmd, "Target power supply: %s", target_power);
1202 }
1203 
1204 static void show_config(struct command_invocation *cmd)
1205 {
1206  command_print(cmd, "J-Link device configuration:");
1207 
1209 
1210  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER))
1212 
1213  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1216  }
1217 }
1218 
1219 static int poll_trace(uint8_t *buf, size_t *size)
1220 {
1221  int ret;
1222  uint32_t length;
1223 
1224  length = *size;
1225 
1226  ret = jaylink_swo_read(devh, buf, &length);
1227 
1228  if (ret != JAYLINK_OK) {
1229  LOG_ERROR("jaylink_swo_read() failed: %s", jaylink_strerror(ret));
1230  return ERROR_FAIL;
1231  }
1232 
1233  *size = length;
1234 
1235  return ERROR_OK;
1236 }
1237 
1238 static uint32_t calculate_trace_buffer_size(void)
1239 {
1240  int ret;
1241  uint32_t tmp;
1242 
1243  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
1244  return 0;
1245 
1246  ret = jaylink_get_free_memory(devh, &tmp);
1247 
1248  if (ret != JAYLINK_OK) {
1249  LOG_ERROR("jaylink_get_free_memory() failed: %s",
1250  jaylink_strerror(ret));
1251  return ERROR_FAIL;
1252  }
1253 
1254  if (tmp > 0x3fff || tmp <= 0x600)
1255  tmp = tmp >> 1;
1256  else
1257  tmp = tmp - 0x400;
1258 
1259  return tmp & 0xffffff00;
1260 }
1261 
1262 static bool calculate_swo_prescaler(unsigned int traceclkin_freq,
1263  uint32_t trace_freq, uint16_t *prescaler)
1264 {
1265  unsigned int presc = (traceclkin_freq + trace_freq / 2) / trace_freq;
1266  if (presc == 0 || presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1267  return false;
1268 
1269  /* Probe's UART speed must be within 3% of the TPIU's SWO baud rate. */
1270  unsigned int max_deviation = (traceclkin_freq * 3) / 100;
1271  if (presc * trace_freq < traceclkin_freq - max_deviation ||
1272  presc * trace_freq > traceclkin_freq + max_deviation)
1273  return false;
1274 
1275  *prescaler = presc;
1276 
1277  return true;
1278 }
1279 
1280 static bool detect_swo_freq_and_prescaler(struct jaylink_swo_speed speed,
1281  unsigned int traceclkin_freq, unsigned int *trace_freq,
1282  uint16_t *prescaler)
1283 {
1284  uint32_t divider;
1285  unsigned int presc;
1286  double deviation;
1287 
1288  for (divider = speed.min_div; divider <= speed.max_div; divider++) {
1289  *trace_freq = speed.freq / divider;
1290  presc = ((1.0 - SWO_MAX_FREQ_DEV) * traceclkin_freq) / *trace_freq + 1;
1291 
1292  if (presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1293  break;
1294 
1295  deviation = fabs(1.0 - ((double)*trace_freq * presc / traceclkin_freq));
1296 
1297  if (deviation <= SWO_MAX_FREQ_DEV) {
1298  *prescaler = presc;
1299  return true;
1300  }
1301  }
1302 
1303  return false;
1304 }
1305 
1306 static int config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
1307  uint32_t port_size, unsigned int *trace_freq,
1308  unsigned int traceclkin_freq, uint16_t *prescaler)
1309 {
1310  int ret;
1311  uint32_t buffer_size;
1312  struct jaylink_swo_speed speed;
1313  uint32_t divider;
1314  uint32_t min_freq;
1315  uint32_t max_freq;
1316 
1317  trace_enabled = enabled;
1318 
1319  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SWO)) {
1320  if (!enabled)
1321  return ERROR_OK;
1322 
1323  LOG_ERROR("Trace capturing is not supported by the device");
1324  return ERROR_FAIL;
1325  }
1326 
1327  ret = jaylink_swo_stop(devh);
1328 
1329  if (ret != JAYLINK_OK) {
1330  LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
1331  return ERROR_FAIL;
1332  }
1333 
1334  if (!enabled) {
1335  /*
1336  * Adjust the SWD transaction buffer size as stopping SWO capturing
1337  * deallocates device internal memory.
1338  */
1339  if (!adjust_swd_buffer_size())
1340  return ERROR_FAIL;
1341 
1342  return ERROR_OK;
1343  }
1344 
1345  if (pin_protocol != TPIU_PIN_PROTOCOL_ASYNC_UART) {
1346  LOG_ERROR("Selected pin protocol is not supported");
1347  return ERROR_FAIL;
1348  }
1349 
1351 
1352  if (!buffer_size) {
1353  LOG_ERROR("Not enough free device memory to start trace capturing");
1354  return ERROR_FAIL;
1355  }
1356 
1357  ret = jaylink_swo_get_speeds(devh, JAYLINK_SWO_MODE_UART, &speed);
1358 
1359  if (ret != JAYLINK_OK) {
1360  LOG_ERROR("jaylink_swo_get_speeds() failed: %s",
1361  jaylink_strerror(ret));
1362  return ERROR_FAIL;
1363  }
1364 
1365  if (*trace_freq > 0) {
1366  divider = speed.freq / *trace_freq;
1367  min_freq = speed.freq / speed.max_div;
1368  max_freq = speed.freq / speed.min_div;
1369 
1370  if (*trace_freq > max_freq) {
1371  LOG_INFO("Given SWO frequency too high, using %" PRIu32 " Hz instead",
1372  max_freq);
1373  *trace_freq = max_freq;
1374  } else if (*trace_freq < min_freq) {
1375  LOG_INFO("Given SWO frequency too low, using %" PRIu32 " Hz instead",
1376  min_freq);
1377  *trace_freq = min_freq;
1378  } else if (*trace_freq != speed.freq / divider) {
1379  *trace_freq = speed.freq / divider;
1380 
1381  LOG_INFO("Given SWO frequency is not supported by the device, "
1382  "using %u Hz instead", *trace_freq);
1383  }
1384 
1385  if (!calculate_swo_prescaler(traceclkin_freq, *trace_freq,
1386  prescaler)) {
1387  LOG_ERROR("SWO frequency is not suitable. Please choose a "
1388  "different frequency or use auto-detection");
1389  return ERROR_FAIL;
1390  }
1391  } else {
1392  LOG_INFO("Trying to auto-detect SWO frequency");
1393 
1394  if (!detect_swo_freq_and_prescaler(speed, traceclkin_freq, trace_freq,
1395  prescaler)) {
1396  LOG_ERROR("Maximum permitted frequency deviation of %.02f %% "
1397  "could not be achieved", SWO_MAX_FREQ_DEV);
1398  LOG_ERROR("Auto-detection of SWO frequency failed");
1399  return ERROR_FAIL;
1400  }
1401 
1402  LOG_INFO("Using SWO frequency of %u Hz", *trace_freq);
1403  }
1404 
1405  ret = jaylink_swo_start(devh, JAYLINK_SWO_MODE_UART, *trace_freq,
1406  buffer_size);
1407 
1408  if (ret != JAYLINK_OK) {
1409  LOG_ERROR("jaylink_start_swo() failed: %s", jaylink_strerror(ret));
1410  return ERROR_FAIL;
1411  }
1412 
1413  LOG_DEBUG("Using %" PRIu32 " bytes device memory for trace capturing",
1414  buffer_size);
1415 
1416  /*
1417  * Adjust the SWD transaction buffer size as starting SWO capturing
1418  * allocates device internal memory.
1419  */
1420  if (!adjust_swd_buffer_size())
1421  return ERROR_FAIL;
1422 
1423  return ERROR_OK;
1424 }
1425 
1426 COMMAND_HANDLER(jlink_handle_config_usb_address_command)
1427 {
1428  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1429  command_print(CMD, "Reading configuration is not supported by the "
1430  "device");
1431  return ERROR_OK;
1432  }
1433 
1434  if (!CMD_ARGC) {
1436  } else if (CMD_ARGC == 1) {
1437  uint8_t tmp;
1438  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], tmp);
1439 
1440  if (tmp > JAYLINK_USB_ADDRESS_3) {
1441  command_print(CMD, "Invalid USB address: %u", tmp);
1443  }
1444 
1445  tmp_config.usb_address = tmp;
1446  } else {
1448  }
1449 
1450  return ERROR_OK;
1451 }
1452 
1453 COMMAND_HANDLER(jlink_handle_config_target_power_command)
1454 {
1455  int enable;
1456 
1457  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1458  command_print(CMD, "Reading configuration is not supported by the "
1459  "device");
1460  return ERROR_OK;
1461  }
1462 
1463  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1464  command_print(CMD, "Target power supply is not supported by the "
1465  "device");
1466  return ERROR_OK;
1467  }
1468 
1469  if (!CMD_ARGC) {
1471  } else if (CMD_ARGC == 1) {
1472  if (!strcmp(CMD_ARGV[0], "on")) {
1473  enable = true;
1474  } else if (!strcmp(CMD_ARGV[0], "off")) {
1475  enable = false;
1476  } else {
1477  command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1479  }
1480 
1481  tmp_config.target_power = enable;
1482  } else {
1484  }
1485 
1486  return ERROR_OK;
1487 }
1488 
1489 COMMAND_HANDLER(jlink_handle_config_mac_address_command)
1490 {
1491  uint8_t addr[6];
1492  int i;
1493  char *e;
1494  const char *str;
1495 
1496  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1497  command_print(CMD, "Reading configuration is not supported by the "
1498  "device");
1499  return ERROR_OK;
1500  }
1501 
1502  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1503  command_print(CMD, "Ethernet connectivity is not supported by the "
1504  "device");
1505  return ERROR_OK;
1506  }
1507 
1508  if (!CMD_ARGC) {
1510  } else if (CMD_ARGC == 1) {
1511  str = CMD_ARGV[0];
1512 
1513  if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' ||
1514  str[8] != ':' || str[11] != ':' || str[14] != ':')) {
1515  command_print(CMD, "Invalid MAC address format");
1517  }
1518 
1519  for (i = 5; i >= 0; i--) {
1520  addr[i] = strtoul(str, &e, 16);
1521  str = e + 1;
1522  }
1523 
1524  if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) {
1525  command_print(CMD, "Invalid MAC address: zero address");
1527  }
1528 
1529  if (!(0x01 & addr[0])) {
1530  command_print(CMD, "Invalid MAC address: multicast address");
1532  }
1533 
1534  memcpy(tmp_config.mac_address, addr, sizeof(addr));
1535  } else {
1537  }
1538 
1539  return ERROR_OK;
1540 }
1541 
1542 static bool string_to_ip(const char *s, uint8_t *ip, int *pos)
1543 {
1544  uint8_t lip[4];
1545  char *e;
1546  const char *s_save = s;
1547  int i;
1548 
1549  if (!s)
1550  return false;
1551 
1552  for (i = 0; i < 4; i++) {
1553  lip[i] = strtoul(s, &e, 10);
1554 
1555  if (*e != '.' && i != 3)
1556  return false;
1557 
1558  s = e + 1;
1559  }
1560 
1561  *pos = e - s_save;
1562  memcpy(ip, lip, sizeof(lip));
1563 
1564  return true;
1565 }
1566 
1567 static void cpy_ip(uint8_t *dst, uint8_t *src)
1568 {
1569  int i, j;
1570 
1571  for (i = 0, j = 3; i < 4; i++, j--)
1572  dst[i] = src[j];
1573 }
1574 
1575 COMMAND_HANDLER(jlink_handle_config_ip_address_command)
1576 {
1577  uint8_t ip_address[4];
1578  uint32_t subnet_mask = 0;
1579  int i, len;
1580  uint8_t subnet_bits = 24;
1581 
1582  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1583  command_print(CMD, "Reading configuration is not supported by the "
1584  "device");
1585  return ERROR_OK;
1586  }
1587 
1588  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1589  command_print(CMD, "Ethernet connectivity is not supported by the "
1590  "device");
1591  return ERROR_OK;
1592  }
1593 
1594  if (!CMD_ARGC) {
1596  } else {
1597  if (!string_to_ip(CMD_ARGV[0], ip_address, &i)) {
1598  command_print(CMD, "invalid IPv4 address");
1600  }
1601 
1602  len = strlen(CMD_ARGV[0]);
1603 
1604  /* Check for format A.B.C.D/E. */
1605  if (i < len) {
1606  if (CMD_ARGV[0][i] != '/') {
1607  command_print(CMD, "missing network mask");
1609  }
1610 
1611  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
1612  } else if (CMD_ARGC > 1) {
1613  if (!string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i)) {
1614  command_print(CMD, "invalid subnet mask");
1616  }
1617  }
1618 
1619  if (!subnet_mask)
1620  subnet_mask = (uint32_t)(subnet_bits < 32 ?
1621  ((1ULL << subnet_bits) - 1) : 0xffffffff);
1622 
1624  cpy_ip(tmp_config.subnet_mask, (uint8_t *)&subnet_mask);
1625  }
1626 
1627  return ERROR_OK;
1628 }
1629 
1630 COMMAND_HANDLER(jlink_handle_config_reset_command)
1631 {
1632  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG))
1633  return ERROR_OK;
1634 
1635  memcpy(&tmp_config, &config, sizeof(struct device_config));
1636 
1637  return ERROR_OK;
1638 }
1639 
1640 
1641 COMMAND_HANDLER(jlink_handle_config_write_command)
1642 {
1643  int ret;
1644 
1645  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1646  command_print(CMD, "Reading configuration is not supported by the "
1647  "device");
1648  return ERROR_OK;
1649  }
1650 
1651  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_WRITE_CONFIG)) {
1652  command_print(CMD, "Writing configuration is not supported by the "
1653  "device");
1654  return ERROR_OK;
1655  }
1656 
1657  if (!memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1658  command_print(CMD, "Operation not performed due to no changes in "
1659  "the configuration");
1660  return ERROR_OK;
1661  }
1662 
1663  ret = jaylink_write_raw_config(devh, (const uint8_t *)&tmp_config);
1664 
1665  if (ret != JAYLINK_OK) {
1666  LOG_ERROR("jaylink_write_raw_config() failed: %s",
1667  jaylink_strerror(ret));
1668  return ERROR_FAIL;
1669  }
1670 
1671  if (!read_device_config(&config)) {
1672  LOG_ERROR("Failed to read device configuration for verification");
1673  return ERROR_FAIL;
1674  }
1675 
1676  if (memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1677  LOG_ERROR("Verification of device configuration failed. Please check "
1678  "your device");
1679  return ERROR_FAIL;
1680  }
1681 
1682  memcpy(&tmp_config, &config, sizeof(struct device_config));
1683  command_print(CMD, "The new device configuration applies after power "
1684  "cycling the J-Link device");
1685 
1686  return ERROR_OK;
1687 }
1688 
1689 COMMAND_HANDLER(jlink_handle_config_command)
1690 {
1691  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1692  command_print(CMD, "Device doesn't support reading configuration");
1693  return ERROR_OK;
1694  }
1695 
1696  if (CMD_ARGC == 0)
1697  show_config(CMD);
1698 
1699  return ERROR_OK;
1700 }
1701 
1702 COMMAND_HANDLER(jlink_handle_emucom_write_command)
1703 {
1704  int ret;
1705  size_t tmp;
1706  uint32_t channel;
1707  uint32_t length;
1708  uint8_t *buf;
1709  size_t dummy;
1710 
1711  if (CMD_ARGC != 2)
1713 
1714  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1715  LOG_ERROR("Device does not support EMUCOM");
1716  return ERROR_FAIL;
1717  }
1718 
1719  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1720 
1721  tmp = strlen(CMD_ARGV[1]);
1722 
1723  if (tmp % 2 != 0) {
1724  LOG_ERROR("Data must be encoded as hexadecimal pairs");
1726  }
1727 
1728  buf = malloc(tmp / 2);
1729 
1730  if (!buf) {
1731  LOG_ERROR("Failed to allocate buffer");
1732  return ERROR_FAIL;
1733  }
1734 
1735  dummy = unhexify(buf, CMD_ARGV[1], tmp / 2);
1736 
1737  if (dummy != (tmp / 2)) {
1738  LOG_ERROR("Data must be encoded as hexadecimal pairs");
1739  free(buf);
1741  }
1742 
1743  length = tmp / 2;
1744  ret = jaylink_emucom_write(devh, channel, buf, &length);
1745 
1746  free(buf);
1747 
1748  if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1749  LOG_ERROR("Channel not supported by the device");
1750  return ERROR_FAIL;
1751  } else if (ret != JAYLINK_OK) {
1752  LOG_ERROR("Failed to write to channel: %s", jaylink_strerror(ret));
1753  return ERROR_FAIL;
1754  }
1755 
1756  if (length != (tmp / 2))
1757  LOG_WARNING("Only %" PRIu32 " bytes written to the channel", length);
1758 
1759  return ERROR_OK;
1760 }
1761 
1762 COMMAND_HANDLER(jlink_handle_emucom_read_command)
1763 {
1764  int ret;
1765  uint32_t channel;
1766  uint32_t length;
1767  uint8_t *buf;
1768  size_t tmp;
1769 
1770  if (CMD_ARGC != 2)
1772 
1773  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1774  LOG_ERROR("Device does not support EMUCOM");
1775  return ERROR_FAIL;
1776  }
1777 
1778  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1780 
1781  buf = malloc(length * 3 + 1);
1782 
1783  if (!buf) {
1784  LOG_ERROR("Failed to allocate buffer");
1785  return ERROR_FAIL;
1786  }
1787 
1788  ret = jaylink_emucom_read(devh, channel, buf, &length);
1789 
1790  if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1791  LOG_ERROR("Channel is not supported by the device");
1792  free(buf);
1793  return ERROR_FAIL;
1794  } else if (ret == JAYLINK_ERR_DEV_NOT_AVAILABLE) {
1795  LOG_ERROR("Channel is not available for the requested amount of data. "
1796  "%" PRIu32 " bytes are available", length);
1797  free(buf);
1798  return ERROR_FAIL;
1799  } else if (ret != JAYLINK_OK) {
1800  LOG_ERROR("Failed to read from channel: %s", jaylink_strerror(ret));
1801  free(buf);
1802  return ERROR_FAIL;
1803  }
1804 
1805  tmp = hexify((char *)buf + length, buf, length, 2 * length + 1);
1806 
1807  if (tmp != 2 * length) {
1808  LOG_ERROR("Failed to convert data into hexadecimal string");
1809  free(buf);
1810  return ERROR_FAIL;
1811  }
1812 
1813  command_print(CMD, "%s", buf + length);
1814  free(buf);
1815 
1816  return ERROR_OK;
1817 }
1818 
1820  {
1821  .name = "usb",
1822  .handler = &jlink_handle_config_usb_address_command,
1823  .mode = COMMAND_EXEC,
1824  .help = "set the USB address",
1825  .usage = "[0-3]",
1826  },
1827  {
1828  .name = "targetpower",
1829  .handler = &jlink_handle_config_target_power_command,
1830  .mode = COMMAND_EXEC,
1831  .help = "set the target power supply",
1832  .usage = "[on|off]"
1833  },
1834  {
1835  .name = "mac",
1836  .handler = &jlink_handle_config_mac_address_command,
1837  .mode = COMMAND_EXEC,
1838  .help = "set the MAC Address",
1839  .usage = "[ff:ff:ff:ff:ff:ff]",
1840  },
1841  {
1842  .name = "ip",
1843  .handler = &jlink_handle_config_ip_address_command,
1844  .mode = COMMAND_EXEC,
1845  .help = "set the IP address, where A.B.C.D is the IP address, "
1846  "E the bit of the subnet mask, F.G.H.I the subnet mask",
1847  .usage = "[A.B.C.D[/E] [F.G.H.I]]",
1848  },
1849  {
1850  .name = "reset",
1851  .handler = &jlink_handle_config_reset_command,
1852  .mode = COMMAND_EXEC,
1853  .help = "undo configuration changes",
1854  .usage = "",
1855  },
1856  {
1857  .name = "write",
1858  .handler = &jlink_handle_config_write_command,
1859  .mode = COMMAND_EXEC,
1860  .help = "write configuration to the device",
1861  .usage = "",
1862  },
1864 };
1865 
1867  {
1868  .name = "write",
1869  .handler = &jlink_handle_emucom_write_command,
1870  .mode = COMMAND_EXEC,
1871  .help = "write to a channel",
1872  .usage = "<channel> <data>",
1873  },
1874  {
1875  .name = "read",
1876  .handler = &jlink_handle_emucom_read_command,
1877  .mode = COMMAND_EXEC,
1878  .help = "read from a channel",
1879  .usage = "<channel> <length>"
1880  },
1882 };
1883 
1884 static const struct command_registration jlink_subcommand_handlers[] = {
1885  {
1886  .name = "jtag",
1887  .handler = &jlink_handle_jlink_jtag_command,
1888  .mode = COMMAND_EXEC,
1889  .help = "select the JTAG command version",
1890  .usage = "[2|3]",
1891  },
1892  {
1893  .name = "targetpower",
1894  .handler = &jlink_handle_target_power_command,
1895  .mode = COMMAND_EXEC,
1896  .help = "set the target power supply",
1897  .usage = "[0|1|on|off]"
1898  },
1899  {
1900  .name = "freemem",
1901  .handler = &jlink_handle_free_memory_command,
1902  .mode = COMMAND_EXEC,
1903  .help = "show free device memory",
1904  .usage = "",
1905  },
1906  {
1907  .name = "hwstatus",
1908  .handler = &jlink_handle_hwstatus_command,
1909  .mode = COMMAND_EXEC,
1910  .help = "show the hardware status",
1911  .usage = "",
1912  },
1913  {
1914  .name = "usb",
1915  .handler = &jlink_usb_command,
1916  .mode = COMMAND_CONFIG,
1917  .help = "set the USB address of the device that should be used",
1918  .usage = "<0-3>"
1919  },
1920  {
1921  .name = "config",
1922  .handler = &jlink_handle_config_command,
1923  .mode = COMMAND_EXEC,
1924  .help = "access the device configuration. If no argument is given "
1925  "this will show the device configuration",
1927  .usage = "[<cmd>]",
1928  },
1929  {
1930  .name = "emucom",
1931  .mode = COMMAND_EXEC,
1932  .help = "access EMUCOM channel",
1934  .usage = "",
1935  },
1937 };
1938 
1939 static const struct command_registration jlink_command_handlers[] = {
1940  {
1941  .name = "jlink",
1942  .mode = COMMAND_ANY,
1943  .help = "perform jlink management",
1944  .chain = jlink_subcommand_handlers,
1945  .usage = "",
1946  },
1948 };
1949 
1950 static int jlink_swd_init(void)
1951 {
1952  iface = JAYLINK_TIF_SWD;
1953 
1954  return ERROR_OK;
1955 }
1956 
1957 static void jlink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1958 {
1959  assert(!(cmd & SWD_CMD_RNW));
1960  jlink_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
1961 }
1962 
1963 static void jlink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1964 {
1965  assert(cmd & SWD_CMD_RNW);
1966  jlink_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
1967 }
1968 
1969 /***************************************************************************/
1970 /* J-Link tap functions */
1971 
1972 static unsigned int tap_length;
1973 /* In SWD mode use tms buffer for direction control */
1977 
1978 struct pending_scan_result {
1980  unsigned int first;
1982  unsigned int length;
1984  void *buffer;
1986  unsigned int buffer_offset;
1988  uint8_t swd_cmd;
1989 };
1990 
1991 #define MAX_PENDING_SCAN_RESULTS 256
1992 
1995 
1996 static void jlink_tap_init(void)
1997 {
1998  tap_length = 0;
2000  memset(tms_buffer, 0, sizeof(tms_buffer));
2001  memset(tdi_buffer, 0, sizeof(tdi_buffer));
2002 }
2003 
2004 static void jlink_clock_data(const uint8_t *out, unsigned int out_offset,
2005  const uint8_t *tms_out, unsigned int tms_offset,
2006  uint8_t *in, unsigned int in_offset,
2007  unsigned int length)
2008 {
2009  do {
2010  unsigned int available_length = JLINK_TAP_BUFFER_SIZE - tap_length / 8;
2011 
2012  if (!available_length ||
2014  if (jlink_flush() != ERROR_OK)
2015  return;
2016  available_length = JLINK_TAP_BUFFER_SIZE;
2017  }
2018 
2021 
2022  unsigned int scan_length = length > available_length ?
2023  available_length : length;
2024 
2025  if (out)
2026  buf_set_buf(out, out_offset, tdi_buffer, tap_length, scan_length);
2027  if (tms_out)
2028  buf_set_buf(tms_out, tms_offset, tms_buffer, tap_length, scan_length);
2029 
2030  if (in) {
2032  pending_scan_result->length = scan_length;
2034  pending_scan_result->buffer_offset = in_offset;
2036  }
2037 
2038  tap_length += scan_length;
2039  out_offset += scan_length;
2040  tms_offset += scan_length;
2041  in_offset += scan_length;
2042  length -= scan_length;
2043  } while (length > 0);
2044 }
2045 
2046 static int jlink_flush(void)
2047 {
2048  int i;
2049  int ret;
2050 
2051  if (!tap_length)
2052  return ERROR_OK;
2053 
2056 
2057  ret = jaylink_jtag_io(devh, tms_buffer, tdi_buffer, tdo_buffer,
2059 
2060  if (ret != JAYLINK_OK) {
2061  LOG_ERROR("jaylink_jtag_io() failed: %s", jaylink_strerror(ret));
2062  jlink_tap_init();
2063  return ERROR_JTAG_QUEUE_FAILED;
2064  }
2065 
2066  for (i = 0; i < pending_scan_results_length; i++) {
2068 
2070  p->buffer_offset, p->length);
2071 
2072  LOG_DEBUG_IO("Pending scan result, length = %d", p->length);
2073  }
2074 
2075  jlink_tap_init();
2076 
2077  return ERROR_OK;
2078 }
2079 
2080 static void fill_buffer(uint8_t *buf, uint32_t val, uint32_t len)
2081 {
2082  unsigned int tap_pos = tap_length;
2083 
2084  while (len > 32) {
2085  buf_set_u32(buf, tap_pos, 32, val);
2086  len -= 32;
2087  tap_pos += 32;
2088  }
2089 
2090  if (len)
2091  buf_set_u32(buf, tap_pos, len, val);
2092 }
2093 
2094 static void jlink_queue_data_out(const uint8_t *data, uint32_t len)
2095 {
2096  const uint32_t dir_out = 0xffffffff;
2097 
2098  if (data)
2099  bit_copy(tdi_buffer, tap_length, data, 0, len);
2100  else
2101  fill_buffer(tdi_buffer, 0, len);
2102 
2103  fill_buffer(tms_buffer, dir_out, len);
2104  tap_length += len;
2105 }
2106 
2107 static void jlink_queue_data_in(uint32_t len)
2108 {
2109  const uint32_t dir_in = 0;
2110 
2111  fill_buffer(tms_buffer, dir_in, len);
2112  tap_length += len;
2113 }
2114 
2116 {
2117  const uint8_t *s;
2118  unsigned int s_len;
2119 
2120  switch (seq) {
2121  case LINE_RESET:
2122  LOG_DEBUG_IO("SWD line reset");
2123  s = swd_seq_line_reset;
2124  s_len = swd_seq_line_reset_len;
2125  break;
2126  case JTAG_TO_SWD:
2127  LOG_DEBUG("JTAG-to-SWD");
2128  s = swd_seq_jtag_to_swd;
2129  s_len = swd_seq_jtag_to_swd_len;
2130  break;
2131  case JTAG_TO_DORMANT:
2132  LOG_DEBUG("JTAG-to-DORMANT");
2135  break;
2136  case SWD_TO_JTAG:
2137  LOG_DEBUG("SWD-to-JTAG");
2138  s = swd_seq_swd_to_jtag;
2139  s_len = swd_seq_swd_to_jtag_len;
2140  break;
2141  case SWD_TO_DORMANT:
2142  LOG_DEBUG("SWD-to-DORMANT");
2145  break;
2146  case DORMANT_TO_SWD:
2147  LOG_DEBUG("DORMANT-to-SWD");
2150  break;
2151  case DORMANT_TO_JTAG:
2152  LOG_DEBUG("DORMANT-to-JTAG");
2155  break;
2156  default:
2157  LOG_ERROR("Sequence %d not supported", seq);
2158  return ERROR_FAIL;
2159  }
2160 
2161  jlink_queue_data_out(s, s_len);
2162 
2163  return ERROR_OK;
2164 }
2165 
2166 static int jlink_swd_run_queue(void)
2167 {
2168  int i;
2169  int ret;
2170 
2171  LOG_DEBUG_IO("Executing %d queued transactions", pending_scan_results_length);
2172 
2173  if (queued_retval != ERROR_OK) {
2174  LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
2175  goto skip;
2176  }
2177 
2178  /*
2179  * A transaction must be followed by another transaction or at least 8 idle
2180  * cycles to ensure that data is clocked through the AP.
2181  */
2183 
2184  ret = jaylink_swd_io(devh, tms_buffer, tdi_buffer, tdo_buffer, tap_length);
2185 
2186  if (ret != JAYLINK_OK) {
2187  LOG_ERROR("jaylink_swd_io() failed: %s", jaylink_strerror(ret));
2188  goto skip;
2189  }
2190 
2191  for (i = 0; i < pending_scan_results_length; i++) {
2192  /* Devices do not reply to DP_TARGETSEL write cmd, ignore received ack */
2195  if (check_ack && ack != SWD_ACK_OK) {
2196  LOG_DEBUG("SWD ack not OK: %d %s", ack,
2197  ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
2199  goto skip;
2200  } else if (pending_scan_results_buffer[i].length) {
2201  uint32_t data = buf_get_u32(tdo_buffer, 3 + pending_scan_results_buffer[i].first, 32);
2203 
2204  if (parity != parity_u32(data)) {
2205  LOG_ERROR("SWD: Read data parity mismatch");
2207  goto skip;
2208  }
2209 
2211  *(uint32_t *)pending_scan_results_buffer[i].buffer = data;
2212  }
2213  }
2214 
2215 skip:
2216  jlink_tap_init();
2217  ret = queued_retval;
2219 
2220  return ret;
2221 }
2222 
2223 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
2224 {
2225  uint8_t data_parity_trn[DIV_ROUND_UP(32 + 1, 8)];
2226  if (tap_length + 46 + 8 + ap_delay_clk >= swd_buffer_size * 8 ||
2228  /* Not enough room in the queue. Run the queue. */
2230  }
2231 
2232  if (queued_retval != ERROR_OK)
2233  return;
2234 
2237 
2239 
2241 
2242  if (cmd & SWD_CMD_RNW) {
2243  /* Queue a read transaction. */
2246 
2247  jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2248  } else {
2249  /* Queue a write transaction. */
2251  jlink_queue_data_in(1 + 3 + 1);
2252 
2253  buf_set_u32(data_parity_trn, 0, 32, data);
2254  buf_set_u32(data_parity_trn, 32, 1, parity_u32(data));
2255 
2256  jlink_queue_data_out(data_parity_trn, 32 + 1);
2257  }
2258 
2260 
2261  /* Insert idle cycles after AP accesses to avoid WAIT. */
2262  if (cmd & SWD_CMD_APNDP)
2263  jlink_queue_data_out(NULL, ap_delay_clk);
2264 }
2265 
2266 static const struct swd_driver jlink_swd = {
2267  .init = &jlink_swd_init,
2268  .switch_seq = &jlink_swd_switch_seq,
2269  .read_reg = &jlink_swd_read_reg,
2270  .write_reg = &jlink_swd_write_reg,
2271  .run = &jlink_swd_run_queue,
2272 };
2273 
2274 static struct jtag_interface jlink_interface = {
2276 };
2277 
2279  .name = "jlink",
2280  .transport_ids = TRANSPORT_JTAG | TRANSPORT_SWD,
2281  .transport_preferred_id = TRANSPORT_JTAG,
2282  .commands = jlink_command_handlers,
2283 
2284  .init = &jlink_init,
2285  .quit = &jlink_quit,
2286  .reset = &jlink_reset_safe,
2287  .speed = &jlink_speed,
2288  .khz = &jlink_khz,
2289  .speed_div = &jlink_speed_div,
2290  .config_trace = &config_trace,
2291  .poll_trace = &poll_trace,
2292 
2293  .jtag_ops = &jlink_interface,
2294  .swd_ops = &jlink_swd,
2295 };
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Definition: adapter.c:300
bool adapter_usb_location_equal(uint8_t dev_bus, uint8_t *port_path, size_t path_len)
Definition: adapter.c:331
unsigned int adapter_get_speed_khz(void)
Retrieves the clock speed of the adapter in kHz.
Definition: adapter.c:208
const char * adapter_usb_get_location(void)
Definition: adapter.c:326
char * serial
Definition: adapter.c:43
#define SWD_ACK_FAULT
Definition: arm_adi_v5.h:33
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
tpiu_pin_protocol
Definition: arm_tpiu_swo.h:7
@ TPIU_PIN_PROTOCOL_ASYNC_UART
asynchronous output with NRZ coding
Definition: arm_tpiu_swo.h:10
const char * name
Definition: armv4_5.c:76
void * buf_set_buf(const void *_src, unsigned int src_start, void *_dst, unsigned int dst_start, unsigned int len)
Definition: binarybuffer.c:120
size_t hexify(char *hex, const uint8_t *bin, size_t count, size_t length)
Convert binary data into a string of hexadecimal pairs.
Definition: binarybuffer.c:380
size_t unhexify(uint8_t *bin, const char *hex, size_t count)
Convert a string of hexadecimal pairs into its binary representation.
Definition: binarybuffer.c:342
static void bit_copy(uint8_t *dst, unsigned int dst_offset, const uint8_t *src, unsigned int src_offset, unsigned int bit_count)
Definition: binarybuffer.h:218
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
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:375
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#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 COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:528
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:400
#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_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:440
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:402
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
enum scan_type jtag_scan_type(const struct scan_command *cmd)
Definition: commands.c:167
@ 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
#define TPIU_ACPR_MAX_SWOSCALER
Definition: cortex_m.h:126
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
uint32_t buffer_size
Size of dw_spi_program::buffer.
Definition: dw-spi-helper.h:5
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h: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_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
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:552
#define ERROR_JTAG_NOT_IMPLEMENTED
Definition: jtag.h:554
static struct libusb_device ** devs
The usb device list.
Definition: libusb_helper.c:26
void log_vprintf_lf(enum log_levels level, const char *file, unsigned int line, const char *function, const char *format, va_list args)
Definition: log.c:170
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:102
#define LOG_WARNING(expr ...)
Definition: log.h:130
#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
log_levels
Definition: log.h:40
@ LOG_LVL_DEBUG
Definition: log.h:47
@ LOG_LVL_DEBUG_IO
Definition: log.h:48
@ LOG_LVL_WARNING
Definition: log.h:45
@ LOG_LVL_ERROR
Definition: log.h:44
#define MIN(a, b)
Definition: replacements.h:22
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
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
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:234
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:239
uint8_t ip_address[4]
Definition: jlink.c:67
uint8_t reserved_1[3]
Definition: jlink.c:62
uint8_t subnet_mask[4]
Definition: jlink.c:68
uint8_t reserved_3[8]
Definition: jlink.c:70
uint8_t reserved_2[24]
Definition: jlink.c:65
uint32_t target_power
Definition: jlink.c:63
uint8_t mac_address[6]
Definition: jlink.c:71
uint8_t reserved_4[202]
Definition: jlink.c:73
uint8_t usb_address
Definition: jlink.c:60
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
unsigned int buffer_offset
Offset in the destination buffer.
Definition: cmsis_dap.c:233
uint8_t swd_cmd
SWD command.
Definition: jlink.c:1988
int first
First bit position in tdo_buffer to read.
Definition: arm-jtag-ew.c:514
uint8_t * ack
Definition: vsllink.c:32
void * buffer
Location to store the result.
Definition: jlink.c:1984
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
This structure defines a single scan field in the scan.
Definition: jtag.h:87
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
unsigned int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
Definition: swd.h:255
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
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
#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 status[4]
Definition: vdebug.c:17
uint8_t cmd
Definition: vdebug.c:1
uint8_t dummy[96]
Definition: vdebug.c:23
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22
static unsigned int parity(unsigned int v)
Definition: xscale.c:623