1 /***************************************************************************
2 * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
3 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * Copyright (C) 2011 by Jean-Christophe PLAGNIOL-VIILARD *
9 * plagnioj@jcrosoft.com *
11 * Copyright (C) 2015 by Marc Schink *
12 * openocd-dev@marcschink.de *
14 * Copyright (C) 2015 by Paul Fertser *
15 * fercerpav@gmail.com *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
29 ***************************************************************************/
38 #include <jtag/interface.h>
40 #include <jtag/commands.h>
41 #include <jtag/drivers/jtag_usb_common.h>
42 #include <target/cortex_m.h>
44 #include <libjaylink/libjaylink.h>
46 static struct jaylink_context
*jayctx
;
47 static struct jaylink_device_handle
*devh
;
48 static struct jaylink_connection conn
;
49 static struct jaylink_connection connlist
[JAYLINK_MAX_CONNECTIONS
];
50 static enum jaylink_jtag_version jtag_command_version
;
51 static uint8_t caps
[JAYLINK_DEV_EXT_CAPS_SIZE
];
53 static uint32_t serial_number
;
54 static bool use_serial_number
;
55 static bool use_usb_location
;
56 static enum jaylink_usb_address usb_address
;
57 static bool use_usb_address
;
58 static enum jaylink_target_interface iface
= JAYLINK_TIF_JTAG
;
59 static bool trace_enabled
;
61 #define JLINK_MAX_SPEED 12000
62 #define JLINK_TAP_BUFFER_SIZE 2048
64 static unsigned int swd_buffer_size
= JLINK_TAP_BUFFER_SIZE
;
66 /* Maximum SWO frequency deviation. */
67 #define SWO_MAX_FREQ_DEV 0.03
69 /* 256 byte non-volatile memory */
70 struct device_config
{
72 /* 0ffset 0x01 to 0x03 */
73 uint8_t reserved_1
[3];
74 uint32_t target_power
;
75 /* 0ffset 0x08 to 0x1f */
76 uint8_t reserved_2
[24];
77 /* IP only for J-Link Pro */
78 uint8_t ip_address
[4];
79 uint8_t subnet_mask
[4];
80 /* 0ffset 0x28 to 0x2f */
81 uint8_t reserved_3
[8];
82 uint8_t mac_address
[6];
83 /* 0ffset 0x36 to 0xff */
84 uint8_t reserved_4
[202];
85 } __attribute__ ((packed
));
87 static struct device_config config
;
88 static struct device_config tmp_config
;
90 /* Queue command functions */
91 static void jlink_end_state(tap_state_t state
);
92 static void jlink_state_move(void);
93 static void jlink_path_move(int num_states
, tap_state_t
*path
);
94 static void jlink_stableclocks(int num_cycles
);
95 static void jlink_runtest(int num_cycles
);
96 static void jlink_reset(int trst
, int srst
);
97 static int jlink_reset_safe(int trst
, int srst
);
98 static int jlink_swd_run_queue(void);
99 static void jlink_swd_queue_cmd(uint8_t cmd
, uint32_t *dst
, uint32_t data
, uint32_t ap_delay_clk
);
100 static int jlink_swd_switch_seq(enum swd_special_seq seq
);
102 /* J-Link tap buffer functions */
103 static void jlink_tap_init(void);
104 static int jlink_flush(void);
106 * Queue data to go out and in, flushing the queue as many times as
109 * @param out A pointer to TDI data, if NULL, old stale data will be used.
110 * @param out_offset A bit offset for TDI data.
111 * @param tms_out A pointer to TMS data, if NULL, zeroes will be emitted.
112 * @param tms_offset A bit offset for TMS data.
113 * @param in A pointer to store TDO data to, if NULL the data will be discarded.
114 * @param in_offset A bit offset for TDO data.
115 * @param length Amount of bits to transfer out and in.
117 * @retval This function doesn't return any value.
119 static void jlink_clock_data(const uint8_t *out
, unsigned out_offset
,
120 const uint8_t *tms_out
, unsigned tms_offset
,
121 uint8_t *in
, unsigned in_offset
,
124 static enum tap_state jlink_last_state
= TAP_RESET
;
125 static int queued_retval
;
127 /***************************************************************************/
128 /* External interface implementation */
130 static void jlink_execute_stableclocks(struct jtag_command
*cmd
)
132 LOG_DEBUG_IO("stableclocks %i cycles", cmd
->cmd
.runtest
->num_cycles
);
133 jlink_stableclocks(cmd
->cmd
.runtest
->num_cycles
);
136 static void jlink_execute_runtest(struct jtag_command
*cmd
)
138 LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd
->cmd
.runtest
->num_cycles
,
139 cmd
->cmd
.runtest
->end_state
);
141 jlink_end_state(cmd
->cmd
.runtest
->end_state
);
142 jlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
145 static void jlink_execute_statemove(struct jtag_command
*cmd
)
147 LOG_DEBUG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
149 jlink_end_state(cmd
->cmd
.statemove
->end_state
);
153 static void jlink_execute_pathmove(struct jtag_command
*cmd
)
155 LOG_DEBUG_IO("pathmove: %i states, end in %i",
156 cmd
->cmd
.pathmove
->num_states
,
157 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
159 jlink_path_move(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
162 static void jlink_execute_scan(struct jtag_command
*cmd
)
164 LOG_DEBUG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN",
165 jtag_scan_type(cmd
->cmd
.scan
));
167 /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
168 while (cmd
->cmd
.scan
->num_fields
> 0
169 && cmd
->cmd
.scan
->fields
[cmd
->cmd
.scan
->num_fields
- 1].num_bits
== 0) {
170 cmd
->cmd
.scan
->num_fields
--;
171 LOG_DEBUG("discarding trailing empty field");
174 if (cmd
->cmd
.scan
->num_fields
== 0) {
175 LOG_DEBUG("empty scan, doing nothing");
179 if (cmd
->cmd
.scan
->ir_scan
) {
180 if (tap_get_state() != TAP_IRSHIFT
) {
181 jlink_end_state(TAP_IRSHIFT
);
185 if (tap_get_state() != TAP_DRSHIFT
) {
186 jlink_end_state(TAP_DRSHIFT
);
191 jlink_end_state(cmd
->cmd
.scan
->end_state
);
193 struct scan_field
*field
= cmd
->cmd
.scan
->fields
;
194 unsigned scan_size
= 0;
196 for (int i
= 0; i
< cmd
->cmd
.scan
->num_fields
; i
++, field
++) {
197 scan_size
+= field
->num_bits
;
198 LOG_DEBUG_IO("%s%s field %d/%d %d bits",
199 field
->in_value
? "in" : "",
200 field
->out_value
? "out" : "",
202 cmd
->cmd
.scan
->num_fields
,
205 if (i
== cmd
->cmd
.scan
->num_fields
- 1 && tap_get_state() != tap_get_end_state()) {
206 /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
207 * movement. This last field can't have length zero, it was checked above. */
208 jlink_clock_data(field
->out_value
,
214 field
->num_bits
- 1);
215 uint8_t last_bit
= 0;
216 if (field
->out_value
)
217 bit_copy(&last_bit
, 0, field
->out_value
, field
->num_bits
- 1, 1);
218 uint8_t tms_bits
= 0x01;
219 jlink_clock_data(&last_bit
,
226 tap_set_state(tap_state_transition(tap_get_state(), 1));
227 jlink_clock_data(NULL
,
234 tap_set_state(tap_state_transition(tap_get_state(), 0));
236 jlink_clock_data(field
->out_value
,
245 if (tap_get_state() != tap_get_end_state()) {
246 jlink_end_state(tap_get_end_state());
250 LOG_DEBUG_IO("%s scan, %i bits, end in %s",
251 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
252 tap_state_name(tap_get_end_state()));
255 static void jlink_execute_sleep(struct jtag_command
*cmd
)
257 LOG_DEBUG_IO("sleep %" PRIi32
"", cmd
->cmd
.sleep
->us
);
259 jtag_sleep(cmd
->cmd
.sleep
->us
);
262 static int jlink_execute_command(struct jtag_command
*cmd
)
265 case JTAG_STABLECLOCKS
:
266 jlink_execute_stableclocks(cmd
);
269 jlink_execute_runtest(cmd
);
272 jlink_execute_statemove(cmd
);
275 jlink_execute_pathmove(cmd
);
278 jlink_execute_scan(cmd
);
281 jlink_execute_sleep(cmd
);
284 LOG_ERROR("BUG: Unknown JTAG command type encountered.");
285 return ERROR_JTAG_QUEUE_FAILED
;
291 static int jlink_execute_queue(void)
294 struct jtag_command
*cmd
= jtag_command_queue
;
296 while (cmd
!= NULL
) {
297 ret
= jlink_execute_command(cmd
);
305 return jlink_flush();
308 static int jlink_speed(int speed
)
311 struct jaylink_speed tmp
;
314 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_SPEEDS
)) {
315 ret
= jaylink_get_speeds(devh
, &tmp
);
317 if (ret
!= JAYLINK_OK
) {
318 LOG_ERROR("jaylink_get_speeds() failed: %s.",
319 jaylink_strerror(ret
));
320 return ERROR_JTAG_DEVICE_ERROR
;
324 max_speed
= tmp
.freq
/ tmp
.div
;
326 max_speed
= JLINK_MAX_SPEED
;
330 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING
)) {
331 LOG_ERROR("Adaptive clocking is not supported by the device.");
332 return ERROR_JTAG_NOT_IMPLEMENTED
;
335 speed
= JAYLINK_SPEED_ADAPTIVE_CLOCKING
;
336 } else if (speed
> max_speed
) {
337 LOG_INFO("Reduced speed from %d kHz to %d kHz (maximum).", speed
,
342 ret
= jaylink_set_speed(devh
, speed
);
344 if (ret
!= JAYLINK_OK
) {
345 LOG_ERROR("jaylink_set_speed() failed: %s.",
346 jaylink_strerror(ret
));
347 return ERROR_JTAG_DEVICE_ERROR
;
353 static int jlink_speed_div(int speed
, int *khz
)
360 static int jlink_khz(int khz
, int *jtag_speed
)
367 static bool read_device_config(struct device_config
*cfg
)
371 ret
= jaylink_read_raw_config(devh
, (uint8_t *)cfg
);
373 if (ret
!= JAYLINK_OK
) {
374 LOG_ERROR("jaylink_read_raw_config() failed: %s.",
375 jaylink_strerror(ret
));
379 if (cfg
->usb_address
== 0xff)
380 cfg
->usb_address
= 0x00;
382 if (cfg
->target_power
== 0xffffffff)
383 cfg
->target_power
= 0;
388 static int select_interface(void)
393 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SELECT_TIF
)) {
394 if (iface
!= JAYLINK_TIF_JTAG
) {
395 LOG_ERROR("Device supports JTAG transport only.");
396 return ERROR_JTAG_INIT_FAILED
;
402 ret
= jaylink_get_available_interfaces(devh
, &interfaces
);
404 if (ret
!= JAYLINK_OK
) {
405 LOG_ERROR("jaylink_get_available_interfaces() failed: %s.",
406 jaylink_strerror(ret
));
407 return ERROR_JTAG_INIT_FAILED
;
410 if (!(interfaces
& (1 << iface
))) {
411 LOG_ERROR("Selected transport is not supported by the device.");
412 return ERROR_JTAG_INIT_FAILED
;
415 ret
= jaylink_select_interface(devh
, iface
, NULL
);
418 LOG_ERROR("jaylink_select_interface() failed: %s.",
419 jaylink_strerror(ret
));
420 return ERROR_JTAG_INIT_FAILED
;
426 static int jlink_register(void)
433 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_REGISTER
))
436 ret
= jaylink_register(devh
, &conn
, connlist
, &count
);
438 if (ret
!= JAYLINK_OK
) {
439 LOG_ERROR("jaylink_register() failed: %s.", jaylink_strerror(ret
));
443 handle_found
= false;
445 for (i
= 0; i
< count
; i
++) {
446 if (connlist
[i
].handle
== conn
.handle
) {
453 LOG_ERROR("Registration failed: maximum number of connections on the "
462 * Adjust the SWD transaction buffer size depending on the free device internal
463 * memory. This ensures that the SWD transactions sent to the device do not
464 * exceed the internal memory of the device.
466 static bool adjust_swd_buffer_size(void)
471 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
))
474 ret
= jaylink_get_free_memory(devh
, &tmp
);
476 if (ret
!= JAYLINK_OK
) {
477 LOG_ERROR("jaylink_get_free_memory() failed: %s.",
478 jaylink_strerror(ret
));
483 LOG_ERROR("Not enough free device internal memory: %u bytes.", tmp
);
487 tmp
= MIN(JLINK_TAP_BUFFER_SIZE
, (tmp
- 16) / 2);
489 if (tmp
!= swd_buffer_size
) {
490 swd_buffer_size
= tmp
;
491 LOG_DEBUG("Adjusted SWD transaction buffer size to %u bytes.",
498 static int jaylink_log_handler(const struct jaylink_context
*ctx
,
499 enum jaylink_log_level level
, const char *format
, va_list args
,
505 case JAYLINK_LOG_LEVEL_ERROR
:
508 case JAYLINK_LOG_LEVEL_WARNING
:
509 tmp
= LOG_LVL_WARNING
;
512 * Forward info messages to the debug output because they are more verbose
513 * than info messages of OpenOCD.
515 case JAYLINK_LOG_LEVEL_INFO
:
516 case JAYLINK_LOG_LEVEL_DEBUG
:
519 case JAYLINK_LOG_LEVEL_DEBUG_IO
:
520 tmp
= LOG_LVL_DEBUG_IO
;
523 tmp
= LOG_LVL_WARNING
;
526 log_vprintf_lf(tmp
, __FILE__
, __LINE__
, __func__
, format
, args
);
531 static bool jlink_usb_location_equal(struct jaylink_device
*dev
)
539 retval
= jaylink_device_get_usb_bus_ports(dev
, &bus
, &ports
, &num_ports
);
541 if (retval
== JAYLINK_ERR_NOT_SUPPORTED
) {
543 } else if (retval
!= JAYLINK_OK
) {
544 LOG_WARNING("jaylink_device_get_usb_bus_ports() failed: %s.",
545 jaylink_strerror(retval
));
549 equal
= jtag_usb_location_equal(bus
, ports
, num_ports
);
556 static int jlink_init(void)
559 struct jaylink_device
**devs
;
563 char *firmware_version
;
564 struct jaylink_hardware_version hwver
;
565 struct jaylink_hardware_status hwstatus
;
566 enum jaylink_usb_address address
;
569 uint32_t host_interfaces
;
571 LOG_DEBUG("Using libjaylink %s (compiled with %s).",
572 jaylink_version_package_get_string(), JAYLINK_VERSION_PACKAGE_STRING
);
574 if (!jaylink_library_has_cap(JAYLINK_CAP_HIF_USB
) && use_usb_address
) {
575 LOG_ERROR("J-Link driver does not support USB devices.");
576 return ERROR_JTAG_INIT_FAILED
;
579 ret
= jaylink_init(&jayctx
);
581 if (ret
!= JAYLINK_OK
) {
582 LOG_ERROR("jaylink_init() failed: %s.", jaylink_strerror(ret
));
583 return ERROR_JTAG_INIT_FAILED
;
586 ret
= jaylink_log_set_callback(jayctx
, &jaylink_log_handler
, NULL
);
588 if (ret
!= JAYLINK_OK
) {
589 LOG_ERROR("jaylink_log_set_callback() failed: %s.",
590 jaylink_strerror(ret
));
591 jaylink_exit(jayctx
);
592 return ERROR_JTAG_INIT_FAILED
;
595 host_interfaces
= JAYLINK_HIF_USB
;
597 if (use_serial_number
)
598 host_interfaces
|= JAYLINK_HIF_TCP
;
600 ret
= jaylink_discovery_scan(jayctx
, host_interfaces
);
602 if (ret
!= JAYLINK_OK
) {
603 LOG_ERROR("jaylink_discovery_scan() failed: %s.",
604 jaylink_strerror(ret
));
605 jaylink_exit(jayctx
);
606 return ERROR_JTAG_INIT_FAILED
;
609 ret
= jaylink_get_devices(jayctx
, &devs
, &num_devices
);
611 if (ret
!= JAYLINK_OK
) {
612 LOG_ERROR("jaylink_get_devices() failed: %s.", jaylink_strerror(ret
));
613 jaylink_exit(jayctx
);
614 return ERROR_JTAG_INIT_FAILED
;
617 use_usb_location
= (jtag_usb_get_location() != NULL
);
619 if (!use_serial_number
&& !use_usb_address
&& !use_usb_location
&& num_devices
> 1) {
620 LOG_ERROR("Multiple devices found, specify the desired device.");
621 jaylink_free_devices(devs
, true);
622 jaylink_exit(jayctx
);
623 return ERROR_JTAG_INIT_FAILED
;
626 found_device
= false;
628 for (i
= 0; devs
[i
]; i
++) {
629 struct jaylink_device
*dev
= devs
[i
];
631 if (use_serial_number
) {
632 ret
= jaylink_device_get_serial_number(dev
, &tmp
);
634 if (ret
== JAYLINK_ERR_NOT_AVAILABLE
) {
636 } else if (ret
!= JAYLINK_OK
) {
637 LOG_WARNING("jaylink_device_get_serial_number() failed: %s.",
638 jaylink_strerror(ret
));
642 if (serial_number
!= tmp
)
646 if (use_usb_address
) {
647 ret
= jaylink_device_get_usb_address(dev
, &address
);
649 if (ret
== JAYLINK_ERR_NOT_SUPPORTED
) {
651 } else if (ret
!= JAYLINK_OK
) {
652 LOG_WARNING("jaylink_device_get_usb_address() failed: %s.",
653 jaylink_strerror(ret
));
657 if (usb_address
!= address
)
661 if (use_usb_location
&& !jlink_usb_location_equal(dev
))
664 ret
= jaylink_open(dev
, &devh
);
666 if (ret
== JAYLINK_OK
) {
671 LOG_ERROR("Failed to open device: %s.", jaylink_strerror(ret
));
674 jaylink_free_devices(devs
, true);
677 LOG_ERROR("No J-Link device found.");
678 jaylink_exit(jayctx
);
679 return ERROR_JTAG_INIT_FAILED
;
683 * Be careful with changing the following initialization sequence because
684 * some devices are known to be sensitive regarding the order.
687 ret
= jaylink_get_firmware_version(devh
, &firmware_version
, &length
);
689 if (ret
!= JAYLINK_OK
) {
690 LOG_ERROR("jaylink_get_firmware_version() failed: %s.",
691 jaylink_strerror(ret
));
693 jaylink_exit(jayctx
);
694 return ERROR_JTAG_INIT_FAILED
;
695 } else if (length
> 0) {
696 LOG_INFO("%s", firmware_version
);
697 free(firmware_version
);
699 LOG_WARNING("Device responds empty firmware version string.");
702 memset(caps
, 0, JAYLINK_DEV_EXT_CAPS_SIZE
);
703 ret
= jaylink_get_caps(devh
, caps
);
705 if (ret
!= JAYLINK_OK
) {
706 LOG_ERROR("jaylink_get_caps() failed: %s.", jaylink_strerror(ret
));
708 jaylink_exit(jayctx
);
709 return ERROR_JTAG_INIT_FAILED
;
712 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_EXT_CAPS
)) {
713 ret
= jaylink_get_extended_caps(devh
, caps
);
715 if (ret
!= JAYLINK_OK
) {
716 LOG_ERROR("jaylink_get_extended_caps() failed: %s.",
717 jaylink_strerror(ret
));
719 jaylink_exit(jayctx
);
720 return ERROR_JTAG_INIT_FAILED
;
724 jtag_command_version
= JAYLINK_JTAG_VERSION_2
;
726 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_HW_VERSION
)) {
727 ret
= jaylink_get_hardware_version(devh
, &hwver
);
729 if (ret
!= JAYLINK_OK
) {
730 LOG_ERROR("Failed to retrieve hardware version: %s.",
731 jaylink_strerror(ret
));
733 jaylink_exit(jayctx
);
734 return ERROR_JTAG_INIT_FAILED
;
737 LOG_INFO("Hardware version: %u.%02u", hwver
.major
, hwver
.minor
);
739 if (hwver
.major
>= 5)
740 jtag_command_version
= JAYLINK_JTAG_VERSION_3
;
743 if (iface
== JAYLINK_TIF_SWD
) {
745 * Adjust the SWD transaction buffer size in case there is already
746 * allocated memory on the device. This happens for example if the
747 * memory for SWO capturing is still allocated because the software
748 * which used the device before has not been shut down properly.
750 if (!adjust_swd_buffer_size()) {
752 jaylink_exit(jayctx
);
753 return ERROR_JTAG_INIT_FAILED
;
757 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
758 if (!read_device_config(&config
)) {
759 LOG_ERROR("Failed to read device configuration data.");
761 jaylink_exit(jayctx
);
762 return ERROR_JTAG_INIT_FAILED
;
765 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
768 ret
= jaylink_get_hardware_status(devh
, &hwstatus
);
770 if (ret
!= JAYLINK_OK
) {
771 LOG_ERROR("jaylink_get_hardware_status() failed: %s.",
772 jaylink_strerror(ret
));
774 jaylink_exit(jayctx
);
775 return ERROR_JTAG_INIT_FAILED
;
778 LOG_INFO("VTarget = %u.%03u V", hwstatus
.target_voltage
/ 1000,
779 hwstatus
.target_voltage
% 1000);
783 strcpy(conn
.hid
, "0.0.0.0");
787 ret
= jlink_register();
789 if (ret
!= ERROR_OK
) {
791 jaylink_exit(jayctx
);
792 return ERROR_JTAG_INIT_FAILED
;
795 ret
= select_interface();
797 if (ret
!= ERROR_OK
) {
799 jaylink_exit(jayctx
);
807 jlink_speed(jtag_get_speed_khz());
809 if (iface
== JAYLINK_TIF_JTAG
) {
811 * J-Link devices with firmware version v5 and v6 seems to have an issue
812 * if the first tap move is not divisible by 8, so we send a TLR on
816 jlink_clock_data(NULL
, 0, &tms
, 0, NULL
, 0, 8);
824 static int jlink_quit(void)
830 ret
= jaylink_swo_stop(devh
);
832 if (ret
!= JAYLINK_OK
)
833 LOG_ERROR("jaylink_swo_stop() failed: %s.", jaylink_strerror(ret
));
836 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_REGISTER
)) {
837 ret
= jaylink_unregister(devh
, &conn
, connlist
, &count
);
839 if (ret
!= JAYLINK_OK
)
840 LOG_ERROR("jaylink_unregister() failed: %s.",
841 jaylink_strerror(ret
));
845 jaylink_exit(jayctx
);
850 /***************************************************************************/
851 /* Queue command implementations */
853 static void jlink_end_state(tap_state_t state
)
855 if (tap_is_state_stable(state
))
856 tap_set_end_state(state
);
858 LOG_ERROR("BUG: %i is not a valid end state", state
);
863 /* Goes to the end state. */
864 static void jlink_state_move(void)
867 uint8_t tms_scan_bits
;
869 tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
870 tms_scan_bits
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
872 jlink_clock_data(NULL
, 0, &tms_scan
, 0, NULL
, 0, tms_scan_bits
);
874 tap_set_state(tap_get_end_state());
877 static void jlink_path_move(int num_states
, tap_state_t
*path
)
882 for (i
= 0; i
< num_states
; i
++) {
883 if (path
[i
] == tap_state_transition(tap_get_state(), false))
884 jlink_clock_data(NULL
, 0, NULL
, 0, NULL
, 0, 1);
885 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
886 jlink_clock_data(NULL
, 0, &tms
, 0, NULL
, 0, 1);
888 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
889 tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
893 tap_set_state(path
[i
]);
896 tap_set_end_state(tap_get_state());
899 static void jlink_stableclocks(int num_cycles
)
903 uint8_t tms
= tap_get_state() == TAP_RESET
;
904 /* Execute num_cycles. */
905 for (i
= 0; i
< num_cycles
; i
++)
906 jlink_clock_data(NULL
, 0, &tms
, 0, NULL
, 0, 1);
909 static void jlink_runtest(int num_cycles
)
911 tap_state_t saved_end_state
= tap_get_end_state();
913 /* Only do a state_move when we're not already in IDLE. */
914 if (tap_get_state() != TAP_IDLE
) {
915 jlink_end_state(TAP_IDLE
);
920 jlink_stableclocks(num_cycles
);
922 /* Finish in end_state. */
923 jlink_end_state(saved_end_state
);
925 if (tap_get_state() != tap_get_end_state())
929 static void jlink_reset(int trst
, int srst
)
931 LOG_DEBUG("TRST: %i, SRST: %i.", trst
, srst
);
933 /* Signals are active low. */
935 jaylink_set_reset(devh
);
938 jaylink_clear_reset(devh
);
941 jaylink_jtag_clear_trst(devh
);
944 jaylink_jtag_set_trst(devh
);
947 static int jlink_reset_safe(int trst
, int srst
)
950 jlink_reset(trst
, srst
);
951 return jlink_flush();
954 COMMAND_HANDLER(jlink_usb_command
)
959 command_print(CMD
, "Need exactly one argument for jlink usb.");
960 return ERROR_COMMAND_SYNTAX_ERROR
;
963 if (sscanf(CMD_ARGV
[0], "%i", &tmp
) != 1) {
964 command_print(CMD
, "Invalid USB address: %s.", CMD_ARGV
[0]);
968 if (tmp
< JAYLINK_USB_ADDRESS_0
|| tmp
> JAYLINK_USB_ADDRESS_3
) {
969 command_print(CMD
, "Invalid USB address: %s.", CMD_ARGV
[0]);
975 use_serial_number
= false;
976 use_usb_address
= true;
981 COMMAND_HANDLER(jlink_serial_command
)
986 command_print(CMD
, "Need exactly one argument for jlink serial.");
987 return ERROR_COMMAND_SYNTAX_ERROR
;
990 ret
= jaylink_parse_serial_number(CMD_ARGV
[0], &serial_number
);
992 if (ret
== JAYLINK_ERR
) {
993 command_print(CMD
, "Invalid serial number: %s.", CMD_ARGV
[0]);
995 } else if (ret
!= JAYLINK_OK
) {
996 command_print(CMD
, "jaylink_parse_serial_number() failed: %s.",
997 jaylink_strerror(ret
));
1001 use_serial_number
= true;
1002 use_usb_address
= false;
1007 COMMAND_HANDLER(jlink_handle_hwstatus_command
)
1010 struct jaylink_hardware_status status
;
1012 ret
= jaylink_get_hardware_status(devh
, &status
);
1014 if (ret
!= JAYLINK_OK
) {
1015 command_print(CMD
, "jaylink_get_hardware_status() failed: %s.",
1016 jaylink_strerror(ret
));
1020 command_print(CMD
, "VTarget = %u.%03u V",
1021 status
.target_voltage
/ 1000, status
.target_voltage
% 1000);
1023 command_print(CMD
, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
1024 "TRST = %u", status
.tck
, status
.tdi
, status
.tdo
, status
.tms
,
1025 status
.tres
, status
.trst
);
1027 if (status
.target_voltage
< 1500)
1028 command_print(CMD
, "Target voltage too low. Check target power.");
1033 COMMAND_HANDLER(jlink_handle_free_memory_command
)
1038 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
)) {
1039 command_print(CMD
, "Retrieval of free memory is not supported by "
1044 ret
= jaylink_get_free_memory(devh
, &tmp
);
1046 if (ret
!= JAYLINK_OK
) {
1047 command_print(CMD
, "jaylink_get_free_memory() failed: %s.",
1048 jaylink_strerror(ret
));
1052 command_print(CMD
, "Device has %u bytes of free memory.", tmp
);
1057 COMMAND_HANDLER(jlink_handle_jlink_jtag_command
)
1063 switch (jtag_command_version
) {
1064 case JAYLINK_JTAG_VERSION_2
:
1067 case JAYLINK_JTAG_VERSION_3
:
1074 command_print(CMD
, "JTAG command version: %i", version
);
1075 } else if (CMD_ARGC
== 1) {
1076 if (sscanf(CMD_ARGV
[0], "%i", &tmp
) != 1) {
1077 command_print(CMD
, "Invalid argument: %s.", CMD_ARGV
[0]);
1078 return ERROR_COMMAND_SYNTAX_ERROR
;
1083 jtag_command_version
= JAYLINK_JTAG_VERSION_2
;
1086 jtag_command_version
= JAYLINK_JTAG_VERSION_3
;
1089 command_print(CMD
, "Invalid argument: %s.", CMD_ARGV
[0]);
1090 return ERROR_COMMAND_SYNTAX_ERROR
;
1093 command_print(CMD
, "Need exactly one argument for jlink jtag.");
1094 return ERROR_COMMAND_SYNTAX_ERROR
;
1100 COMMAND_HANDLER(jlink_handle_target_power_command
)
1105 if (CMD_ARGC
!= 1) {
1106 command_print(CMD
, "Need exactly one argument for jlink "
1108 return ERROR_COMMAND_SYNTAX_ERROR
;
1111 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
)) {
1112 command_print(CMD
, "Target power supply is not supported by the "
1117 if (!strcmp(CMD_ARGV
[0], "on")) {
1119 } else if (!strcmp(CMD_ARGV
[0], "off")) {
1122 command_print(CMD
, "Invalid argument: %s.", CMD_ARGV
[0]);
1126 ret
= jaylink_set_target_power(devh
, enable
);
1128 if (ret
!= JAYLINK_OK
) {
1129 command_print(CMD
, "jaylink_set_target_power() failed: %s.",
1130 jaylink_strerror(ret
));
1137 static void show_config_usb_address(struct command_invocation
*cmd
)
1139 if (config
.usb_address
!= tmp_config
.usb_address
)
1140 command_print(cmd
, "USB address: %u [%u]", config
.usb_address
,
1141 tmp_config
.usb_address
);
1143 command_print(cmd
, "USB address: %u", config
.usb_address
);
1146 static void show_config_ip_address(struct command_invocation
*cmd
)
1148 if (!memcmp(config
.ip_address
, tmp_config
.ip_address
, 4))
1149 command_print(cmd
, "IP address: %d.%d.%d.%d",
1150 config
.ip_address
[3], config
.ip_address
[2],
1151 config
.ip_address
[1], config
.ip_address
[0]);
1153 command_print(cmd
, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1154 config
.ip_address
[3], config
.ip_address
[2],
1155 config
.ip_address
[1], config
.ip_address
[0],
1156 tmp_config
.ip_address
[3], tmp_config
.ip_address
[2],
1157 tmp_config
.ip_address
[1], tmp_config
.ip_address
[0]);
1159 if (!memcmp(config
.subnet_mask
, tmp_config
.subnet_mask
, 4))
1160 command_print(cmd
, "Subnet mask: %d.%d.%d.%d",
1161 config
.subnet_mask
[3], config
.subnet_mask
[2],
1162 config
.subnet_mask
[1], config
.subnet_mask
[0]);
1164 command_print(cmd
, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1165 config
.subnet_mask
[3], config
.subnet_mask
[2],
1166 config
.subnet_mask
[1], config
.subnet_mask
[0],
1167 tmp_config
.subnet_mask
[3], tmp_config
.subnet_mask
[2],
1168 tmp_config
.subnet_mask
[1], tmp_config
.subnet_mask
[0]);
1171 static void show_config_mac_address(struct command_invocation
*cmd
)
1173 if (!memcmp(config
.mac_address
, tmp_config
.mac_address
, 6))
1174 command_print(cmd
, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1175 config
.mac_address
[5], config
.mac_address
[4],
1176 config
.mac_address
[3], config
.mac_address
[2],
1177 config
.mac_address
[1], config
.mac_address
[0]);
1179 command_print(cmd
, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1180 "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1181 config
.mac_address
[5], config
.mac_address
[4],
1182 config
.mac_address
[3], config
.mac_address
[2],
1183 config
.mac_address
[1], config
.mac_address
[0],
1184 tmp_config
.mac_address
[5], tmp_config
.mac_address
[4],
1185 tmp_config
.mac_address
[3], tmp_config
.mac_address
[2],
1186 tmp_config
.mac_address
[1], tmp_config
.mac_address
[0]);
1189 static void show_config_target_power(struct command_invocation
*cmd
)
1191 const char *target_power
;
1192 const char *current_target_power
;
1194 if (!config
.target_power
)
1195 target_power
= "off";
1197 target_power
= "on";
1199 if (!tmp_config
.target_power
)
1200 current_target_power
= "off";
1202 current_target_power
= "on";
1204 if (config
.target_power
!= tmp_config
.target_power
)
1205 command_print(cmd
, "Target power supply: %s [%s]", target_power
,
1206 current_target_power
);
1208 command_print(cmd
, "Target power supply: %s", target_power
);
1211 static void show_config(struct command_invocation
*cmd
)
1213 command_print(cmd
, "J-Link device configuration:");
1215 show_config_usb_address(cmd
);
1217 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
))
1218 show_config_target_power(cmd
);
1220 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1221 show_config_ip_address(cmd
);
1222 show_config_mac_address(cmd
);
1226 static int poll_trace(uint8_t *buf
, size_t *size
)
1233 ret
= jaylink_swo_read(devh
, buf
, &length
);
1235 if (ret
!= JAYLINK_OK
) {
1236 LOG_ERROR("jaylink_swo_read() failed: %s.", jaylink_strerror(ret
));
1245 static uint32_t calculate_trace_buffer_size(void)
1250 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
))
1253 ret
= jaylink_get_free_memory(devh
, &tmp
);
1255 if (ret
!= JAYLINK_OK
) {
1256 LOG_ERROR("jaylink_get_free_memory() failed: %s.",
1257 jaylink_strerror(ret
));
1261 if (tmp
> 0x3fff || tmp
<= 0x600)
1266 return tmp
& 0xffffff00;
1269 static bool calculate_swo_prescaler(unsigned int traceclkin_freq
,
1270 uint32_t trace_freq
, uint16_t *prescaler
)
1275 presc
= ((1.0 - SWO_MAX_FREQ_DEV
) * traceclkin_freq
) / trace_freq
+ 1;
1277 if (presc
> TPIU_ACPR_MAX_SWOSCALER
)
1280 deviation
= fabs(1.0 - ((double)trace_freq
* presc
/ traceclkin_freq
));
1282 if (deviation
> SWO_MAX_FREQ_DEV
)
1290 static bool detect_swo_freq_and_prescaler(struct jaylink_swo_speed speed
,
1291 unsigned int traceclkin_freq
, uint32_t *trace_freq
,
1292 uint16_t *prescaler
)
1298 for (divider
= speed
.min_div
; divider
<= speed
.max_div
; divider
++) {
1299 *trace_freq
= speed
.freq
/ divider
;
1300 presc
= ((1.0 - SWO_MAX_FREQ_DEV
) * traceclkin_freq
) / *trace_freq
+ 1;
1302 if (presc
> TPIU_ACPR_MAX_SWOSCALER
)
1305 deviation
= fabs(1.0 - ((double)*trace_freq
* presc
/ traceclkin_freq
));
1307 if (deviation
<= SWO_MAX_FREQ_DEV
) {
1316 static int config_trace(bool enabled
, enum tpiu_pin_protocol pin_protocol
,
1317 uint32_t port_size
, unsigned int *trace_freq
,
1318 unsigned int traceclkin_freq
, uint16_t *prescaler
)
1321 uint32_t buffer_size
;
1322 struct jaylink_swo_speed speed
;
1327 trace_enabled
= enabled
;
1329 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SWO
)) {
1333 LOG_ERROR("Trace capturing is not supported by the device.");
1337 ret
= jaylink_swo_stop(devh
);
1339 if (ret
!= JAYLINK_OK
) {
1340 LOG_ERROR("jaylink_swo_stop() failed: %s.", jaylink_strerror(ret
));
1346 * Adjust the SWD transaction buffer size as stopping SWO capturing
1347 * deallocates device internal memory.
1349 if (!adjust_swd_buffer_size())
1355 if (pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
) {
1356 LOG_ERROR("Selected pin protocol is not supported.");
1360 buffer_size
= calculate_trace_buffer_size();
1363 LOG_ERROR("Not enough free device memory to start trace capturing.");
1367 ret
= jaylink_swo_get_speeds(devh
, JAYLINK_SWO_MODE_UART
, &speed
);
1369 if (ret
!= JAYLINK_OK
) {
1370 LOG_ERROR("jaylink_swo_get_speeds() failed: %s.",
1371 jaylink_strerror(ret
));
1375 if (*trace_freq
> 0) {
1376 divider
= speed
.freq
/ *trace_freq
;
1377 min_freq
= speed
.freq
/ speed
.max_div
;
1378 max_freq
= speed
.freq
/ speed
.min_div
;
1380 if (*trace_freq
> max_freq
) {
1381 LOG_INFO("Given SWO frequency too high, using %u Hz instead.",
1383 *trace_freq
= max_freq
;
1384 } else if (*trace_freq
< min_freq
) {
1385 LOG_INFO("Given SWO frequency too low, using %u Hz instead.",
1387 *trace_freq
= min_freq
;
1388 } else if (*trace_freq
!= speed
.freq
/ divider
) {
1389 *trace_freq
= speed
.freq
/ divider
;
1391 LOG_INFO("Given SWO frequency is not supported by the device, "
1392 "using %u Hz instead.", *trace_freq
);
1395 if (!calculate_swo_prescaler(traceclkin_freq
, *trace_freq
,
1397 LOG_ERROR("SWO frequency is not suitable. Please choose a "
1398 "different frequency or use auto-detection.");
1402 LOG_INFO("Trying to auto-detect SWO frequency.");
1404 if (!detect_swo_freq_and_prescaler(speed
, traceclkin_freq
, trace_freq
,
1406 LOG_ERROR("Maximum permitted frequency deviation of %.02f %% "
1407 "could not be achieved.", SWO_MAX_FREQ_DEV
);
1408 LOG_ERROR("Auto-detection of SWO frequency failed.");
1412 LOG_INFO("Using SWO frequency of %u Hz.", *trace_freq
);
1415 ret
= jaylink_swo_start(devh
, JAYLINK_SWO_MODE_UART
, *trace_freq
,
1418 if (ret
!= JAYLINK_OK
) {
1419 LOG_ERROR("jaylink_start_swo() failed: %s.", jaylink_strerror(ret
));
1423 LOG_DEBUG("Using %u bytes device memory for trace capturing.",
1427 * Adjust the SWD transaction buffer size as starting SWO capturing
1428 * allocates device internal memory.
1430 if (!adjust_swd_buffer_size())
1436 COMMAND_HANDLER(jlink_handle_config_usb_address_command
)
1440 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1441 command_print(CMD
, "Reading configuration is not supported by the "
1447 show_config_usb_address(CMD
);
1448 } else if (CMD_ARGC
== 1) {
1449 if (sscanf(CMD_ARGV
[0], "%" SCNd8
, &tmp
) != 1) {
1450 command_print(CMD
, "Invalid USB address: %s.", CMD_ARGV
[0]);
1454 if (tmp
> JAYLINK_USB_ADDRESS_3
) {
1455 command_print(CMD
, "Invalid USB address: %u.", tmp
);
1459 tmp_config
.usb_address
= tmp
;
1461 command_print(CMD
, "Need exactly one argument for jlink config "
1463 return ERROR_COMMAND_SYNTAX_ERROR
;
1469 COMMAND_HANDLER(jlink_handle_config_target_power_command
)
1473 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1474 command_print(CMD
, "Reading configuration is not supported by the "
1479 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
)) {
1480 command_print(CMD
, "Target power supply is not supported by the "
1486 show_config_target_power(CMD
);
1487 } else if (CMD_ARGC
== 1) {
1488 if (!strcmp(CMD_ARGV
[0], "on")) {
1490 } else if (!strcmp(CMD_ARGV
[0], "off")) {
1493 command_print(CMD
, "Invalid argument: %s.", CMD_ARGV
[0]);
1497 tmp_config
.target_power
= enable
;
1499 command_print(CMD
, "Need exactly one argument for jlink config "
1501 return ERROR_COMMAND_SYNTAX_ERROR
;
1507 COMMAND_HANDLER(jlink_handle_config_mac_address_command
)
1514 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1515 command_print(CMD
, "Reading configuration is not supported by the "
1520 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1521 command_print(CMD
, "Ethernet connectivity is not supported by the "
1527 show_config_mac_address(CMD
);
1528 } else if (CMD_ARGC
== 1) {
1531 if ((strlen(str
) != 17) || (str
[2] != ':' || str
[5] != ':' ||
1532 str
[8] != ':' || str
[11] != ':' || str
[14] != ':')) {
1533 command_print(CMD
, "Invalid MAC address format.");
1534 return ERROR_COMMAND_SYNTAX_ERROR
;
1537 for (i
= 5; i
>= 0; i
--) {
1538 addr
[i
] = strtoul(str
, &e
, 16);
1542 if (!(addr
[0] | addr
[1] | addr
[2] | addr
[3] | addr
[4] | addr
[5])) {
1543 command_print(CMD
, "Invalid MAC address: zero address.");
1544 return ERROR_COMMAND_SYNTAX_ERROR
;
1547 if (!(0x01 & addr
[0])) {
1548 command_print(CMD
, "Invalid MAC address: multicast address.");
1549 return ERROR_COMMAND_SYNTAX_ERROR
;
1552 memcpy(tmp_config
.mac_address
, addr
, sizeof(addr
));
1554 command_print(CMD
, "Need exactly one argument for jlink config "
1556 return ERROR_COMMAND_SYNTAX_ERROR
;
1562 static bool string_to_ip(const char *s
, uint8_t *ip
, int *pos
)
1566 const char *s_save
= s
;
1572 for (i
= 0; i
< 4; i
++) {
1573 lip
[i
] = strtoul(s
, &e
, 10);
1575 if (*e
!= '.' && i
!= 3)
1582 memcpy(ip
, lip
, sizeof(lip
));
1587 static void cpy_ip(uint8_t *dst
, uint8_t *src
)
1591 for (i
= 0, j
= 3; i
< 4; i
++, j
--)
1595 COMMAND_HANDLER(jlink_handle_config_ip_address_command
)
1597 uint8_t ip_address
[4];
1598 uint32_t subnet_mask
= 0;
1600 uint8_t subnet_bits
= 24;
1602 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1603 command_print(CMD
, "Reading configuration is not supported by the "
1608 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1609 command_print(CMD
, "Ethernet connectivity is not supported by the "
1615 show_config_ip_address(CMD
);
1617 if (!string_to_ip(CMD_ARGV
[0], ip_address
, &i
))
1618 return ERROR_COMMAND_SYNTAX_ERROR
;
1620 len
= strlen(CMD_ARGV
[0]);
1622 /* Check for format A.B.C.D/E. */
1624 if (CMD_ARGV
[0][i
] != '/')
1625 return ERROR_COMMAND_SYNTAX_ERROR
;
1627 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0] + i
+ 1, subnet_bits
);
1628 } else if (CMD_ARGC
> 1) {
1629 if (!string_to_ip(CMD_ARGV
[1], (uint8_t *)&subnet_mask
, &i
))
1630 return ERROR_COMMAND_SYNTAX_ERROR
;
1634 subnet_mask
= (uint32_t)(subnet_bits
< 32 ?
1635 ((1ULL << subnet_bits
) - 1) : 0xffffffff);
1637 cpy_ip(tmp_config
.ip_address
, ip_address
);
1638 cpy_ip(tmp_config
.subnet_mask
, (uint8_t *)&subnet_mask
);
1644 COMMAND_HANDLER(jlink_handle_config_reset_command
)
1646 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
))
1649 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
1655 COMMAND_HANDLER(jlink_handle_config_write_command
)
1659 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1660 command_print(CMD
, "Reading configuration is not supported by the "
1665 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_WRITE_CONFIG
)) {
1666 command_print(CMD
, "Writing configuration is not supported by the "
1671 if (!memcmp(&config
, &tmp_config
, sizeof(struct device_config
))) {
1672 command_print(CMD
, "Operation not performed due to no changes in "
1673 "the configuration.");
1677 ret
= jaylink_write_raw_config(devh
, (const uint8_t *)&tmp_config
);
1679 if (ret
!= JAYLINK_OK
) {
1680 LOG_ERROR("jaylink_write_raw_config() failed: %s.",
1681 jaylink_strerror(ret
));
1685 if (!read_device_config(&config
)) {
1686 LOG_ERROR("Failed to read device configuration for verification.");
1690 if (memcmp(&config
, &tmp_config
, sizeof(struct device_config
))) {
1691 LOG_ERROR("Verification of device configuration failed. Please check "
1696 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
1697 command_print(CMD
, "The new device configuration applies after power "
1698 "cycling the J-Link device.");
1703 COMMAND_HANDLER(jlink_handle_config_command
)
1705 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1706 command_print(CMD
, "Device doesn't support reading configuration.");
1716 COMMAND_HANDLER(jlink_handle_emucom_write_command
)
1726 return ERROR_COMMAND_SYNTAX_ERROR
;
1728 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_EMUCOM
)) {
1729 LOG_ERROR("Device does not support EMUCOM.");
1733 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], channel
);
1735 tmp
= strlen(CMD_ARGV
[1]);
1738 LOG_ERROR("Data must be encoded as hexadecimal pairs.");
1739 return ERROR_COMMAND_ARGUMENT_INVALID
;
1742 buf
= malloc(tmp
/ 2);
1745 LOG_ERROR("Failed to allocate buffer.");
1749 dummy
= unhexify(buf
, CMD_ARGV
[1], tmp
/ 2);
1751 if (dummy
!= (tmp
/ 2)) {
1752 LOG_ERROR("Data must be encoded as hexadecimal pairs.");
1754 return ERROR_COMMAND_ARGUMENT_INVALID
;
1758 ret
= jaylink_emucom_write(devh
, channel
, buf
, &length
);
1762 if (ret
== JAYLINK_ERR_DEV_NOT_SUPPORTED
) {
1763 LOG_ERROR("Channel not supported by the device.");
1765 } else if (ret
!= JAYLINK_OK
) {
1766 LOG_ERROR("Failed to write to channel: %s.", jaylink_strerror(ret
));
1770 if (length
!= (tmp
/ 2))
1771 LOG_WARNING("Only %" PRIu32
" bytes written to the channel.", length
);
1776 COMMAND_HANDLER(jlink_handle_emucom_read_command
)
1785 return ERROR_COMMAND_SYNTAX_ERROR
;
1787 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_EMUCOM
)) {
1788 LOG_ERROR("Device does not support EMUCOM.");
1792 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], channel
);
1793 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
1795 buf
= malloc(length
* 3 + 1);
1798 LOG_ERROR("Failed to allocate buffer.");
1802 ret
= jaylink_emucom_read(devh
, channel
, buf
, &length
);
1804 if (ret
== JAYLINK_ERR_DEV_NOT_SUPPORTED
) {
1805 LOG_ERROR("Channel is not supported by the device.");
1808 } else if (ret
== JAYLINK_ERR_DEV_NOT_AVAILABLE
) {
1809 LOG_ERROR("Channel is not available for the requested amount of data. "
1810 "%" PRIu32
" bytes are avilable.", length
);
1813 } else if (ret
!= JAYLINK_OK
) {
1814 LOG_ERROR("Failed to read from channel: %s.", jaylink_strerror(ret
));
1819 tmp
= hexify((char *)buf
+ length
, buf
, length
, 2 * length
+ 1);
1821 if (tmp
!= 2 * length
) {
1822 LOG_ERROR("Failed to convert data into hexadecimal string.");
1827 command_print(CMD
, "%s", buf
+ length
);
1833 static const struct command_registration jlink_config_subcommand_handlers
[] = {
1836 .handler
= &jlink_handle_config_usb_address_command
,
1837 .mode
= COMMAND_EXEC
,
1838 .help
= "set the USB address",
1842 .name
= "targetpower",
1843 .handler
= &jlink_handle_config_target_power_command
,
1844 .mode
= COMMAND_EXEC
,
1845 .help
= "set the target power supply",
1850 .handler
= &jlink_handle_config_mac_address_command
,
1851 .mode
= COMMAND_EXEC
,
1852 .help
= "set the MAC Address",
1853 .usage
= "[ff:ff:ff:ff:ff:ff]",
1857 .handler
= &jlink_handle_config_ip_address_command
,
1858 .mode
= COMMAND_EXEC
,
1859 .help
= "set the IP address, where A.B.C.D is the IP address, "
1860 "E the bit of the subnet mask, F.G.H.I the subnet mask",
1861 .usage
= "[A.B.C.D[/E] [F.G.H.I]]",
1865 .handler
= &jlink_handle_config_reset_command
,
1866 .mode
= COMMAND_EXEC
,
1867 .help
= "undo configuration changes",
1872 .handler
= &jlink_handle_config_write_command
,
1873 .mode
= COMMAND_EXEC
,
1874 .help
= "write configuration to the device",
1877 COMMAND_REGISTRATION_DONE
1880 static const struct command_registration jlink_emucom_subcommand_handlers
[] = {
1883 .handler
= &jlink_handle_emucom_write_command
,
1884 .mode
= COMMAND_EXEC
,
1885 .help
= "write to a channel",
1886 .usage
= "<channel> <data>",
1890 .handler
= &jlink_handle_emucom_read_command
,
1891 .mode
= COMMAND_EXEC
,
1892 .help
= "read from a channel",
1893 .usage
= "<channel> <length>"
1895 COMMAND_REGISTRATION_DONE
1898 static const struct command_registration jlink_subcommand_handlers
[] = {
1901 .handler
= &jlink_handle_jlink_jtag_command
,
1902 .mode
= COMMAND_EXEC
,
1903 .help
= "select the JTAG command version",
1907 .name
= "targetpower",
1908 .handler
= &jlink_handle_target_power_command
,
1909 .mode
= COMMAND_EXEC
,
1910 .help
= "set the target power supply",
1915 .handler
= &jlink_handle_free_memory_command
,
1916 .mode
= COMMAND_EXEC
,
1917 .help
= "show free device memory",
1922 .handler
= &jlink_handle_hwstatus_command
,
1923 .mode
= COMMAND_EXEC
,
1924 .help
= "show the hardware status",
1929 .handler
= &jlink_usb_command
,
1930 .mode
= COMMAND_CONFIG
,
1931 .help
= "set the USB address of the device that should be used",
1936 .handler
= &jlink_serial_command
,
1937 .mode
= COMMAND_CONFIG
,
1938 .help
= "set the serial number of the device that should be used",
1939 .usage
= "<serial number>"
1943 .handler
= &jlink_handle_config_command
,
1944 .mode
= COMMAND_EXEC
,
1945 .help
= "access the device configuration. If no argument is given "
1946 "this will show the device configuration",
1947 .chain
= jlink_config_subcommand_handlers
,
1952 .mode
= COMMAND_EXEC
,
1953 .help
= "access EMUCOM channel",
1954 .chain
= jlink_emucom_subcommand_handlers
,
1957 COMMAND_REGISTRATION_DONE
1960 static const struct command_registration jlink_command_handlers
[] = {
1963 .mode
= COMMAND_ANY
,
1964 .help
= "perform jlink management",
1965 .chain
= jlink_subcommand_handlers
,
1968 COMMAND_REGISTRATION_DONE
1971 static int jlink_swd_init(void)
1973 iface
= JAYLINK_TIF_SWD
;
1978 static void jlink_swd_write_reg(uint8_t cmd
, uint32_t value
, uint32_t ap_delay_clk
)
1980 assert(!(cmd
& SWD_CMD_RnW
));
1981 jlink_swd_queue_cmd(cmd
, NULL
, value
, ap_delay_clk
);
1984 static void jlink_swd_read_reg(uint8_t cmd
, uint32_t *value
, uint32_t ap_delay_clk
)
1986 assert(cmd
& SWD_CMD_RnW
);
1987 jlink_swd_queue_cmd(cmd
, value
, 0, ap_delay_clk
);
1990 /***************************************************************************/
1991 /* J-Link tap functions */
1993 static unsigned tap_length
;
1994 /* In SWD mode use tms buffer for direction control */
1995 static uint8_t tms_buffer
[JLINK_TAP_BUFFER_SIZE
];
1996 static uint8_t tdi_buffer
[JLINK_TAP_BUFFER_SIZE
];
1997 static uint8_t tdo_buffer
[JLINK_TAP_BUFFER_SIZE
];
1999 struct pending_scan_result
{
2000 /** First bit position in tdo_buffer to read. */
2002 /** Number of bits to read. */
2004 /** Location to store the result */
2006 /** Offset in the destination buffer */
2007 unsigned buffer_offset
;
2010 #define MAX_PENDING_SCAN_RESULTS 256
2012 static int pending_scan_results_length
;
2013 static struct pending_scan_result pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
2015 static void jlink_tap_init(void)
2018 pending_scan_results_length
= 0;
2019 memset(tms_buffer
, 0, sizeof(tms_buffer
));
2020 memset(tdi_buffer
, 0, sizeof(tdi_buffer
));
2023 static void jlink_clock_data(const uint8_t *out
, unsigned out_offset
,
2024 const uint8_t *tms_out
, unsigned tms_offset
,
2025 uint8_t *in
, unsigned in_offset
,
2029 unsigned available_length
= JLINK_TAP_BUFFER_SIZE
- tap_length
/ 8;
2031 if (!available_length
||
2032 (in
&& pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
)) {
2033 if (jlink_flush() != ERROR_OK
)
2035 available_length
= JLINK_TAP_BUFFER_SIZE
;
2038 struct pending_scan_result
*pending_scan_result
=
2039 &pending_scan_results_buffer
[pending_scan_results_length
];
2041 unsigned scan_length
= length
> available_length
?
2042 available_length
: length
;
2045 buf_set_buf(out
, out_offset
, tdi_buffer
, tap_length
, scan_length
);
2047 buf_set_buf(tms_out
, tms_offset
, tms_buffer
, tap_length
, scan_length
);
2050 pending_scan_result
->first
= tap_length
;
2051 pending_scan_result
->length
= scan_length
;
2052 pending_scan_result
->buffer
= in
;
2053 pending_scan_result
->buffer_offset
= in_offset
;
2054 pending_scan_results_length
++;
2057 tap_length
+= scan_length
;
2058 out_offset
+= scan_length
;
2059 tms_offset
+= scan_length
;
2060 in_offset
+= scan_length
;
2061 length
-= scan_length
;
2062 } while (length
> 0);
2065 static int jlink_flush(void)
2073 jlink_last_state
= jtag_debug_state_machine(tms_buffer
, tdi_buffer
,
2074 tap_length
, jlink_last_state
);
2076 ret
= jaylink_jtag_io(devh
, tms_buffer
, tdi_buffer
, tdo_buffer
,
2077 tap_length
, jtag_command_version
);
2079 if (ret
!= JAYLINK_OK
) {
2080 LOG_ERROR("jaylink_jtag_io() failed: %s.", jaylink_strerror(ret
));
2082 return ERROR_JTAG_QUEUE_FAILED
;
2085 for (i
= 0; i
< pending_scan_results_length
; i
++) {
2086 struct pending_scan_result
*p
= &pending_scan_results_buffer
[i
];
2088 buf_set_buf(tdo_buffer
, p
->first
, p
->buffer
,
2089 p
->buffer_offset
, p
->length
);
2091 LOG_DEBUG_IO("Pending scan result, length = %d.", p
->length
);
2099 static void fill_buffer(uint8_t *buf
, uint32_t val
, uint32_t len
)
2101 unsigned int tap_pos
= tap_length
;
2104 buf_set_u32(buf
, tap_pos
, 32, val
);
2110 buf_set_u32(buf
, tap_pos
, len
, val
);
2113 static void jlink_queue_data_out(const uint8_t *data
, uint32_t len
)
2115 const uint32_t dir_out
= 0xffffffff;
2118 bit_copy(tdi_buffer
, tap_length
, data
, 0, len
);
2120 fill_buffer(tdi_buffer
, 0, len
);
2122 fill_buffer(tms_buffer
, dir_out
, len
);
2126 static void jlink_queue_data_in(uint32_t len
)
2128 const uint32_t dir_in
= 0;
2130 fill_buffer(tms_buffer
, dir_in
, len
);
2134 static int jlink_swd_switch_seq(enum swd_special_seq seq
)
2141 LOG_DEBUG("SWD line reset");
2142 s
= swd_seq_line_reset
;
2143 s_len
= swd_seq_line_reset_len
;
2146 LOG_DEBUG("JTAG-to-SWD");
2147 s
= swd_seq_jtag_to_swd
;
2148 s_len
= swd_seq_jtag_to_swd_len
;
2151 LOG_DEBUG("SWD-to-JTAG");
2152 s
= swd_seq_swd_to_jtag
;
2153 s_len
= swd_seq_swd_to_jtag_len
;
2156 LOG_ERROR("Sequence %d not supported.", seq
);
2160 jlink_queue_data_out(s
, s_len
);
2165 static int jlink_swd_run_queue(void)
2170 LOG_DEBUG("Executing %d queued transactions.", pending_scan_results_length
);
2172 if (queued_retval
!= ERROR_OK
) {
2173 LOG_DEBUG("Skipping due to previous errors: %d.", queued_retval
);
2178 * A transaction must be followed by another transaction or at least 8 idle
2179 * cycles to ensure that data is clocked through the AP.
2181 jlink_queue_data_out(NULL
, 8);
2183 ret
= jaylink_swd_io(devh
, tms_buffer
, tdi_buffer
, tdo_buffer
, tap_length
);
2185 if (ret
!= JAYLINK_OK
) {
2186 LOG_ERROR("jaylink_swd_io() failed: %s.", jaylink_strerror(ret
));
2190 for (i
= 0; i
< pending_scan_results_length
; i
++) {
2191 int ack
= buf_get_u32(tdo_buffer
, pending_scan_results_buffer
[i
].first
, 3);
2193 if (ack
!= SWD_ACK_OK
) {
2194 LOG_DEBUG("SWD ack not OK: %d %s", ack
,
2195 ack
== SWD_ACK_WAIT
? "WAIT" : ack
== SWD_ACK_FAULT
? "FAULT" : "JUNK");
2196 queued_retval
= ack
== SWD_ACK_WAIT
? ERROR_WAIT
: ERROR_FAIL
;
2198 } else if (pending_scan_results_buffer
[i
].length
) {
2199 uint32_t data
= buf_get_u32(tdo_buffer
, 3 + pending_scan_results_buffer
[i
].first
, 32);
2200 int parity
= buf_get_u32(tdo_buffer
, 3 + 32 + pending_scan_results_buffer
[i
].first
, 1);
2202 if (parity
!= parity_u32(data
)) {
2203 LOG_ERROR("SWD: Read data parity mismatch.");
2204 queued_retval
= ERROR_FAIL
;
2208 if (pending_scan_results_buffer
[i
].buffer
)
2209 *(uint32_t *)pending_scan_results_buffer
[i
].buffer
= data
;
2215 ret
= queued_retval
;
2216 queued_retval
= ERROR_OK
;
2221 static void jlink_swd_queue_cmd(uint8_t cmd
, uint32_t *dst
, uint32_t data
, uint32_t ap_delay_clk
)
2223 uint8_t data_parity_trn
[DIV_ROUND_UP(32 + 1, 8)];
2224 if (tap_length
+ 46 + 8 + ap_delay_clk
>= swd_buffer_size
* 8 ||
2225 pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
) {
2226 /* Not enough room in the queue. Run the queue. */
2227 queued_retval
= jlink_swd_run_queue();
2230 if (queued_retval
!= ERROR_OK
)
2233 cmd
|= SWD_CMD_START
| SWD_CMD_PARK
;
2235 jlink_queue_data_out(&cmd
, 8);
2237 pending_scan_results_buffer
[pending_scan_results_length
].first
= tap_length
;
2239 if (cmd
& SWD_CMD_RnW
) {
2240 /* Queue a read transaction. */
2241 pending_scan_results_buffer
[pending_scan_results_length
].length
= 32;
2242 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= dst
;
2244 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2246 /* Queue a write transaction. */
2247 pending_scan_results_buffer
[pending_scan_results_length
].length
= 0;
2248 jlink_queue_data_in(1 + 3 + 1);
2250 buf_set_u32(data_parity_trn
, 0, 32, data
);
2251 buf_set_u32(data_parity_trn
, 32, 1, parity_u32(data
));
2253 jlink_queue_data_out(data_parity_trn
, 32 + 1);
2256 pending_scan_results_length
++;
2258 /* Insert idle cycles after AP accesses to avoid WAIT. */
2259 if (cmd
& SWD_CMD_APnDP
)
2260 jlink_queue_data_out(NULL
, ap_delay_clk
);
2263 static const struct swd_driver jlink_swd
= {
2264 .init
= &jlink_swd_init
,
2265 .switch_seq
= &jlink_swd_switch_seq
,
2266 .read_reg
= &jlink_swd_read_reg
,
2267 .write_reg
= &jlink_swd_write_reg
,
2268 .run
= &jlink_swd_run_queue
,
2271 static const char * const jlink_transports
[] = { "jtag", "swd", NULL
};
2273 static struct jtag_interface jlink_interface
= {
2274 .execute_queue
= &jlink_execute_queue
,
2277 struct adapter_driver jlink_adapter_driver
= {
2279 .transports
= jlink_transports
,
2280 .commands
= jlink_command_handlers
,
2282 .init
= &jlink_init
,
2283 .quit
= &jlink_quit
,
2284 .reset
= &jlink_reset_safe
,
2285 .speed
= &jlink_speed
,
2287 .speed_div
= &jlink_speed_div
,
2288 .config_trace
= &config_trace
,
2289 .poll_trace
= &poll_trace
,
2291 .jtag_ops
= &jlink_interface
,
2292 .swd_ops
= &jlink_swd
,