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_USB;
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  LOG_WARNING("DEPRECATED! Using the USB address is deprecated, use the serial number instead");
989 
990  unsigned int tmp;
991  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], tmp);
992 
993  if (tmp > JAYLINK_USB_ADDRESS_3) {
994  command_print(CMD, "Invalid USB address: %s", CMD_ARGV[0]);
996  }
997 
998  usb_address = tmp;
999  use_usb_address = true;
1000 
1001  return ERROR_OK;
1002 }
1003 
1004 COMMAND_HANDLER(jlink_handle_hwstatus_command)
1005 {
1006  int ret;
1007  struct jaylink_hardware_status status;
1008 
1009  ret = jaylink_get_hardware_status(devh, &status);
1010 
1011  if (ret != JAYLINK_OK) {
1012  command_print(CMD, "jaylink_get_hardware_status() failed: %s",
1013  jaylink_strerror(ret));
1014  return ERROR_FAIL;
1015  }
1016 
1017  command_print(CMD, "VTarget = %u.%03u V",
1018  status.target_voltage / 1000, status.target_voltage % 1000);
1019 
1020  command_print(CMD, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
1021  "TRST = %u", status.tck, status.tdi, status.tdo, status.tms,
1022  status.tres, status.trst);
1023 
1024  if (status.target_voltage < 1500)
1025  command_print(CMD, "Target voltage too low. Check target power");
1026 
1027  return ERROR_OK;
1028 }
1029 
1030 COMMAND_HANDLER(jlink_handle_free_memory_command)
1031 {
1032  int ret;
1033  uint32_t tmp;
1034 
1035  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY)) {
1036  command_print(CMD, "Retrieval of free memory is not supported by "
1037  "the device");
1038  return ERROR_OK;
1039  }
1040 
1041  ret = jaylink_get_free_memory(devh, &tmp);
1042 
1043  if (ret != JAYLINK_OK) {
1044  command_print(CMD, "jaylink_get_free_memory() failed: %s",
1045  jaylink_strerror(ret));
1046  return ERROR_FAIL;
1047  }
1048 
1049  command_print(CMD, "Device has %" PRIu32 " bytes of free memory", tmp);
1050 
1051  return ERROR_OK;
1052 }
1053 
1054 COMMAND_HANDLER(jlink_handle_jlink_jtag_command)
1055 {
1056  if (!CMD_ARGC) {
1057  unsigned int version;
1058 
1059  switch (jtag_command_version) {
1060  case JAYLINK_JTAG_VERSION_2:
1061  version = 2;
1062  break;
1063  case JAYLINK_JTAG_VERSION_3:
1064  version = 3;
1065  break;
1066  default:
1067  return ERROR_FAIL;
1068  }
1069 
1070  command_print(CMD, "JTAG command version: %u", version);
1071  } else if (CMD_ARGC == 1) {
1072  uint8_t tmp;
1073  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], tmp);
1074 
1075  switch (tmp) {
1076  case 2:
1077  jtag_command_version = JAYLINK_JTAG_VERSION_2;
1078  break;
1079  case 3:
1080  jtag_command_version = JAYLINK_JTAG_VERSION_3;
1081  break;
1082  default:
1083  command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1085  }
1086  } else {
1088  }
1089 
1090  return ERROR_OK;
1091 }
1092 
1093 COMMAND_HANDLER(jlink_handle_target_power_command)
1094 {
1095  if (CMD_ARGC > 1)
1097 
1098  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1099  command_print(CMD, "Target power supply is not supported by the "
1100  "device");
1101  return ERROR_OK;
1102  }
1103 
1104  if (!CMD_ARGC) {
1105  uint32_t state;
1106  int ret = jaylink_get_hardware_info(devh, JAYLINK_HW_INFO_TARGET_POWER,
1107  &state);
1108 
1109  if (ret != JAYLINK_OK) {
1110  command_print(CMD, "Failed to retrieve target power state");
1111  return ERROR_FAIL;
1112  }
1113 
1114  command_print(CMD, "%d", (bool)state);
1115  return ERROR_OK;
1116  }
1117 
1118  bool enable;
1119  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
1120 
1121  int ret = jaylink_set_target_power(devh, enable);
1122 
1123  if (ret != JAYLINK_OK) {
1124  command_print(CMD, "jaylink_set_target_power() failed: %s",
1125  jaylink_strerror(ret));
1126  return ERROR_FAIL;
1127  }
1128 
1129  return ERROR_OK;
1130 }
1131 
1133 {
1135  command_print(cmd, "USB address: %u [%u]", config.usb_address,
1137  else
1138  command_print(cmd, "USB address: %u", config.usb_address);
1139 }
1140 
1142 {
1143  if (!memcmp(config.ip_address, tmp_config.ip_address, 4))
1144  command_print(cmd, "IP address: %d.%d.%d.%d",
1147  else
1148  command_print(cmd, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1153 
1154  if (!memcmp(config.subnet_mask, tmp_config.subnet_mask, 4))
1155  command_print(cmd, "Subnet mask: %d.%d.%d.%d",
1158  else
1159  command_print(cmd, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1164 }
1165 
1167 {
1168  if (!memcmp(config.mac_address, tmp_config.mac_address, 6))
1169  command_print(cmd, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1173  else
1174  command_print(cmd, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1175  "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1182 }
1183 
1185 {
1186  const char *target_power;
1187  const char *current_target_power;
1188 
1189  if (!config.target_power)
1190  target_power = "off";
1191  else
1192  target_power = "on";
1193 
1194  if (!tmp_config.target_power)
1195  current_target_power = "off";
1196  else
1197  current_target_power = "on";
1198 
1200  command_print(cmd, "Target power supply: %s [%s]", target_power,
1201  current_target_power);
1202  else
1203  command_print(cmd, "Target power supply: %s", target_power);
1204 }
1205 
1206 static void show_config(struct command_invocation *cmd)
1207 {
1208  command_print(cmd, "J-Link device configuration:");
1209 
1211 
1212  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER))
1214 
1215  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1218  }
1219 }
1220 
1221 static int poll_trace(uint8_t *buf, size_t *size)
1222 {
1223  int ret;
1224  uint32_t length;
1225 
1226  length = *size;
1227 
1228  ret = jaylink_swo_read(devh, buf, &length);
1229 
1230  if (ret != JAYLINK_OK) {
1231  LOG_ERROR("jaylink_swo_read() failed: %s", jaylink_strerror(ret));
1232  return ERROR_FAIL;
1233  }
1234 
1235  *size = length;
1236 
1237  return ERROR_OK;
1238 }
1239 
1240 static uint32_t calculate_trace_buffer_size(void)
1241 {
1242  int ret;
1243  uint32_t tmp;
1244 
1245  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
1246  return 0;
1247 
1248  ret = jaylink_get_free_memory(devh, &tmp);
1249 
1250  if (ret != JAYLINK_OK) {
1251  LOG_ERROR("jaylink_get_free_memory() failed: %s",
1252  jaylink_strerror(ret));
1253  return ERROR_FAIL;
1254  }
1255 
1256  if (tmp > 0x3fff || tmp <= 0x600)
1257  tmp = tmp >> 1;
1258  else
1259  tmp = tmp - 0x400;
1260 
1261  return tmp & 0xffffff00;
1262 }
1263 
1264 static bool calculate_swo_prescaler(unsigned int traceclkin_freq,
1265  uint32_t trace_freq, uint16_t *prescaler)
1266 {
1267  unsigned int presc = (traceclkin_freq + trace_freq / 2) / trace_freq;
1268  if (presc == 0 || presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1269  return false;
1270 
1271  /* Probe's UART speed must be within 3% of the TPIU's SWO baud rate. */
1272  unsigned int max_deviation = (traceclkin_freq * 3) / 100;
1273  if (presc * trace_freq < traceclkin_freq - max_deviation ||
1274  presc * trace_freq > traceclkin_freq + max_deviation)
1275  return false;
1276 
1277  *prescaler = presc;
1278 
1279  return true;
1280 }
1281 
1282 static bool detect_swo_freq_and_prescaler(struct jaylink_swo_speed speed,
1283  unsigned int traceclkin_freq, unsigned int *trace_freq,
1284  uint16_t *prescaler)
1285 {
1286  uint32_t divider;
1287  unsigned int presc;
1288  double deviation;
1289 
1290  for (divider = speed.min_div; divider <= speed.max_div; divider++) {
1291  *trace_freq = speed.freq / divider;
1292  presc = ((1.0 - SWO_MAX_FREQ_DEV) * traceclkin_freq) / *trace_freq + 1;
1293 
1294  if (presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1295  break;
1296 
1297  deviation = fabs(1.0 - ((double)*trace_freq * presc / traceclkin_freq));
1298 
1299  if (deviation <= SWO_MAX_FREQ_DEV) {
1300  *prescaler = presc;
1301  return true;
1302  }
1303  }
1304 
1305  return false;
1306 }
1307 
1308 static int config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
1309  uint32_t port_size, unsigned int *trace_freq,
1310  unsigned int traceclkin_freq, uint16_t *prescaler)
1311 {
1312  int ret;
1313  uint32_t buffer_size;
1314  struct jaylink_swo_speed speed;
1315  uint32_t divider;
1316  uint32_t min_freq;
1317  uint32_t max_freq;
1318 
1319  trace_enabled = enabled;
1320 
1321  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SWO)) {
1322  if (!enabled)
1323  return ERROR_OK;
1324 
1325  LOG_ERROR("Trace capturing is not supported by the device");
1326  return ERROR_FAIL;
1327  }
1328 
1329  ret = jaylink_swo_stop(devh);
1330 
1331  if (ret != JAYLINK_OK) {
1332  LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
1333  return ERROR_FAIL;
1334  }
1335 
1336  if (!enabled) {
1337  /*
1338  * Adjust the SWD transaction buffer size as stopping SWO capturing
1339  * deallocates device internal memory.
1340  */
1341  if (!adjust_swd_buffer_size())
1342  return ERROR_FAIL;
1343 
1344  return ERROR_OK;
1345  }
1346 
1347  if (pin_protocol != TPIU_PIN_PROTOCOL_ASYNC_UART) {
1348  LOG_ERROR("Selected pin protocol is not supported");
1349  return ERROR_FAIL;
1350  }
1351 
1353 
1354  if (!buffer_size) {
1355  LOG_ERROR("Not enough free device memory to start trace capturing");
1356  return ERROR_FAIL;
1357  }
1358 
1359  ret = jaylink_swo_get_speeds(devh, JAYLINK_SWO_MODE_UART, &speed);
1360 
1361  if (ret != JAYLINK_OK) {
1362  LOG_ERROR("jaylink_swo_get_speeds() failed: %s",
1363  jaylink_strerror(ret));
1364  return ERROR_FAIL;
1365  }
1366 
1367  if (*trace_freq > 0) {
1368  divider = speed.freq / *trace_freq;
1369  min_freq = speed.freq / speed.max_div;
1370  max_freq = speed.freq / speed.min_div;
1371 
1372  if (*trace_freq > max_freq) {
1373  LOG_INFO("Given SWO frequency too high, using %" PRIu32 " Hz instead",
1374  max_freq);
1375  *trace_freq = max_freq;
1376  } else if (*trace_freq < min_freq) {
1377  LOG_INFO("Given SWO frequency too low, using %" PRIu32 " Hz instead",
1378  min_freq);
1379  *trace_freq = min_freq;
1380  } else if (*trace_freq != speed.freq / divider) {
1381  *trace_freq = speed.freq / divider;
1382 
1383  LOG_INFO("Given SWO frequency is not supported by the device, "
1384  "using %u Hz instead", *trace_freq);
1385  }
1386 
1387  if (!calculate_swo_prescaler(traceclkin_freq, *trace_freq,
1388  prescaler)) {
1389  LOG_ERROR("SWO frequency is not suitable. Please choose a "
1390  "different frequency or use auto-detection");
1391  return ERROR_FAIL;
1392  }
1393  } else {
1394  LOG_INFO("Trying to auto-detect SWO frequency");
1395 
1396  if (!detect_swo_freq_and_prescaler(speed, traceclkin_freq, trace_freq,
1397  prescaler)) {
1398  LOG_ERROR("Maximum permitted frequency deviation of %.02f %% "
1399  "could not be achieved", SWO_MAX_FREQ_DEV);
1400  LOG_ERROR("Auto-detection of SWO frequency failed");
1401  return ERROR_FAIL;
1402  }
1403 
1404  LOG_INFO("Using SWO frequency of %u Hz", *trace_freq);
1405  }
1406 
1407  ret = jaylink_swo_start(devh, JAYLINK_SWO_MODE_UART, *trace_freq,
1408  buffer_size);
1409 
1410  if (ret != JAYLINK_OK) {
1411  LOG_ERROR("jaylink_start_swo() failed: %s", jaylink_strerror(ret));
1412  return ERROR_FAIL;
1413  }
1414 
1415  LOG_DEBUG("Using %" PRIu32 " bytes device memory for trace capturing",
1416  buffer_size);
1417 
1418  /*
1419  * Adjust the SWD transaction buffer size as starting SWO capturing
1420  * allocates device internal memory.
1421  */
1422  if (!adjust_swd_buffer_size())
1423  return ERROR_FAIL;
1424 
1425  return ERROR_OK;
1426 }
1427 
1428 COMMAND_HANDLER(jlink_handle_config_usb_address_command)
1429 {
1430  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1431  command_print(CMD, "Reading configuration is not supported by the "
1432  "device");
1433  return ERROR_OK;
1434  }
1435 
1436  if (!CMD_ARGC) {
1438  } else if (CMD_ARGC == 1) {
1439  uint8_t tmp;
1440  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], tmp);
1441 
1442  if (tmp > JAYLINK_USB_ADDRESS_3) {
1443  command_print(CMD, "Invalid USB address: %u", tmp);
1445  }
1446 
1447  tmp_config.usb_address = tmp;
1448  } else {
1450  }
1451 
1452  return ERROR_OK;
1453 }
1454 
1455 COMMAND_HANDLER(jlink_handle_config_target_power_command)
1456 {
1457  int enable;
1458 
1459  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1460  command_print(CMD, "Reading configuration is not supported by the "
1461  "device");
1462  return ERROR_OK;
1463  }
1464 
1465  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1466  command_print(CMD, "Target power supply is not supported by the "
1467  "device");
1468  return ERROR_OK;
1469  }
1470 
1471  if (!CMD_ARGC) {
1473  } else if (CMD_ARGC == 1) {
1474  if (!strcmp(CMD_ARGV[0], "on")) {
1475  enable = true;
1476  } else if (!strcmp(CMD_ARGV[0], "off")) {
1477  enable = false;
1478  } else {
1479  command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1481  }
1482 
1483  tmp_config.target_power = enable;
1484  } else {
1486  }
1487 
1488  return ERROR_OK;
1489 }
1490 
1491 COMMAND_HANDLER(jlink_handle_config_mac_address_command)
1492 {
1493  uint8_t addr[6];
1494  int i;
1495  char *e;
1496  const char *str;
1497 
1498  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1499  command_print(CMD, "Reading configuration is not supported by the "
1500  "device");
1501  return ERROR_OK;
1502  }
1503 
1504  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1505  command_print(CMD, "Ethernet connectivity is not supported by the "
1506  "device");
1507  return ERROR_OK;
1508  }
1509 
1510  if (!CMD_ARGC) {
1512  } else if (CMD_ARGC == 1) {
1513  str = CMD_ARGV[0];
1514 
1515  if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' ||
1516  str[8] != ':' || str[11] != ':' || str[14] != ':')) {
1517  command_print(CMD, "Invalid MAC address format");
1519  }
1520 
1521  for (i = 5; i >= 0; i--) {
1522  addr[i] = strtoul(str, &e, 16);
1523  str = e + 1;
1524  }
1525 
1526  if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) {
1527  command_print(CMD, "Invalid MAC address: zero address");
1529  }
1530 
1531  if (!(0x01 & addr[0])) {
1532  command_print(CMD, "Invalid MAC address: multicast address");
1534  }
1535 
1536  memcpy(tmp_config.mac_address, addr, sizeof(addr));
1537  } else {
1539  }
1540 
1541  return ERROR_OK;
1542 }
1543 
1544 static bool string_to_ip(const char *s, uint8_t *ip, int *pos)
1545 {
1546  uint8_t lip[4];
1547  char *e;
1548  const char *s_save = s;
1549  int i;
1550 
1551  if (!s)
1552  return false;
1553 
1554  for (i = 0; i < 4; i++) {
1555  lip[i] = strtoul(s, &e, 10);
1556 
1557  if (*e != '.' && i != 3)
1558  return false;
1559 
1560  s = e + 1;
1561  }
1562 
1563  *pos = e - s_save;
1564  memcpy(ip, lip, sizeof(lip));
1565 
1566  return true;
1567 }
1568 
1569 static void cpy_ip(uint8_t *dst, uint8_t *src)
1570 {
1571  int i, j;
1572 
1573  for (i = 0, j = 3; i < 4; i++, j--)
1574  dst[i] = src[j];
1575 }
1576 
1577 COMMAND_HANDLER(jlink_handle_config_ip_address_command)
1578 {
1579  uint8_t ip_address[4];
1580  uint32_t subnet_mask = 0;
1581  int i, len;
1582  uint8_t subnet_bits = 24;
1583 
1584  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1585  command_print(CMD, "Reading configuration is not supported by the "
1586  "device");
1587  return ERROR_OK;
1588  }
1589 
1590  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1591  command_print(CMD, "Ethernet connectivity is not supported by the "
1592  "device");
1593  return ERROR_OK;
1594  }
1595 
1596  if (!CMD_ARGC) {
1598  } else {
1599  if (!string_to_ip(CMD_ARGV[0], ip_address, &i)) {
1600  command_print(CMD, "invalid IPv4 address");
1602  }
1603 
1604  len = strlen(CMD_ARGV[0]);
1605 
1606  /* Check for format A.B.C.D/E. */
1607  if (i < len) {
1608  if (CMD_ARGV[0][i] != '/') {
1609  command_print(CMD, "missing network mask");
1611  }
1612 
1613  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
1614  } else if (CMD_ARGC > 1) {
1615  if (!string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i)) {
1616  command_print(CMD, "invalid subnet mask");
1618  }
1619  }
1620 
1621  if (!subnet_mask)
1622  subnet_mask = (uint32_t)(subnet_bits < 32 ?
1623  ((1ULL << subnet_bits) - 1) : 0xffffffff);
1624 
1626  cpy_ip(tmp_config.subnet_mask, (uint8_t *)&subnet_mask);
1627  }
1628 
1629  return ERROR_OK;
1630 }
1631 
1632 COMMAND_HANDLER(jlink_handle_config_reset_command)
1633 {
1634  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG))
1635  return ERROR_OK;
1636 
1637  memcpy(&tmp_config, &config, sizeof(struct device_config));
1638 
1639  return ERROR_OK;
1640 }
1641 
1642 
1643 COMMAND_HANDLER(jlink_handle_config_write_command)
1644 {
1645  int ret;
1646 
1647  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1648  command_print(CMD, "Reading configuration is not supported by the "
1649  "device");
1650  return ERROR_OK;
1651  }
1652 
1653  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_WRITE_CONFIG)) {
1654  command_print(CMD, "Writing configuration is not supported by the "
1655  "device");
1656  return ERROR_OK;
1657  }
1658 
1659  if (!memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1660  command_print(CMD, "Operation not performed due to no changes in "
1661  "the configuration");
1662  return ERROR_OK;
1663  }
1664 
1665  ret = jaylink_write_raw_config(devh, (const uint8_t *)&tmp_config);
1666 
1667  if (ret != JAYLINK_OK) {
1668  LOG_ERROR("jaylink_write_raw_config() failed: %s",
1669  jaylink_strerror(ret));
1670  return ERROR_FAIL;
1671  }
1672 
1673  if (!read_device_config(&config)) {
1674  LOG_ERROR("Failed to read device configuration for verification");
1675  return ERROR_FAIL;
1676  }
1677 
1678  if (memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1679  LOG_ERROR("Verification of device configuration failed. Please check "
1680  "your device");
1681  return ERROR_FAIL;
1682  }
1683 
1684  memcpy(&tmp_config, &config, sizeof(struct device_config));
1685  command_print(CMD, "The new device configuration applies after power "
1686  "cycling the J-Link device");
1687 
1688  return ERROR_OK;
1689 }
1690 
1691 COMMAND_HANDLER(jlink_handle_config_command)
1692 {
1693  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1694  command_print(CMD, "Device doesn't support reading configuration");
1695  return ERROR_OK;
1696  }
1697 
1698  if (CMD_ARGC == 0)
1699  show_config(CMD);
1700 
1701  return ERROR_OK;
1702 }
1703 
1704 COMMAND_HANDLER(jlink_handle_emucom_write_command)
1705 {
1706  int ret;
1707  size_t tmp;
1708  uint32_t channel;
1709  uint32_t length;
1710  uint8_t *buf;
1711  size_t dummy;
1712 
1713  if (CMD_ARGC != 2)
1715 
1716  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1717  LOG_ERROR("Device does not support EMUCOM");
1718  return ERROR_FAIL;
1719  }
1720 
1721  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1722 
1723  tmp = strlen(CMD_ARGV[1]);
1724 
1725  if (tmp % 2 != 0) {
1726  LOG_ERROR("Data must be encoded as hexadecimal pairs");
1728  }
1729 
1730  buf = malloc(tmp / 2);
1731 
1732  if (!buf) {
1733  LOG_ERROR("Failed to allocate buffer");
1734  return ERROR_FAIL;
1735  }
1736 
1737  dummy = unhexify(buf, CMD_ARGV[1], tmp / 2);
1738 
1739  if (dummy != (tmp / 2)) {
1740  LOG_ERROR("Data must be encoded as hexadecimal pairs");
1741  free(buf);
1743  }
1744 
1745  length = tmp / 2;
1746  ret = jaylink_emucom_write(devh, channel, buf, &length);
1747 
1748  free(buf);
1749 
1750  if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1751  LOG_ERROR("Channel not supported by the device");
1752  return ERROR_FAIL;
1753  } else if (ret != JAYLINK_OK) {
1754  LOG_ERROR("Failed to write to channel: %s", jaylink_strerror(ret));
1755  return ERROR_FAIL;
1756  }
1757 
1758  if (length != (tmp / 2))
1759  LOG_WARNING("Only %" PRIu32 " bytes written to the channel", length);
1760 
1761  return ERROR_OK;
1762 }
1763 
1764 COMMAND_HANDLER(jlink_handle_emucom_read_command)
1765 {
1766  int ret;
1767  uint32_t channel;
1768  uint32_t length;
1769  uint8_t *buf;
1770  size_t tmp;
1771 
1772  if (CMD_ARGC != 2)
1774 
1775  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1776  LOG_ERROR("Device does not support EMUCOM");
1777  return ERROR_FAIL;
1778  }
1779 
1780  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1782 
1783  buf = malloc(length * 3 + 1);
1784 
1785  if (!buf) {
1786  LOG_ERROR("Failed to allocate buffer");
1787  return ERROR_FAIL;
1788  }
1789 
1790  ret = jaylink_emucom_read(devh, channel, buf, &length);
1791 
1792  if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1793  LOG_ERROR("Channel is not supported by the device");
1794  free(buf);
1795  return ERROR_FAIL;
1796  } else if (ret == JAYLINK_ERR_DEV_NOT_AVAILABLE) {
1797  LOG_ERROR("Channel is not available for the requested amount of data. "
1798  "%" PRIu32 " bytes are available", length);
1799  free(buf);
1800  return ERROR_FAIL;
1801  } else if (ret != JAYLINK_OK) {
1802  LOG_ERROR("Failed to read from channel: %s", jaylink_strerror(ret));
1803  free(buf);
1804  return ERROR_FAIL;
1805  }
1806 
1807  tmp = hexify((char *)buf + length, buf, length, 2 * length + 1);
1808 
1809  if (tmp != 2 * length) {
1810  LOG_ERROR("Failed to convert data into hexadecimal string");
1811  free(buf);
1812  return ERROR_FAIL;
1813  }
1814 
1815  command_print(CMD, "%s", buf + length);
1816  free(buf);
1817 
1818  return ERROR_OK;
1819 }
1820 
1822  {
1823  .name = "usb",
1824  .handler = &jlink_handle_config_usb_address_command,
1825  .mode = COMMAND_EXEC,
1826  .help = "set the USB address",
1827  .usage = "[0-3]",
1828  },
1829  {
1830  .name = "targetpower",
1831  .handler = &jlink_handle_config_target_power_command,
1832  .mode = COMMAND_EXEC,
1833  .help = "set the target power supply",
1834  .usage = "[on|off]"
1835  },
1836  {
1837  .name = "mac",
1838  .handler = &jlink_handle_config_mac_address_command,
1839  .mode = COMMAND_EXEC,
1840  .help = "set the MAC Address",
1841  .usage = "[ff:ff:ff:ff:ff:ff]",
1842  },
1843  {
1844  .name = "ip",
1845  .handler = &jlink_handle_config_ip_address_command,
1846  .mode = COMMAND_EXEC,
1847  .help = "set the IP address, where A.B.C.D is the IP address, "
1848  "E the bit of the subnet mask, F.G.H.I the subnet mask",
1849  .usage = "[A.B.C.D[/E] [F.G.H.I]]",
1850  },
1851  {
1852  .name = "reset",
1853  .handler = &jlink_handle_config_reset_command,
1854  .mode = COMMAND_EXEC,
1855  .help = "undo configuration changes",
1856  .usage = "",
1857  },
1858  {
1859  .name = "write",
1860  .handler = &jlink_handle_config_write_command,
1861  .mode = COMMAND_EXEC,
1862  .help = "write configuration to the device",
1863  .usage = "",
1864  },
1866 };
1867 
1869  {
1870  .name = "write",
1871  .handler = &jlink_handle_emucom_write_command,
1872  .mode = COMMAND_EXEC,
1873  .help = "write to a channel",
1874  .usage = "<channel> <data>",
1875  },
1876  {
1877  .name = "read",
1878  .handler = &jlink_handle_emucom_read_command,
1879  .mode = COMMAND_EXEC,
1880  .help = "read from a channel",
1881  .usage = "<channel> <length>"
1882  },
1884 };
1885 
1886 static const struct command_registration jlink_subcommand_handlers[] = {
1887  {
1888  .name = "jtag",
1889  .handler = &jlink_handle_jlink_jtag_command,
1890  .mode = COMMAND_EXEC,
1891  .help = "select the JTAG command version",
1892  .usage = "[2|3]",
1893  },
1894  {
1895  .name = "targetpower",
1896  .handler = &jlink_handle_target_power_command,
1897  .mode = COMMAND_EXEC,
1898  .help = "set the target power supply",
1899  .usage = "[0|1|on|off]"
1900  },
1901  {
1902  .name = "freemem",
1903  .handler = &jlink_handle_free_memory_command,
1904  .mode = COMMAND_EXEC,
1905  .help = "show free device memory",
1906  .usage = "",
1907  },
1908  {
1909  .name = "hwstatus",
1910  .handler = &jlink_handle_hwstatus_command,
1911  .mode = COMMAND_EXEC,
1912  .help = "show the hardware status",
1913  .usage = "",
1914  },
1915  {
1916  .name = "usb",
1917  .handler = &jlink_usb_command,
1918  .mode = COMMAND_CONFIG,
1919  .help = "set the USB address of the device that should be used",
1920  .usage = "<0-3>"
1921  },
1922  {
1923  .name = "config",
1924  .handler = &jlink_handle_config_command,
1925  .mode = COMMAND_EXEC,
1926  .help = "access the device configuration. If no argument is given "
1927  "this will show the device configuration",
1929  .usage = "[<cmd>]",
1930  },
1931  {
1932  .name = "emucom",
1933  .mode = COMMAND_EXEC,
1934  .help = "access EMUCOM channel",
1936  .usage = "",
1937  },
1939 };
1940 
1941 static const struct command_registration jlink_command_handlers[] = {
1942  {
1943  .name = "jlink",
1944  .mode = COMMAND_ANY,
1945  .help = "perform jlink management",
1946  .chain = jlink_subcommand_handlers,
1947  .usage = "",
1948  },
1950 };
1951 
1952 static int jlink_swd_init(void)
1953 {
1954  iface = JAYLINK_TIF_SWD;
1955 
1956  return ERROR_OK;
1957 }
1958 
1959 static int jlink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1960 {
1961  assert(!(cmd & SWD_CMD_RNW));
1962  jlink_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
1963 
1964  return ERROR_OK; /* TODO: return error instead of queuing it */
1965 }
1966 
1967 static int jlink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1968 {
1969  assert(cmd & SWD_CMD_RNW);
1970  jlink_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
1971 
1972  return ERROR_OK; /* TODO: return error instead of queuing it */
1973 }
1974 
1975 /***************************************************************************/
1976 /* J-Link tap functions */
1977 
1978 static unsigned int tap_length;
1979 /* In SWD mode use tms buffer for direction control */
1983 
1984 struct pending_scan_result {
1986  unsigned int first;
1988  unsigned int length;
1990  void *buffer;
1992  unsigned int buffer_offset;
1994  uint8_t swd_cmd;
1995 };
1996 
1997 #define MAX_PENDING_SCAN_RESULTS 256
1998 
2001 
2002 static void jlink_tap_init(void)
2003 {
2004  tap_length = 0;
2006  memset(tms_buffer, 0, sizeof(tms_buffer));
2007  memset(tdi_buffer, 0, sizeof(tdi_buffer));
2008 }
2009 
2010 static void jlink_clock_data(const uint8_t *out, unsigned int out_offset,
2011  const uint8_t *tms_out, unsigned int tms_offset,
2012  uint8_t *in, unsigned int in_offset,
2013  unsigned int length)
2014 {
2015  do {
2016  unsigned int available_length = JLINK_TAP_BUFFER_SIZE - tap_length / 8;
2017 
2018  if (!available_length ||
2020  if (jlink_flush() != ERROR_OK)
2021  return;
2022  available_length = JLINK_TAP_BUFFER_SIZE;
2023  }
2024 
2027 
2028  unsigned int scan_length = length > available_length ?
2029  available_length : length;
2030 
2031  if (out)
2032  buf_set_buf(out, out_offset, tdi_buffer, tap_length, scan_length);
2033  if (tms_out)
2034  buf_set_buf(tms_out, tms_offset, tms_buffer, tap_length, scan_length);
2035 
2036  if (in) {
2038  pending_scan_result->length = scan_length;
2040  pending_scan_result->buffer_offset = in_offset;
2042  }
2043 
2044  tap_length += scan_length;
2045  out_offset += scan_length;
2046  tms_offset += scan_length;
2047  in_offset += scan_length;
2048  length -= scan_length;
2049  } while (length > 0);
2050 }
2051 
2052 static int jlink_flush(void)
2053 {
2054  int i;
2055  int ret;
2056 
2057  if (!tap_length)
2058  return ERROR_OK;
2059 
2062 
2063  ret = jaylink_jtag_io(devh, tms_buffer, tdi_buffer, tdo_buffer,
2065 
2066  if (ret != JAYLINK_OK) {
2067  LOG_ERROR("jaylink_jtag_io() failed: %s", jaylink_strerror(ret));
2068  jlink_tap_init();
2069  return ERROR_JTAG_QUEUE_FAILED;
2070  }
2071 
2072  for (i = 0; i < pending_scan_results_length; i++) {
2074 
2076  p->buffer_offset, p->length);
2077 
2078  LOG_DEBUG_IO("Pending scan result, length = %d", p->length);
2079  }
2080 
2081  jlink_tap_init();
2082 
2083  return ERROR_OK;
2084 }
2085 
2086 static void fill_buffer(uint8_t *buf, uint32_t val, uint32_t len)
2087 {
2088  unsigned int tap_pos = tap_length;
2089 
2090  while (len > 32) {
2091  buf_set_u32(buf, tap_pos, 32, val);
2092  len -= 32;
2093  tap_pos += 32;
2094  }
2095 
2096  if (len)
2097  buf_set_u32(buf, tap_pos, len, val);
2098 }
2099 
2100 static void jlink_queue_data_out(const uint8_t *data, uint32_t len)
2101 {
2102  const uint32_t dir_out = 0xffffffff;
2103 
2104  if (data)
2105  bit_copy(tdi_buffer, tap_length, data, 0, len);
2106  else
2107  fill_buffer(tdi_buffer, 0, len);
2108 
2109  fill_buffer(tms_buffer, dir_out, len);
2110  tap_length += len;
2111 }
2112 
2113 static void jlink_queue_data_in(uint32_t len)
2114 {
2115  const uint32_t dir_in = 0;
2116 
2117  fill_buffer(tms_buffer, dir_in, len);
2118  tap_length += len;
2119 }
2120 
2122 {
2123  const uint8_t *s;
2124  unsigned int s_len;
2125 
2126  switch (seq) {
2127  case LINE_RESET:
2128  LOG_DEBUG_IO("SWD line reset");
2129  s = swd_seq_line_reset;
2130  s_len = swd_seq_line_reset_len;
2131  break;
2132  case JTAG_TO_SWD:
2133  LOG_DEBUG("JTAG-to-SWD");
2134  s = swd_seq_jtag_to_swd;
2135  s_len = swd_seq_jtag_to_swd_len;
2136  break;
2137  case JTAG_TO_DORMANT:
2138  LOG_DEBUG("JTAG-to-DORMANT");
2141  break;
2142  case SWD_TO_JTAG:
2143  LOG_DEBUG("SWD-to-JTAG");
2144  s = swd_seq_swd_to_jtag;
2145  s_len = swd_seq_swd_to_jtag_len;
2146  break;
2147  case SWD_TO_DORMANT:
2148  LOG_DEBUG("SWD-to-DORMANT");
2151  break;
2152  case DORMANT_TO_SWD:
2153  LOG_DEBUG("DORMANT-to-SWD");
2156  break;
2157  case DORMANT_TO_JTAG:
2158  LOG_DEBUG("DORMANT-to-JTAG");
2161  break;
2162  default:
2163  LOG_ERROR("Sequence %d not supported", seq);
2164  return ERROR_FAIL;
2165  }
2166 
2167  jlink_queue_data_out(s, s_len);
2168 
2169  return ERROR_OK;
2170 }
2171 
2172 static int jlink_swd_run_queue(void)
2173 {
2174  int i;
2175  int ret;
2176 
2177  LOG_DEBUG_IO("Executing %d queued transactions", pending_scan_results_length);
2178 
2179  if (queued_retval != ERROR_OK) {
2180  LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
2181  goto skip;
2182  }
2183 
2184  /*
2185  * A transaction must be followed by another transaction or at least 8 idle
2186  * cycles to ensure that data is clocked through the AP.
2187  */
2189 
2190  ret = jaylink_swd_io(devh, tms_buffer, tdi_buffer, tdo_buffer, tap_length);
2191 
2192  if (ret != JAYLINK_OK) {
2193  LOG_ERROR("jaylink_swd_io() failed: %s", jaylink_strerror(ret));
2194  goto skip;
2195  }
2196 
2197  for (i = 0; i < pending_scan_results_length; i++) {
2198  /* Devices do not reply to DP_TARGETSEL write cmd, ignore received ack */
2201  if (check_ack && ack != SWD_ACK_OK) {
2202  LOG_DEBUG("SWD ack not OK: %d %s", ack,
2203  ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
2205  goto skip;
2206  } else if (pending_scan_results_buffer[i].length) {
2207  uint32_t data = buf_get_u32(tdo_buffer, 3 + pending_scan_results_buffer[i].first, 32);
2209 
2210  if (parity != parity_u32(data)) {
2211  LOG_ERROR("SWD: Read data parity mismatch");
2213  goto skip;
2214  }
2215 
2217  *(uint32_t *)pending_scan_results_buffer[i].buffer = data;
2218  }
2219  }
2220 
2221 skip:
2222  jlink_tap_init();
2223  ret = queued_retval;
2225 
2226  return ret;
2227 }
2228 
2229 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
2230 {
2231  uint8_t data_parity_trn[DIV_ROUND_UP(32 + 1, 8)];
2232  if (tap_length + 46 + 8 + ap_delay_clk >= swd_buffer_size * 8 ||
2234  /* Not enough room in the queue. Run the queue. */
2236  }
2237 
2238  if (queued_retval != ERROR_OK)
2239  return;
2240 
2243 
2245 
2247 
2248  if (cmd & SWD_CMD_RNW) {
2249  /* Queue a read transaction. */
2252 
2253  jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2254  } else {
2255  /* Queue a write transaction. */
2257  jlink_queue_data_in(1 + 3 + 1);
2258 
2259  buf_set_u32(data_parity_trn, 0, 32, data);
2260  buf_set_u32(data_parity_trn, 32, 1, parity_u32(data));
2261 
2262  jlink_queue_data_out(data_parity_trn, 32 + 1);
2263  }
2264 
2266 
2267  /* Insert idle cycles after AP accesses to avoid WAIT. */
2268  if (cmd & SWD_CMD_APNDP)
2269  jlink_queue_data_out(NULL, ap_delay_clk);
2270 }
2271 
2272 static const struct swd_driver jlink_swd = {
2273  .init = &jlink_swd_init,
2274  .switch_seq = &jlink_swd_switch_seq,
2275  .read_reg = &jlink_swd_read_reg,
2276  .write_reg = &jlink_swd_write_reg,
2277  .run = &jlink_swd_run_queue,
2278 };
2279 
2280 static struct jtag_interface jlink_interface = {
2282 };
2283 
2285  .name = "jlink",
2286  .transport_ids = TRANSPORT_JTAG | TRANSPORT_SWD,
2287  .transport_preferred_id = TRANSPORT_JTAG,
2288  .commands = jlink_command_handlers,
2289 
2290  .init = &jlink_init,
2291  .quit = &jlink_quit,
2292  .reset = &jlink_reset_safe,
2293  .speed = &jlink_speed,
2294  .khz = &jlink_khz,
2295  .speed_div = &jlink_speed_div,
2296  .config_trace = &config_trace,
2297  .poll_trace = &poll_trace,
2298 
2299  .jtag_ops = &jlink_interface,
2300  .swd_ops = &jlink_swd,
2301 };
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Definition: adapter.c:309
bool adapter_usb_location_equal(uint8_t dev_bus, uint8_t *port_path, size_t path_len)
Definition: adapter.c:355
unsigned int adapter_get_speed_khz(void)
Retrieves the clock speed of the adapter in kHz.
Definition: adapter.c:217
const char * adapter_usb_get_location(void)
Definition: adapter.c:345
char * serial
Definition: adapter.c:49
#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:75
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:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:533
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_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:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
@ 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:172
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:168
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1058
#define ERROR_JTAG_DEVICE_ERROR
Definition: jtag.h:558
tap_state
Defines JTAG Test Access Port states.
Definition: jtag.h:37
@ TAP_RESET
Definition: jtag.h:56
@ TAP_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:177
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:116
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
log_levels
Definition: log.h:48
@ LOG_LVL_DEBUG_USB
Definition: log.h:59
@ LOG_LVL_DEBUG
Definition: log.h:55
@ LOG_LVL_WARNING
Definition: log.h:53
@ LOG_LVL_ERROR
Definition: log.h:52
#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:214
const char *const name
The name of the interface driver.
Definition: interface.h:216
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:239
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
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:189
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
Definition: interface.h:202
unsigned int buffer_offset
Offset in the destination buffer.
Definition: cmsis_dap.c:238
uint8_t swd_cmd
SWD command.
Definition: jlink.c:1994
int first
First bit position in tdo_buffer to read.
Definition: arm-jtag-ew.c:511
uint8_t * ack
Definition: vsllink.c:32
void * buffer
Location to store the result.
Definition: jlink.c:1990
int length
Number of bits to read.
Definition: arm-jtag-ew.c:512
uint8_t * buffer
Location to store the result.
Definition: arm-jtag-ew.c:514
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:619