1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
5 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
7 * Copyright (C) 2008 by Spencer Oliver *
8 * spen@spen-soft.co.uk *
10 * Copyright (C) 2011 by Jean-Christophe PLAGNIOL-VIILARD *
11 * plagnioj@jcrosoft.com *
13 * Copyright (C) 2015 by Marc Schink *
14 * openocd-dev@marcschink.de *
16 * Copyright (C) 2015 by Paul Fertser *
17 * fercerpav@gmail.com *
18 ***************************************************************************/
27 #include <jtag/interface.h>
29 #include <jtag/commands.h>
30 #include <jtag/adapter.h>
31 #include <helper/replacements.h>
32 #include <target/cortex_m.h>
34 #include <libjaylink/libjaylink.h>
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
];
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
;
51 #define JLINK_MAX_SPEED 12000
52 #define JLINK_TAP_BUFFER_SIZE 2048
54 static unsigned int swd_buffer_size
= JLINK_TAP_BUFFER_SIZE
;
56 /* Maximum SWO frequency deviation. */
57 #define SWO_MAX_FREQ_DEV 0.03
59 /* 256 byte non-volatile memory */
60 struct device_config
{
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
));
77 static struct device_config config
;
78 static struct device_config tmp_config
;
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
);
92 /* J-Link tap buffer functions */
93 static void jlink_tap_init(void);
94 static int jlink_flush(void);
96 * Queue data to go out and in, flushing the queue as many times as
99 * @param out A pointer to TDI data, if NULL, old stale data will be used.
100 * @param out_offset A bit offset for TDI data.
101 * @param tms_out A pointer to TMS data, if NULL, zeroes will be emitted.
102 * @param tms_offset A bit offset for TMS data.
103 * @param in A pointer to store TDO data to, if NULL the data will be discarded.
104 * @param in_offset A bit offset for TDO data.
105 * @param length Amount of bits to transfer out and in.
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
,
112 static enum tap_state jlink_last_state
= TAP_RESET
;
113 static int queued_retval
;
115 /***************************************************************************/
116 /* External interface implementation */
118 static void jlink_execute_stableclocks(struct jtag_command
*cmd
)
120 LOG_DEBUG_IO("stableclocks %i cycles", cmd
->cmd
.runtest
->num_cycles
);
121 jlink_stableclocks(cmd
->cmd
.runtest
->num_cycles
);
124 static void jlink_execute_runtest(struct jtag_command
*cmd
)
126 LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd
->cmd
.runtest
->num_cycles
,
127 cmd
->cmd
.runtest
->end_state
);
129 jlink_end_state(cmd
->cmd
.runtest
->end_state
);
130 jlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
133 static void jlink_execute_statemove(struct jtag_command
*cmd
)
135 LOG_DEBUG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
137 jlink_end_state(cmd
->cmd
.statemove
->end_state
);
141 static void jlink_execute_pathmove(struct jtag_command
*cmd
)
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]);
147 jlink_path_move(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
150 static void jlink_execute_scan(struct jtag_command
*cmd
)
152 LOG_DEBUG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN",
153 jtag_scan_type(cmd
->cmd
.scan
));
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");
162 if (cmd
->cmd
.scan
->num_fields
== 0) {
163 LOG_DEBUG("empty scan, doing nothing");
167 if (cmd
->cmd
.scan
->ir_scan
) {
168 if (tap_get_state() != TAP_IRSHIFT
) {
169 jlink_end_state(TAP_IRSHIFT
);
173 if (tap_get_state() != TAP_DRSHIFT
) {
174 jlink_end_state(TAP_DRSHIFT
);
179 jlink_end_state(cmd
->cmd
.scan
->end_state
);
181 struct scan_field
*field
= cmd
->cmd
.scan
->fields
;
182 unsigned scan_size
= 0;
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" : "",
190 cmd
->cmd
.scan
->num_fields
,
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. */
196 jlink_clock_data(field
->out_value
,
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
,
214 tap_set_state(tap_state_transition(tap_get_state(), 1));
215 jlink_clock_data(NULL
,
222 tap_set_state(tap_state_transition(tap_get_state(), 0));
224 jlink_clock_data(field
->out_value
,
233 if (tap_get_state() != tap_get_end_state()) {
234 jlink_end_state(tap_get_end_state());
238 LOG_DEBUG_IO("%s scan, %i bits, end in %s",
239 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
240 tap_state_name(tap_get_end_state()));
243 static void jlink_execute_sleep(struct jtag_command
*cmd
)
245 LOG_DEBUG_IO("sleep %" PRIu32
"", cmd
->cmd
.sleep
->us
);
247 jtag_sleep(cmd
->cmd
.sleep
->us
);
250 static int jlink_execute_command(struct jtag_command
*cmd
)
253 case JTAG_STABLECLOCKS
:
254 jlink_execute_stableclocks(cmd
);
257 jlink_execute_runtest(cmd
);
260 jlink_execute_statemove(cmd
);
263 jlink_execute_pathmove(cmd
);
266 jlink_execute_scan(cmd
);
269 jlink_execute_sleep(cmd
);
272 LOG_ERROR("BUG: Unknown JTAG command type encountered");
273 return ERROR_JTAG_QUEUE_FAILED
;
279 static int jlink_execute_queue(void)
282 struct jtag_command
*cmd
= jtag_command_queue
;
285 ret
= jlink_execute_command(cmd
);
293 return jlink_flush();
296 static int jlink_speed(int speed
)
299 struct jaylink_speed tmp
;
302 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_SPEEDS
)) {
303 ret
= jaylink_get_speeds(devh
, &tmp
);
305 if (ret
!= JAYLINK_OK
) {
306 LOG_ERROR("jaylink_get_speeds() failed: %s",
307 jaylink_strerror(ret
));
308 return ERROR_JTAG_DEVICE_ERROR
;
312 max_speed
= tmp
.freq
/ tmp
.div
;
314 max_speed
= JLINK_MAX_SPEED
;
318 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING
)) {
319 LOG_ERROR("Adaptive clocking is not supported by the device");
320 return ERROR_JTAG_NOT_IMPLEMENTED
;
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
,
330 ret
= jaylink_set_speed(devh
, speed
);
332 if (ret
!= JAYLINK_OK
) {
333 LOG_ERROR("jaylink_set_speed() failed: %s",
334 jaylink_strerror(ret
));
335 return ERROR_JTAG_DEVICE_ERROR
;
341 static int jlink_speed_div(int speed
, int *khz
)
348 static int jlink_khz(int khz
, int *jtag_speed
)
355 static bool read_device_config(struct device_config
*cfg
)
359 ret
= jaylink_read_raw_config(devh
, (uint8_t *)cfg
);
361 if (ret
!= JAYLINK_OK
) {
362 LOG_ERROR("jaylink_read_raw_config() failed: %s",
363 jaylink_strerror(ret
));
367 if (cfg
->usb_address
== 0xff)
368 cfg
->usb_address
= 0x00;
370 if (cfg
->target_power
== 0xffffffff)
371 cfg
->target_power
= 0;
376 static int select_interface(void)
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
;
390 ret
= jaylink_get_available_interfaces(devh
, &interfaces
);
392 if (ret
!= JAYLINK_OK
) {
393 LOG_ERROR("jaylink_get_available_interfaces() failed: %s",
394 jaylink_strerror(ret
));
395 return ERROR_JTAG_INIT_FAILED
;
398 if (!(interfaces
& (1 << iface
))) {
399 LOG_ERROR("Selected transport is not supported by the device");
400 return ERROR_JTAG_INIT_FAILED
;
403 ret
= jaylink_select_interface(devh
, iface
, NULL
);
406 LOG_ERROR("jaylink_select_interface() failed: %s",
407 jaylink_strerror(ret
));
408 return ERROR_JTAG_INIT_FAILED
;
414 static int jlink_register(void)
421 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_REGISTER
))
424 ret
= jaylink_register(devh
, &conn
, connlist
, &count
);
426 if (ret
!= JAYLINK_OK
) {
427 LOG_ERROR("jaylink_register() failed: %s", jaylink_strerror(ret
));
431 handle_found
= false;
433 for (i
= 0; i
< count
; i
++) {
434 if (connlist
[i
].handle
== conn
.handle
) {
441 LOG_ERROR("Registration failed: maximum number of connections on the "
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.
454 static bool adjust_swd_buffer_size(void)
459 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
))
462 ret
= jaylink_get_free_memory(devh
, &tmp
);
464 if (ret
!= JAYLINK_OK
) {
465 LOG_ERROR("jaylink_get_free_memory() failed: %s",
466 jaylink_strerror(ret
));
471 LOG_ERROR("Not enough free device internal memory: %" PRIu32
" bytes", tmp
);
475 tmp
= MIN(JLINK_TAP_BUFFER_SIZE
, (tmp
- 16) / 2);
477 if (tmp
!= swd_buffer_size
) {
478 swd_buffer_size
= tmp
;
479 LOG_DEBUG("Adjusted SWD transaction buffer size to %u bytes",
486 static int jaylink_log_handler(const struct jaylink_context
*ctx
,
487 enum jaylink_log_level level
, const char *format
, va_list args
,
493 case JAYLINK_LOG_LEVEL_ERROR
:
496 case JAYLINK_LOG_LEVEL_WARNING
:
497 tmp
= LOG_LVL_WARNING
;
500 * Forward info messages to the debug output because they are more verbose
501 * than info messages of OpenOCD.
503 case JAYLINK_LOG_LEVEL_INFO
:
504 case JAYLINK_LOG_LEVEL_DEBUG
:
507 case JAYLINK_LOG_LEVEL_DEBUG_IO
:
508 tmp
= LOG_LVL_DEBUG_IO
;
511 tmp
= LOG_LVL_WARNING
;
514 log_vprintf_lf(tmp
, __FILE__
, __LINE__
, __func__
, format
, args
);
519 static bool jlink_usb_location_equal(struct jaylink_device
*dev
)
527 retval
= jaylink_device_get_usb_bus_ports(dev
, &bus
, &ports
, &num_ports
);
529 if (retval
== JAYLINK_ERR_NOT_SUPPORTED
) {
531 } else if (retval
!= JAYLINK_OK
) {
532 LOG_WARNING("jaylink_device_get_usb_bus_ports() failed: %s",
533 jaylink_strerror(retval
));
537 equal
= adapter_usb_location_equal(bus
, ports
, num_ports
);
544 static int jlink_open_device(uint32_t ifaces
, bool *found_device
)
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
;
554 struct jaylink_device
**devs
;
555 ret
= jaylink_get_devices(jayctx
, &devs
, &num_devices
);
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
;
563 use_usb_location
= !!adapter_usb_get_location();
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
;
572 *found_device
= false;
574 for (size_t i
= 0; devs
[i
]; i
++) {
575 struct jaylink_device
*dev
= devs
[i
];
577 if (use_serial_number
) {
579 ret
= jaylink_device_get_serial_number(dev
, &tmp
);
581 if (ret
== JAYLINK_ERR_NOT_AVAILABLE
) {
583 } else if (ret
!= JAYLINK_OK
) {
584 LOG_WARNING("jaylink_device_get_serial_number() failed: %s",
585 jaylink_strerror(ret
));
589 if (serial_number
!= tmp
)
593 if (use_usb_address
) {
594 enum jaylink_usb_address address
;
595 ret
= jaylink_device_get_usb_address(dev
, &address
);
597 if (ret
== JAYLINK_ERR_NOT_SUPPORTED
) {
599 } else if (ret
!= JAYLINK_OK
) {
600 LOG_WARNING("jaylink_device_get_usb_address() failed: %s",
601 jaylink_strerror(ret
));
605 if (usb_address
!= address
)
609 if (use_usb_location
&& !jlink_usb_location_equal(dev
))
612 ret
= jaylink_open(dev
, &devh
);
614 if (ret
== JAYLINK_OK
) {
615 *found_device
= true;
619 LOG_ERROR("Failed to open device: %s", jaylink_strerror(ret
));
622 jaylink_free_devices(devs
, true);
627 static int jlink_init(void)
630 char *firmware_version
;
631 struct jaylink_hardware_version hwver
;
632 struct jaylink_hardware_status hwstatus
;
635 LOG_DEBUG("Using libjaylink %s (compiled with %s)",
636 jaylink_version_package_get_string(), JAYLINK_VERSION_PACKAGE_STRING
);
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
;
643 ret
= jaylink_init(&jayctx
);
645 if (ret
!= JAYLINK_OK
) {
646 LOG_ERROR("jaylink_init() failed: %s", jaylink_strerror(ret
));
647 return ERROR_JTAG_INIT_FAILED
;
650 ret
= jaylink_log_set_callback(jayctx
, &jaylink_log_handler
, NULL
);
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
;
659 const char *serial
= adapter_get_required_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
;
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
;
672 use_serial_number
= true;
673 use_usb_address
= false;
677 ret
= jlink_open_device(JAYLINK_HIF_USB
, &found_device
);
681 if (!found_device
&& use_serial_number
) {
682 ret
= jlink_open_device(JAYLINK_HIF_TCP
, &found_device
);
688 LOG_ERROR("No J-Link device found");
689 jaylink_exit(jayctx
);
690 return ERROR_JTAG_INIT_FAILED
;
694 * Be careful with changing the following initialization sequence because
695 * some devices are known to be sensitive regarding the order.
698 ret
= jaylink_get_firmware_version(devh
, &firmware_version
, &length
);
700 if (ret
!= JAYLINK_OK
) {
701 LOG_ERROR("jaylink_get_firmware_version() failed: %s",
702 jaylink_strerror(ret
));
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
);
710 LOG_WARNING("Device responds empty firmware version string");
713 memset(caps
, 0, JAYLINK_DEV_EXT_CAPS_SIZE
);
714 ret
= jaylink_get_caps(devh
, caps
);
716 if (ret
!= JAYLINK_OK
) {
717 LOG_ERROR("jaylink_get_caps() failed: %s", jaylink_strerror(ret
));
719 jaylink_exit(jayctx
);
720 return ERROR_JTAG_INIT_FAILED
;
723 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_EXT_CAPS
)) {
724 ret
= jaylink_get_extended_caps(devh
, caps
);
726 if (ret
!= JAYLINK_OK
) {
727 LOG_ERROR("jaylink_get_extended_caps() failed: %s",
728 jaylink_strerror(ret
));
730 jaylink_exit(jayctx
);
731 return ERROR_JTAG_INIT_FAILED
;
735 jtag_command_version
= JAYLINK_JTAG_VERSION_2
;
737 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_HW_VERSION
)) {
738 ret
= jaylink_get_hardware_version(devh
, &hwver
);
740 if (ret
!= JAYLINK_OK
) {
741 LOG_ERROR("Failed to retrieve hardware version: %s",
742 jaylink_strerror(ret
));
744 jaylink_exit(jayctx
);
745 return ERROR_JTAG_INIT_FAILED
;
748 LOG_INFO("Hardware version: %u.%02u", hwver
.major
, hwver
.minor
);
750 if (hwver
.major
>= 5)
751 jtag_command_version
= JAYLINK_JTAG_VERSION_3
;
754 if (iface
== JAYLINK_TIF_SWD
) {
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.
761 if (!adjust_swd_buffer_size()) {
763 jaylink_exit(jayctx
);
764 return ERROR_JTAG_INIT_FAILED
;
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");
772 jaylink_exit(jayctx
);
773 return ERROR_JTAG_INIT_FAILED
;
776 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
779 ret
= jaylink_get_hardware_status(devh
, &hwstatus
);
781 if (ret
!= JAYLINK_OK
) {
782 LOG_ERROR("jaylink_get_hardware_status() failed: %s",
783 jaylink_strerror(ret
));
785 jaylink_exit(jayctx
);
786 return ERROR_JTAG_INIT_FAILED
;
789 LOG_INFO("VTarget = %u.%03u V", hwstatus
.target_voltage
/ 1000,
790 hwstatus
.target_voltage
% 1000);
794 strcpy(conn
.hid
, "0.0.0.0");
798 ret
= jlink_register();
800 if (ret
!= ERROR_OK
) {
802 jaylink_exit(jayctx
);
803 return ERROR_JTAG_INIT_FAILED
;
806 ret
= select_interface();
808 if (ret
!= ERROR_OK
) {
810 jaylink_exit(jayctx
);
818 jlink_speed(adapter_get_speed_khz());
820 if (iface
== JAYLINK_TIF_JTAG
) {
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
827 jlink_clock_data(NULL
, 0, &tms
, 0, NULL
, 0, 8);
835 static int jlink_quit(void)
841 ret
= jaylink_swo_stop(devh
);
843 if (ret
!= JAYLINK_OK
)
844 LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret
));
847 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_REGISTER
)) {
848 ret
= jaylink_unregister(devh
, &conn
, connlist
, &count
);
850 if (ret
!= JAYLINK_OK
)
851 LOG_ERROR("jaylink_unregister() failed: %s",
852 jaylink_strerror(ret
));
856 jaylink_exit(jayctx
);
861 /***************************************************************************/
862 /* Queue command implementations */
864 static void jlink_end_state(tap_state_t state
)
866 if (tap_is_state_stable(state
))
867 tap_set_end_state(state
);
869 LOG_ERROR("BUG: %i is not a valid end state", state
);
874 /* Goes to the end state. */
875 static void jlink_state_move(void)
878 uint8_t tms_scan_bits
;
880 tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
881 tms_scan_bits
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
883 jlink_clock_data(NULL
, 0, &tms_scan
, 0, NULL
, 0, tms_scan_bits
);
885 tap_set_state(tap_get_end_state());
888 static void jlink_path_move(int num_states
, tap_state_t
*path
)
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);
899 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
900 tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
904 tap_set_state(path
[i
]);
907 tap_set_end_state(tap_get_state());
910 static void jlink_stableclocks(int num_cycles
)
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);
920 static void jlink_runtest(int num_cycles
)
922 tap_state_t saved_end_state
= tap_get_end_state();
924 /* Only do a state_move when we're not already in IDLE. */
925 if (tap_get_state() != TAP_IDLE
) {
926 jlink_end_state(TAP_IDLE
);
931 jlink_stableclocks(num_cycles
);
933 /* Finish in end_state. */
934 jlink_end_state(saved_end_state
);
936 if (tap_get_state() != tap_get_end_state())
940 static void jlink_reset(int trst
, int srst
)
942 LOG_DEBUG("TRST: %i, SRST: %i", trst
, srst
);
944 /* Signals are active low. */
946 jaylink_set_reset(devh
);
949 jaylink_clear_reset(devh
);
952 jaylink_jtag_clear_trst(devh
);
955 jaylink_jtag_set_trst(devh
);
958 static int jlink_reset_safe(int trst
, int srst
)
961 jlink_reset(trst
, srst
);
962 return jlink_flush();
965 COMMAND_HANDLER(jlink_usb_command
)
970 return ERROR_COMMAND_SYNTAX_ERROR
;
972 if (sscanf(CMD_ARGV
[0], "%i", &tmp
) != 1) {
973 command_print(CMD
, "Invalid USB address: %s", CMD_ARGV
[0]);
974 return ERROR_COMMAND_ARGUMENT_INVALID
;
977 if (tmp
< JAYLINK_USB_ADDRESS_0
|| tmp
> JAYLINK_USB_ADDRESS_3
) {
978 command_print(CMD
, "Invalid USB address: %s", CMD_ARGV
[0]);
979 return ERROR_COMMAND_ARGUMENT_INVALID
;
984 use_usb_address
= true;
989 COMMAND_HANDLER(jlink_handle_hwstatus_command
)
992 struct jaylink_hardware_status status
;
994 ret
= jaylink_get_hardware_status(devh
, &status
);
996 if (ret
!= JAYLINK_OK
) {
997 command_print(CMD
, "jaylink_get_hardware_status() failed: %s",
998 jaylink_strerror(ret
));
1002 command_print(CMD
, "VTarget = %u.%03u V",
1003 status
.target_voltage
/ 1000, status
.target_voltage
% 1000);
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
);
1009 if (status
.target_voltage
< 1500)
1010 command_print(CMD
, "Target voltage too low. Check target power");
1015 COMMAND_HANDLER(jlink_handle_free_memory_command
)
1020 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
)) {
1021 command_print(CMD
, "Retrieval of free memory is not supported by "
1026 ret
= jaylink_get_free_memory(devh
, &tmp
);
1028 if (ret
!= JAYLINK_OK
) {
1029 command_print(CMD
, "jaylink_get_free_memory() failed: %s",
1030 jaylink_strerror(ret
));
1034 command_print(CMD
, "Device has %" PRIu32
" bytes of free memory", tmp
);
1039 COMMAND_HANDLER(jlink_handle_jlink_jtag_command
)
1045 switch (jtag_command_version
) {
1046 case JAYLINK_JTAG_VERSION_2
:
1049 case JAYLINK_JTAG_VERSION_3
:
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]);
1060 return ERROR_COMMAND_ARGUMENT_INVALID
;
1065 jtag_command_version
= JAYLINK_JTAG_VERSION_2
;
1068 jtag_command_version
= JAYLINK_JTAG_VERSION_3
;
1071 command_print(CMD
, "Invalid argument: %s", CMD_ARGV
[0]);
1072 return ERROR_COMMAND_ARGUMENT_INVALID
;
1075 return ERROR_COMMAND_SYNTAX_ERROR
;
1081 COMMAND_HANDLER(jlink_handle_target_power_command
)
1087 return ERROR_COMMAND_SYNTAX_ERROR
;
1089 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
)) {
1090 command_print(CMD
, "Target power supply is not supported by the "
1095 if (!strcmp(CMD_ARGV
[0], "on")) {
1097 } else if (!strcmp(CMD_ARGV
[0], "off")) {
1100 command_print(CMD
, "Invalid argument: %s", CMD_ARGV
[0]);
1104 ret
= jaylink_set_target_power(devh
, enable
);
1106 if (ret
!= JAYLINK_OK
) {
1107 command_print(CMD
, "jaylink_set_target_power() failed: %s",
1108 jaylink_strerror(ret
));
1115 static void show_config_usb_address(struct command_invocation
*cmd
)
1117 if (config
.usb_address
!= tmp_config
.usb_address
)
1118 command_print(cmd
, "USB address: %u [%u]", config
.usb_address
,
1119 tmp_config
.usb_address
);
1121 command_print(cmd
, "USB address: %u", config
.usb_address
);
1124 static void show_config_ip_address(struct command_invocation
*cmd
)
1126 if (!memcmp(config
.ip_address
, tmp_config
.ip_address
, 4))
1127 command_print(cmd
, "IP address: %d.%d.%d.%d",
1128 config
.ip_address
[3], config
.ip_address
[2],
1129 config
.ip_address
[1], config
.ip_address
[0]);
1131 command_print(cmd
, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1132 config
.ip_address
[3], config
.ip_address
[2],
1133 config
.ip_address
[1], config
.ip_address
[0],
1134 tmp_config
.ip_address
[3], tmp_config
.ip_address
[2],
1135 tmp_config
.ip_address
[1], tmp_config
.ip_address
[0]);
1137 if (!memcmp(config
.subnet_mask
, tmp_config
.subnet_mask
, 4))
1138 command_print(cmd
, "Subnet mask: %d.%d.%d.%d",
1139 config
.subnet_mask
[3], config
.subnet_mask
[2],
1140 config
.subnet_mask
[1], config
.subnet_mask
[0]);
1142 command_print(cmd
, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1143 config
.subnet_mask
[3], config
.subnet_mask
[2],
1144 config
.subnet_mask
[1], config
.subnet_mask
[0],
1145 tmp_config
.subnet_mask
[3], tmp_config
.subnet_mask
[2],
1146 tmp_config
.subnet_mask
[1], tmp_config
.subnet_mask
[0]);
1149 static void show_config_mac_address(struct command_invocation
*cmd
)
1151 if (!memcmp(config
.mac_address
, tmp_config
.mac_address
, 6))
1152 command_print(cmd
, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1153 config
.mac_address
[5], config
.mac_address
[4],
1154 config
.mac_address
[3], config
.mac_address
[2],
1155 config
.mac_address
[1], config
.mac_address
[0]);
1157 command_print(cmd
, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1158 "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1159 config
.mac_address
[5], config
.mac_address
[4],
1160 config
.mac_address
[3], config
.mac_address
[2],
1161 config
.mac_address
[1], config
.mac_address
[0],
1162 tmp_config
.mac_address
[5], tmp_config
.mac_address
[4],
1163 tmp_config
.mac_address
[3], tmp_config
.mac_address
[2],
1164 tmp_config
.mac_address
[1], tmp_config
.mac_address
[0]);
1167 static void show_config_target_power(struct command_invocation
*cmd
)
1169 const char *target_power
;
1170 const char *current_target_power
;
1172 if (!config
.target_power
)
1173 target_power
= "off";
1175 target_power
= "on";
1177 if (!tmp_config
.target_power
)
1178 current_target_power
= "off";
1180 current_target_power
= "on";
1182 if (config
.target_power
!= tmp_config
.target_power
)
1183 command_print(cmd
, "Target power supply: %s [%s]", target_power
,
1184 current_target_power
);
1186 command_print(cmd
, "Target power supply: %s", target_power
);
1189 static void show_config(struct command_invocation
*cmd
)
1191 command_print(cmd
, "J-Link device configuration:");
1193 show_config_usb_address(cmd
);
1195 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
))
1196 show_config_target_power(cmd
);
1198 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1199 show_config_ip_address(cmd
);
1200 show_config_mac_address(cmd
);
1204 static int poll_trace(uint8_t *buf
, size_t *size
)
1211 ret
= jaylink_swo_read(devh
, buf
, &length
);
1213 if (ret
!= JAYLINK_OK
) {
1214 LOG_ERROR("jaylink_swo_read() failed: %s", jaylink_strerror(ret
));
1223 static uint32_t calculate_trace_buffer_size(void)
1228 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
))
1231 ret
= jaylink_get_free_memory(devh
, &tmp
);
1233 if (ret
!= JAYLINK_OK
) {
1234 LOG_ERROR("jaylink_get_free_memory() failed: %s",
1235 jaylink_strerror(ret
));
1239 if (tmp
> 0x3fff || tmp
<= 0x600)
1244 return tmp
& 0xffffff00;
1247 static bool calculate_swo_prescaler(unsigned int traceclkin_freq
,
1248 uint32_t trace_freq
, uint16_t *prescaler
)
1250 unsigned int presc
= (traceclkin_freq
+ trace_freq
/ 2) / trace_freq
;
1251 if (presc
== 0 || presc
> TPIU_ACPR_MAX_SWOSCALER
+ 1)
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
)
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
)
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;
1277 if (presc
> TPIU_ACPR_MAX_SWOSCALER
+ 1)
1280 deviation
= fabs(1.0 - ((double)*trace_freq
* presc
/ traceclkin_freq
));
1282 if (deviation
<= SWO_MAX_FREQ_DEV
) {
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
)
1296 uint32_t buffer_size
;
1297 struct jaylink_swo_speed speed
;
1302 trace_enabled
= enabled
;
1304 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SWO
)) {
1308 LOG_ERROR("Trace capturing is not supported by the device");
1312 ret
= jaylink_swo_stop(devh
);
1314 if (ret
!= JAYLINK_OK
) {
1315 LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret
));
1321 * Adjust the SWD transaction buffer size as stopping SWO capturing
1322 * deallocates device internal memory.
1324 if (!adjust_swd_buffer_size())
1330 if (pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
) {
1331 LOG_ERROR("Selected pin protocol is not supported");
1335 buffer_size
= calculate_trace_buffer_size();
1338 LOG_ERROR("Not enough free device memory to start trace capturing");
1342 ret
= jaylink_swo_get_speeds(devh
, JAYLINK_SWO_MODE_UART
, &speed
);
1344 if (ret
!= JAYLINK_OK
) {
1345 LOG_ERROR("jaylink_swo_get_speeds() failed: %s",
1346 jaylink_strerror(ret
));
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
;
1355 if (*trace_freq
> max_freq
) {
1356 LOG_INFO("Given SWO frequency too high, using %" PRIu32
" Hz instead",
1358 *trace_freq
= max_freq
;
1359 } else if (*trace_freq
< min_freq
) {
1360 LOG_INFO("Given SWO frequency too low, using %" PRIu32
" Hz instead",
1362 *trace_freq
= min_freq
;
1363 } else if (*trace_freq
!= speed
.freq
/ divider
) {
1364 *trace_freq
= speed
.freq
/ divider
;
1366 LOG_INFO("Given SWO frequency is not supported by the device, "
1367 "using %u Hz instead", *trace_freq
);
1370 if (!calculate_swo_prescaler(traceclkin_freq
, *trace_freq
,
1372 LOG_ERROR("SWO frequency is not suitable. Please choose a "
1373 "different frequency or use auto-detection");
1377 LOG_INFO("Trying to auto-detect SWO frequency");
1379 if (!detect_swo_freq_and_prescaler(speed
, traceclkin_freq
, trace_freq
,
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");
1387 LOG_INFO("Using SWO frequency of %u Hz", *trace_freq
);
1390 ret
= jaylink_swo_start(devh
, JAYLINK_SWO_MODE_UART
, *trace_freq
,
1393 if (ret
!= JAYLINK_OK
) {
1394 LOG_ERROR("jaylink_start_swo() failed: %s", jaylink_strerror(ret
));
1398 LOG_DEBUG("Using %" PRIu32
" bytes device memory for trace capturing",
1402 * Adjust the SWD transaction buffer size as starting SWO capturing
1403 * allocates device internal memory.
1405 if (!adjust_swd_buffer_size())
1411 COMMAND_HANDLER(jlink_handle_config_usb_address_command
)
1415 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1416 command_print(CMD
, "Reading configuration is not supported by the "
1422 show_config_usb_address(CMD
);
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]);
1426 return ERROR_COMMAND_ARGUMENT_INVALID
;
1429 if (tmp
> JAYLINK_USB_ADDRESS_3
) {
1430 command_print(CMD
, "Invalid USB address: %u", tmp
);
1431 return ERROR_COMMAND_ARGUMENT_INVALID
;
1434 tmp_config
.usb_address
= tmp
;
1436 return ERROR_COMMAND_SYNTAX_ERROR
;
1442 COMMAND_HANDLER(jlink_handle_config_target_power_command
)
1446 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1447 command_print(CMD
, "Reading configuration is not supported by the "
1452 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
)) {
1453 command_print(CMD
, "Target power supply is not supported by the "
1459 show_config_target_power(CMD
);
1460 } else if (CMD_ARGC
== 1) {
1461 if (!strcmp(CMD_ARGV
[0], "on")) {
1463 } else if (!strcmp(CMD_ARGV
[0], "off")) {
1466 command_print(CMD
, "Invalid argument: %s", CMD_ARGV
[0]);
1467 return ERROR_COMMAND_ARGUMENT_INVALID
;
1470 tmp_config
.target_power
= enable
;
1472 return ERROR_COMMAND_SYNTAX_ERROR
;
1478 COMMAND_HANDLER(jlink_handle_config_mac_address_command
)
1485 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1486 command_print(CMD
, "Reading configuration is not supported by the "
1491 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1492 command_print(CMD
, "Ethernet connectivity is not supported by the "
1498 show_config_mac_address(CMD
);
1499 } else if (CMD_ARGC
== 1) {
1502 if ((strlen(str
) != 17) || (str
[2] != ':' || str
[5] != ':' ||
1503 str
[8] != ':' || str
[11] != ':' || str
[14] != ':')) {
1504 command_print(CMD
, "Invalid MAC address format");
1505 return ERROR_COMMAND_ARGUMENT_INVALID
;
1508 for (i
= 5; i
>= 0; i
--) {
1509 addr
[i
] = strtoul(str
, &e
, 16);
1513 if (!(addr
[0] | addr
[1] | addr
[2] | addr
[3] | addr
[4] | addr
[5])) {
1514 command_print(CMD
, "Invalid MAC address: zero address");
1515 return ERROR_COMMAND_ARGUMENT_INVALID
;
1518 if (!(0x01 & addr
[0])) {
1519 command_print(CMD
, "Invalid MAC address: multicast address");
1520 return ERROR_COMMAND_ARGUMENT_INVALID
;
1523 memcpy(tmp_config
.mac_address
, addr
, sizeof(addr
));
1525 return ERROR_COMMAND_SYNTAX_ERROR
;
1531 static bool string_to_ip(const char *s
, uint8_t *ip
, int *pos
)
1535 const char *s_save
= s
;
1541 for (i
= 0; i
< 4; i
++) {
1542 lip
[i
] = strtoul(s
, &e
, 10);
1544 if (*e
!= '.' && i
!= 3)
1551 memcpy(ip
, lip
, sizeof(lip
));
1556 static void cpy_ip(uint8_t *dst
, uint8_t *src
)
1560 for (i
= 0, j
= 3; i
< 4; i
++, j
--)
1564 COMMAND_HANDLER(jlink_handle_config_ip_address_command
)
1566 uint8_t ip_address
[4];
1567 uint32_t subnet_mask
= 0;
1569 uint8_t subnet_bits
= 24;
1571 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1572 command_print(CMD
, "Reading configuration is not supported by the "
1577 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1578 command_print(CMD
, "Ethernet connectivity is not supported by the "
1584 show_config_ip_address(CMD
);
1586 if (!string_to_ip(CMD_ARGV
[0], ip_address
, &i
)) {
1587 command_print(CMD
, "invalid IPv4 address");
1588 return ERROR_COMMAND_ARGUMENT_INVALID
;
1591 len
= strlen(CMD_ARGV
[0]);
1593 /* Check for format A.B.C.D/E. */
1595 if (CMD_ARGV
[0][i
] != '/') {
1596 command_print(CMD
, "missing network mask");
1597 return ERROR_COMMAND_ARGUMENT_INVALID
;
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");
1604 return ERROR_COMMAND_ARGUMENT_INVALID
;
1609 subnet_mask
= (uint32_t)(subnet_bits
< 32 ?
1610 ((1ULL << subnet_bits
) - 1) : 0xffffffff);
1612 cpy_ip(tmp_config
.ip_address
, ip_address
);
1613 cpy_ip(tmp_config
.subnet_mask
, (uint8_t *)&subnet_mask
);
1619 COMMAND_HANDLER(jlink_handle_config_reset_command
)
1621 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
))
1624 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
1630 COMMAND_HANDLER(jlink_handle_config_write_command
)
1634 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1635 command_print(CMD
, "Reading configuration is not supported by the "
1640 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_WRITE_CONFIG
)) {
1641 command_print(CMD
, "Writing configuration is not supported by the "
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");
1652 ret
= jaylink_write_raw_config(devh
, (const uint8_t *)&tmp_config
);
1654 if (ret
!= JAYLINK_OK
) {
1655 LOG_ERROR("jaylink_write_raw_config() failed: %s",
1656 jaylink_strerror(ret
));
1660 if (!read_device_config(&config
)) {
1661 LOG_ERROR("Failed to read device configuration for verification");
1665 if (memcmp(&config
, &tmp_config
, sizeof(struct device_config
))) {
1666 LOG_ERROR("Verification of device configuration failed. Please check "
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");
1678 COMMAND_HANDLER(jlink_handle_config_command
)
1680 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1681 command_print(CMD
, "Device doesn't support reading configuration");
1691 COMMAND_HANDLER(jlink_handle_emucom_write_command
)
1701 return ERROR_COMMAND_SYNTAX_ERROR
;
1703 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_EMUCOM
)) {
1704 LOG_ERROR("Device does not support EMUCOM");
1708 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], channel
);
1710 tmp
= strlen(CMD_ARGV
[1]);
1713 LOG_ERROR("Data must be encoded as hexadecimal pairs");
1714 return ERROR_COMMAND_ARGUMENT_INVALID
;
1717 buf
= malloc(tmp
/ 2);
1720 LOG_ERROR("Failed to allocate buffer");
1724 dummy
= unhexify(buf
, CMD_ARGV
[1], tmp
/ 2);
1726 if (dummy
!= (tmp
/ 2)) {
1727 LOG_ERROR("Data must be encoded as hexadecimal pairs");
1729 return ERROR_COMMAND_ARGUMENT_INVALID
;
1733 ret
= jaylink_emucom_write(devh
, channel
, buf
, &length
);
1737 if (ret
== JAYLINK_ERR_DEV_NOT_SUPPORTED
) {
1738 LOG_ERROR("Channel not supported by the device");
1740 } else if (ret
!= JAYLINK_OK
) {
1741 LOG_ERROR("Failed to write to channel: %s", jaylink_strerror(ret
));
1745 if (length
!= (tmp
/ 2))
1746 LOG_WARNING("Only %" PRIu32
" bytes written to the channel", length
);
1751 COMMAND_HANDLER(jlink_handle_emucom_read_command
)
1760 return ERROR_COMMAND_SYNTAX_ERROR
;
1762 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_EMUCOM
)) {
1763 LOG_ERROR("Device does not support EMUCOM");
1767 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], channel
);
1768 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
1770 buf
= malloc(length
* 3 + 1);
1773 LOG_ERROR("Failed to allocate buffer");
1777 ret
= jaylink_emucom_read(devh
, channel
, buf
, &length
);
1779 if (ret
== JAYLINK_ERR_DEV_NOT_SUPPORTED
) {
1780 LOG_ERROR("Channel is not supported by the device");
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
);
1788 } else if (ret
!= JAYLINK_OK
) {
1789 LOG_ERROR("Failed to read from channel: %s", jaylink_strerror(ret
));
1794 tmp
= hexify((char *)buf
+ length
, buf
, length
, 2 * length
+ 1);
1796 if (tmp
!= 2 * length
) {
1797 LOG_ERROR("Failed to convert data into hexadecimal string");
1802 command_print(CMD
, "%s", buf
+ length
);
1808 static const struct command_registration jlink_config_subcommand_handlers
[] = {
1811 .handler
= &jlink_handle_config_usb_address_command
,
1812 .mode
= COMMAND_EXEC
,
1813 .help
= "set the USB address",
1817 .name
= "targetpower",
1818 .handler
= &jlink_handle_config_target_power_command
,
1819 .mode
= COMMAND_EXEC
,
1820 .help
= "set the target power supply",
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]",
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]]",
1840 .handler
= &jlink_handle_config_reset_command
,
1841 .mode
= COMMAND_EXEC
,
1842 .help
= "undo configuration changes",
1847 .handler
= &jlink_handle_config_write_command
,
1848 .mode
= COMMAND_EXEC
,
1849 .help
= "write configuration to the device",
1852 COMMAND_REGISTRATION_DONE
1855 static const struct command_registration jlink_emucom_subcommand_handlers
[] = {
1858 .handler
= &jlink_handle_emucom_write_command
,
1859 .mode
= COMMAND_EXEC
,
1860 .help
= "write to a channel",
1861 .usage
= "<channel> <data>",
1865 .handler
= &jlink_handle_emucom_read_command
,
1866 .mode
= COMMAND_EXEC
,
1867 .help
= "read from a channel",
1868 .usage
= "<channel> <length>"
1870 COMMAND_REGISTRATION_DONE
1873 static const struct command_registration jlink_subcommand_handlers
[] = {
1876 .handler
= &jlink_handle_jlink_jtag_command
,
1877 .mode
= COMMAND_EXEC
,
1878 .help
= "select the JTAG command version",
1882 .name
= "targetpower",
1883 .handler
= &jlink_handle_target_power_command
,
1884 .mode
= COMMAND_EXEC
,
1885 .help
= "set the target power supply",
1890 .handler
= &jlink_handle_free_memory_command
,
1891 .mode
= COMMAND_EXEC
,
1892 .help
= "show free device memory",
1897 .handler
= &jlink_handle_hwstatus_command
,
1898 .mode
= COMMAND_EXEC
,
1899 .help
= "show the hardware status",
1904 .handler
= &jlink_usb_command
,
1905 .mode
= COMMAND_CONFIG
,
1906 .help
= "set the USB address of the device that should be used",
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",
1915 .chain
= jlink_config_subcommand_handlers
,
1920 .mode
= COMMAND_EXEC
,
1921 .help
= "access EMUCOM channel",
1922 .chain
= jlink_emucom_subcommand_handlers
,
1925 COMMAND_REGISTRATION_DONE
1928 static const struct command_registration jlink_command_handlers
[] = {
1931 .mode
= COMMAND_ANY
,
1932 .help
= "perform jlink management",
1933 .chain
= jlink_subcommand_handlers
,
1936 COMMAND_REGISTRATION_DONE
1939 static int jlink_swd_init(void)
1941 iface
= JAYLINK_TIF_SWD
;
1946 static void jlink_swd_write_reg(uint8_t cmd
, uint32_t value
, uint32_t ap_delay_clk
)
1948 assert(!(cmd
& SWD_CMD_RNW
));
1949 jlink_swd_queue_cmd(cmd
, NULL
, value
, ap_delay_clk
);
1952 static void jlink_swd_read_reg(uint8_t cmd
, uint32_t *value
, uint32_t ap_delay_clk
)
1954 assert(cmd
& SWD_CMD_RNW
);
1955 jlink_swd_queue_cmd(cmd
, value
, 0, ap_delay_clk
);
1958 /***************************************************************************/
1959 /* J-Link tap functions */
1961 static unsigned tap_length
;
1962 /* In SWD mode use tms buffer for direction control */
1963 static uint8_t tms_buffer
[JLINK_TAP_BUFFER_SIZE
];
1964 static uint8_t tdi_buffer
[JLINK_TAP_BUFFER_SIZE
];
1965 static uint8_t tdo_buffer
[JLINK_TAP_BUFFER_SIZE
];
1967 struct pending_scan_result
{
1968 /** First bit position in tdo_buffer to read. */
1970 /** Number of bits to read. */
1972 /** Location to store the result */
1974 /** Offset in the destination buffer */
1975 unsigned buffer_offset
;
1980 #define MAX_PENDING_SCAN_RESULTS 256
1982 static int pending_scan_results_length
;
1983 static struct pending_scan_result pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
1985 static void jlink_tap_init(void)
1988 pending_scan_results_length
= 0;
1989 memset(tms_buffer
, 0, sizeof(tms_buffer
));
1990 memset(tdi_buffer
, 0, sizeof(tdi_buffer
));
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
,
1999 unsigned available_length
= JLINK_TAP_BUFFER_SIZE
- tap_length
/ 8;
2001 if (!available_length
||
2002 (in
&& pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
)) {
2003 if (jlink_flush() != ERROR_OK
)
2005 available_length
= JLINK_TAP_BUFFER_SIZE
;
2008 struct pending_scan_result
*pending_scan_result
=
2009 &pending_scan_results_buffer
[pending_scan_results_length
];
2011 unsigned scan_length
= length
> available_length
?
2012 available_length
: length
;
2015 buf_set_buf(out
, out_offset
, tdi_buffer
, tap_length
, scan_length
);
2017 buf_set_buf(tms_out
, tms_offset
, tms_buffer
, tap_length
, scan_length
);
2020 pending_scan_result
->first
= tap_length
;
2021 pending_scan_result
->length
= scan_length
;
2022 pending_scan_result
->buffer
= in
;
2023 pending_scan_result
->buffer_offset
= in_offset
;
2024 pending_scan_results_length
++;
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);
2035 static int jlink_flush(void)
2043 jlink_last_state
= jtag_debug_state_machine(tms_buffer
, tdi_buffer
,
2044 tap_length
, jlink_last_state
);
2046 ret
= jaylink_jtag_io(devh
, tms_buffer
, tdi_buffer
, tdo_buffer
,
2047 tap_length
, jtag_command_version
);
2049 if (ret
!= JAYLINK_OK
) {
2050 LOG_ERROR("jaylink_jtag_io() failed: %s", jaylink_strerror(ret
));
2052 return ERROR_JTAG_QUEUE_FAILED
;
2055 for (i
= 0; i
< pending_scan_results_length
; i
++) {
2056 struct pending_scan_result
*p
= &pending_scan_results_buffer
[i
];
2058 buf_set_buf(tdo_buffer
, p
->first
, p
->buffer
,
2059 p
->buffer_offset
, p
->length
);
2061 LOG_DEBUG_IO("Pending scan result, length = %d", p
->length
);
2069 static void fill_buffer(uint8_t *buf
, uint32_t val
, uint32_t len
)
2071 unsigned int tap_pos
= tap_length
;
2074 buf_set_u32(buf
, tap_pos
, 32, val
);
2080 buf_set_u32(buf
, tap_pos
, len
, val
);
2083 static void jlink_queue_data_out(const uint8_t *data
, uint32_t len
)
2085 const uint32_t dir_out
= 0xffffffff;
2088 bit_copy(tdi_buffer
, tap_length
, data
, 0, len
);
2090 fill_buffer(tdi_buffer
, 0, len
);
2092 fill_buffer(tms_buffer
, dir_out
, len
);
2096 static void jlink_queue_data_in(uint32_t len
)
2098 const uint32_t dir_in
= 0;
2100 fill_buffer(tms_buffer
, dir_in
, len
);
2104 static int jlink_swd_switch_seq(enum swd_special_seq seq
)
2111 LOG_DEBUG("SWD line reset");
2112 s
= swd_seq_line_reset
;
2113 s_len
= swd_seq_line_reset_len
;
2116 LOG_DEBUG("JTAG-to-SWD");
2117 s
= swd_seq_jtag_to_swd
;
2118 s_len
= swd_seq_jtag_to_swd_len
;
2120 case JTAG_TO_DORMANT
:
2121 LOG_DEBUG("JTAG-to-DORMANT");
2122 s
= swd_seq_jtag_to_dormant
;
2123 s_len
= swd_seq_jtag_to_dormant_len
;
2126 LOG_DEBUG("SWD-to-JTAG");
2127 s
= swd_seq_swd_to_jtag
;
2128 s_len
= swd_seq_swd_to_jtag_len
;
2130 case SWD_TO_DORMANT
:
2131 LOG_DEBUG("SWD-to-DORMANT");
2132 s
= swd_seq_swd_to_dormant
;
2133 s_len
= swd_seq_swd_to_dormant_len
;
2135 case DORMANT_TO_SWD
:
2136 LOG_DEBUG("DORMANT-to-SWD");
2137 s
= swd_seq_dormant_to_swd
;
2138 s_len
= swd_seq_dormant_to_swd_len
;
2140 case DORMANT_TO_JTAG
:
2141 LOG_DEBUG("DORMANT-to-JTAG");
2142 s
= swd_seq_dormant_to_jtag
;
2143 s_len
= swd_seq_dormant_to_jtag_len
;
2146 LOG_ERROR("Sequence %d not supported", seq
);
2150 jlink_queue_data_out(s
, s_len
);
2155 static int jlink_swd_run_queue(void)
2160 LOG_DEBUG("Executing %d queued transactions", pending_scan_results_length
);
2162 if (queued_retval
!= ERROR_OK
) {
2163 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval
);
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.
2171 jlink_queue_data_out(NULL
, 8);
2173 ret
= jaylink_swd_io(devh
, tms_buffer
, tdi_buffer
, tdo_buffer
, tap_length
);
2175 if (ret
!= JAYLINK_OK
) {
2176 LOG_ERROR("jaylink_swd_io() failed: %s", jaylink_strerror(ret
));
2180 for (i
= 0; i
< pending_scan_results_length
; i
++) {
2181 /* Devices do not reply to DP_TARGETSEL write cmd, ignore received ack */
2182 bool check_ack
= swd_cmd_returns_ack(pending_scan_results_buffer
[i
].swd_cmd
);
2183 int ack
= buf_get_u32(tdo_buffer
, pending_scan_results_buffer
[i
].first
, 3);
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");
2187 queued_retval
= swd_ack_to_error_code(ack
);
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);
2191 int parity
= buf_get_u32(tdo_buffer
, 3 + 32 + pending_scan_results_buffer
[i
].first
, 1);
2193 if (parity
!= parity_u32(data
)) {
2194 LOG_ERROR("SWD: Read data parity mismatch");
2195 queued_retval
= ERROR_FAIL
;
2199 if (pending_scan_results_buffer
[i
].buffer
)
2200 *(uint32_t *)pending_scan_results_buffer
[i
].buffer
= data
;
2206 ret
= queued_retval
;
2207 queued_retval
= ERROR_OK
;
2212 static void jlink_swd_queue_cmd(uint8_t cmd
, uint32_t *dst
, uint32_t data
, uint32_t ap_delay_clk
)
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 ||
2216 pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
) {
2217 /* Not enough room in the queue. Run the queue. */
2218 queued_retval
= jlink_swd_run_queue();
2221 if (queued_retval
!= ERROR_OK
)
2224 pending_scan_results_buffer
[pending_scan_results_length
].swd_cmd
= cmd
;
2225 cmd
|= SWD_CMD_START
| SWD_CMD_PARK
;
2227 jlink_queue_data_out(&cmd
, 8);
2229 pending_scan_results_buffer
[pending_scan_results_length
].first
= tap_length
;
2231 if (cmd
& SWD_CMD_RNW
) {
2232 /* Queue a read transaction. */
2233 pending_scan_results_buffer
[pending_scan_results_length
].length
= 32;
2234 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= dst
;
2236 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2238 /* Queue a write transaction. */
2239 pending_scan_results_buffer
[pending_scan_results_length
].length
= 0;
2240 jlink_queue_data_in(1 + 3 + 1);
2242 buf_set_u32(data_parity_trn
, 0, 32, data
);
2243 buf_set_u32(data_parity_trn
, 32, 1, parity_u32(data
));
2245 jlink_queue_data_out(data_parity_trn
, 32 + 1);
2248 pending_scan_results_length
++;
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
);
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
,
2263 static const char * const jlink_transports
[] = { "jtag", "swd", NULL
};
2265 static struct jtag_interface jlink_interface
= {
2266 .execute_queue
= &jlink_execute_queue
,
2269 struct adapter_driver jlink_adapter_driver
= {
2271 .transports
= jlink_transports
,
2272 .commands
= jlink_command_handlers
,
2274 .init
= &jlink_init
,
2275 .quit
= &jlink_quit
,
2276 .reset
= &jlink_reset_safe
,
2277 .speed
= &jlink_speed
,
2279 .speed_div
= &jlink_speed_div
,
2280 .config_trace
= &config_trace
,
2281 .poll_trace
= &poll_trace
,
2283 .jtag_ops
= &jlink_interface
,
2284 .swd_ops
= &jlink_swd
,