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>
43 #include <libjaylink/libjaylink.h>
45 static struct jaylink_context
*jayctx
;
46 static struct jaylink_device_handle
*devh
;
47 static struct jaylink_connection conn
;
48 static struct jaylink_connection connlist
[JAYLINK_MAX_CONNECTIONS
];
49 static enum jaylink_jtag_version jtag_command_version
;
50 static uint8_t caps
[JAYLINK_DEV_EXT_CAPS_SIZE
];
52 static uint32_t serial_number
;
53 static bool use_serial_number
;
54 static bool use_usb_location
;
55 static enum jaylink_usb_address usb_address
;
56 static bool use_usb_address
;
57 static enum jaylink_target_interface iface
= JAYLINK_TIF_JTAG
;
58 static bool trace_enabled
;
60 #define JLINK_MAX_SPEED 12000
61 #define JLINK_TAP_BUFFER_SIZE 2048
63 static unsigned int swd_buffer_size
= JLINK_TAP_BUFFER_SIZE
;
65 /* 256 byte non-volatile memory */
66 struct device_config
{
68 /* 0ffset 0x01 to 0x03 */
69 uint8_t reserved_1
[3];
70 uint32_t target_power
;
71 /* 0ffset 0x08 to 0x1f */
72 uint8_t reserved_2
[24];
73 /* IP only for J-Link Pro */
74 uint8_t ip_address
[4];
75 uint8_t subnet_mask
[4];
76 /* 0ffset 0x28 to 0x2f */
77 uint8_t reserved_3
[8];
78 uint8_t mac_address
[6];
79 /* 0ffset 0x36 to 0xff */
80 uint8_t reserved_4
[202];
81 } __attribute__ ((packed
));
83 static struct device_config config
;
84 static struct device_config tmp_config
;
86 /* Queue command functions */
87 static void jlink_end_state(tap_state_t state
);
88 static void jlink_state_move(void);
89 static void jlink_path_move(int num_states
, tap_state_t
*path
);
90 static void jlink_stableclocks(int num_cycles
);
91 static void jlink_runtest(int num_cycles
);
92 static void jlink_reset(int trst
, int srst
);
93 static int jlink_swd_run_queue(void);
94 static void jlink_swd_queue_cmd(uint8_t cmd
, uint32_t *dst
, uint32_t data
, uint32_t ap_delay_clk
);
95 static int jlink_swd_switch_seq(enum swd_special_seq seq
);
97 /* J-Link tap buffer functions */
98 static void jlink_tap_init(void);
99 static int jlink_flush(void);
101 * Queue data to go out and in, flushing the queue as many times as
104 * @param out A pointer to TDI data, if NULL, old stale data will be used.
105 * @param out_offset A bit offset for TDI data.
106 * @param tms_out A pointer to TMS data, if NULL, zeroes will be emitted.
107 * @param tms_offset A bit offset for TMS data.
108 * @param in A pointer to store TDO data to, if NULL the data will be discarded.
109 * @param in_offset A bit offset for TDO data.
110 * @param length Amount of bits to transfer out and in.
112 * @retval This function doesn't return any value.
114 static void jlink_clock_data(const uint8_t *out
, unsigned out_offset
,
115 const uint8_t *tms_out
, unsigned tms_offset
,
116 uint8_t *in
, unsigned in_offset
,
119 static enum tap_state jlink_last_state
= TAP_RESET
;
120 static int queued_retval
;
122 /***************************************************************************/
123 /* External interface implementation */
125 static void jlink_execute_stableclocks(struct jtag_command
*cmd
)
127 LOG_DEBUG_IO("stableclocks %i cycles", cmd
->cmd
.runtest
->num_cycles
);
128 jlink_stableclocks(cmd
->cmd
.runtest
->num_cycles
);
131 static void jlink_execute_runtest(struct jtag_command
*cmd
)
133 LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd
->cmd
.runtest
->num_cycles
,
134 cmd
->cmd
.runtest
->end_state
);
136 jlink_end_state(cmd
->cmd
.runtest
->end_state
);
137 jlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
140 static void jlink_execute_statemove(struct jtag_command
*cmd
)
142 LOG_DEBUG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
144 jlink_end_state(cmd
->cmd
.statemove
->end_state
);
148 static void jlink_execute_pathmove(struct jtag_command
*cmd
)
150 LOG_DEBUG_IO("pathmove: %i states, end in %i",
151 cmd
->cmd
.pathmove
->num_states
,
152 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
154 jlink_path_move(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
157 static void jlink_execute_scan(struct jtag_command
*cmd
)
159 LOG_DEBUG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN",
160 jtag_scan_type(cmd
->cmd
.scan
));
162 /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
163 while (cmd
->cmd
.scan
->num_fields
> 0
164 && cmd
->cmd
.scan
->fields
[cmd
->cmd
.scan
->num_fields
- 1].num_bits
== 0) {
165 cmd
->cmd
.scan
->num_fields
--;
166 LOG_DEBUG("discarding trailing empty field");
169 if (cmd
->cmd
.scan
->num_fields
== 0) {
170 LOG_DEBUG("empty scan, doing nothing");
174 if (cmd
->cmd
.scan
->ir_scan
) {
175 if (tap_get_state() != TAP_IRSHIFT
) {
176 jlink_end_state(TAP_IRSHIFT
);
180 if (tap_get_state() != TAP_DRSHIFT
) {
181 jlink_end_state(TAP_DRSHIFT
);
186 jlink_end_state(cmd
->cmd
.scan
->end_state
);
188 struct scan_field
*field
= cmd
->cmd
.scan
->fields
;
189 unsigned scan_size
= 0;
191 for (int i
= 0; i
< cmd
->cmd
.scan
->num_fields
; i
++, field
++) {
192 scan_size
+= field
->num_bits
;
193 LOG_DEBUG_IO("%s%s field %d/%d %d bits",
194 field
->in_value
? "in" : "",
195 field
->out_value
? "out" : "",
197 cmd
->cmd
.scan
->num_fields
,
200 if (i
== cmd
->cmd
.scan
->num_fields
- 1 && tap_get_state() != tap_get_end_state()) {
201 /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
202 * movement. This last field can't have length zero, it was checked above. */
203 jlink_clock_data(field
->out_value
,
209 field
->num_bits
- 1);
210 uint8_t last_bit
= 0;
211 if (field
->out_value
)
212 bit_copy(&last_bit
, 0, field
->out_value
, field
->num_bits
- 1, 1);
213 uint8_t tms_bits
= 0x01;
214 jlink_clock_data(&last_bit
,
221 tap_set_state(tap_state_transition(tap_get_state(), 1));
222 jlink_clock_data(NULL
,
229 tap_set_state(tap_state_transition(tap_get_state(), 0));
231 jlink_clock_data(field
->out_value
,
240 if (tap_get_state() != tap_get_end_state()) {
241 jlink_end_state(tap_get_end_state());
245 LOG_DEBUG_IO("%s scan, %i bits, end in %s",
246 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
247 tap_state_name(tap_get_end_state()));
250 static void jlink_execute_reset(struct jtag_command
*cmd
)
252 LOG_DEBUG_IO("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
,
253 cmd
->cmd
.reset
->srst
);
256 jlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
260 static void jlink_execute_sleep(struct jtag_command
*cmd
)
262 LOG_DEBUG_IO("sleep %" PRIi32
"", cmd
->cmd
.sleep
->us
);
264 jtag_sleep(cmd
->cmd
.sleep
->us
);
267 static int jlink_execute_command(struct jtag_command
*cmd
)
270 case JTAG_STABLECLOCKS
:
271 jlink_execute_stableclocks(cmd
);
274 jlink_execute_runtest(cmd
);
277 jlink_execute_statemove(cmd
);
280 jlink_execute_pathmove(cmd
);
283 jlink_execute_scan(cmd
);
286 jlink_execute_reset(cmd
);
289 jlink_execute_sleep(cmd
);
292 LOG_ERROR("BUG: Unknown JTAG command type encountered.");
293 return ERROR_JTAG_QUEUE_FAILED
;
299 static int jlink_execute_queue(void)
302 struct jtag_command
*cmd
= jtag_command_queue
;
304 while (cmd
!= NULL
) {
305 ret
= jlink_execute_command(cmd
);
313 return jlink_flush();
316 static int jlink_speed(int speed
)
319 struct jaylink_speed tmp
;
322 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_SPEEDS
)) {
323 ret
= jaylink_get_speeds(devh
, &tmp
);
325 if (ret
!= JAYLINK_OK
) {
326 LOG_ERROR("jaylink_get_speeds() failed: %s.",
327 jaylink_strerror(ret
));
328 return ERROR_JTAG_DEVICE_ERROR
;
332 max_speed
= tmp
.freq
/ tmp
.div
;
334 max_speed
= JLINK_MAX_SPEED
;
338 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING
)) {
339 LOG_ERROR("Adaptive clocking is not supported by the device.");
340 return ERROR_JTAG_NOT_IMPLEMENTED
;
343 speed
= JAYLINK_SPEED_ADAPTIVE_CLOCKING
;
344 } else if (speed
> max_speed
) {
345 LOG_INFO("Reduced speed from %d kHz to %d kHz (maximum).", speed
,
350 ret
= jaylink_set_speed(devh
, speed
);
352 if (ret
!= JAYLINK_OK
) {
353 LOG_ERROR("jaylink_set_speed() failed: %s.",
354 jaylink_strerror(ret
));
355 return ERROR_JTAG_DEVICE_ERROR
;
361 static int jlink_speed_div(int speed
, int *khz
)
368 static int jlink_khz(int khz
, int *jtag_speed
)
375 static bool read_device_config(struct device_config
*cfg
)
379 ret
= jaylink_read_raw_config(devh
, (uint8_t *)cfg
);
381 if (ret
!= JAYLINK_OK
) {
382 LOG_ERROR("jaylink_read_raw_config() failed: %s.",
383 jaylink_strerror(ret
));
387 if (cfg
->usb_address
== 0xff)
388 cfg
->usb_address
= 0x00;
390 if (cfg
->target_power
== 0xffffffff)
391 cfg
->target_power
= 0;
396 static int select_interface(void)
401 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SELECT_TIF
)) {
402 if (iface
!= JAYLINK_TIF_JTAG
) {
403 LOG_ERROR("Device supports JTAG transport only.");
404 return ERROR_JTAG_INIT_FAILED
;
410 ret
= jaylink_get_available_interfaces(devh
, &interfaces
);
412 if (ret
!= JAYLINK_OK
) {
413 LOG_ERROR("jaylink_get_available_interfaces() failed: %s.",
414 jaylink_strerror(ret
));
415 return ERROR_JTAG_INIT_FAILED
;
418 if (!(interfaces
& (1 << iface
))) {
419 LOG_ERROR("Selected transport is not supported by the device.");
420 return ERROR_JTAG_INIT_FAILED
;
423 ret
= jaylink_select_interface(devh
, iface
, NULL
);
426 LOG_ERROR("jaylink_select_interface() failed: %s.",
427 jaylink_strerror(ret
));
428 return ERROR_JTAG_INIT_FAILED
;
434 static int jlink_register(void)
441 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_REGISTER
))
444 ret
= jaylink_register(devh
, &conn
, connlist
, &count
);
446 if (ret
!= JAYLINK_OK
) {
447 LOG_ERROR("jaylink_register() failed: %s.", jaylink_strerror(ret
));
451 handle_found
= false;
453 for (i
= 0; i
< count
; i
++) {
454 if (connlist
[i
].handle
== conn
.handle
) {
461 LOG_ERROR("Registration failed: maximum number of connections on the "
470 * Adjust the SWD transaction buffer size depending on the free device internal
471 * memory. This ensures that the SWD transactions sent to the device do not
472 * exceed the internal memory of the device.
474 static bool adjust_swd_buffer_size(void)
479 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
))
482 ret
= jaylink_get_free_memory(devh
, &tmp
);
484 if (ret
!= JAYLINK_OK
) {
485 LOG_ERROR("jaylink_get_free_memory() failed: %s.",
486 jaylink_strerror(ret
));
491 LOG_ERROR("Not enough free device internal memory: %u bytes.", tmp
);
495 tmp
= MIN(JLINK_TAP_BUFFER_SIZE
, (tmp
- 16) / 2);
497 if (tmp
!= swd_buffer_size
) {
498 swd_buffer_size
= tmp
;
499 LOG_DEBUG("Adjusted SWD transaction buffer size to %u bytes.",
506 static int jaylink_log_handler(const struct jaylink_context
*ctx
,
507 enum jaylink_log_level level
, const char *format
, va_list args
,
513 case JAYLINK_LOG_LEVEL_ERROR
:
516 case JAYLINK_LOG_LEVEL_WARNING
:
517 tmp
= LOG_LVL_WARNING
;
520 * Forward info messages to the debug output because they are more verbose
521 * than info messages of OpenOCD.
523 case JAYLINK_LOG_LEVEL_INFO
:
524 case JAYLINK_LOG_LEVEL_DEBUG
:
527 case JAYLINK_LOG_LEVEL_DEBUG_IO
:
528 tmp
= LOG_LVL_DEBUG_IO
;
531 tmp
= LOG_LVL_WARNING
;
534 log_vprintf_lf(tmp
, __FILE__
, __LINE__
, __func__
, format
, args
);
539 static bool jlink_usb_location_equal(struct jaylink_device
*dev
)
547 retval
= jaylink_device_get_usb_bus_ports(dev
, &bus
, &ports
, &num_ports
);
549 if (retval
== JAYLINK_ERR_NOT_SUPPORTED
) {
551 } else if (retval
!= JAYLINK_OK
) {
552 LOG_WARNING("jaylink_device_get_usb_bus_ports() failed: %s.",
553 jaylink_strerror(retval
));
557 equal
= jtag_usb_location_equal(bus
, ports
, num_ports
);
564 static int jlink_init(void)
567 struct jaylink_device
**devs
;
571 char *firmware_version
;
572 struct jaylink_hardware_version hwver
;
573 struct jaylink_hardware_status hwstatus
;
574 enum jaylink_usb_address address
;
577 uint32_t host_interfaces
;
579 LOG_DEBUG("Using libjaylink %s (compiled with %s).",
580 jaylink_version_package_get_string(), JAYLINK_VERSION_PACKAGE_STRING
);
582 if (!jaylink_library_has_cap(JAYLINK_CAP_HIF_USB
) && use_usb_address
) {
583 LOG_ERROR("J-Link driver does not support USB devices.");
584 return ERROR_JTAG_INIT_FAILED
;
587 ret
= jaylink_init(&jayctx
);
589 if (ret
!= JAYLINK_OK
) {
590 LOG_ERROR("jaylink_init() failed: %s.", jaylink_strerror(ret
));
591 return ERROR_JTAG_INIT_FAILED
;
594 ret
= jaylink_log_set_callback(jayctx
, &jaylink_log_handler
, NULL
);
596 if (ret
!= JAYLINK_OK
) {
597 LOG_ERROR("jaylink_log_set_callback() failed: %s.",
598 jaylink_strerror(ret
));
599 jaylink_exit(jayctx
);
600 return ERROR_JTAG_INIT_FAILED
;
603 host_interfaces
= JAYLINK_HIF_USB
;
605 if (use_serial_number
)
606 host_interfaces
|= JAYLINK_HIF_TCP
;
608 ret
= jaylink_discovery_scan(jayctx
, host_interfaces
);
610 if (ret
!= JAYLINK_OK
) {
611 LOG_ERROR("jaylink_discovery_scan() failed: %s.",
612 jaylink_strerror(ret
));
613 jaylink_exit(jayctx
);
614 return ERROR_JTAG_INIT_FAILED
;
617 ret
= jaylink_get_devices(jayctx
, &devs
, &num_devices
);
619 if (ret
!= JAYLINK_OK
) {
620 LOG_ERROR("jaylink_get_devices() failed: %s.", jaylink_strerror(ret
));
621 jaylink_exit(jayctx
);
622 return ERROR_JTAG_INIT_FAILED
;
625 use_usb_location
= (jtag_usb_get_location() != NULL
);
627 if (!use_serial_number
&& !use_usb_address
&& !use_usb_location
&& num_devices
> 1) {
628 LOG_ERROR("Multiple devices found, specify the desired device.");
629 jaylink_free_devices(devs
, true);
630 jaylink_exit(jayctx
);
631 return ERROR_JTAG_INIT_FAILED
;
634 found_device
= false;
636 for (i
= 0; devs
[i
]; i
++) {
637 struct jaylink_device
*dev
= devs
[i
];
639 if (use_serial_number
) {
640 ret
= jaylink_device_get_serial_number(dev
, &tmp
);
642 if (ret
== JAYLINK_ERR_NOT_AVAILABLE
) {
644 } else if (ret
!= JAYLINK_OK
) {
645 LOG_WARNING("jaylink_device_get_serial_number() failed: %s.",
646 jaylink_strerror(ret
));
650 if (serial_number
!= tmp
)
654 if (use_usb_address
) {
655 ret
= jaylink_device_get_usb_address(dev
, &address
);
657 if (ret
== JAYLINK_ERR_NOT_SUPPORTED
) {
659 } else if (ret
!= JAYLINK_OK
) {
660 LOG_WARNING("jaylink_device_get_usb_address() failed: %s.",
661 jaylink_strerror(ret
));
665 if (usb_address
!= address
)
669 if (use_usb_location
&& !jlink_usb_location_equal(dev
))
672 ret
= jaylink_open(dev
, &devh
);
674 if (ret
== JAYLINK_OK
) {
679 LOG_ERROR("Failed to open device: %s.", jaylink_strerror(ret
));
682 jaylink_free_devices(devs
, true);
685 LOG_ERROR("No J-Link device found.");
686 jaylink_exit(jayctx
);
687 return ERROR_JTAG_INIT_FAILED
;
691 * Be careful with changing the following initialization sequence because
692 * some devices are known to be sensitive regarding the order.
695 ret
= jaylink_get_firmware_version(devh
, &firmware_version
, &length
);
697 if (ret
!= JAYLINK_OK
) {
698 LOG_ERROR("jaylink_get_firmware_version() failed: %s.",
699 jaylink_strerror(ret
));
701 jaylink_exit(jayctx
);
702 return ERROR_JTAG_INIT_FAILED
;
703 } else if (length
> 0) {
704 LOG_INFO("%s", firmware_version
);
705 free(firmware_version
);
707 LOG_WARNING("Device responds empty firmware version string.");
710 memset(caps
, 0, JAYLINK_DEV_EXT_CAPS_SIZE
);
711 ret
= jaylink_get_caps(devh
, caps
);
713 if (ret
!= JAYLINK_OK
) {
714 LOG_ERROR("jaylink_get_caps() failed: %s.", jaylink_strerror(ret
));
716 jaylink_exit(jayctx
);
717 return ERROR_JTAG_INIT_FAILED
;
720 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_EXT_CAPS
)) {
721 ret
= jaylink_get_extended_caps(devh
, caps
);
723 if (ret
!= JAYLINK_OK
) {
724 LOG_ERROR("jaylink_get_extended_caps() failed: %s.",
725 jaylink_strerror(ret
));
727 jaylink_exit(jayctx
);
728 return ERROR_JTAG_INIT_FAILED
;
732 jtag_command_version
= JAYLINK_JTAG_VERSION_2
;
734 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_HW_VERSION
)) {
735 ret
= jaylink_get_hardware_version(devh
, &hwver
);
737 if (ret
!= JAYLINK_OK
) {
738 LOG_ERROR("Failed to retrieve hardware version: %s.",
739 jaylink_strerror(ret
));
741 jaylink_exit(jayctx
);
742 return ERROR_JTAG_INIT_FAILED
;
745 LOG_INFO("Hardware version: %u.%02u", hwver
.major
, hwver
.minor
);
747 if (hwver
.major
>= 5)
748 jtag_command_version
= JAYLINK_JTAG_VERSION_3
;
751 if (iface
== JAYLINK_TIF_SWD
) {
753 * Adjust the SWD transaction buffer size in case there is already
754 * allocated memory on the device. This happens for example if the
755 * memory for SWO capturing is still allocated because the software
756 * which used the device before has not been shut down properly.
758 if (!adjust_swd_buffer_size()) {
760 jaylink_exit(jayctx
);
761 return ERROR_JTAG_INIT_FAILED
;
765 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
766 if (!read_device_config(&config
)) {
767 LOG_ERROR("Failed to read device configuration data.");
769 jaylink_exit(jayctx
);
770 return ERROR_JTAG_INIT_FAILED
;
773 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
776 ret
= jaylink_get_hardware_status(devh
, &hwstatus
);
778 if (ret
!= JAYLINK_OK
) {
779 LOG_ERROR("jaylink_get_hardware_status() failed: %s.",
780 jaylink_strerror(ret
));
782 jaylink_exit(jayctx
);
783 return ERROR_JTAG_INIT_FAILED
;
786 LOG_INFO("VTarget = %u.%03u V", hwstatus
.target_voltage
/ 1000,
787 hwstatus
.target_voltage
% 1000);
791 strcpy(conn
.hid
, "0.0.0.0");
795 ret
= jlink_register();
797 if (ret
!= ERROR_OK
) {
799 jaylink_exit(jayctx
);
800 return ERROR_JTAG_INIT_FAILED
;
803 ret
= select_interface();
805 if (ret
!= ERROR_OK
) {
807 jaylink_exit(jayctx
);
815 jlink_speed(jtag_get_speed_khz());
817 if (iface
== JAYLINK_TIF_JTAG
) {
819 * J-Link devices with firmware version v5 and v6 seems to have an issue
820 * if the first tap move is not divisible by 8, so we send a TLR on
824 jlink_clock_data(NULL
, 0, &tms
, 0, NULL
, 0, 8);
832 static int jlink_quit(void)
838 ret
= jaylink_swo_stop(devh
);
840 if (ret
!= JAYLINK_OK
)
841 LOG_ERROR("jaylink_swo_stop() failed: %s.", jaylink_strerror(ret
));
844 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_REGISTER
)) {
845 ret
= jaylink_unregister(devh
, &conn
, connlist
, &count
);
847 if (ret
!= JAYLINK_OK
)
848 LOG_ERROR("jaylink_unregister() failed: %s.",
849 jaylink_strerror(ret
));
853 jaylink_exit(jayctx
);
858 /***************************************************************************/
859 /* Queue command implementations */
861 static void jlink_end_state(tap_state_t state
)
863 if (tap_is_state_stable(state
))
864 tap_set_end_state(state
);
866 LOG_ERROR("BUG: %i is not a valid end state", state
);
871 /* Goes to the end state. */
872 static void jlink_state_move(void)
875 uint8_t tms_scan_bits
;
877 tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
878 tms_scan_bits
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
880 jlink_clock_data(NULL
, 0, &tms_scan
, 0, NULL
, 0, tms_scan_bits
);
882 tap_set_state(tap_get_end_state());
885 static void jlink_path_move(int num_states
, tap_state_t
*path
)
890 for (i
= 0; i
< num_states
; i
++) {
891 if (path
[i
] == tap_state_transition(tap_get_state(), false))
892 jlink_clock_data(NULL
, 0, NULL
, 0, NULL
, 0, 1);
893 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
894 jlink_clock_data(NULL
, 0, &tms
, 0, NULL
, 0, 1);
896 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
897 tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
901 tap_set_state(path
[i
]);
904 tap_set_end_state(tap_get_state());
907 static void jlink_stableclocks(int num_cycles
)
911 uint8_t tms
= tap_get_state() == TAP_RESET
;
912 /* Execute num_cycles. */
913 for (i
= 0; i
< num_cycles
; i
++)
914 jlink_clock_data(NULL
, 0, &tms
, 0, NULL
, 0, 1);
917 static void jlink_runtest(int num_cycles
)
919 tap_state_t saved_end_state
= tap_get_end_state();
921 /* Only do a state_move when we're not already in IDLE. */
922 if (tap_get_state() != TAP_IDLE
) {
923 jlink_end_state(TAP_IDLE
);
928 jlink_stableclocks(num_cycles
);
930 /* Finish in end_state. */
931 jlink_end_state(saved_end_state
);
933 if (tap_get_state() != tap_get_end_state())
937 static void jlink_reset(int trst
, int srst
)
939 LOG_DEBUG("TRST: %i, SRST: %i.", trst
, srst
);
941 /* Signals are active low. */
943 jaylink_set_reset(devh
);
946 jaylink_clear_reset(devh
);
949 jaylink_jtag_clear_trst(devh
);
952 jaylink_jtag_set_trst(devh
);
955 COMMAND_HANDLER(jlink_usb_command
)
960 command_print(CMD
, "Need exactly one argument for jlink usb.");
961 return ERROR_COMMAND_SYNTAX_ERROR
;
964 if (sscanf(CMD_ARGV
[0], "%i", &tmp
) != 1) {
965 command_print(CMD
, "Invalid USB address: %s.", CMD_ARGV
[0]);
969 if (tmp
< JAYLINK_USB_ADDRESS_0
|| tmp
> JAYLINK_USB_ADDRESS_3
) {
970 command_print(CMD
, "Invalid USB address: %s.", CMD_ARGV
[0]);
976 use_serial_number
= false;
977 use_usb_address
= true;
982 COMMAND_HANDLER(jlink_serial_command
)
987 command_print(CMD
, "Need exactly one argument for jlink serial.");
988 return ERROR_COMMAND_SYNTAX_ERROR
;
991 ret
= jaylink_parse_serial_number(CMD_ARGV
[0], &serial_number
);
993 if (ret
== JAYLINK_ERR
) {
994 command_print(CMD
, "Invalid serial number: %s.", CMD_ARGV
[0]);
996 } else if (ret
!= JAYLINK_OK
) {
997 command_print(CMD
, "jaylink_parse_serial_number() failed: %s.",
998 jaylink_strerror(ret
));
1002 use_serial_number
= true;
1003 use_usb_address
= false;
1008 COMMAND_HANDLER(jlink_handle_hwstatus_command
)
1011 struct jaylink_hardware_status status
;
1013 ret
= jaylink_get_hardware_status(devh
, &status
);
1015 if (ret
!= JAYLINK_OK
) {
1016 command_print(CMD
, "jaylink_get_hardware_status() failed: %s.",
1017 jaylink_strerror(ret
));
1021 command_print(CMD
, "VTarget = %u.%03u V",
1022 status
.target_voltage
/ 1000, status
.target_voltage
% 1000);
1024 command_print(CMD
, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
1025 "TRST = %u", status
.tck
, status
.tdi
, status
.tdo
, status
.tms
,
1026 status
.tres
, status
.trst
);
1028 if (status
.target_voltage
< 1500)
1029 command_print(CMD
, "Target voltage too low. Check target power.");
1034 COMMAND_HANDLER(jlink_handle_free_memory_command
)
1039 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
)) {
1040 command_print(CMD
, "Retrieval of free memory is not supported by "
1045 ret
= jaylink_get_free_memory(devh
, &tmp
);
1047 if (ret
!= JAYLINK_OK
) {
1048 command_print(CMD
, "jaylink_get_free_memory() failed: %s.",
1049 jaylink_strerror(ret
));
1053 command_print(CMD
, "Device has %u bytes of free memory.", tmp
);
1058 COMMAND_HANDLER(jlink_handle_jlink_jtag_command
)
1064 switch (jtag_command_version
) {
1065 case JAYLINK_JTAG_VERSION_2
:
1068 case JAYLINK_JTAG_VERSION_3
:
1075 command_print(CMD
, "JTAG command version: %i", version
);
1076 } else if (CMD_ARGC
== 1) {
1077 if (sscanf(CMD_ARGV
[0], "%i", &tmp
) != 1) {
1078 command_print(CMD
, "Invalid argument: %s.", CMD_ARGV
[0]);
1079 return ERROR_COMMAND_SYNTAX_ERROR
;
1084 jtag_command_version
= JAYLINK_JTAG_VERSION_2
;
1087 jtag_command_version
= JAYLINK_JTAG_VERSION_3
;
1090 command_print(CMD
, "Invalid argument: %s.", CMD_ARGV
[0]);
1091 return ERROR_COMMAND_SYNTAX_ERROR
;
1094 command_print(CMD
, "Need exactly one argument for jlink jtag.");
1095 return ERROR_COMMAND_SYNTAX_ERROR
;
1101 COMMAND_HANDLER(jlink_handle_target_power_command
)
1106 if (CMD_ARGC
!= 1) {
1107 command_print(CMD
, "Need exactly one argument for jlink "
1109 return ERROR_COMMAND_SYNTAX_ERROR
;
1112 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
)) {
1113 command_print(CMD
, "Target power supply is not supported by the "
1118 if (!strcmp(CMD_ARGV
[0], "on")) {
1120 } else if (!strcmp(CMD_ARGV
[0], "off")) {
1123 command_print(CMD
, "Invalid argument: %s.", CMD_ARGV
[0]);
1127 ret
= jaylink_set_target_power(devh
, enable
);
1129 if (ret
!= JAYLINK_OK
) {
1130 command_print(CMD
, "jaylink_set_target_power() failed: %s.",
1131 jaylink_strerror(ret
));
1138 static void show_config_usb_address(struct command_invocation
*cmd
)
1140 if (config
.usb_address
!= tmp_config
.usb_address
)
1141 command_print(cmd
, "USB address: %u [%u]", config
.usb_address
,
1142 tmp_config
.usb_address
);
1144 command_print(cmd
, "USB address: %u", config
.usb_address
);
1147 static void show_config_ip_address(struct command_invocation
*cmd
)
1149 if (!memcmp(config
.ip_address
, tmp_config
.ip_address
, 4))
1150 command_print(cmd
, "IP address: %d.%d.%d.%d",
1151 config
.ip_address
[3], config
.ip_address
[2],
1152 config
.ip_address
[1], config
.ip_address
[0]);
1154 command_print(cmd
, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1155 config
.ip_address
[3], config
.ip_address
[2],
1156 config
.ip_address
[1], config
.ip_address
[0],
1157 tmp_config
.ip_address
[3], tmp_config
.ip_address
[2],
1158 tmp_config
.ip_address
[1], tmp_config
.ip_address
[0]);
1160 if (!memcmp(config
.subnet_mask
, tmp_config
.subnet_mask
, 4))
1161 command_print(cmd
, "Subnet mask: %d.%d.%d.%d",
1162 config
.subnet_mask
[3], config
.subnet_mask
[2],
1163 config
.subnet_mask
[1], config
.subnet_mask
[0]);
1165 command_print(cmd
, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1166 config
.subnet_mask
[3], config
.subnet_mask
[2],
1167 config
.subnet_mask
[1], config
.subnet_mask
[0],
1168 tmp_config
.subnet_mask
[3], tmp_config
.subnet_mask
[2],
1169 tmp_config
.subnet_mask
[1], tmp_config
.subnet_mask
[0]);
1172 static void show_config_mac_address(struct command_invocation
*cmd
)
1174 if (!memcmp(config
.mac_address
, tmp_config
.mac_address
, 6))
1175 command_print(cmd
, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1176 config
.mac_address
[5], config
.mac_address
[4],
1177 config
.mac_address
[3], config
.mac_address
[2],
1178 config
.mac_address
[1], config
.mac_address
[0]);
1180 command_print(cmd
, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1181 "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1182 config
.mac_address
[5], config
.mac_address
[4],
1183 config
.mac_address
[3], config
.mac_address
[2],
1184 config
.mac_address
[1], config
.mac_address
[0],
1185 tmp_config
.mac_address
[5], tmp_config
.mac_address
[4],
1186 tmp_config
.mac_address
[3], tmp_config
.mac_address
[2],
1187 tmp_config
.mac_address
[1], tmp_config
.mac_address
[0]);
1190 static void show_config_target_power(struct command_invocation
*cmd
)
1192 const char *target_power
;
1193 const char *current_target_power
;
1195 if (!config
.target_power
)
1196 target_power
= "off";
1198 target_power
= "on";
1200 if (!tmp_config
.target_power
)
1201 current_target_power
= "off";
1203 current_target_power
= "on";
1205 if (config
.target_power
!= tmp_config
.target_power
)
1206 command_print(cmd
, "Target power supply: %s [%s]", target_power
,
1207 current_target_power
);
1209 command_print(cmd
, "Target power supply: %s", target_power
);
1212 static void show_config(struct command_invocation
*cmd
)
1214 command_print(cmd
, "J-Link device configuration:");
1216 show_config_usb_address(cmd
);
1218 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
))
1219 show_config_target_power(cmd
);
1221 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1222 show_config_ip_address(cmd
);
1223 show_config_mac_address(cmd
);
1227 static int poll_trace(uint8_t *buf
, size_t *size
)
1234 ret
= jaylink_swo_read(devh
, buf
, &length
);
1236 if (ret
!= JAYLINK_OK
) {
1237 LOG_ERROR("jaylink_swo_read() failed: %s.", jaylink_strerror(ret
));
1246 static uint32_t calculate_trace_buffer_size(void)
1251 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
))
1254 ret
= jaylink_get_free_memory(devh
, &tmp
);
1256 if (ret
!= JAYLINK_OK
) {
1257 LOG_ERROR("jaylink_get_free_memory() failed: %s.",
1258 jaylink_strerror(ret
));
1262 if (tmp
> 0x3fff || tmp
<= 0x600)
1267 return tmp
& 0xffffff00;
1270 static bool check_trace_freq(struct jaylink_swo_speed speed
,
1271 uint32_t trace_freq
)
1277 min
= fabs(1.0 - (speed
.freq
/ ((double)trace_freq
* speed
.min_div
)));
1279 for (divider
= speed
.min_div
; divider
< speed
.max_div
; divider
++) {
1280 deviation
= fabs(1.0 - (speed
.freq
/ ((double)trace_freq
* divider
)));
1282 if (deviation
< 0.03) {
1283 LOG_DEBUG("Found suitable frequency divider %u with deviation of "
1284 "%.02f %%.", divider
, deviation
);
1288 if (deviation
< min
)
1292 LOG_ERROR("Selected trace frequency is not supported by the device. "
1293 "Please choose a different trace frequency.");
1294 LOG_ERROR("Maximum permitted deviation is 3.00 %%, but only %.02f %% "
1295 "could be achieved.", min
* 100);
1300 static int config_trace(bool enabled
, enum tpiu_pin_protocol pin_protocol
,
1301 uint32_t port_size
, unsigned int *trace_freq
)
1304 uint32_t buffer_size
;
1305 struct jaylink_swo_speed speed
;
1307 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SWO
)) {
1308 LOG_ERROR("Trace capturing is not supported by the device.");
1312 if (pin_protocol
!= TPIU_PIN_PROTOCOL_ASYNC_UART
) {
1313 LOG_ERROR("Selected pin protocol is not supported.");
1317 trace_enabled
= enabled
;
1319 ret
= jaylink_swo_stop(devh
);
1321 if (ret
!= JAYLINK_OK
) {
1322 LOG_ERROR("jaylink_swo_stop() failed: %s.", jaylink_strerror(ret
));
1328 * Adjust the SWD transaction buffer size as stopping SWO capturing
1329 * deallocates device internal memory.
1331 if (!adjust_swd_buffer_size())
1337 buffer_size
= calculate_trace_buffer_size();
1340 LOG_ERROR("Not enough free device memory to start trace capturing.");
1344 ret
= jaylink_swo_get_speeds(devh
, JAYLINK_SWO_MODE_UART
, &speed
);
1346 if (ret
!= JAYLINK_OK
) {
1347 LOG_ERROR("jaylink_swo_get_speeds() failed: %s.",
1348 jaylink_strerror(ret
));
1353 *trace_freq
= speed
.freq
/ speed
.min_div
;
1355 if (!check_trace_freq(speed
, *trace_freq
))
1358 LOG_DEBUG("Using %u bytes device memory for trace capturing.", buffer_size
);
1360 ret
= jaylink_swo_start(devh
, JAYLINK_SWO_MODE_UART
, *trace_freq
,
1363 if (ret
!= JAYLINK_OK
) {
1364 LOG_ERROR("jaylink_start_swo() failed: %s.", jaylink_strerror(ret
));
1369 * Adjust the SWD transaction buffer size as starting SWO capturing
1370 * allocates device internal memory.
1372 if (!adjust_swd_buffer_size())
1378 COMMAND_HANDLER(jlink_handle_config_usb_address_command
)
1382 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1383 command_print(CMD
, "Reading configuration is not supported by the "
1389 show_config_usb_address(CMD
);
1390 } else if (CMD_ARGC
== 1) {
1391 if (sscanf(CMD_ARGV
[0], "%" SCNd8
, &tmp
) != 1) {
1392 command_print(CMD
, "Invalid USB address: %s.", CMD_ARGV
[0]);
1396 if (tmp
> JAYLINK_USB_ADDRESS_3
) {
1397 command_print(CMD
, "Invalid USB address: %u.", tmp
);
1401 tmp_config
.usb_address
= tmp
;
1403 command_print(CMD
, "Need exactly one argument for jlink config "
1405 return ERROR_COMMAND_SYNTAX_ERROR
;
1411 COMMAND_HANDLER(jlink_handle_config_target_power_command
)
1415 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1416 command_print(CMD
, "Reading configuration is not supported by the "
1421 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
)) {
1422 command_print(CMD
, "Target power supply is not supported by the "
1428 show_config_target_power(CMD
);
1429 } else if (CMD_ARGC
== 1) {
1430 if (!strcmp(CMD_ARGV
[0], "on")) {
1432 } else if (!strcmp(CMD_ARGV
[0], "off")) {
1435 command_print(CMD
, "Invalid argument: %s.", CMD_ARGV
[0]);
1439 tmp_config
.target_power
= enable
;
1441 command_print(CMD
, "Need exactly one argument for jlink config "
1443 return ERROR_COMMAND_SYNTAX_ERROR
;
1449 COMMAND_HANDLER(jlink_handle_config_mac_address_command
)
1456 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1457 command_print(CMD
, "Reading configuration is not supported by the "
1462 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1463 command_print(CMD
, "Ethernet connectivity is not supported by the "
1469 show_config_mac_address(CMD
);
1470 } else if (CMD_ARGC
== 1) {
1473 if ((strlen(str
) != 17) || (str
[2] != ':' || str
[5] != ':' || \
1474 str
[8] != ':' || str
[11] != ':' || str
[14] != ':')) {
1475 command_print(CMD
, "Invalid MAC address format.");
1476 return ERROR_COMMAND_SYNTAX_ERROR
;
1479 for (i
= 5; i
>= 0; i
--) {
1480 addr
[i
] = strtoul(str
, &e
, 16);
1484 if (!(addr
[0] | addr
[1] | addr
[2] | addr
[3] | addr
[4] | addr
[5])) {
1485 command_print(CMD
, "Invalid MAC address: zero address.");
1486 return ERROR_COMMAND_SYNTAX_ERROR
;
1489 if (!(0x01 & addr
[0])) {
1490 command_print(CMD
, "Invalid MAC address: multicast address.");
1491 return ERROR_COMMAND_SYNTAX_ERROR
;
1494 memcpy(tmp_config
.mac_address
, addr
, sizeof(addr
));
1496 command_print(CMD
, "Need exactly one argument for jlink config "
1498 return ERROR_COMMAND_SYNTAX_ERROR
;
1504 static bool string_to_ip(const char *s
, uint8_t *ip
, int *pos
)
1508 const char *s_save
= s
;
1514 for (i
= 0; i
< 4; i
++) {
1515 lip
[i
] = strtoul(s
, &e
, 10);
1517 if (*e
!= '.' && i
!= 3)
1524 memcpy(ip
, lip
, sizeof(lip
));
1529 static void cpy_ip(uint8_t *dst
, uint8_t *src
)
1533 for (i
= 0, j
= 3; i
< 4; i
++, j
--)
1537 COMMAND_HANDLER(jlink_handle_config_ip_address_command
)
1539 uint8_t ip_address
[4];
1540 uint32_t subnet_mask
= 0;
1542 uint8_t subnet_bits
= 24;
1544 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1545 command_print(CMD
, "Reading configuration is not supported by the "
1550 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1551 command_print(CMD
, "Ethernet connectivity is not supported by the "
1557 show_config_ip_address(CMD
);
1559 if (!string_to_ip(CMD_ARGV
[0], ip_address
, &i
))
1560 return ERROR_COMMAND_SYNTAX_ERROR
;
1562 len
= strlen(CMD_ARGV
[0]);
1564 /* Check for format A.B.C.D/E. */
1566 if (CMD_ARGV
[0][i
] != '/')
1567 return ERROR_COMMAND_SYNTAX_ERROR
;
1569 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0] + i
+ 1, subnet_bits
);
1570 } else if (CMD_ARGC
> 1) {
1571 if (!string_to_ip(CMD_ARGV
[1], (uint8_t *)&subnet_mask
, &i
))
1572 return ERROR_COMMAND_SYNTAX_ERROR
;
1576 subnet_mask
= (uint32_t)(subnet_bits
< 32 ?
1577 ((1ULL << subnet_bits
) - 1) : 0xffffffff);
1579 cpy_ip(tmp_config
.ip_address
, ip_address
);
1580 cpy_ip(tmp_config
.subnet_mask
, (uint8_t *)&subnet_mask
);
1586 COMMAND_HANDLER(jlink_handle_config_reset_command
)
1588 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
))
1591 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
1597 COMMAND_HANDLER(jlink_handle_config_write_command
)
1601 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1602 command_print(CMD
, "Reading configuration is not supported by the "
1607 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_WRITE_CONFIG
)) {
1608 command_print(CMD
, "Writing configuration is not supported by the "
1613 if (!memcmp(&config
, &tmp_config
, sizeof(struct device_config
))) {
1614 command_print(CMD
, "Operation not performed due to no changes in "
1615 "the configuration.");
1619 ret
= jaylink_write_raw_config(devh
, (const uint8_t *)&tmp_config
);
1621 if (ret
!= JAYLINK_OK
) {
1622 LOG_ERROR("jaylink_write_raw_config() failed: %s.",
1623 jaylink_strerror(ret
));
1627 if (!read_device_config(&config
)) {
1628 LOG_ERROR("Failed to read device configuration for verification.");
1632 if (memcmp(&config
, &tmp_config
, sizeof(struct device_config
))) {
1633 LOG_ERROR("Verification of device configuration failed. Please check "
1638 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
1639 command_print(CMD
, "The new device configuration applies after power "
1640 "cycling the J-Link device.");
1645 COMMAND_HANDLER(jlink_handle_config_command
)
1647 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1648 command_print(CMD
, "Device doesn't support reading configuration.");
1658 COMMAND_HANDLER(jlink_handle_emucom_write_command
)
1668 return ERROR_COMMAND_SYNTAX_ERROR
;
1670 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_EMUCOM
)) {
1671 LOG_ERROR("Device does not support EMUCOM.");
1675 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], channel
);
1677 tmp
= strlen(CMD_ARGV
[1]);
1680 LOG_ERROR("Data must be encoded as hexadecimal pairs.");
1681 return ERROR_COMMAND_ARGUMENT_INVALID
;
1684 buf
= malloc(tmp
/ 2);
1687 LOG_ERROR("Failed to allocate buffer.");
1691 dummy
= unhexify(buf
, CMD_ARGV
[1], tmp
/ 2);
1693 if (dummy
!= (tmp
/ 2)) {
1694 LOG_ERROR("Data must be encoded as hexadecimal pairs.");
1696 return ERROR_COMMAND_ARGUMENT_INVALID
;
1700 ret
= jaylink_emucom_write(devh
, channel
, buf
, &length
);
1704 if (ret
== JAYLINK_ERR_DEV_NOT_SUPPORTED
) {
1705 LOG_ERROR("Channel not supported by the device.");
1707 } else if (ret
!= JAYLINK_OK
) {
1708 LOG_ERROR("Failed to write to channel: %s.", jaylink_strerror(ret
));
1712 if (length
!= (tmp
/ 2))
1713 LOG_WARNING("Only %" PRIu32
" bytes written to the channel.", length
);
1718 COMMAND_HANDLER(jlink_handle_emucom_read_command
)
1727 return ERROR_COMMAND_SYNTAX_ERROR
;
1729 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_EMUCOM
)) {
1730 LOG_ERROR("Device does not support EMUCOM.");
1734 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], channel
);
1735 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
1737 buf
= malloc(length
* 3 + 1);
1740 LOG_ERROR("Failed to allocate buffer.");
1744 ret
= jaylink_emucom_read(devh
, channel
, buf
, &length
);
1746 if (ret
== JAYLINK_ERR_DEV_NOT_SUPPORTED
) {
1747 LOG_ERROR("Channel is not supported by the device.");
1750 } else if (ret
== JAYLINK_ERR_DEV_NOT_AVAILABLE
) {
1751 LOG_ERROR("Channel is not available for the requested amount of data. "
1752 "%" PRIu32
" bytes are avilable.", length
);
1755 } else if (ret
!= JAYLINK_OK
) {
1756 LOG_ERROR("Failed to read from channel: %s.", jaylink_strerror(ret
));
1761 tmp
= hexify((char *)buf
+ length
, buf
, length
, 2 * length
+ 1);
1763 if (tmp
!= 2 * length
) {
1764 LOG_ERROR("Failed to convert data into hexadecimal string.");
1769 command_print(CMD
, "%s", buf
+ length
);
1775 static const struct command_registration jlink_config_subcommand_handlers
[] = {
1778 .handler
= &jlink_handle_config_usb_address_command
,
1779 .mode
= COMMAND_EXEC
,
1780 .help
= "set the USB address",
1784 .name
= "targetpower",
1785 .handler
= &jlink_handle_config_target_power_command
,
1786 .mode
= COMMAND_EXEC
,
1787 .help
= "set the target power supply",
1792 .handler
= &jlink_handle_config_mac_address_command
,
1793 .mode
= COMMAND_EXEC
,
1794 .help
= "set the MAC Address",
1795 .usage
= "[ff:ff:ff:ff:ff:ff]",
1799 .handler
= &jlink_handle_config_ip_address_command
,
1800 .mode
= COMMAND_EXEC
,
1801 .help
= "set the IP address, where A.B.C.D is the IP address, "
1802 "E the bit of the subnet mask, F.G.H.I the subnet mask",
1803 .usage
= "[A.B.C.D[/E] [F.G.H.I]]",
1807 .handler
= &jlink_handle_config_reset_command
,
1808 .mode
= COMMAND_EXEC
,
1809 .help
= "undo configuration changes",
1814 .handler
= &jlink_handle_config_write_command
,
1815 .mode
= COMMAND_EXEC
,
1816 .help
= "write configuration to the device",
1819 COMMAND_REGISTRATION_DONE
1822 static const struct command_registration jlink_emucom_subcommand_handlers
[] = {
1825 .handler
= &jlink_handle_emucom_write_command
,
1826 .mode
= COMMAND_EXEC
,
1827 .help
= "write to a channel",
1828 .usage
= "<channel> <data>",
1832 .handler
= &jlink_handle_emucom_read_command
,
1833 .mode
= COMMAND_EXEC
,
1834 .help
= "read from a channel",
1835 .usage
= "<channel> <length>"
1837 COMMAND_REGISTRATION_DONE
1840 static const struct command_registration jlink_subcommand_handlers
[] = {
1843 .handler
= &jlink_handle_jlink_jtag_command
,
1844 .mode
= COMMAND_EXEC
,
1845 .help
= "select the JTAG command version",
1849 .name
= "targetpower",
1850 .handler
= &jlink_handle_target_power_command
,
1851 .mode
= COMMAND_EXEC
,
1852 .help
= "set the target power supply",
1857 .handler
= &jlink_handle_free_memory_command
,
1858 .mode
= COMMAND_EXEC
,
1859 .help
= "show free device memory",
1864 .handler
= &jlink_handle_hwstatus_command
,
1865 .mode
= COMMAND_EXEC
,
1866 .help
= "show the hardware status",
1871 .handler
= &jlink_usb_command
,
1872 .mode
= COMMAND_CONFIG
,
1873 .help
= "set the USB address of the device that should be used",
1878 .handler
= &jlink_serial_command
,
1879 .mode
= COMMAND_CONFIG
,
1880 .help
= "set the serial number of the device that should be used",
1881 .usage
= "<serial number>"
1885 .handler
= &jlink_handle_config_command
,
1886 .mode
= COMMAND_EXEC
,
1887 .help
= "access the device configuration. If no argument is given "
1888 "this will show the device configuration",
1889 .chain
= jlink_config_subcommand_handlers
,
1894 .mode
= COMMAND_EXEC
,
1895 .help
= "access EMUCOM channel",
1896 .chain
= jlink_emucom_subcommand_handlers
,
1899 COMMAND_REGISTRATION_DONE
1902 static const struct command_registration jlink_command_handlers
[] = {
1905 .mode
= COMMAND_ANY
,
1906 .help
= "perform jlink management",
1907 .chain
= jlink_subcommand_handlers
,
1910 COMMAND_REGISTRATION_DONE
1913 static int jlink_swd_init(void)
1915 iface
= JAYLINK_TIF_SWD
;
1920 static void jlink_swd_write_reg(uint8_t cmd
, uint32_t value
, uint32_t ap_delay_clk
)
1922 assert(!(cmd
& SWD_CMD_RnW
));
1923 jlink_swd_queue_cmd(cmd
, NULL
, value
, ap_delay_clk
);
1926 static void jlink_swd_read_reg(uint8_t cmd
, uint32_t *value
, uint32_t ap_delay_clk
)
1928 assert(cmd
& SWD_CMD_RnW
);
1929 jlink_swd_queue_cmd(cmd
, value
, 0, ap_delay_clk
);
1932 /***************************************************************************/
1933 /* J-Link tap functions */
1935 static unsigned tap_length
;
1936 /* In SWD mode use tms buffer for direction control */
1937 static uint8_t tms_buffer
[JLINK_TAP_BUFFER_SIZE
];
1938 static uint8_t tdi_buffer
[JLINK_TAP_BUFFER_SIZE
];
1939 static uint8_t tdo_buffer
[JLINK_TAP_BUFFER_SIZE
];
1941 struct pending_scan_result
{
1942 /** First bit position in tdo_buffer to read. */
1944 /** Number of bits to read. */
1946 /** Location to store the result */
1948 /** Offset in the destination buffer */
1949 unsigned buffer_offset
;
1952 #define MAX_PENDING_SCAN_RESULTS 256
1954 static int pending_scan_results_length
;
1955 static struct pending_scan_result pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
1957 static void jlink_tap_init(void)
1960 pending_scan_results_length
= 0;
1961 memset(tms_buffer
, 0, sizeof(tms_buffer
));
1962 memset(tdi_buffer
, 0, sizeof(tdi_buffer
));
1965 static void jlink_clock_data(const uint8_t *out
, unsigned out_offset
,
1966 const uint8_t *tms_out
, unsigned tms_offset
,
1967 uint8_t *in
, unsigned in_offset
,
1971 unsigned available_length
= JLINK_TAP_BUFFER_SIZE
- tap_length
/ 8;
1973 if (!available_length
||
1974 (in
&& pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
)) {
1975 if (jlink_flush() != ERROR_OK
)
1977 available_length
= JLINK_TAP_BUFFER_SIZE
;
1980 struct pending_scan_result
*pending_scan_result
=
1981 &pending_scan_results_buffer
[pending_scan_results_length
];
1983 unsigned scan_length
= length
> available_length
?
1984 available_length
: length
;
1987 buf_set_buf(out
, out_offset
, tdi_buffer
, tap_length
, scan_length
);
1989 buf_set_buf(tms_out
, tms_offset
, tms_buffer
, tap_length
, scan_length
);
1992 pending_scan_result
->first
= tap_length
;
1993 pending_scan_result
->length
= scan_length
;
1994 pending_scan_result
->buffer
= in
;
1995 pending_scan_result
->buffer_offset
= in_offset
;
1996 pending_scan_results_length
++;
1999 tap_length
+= scan_length
;
2000 out_offset
+= scan_length
;
2001 tms_offset
+= scan_length
;
2002 in_offset
+= scan_length
;
2003 length
-= scan_length
;
2004 } while (length
> 0);
2007 static int jlink_flush(void)
2015 jlink_last_state
= jtag_debug_state_machine(tms_buffer
, tdi_buffer
,
2016 tap_length
, jlink_last_state
);
2018 ret
= jaylink_jtag_io(devh
, tms_buffer
, tdi_buffer
, tdo_buffer
,
2019 tap_length
, jtag_command_version
);
2021 if (ret
!= JAYLINK_OK
) {
2022 LOG_ERROR("jaylink_jtag_io() failed: %s.", jaylink_strerror(ret
));
2024 return ERROR_JTAG_QUEUE_FAILED
;
2027 for (i
= 0; i
< pending_scan_results_length
; i
++) {
2028 struct pending_scan_result
*p
= &pending_scan_results_buffer
[i
];
2030 buf_set_buf(tdo_buffer
, p
->first
, p
->buffer
,
2031 p
->buffer_offset
, p
->length
);
2033 LOG_DEBUG_IO("Pending scan result, length = %d.", p
->length
);
2041 static void fill_buffer(uint8_t *buf
, uint32_t val
, uint32_t len
)
2043 unsigned int tap_pos
= tap_length
;
2046 buf_set_u32(buf
, tap_pos
, 32, val
);
2052 buf_set_u32(buf
, tap_pos
, len
, val
);
2055 static void jlink_queue_data_out(const uint8_t *data
, uint32_t len
)
2057 const uint32_t dir_out
= 0xffffffff;
2060 bit_copy(tdi_buffer
, tap_length
, data
, 0, len
);
2062 fill_buffer(tdi_buffer
, 0, len
);
2064 fill_buffer(tms_buffer
, dir_out
, len
);
2068 static void jlink_queue_data_in(uint32_t len
)
2070 const uint32_t dir_in
= 0;
2072 fill_buffer(tms_buffer
, dir_in
, len
);
2076 static int jlink_swd_switch_seq(enum swd_special_seq seq
)
2083 LOG_DEBUG("SWD line reset");
2084 s
= swd_seq_line_reset
;
2085 s_len
= swd_seq_line_reset_len
;
2088 LOG_DEBUG("JTAG-to-SWD");
2089 s
= swd_seq_jtag_to_swd
;
2090 s_len
= swd_seq_jtag_to_swd_len
;
2093 LOG_DEBUG("SWD-to-JTAG");
2094 s
= swd_seq_swd_to_jtag
;
2095 s_len
= swd_seq_swd_to_jtag_len
;
2098 LOG_ERROR("Sequence %d not supported.", seq
);
2102 jlink_queue_data_out(s
, s_len
);
2107 static int jlink_swd_run_queue(void)
2112 LOG_DEBUG("Executing %d queued transactions.", pending_scan_results_length
);
2114 if (queued_retval
!= ERROR_OK
) {
2115 LOG_DEBUG("Skipping due to previous errors: %d.", queued_retval
);
2120 * A transaction must be followed by another transaction or at least 8 idle
2121 * cycles to ensure that data is clocked through the AP.
2123 jlink_queue_data_out(NULL
, 8);
2125 ret
= jaylink_swd_io(devh
, tms_buffer
, tdi_buffer
, tdo_buffer
, tap_length
);
2127 if (ret
!= JAYLINK_OK
) {
2128 LOG_ERROR("jaylink_swd_io() failed: %s.", jaylink_strerror(ret
));
2132 for (i
= 0; i
< pending_scan_results_length
; i
++) {
2133 int ack
= buf_get_u32(tdo_buffer
, pending_scan_results_buffer
[i
].first
, 3);
2135 if (ack
!= SWD_ACK_OK
) {
2136 LOG_DEBUG("SWD ack not OK: %d %s", ack
,
2137 ack
== SWD_ACK_WAIT
? "WAIT" : ack
== SWD_ACK_FAULT
? "FAULT" : "JUNK");
2138 queued_retval
= ack
== SWD_ACK_WAIT
? ERROR_WAIT
: ERROR_FAIL
;
2140 } else if (pending_scan_results_buffer
[i
].length
) {
2141 uint32_t data
= buf_get_u32(tdo_buffer
, 3 + pending_scan_results_buffer
[i
].first
, 32);
2142 int parity
= buf_get_u32(tdo_buffer
, 3 + 32 + pending_scan_results_buffer
[i
].first
, 1);
2144 if (parity
!= parity_u32(data
)) {
2145 LOG_ERROR("SWD: Read data parity mismatch.");
2146 queued_retval
= ERROR_FAIL
;
2150 if (pending_scan_results_buffer
[i
].buffer
)
2151 *(uint32_t *)pending_scan_results_buffer
[i
].buffer
= data
;
2157 ret
= queued_retval
;
2158 queued_retval
= ERROR_OK
;
2163 static void jlink_swd_queue_cmd(uint8_t cmd
, uint32_t *dst
, uint32_t data
, uint32_t ap_delay_clk
)
2165 uint8_t data_parity_trn
[DIV_ROUND_UP(32 + 1, 8)];
2166 if (tap_length
+ 46 + 8 + ap_delay_clk
>= swd_buffer_size
* 8 ||
2167 pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
) {
2168 /* Not enough room in the queue. Run the queue. */
2169 queued_retval
= jlink_swd_run_queue();
2172 if (queued_retval
!= ERROR_OK
)
2175 cmd
|= SWD_CMD_START
| SWD_CMD_PARK
;
2177 jlink_queue_data_out(&cmd
, 8);
2179 pending_scan_results_buffer
[pending_scan_results_length
].first
= tap_length
;
2181 if (cmd
& SWD_CMD_RnW
) {
2182 /* Queue a read transaction. */
2183 pending_scan_results_buffer
[pending_scan_results_length
].length
= 32;
2184 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= dst
;
2186 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2188 /* Queue a write transaction. */
2189 pending_scan_results_buffer
[pending_scan_results_length
].length
= 0;
2190 jlink_queue_data_in(1 + 3 + 1);
2192 buf_set_u32(data_parity_trn
, 0, 32, data
);
2193 buf_set_u32(data_parity_trn
, 32, 1, parity_u32(data
));
2195 jlink_queue_data_out(data_parity_trn
, 32 + 1);
2198 pending_scan_results_length
++;
2200 /* Insert idle cycles after AP accesses to avoid WAIT. */
2201 if (cmd
& SWD_CMD_APnDP
)
2202 jlink_queue_data_out(NULL
, ap_delay_clk
);
2205 static const struct swd_driver jlink_swd
= {
2206 .init
= &jlink_swd_init
,
2207 .switch_seq
= &jlink_swd_switch_seq
,
2208 .read_reg
= &jlink_swd_read_reg
,
2209 .write_reg
= &jlink_swd_write_reg
,
2210 .run
= &jlink_swd_run_queue
,
2213 static const char * const jlink_transports
[] = { "jtag", "swd", NULL
};
2215 struct jtag_interface jlink_interface
= {
2217 .commands
= jlink_command_handlers
,
2218 .transports
= jlink_transports
,
2220 .execute_queue
= &jlink_execute_queue
,
2221 .speed
= &jlink_speed
,
2222 .speed_div
= &jlink_speed_div
,
2224 .init
= &jlink_init
,
2225 .quit
= &jlink_quit
,
2226 .config_trace
= &config_trace
,
2227 .poll_trace
= &poll_trace
,