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 command_print(CMD
, "Need exactly one argument for jlink usb");
971 return ERROR_COMMAND_SYNTAX_ERROR
;
974 if (sscanf(CMD_ARGV
[0], "%i", &tmp
) != 1) {
975 command_print(CMD
, "Invalid USB address: %s", CMD_ARGV
[0]);
979 if (tmp
< JAYLINK_USB_ADDRESS_0
|| tmp
> JAYLINK_USB_ADDRESS_3
) {
980 command_print(CMD
, "Invalid USB address: %s", CMD_ARGV
[0]);
986 use_usb_address
= true;
991 COMMAND_HANDLER(jlink_handle_hwstatus_command
)
994 struct jaylink_hardware_status status
;
996 ret
= jaylink_get_hardware_status(devh
, &status
);
998 if (ret
!= JAYLINK_OK
) {
999 command_print(CMD
, "jaylink_get_hardware_status() failed: %s",
1000 jaylink_strerror(ret
));
1004 command_print(CMD
, "VTarget = %u.%03u V",
1005 status
.target_voltage
/ 1000, status
.target_voltage
% 1000);
1007 command_print(CMD
, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
1008 "TRST = %u", status
.tck
, status
.tdi
, status
.tdo
, status
.tms
,
1009 status
.tres
, status
.trst
);
1011 if (status
.target_voltage
< 1500)
1012 command_print(CMD
, "Target voltage too low. Check target power");
1017 COMMAND_HANDLER(jlink_handle_free_memory_command
)
1022 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
)) {
1023 command_print(CMD
, "Retrieval of free memory is not supported by "
1028 ret
= jaylink_get_free_memory(devh
, &tmp
);
1030 if (ret
!= JAYLINK_OK
) {
1031 command_print(CMD
, "jaylink_get_free_memory() failed: %s",
1032 jaylink_strerror(ret
));
1036 command_print(CMD
, "Device has %" PRIu32
" bytes of free memory", tmp
);
1041 COMMAND_HANDLER(jlink_handle_jlink_jtag_command
)
1047 switch (jtag_command_version
) {
1048 case JAYLINK_JTAG_VERSION_2
:
1051 case JAYLINK_JTAG_VERSION_3
:
1058 command_print(CMD
, "JTAG command version: %i", version
);
1059 } else if (CMD_ARGC
== 1) {
1060 if (sscanf(CMD_ARGV
[0], "%i", &tmp
) != 1) {
1061 command_print(CMD
, "Invalid argument: %s", CMD_ARGV
[0]);
1062 return ERROR_COMMAND_SYNTAX_ERROR
;
1067 jtag_command_version
= JAYLINK_JTAG_VERSION_2
;
1070 jtag_command_version
= JAYLINK_JTAG_VERSION_3
;
1073 command_print(CMD
, "Invalid argument: %s", CMD_ARGV
[0]);
1074 return ERROR_COMMAND_SYNTAX_ERROR
;
1077 command_print(CMD
, "Need exactly one argument for jlink jtag");
1078 return ERROR_COMMAND_SYNTAX_ERROR
;
1084 COMMAND_HANDLER(jlink_handle_target_power_command
)
1089 if (CMD_ARGC
!= 1) {
1090 command_print(CMD
, "Need exactly one argument for jlink targetpower");
1091 return ERROR_COMMAND_SYNTAX_ERROR
;
1094 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
)) {
1095 command_print(CMD
, "Target power supply is not supported by the "
1100 if (!strcmp(CMD_ARGV
[0], "on")) {
1102 } else if (!strcmp(CMD_ARGV
[0], "off")) {
1105 command_print(CMD
, "Invalid argument: %s", CMD_ARGV
[0]);
1109 ret
= jaylink_set_target_power(devh
, enable
);
1111 if (ret
!= JAYLINK_OK
) {
1112 command_print(CMD
, "jaylink_set_target_power() failed: %s",
1113 jaylink_strerror(ret
));
1120 static void show_config_usb_address(struct command_invocation
*cmd
)
1122 if (config
.usb_address
!= tmp_config
.usb_address
)
1123 command_print(cmd
, "USB address: %u [%u]", config
.usb_address
,
1124 tmp_config
.usb_address
);
1126 command_print(cmd
, "USB address: %u", config
.usb_address
);
1129 static void show_config_ip_address(struct command_invocation
*cmd
)
1131 if (!memcmp(config
.ip_address
, tmp_config
.ip_address
, 4))
1132 command_print(cmd
, "IP address: %d.%d.%d.%d",
1133 config
.ip_address
[3], config
.ip_address
[2],
1134 config
.ip_address
[1], config
.ip_address
[0]);
1136 command_print(cmd
, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1137 config
.ip_address
[3], config
.ip_address
[2],
1138 config
.ip_address
[1], config
.ip_address
[0],
1139 tmp_config
.ip_address
[3], tmp_config
.ip_address
[2],
1140 tmp_config
.ip_address
[1], tmp_config
.ip_address
[0]);
1142 if (!memcmp(config
.subnet_mask
, tmp_config
.subnet_mask
, 4))
1143 command_print(cmd
, "Subnet mask: %d.%d.%d.%d",
1144 config
.subnet_mask
[3], config
.subnet_mask
[2],
1145 config
.subnet_mask
[1], config
.subnet_mask
[0]);
1147 command_print(cmd
, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1148 config
.subnet_mask
[3], config
.subnet_mask
[2],
1149 config
.subnet_mask
[1], config
.subnet_mask
[0],
1150 tmp_config
.subnet_mask
[3], tmp_config
.subnet_mask
[2],
1151 tmp_config
.subnet_mask
[1], tmp_config
.subnet_mask
[0]);
1154 static void show_config_mac_address(struct command_invocation
*cmd
)
1156 if (!memcmp(config
.mac_address
, tmp_config
.mac_address
, 6))
1157 command_print(cmd
, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1158 config
.mac_address
[5], config
.mac_address
[4],
1159 config
.mac_address
[3], config
.mac_address
[2],
1160 config
.mac_address
[1], config
.mac_address
[0]);
1162 command_print(cmd
, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1163 "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1164 config
.mac_address
[5], config
.mac_address
[4],
1165 config
.mac_address
[3], config
.mac_address
[2],
1166 config
.mac_address
[1], config
.mac_address
[0],
1167 tmp_config
.mac_address
[5], tmp_config
.mac_address
[4],
1168 tmp_config
.mac_address
[3], tmp_config
.mac_address
[2],
1169 tmp_config
.mac_address
[1], tmp_config
.mac_address
[0]);
1172 static void show_config_target_power(struct command_invocation
*cmd
)
1174 const char *target_power
;
1175 const char *current_target_power
;
1177 if (!config
.target_power
)
1178 target_power
= "off";
1180 target_power
= "on";
1182 if (!tmp_config
.target_power
)
1183 current_target_power
= "off";
1185 current_target_power
= "on";
1187 if (config
.target_power
!= tmp_config
.target_power
)
1188 command_print(cmd
, "Target power supply: %s [%s]", target_power
,
1189 current_target_power
);
1191 command_print(cmd
, "Target power supply: %s", target_power
);
1194 static void show_config(struct command_invocation
*cmd
)
1196 command_print(cmd
, "J-Link device configuration:");
1198 show_config_usb_address(cmd
);
1200 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
))
1201 show_config_target_power(cmd
);
1203 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1204 show_config_ip_address(cmd
);
1205 show_config_mac_address(cmd
);
1209 static int poll_trace(uint8_t *buf
, size_t *size
)
1216 ret
= jaylink_swo_read(devh
, buf
, &length
);
1218 if (ret
!= JAYLINK_OK
) {
1219 LOG_ERROR("jaylink_swo_read() failed: %s", jaylink_strerror(ret
));
1228 static uint32_t calculate_trace_buffer_size(void)
1233 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
))
1236 ret
= jaylink_get_free_memory(devh
, &tmp
);
1238 if (ret
!= JAYLINK_OK
) {
1239 LOG_ERROR("jaylink_get_free_memory() failed: %s",
1240 jaylink_strerror(ret
));
1244 if (tmp
> 0x3fff || tmp
<= 0x600)
1249 return tmp
& 0xffffff00;
1252 static bool calculate_swo_prescaler(unsigned int traceclkin_freq
,
1253 uint32_t trace_freq
, uint16_t *prescaler
)
1255 unsigned int presc
= (traceclkin_freq
+ trace_freq
/ 2) / trace_freq
;
1256 if (presc
== 0 || presc
> TPIU_ACPR_MAX_SWOSCALER
+ 1)
1259 /* Probe's UART speed must be within 3% of the TPIU's SWO baud rate. */
1260 unsigned int max_deviation
= (traceclkin_freq
* 3) / 100;
1261 if (presc
* trace_freq
< traceclkin_freq
- max_deviation
||
1262 presc
* trace_freq
> traceclkin_freq
+ max_deviation
)
1270 static bool detect_swo_freq_and_prescaler(struct jaylink_swo_speed speed
,
1271 unsigned int traceclkin_freq
, unsigned int *trace_freq
,
1272 uint16_t *prescaler
)
1278 for (divider
= speed
.min_div
; divider
<= speed
.max_div
; divider
++) {
1279 *trace_freq
= speed
.freq
/ divider
;
1280 presc
= ((1.0 - SWO_MAX_FREQ_DEV
) * traceclkin_freq
) / *trace_freq
+ 1;
1282 if (presc
> TPIU_ACPR_MAX_SWOSCALER
+ 1)
1285 deviation
= fabs(1.0 - ((double)*trace_freq
* presc
/ traceclkin_freq
));
1287 if (deviation
<= SWO_MAX_FREQ_DEV
) {
1296 static int config_trace(bool enabled
, enum tpiu_pin_protocol pin_protocol
,
1297 uint32_t port_size
, unsigned int *trace_freq
,
1298 unsigned int traceclkin_freq
, uint16_t *prescaler
)
1301 uint32_t buffer_size
;
1302 struct jaylink_swo_speed speed
;
1307 trace_enabled
= enabled
;
1309 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SWO
)) {
1313 LOG_ERROR("Trace capturing is not supported by the device");
1317 ret
= jaylink_swo_stop(devh
);
1319 if (ret
!= JAYLINK_OK
) {
1320 LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret
));
1326 * Adjust the SWD transaction buffer size as stopping SWO capturing
1327 * deallocates device internal memory.
1329 if (!adjust_swd_buffer_size())
1335 if (pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
) {
1336 LOG_ERROR("Selected pin protocol is not supported");
1340 buffer_size
= calculate_trace_buffer_size();
1343 LOG_ERROR("Not enough free device memory to start trace capturing");
1347 ret
= jaylink_swo_get_speeds(devh
, JAYLINK_SWO_MODE_UART
, &speed
);
1349 if (ret
!= JAYLINK_OK
) {
1350 LOG_ERROR("jaylink_swo_get_speeds() failed: %s",
1351 jaylink_strerror(ret
));
1355 if (*trace_freq
> 0) {
1356 divider
= speed
.freq
/ *trace_freq
;
1357 min_freq
= speed
.freq
/ speed
.max_div
;
1358 max_freq
= speed
.freq
/ speed
.min_div
;
1360 if (*trace_freq
> max_freq
) {
1361 LOG_INFO("Given SWO frequency too high, using %" PRIu32
" Hz instead",
1363 *trace_freq
= max_freq
;
1364 } else if (*trace_freq
< min_freq
) {
1365 LOG_INFO("Given SWO frequency too low, using %" PRIu32
" Hz instead",
1367 *trace_freq
= min_freq
;
1368 } else if (*trace_freq
!= speed
.freq
/ divider
) {
1369 *trace_freq
= speed
.freq
/ divider
;
1371 LOG_INFO("Given SWO frequency is not supported by the device, "
1372 "using %u Hz instead", *trace_freq
);
1375 if (!calculate_swo_prescaler(traceclkin_freq
, *trace_freq
,
1377 LOG_ERROR("SWO frequency is not suitable. Please choose a "
1378 "different frequency or use auto-detection");
1382 LOG_INFO("Trying to auto-detect SWO frequency");
1384 if (!detect_swo_freq_and_prescaler(speed
, traceclkin_freq
, trace_freq
,
1386 LOG_ERROR("Maximum permitted frequency deviation of %.02f %% "
1387 "could not be achieved", SWO_MAX_FREQ_DEV
);
1388 LOG_ERROR("Auto-detection of SWO frequency failed");
1392 LOG_INFO("Using SWO frequency of %u Hz", *trace_freq
);
1395 ret
= jaylink_swo_start(devh
, JAYLINK_SWO_MODE_UART
, *trace_freq
,
1398 if (ret
!= JAYLINK_OK
) {
1399 LOG_ERROR("jaylink_start_swo() failed: %s", jaylink_strerror(ret
));
1403 LOG_DEBUG("Using %" PRIu32
" bytes device memory for trace capturing",
1407 * Adjust the SWD transaction buffer size as starting SWO capturing
1408 * allocates device internal memory.
1410 if (!adjust_swd_buffer_size())
1416 COMMAND_HANDLER(jlink_handle_config_usb_address_command
)
1420 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1421 command_print(CMD
, "Reading configuration is not supported by the "
1427 show_config_usb_address(CMD
);
1428 } else if (CMD_ARGC
== 1) {
1429 if (sscanf(CMD_ARGV
[0], "%" SCNd8
, &tmp
) != 1) {
1430 command_print(CMD
, "Invalid USB address: %s", CMD_ARGV
[0]);
1434 if (tmp
> JAYLINK_USB_ADDRESS_3
) {
1435 command_print(CMD
, "Invalid USB address: %u", tmp
);
1439 tmp_config
.usb_address
= tmp
;
1441 command_print(CMD
, "Need exactly one argument for jlink config usb");
1442 return ERROR_COMMAND_SYNTAX_ERROR
;
1448 COMMAND_HANDLER(jlink_handle_config_target_power_command
)
1452 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1453 command_print(CMD
, "Reading configuration is not supported by the "
1458 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
)) {
1459 command_print(CMD
, "Target power supply is not supported by the "
1465 show_config_target_power(CMD
);
1466 } else if (CMD_ARGC
== 1) {
1467 if (!strcmp(CMD_ARGV
[0], "on")) {
1469 } else if (!strcmp(CMD_ARGV
[0], "off")) {
1472 command_print(CMD
, "Invalid argument: %s", CMD_ARGV
[0]);
1476 tmp_config
.target_power
= enable
;
1478 command_print(CMD
, "Need exactly one argument for jlink config "
1480 return ERROR_COMMAND_SYNTAX_ERROR
;
1486 COMMAND_HANDLER(jlink_handle_config_mac_address_command
)
1493 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1494 command_print(CMD
, "Reading configuration is not supported by the "
1499 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1500 command_print(CMD
, "Ethernet connectivity is not supported by the "
1506 show_config_mac_address(CMD
);
1507 } else if (CMD_ARGC
== 1) {
1510 if ((strlen(str
) != 17) || (str
[2] != ':' || str
[5] != ':' ||
1511 str
[8] != ':' || str
[11] != ':' || str
[14] != ':')) {
1512 command_print(CMD
, "Invalid MAC address format");
1513 return ERROR_COMMAND_SYNTAX_ERROR
;
1516 for (i
= 5; i
>= 0; i
--) {
1517 addr
[i
] = strtoul(str
, &e
, 16);
1521 if (!(addr
[0] | addr
[1] | addr
[2] | addr
[3] | addr
[4] | addr
[5])) {
1522 command_print(CMD
, "Invalid MAC address: zero address");
1523 return ERROR_COMMAND_SYNTAX_ERROR
;
1526 if (!(0x01 & addr
[0])) {
1527 command_print(CMD
, "Invalid MAC address: multicast address");
1528 return ERROR_COMMAND_SYNTAX_ERROR
;
1531 memcpy(tmp_config
.mac_address
, addr
, sizeof(addr
));
1533 command_print(CMD
, "Need exactly one argument for jlink config mac");
1534 return ERROR_COMMAND_SYNTAX_ERROR
;
1540 static bool string_to_ip(const char *s
, uint8_t *ip
, int *pos
)
1544 const char *s_save
= s
;
1550 for (i
= 0; i
< 4; i
++) {
1551 lip
[i
] = strtoul(s
, &e
, 10);
1553 if (*e
!= '.' && i
!= 3)
1560 memcpy(ip
, lip
, sizeof(lip
));
1565 static void cpy_ip(uint8_t *dst
, uint8_t *src
)
1569 for (i
= 0, j
= 3; i
< 4; i
++, j
--)
1573 COMMAND_HANDLER(jlink_handle_config_ip_address_command
)
1575 uint8_t ip_address
[4];
1576 uint32_t subnet_mask
= 0;
1578 uint8_t subnet_bits
= 24;
1580 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1581 command_print(CMD
, "Reading configuration is not supported by the "
1586 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1587 command_print(CMD
, "Ethernet connectivity is not supported by the "
1593 show_config_ip_address(CMD
);
1595 if (!string_to_ip(CMD_ARGV
[0], ip_address
, &i
))
1596 return ERROR_COMMAND_SYNTAX_ERROR
;
1598 len
= strlen(CMD_ARGV
[0]);
1600 /* Check for format A.B.C.D/E. */
1602 if (CMD_ARGV
[0][i
] != '/')
1603 return ERROR_COMMAND_SYNTAX_ERROR
;
1605 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0] + i
+ 1, subnet_bits
);
1606 } else if (CMD_ARGC
> 1) {
1607 if (!string_to_ip(CMD_ARGV
[1], (uint8_t *)&subnet_mask
, &i
))
1608 return ERROR_COMMAND_SYNTAX_ERROR
;
1612 subnet_mask
= (uint32_t)(subnet_bits
< 32 ?
1613 ((1ULL << subnet_bits
) - 1) : 0xffffffff);
1615 cpy_ip(tmp_config
.ip_address
, ip_address
);
1616 cpy_ip(tmp_config
.subnet_mask
, (uint8_t *)&subnet_mask
);
1622 COMMAND_HANDLER(jlink_handle_config_reset_command
)
1624 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
))
1627 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
1633 COMMAND_HANDLER(jlink_handle_config_write_command
)
1637 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1638 command_print(CMD
, "Reading configuration is not supported by the "
1643 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_WRITE_CONFIG
)) {
1644 command_print(CMD
, "Writing configuration is not supported by the "
1649 if (!memcmp(&config
, &tmp_config
, sizeof(struct device_config
))) {
1650 command_print(CMD
, "Operation not performed due to no changes in "
1651 "the configuration");
1655 ret
= jaylink_write_raw_config(devh
, (const uint8_t *)&tmp_config
);
1657 if (ret
!= JAYLINK_OK
) {
1658 LOG_ERROR("jaylink_write_raw_config() failed: %s",
1659 jaylink_strerror(ret
));
1663 if (!read_device_config(&config
)) {
1664 LOG_ERROR("Failed to read device configuration for verification");
1668 if (memcmp(&config
, &tmp_config
, sizeof(struct device_config
))) {
1669 LOG_ERROR("Verification of device configuration failed. Please check "
1674 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
1675 command_print(CMD
, "The new device configuration applies after power "
1676 "cycling the J-Link device");
1681 COMMAND_HANDLER(jlink_handle_config_command
)
1683 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1684 command_print(CMD
, "Device doesn't support reading configuration");
1694 COMMAND_HANDLER(jlink_handle_emucom_write_command
)
1704 return ERROR_COMMAND_SYNTAX_ERROR
;
1706 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_EMUCOM
)) {
1707 LOG_ERROR("Device does not support EMUCOM");
1711 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], channel
);
1713 tmp
= strlen(CMD_ARGV
[1]);
1716 LOG_ERROR("Data must be encoded as hexadecimal pairs");
1717 return ERROR_COMMAND_ARGUMENT_INVALID
;
1720 buf
= malloc(tmp
/ 2);
1723 LOG_ERROR("Failed to allocate buffer");
1727 dummy
= unhexify(buf
, CMD_ARGV
[1], tmp
/ 2);
1729 if (dummy
!= (tmp
/ 2)) {
1730 LOG_ERROR("Data must be encoded as hexadecimal pairs");
1732 return ERROR_COMMAND_ARGUMENT_INVALID
;
1736 ret
= jaylink_emucom_write(devh
, channel
, buf
, &length
);
1740 if (ret
== JAYLINK_ERR_DEV_NOT_SUPPORTED
) {
1741 LOG_ERROR("Channel not supported by the device");
1743 } else if (ret
!= JAYLINK_OK
) {
1744 LOG_ERROR("Failed to write to channel: %s", jaylink_strerror(ret
));
1748 if (length
!= (tmp
/ 2))
1749 LOG_WARNING("Only %" PRIu32
" bytes written to the channel", length
);
1754 COMMAND_HANDLER(jlink_handle_emucom_read_command
)
1763 return ERROR_COMMAND_SYNTAX_ERROR
;
1765 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_EMUCOM
)) {
1766 LOG_ERROR("Device does not support EMUCOM");
1770 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], channel
);
1771 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
1773 buf
= malloc(length
* 3 + 1);
1776 LOG_ERROR("Failed to allocate buffer");
1780 ret
= jaylink_emucom_read(devh
, channel
, buf
, &length
);
1782 if (ret
== JAYLINK_ERR_DEV_NOT_SUPPORTED
) {
1783 LOG_ERROR("Channel is not supported by the device");
1786 } else if (ret
== JAYLINK_ERR_DEV_NOT_AVAILABLE
) {
1787 LOG_ERROR("Channel is not available for the requested amount of data. "
1788 "%" PRIu32
" bytes are available", length
);
1791 } else if (ret
!= JAYLINK_OK
) {
1792 LOG_ERROR("Failed to read from channel: %s", jaylink_strerror(ret
));
1797 tmp
= hexify((char *)buf
+ length
, buf
, length
, 2 * length
+ 1);
1799 if (tmp
!= 2 * length
) {
1800 LOG_ERROR("Failed to convert data into hexadecimal string");
1805 command_print(CMD
, "%s", buf
+ length
);
1811 static const struct command_registration jlink_config_subcommand_handlers
[] = {
1814 .handler
= &jlink_handle_config_usb_address_command
,
1815 .mode
= COMMAND_EXEC
,
1816 .help
= "set the USB address",
1820 .name
= "targetpower",
1821 .handler
= &jlink_handle_config_target_power_command
,
1822 .mode
= COMMAND_EXEC
,
1823 .help
= "set the target power supply",
1828 .handler
= &jlink_handle_config_mac_address_command
,
1829 .mode
= COMMAND_EXEC
,
1830 .help
= "set the MAC Address",
1831 .usage
= "[ff:ff:ff:ff:ff:ff]",
1835 .handler
= &jlink_handle_config_ip_address_command
,
1836 .mode
= COMMAND_EXEC
,
1837 .help
= "set the IP address, where A.B.C.D is the IP address, "
1838 "E the bit of the subnet mask, F.G.H.I the subnet mask",
1839 .usage
= "[A.B.C.D[/E] [F.G.H.I]]",
1843 .handler
= &jlink_handle_config_reset_command
,
1844 .mode
= COMMAND_EXEC
,
1845 .help
= "undo configuration changes",
1850 .handler
= &jlink_handle_config_write_command
,
1851 .mode
= COMMAND_EXEC
,
1852 .help
= "write configuration to the device",
1855 COMMAND_REGISTRATION_DONE
1858 static const struct command_registration jlink_emucom_subcommand_handlers
[] = {
1861 .handler
= &jlink_handle_emucom_write_command
,
1862 .mode
= COMMAND_EXEC
,
1863 .help
= "write to a channel",
1864 .usage
= "<channel> <data>",
1868 .handler
= &jlink_handle_emucom_read_command
,
1869 .mode
= COMMAND_EXEC
,
1870 .help
= "read from a channel",
1871 .usage
= "<channel> <length>"
1873 COMMAND_REGISTRATION_DONE
1876 static const struct command_registration jlink_subcommand_handlers
[] = {
1879 .handler
= &jlink_handle_jlink_jtag_command
,
1880 .mode
= COMMAND_EXEC
,
1881 .help
= "select the JTAG command version",
1885 .name
= "targetpower",
1886 .handler
= &jlink_handle_target_power_command
,
1887 .mode
= COMMAND_EXEC
,
1888 .help
= "set the target power supply",
1893 .handler
= &jlink_handle_free_memory_command
,
1894 .mode
= COMMAND_EXEC
,
1895 .help
= "show free device memory",
1900 .handler
= &jlink_handle_hwstatus_command
,
1901 .mode
= COMMAND_EXEC
,
1902 .help
= "show the hardware status",
1907 .handler
= &jlink_usb_command
,
1908 .mode
= COMMAND_CONFIG
,
1909 .help
= "set the USB address of the device that should be used",
1914 .handler
= &jlink_handle_config_command
,
1915 .mode
= COMMAND_EXEC
,
1916 .help
= "access the device configuration. If no argument is given "
1917 "this will show the device configuration",
1918 .chain
= jlink_config_subcommand_handlers
,
1923 .mode
= COMMAND_EXEC
,
1924 .help
= "access EMUCOM channel",
1925 .chain
= jlink_emucom_subcommand_handlers
,
1928 COMMAND_REGISTRATION_DONE
1931 static const struct command_registration jlink_command_handlers
[] = {
1934 .mode
= COMMAND_ANY
,
1935 .help
= "perform jlink management",
1936 .chain
= jlink_subcommand_handlers
,
1939 COMMAND_REGISTRATION_DONE
1942 static int jlink_swd_init(void)
1944 iface
= JAYLINK_TIF_SWD
;
1949 static void jlink_swd_write_reg(uint8_t cmd
, uint32_t value
, uint32_t ap_delay_clk
)
1951 assert(!(cmd
& SWD_CMD_RNW
));
1952 jlink_swd_queue_cmd(cmd
, NULL
, value
, ap_delay_clk
);
1955 static void jlink_swd_read_reg(uint8_t cmd
, uint32_t *value
, uint32_t ap_delay_clk
)
1957 assert(cmd
& SWD_CMD_RNW
);
1958 jlink_swd_queue_cmd(cmd
, value
, 0, ap_delay_clk
);
1961 /***************************************************************************/
1962 /* J-Link tap functions */
1964 static unsigned tap_length
;
1965 /* In SWD mode use tms buffer for direction control */
1966 static uint8_t tms_buffer
[JLINK_TAP_BUFFER_SIZE
];
1967 static uint8_t tdi_buffer
[JLINK_TAP_BUFFER_SIZE
];
1968 static uint8_t tdo_buffer
[JLINK_TAP_BUFFER_SIZE
];
1970 struct pending_scan_result
{
1971 /** First bit position in tdo_buffer to read. */
1973 /** Number of bits to read. */
1975 /** Location to store the result */
1977 /** Offset in the destination buffer */
1978 unsigned buffer_offset
;
1981 #define MAX_PENDING_SCAN_RESULTS 256
1983 static int pending_scan_results_length
;
1984 static struct pending_scan_result pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
1986 static void jlink_tap_init(void)
1989 pending_scan_results_length
= 0;
1990 memset(tms_buffer
, 0, sizeof(tms_buffer
));
1991 memset(tdi_buffer
, 0, sizeof(tdi_buffer
));
1994 static void jlink_clock_data(const uint8_t *out
, unsigned out_offset
,
1995 const uint8_t *tms_out
, unsigned tms_offset
,
1996 uint8_t *in
, unsigned in_offset
,
2000 unsigned available_length
= JLINK_TAP_BUFFER_SIZE
- tap_length
/ 8;
2002 if (!available_length
||
2003 (in
&& pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
)) {
2004 if (jlink_flush() != ERROR_OK
)
2006 available_length
= JLINK_TAP_BUFFER_SIZE
;
2009 struct pending_scan_result
*pending_scan_result
=
2010 &pending_scan_results_buffer
[pending_scan_results_length
];
2012 unsigned scan_length
= length
> available_length
?
2013 available_length
: length
;
2016 buf_set_buf(out
, out_offset
, tdi_buffer
, tap_length
, scan_length
);
2018 buf_set_buf(tms_out
, tms_offset
, tms_buffer
, tap_length
, scan_length
);
2021 pending_scan_result
->first
= tap_length
;
2022 pending_scan_result
->length
= scan_length
;
2023 pending_scan_result
->buffer
= in
;
2024 pending_scan_result
->buffer_offset
= in_offset
;
2025 pending_scan_results_length
++;
2028 tap_length
+= scan_length
;
2029 out_offset
+= scan_length
;
2030 tms_offset
+= scan_length
;
2031 in_offset
+= scan_length
;
2032 length
-= scan_length
;
2033 } while (length
> 0);
2036 static int jlink_flush(void)
2044 jlink_last_state
= jtag_debug_state_machine(tms_buffer
, tdi_buffer
,
2045 tap_length
, jlink_last_state
);
2047 ret
= jaylink_jtag_io(devh
, tms_buffer
, tdi_buffer
, tdo_buffer
,
2048 tap_length
, jtag_command_version
);
2050 if (ret
!= JAYLINK_OK
) {
2051 LOG_ERROR("jaylink_jtag_io() failed: %s", jaylink_strerror(ret
));
2053 return ERROR_JTAG_QUEUE_FAILED
;
2056 for (i
= 0; i
< pending_scan_results_length
; i
++) {
2057 struct pending_scan_result
*p
= &pending_scan_results_buffer
[i
];
2059 buf_set_buf(tdo_buffer
, p
->first
, p
->buffer
,
2060 p
->buffer_offset
, p
->length
);
2062 LOG_DEBUG_IO("Pending scan result, length = %d", p
->length
);
2070 static void fill_buffer(uint8_t *buf
, uint32_t val
, uint32_t len
)
2072 unsigned int tap_pos
= tap_length
;
2075 buf_set_u32(buf
, tap_pos
, 32, val
);
2081 buf_set_u32(buf
, tap_pos
, len
, val
);
2084 static void jlink_queue_data_out(const uint8_t *data
, uint32_t len
)
2086 const uint32_t dir_out
= 0xffffffff;
2089 bit_copy(tdi_buffer
, tap_length
, data
, 0, len
);
2091 fill_buffer(tdi_buffer
, 0, len
);
2093 fill_buffer(tms_buffer
, dir_out
, len
);
2097 static void jlink_queue_data_in(uint32_t len
)
2099 const uint32_t dir_in
= 0;
2101 fill_buffer(tms_buffer
, dir_in
, len
);
2105 static int jlink_swd_switch_seq(enum swd_special_seq seq
)
2112 LOG_DEBUG("SWD line reset");
2113 s
= swd_seq_line_reset
;
2114 s_len
= swd_seq_line_reset_len
;
2117 LOG_DEBUG("JTAG-to-SWD");
2118 s
= swd_seq_jtag_to_swd
;
2119 s_len
= swd_seq_jtag_to_swd_len
;
2121 case JTAG_TO_DORMANT
:
2122 LOG_DEBUG("JTAG-to-DORMANT");
2123 s
= swd_seq_jtag_to_dormant
;
2124 s_len
= swd_seq_jtag_to_dormant_len
;
2127 LOG_DEBUG("SWD-to-JTAG");
2128 s
= swd_seq_swd_to_jtag
;
2129 s_len
= swd_seq_swd_to_jtag_len
;
2131 case SWD_TO_DORMANT
:
2132 LOG_DEBUG("SWD-to-DORMANT");
2133 s
= swd_seq_swd_to_dormant
;
2134 s_len
= swd_seq_swd_to_dormant_len
;
2136 case DORMANT_TO_SWD
:
2137 LOG_DEBUG("DORMANT-to-SWD");
2138 s
= swd_seq_dormant_to_swd
;
2139 s_len
= swd_seq_dormant_to_swd_len
;
2141 case DORMANT_TO_JTAG
:
2142 LOG_DEBUG("DORMANT-to-JTAG");
2143 s
= swd_seq_dormant_to_jtag
;
2144 s_len
= swd_seq_dormant_to_jtag_len
;
2147 LOG_ERROR("Sequence %d not supported", seq
);
2151 jlink_queue_data_out(s
, s_len
);
2156 static int jlink_swd_run_queue(void)
2161 LOG_DEBUG("Executing %d queued transactions", pending_scan_results_length
);
2163 if (queued_retval
!= ERROR_OK
) {
2164 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval
);
2169 * A transaction must be followed by another transaction or at least 8 idle
2170 * cycles to ensure that data is clocked through the AP.
2172 jlink_queue_data_out(NULL
, 8);
2174 ret
= jaylink_swd_io(devh
, tms_buffer
, tdi_buffer
, tdo_buffer
, tap_length
);
2176 if (ret
!= JAYLINK_OK
) {
2177 LOG_ERROR("jaylink_swd_io() failed: %s", jaylink_strerror(ret
));
2181 for (i
= 0; i
< pending_scan_results_length
; i
++) {
2182 int ack
= buf_get_u32(tdo_buffer
, pending_scan_results_buffer
[i
].first
, 3);
2184 if (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
= ack
== SWD_ACK_WAIT
? ERROR_WAIT
: ERROR_FAIL
;
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 cmd
|= SWD_CMD_START
| SWD_CMD_PARK
;
2226 jlink_queue_data_out(&cmd
, 8);
2228 pending_scan_results_buffer
[pending_scan_results_length
].first
= tap_length
;
2230 if (cmd
& SWD_CMD_RNW
) {
2231 /* Queue a read transaction. */
2232 pending_scan_results_buffer
[pending_scan_results_length
].length
= 32;
2233 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= dst
;
2235 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2237 /* Queue a write transaction. */
2238 pending_scan_results_buffer
[pending_scan_results_length
].length
= 0;
2239 jlink_queue_data_in(1 + 3 + 1);
2241 buf_set_u32(data_parity_trn
, 0, 32, data
);
2242 buf_set_u32(data_parity_trn
, 32, 1, parity_u32(data
));
2244 jlink_queue_data_out(data_parity_trn
, 32 + 1);
2247 pending_scan_results_length
++;
2249 /* Insert idle cycles after AP accesses to avoid WAIT. */
2250 if (cmd
& SWD_CMD_APNDP
)
2251 jlink_queue_data_out(NULL
, ap_delay_clk
);
2254 static const struct swd_driver jlink_swd
= {
2255 .init
= &jlink_swd_init
,
2256 .switch_seq
= &jlink_swd_switch_seq
,
2257 .read_reg
= &jlink_swd_read_reg
,
2258 .write_reg
= &jlink_swd_write_reg
,
2259 .run
= &jlink_swd_run_queue
,
2262 static const char * const jlink_transports
[] = { "jtag", "swd", NULL
};
2264 static struct jtag_interface jlink_interface
= {
2265 .execute_queue
= &jlink_execute_queue
,
2268 struct adapter_driver jlink_adapter_driver
= {
2270 .transports
= jlink_transports
,
2271 .commands
= jlink_command_handlers
,
2273 .init
= &jlink_init
,
2274 .quit
= &jlink_quit
,
2275 .reset
= &jlink_reset_safe
,
2276 .speed
= &jlink_speed
,
2278 .speed_div
= &jlink_speed_div
,
2279 .config_trace
= &config_trace
,
2280 .poll_trace
= &poll_trace
,
2282 .jtag_ops
= &jlink_interface
,
2283 .swd_ops
= &jlink_swd
,