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