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(unsigned int num_states, tap_state_t *path);
84 static void jlink_stableclocks(unsigned int num_cycles);
85 static void jlink_runtest(unsigned 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 int out_offset,
108  const uint8_t *tms_out, unsigned int tms_offset,
109  uint8_t *in, unsigned int in_offset,
110  unsigned int 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: %u 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) {
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 int scan_size = 0;
183 
184  for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
185  scan_size += field->num_bits;
186  LOG_DEBUG_IO("%s%s field %u/%u %u 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  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  LOG_INFO("Device %zu serial: %" PRIu32, i, serial);
579  }
580 
581  jaylink_free_devices(devs, true);
582  jaylink_exit(jayctx);
583  return ERROR_JTAG_INIT_FAILED;
584  }
585 
586  *found_device = false;
587 
588  for (size_t i = 0; devs[i]; i++) {
589  struct jaylink_device *dev = devs[i];
590 
591  if (use_serial_number) {
592  uint32_t tmp;
593  ret = jaylink_device_get_serial_number(dev, &tmp);
594 
595  if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
596  continue;
597  } else if (ret != JAYLINK_OK) {
598  LOG_WARNING("jaylink_device_get_serial_number() failed: %s",
599  jaylink_strerror(ret));
600  continue;
601  }
602 
603  if (serial_number != tmp)
604  continue;
605  }
606 
607  if (use_usb_address) {
608  enum jaylink_usb_address address;
609  ret = jaylink_device_get_usb_address(dev, &address);
610 
611  if (ret == JAYLINK_ERR_NOT_SUPPORTED) {
612  continue;
613  } else if (ret != JAYLINK_OK) {
614  LOG_WARNING("jaylink_device_get_usb_address() failed: %s",
615  jaylink_strerror(ret));
616  continue;
617  }
618 
619  if (usb_address != address)
620  continue;
621  }
622 
624  continue;
625 
626  ret = jaylink_open(dev, &devh);
627 
628  if (ret == JAYLINK_OK) {
629  *found_device = true;
630  break;
631  }
632 
633  LOG_ERROR("Failed to open device: %s", jaylink_strerror(ret));
634  }
635 
636  jaylink_free_devices(devs, true);
637  return ERROR_OK;
638 }
639 
640 
641 static int jlink_init(void)
642 {
643  int ret;
644  char *firmware_version;
645  struct jaylink_hardware_version hwver;
646  struct jaylink_hardware_status hwstatus;
647  size_t length;
648 
649  LOG_DEBUG("Using libjaylink %s (compiled with %s)",
650  jaylink_version_package_get_string(), JAYLINK_VERSION_PACKAGE_STRING);
651 
652  if (!jaylink_library_has_cap(JAYLINK_CAP_HIF_USB) && use_usb_address) {
653  LOG_ERROR("J-Link driver does not support USB devices");
654  return ERROR_JTAG_INIT_FAILED;
655  }
656 
657  ret = jaylink_init(&jayctx);
658 
659  if (ret != JAYLINK_OK) {
660  LOG_ERROR("jaylink_init() failed: %s", jaylink_strerror(ret));
661  return ERROR_JTAG_INIT_FAILED;
662  }
663 
664  ret = jaylink_log_set_callback(jayctx, &jaylink_log_handler, NULL);
665 
666  if (ret != JAYLINK_OK) {
667  LOG_ERROR("jaylink_log_set_callback() failed: %s",
668  jaylink_strerror(ret));
669  jaylink_exit(jayctx);
670  return ERROR_JTAG_INIT_FAILED;
671  }
672 
673  const char *serial = adapter_get_required_serial();
674  if (serial) {
675  ret = jaylink_parse_serial_number(serial, &serial_number);
676  if (ret == JAYLINK_ERR) {
677  LOG_ERROR("Invalid serial number: %s", serial);
678  jaylink_exit(jayctx);
679  return ERROR_JTAG_INIT_FAILED;
680  }
681  if (ret != JAYLINK_OK) {
682  LOG_ERROR("jaylink_parse_serial_number() failed: %s", jaylink_strerror(ret));
683  jaylink_exit(jayctx);
684  return ERROR_JTAG_INIT_FAILED;
685  }
686  use_serial_number = true;
687  use_usb_address = false;
688  }
689 
690  bool found_device;
691  ret = jlink_open_device(JAYLINK_HIF_USB, &found_device);
692  if (ret != ERROR_OK)
693  return ret;
694 
695  if (!found_device && use_serial_number) {
696  ret = jlink_open_device(JAYLINK_HIF_TCP, &found_device);
697  if (ret != ERROR_OK)
698  return ret;
699  }
700 
701  if (!found_device) {
702  LOG_ERROR("No J-Link device found");
703  jaylink_exit(jayctx);
704  return ERROR_JTAG_INIT_FAILED;
705  }
706 
707  /*
708  * Be careful with changing the following initialization sequence because
709  * some devices are known to be sensitive regarding the order.
710  */
711 
712  ret = jaylink_get_firmware_version(devh, &firmware_version, &length);
713 
714  if (ret != JAYLINK_OK) {
715  LOG_ERROR("jaylink_get_firmware_version() failed: %s",
716  jaylink_strerror(ret));
717  jaylink_close(devh);
718  jaylink_exit(jayctx);
719  return ERROR_JTAG_INIT_FAILED;
720  } else if (length > 0) {
721  LOG_INFO("%s", firmware_version);
722  free(firmware_version);
723  } else {
724  LOG_WARNING("Device responds empty firmware version string");
725  }
726 
727  memset(caps, 0, JAYLINK_DEV_EXT_CAPS_SIZE);
728  ret = jaylink_get_caps(devh, caps);
729 
730  if (ret != JAYLINK_OK) {
731  LOG_ERROR("jaylink_get_caps() failed: %s", jaylink_strerror(ret));
732  jaylink_close(devh);
733  jaylink_exit(jayctx);
734  return ERROR_JTAG_INIT_FAILED;
735  }
736 
737  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
738  ret = jaylink_get_extended_caps(devh, caps);
739 
740  if (ret != JAYLINK_OK) {
741  LOG_ERROR("jaylink_get_extended_caps() failed: %s",
742  jaylink_strerror(ret));
743  jaylink_close(devh);
744  jaylink_exit(jayctx);
745  return ERROR_JTAG_INIT_FAILED;
746  }
747  }
748 
749  jtag_command_version = JAYLINK_JTAG_VERSION_2;
750 
751  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_HW_VERSION)) {
752  ret = jaylink_get_hardware_version(devh, &hwver);
753 
754  if (ret != JAYLINK_OK) {
755  LOG_ERROR("Failed to retrieve hardware version: %s",
756  jaylink_strerror(ret));
757  jaylink_close(devh);
758  jaylink_exit(jayctx);
759  return ERROR_JTAG_INIT_FAILED;
760  }
761 
762  LOG_INFO("Hardware version: %u.%02u", hwver.major, hwver.minor);
763 
764  if (hwver.major >= 5)
765  jtag_command_version = JAYLINK_JTAG_VERSION_3;
766  }
767 
768  if (iface == JAYLINK_TIF_SWD) {
769  /*
770  * Adjust the SWD transaction buffer size in case there is already
771  * allocated memory on the device. This happens for example if the
772  * memory for SWO capturing is still allocated because the software
773  * which used the device before has not been shut down properly.
774  */
775  if (!adjust_swd_buffer_size()) {
776  jaylink_close(devh);
777  jaylink_exit(jayctx);
778  return ERROR_JTAG_INIT_FAILED;
779  }
780  }
781 
782  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
783  if (!read_device_config(&config)) {
784  LOG_ERROR("Failed to read device configuration data");
785  jaylink_close(devh);
786  jaylink_exit(jayctx);
787  return ERROR_JTAG_INIT_FAILED;
788  }
789 
790  memcpy(&tmp_config, &config, sizeof(struct device_config));
791  }
792 
793  ret = jaylink_get_hardware_status(devh, &hwstatus);
794 
795  if (ret != JAYLINK_OK) {
796  LOG_ERROR("jaylink_get_hardware_status() failed: %s",
797  jaylink_strerror(ret));
798  jaylink_close(devh);
799  jaylink_exit(jayctx);
800  return ERROR_JTAG_INIT_FAILED;
801  }
802 
803  LOG_INFO("VTarget = %u.%03u V", hwstatus.target_voltage / 1000,
804  hwstatus.target_voltage % 1000);
805 
806  conn.handle = 0;
807  conn.pid = 0;
808  strcpy(conn.hid, "0.0.0.0");
809  conn.iid = 0;
810  conn.cid = 0;
811 
812  ret = jlink_register();
813 
814  if (ret != ERROR_OK) {
815  jaylink_close(devh);
816  jaylink_exit(jayctx);
817  return ERROR_JTAG_INIT_FAILED;
818  }
819 
820  ret = select_interface();
821 
822  if (ret != ERROR_OK) {
823  jaylink_close(devh);
824  jaylink_exit(jayctx);
825  return ret;
826  }
827 
828  jlink_reset(0, 0);
829  jtag_sleep(3000);
830  jlink_tap_init();
831 
833 
834  if (iface == JAYLINK_TIF_JTAG) {
835  /*
836  * J-Link devices with firmware version v5 and v6 seems to have an issue
837  * if the first tap move is not divisible by 8, so we send a TLR on
838  * first power up.
839  */
840  uint8_t tms = 0xff;
841  jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 8);
842 
843  jlink_flush();
844  }
845 
846  return ERROR_OK;
847 }
848 
849 static int jlink_quit(void)
850 {
851  int ret;
852  size_t count;
853 
854  if (trace_enabled) {
855  ret = jaylink_swo_stop(devh);
856 
857  if (ret != JAYLINK_OK)
858  LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
859  }
860 
861  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER)) {
862  ret = jaylink_unregister(devh, &conn, connlist, &count);
863 
864  if (ret != JAYLINK_OK)
865  LOG_ERROR("jaylink_unregister() failed: %s",
866  jaylink_strerror(ret));
867  }
868 
869  jaylink_close(devh);
870  jaylink_exit(jayctx);
871 
872  return ERROR_OK;
873 }
874 
875 /***************************************************************************/
876 /* Queue command implementations */
877 
879 {
882  else {
883  LOG_ERROR("BUG: %i is not a valid end state", state);
884  exit(-1);
885  }
886 }
887 
888 /* Goes to the end state. */
889 static void jlink_state_move(void)
890 {
891  uint8_t tms_scan;
892  uint8_t tms_scan_bits;
893 
896 
897  jlink_clock_data(NULL, 0, &tms_scan, 0, NULL, 0, tms_scan_bits);
898 
900 }
901 
902 static void jlink_path_move(unsigned int num_states, tap_state_t *path)
903 {
904  uint8_t tms = 0xff;
905 
906  for (unsigned int i = 0; i < num_states; i++) {
907  if (path[i] == tap_state_transition(tap_get_state(), false))
908  jlink_clock_data(NULL, 0, NULL, 0, NULL, 0, 1);
909  else if (path[i] == tap_state_transition(tap_get_state(), true))
910  jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
911  else {
912  LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
914  exit(-1);
915  }
916 
917  tap_set_state(path[i]);
918  }
919 
921 }
922 
923 static void jlink_stableclocks(unsigned int num_cycles)
924 {
925  uint8_t tms = tap_get_state() == TAP_RESET;
926  /* Execute num_cycles. */
927  for (unsigned int i = 0; i < num_cycles; i++)
928  jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
929 }
930 
931 static void jlink_runtest(unsigned int num_cycles)
932 {
933  tap_state_t saved_end_state = tap_get_end_state();
934 
935  /* Only do a state_move when we're not already in IDLE. */
936  if (tap_get_state() != TAP_IDLE) {
939  /* num_cycles--; */
940  }
941 
942  jlink_stableclocks(num_cycles);
943 
944  /* Finish in end_state. */
945  jlink_end_state(saved_end_state);
946 
947  if (tap_get_state() != tap_get_end_state())
949 }
950 
951 static void jlink_reset(int trst, int srst)
952 {
953  LOG_DEBUG("TRST: %i, SRST: %i", trst, srst);
954 
955  /* Signals are active low. */
956  if (srst == 0)
957  jaylink_set_reset(devh);
958 
959  if (srst == 1)
960  jaylink_clear_reset(devh);
961 
962  if (trst == 1)
963  jaylink_jtag_clear_trst(devh);
964 
965  if (trst == 0)
966  jaylink_jtag_set_trst(devh);
967 }
968 
969 static int jlink_reset_safe(int trst, int srst)
970 {
971  jlink_flush();
972  jlink_reset(trst, srst);
973  return jlink_flush();
974 }
975 
976 COMMAND_HANDLER(jlink_usb_command)
977 {
978  if (CMD_ARGC != 1)
980 
981  unsigned int tmp;
982  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], tmp);
983 
984  if (tmp > JAYLINK_USB_ADDRESS_3) {
985  command_print(CMD, "Invalid USB address: %s", CMD_ARGV[0]);
987  }
988 
989  usb_address = tmp;
990  use_usb_address = true;
991 
992  return ERROR_OK;
993 }
994 
995 COMMAND_HANDLER(jlink_handle_hwstatus_command)
996 {
997  int ret;
998  struct jaylink_hardware_status status;
999 
1000  ret = jaylink_get_hardware_status(devh, &status);
1001 
1002  if (ret != JAYLINK_OK) {
1003  command_print(CMD, "jaylink_get_hardware_status() failed: %s",
1004  jaylink_strerror(ret));
1005  return ERROR_FAIL;
1006  }
1007 
1008  command_print(CMD, "VTarget = %u.%03u V",
1009  status.target_voltage / 1000, status.target_voltage % 1000);
1010 
1011  command_print(CMD, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
1012  "TRST = %u", status.tck, status.tdi, status.tdo, status.tms,
1013  status.tres, status.trst);
1014 
1015  if (status.target_voltage < 1500)
1016  command_print(CMD, "Target voltage too low. Check target power");
1017 
1018  return ERROR_OK;
1019 }
1020 
1021 COMMAND_HANDLER(jlink_handle_free_memory_command)
1022 {
1023  int ret;
1024  uint32_t tmp;
1025 
1026  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY)) {
1027  command_print(CMD, "Retrieval of free memory is not supported by "
1028  "the device");
1029  return ERROR_OK;
1030  }
1031 
1032  ret = jaylink_get_free_memory(devh, &tmp);
1033 
1034  if (ret != JAYLINK_OK) {
1035  command_print(CMD, "jaylink_get_free_memory() failed: %s",
1036  jaylink_strerror(ret));
1037  return ERROR_FAIL;
1038  }
1039 
1040  command_print(CMD, "Device has %" PRIu32 " bytes of free memory", tmp);
1041 
1042  return ERROR_OK;
1043 }
1044 
1045 COMMAND_HANDLER(jlink_handle_jlink_jtag_command)
1046 {
1047  if (!CMD_ARGC) {
1048  unsigned int version;
1049 
1050  switch (jtag_command_version) {
1051  case JAYLINK_JTAG_VERSION_2:
1052  version = 2;
1053  break;
1054  case JAYLINK_JTAG_VERSION_3:
1055  version = 3;
1056  break;
1057  default:
1058  return ERROR_FAIL;
1059  }
1060 
1061  command_print(CMD, "JTAG command version: %u", version);
1062  } else if (CMD_ARGC == 1) {
1063  uint8_t tmp;
1064  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], tmp);
1065 
1066  switch (tmp) {
1067  case 2:
1068  jtag_command_version = JAYLINK_JTAG_VERSION_2;
1069  break;
1070  case 3:
1071  jtag_command_version = JAYLINK_JTAG_VERSION_3;
1072  break;
1073  default:
1074  command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1076  }
1077  } else {
1079  }
1080 
1081  return ERROR_OK;
1082 }
1083 
1084 COMMAND_HANDLER(jlink_handle_target_power_command)
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 (!CMD_ARGC) {
1096  uint32_t state;
1097  int ret = jaylink_get_hardware_info(devh, JAYLINK_HW_INFO_TARGET_POWER,
1098  &state);
1099 
1100  if (ret != JAYLINK_OK) {
1101  command_print(CMD, "Failed to retrieve target power state");
1102  return ERROR_FAIL;
1103  }
1104 
1105  command_print(CMD, "%d", (bool)state);
1106  return ERROR_OK;
1107  }
1108 
1109  bool enable;
1110  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
1111 
1112  int ret = jaylink_set_target_power(devh, enable);
1113 
1114  if (ret != JAYLINK_OK) {
1115  command_print(CMD, "jaylink_set_target_power() failed: %s",
1116  jaylink_strerror(ret));
1117  return ERROR_FAIL;
1118  }
1119 
1120  return ERROR_OK;
1121 }
1122 
1124 {
1126  command_print(cmd, "USB address: %u [%u]", config.usb_address,
1128  else
1129  command_print(cmd, "USB address: %u", config.usb_address);
1130 }
1131 
1133 {
1134  if (!memcmp(config.ip_address, tmp_config.ip_address, 4))
1135  command_print(cmd, "IP address: %d.%d.%d.%d",
1138  else
1139  command_print(cmd, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1144 
1145  if (!memcmp(config.subnet_mask, tmp_config.subnet_mask, 4))
1146  command_print(cmd, "Subnet mask: %d.%d.%d.%d",
1149  else
1150  command_print(cmd, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1155 }
1156 
1158 {
1159  if (!memcmp(config.mac_address, tmp_config.mac_address, 6))
1160  command_print(cmd, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1164  else
1165  command_print(cmd, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1166  "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1173 }
1174 
1176 {
1177  const char *target_power;
1178  const char *current_target_power;
1179 
1180  if (!config.target_power)
1181  target_power = "off";
1182  else
1183  target_power = "on";
1184 
1185  if (!tmp_config.target_power)
1186  current_target_power = "off";
1187  else
1188  current_target_power = "on";
1189 
1191  command_print(cmd, "Target power supply: %s [%s]", target_power,
1192  current_target_power);
1193  else
1194  command_print(cmd, "Target power supply: %s", target_power);
1195 }
1196 
1197 static void show_config(struct command_invocation *cmd)
1198 {
1199  command_print(cmd, "J-Link device configuration:");
1200 
1202 
1203  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER))
1205 
1206  if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1209  }
1210 }
1211 
1212 static int poll_trace(uint8_t *buf, size_t *size)
1213 {
1214  int ret;
1215  uint32_t length;
1216 
1217  length = *size;
1218 
1219  ret = jaylink_swo_read(devh, buf, &length);
1220 
1221  if (ret != JAYLINK_OK) {
1222  LOG_ERROR("jaylink_swo_read() failed: %s", jaylink_strerror(ret));
1223  return ERROR_FAIL;
1224  }
1225 
1226  *size = length;
1227 
1228  return ERROR_OK;
1229 }
1230 
1231 static uint32_t calculate_trace_buffer_size(void)
1232 {
1233  int ret;
1234  uint32_t tmp;
1235 
1236  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
1237  return 0;
1238 
1239  ret = jaylink_get_free_memory(devh, &tmp);
1240 
1241  if (ret != JAYLINK_OK) {
1242  LOG_ERROR("jaylink_get_free_memory() failed: %s",
1243  jaylink_strerror(ret));
1244  return ERROR_FAIL;
1245  }
1246 
1247  if (tmp > 0x3fff || tmp <= 0x600)
1248  tmp = tmp >> 1;
1249  else
1250  tmp = tmp - 0x400;
1251 
1252  return tmp & 0xffffff00;
1253 }
1254 
1255 static bool calculate_swo_prescaler(unsigned int traceclkin_freq,
1256  uint32_t trace_freq, uint16_t *prescaler)
1257 {
1258  unsigned int presc = (traceclkin_freq + trace_freq / 2) / trace_freq;
1259  if (presc == 0 || presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1260  return false;
1261 
1262  /* Probe's UART speed must be within 3% of the TPIU's SWO baud rate. */
1263  unsigned int max_deviation = (traceclkin_freq * 3) / 100;
1264  if (presc * trace_freq < traceclkin_freq - max_deviation ||
1265  presc * trace_freq > traceclkin_freq + max_deviation)
1266  return false;
1267 
1268  *prescaler = presc;
1269 
1270  return true;
1271 }
1272 
1273 static bool detect_swo_freq_and_prescaler(struct jaylink_swo_speed speed,
1274  unsigned int traceclkin_freq, unsigned int *trace_freq,
1275  uint16_t *prescaler)
1276 {
1277  uint32_t divider;
1278  unsigned int presc;
1279  double deviation;
1280 
1281  for (divider = speed.min_div; divider <= speed.max_div; divider++) {
1282  *trace_freq = speed.freq / divider;
1283  presc = ((1.0 - SWO_MAX_FREQ_DEV) * traceclkin_freq) / *trace_freq + 1;
1284 
1285  if (presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1286  break;
1287 
1288  deviation = fabs(1.0 - ((double)*trace_freq * presc / traceclkin_freq));
1289 
1290  if (deviation <= SWO_MAX_FREQ_DEV) {
1291  *prescaler = presc;
1292  return true;
1293  }
1294  }
1295 
1296  return false;
1297 }
1298 
1299 static int config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
1300  uint32_t port_size, unsigned int *trace_freq,
1301  unsigned int traceclkin_freq, uint16_t *prescaler)
1302 {
1303  int ret;
1304  uint32_t buffer_size;
1305  struct jaylink_swo_speed speed;
1306  uint32_t divider;
1307  uint32_t min_freq;
1308  uint32_t max_freq;
1309 
1310  trace_enabled = enabled;
1311 
1312  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SWO)) {
1313  if (!enabled)
1314  return ERROR_OK;
1315 
1316  LOG_ERROR("Trace capturing is not supported by the device");
1317  return ERROR_FAIL;
1318  }
1319 
1320  ret = jaylink_swo_stop(devh);
1321 
1322  if (ret != JAYLINK_OK) {
1323  LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
1324  return ERROR_FAIL;
1325  }
1326 
1327  if (!enabled) {
1328  /*
1329  * Adjust the SWD transaction buffer size as stopping SWO capturing
1330  * deallocates device internal memory.
1331  */
1332  if (!adjust_swd_buffer_size())
1333  return ERROR_FAIL;
1334 
1335  return ERROR_OK;
1336  }
1337 
1338  if (pin_protocol != TPIU_PIN_PROTOCOL_ASYNC_UART) {
1339  LOG_ERROR("Selected pin protocol is not supported");
1340  return ERROR_FAIL;
1341  }
1342 
1343  buffer_size = calculate_trace_buffer_size();
1344 
1345  if (!buffer_size) {
1346  LOG_ERROR("Not enough free device memory to start trace capturing");
1347  return ERROR_FAIL;
1348  }
1349 
1350  ret = jaylink_swo_get_speeds(devh, JAYLINK_SWO_MODE_UART, &speed);
1351 
1352  if (ret != JAYLINK_OK) {
1353  LOG_ERROR("jaylink_swo_get_speeds() failed: %s",
1354  jaylink_strerror(ret));
1355  return ERROR_FAIL;
1356  }
1357 
1358  if (*trace_freq > 0) {
1359  divider = speed.freq / *trace_freq;
1360  min_freq = speed.freq / speed.max_div;
1361  max_freq = speed.freq / speed.min_div;
1362 
1363  if (*trace_freq > max_freq) {
1364  LOG_INFO("Given SWO frequency too high, using %" PRIu32 " Hz instead",
1365  max_freq);
1366  *trace_freq = max_freq;
1367  } else if (*trace_freq < min_freq) {
1368  LOG_INFO("Given SWO frequency too low, using %" PRIu32 " Hz instead",
1369  min_freq);
1370  *trace_freq = min_freq;
1371  } else if (*trace_freq != speed.freq / divider) {
1372  *trace_freq = speed.freq / divider;
1373 
1374  LOG_INFO("Given SWO frequency is not supported by the device, "
1375  "using %u Hz instead", *trace_freq);
1376  }
1377 
1378  if (!calculate_swo_prescaler(traceclkin_freq, *trace_freq,
1379  prescaler)) {
1380  LOG_ERROR("SWO frequency is not suitable. Please choose a "
1381  "different frequency or use auto-detection");
1382  return ERROR_FAIL;
1383  }
1384  } else {
1385  LOG_INFO("Trying to auto-detect SWO frequency");
1386 
1387  if (!detect_swo_freq_and_prescaler(speed, traceclkin_freq, trace_freq,
1388  prescaler)) {
1389  LOG_ERROR("Maximum permitted frequency deviation of %.02f %% "
1390  "could not be achieved", SWO_MAX_FREQ_DEV);
1391  LOG_ERROR("Auto-detection of SWO frequency failed");
1392  return ERROR_FAIL;
1393  }
1394 
1395  LOG_INFO("Using SWO frequency of %u Hz", *trace_freq);
1396  }
1397 
1398  ret = jaylink_swo_start(devh, JAYLINK_SWO_MODE_UART, *trace_freq,
1399  buffer_size);
1400 
1401  if (ret != JAYLINK_OK) {
1402  LOG_ERROR("jaylink_start_swo() failed: %s", jaylink_strerror(ret));
1403  return ERROR_FAIL;
1404  }
1405 
1406  LOG_DEBUG("Using %" PRIu32 " bytes device memory for trace capturing",
1407  buffer_size);
1408 
1409  /*
1410  * Adjust the SWD transaction buffer size as starting SWO capturing
1411  * allocates device internal memory.
1412  */
1413  if (!adjust_swd_buffer_size())
1414  return ERROR_FAIL;
1415 
1416  return ERROR_OK;
1417 }
1418 
1419 COMMAND_HANDLER(jlink_handle_config_usb_address_command)
1420 {
1421  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1422  command_print(CMD, "Reading configuration is not supported by the "
1423  "device");
1424  return ERROR_OK;
1425  }
1426 
1427  if (!CMD_ARGC) {
1429  } else if (CMD_ARGC == 1) {
1430  uint8_t tmp;
1431  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], tmp);
1432 
1433  if (tmp > JAYLINK_USB_ADDRESS_3) {
1434  command_print(CMD, "Invalid USB address: %u", tmp);
1436  }
1437 
1438  tmp_config.usb_address = tmp;
1439  } else {
1441  }
1442 
1443  return ERROR_OK;
1444 }
1445 
1446 COMMAND_HANDLER(jlink_handle_config_target_power_command)
1447 {
1448  int enable;
1449 
1450  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1451  command_print(CMD, "Reading configuration is not supported by the "
1452  "device");
1453  return ERROR_OK;
1454  }
1455 
1456  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1457  command_print(CMD, "Target power supply is not supported by the "
1458  "device");
1459  return ERROR_OK;
1460  }
1461 
1462  if (!CMD_ARGC) {
1464  } else if (CMD_ARGC == 1) {
1465  if (!strcmp(CMD_ARGV[0], "on")) {
1466  enable = true;
1467  } else if (!strcmp(CMD_ARGV[0], "off")) {
1468  enable = false;
1469  } else {
1470  command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1472  }
1473 
1474  tmp_config.target_power = enable;
1475  } else {
1477  }
1478 
1479  return ERROR_OK;
1480 }
1481 
1482 COMMAND_HANDLER(jlink_handle_config_mac_address_command)
1483 {
1484  uint8_t addr[6];
1485  int i;
1486  char *e;
1487  const char *str;
1488 
1489  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1490  command_print(CMD, "Reading configuration is not supported by the "
1491  "device");
1492  return ERROR_OK;
1493  }
1494 
1495  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1496  command_print(CMD, "Ethernet connectivity is not supported by the "
1497  "device");
1498  return ERROR_OK;
1499  }
1500 
1501  if (!CMD_ARGC) {
1503  } else if (CMD_ARGC == 1) {
1504  str = CMD_ARGV[0];
1505 
1506  if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' ||
1507  str[8] != ':' || str[11] != ':' || str[14] != ':')) {
1508  command_print(CMD, "Invalid MAC address format");
1510  }
1511 
1512  for (i = 5; i >= 0; i--) {
1513  addr[i] = strtoul(str, &e, 16);
1514  str = e + 1;
1515  }
1516 
1517  if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) {
1518  command_print(CMD, "Invalid MAC address: zero address");
1520  }
1521 
1522  if (!(0x01 & addr[0])) {
1523  command_print(CMD, "Invalid MAC address: multicast address");
1525  }
1526 
1527  memcpy(tmp_config.mac_address, addr, sizeof(addr));
1528  } else {
1530  }
1531 
1532  return ERROR_OK;
1533 }
1534 
1535 static bool string_to_ip(const char *s, uint8_t *ip, int *pos)
1536 {
1537  uint8_t lip[4];
1538  char *e;
1539  const char *s_save = s;
1540  int i;
1541 
1542  if (!s)
1543  return false;
1544 
1545  for (i = 0; i < 4; i++) {
1546  lip[i] = strtoul(s, &e, 10);
1547 
1548  if (*e != '.' && i != 3)
1549  return false;
1550 
1551  s = e + 1;
1552  }
1553 
1554  *pos = e - s_save;
1555  memcpy(ip, lip, sizeof(lip));
1556 
1557  return true;
1558 }
1559 
1560 static void cpy_ip(uint8_t *dst, uint8_t *src)
1561 {
1562  int i, j;
1563 
1564  for (i = 0, j = 3; i < 4; i++, j--)
1565  dst[i] = src[j];
1566 }
1567 
1568 COMMAND_HANDLER(jlink_handle_config_ip_address_command)
1569 {
1570  uint8_t ip_address[4];
1571  uint32_t subnet_mask = 0;
1572  int i, len;
1573  uint8_t subnet_bits = 24;
1574 
1575  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1576  command_print(CMD, "Reading configuration is not supported by the "
1577  "device");
1578  return ERROR_OK;
1579  }
1580 
1581  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1582  command_print(CMD, "Ethernet connectivity is not supported by the "
1583  "device");
1584  return ERROR_OK;
1585  }
1586 
1587  if (!CMD_ARGC) {
1589  } else {
1590  if (!string_to_ip(CMD_ARGV[0], ip_address, &i)) {
1591  command_print(CMD, "invalid IPv4 address");
1593  }
1594 
1595  len = strlen(CMD_ARGV[0]);
1596 
1597  /* Check for format A.B.C.D/E. */
1598  if (i < len) {
1599  if (CMD_ARGV[0][i] != '/') {
1600  command_print(CMD, "missing network mask");
1602  }
1603 
1604  COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
1605  } else if (CMD_ARGC > 1) {
1606  if (!string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i)) {
1607  command_print(CMD, "invalid subnet mask");
1609  }
1610  }
1611 
1612  if (!subnet_mask)
1613  subnet_mask = (uint32_t)(subnet_bits < 32 ?
1614  ((1ULL << subnet_bits) - 1) : 0xffffffff);
1615 
1617  cpy_ip(tmp_config.subnet_mask, (uint8_t *)&subnet_mask);
1618  }
1619 
1620  return ERROR_OK;
1621 }
1622 
1623 COMMAND_HANDLER(jlink_handle_config_reset_command)
1624 {
1625  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG))
1626  return ERROR_OK;
1627 
1628  memcpy(&tmp_config, &config, sizeof(struct device_config));
1629 
1630  return ERROR_OK;
1631 }
1632 
1633 
1634 COMMAND_HANDLER(jlink_handle_config_write_command)
1635 {
1636  int ret;
1637 
1638  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1639  command_print(CMD, "Reading configuration is not supported by the "
1640  "device");
1641  return ERROR_OK;
1642  }
1643 
1644  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_WRITE_CONFIG)) {
1645  command_print(CMD, "Writing configuration is not supported by the "
1646  "device");
1647  return ERROR_OK;
1648  }
1649 
1650  if (!memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1651  command_print(CMD, "Operation not performed due to no changes in "
1652  "the configuration");
1653  return ERROR_OK;
1654  }
1655 
1656  ret = jaylink_write_raw_config(devh, (const uint8_t *)&tmp_config);
1657 
1658  if (ret != JAYLINK_OK) {
1659  LOG_ERROR("jaylink_write_raw_config() failed: %s",
1660  jaylink_strerror(ret));
1661  return ERROR_FAIL;
1662  }
1663 
1664  if (!read_device_config(&config)) {
1665  LOG_ERROR("Failed to read device configuration for verification");
1666  return ERROR_FAIL;
1667  }
1668 
1669  if (memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1670  LOG_ERROR("Verification of device configuration failed. Please check "
1671  "your device");
1672  return ERROR_FAIL;
1673  }
1674 
1675  memcpy(&tmp_config, &config, sizeof(struct device_config));
1676  command_print(CMD, "The new device configuration applies after power "
1677  "cycling the J-Link device");
1678 
1679  return ERROR_OK;
1680 }
1681 
1682 COMMAND_HANDLER(jlink_handle_config_command)
1683 {
1684  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1685  command_print(CMD, "Device doesn't support reading configuration");
1686  return ERROR_OK;
1687  }
1688 
1689  if (CMD_ARGC == 0)
1690  show_config(CMD);
1691 
1692  return ERROR_OK;
1693 }
1694 
1695 COMMAND_HANDLER(jlink_handle_emucom_write_command)
1696 {
1697  int ret;
1698  size_t tmp;
1699  uint32_t channel;
1700  uint32_t length;
1701  uint8_t *buf;
1702  size_t dummy;
1703 
1704  if (CMD_ARGC != 2)
1706 
1707  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1708  LOG_ERROR("Device does not support EMUCOM");
1709  return ERROR_FAIL;
1710  }
1711 
1712  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1713 
1714  tmp = strlen(CMD_ARGV[1]);
1715 
1716  if (tmp % 2 != 0) {
1717  LOG_ERROR("Data must be encoded as hexadecimal pairs");
1719  }
1720 
1721  buf = malloc(tmp / 2);
1722 
1723  if (!buf) {
1724  LOG_ERROR("Failed to allocate buffer");
1725  return ERROR_FAIL;
1726  }
1727 
1728  dummy = unhexify(buf, CMD_ARGV[1], tmp / 2);
1729 
1730  if (dummy != (tmp / 2)) {
1731  LOG_ERROR("Data must be encoded as hexadecimal pairs");
1732  free(buf);
1734  }
1735 
1736  length = tmp / 2;
1737  ret = jaylink_emucom_write(devh, channel, buf, &length);
1738 
1739  free(buf);
1740 
1741  if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1742  LOG_ERROR("Channel not supported by the device");
1743  return ERROR_FAIL;
1744  } else if (ret != JAYLINK_OK) {
1745  LOG_ERROR("Failed to write to channel: %s", jaylink_strerror(ret));
1746  return ERROR_FAIL;
1747  }
1748 
1749  if (length != (tmp / 2))
1750  LOG_WARNING("Only %" PRIu32 " bytes written to the channel", length);
1751 
1752  return ERROR_OK;
1753 }
1754 
1755 COMMAND_HANDLER(jlink_handle_emucom_read_command)
1756 {
1757  int ret;
1758  uint32_t channel;
1759  uint32_t length;
1760  uint8_t *buf;
1761  size_t tmp;
1762 
1763  if (CMD_ARGC != 2)
1765 
1766  if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1767  LOG_ERROR("Device does not support EMUCOM");
1768  return ERROR_FAIL;
1769  }
1770 
1771  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1773 
1774  buf = malloc(length * 3 + 1);
1775 
1776  if (!buf) {
1777  LOG_ERROR("Failed to allocate buffer");
1778  return ERROR_FAIL;
1779  }
1780 
1781  ret = jaylink_emucom_read(devh, channel, buf, &length);
1782 
1783  if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1784  LOG_ERROR("Channel is not supported by the device");
1785  free(buf);
1786  return ERROR_FAIL;
1787  } else if (ret == JAYLINK_ERR_DEV_NOT_AVAILABLE) {
1788  LOG_ERROR("Channel is not available for the requested amount of data. "
1789  "%" PRIu32 " bytes are available", length);
1790  free(buf);
1791  return ERROR_FAIL;
1792  } else if (ret != JAYLINK_OK) {
1793  LOG_ERROR("Failed to read from channel: %s", jaylink_strerror(ret));
1794  free(buf);
1795  return ERROR_FAIL;
1796  }
1797 
1798  tmp = hexify((char *)buf + length, buf, length, 2 * length + 1);
1799 
1800  if (tmp != 2 * length) {
1801  LOG_ERROR("Failed to convert data into hexadecimal string");
1802  free(buf);
1803  return ERROR_FAIL;
1804  }
1805 
1806  command_print(CMD, "%s", buf + length);
1807  free(buf);
1808 
1809  return ERROR_OK;
1810 }
1811 
1813  {
1814  .name = "usb",
1815  .handler = &jlink_handle_config_usb_address_command,
1816  .mode = COMMAND_EXEC,
1817  .help = "set the USB address",
1818  .usage = "[0-3]",
1819  },
1820  {
1821  .name = "targetpower",
1822  .handler = &jlink_handle_config_target_power_command,
1823  .mode = COMMAND_EXEC,
1824  .help = "set the target power supply",
1825  .usage = "[on|off]"
1826  },
1827  {
1828  .name = "mac",
1829  .handler = &jlink_handle_config_mac_address_command,
1830  .mode = COMMAND_EXEC,
1831  .help = "set the MAC Address",
1832  .usage = "[ff:ff:ff:ff:ff:ff]",
1833  },
1834  {
1835  .name = "ip",
1836  .handler = &jlink_handle_config_ip_address_command,
1837  .mode = COMMAND_EXEC,
1838  .help = "set the IP address, where A.B.C.D is the IP address, "
1839  "E the bit of the subnet mask, F.G.H.I the subnet mask",
1840  .usage = "[A.B.C.D[/E] [F.G.H.I]]",
1841  },
1842  {
1843  .name = "reset",
1844  .handler = &jlink_handle_config_reset_command,
1845  .mode = COMMAND_EXEC,
1846  .help = "undo configuration changes",
1847  .usage = "",
1848  },
1849  {
1850  .name = "write",
1851  .handler = &jlink_handle_config_write_command,
1852  .mode = COMMAND_EXEC,
1853  .help = "write configuration to the device",
1854  .usage = "",
1855  },
1857 };
1858 
1860  {
1861  .name = "write",
1862  .handler = &jlink_handle_emucom_write_command,
1863  .mode = COMMAND_EXEC,
1864  .help = "write to a channel",
1865  .usage = "<channel> <data>",
1866  },
1867  {
1868  .name = "read",
1869  .handler = &jlink_handle_emucom_read_command,
1870  .mode = COMMAND_EXEC,
1871  .help = "read from a channel",
1872  .usage = "<channel> <length>"
1873  },
1875 };
1876 
1877 static const struct command_registration jlink_subcommand_handlers[] = {
1878  {
1879  .name = "jtag",
1880  .handler = &jlink_handle_jlink_jtag_command,
1881  .mode = COMMAND_EXEC,
1882  .help = "select the JTAG command version",
1883  .usage = "[2|3]",
1884  },
1885  {
1886  .name = "targetpower",
1887  .handler = &jlink_handle_target_power_command,
1888  .mode = COMMAND_EXEC,
1889  .help = "set the target power supply",
1890  .usage = "[0|1|on|off]"
1891  },
1892  {
1893  .name = "freemem",
1894  .handler = &jlink_handle_free_memory_command,
1895  .mode = COMMAND_EXEC,
1896  .help = "show free device memory",
1897  .usage = "",
1898  },
1899  {
1900  .name = "hwstatus",
1901  .handler = &jlink_handle_hwstatus_command,
1902  .mode = COMMAND_EXEC,
1903  .help = "show the hardware status",
1904  .usage = "",
1905  },
1906  {
1907  .name = "usb",
1908  .handler = &jlink_usb_command,
1909  .mode = COMMAND_CONFIG,
1910  .help = "set the USB address of the device that should be used",
1911  .usage = "<0-3>"
1912  },
1913  {
1914  .name = "config",
1915  .handler = &jlink_handle_config_command,
1916  .mode = COMMAND_EXEC,
1917  .help = "access the device configuration. If no argument is given "
1918  "this will show the device configuration",
1920  .usage = "[<cmd>]",
1921  },
1922  {
1923  .name = "emucom",
1924  .mode = COMMAND_EXEC,
1925  .help = "access EMUCOM channel",
1927  .usage = "",
1928  },
1930 };
1931 
1932 static const struct command_registration jlink_command_handlers[] = {
1933  {
1934  .name = "jlink",
1935  .mode = COMMAND_ANY,
1936  .help = "perform jlink management",
1937  .chain = jlink_subcommand_handlers,
1938  .usage = "",
1939  },
1941 };
1942 
1943 static int jlink_swd_init(void)
1944 {
1945  iface = JAYLINK_TIF_SWD;
1946 
1947  return ERROR_OK;
1948 }
1949 
1950 static void jlink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1951 {
1952  assert(!(cmd & SWD_CMD_RNW));
1953  jlink_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
1954 }
1955 
1956 static void jlink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1957 {
1958  assert(cmd & SWD_CMD_RNW);
1959  jlink_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
1960 }
1961 
1962 /***************************************************************************/
1963 /* J-Link tap functions */
1964 
1965 static unsigned int tap_length;
1966 /* In SWD mode use tms buffer for direction control */
1970 
1971 struct pending_scan_result {
1973  unsigned int first;
1975  unsigned int length;
1977  void *buffer;
1979  unsigned int buffer_offset;
1981  uint8_t swd_cmd;
1982 };
1983 
1984 #define MAX_PENDING_SCAN_RESULTS 256
1985 
1988 
1989 static void jlink_tap_init(void)
1990 {
1991  tap_length = 0;
1993  memset(tms_buffer, 0, sizeof(tms_buffer));
1994  memset(tdi_buffer, 0, sizeof(tdi_buffer));
1995 }
1996 
1997 static void jlink_clock_data(const uint8_t *out, unsigned int out_offset,
1998  const uint8_t *tms_out, unsigned int tms_offset,
1999  uint8_t *in, unsigned int in_offset,
2000  unsigned int length)
2001 {
2002  do {
2003  unsigned int available_length = JLINK_TAP_BUFFER_SIZE - tap_length / 8;
2004 
2005  if (!available_length ||
2007  if (jlink_flush() != ERROR_OK)
2008  return;
2009  available_length = JLINK_TAP_BUFFER_SIZE;
2010  }
2011 
2014 
2015  unsigned int scan_length = length > available_length ?
2016  available_length : length;
2017 
2018  if (out)
2019  buf_set_buf(out, out_offset, tdi_buffer, tap_length, scan_length);
2020  if (tms_out)
2021  buf_set_buf(tms_out, tms_offset, tms_buffer, tap_length, scan_length);
2022 
2023  if (in) {
2025  pending_scan_result->length = scan_length;
2027  pending_scan_result->buffer_offset = in_offset;
2029  }
2030 
2031  tap_length += scan_length;
2032  out_offset += scan_length;
2033  tms_offset += scan_length;
2034  in_offset += scan_length;
2035  length -= scan_length;
2036  } while (length > 0);
2037 }
2038 
2039 static int jlink_flush(void)
2040 {
2041  int i;
2042  int ret;
2043 
2044  if (!tap_length)
2045  return ERROR_OK;
2046 
2049 
2050  ret = jaylink_jtag_io(devh, tms_buffer, tdi_buffer, tdo_buffer,
2052 
2053  if (ret != JAYLINK_OK) {
2054  LOG_ERROR("jaylink_jtag_io() failed: %s", jaylink_strerror(ret));
2055  jlink_tap_init();
2056  return ERROR_JTAG_QUEUE_FAILED;
2057  }
2058 
2059  for (i = 0; i < pending_scan_results_length; i++) {
2061 
2063  p->buffer_offset, p->length);
2064 
2065  LOG_DEBUG_IO("Pending scan result, length = %d", p->length);
2066  }
2067 
2068  jlink_tap_init();
2069 
2070  return ERROR_OK;
2071 }
2072 
2073 static void fill_buffer(uint8_t *buf, uint32_t val, uint32_t len)
2074 {
2075  unsigned int tap_pos = tap_length;
2076 
2077  while (len > 32) {
2078  buf_set_u32(buf, tap_pos, 32, val);
2079  len -= 32;
2080  tap_pos += 32;
2081  }
2082 
2083  if (len)
2084  buf_set_u32(buf, tap_pos, len, val);
2085 }
2086 
2087 static void jlink_queue_data_out(const uint8_t *data, uint32_t len)
2088 {
2089  const uint32_t dir_out = 0xffffffff;
2090 
2091  if (data)
2092  bit_copy(tdi_buffer, tap_length, data, 0, len);
2093  else
2094  fill_buffer(tdi_buffer, 0, len);
2095 
2096  fill_buffer(tms_buffer, dir_out, len);
2097  tap_length += len;
2098 }
2099 
2100 static void jlink_queue_data_in(uint32_t len)
2101 {
2102  const uint32_t dir_in = 0;
2103 
2104  fill_buffer(tms_buffer, dir_in, len);
2105  tap_length += len;
2106 }
2107 
2109 {
2110  const uint8_t *s;
2111  unsigned int s_len;
2112 
2113  switch (seq) {
2114  case LINE_RESET:
2115  LOG_DEBUG_IO("SWD line reset");
2116  s = swd_seq_line_reset;
2117  s_len = swd_seq_line_reset_len;
2118  break;
2119  case JTAG_TO_SWD:
2120  LOG_DEBUG("JTAG-to-SWD");
2121  s = swd_seq_jtag_to_swd;
2122  s_len = swd_seq_jtag_to_swd_len;
2123  break;
2124  case JTAG_TO_DORMANT:
2125  LOG_DEBUG("JTAG-to-DORMANT");
2128  break;
2129  case SWD_TO_JTAG:
2130  LOG_DEBUG("SWD-to-JTAG");
2131  s = swd_seq_swd_to_jtag;
2132  s_len = swd_seq_swd_to_jtag_len;
2133  break;
2134  case SWD_TO_DORMANT:
2135  LOG_DEBUG("SWD-to-DORMANT");
2138  break;
2139  case DORMANT_TO_SWD:
2140  LOG_DEBUG("DORMANT-to-SWD");
2143  break;
2144  case DORMANT_TO_JTAG:
2145  LOG_DEBUG("DORMANT-to-JTAG");
2148  break;
2149  default:
2150  LOG_ERROR("Sequence %d not supported", seq);
2151  return ERROR_FAIL;
2152  }
2153 
2154  jlink_queue_data_out(s, s_len);
2155 
2156  return ERROR_OK;
2157 }
2158 
2159 static int jlink_swd_run_queue(void)
2160 {
2161  int i;
2162  int ret;
2163 
2164  LOG_DEBUG_IO("Executing %d queued transactions", pending_scan_results_length);
2165 
2166  if (queued_retval != ERROR_OK) {
2167  LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
2168  goto skip;
2169  }
2170 
2171  /*
2172  * A transaction must be followed by another transaction or at least 8 idle
2173  * cycles to ensure that data is clocked through the AP.
2174  */
2176 
2177  ret = jaylink_swd_io(devh, tms_buffer, tdi_buffer, tdo_buffer, tap_length);
2178 
2179  if (ret != JAYLINK_OK) {
2180  LOG_ERROR("jaylink_swd_io() failed: %s", jaylink_strerror(ret));
2181  goto skip;
2182  }
2183 
2184  for (i = 0; i < pending_scan_results_length; i++) {
2185  /* Devices do not reply to DP_TARGETSEL write cmd, ignore received ack */
2188  if (check_ack && ack != SWD_ACK_OK) {
2189  LOG_DEBUG("SWD ack not OK: %d %s", ack,
2190  ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
2192  goto skip;
2193  } else if (pending_scan_results_buffer[i].length) {
2194  uint32_t data = buf_get_u32(tdo_buffer, 3 + pending_scan_results_buffer[i].first, 32);
2196 
2197  if (parity != parity_u32(data)) {
2198  LOG_ERROR("SWD: Read data parity mismatch");
2200  goto skip;
2201  }
2202 
2204  *(uint32_t *)pending_scan_results_buffer[i].buffer = data;
2205  }
2206  }
2207 
2208 skip:
2209  jlink_tap_init();
2210  ret = queued_retval;
2212 
2213  return ret;
2214 }
2215 
2216 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
2217 {
2218  uint8_t data_parity_trn[DIV_ROUND_UP(32 + 1, 8)];
2219  if (tap_length + 46 + 8 + ap_delay_clk >= swd_buffer_size * 8 ||
2221  /* Not enough room in the queue. Run the queue. */
2223  }
2224 
2225  if (queued_retval != ERROR_OK)
2226  return;
2227 
2230 
2232 
2234 
2235  if (cmd & SWD_CMD_RNW) {
2236  /* Queue a read transaction. */
2239 
2240  jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2241  } else {
2242  /* Queue a write transaction. */
2244  jlink_queue_data_in(1 + 3 + 1);
2245 
2246  buf_set_u32(data_parity_trn, 0, 32, data);
2247  buf_set_u32(data_parity_trn, 32, 1, parity_u32(data));
2248 
2249  jlink_queue_data_out(data_parity_trn, 32 + 1);
2250  }
2251 
2253 
2254  /* Insert idle cycles after AP accesses to avoid WAIT. */
2255  if (cmd & SWD_CMD_APNDP)
2256  jlink_queue_data_out(NULL, ap_delay_clk);
2257 }
2258 
2259 static const struct swd_driver jlink_swd = {
2260  .init = &jlink_swd_init,
2261  .switch_seq = &jlink_swd_switch_seq,
2262  .read_reg = &jlink_swd_read_reg,
2263  .write_reg = &jlink_swd_write_reg,
2264  .run = &jlink_swd_run_queue,
2265 };
2266 
2267 static const char * const jlink_transports[] = { "jtag", "swd", NULL };
2268 
2269 static struct jtag_interface jlink_interface = {
2271 };
2272 
2274  .name = "jlink",
2275  .transports = jlink_transports,
2276  .commands = jlink_command_handlers,
2277 
2278  .init = &jlink_init,
2279  .quit = &jlink_quit,
2280  .reset = &jlink_reset_safe,
2281  .speed = &jlink_speed,
2282  .khz = &jlink_khz,
2283  .speed_div = &jlink_speed_div,
2284  .config_trace = &config_trace,
2285  .poll_trace = &poll_trace,
2286 
2287  .jtag_ops = &jlink_interface,
2288  .swd_ops = &jlink_swd,
2289 };
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Definition: adapter.c:301
bool adapter_usb_location_equal(uint8_t dev_bus, uint8_t *port_path, size_t path_len)
Definition: adapter.c:332
unsigned int adapter_get_speed_khz(void)
Retrieves the clock speed of the adapter in kHz.
Definition: adapter.c:209
const char * adapter_usb_get_location(void)
Definition: adapter.c:327
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
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: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 COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:530
#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:126
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
#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
static tap_state_t jtag_debug_state_machine(const void *tms_buf, const void *tdi_buf, unsigned int tap_len, tap_state_t start_tap_state)
Prints verbose TAP state transitions for the given TMS/TDI buffers.
Definition: interface.h:161
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1068
#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
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
#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: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
uint8_t swd_cmd
SWD command.
Definition: jlink.c:1981
int first
First bit position in tdo_buffer to read.
Definition: arm-jtag-ew.c:513
uint8_t * ack
Definition: vsllink.c:32
void * buffer
Location to store the result.
Definition: jlink.c:1977
int length
Number of bits to read.
Definition: arm-jtag-ew.c:514
uint8_t * buffer
Location to store the result.
Definition: arm-jtag-ew.c:516
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 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