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>
42 #include <libjaylink/libjaylink.h>
44 static struct jaylink_context
*jayctx
;
45 static struct jaylink_device_handle
*devh
;
46 static struct jaylink_connection conn
;
47 static struct jaylink_connection connlist
[JAYLINK_MAX_CONNECTIONS
];
48 static enum jaylink_jtag_version jtag_command_version
;
49 static uint8_t caps
[JAYLINK_DEV_EXT_CAPS_SIZE
];
51 static uint32_t serial_number
;
52 static bool use_serial_number
;
53 static enum jaylink_usb_address usb_address
;
54 static bool use_usb_address
;
55 static enum jaylink_target_interface iface
= JAYLINK_TIF_JTAG
;
56 static bool trace_enabled
;
58 #define JLINK_MAX_SPEED 12000
59 #define JLINK_TAP_BUFFER_SIZE 2048
61 static unsigned int swd_buffer_size
= JLINK_TAP_BUFFER_SIZE
;
63 /* 256 byte non-volatile memory */
64 struct device_config
{
66 /* 0ffset 0x01 to 0x03 */
67 uint8_t reserved_1
[3];
68 uint32_t target_power
;
69 /* 0ffset 0x08 to 0x1f */
70 uint8_t reserved_2
[24];
71 /* IP only for J-Link Pro */
72 uint8_t ip_address
[4];
73 uint8_t subnet_mask
[4];
74 /* 0ffset 0x28 to 0x2f */
75 uint8_t reserved_3
[8];
76 uint8_t mac_address
[6];
77 /* 0ffset 0x36 to 0xff */
78 uint8_t reserved_4
[202];
79 } __attribute__ ((packed
));
81 static struct device_config config
;
82 static struct device_config tmp_config
;
84 /* Queue command functions */
85 static void jlink_end_state(tap_state_t state
);
86 static void jlink_state_move(void);
87 static void jlink_path_move(int num_states
, tap_state_t
*path
);
88 static void jlink_stableclocks(int num_cycles
);
89 static void jlink_runtest(int num_cycles
);
90 static void jlink_reset(int trst
, int srst
);
91 static int jlink_swd_run_queue(void);
92 static void jlink_swd_queue_cmd(uint8_t cmd
, uint32_t *dst
, uint32_t data
, uint32_t ap_delay_clk
);
93 static int jlink_swd_switch_seq(enum swd_special_seq seq
);
95 /* J-Link tap buffer functions */
96 static void jlink_tap_init(void);
97 static int jlink_flush(void);
99 * Queue data to go out and in, flushing the queue as many times as
102 * @param out A pointer to TDI data, if NULL, old stale data will be used.
103 * @param out_offset A bit offset for TDI data.
104 * @param tms_out A pointer to TMS data, if NULL, zeroes will be emitted.
105 * @param tms_offset A bit offset for TMS data.
106 * @param in A pointer to store TDO data to, if NULL the data will be discarded.
107 * @param in_offset A bit offset for TDO data.
108 * @param length Amount of bits to transfer out and in.
110 * @retval This function doesn't return any value.
112 static void jlink_clock_data(const uint8_t *out
, unsigned out_offset
,
113 const uint8_t *tms_out
, unsigned tms_offset
,
114 uint8_t *in
, unsigned in_offset
,
117 static enum tap_state jlink_last_state
= TAP_RESET
;
118 static int queued_retval
;
120 /***************************************************************************/
121 /* External interface implementation */
123 static void jlink_execute_stableclocks(struct jtag_command
*cmd
)
125 DEBUG_JTAG_IO("stableclocks %i cycles", cmd
->cmd
.runtest
->num_cycles
);
126 jlink_stableclocks(cmd
->cmd
.runtest
->num_cycles
);
129 static void jlink_execute_runtest(struct jtag_command
*cmd
)
131 DEBUG_JTAG_IO("runtest %i cycles, end in %i", cmd
->cmd
.runtest
->num_cycles
,
132 cmd
->cmd
.runtest
->end_state
);
134 jlink_end_state(cmd
->cmd
.runtest
->end_state
);
135 jlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
138 static void jlink_execute_statemove(struct jtag_command
*cmd
)
140 DEBUG_JTAG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
142 jlink_end_state(cmd
->cmd
.statemove
->end_state
);
146 static void jlink_execute_pathmove(struct jtag_command
*cmd
)
148 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
149 cmd
->cmd
.pathmove
->num_states
,
150 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
152 jlink_path_move(cmd
->cmd
.pathmove
->num_states
, cmd
->cmd
.pathmove
->path
);
155 static void jlink_execute_scan(struct jtag_command
*cmd
)
157 DEBUG_JTAG_IO("%s type:%d", cmd
->cmd
.scan
->ir_scan
? "IRSCAN" : "DRSCAN",
158 jtag_scan_type(cmd
->cmd
.scan
));
160 /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
161 while (cmd
->cmd
.scan
->num_fields
> 0
162 && cmd
->cmd
.scan
->fields
[cmd
->cmd
.scan
->num_fields
- 1].num_bits
== 0) {
163 cmd
->cmd
.scan
->num_fields
--;
164 LOG_DEBUG("discarding trailing empty field");
167 if (cmd
->cmd
.scan
->num_fields
== 0) {
168 LOG_DEBUG("empty scan, doing nothing");
172 if (cmd
->cmd
.scan
->ir_scan
) {
173 if (tap_get_state() != TAP_IRSHIFT
) {
174 jlink_end_state(TAP_IRSHIFT
);
178 if (tap_get_state() != TAP_DRSHIFT
) {
179 jlink_end_state(TAP_DRSHIFT
);
184 jlink_end_state(cmd
->cmd
.scan
->end_state
);
186 struct scan_field
*field
= cmd
->cmd
.scan
->fields
;
187 unsigned scan_size
= 0;
189 for (int i
= 0; i
< cmd
->cmd
.scan
->num_fields
; i
++, field
++) {
190 scan_size
+= field
->num_bits
;
191 DEBUG_JTAG_IO("%s%s field %d/%d %d bits",
192 field
->in_value
? "in" : "",
193 field
->out_value
? "out" : "",
195 cmd
->cmd
.scan
->num_fields
,
198 if (i
== cmd
->cmd
.scan
->num_fields
- 1 && tap_get_state() != tap_get_end_state()) {
199 /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
200 * movement. This last field can't have length zero, it was checked above. */
201 jlink_clock_data(field
->out_value
,
207 field
->num_bits
- 1);
208 uint8_t last_bit
= 0;
209 if (field
->out_value
)
210 bit_copy(&last_bit
, 0, field
->out_value
, field
->num_bits
- 1, 1);
211 uint8_t tms_bits
= 0x01;
212 jlink_clock_data(&last_bit
,
219 tap_set_state(tap_state_transition(tap_get_state(), 1));
220 jlink_clock_data(NULL
,
227 tap_set_state(tap_state_transition(tap_get_state(), 0));
229 jlink_clock_data(field
->out_value
,
238 if (tap_get_state() != tap_get_end_state()) {
239 jlink_end_state(tap_get_end_state());
243 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
244 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR", scan_size
,
245 tap_state_name(tap_get_end_state()));
248 static void jlink_execute_reset(struct jtag_command
*cmd
)
250 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd
->cmd
.reset
->trst
,
251 cmd
->cmd
.reset
->srst
);
254 jlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
258 static void jlink_execute_sleep(struct jtag_command
*cmd
)
260 DEBUG_JTAG_IO("sleep %" PRIi32
"", cmd
->cmd
.sleep
->us
);
262 jtag_sleep(cmd
->cmd
.sleep
->us
);
265 static int jlink_execute_command(struct jtag_command
*cmd
)
268 case JTAG_STABLECLOCKS
:
269 jlink_execute_stableclocks(cmd
);
272 jlink_execute_runtest(cmd
);
275 jlink_execute_statemove(cmd
);
278 jlink_execute_pathmove(cmd
);
281 jlink_execute_scan(cmd
);
284 jlink_execute_reset(cmd
);
287 jlink_execute_sleep(cmd
);
290 LOG_ERROR("BUG: Unknown JTAG command type encountered.");
291 return ERROR_JTAG_QUEUE_FAILED
;
297 static int jlink_execute_queue(void)
300 struct jtag_command
*cmd
= jtag_command_queue
;
302 while (cmd
!= NULL
) {
303 ret
= jlink_execute_command(cmd
);
311 return jlink_flush();
314 static int jlink_speed(int speed
)
317 struct jaylink_speed tmp
;
320 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_SPEEDS
)) {
321 ret
= jaylink_get_speeds(devh
, &tmp
);
323 if (ret
!= JAYLINK_OK
) {
324 LOG_ERROR("jaylink_get_speeds() failed: %s.",
325 jaylink_strerror_name(ret
));
326 return ERROR_JTAG_DEVICE_ERROR
;
330 max_speed
= tmp
.freq
/ tmp
.div
;
332 max_speed
= JLINK_MAX_SPEED
;
336 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING
)) {
337 LOG_ERROR("Adaptive clocking is not supported by the device.");
338 return ERROR_JTAG_NOT_IMPLEMENTED
;
341 speed
= JAYLINK_SPEED_ADAPTIVE_CLOCKING
;
342 } else if (speed
> max_speed
) {
343 LOG_INFO("Reduced speed from %d kHz to %d kHz (maximum).", speed
,
348 ret
= jaylink_set_speed(devh
, speed
);
350 if (ret
!= JAYLINK_OK
) {
351 LOG_ERROR("jaylink_set_speed() failed: %s.",
352 jaylink_strerror_name(ret
));
353 return ERROR_JTAG_DEVICE_ERROR
;
359 static int jlink_speed_div(int speed
, int *khz
)
366 static int jlink_khz(int khz
, int *jtag_speed
)
373 static bool read_device_config(struct device_config
*cfg
)
377 ret
= jaylink_read_raw_config(devh
, (uint8_t *)cfg
);
379 if (ret
!= JAYLINK_OK
) {
380 LOG_ERROR("jaylink_read_raw_config() failed: %s.",
381 jaylink_strerror_name(ret
));
385 if (cfg
->usb_address
== 0xff)
386 cfg
->usb_address
= 0x00;
388 if (cfg
->target_power
== 0xffffffff)
389 cfg
->target_power
= 0;
394 static int select_interface(void)
399 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SELECT_TIF
)) {
400 if (iface
!= JAYLINK_TIF_JTAG
) {
401 LOG_ERROR("Device supports JTAG transport only.");
402 return ERROR_JTAG_INIT_FAILED
;
408 ret
= jaylink_get_available_interfaces(devh
, &interfaces
);
410 if (ret
!= JAYLINK_OK
) {
411 LOG_ERROR("jaylink_get_available_interfaces() failed: %s.",
412 jaylink_strerror_name(ret
));
413 return ERROR_JTAG_INIT_FAILED
;
416 if (!(interfaces
& (1 << iface
))) {
417 LOG_ERROR("Selected transport is not supported by the device.");
418 return ERROR_JTAG_INIT_FAILED
;
421 ret
= jaylink_select_interface(devh
, iface
, NULL
);
424 LOG_ERROR("jaylink_select_interface() failed: %s.",
425 jaylink_strerror_name(ret
));
426 return ERROR_JTAG_INIT_FAILED
;
432 static int jlink_register(void)
439 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_REGISTER
))
442 ret
= jaylink_register(devh
, &conn
, connlist
, &count
);
444 if (ret
!= JAYLINK_OK
) {
445 LOG_ERROR("jaylink_register() failed: %s.",
446 jaylink_strerror_name(ret
));
450 handle_found
= false;
452 for (i
= 0; i
< count
; i
++) {
453 if (connlist
[i
].handle
== conn
.handle
) {
460 LOG_ERROR("Registration failed: maximum number of connections on the "
469 * Adjust the SWD transaction buffer size depending on the free device internal
470 * memory. This ensures that the SWD transactions sent to the device do not
471 * exceed the internal memory of the device.
473 static bool adjust_swd_buffer_size(void)
478 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
))
481 ret
= jaylink_get_free_memory(devh
, &tmp
);
483 if (ret
!= JAYLINK_OK
) {
484 LOG_ERROR("jaylink_get_free_memory() failed: %s.",
485 jaylink_strerror_name(ret
));
490 LOG_ERROR("Not enough free device internal memory: %u bytes.", tmp
);
494 tmp
= MIN(JLINK_TAP_BUFFER_SIZE
, (tmp
- 16) / 2);
496 if (tmp
!= swd_buffer_size
) {
497 swd_buffer_size
= tmp
;
498 LOG_DEBUG("Adjusted SWD transaction buffer size to %u bytes.",
505 static int jaylink_log_handler(const struct jaylink_context
*ctx
,
506 enum jaylink_log_level level
, const char *format
, va_list args
,
512 case JAYLINK_LOG_LEVEL_ERROR
:
515 case JAYLINK_LOG_LEVEL_WARNING
:
516 tmp
= LOG_LVL_WARNING
;
519 * Forward info messages to the debug output because they are more verbose
520 * than info messages of OpenOCD.
522 case JAYLINK_LOG_LEVEL_INFO
:
523 case JAYLINK_LOG_LEVEL_DEBUG
:
527 tmp
= LOG_LVL_WARNING
;
530 log_vprintf_lf(tmp
, __FILE__
, __LINE__
, __func__
, format
, args
);
535 static int jlink_init(void)
538 struct jaylink_device
**devs
;
542 char *firmware_version
;
543 struct jaylink_hardware_version hwver
;
544 struct jaylink_hardware_status hwstatus
;
545 enum jaylink_usb_address address
;
548 LOG_DEBUG("Using libjaylink %s (compiled with %s).",
549 jaylink_version_package_get_string(), JAYLINK_VERSION_PACKAGE_STRING
);
551 ret
= jaylink_init(&jayctx
);
553 if (ret
!= JAYLINK_OK
) {
554 LOG_ERROR("jaylink_init() failed: %s.",
555 jaylink_strerror_name(ret
));
556 return ERROR_JTAG_INIT_FAILED
;
559 ret
= jaylink_log_set_callback(jayctx
, &jaylink_log_handler
, NULL
);
561 if (ret
!= JAYLINK_OK
) {
562 LOG_ERROR("jaylink_log_set_callback() failed: %s.",
563 jaylink_strerror_name(ret
));
564 jaylink_exit(jayctx
);
565 return ERROR_JTAG_INIT_FAILED
;
568 ret
= jaylink_discovery_scan(jayctx
, 0);
570 if (ret
!= JAYLINK_OK
) {
571 LOG_ERROR("jaylink_discovery_scan() failed: %s.",
572 jaylink_strerror_name(ret
));
573 jaylink_exit(jayctx
);
574 return ERROR_JTAG_INIT_FAILED
;
577 ret
= jaylink_get_devices(jayctx
, &devs
, NULL
);
579 if (ret
!= JAYLINK_OK
) {
580 LOG_ERROR("jaylink_get_devices() failed: %s.",
581 jaylink_strerror_name(ret
));
582 jaylink_exit(jayctx
);
583 return ERROR_JTAG_INIT_FAILED
;
586 found_device
= false;
588 if (!use_serial_number
&& !use_usb_address
)
589 LOG_INFO("No device selected, using first device.");
591 for (i
= 0; devs
[i
]; i
++) {
592 if (use_serial_number
) {
593 ret
= jaylink_device_get_serial_number(devs
[i
], &tmp
);
595 if (ret
== JAYLINK_ERR_NOT_AVAILABLE
) {
597 } else if (ret
!= JAYLINK_OK
) {
598 LOG_WARNING("jaylink_device_get_serial_number() failed: %s.",
599 jaylink_strerror_name(ret
));
603 if (serial_number
!= tmp
)
607 if (use_usb_address
) {
608 ret
= jaylink_device_get_usb_address(devs
[i
], &address
);
610 if (ret
!= JAYLINK_OK
) {
611 LOG_WARNING("jaylink_device_get_usb_address() failed: %s.",
612 jaylink_strerror_name(ret
));
616 if (usb_address
!= address
)
620 ret
= jaylink_open(devs
[i
], &devh
);
622 if (ret
== JAYLINK_OK
) {
627 LOG_ERROR("Failed to open device: %s.", jaylink_strerror_name(ret
));
630 jaylink_free_devices(devs
, true);
633 LOG_ERROR("No J-Link device found.");
634 jaylink_exit(jayctx
);
635 return ERROR_JTAG_INIT_FAILED
;
639 * Be careful with changing the following initialization sequence because
640 * some devices are known to be sensitive regarding the order.
643 ret
= jaylink_get_firmware_version(devh
, &firmware_version
, &length
);
645 if (ret
!= JAYLINK_OK
) {
646 LOG_ERROR("jaylink_get_firmware_version() failed: %s.",
647 jaylink_strerror_name(ret
));
649 jaylink_exit(jayctx
);
650 return ERROR_JTAG_INIT_FAILED
;
651 } else if (length
> 0) {
652 LOG_INFO("%s", firmware_version
);
653 free(firmware_version
);
655 LOG_WARNING("Device responds empty firmware version string.");
658 memset(caps
, 0, JAYLINK_DEV_EXT_CAPS_SIZE
);
659 ret
= jaylink_get_caps(devh
, caps
);
661 if (ret
!= JAYLINK_OK
) {
662 LOG_ERROR("jaylink_get_caps() failed: %s.", jaylink_strerror_name(ret
));
664 jaylink_exit(jayctx
);
665 return ERROR_JTAG_INIT_FAILED
;
668 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_EXT_CAPS
)) {
669 ret
= jaylink_get_extended_caps(devh
, caps
);
671 if (ret
!= JAYLINK_OK
) {
672 LOG_ERROR("jaylink_get_extended_caps() failed: %s.",
673 jaylink_strerror_name(ret
));
675 jaylink_exit(jayctx
);
676 return ERROR_JTAG_INIT_FAILED
;
680 jtag_command_version
= JAYLINK_JTAG_VERSION_2
;
682 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_HW_VERSION
)) {
683 ret
= jaylink_get_hardware_version(devh
, &hwver
);
685 if (ret
!= JAYLINK_OK
) {
686 LOG_ERROR("Failed to retrieve hardware version: %s.",
687 jaylink_strerror_name(ret
));
689 jaylink_exit(jayctx
);
690 return ERROR_JTAG_INIT_FAILED
;
693 LOG_INFO("Hardware version: %u.%02u", hwver
.major
, hwver
.minor
);
695 if (hwver
.major
>= 5)
696 jtag_command_version
= JAYLINK_JTAG_VERSION_3
;
699 if (iface
== JAYLINK_TIF_SWD
) {
701 * Adjust the SWD transaction buffer size in case there is already
702 * allocated memory on the device. This happens for example if the
703 * memory for SWO capturing is still allocated because the software
704 * which used the device before has not been shut down properly.
706 if (!adjust_swd_buffer_size()) {
708 jaylink_exit(jayctx
);
709 return ERROR_JTAG_INIT_FAILED
;
713 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
714 if (!read_device_config(&config
)) {
715 LOG_ERROR("Failed to read device configuration data.");
717 jaylink_exit(jayctx
);
718 return ERROR_JTAG_INIT_FAILED
;
721 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
724 ret
= jaylink_get_hardware_status(devh
, &hwstatus
);
726 if (ret
!= JAYLINK_OK
) {
727 LOG_ERROR("jaylink_get_hardware_status() failed: %s.",
728 jaylink_strerror_name(ret
));
730 jaylink_exit(jayctx
);
731 return ERROR_JTAG_INIT_FAILED
;
734 LOG_INFO("VTarget = %u.%03u V", hwstatus
.target_voltage
/ 1000,
735 hwstatus
.target_voltage
% 1000);
739 strcpy(conn
.hid
, "0.0.0.0");
743 ret
= jlink_register();
745 if (ret
!= ERROR_OK
) {
747 jaylink_exit(jayctx
);
748 return ERROR_JTAG_INIT_FAILED
;
751 ret
= select_interface();
753 if (ret
!= ERROR_OK
) {
755 jaylink_exit(jayctx
);
763 jlink_speed(jtag_get_speed_khz());
765 if (iface
== JAYLINK_TIF_JTAG
) {
767 * J-Link devices with firmware version v5 and v6 seems to have an issue
768 * if the first tap move is not divisible by 8, so we send a TLR on
772 jlink_clock_data(NULL
, 0, &tms
, 0, NULL
, 0, 8);
780 static int jlink_quit(void)
786 ret
= jaylink_swo_stop(devh
);
788 if (ret
!= JAYLINK_OK
)
789 LOG_ERROR("jaylink_swo_stop() failed: %s.",
790 jaylink_strerror_name(ret
));
793 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_REGISTER
)) {
794 ret
= jaylink_unregister(devh
, &conn
, connlist
, &count
);
796 if (ret
!= JAYLINK_OK
)
797 LOG_ERROR("jaylink_unregister() failed: %s.",
798 jaylink_strerror_name(ret
));
802 jaylink_exit(jayctx
);
807 /***************************************************************************/
808 /* Queue command implementations */
810 static void jlink_end_state(tap_state_t state
)
812 if (tap_is_state_stable(state
))
813 tap_set_end_state(state
);
815 LOG_ERROR("BUG: %i is not a valid end state", state
);
820 /* Goes to the end state. */
821 static void jlink_state_move(void)
824 uint8_t tms_scan_bits
;
826 tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
827 tms_scan_bits
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
829 jlink_clock_data(NULL
, 0, &tms_scan
, 0, NULL
, 0, tms_scan_bits
);
831 tap_set_state(tap_get_end_state());
834 static void jlink_path_move(int num_states
, tap_state_t
*path
)
839 for (i
= 0; i
< num_states
; i
++) {
840 if (path
[i
] == tap_state_transition(tap_get_state(), false))
841 jlink_clock_data(NULL
, 0, NULL
, 0, NULL
, 0, 1);
842 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
843 jlink_clock_data(NULL
, 0, &tms
, 0, NULL
, 0, 1);
845 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
846 tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
850 tap_set_state(path
[i
]);
853 tap_set_end_state(tap_get_state());
856 static void jlink_stableclocks(int num_cycles
)
860 uint8_t tms
= tap_get_state() == TAP_RESET
;
861 /* Execute num_cycles. */
862 for (i
= 0; i
< num_cycles
; i
++)
863 jlink_clock_data(NULL
, 0, &tms
, 0, NULL
, 0, 1);
866 static void jlink_runtest(int num_cycles
)
868 tap_state_t saved_end_state
= tap_get_end_state();
870 /* Only do a state_move when we're not already in IDLE. */
871 if (tap_get_state() != TAP_IDLE
) {
872 jlink_end_state(TAP_IDLE
);
877 jlink_stableclocks(num_cycles
);
879 /* Finish in end_state. */
880 jlink_end_state(saved_end_state
);
882 if (tap_get_state() != tap_get_end_state())
886 static void jlink_reset(int trst
, int srst
)
888 LOG_DEBUG("TRST: %i, SRST: %i.", trst
, srst
);
890 /* Signals are active low. */
892 jaylink_set_reset(devh
);
895 jaylink_clear_reset(devh
);
898 jaylink_jtag_clear_trst(devh
);
901 jaylink_jtag_set_trst(devh
);
904 COMMAND_HANDLER(jlink_usb_command
)
909 command_print(CMD_CTX
, "Need exactly one argument for jlink usb.");
910 return ERROR_COMMAND_SYNTAX_ERROR
;
913 if (sscanf(CMD_ARGV
[0], "%i", &tmp
) != 1) {
914 command_print(CMD_CTX
, "Invalid USB address: %s.", CMD_ARGV
[0]);
918 if (tmp
< JAYLINK_USB_ADDRESS_0
|| tmp
> JAYLINK_USB_ADDRESS_3
) {
919 command_print(CMD_CTX
, "Invalid USB address: %s.", CMD_ARGV
[0]);
925 use_serial_number
= false;
926 use_usb_address
= true;
931 COMMAND_HANDLER(jlink_serial_command
)
936 command_print(CMD_CTX
, "Need exactly one argument for jlink serial.");
937 return ERROR_COMMAND_SYNTAX_ERROR
;
940 ret
= jaylink_parse_serial_number(CMD_ARGV
[0], &serial_number
);
942 if (ret
== JAYLINK_ERR
) {
943 command_print(CMD_CTX
, "Invalid serial number: %s.", CMD_ARGV
[0]);
945 } else if (ret
!= JAYLINK_OK
) {
946 command_print(CMD_CTX
, "jaylink_parse_serial_number() failed: %s.",
947 jaylink_strerror_name(ret
));
951 use_serial_number
= true;
952 use_usb_address
= false;
957 COMMAND_HANDLER(jlink_handle_hwstatus_command
)
960 struct jaylink_hardware_status status
;
962 ret
= jaylink_get_hardware_status(devh
, &status
);
964 if (ret
!= JAYLINK_OK
) {
965 command_print(CMD_CTX
, "jaylink_get_hardware_status() failed: %s.",
966 jaylink_strerror_name(ret
));
970 command_print(CMD_CTX
, "VTarget = %u.%03u V",
971 status
.target_voltage
/ 1000, status
.target_voltage
% 1000);
973 command_print(CMD_CTX
, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
974 "TRST = %u", status
.tck
, status
.tdi
, status
.tdo
, status
.tms
,
975 status
.tres
, status
.trst
);
977 if (status
.target_voltage
< 1500)
978 command_print(CMD_CTX
, "Target voltage too low. Check target power.");
983 COMMAND_HANDLER(jlink_handle_free_memory_command
)
988 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
)) {
989 command_print(CMD_CTX
, "Retrieval of free memory is not supported by "
994 ret
= jaylink_get_free_memory(devh
, &tmp
);
996 if (ret
!= JAYLINK_OK
) {
997 command_print(CMD_CTX
, "jaylink_get_free_memory() failed: %s.",
998 jaylink_strerror_name(ret
));
1002 command_print(CMD_CTX
, "Device has %u bytes of free memory.", tmp
);
1007 COMMAND_HANDLER(jlink_handle_jlink_jtag_command
)
1013 switch (jtag_command_version
) {
1014 case JAYLINK_JTAG_VERSION_2
:
1017 case JAYLINK_JTAG_VERSION_3
:
1024 command_print(CMD_CTX
, "JTAG command version: %i", version
);
1025 } else if (CMD_ARGC
== 1) {
1026 if (sscanf(CMD_ARGV
[0], "%i", &tmp
) != 1) {
1027 command_print(CMD_CTX
, "Invalid argument: %s.", CMD_ARGV
[0]);
1028 return ERROR_COMMAND_SYNTAX_ERROR
;
1033 jtag_command_version
= JAYLINK_JTAG_VERSION_2
;
1036 jtag_command_version
= JAYLINK_JTAG_VERSION_3
;
1039 command_print(CMD_CTX
, "Invalid argument: %s.", CMD_ARGV
[0]);
1040 return ERROR_COMMAND_SYNTAX_ERROR
;
1043 command_print(CMD_CTX
, "Need exactly one argument for jlink jtag.");
1044 return ERROR_COMMAND_SYNTAX_ERROR
;
1050 COMMAND_HANDLER(jlink_handle_target_power_command
)
1055 if (CMD_ARGC
!= 1) {
1056 command_print(CMD_CTX
, "Need exactly one argument for jlink "
1058 return ERROR_COMMAND_SYNTAX_ERROR
;
1061 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
)) {
1062 command_print(CMD_CTX
, "Target power supply is not supported by the "
1067 if (!strcmp(CMD_ARGV
[0], "on")) {
1069 } else if (!strcmp(CMD_ARGV
[0], "off")) {
1072 command_print(CMD_CTX
, "Invalid argument: %s.", CMD_ARGV
[0]);
1076 ret
= jaylink_set_target_power(devh
, enable
);
1078 if (ret
!= JAYLINK_OK
) {
1079 command_print(CMD_CTX
, "jaylink_set_target_power() failed: %s.",
1080 jaylink_strerror_name(ret
));
1087 static void show_config_usb_address(struct command_context
*ctx
)
1089 if (config
.usb_address
!= tmp_config
.usb_address
)
1090 command_print(ctx
, "USB address: %u [%u]", config
.usb_address
,
1091 tmp_config
.usb_address
);
1093 command_print(ctx
, "USB address: %u", config
.usb_address
);
1096 static void show_config_ip_address(struct command_context
*ctx
)
1098 if (!memcmp(config
.ip_address
, tmp_config
.ip_address
, 4))
1099 command_print(ctx
, "IP address: %d.%d.%d.%d",
1100 config
.ip_address
[3], config
.ip_address
[2],
1101 config
.ip_address
[1], config
.ip_address
[0]);
1103 command_print(ctx
, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1104 config
.ip_address
[3], config
.ip_address
[2],
1105 config
.ip_address
[1], config
.ip_address
[0],
1106 tmp_config
.ip_address
[3], tmp_config
.ip_address
[2],
1107 tmp_config
.ip_address
[1], tmp_config
.ip_address
[0]);
1109 if (!memcmp(config
.subnet_mask
, tmp_config
.subnet_mask
, 4))
1110 command_print(ctx
, "Subnet mask: %d.%d.%d.%d",
1111 config
.subnet_mask
[3], config
.subnet_mask
[2],
1112 config
.subnet_mask
[1], config
.subnet_mask
[0]);
1114 command_print(ctx
, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1115 config
.subnet_mask
[3], config
.subnet_mask
[2],
1116 config
.subnet_mask
[1], config
.subnet_mask
[0],
1117 tmp_config
.subnet_mask
[3], tmp_config
.subnet_mask
[2],
1118 tmp_config
.subnet_mask
[1], tmp_config
.subnet_mask
[0]);
1121 static void show_config_mac_address(struct command_context
*ctx
)
1123 if (!memcmp(config
.mac_address
, tmp_config
.mac_address
, 6))
1124 command_print(ctx
, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1125 config
.mac_address
[5], config
.mac_address
[4],
1126 config
.mac_address
[3], config
.mac_address
[2],
1127 config
.mac_address
[1], config
.mac_address
[0]);
1129 command_print(ctx
, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1130 "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1131 config
.mac_address
[5], config
.mac_address
[4],
1132 config
.mac_address
[3], config
.mac_address
[2],
1133 config
.mac_address
[1], config
.mac_address
[0],
1134 tmp_config
.mac_address
[5], tmp_config
.mac_address
[4],
1135 tmp_config
.mac_address
[3], tmp_config
.mac_address
[2],
1136 tmp_config
.mac_address
[1], tmp_config
.mac_address
[0]);
1139 static void show_config_target_power(struct command_context
*ctx
)
1141 const char *target_power
;
1142 const char *current_target_power
;
1144 if (!config
.target_power
)
1145 target_power
= "off";
1147 target_power
= "on";
1149 if (!tmp_config
.target_power
)
1150 current_target_power
= "off";
1152 current_target_power
= "on";
1154 if (config
.target_power
!= tmp_config
.target_power
)
1155 command_print(ctx
, "Target power supply: %s [%s]", target_power
,
1156 current_target_power
);
1158 command_print(ctx
, "Target power supply: %s", target_power
);
1161 static void show_config(struct command_context
*ctx
)
1163 command_print(ctx
, "J-Link device configuration:");
1165 show_config_usb_address(ctx
);
1167 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
))
1168 show_config_target_power(ctx
);
1170 if (jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1171 show_config_ip_address(ctx
);
1172 show_config_mac_address(ctx
);
1176 static int poll_trace(uint8_t *buf
, size_t *size
)
1183 ret
= jaylink_swo_read(devh
, buf
, &length
);
1185 if (ret
!= JAYLINK_OK
) {
1186 LOG_ERROR("jaylink_swo_read() failed: %s.", jaylink_strerror_name(ret
));
1195 static uint32_t calculate_trace_buffer_size(void)
1200 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_GET_FREE_MEMORY
))
1203 ret
= jaylink_get_free_memory(devh
, &tmp
);
1205 if (ret
!= JAYLINK_OK
) {
1206 LOG_ERROR("jaylink_get_free_memory() failed: %s.",
1207 jaylink_strerror_name(ret
));
1211 if (tmp
> 0x3fff || tmp
<= 0x600)
1216 return tmp
& 0xffffff00;
1219 static bool check_trace_freq(struct jaylink_swo_speed speed
,
1220 uint32_t trace_freq
)
1226 min
= fabs(1.0 - (speed
.freq
/ ((double)trace_freq
* speed
.min_div
)));
1228 for (divider
= speed
.min_div
; divider
< speed
.max_div
; divider
++) {
1229 deviation
= fabs(1.0 - (speed
.freq
/ ((double)trace_freq
* divider
)));
1231 if (deviation
< 0.03) {
1232 LOG_DEBUG("Found suitable frequency divider %u with deviation of "
1233 "%.02f %%.", divider
, deviation
);
1237 if (deviation
< min
)
1241 LOG_ERROR("Selected trace frequency is not supported by the device. "
1242 "Please choose a different trace frequency.");
1243 LOG_ERROR("Maximum permitted deviation is 3.00 %%, but only %.02f %% "
1244 "could be achieved.", min
* 100);
1249 static int config_trace(bool enabled
, enum tpio_pin_protocol pin_protocol
,
1250 uint32_t port_size
, unsigned int *trace_freq
)
1253 uint32_t buffer_size
;
1254 struct jaylink_swo_speed speed
;
1256 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SWO
)) {
1257 LOG_ERROR("Trace capturing is not supported by the device.");
1261 if (pin_protocol
!= ASYNC_UART
) {
1262 LOG_ERROR("Selected pin protocol is not supported.");
1266 trace_enabled
= enabled
;
1268 ret
= jaylink_swo_stop(devh
);
1270 if (ret
!= JAYLINK_OK
) {
1271 LOG_ERROR("jaylink_swo_stop() failed: %s.", jaylink_strerror_name(ret
));
1277 * Adjust the SWD transaction buffer size as stopping SWO capturing
1278 * deallocates device internal memory.
1280 if (!adjust_swd_buffer_size())
1286 buffer_size
= calculate_trace_buffer_size();
1289 LOG_ERROR("Not enough free device memory to start trace capturing.");
1293 ret
= jaylink_swo_get_speeds(devh
, JAYLINK_SWO_MODE_UART
, &speed
);
1295 if (ret
!= JAYLINK_OK
) {
1296 LOG_ERROR("jaylink_swo_get_speeds() failed: %s.",
1297 jaylink_strerror_name(ret
));
1302 *trace_freq
= speed
.freq
/ speed
.min_div
;
1304 if (!check_trace_freq(speed
, *trace_freq
))
1307 LOG_DEBUG("Using %u bytes device memory for trace capturing.", buffer_size
);
1309 ret
= jaylink_swo_start(devh
, JAYLINK_SWO_MODE_UART
, *trace_freq
,
1312 if (ret
!= JAYLINK_OK
) {
1313 LOG_ERROR("jaylink_start_swo() failed: %s.",
1314 jaylink_strerror_name(ret
));
1319 * Adjust the SWD transaction buffer size as starting SWO capturing
1320 * allocates device internal memory.
1322 if (!adjust_swd_buffer_size())
1328 COMMAND_HANDLER(jlink_handle_config_usb_address_command
)
1332 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1333 command_print(CMD_CTX
, "Reading configuration is not supported by the "
1339 show_config_usb_address(CMD_CTX
);
1340 } else if (CMD_ARGC
== 1) {
1341 if (sscanf(CMD_ARGV
[0], "%" SCNd8
, &tmp
) != 1) {
1342 command_print(CMD_CTX
, "Invalid USB address: %s.", CMD_ARGV
[0]);
1346 if (tmp
> JAYLINK_USB_ADDRESS_3
) {
1347 command_print(CMD_CTX
, "Invalid USB address: %u.", tmp
);
1351 tmp_config
.usb_address
= tmp
;
1353 command_print(CMD_CTX
, "Need exactly one argument for jlink config "
1355 return ERROR_COMMAND_SYNTAX_ERROR
;
1361 COMMAND_HANDLER(jlink_handle_config_target_power_command
)
1365 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1366 command_print(CMD_CTX
, "Reading configuration is not supported by the "
1371 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_SET_TARGET_POWER
)) {
1372 command_print(CMD_CTX
, "Target power supply is not supported by the "
1378 show_config_target_power(CMD_CTX
);
1379 } else if (CMD_ARGC
== 1) {
1380 if (!strcmp(CMD_ARGV
[0], "on")) {
1382 } else if (!strcmp(CMD_ARGV
[0], "off")) {
1385 command_print(CMD_CTX
, "Invalid argument: %s.", CMD_ARGV
[0]);
1389 tmp_config
.target_power
= enable
;
1391 command_print(CMD_CTX
, "Need exactly one argument for jlink config "
1393 return ERROR_COMMAND_SYNTAX_ERROR
;
1399 COMMAND_HANDLER(jlink_handle_config_mac_address_command
)
1406 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1407 command_print(CMD_CTX
, "Reading configuration is not supported by the "
1412 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1413 command_print(CMD_CTX
, "Ethernet connectivity is not supported by the "
1419 show_config_mac_address(CMD_CTX
);
1420 } else if (CMD_ARGC
== 1) {
1423 if ((strlen(str
) != 17) || (str
[2] != ':' || str
[5] != ':' || \
1424 str
[8] != ':' || str
[11] != ':' || str
[14] != ':')) {
1425 command_print(CMD_CTX
, "Invalid MAC address format.");
1426 return ERROR_COMMAND_SYNTAX_ERROR
;
1429 for (i
= 5; i
>= 0; i
--) {
1430 addr
[i
] = strtoul(str
, &e
, 16);
1434 if (!(addr
[0] | addr
[1] | addr
[2] | addr
[3] | addr
[4] | addr
[5])) {
1435 command_print(CMD_CTX
, "Invalid MAC address: zero address.");
1436 return ERROR_COMMAND_SYNTAX_ERROR
;
1439 if (!(0x01 & addr
[0])) {
1440 command_print(CMD_CTX
, "Invalid MAC address: multicast address.");
1441 return ERROR_COMMAND_SYNTAX_ERROR
;
1444 memcpy(tmp_config
.mac_address
, addr
, sizeof(addr
));
1446 command_print(CMD_CTX
, "Need exactly one argument for jlink config "
1448 return ERROR_COMMAND_SYNTAX_ERROR
;
1454 static bool string_to_ip(const char *s
, uint8_t *ip
, int *pos
)
1458 const char *s_save
= s
;
1464 for (i
= 0; i
< 4; i
++) {
1465 lip
[i
] = strtoul(s
, &e
, 10);
1467 if (*e
!= '.' && i
!= 3)
1474 memcpy(ip
, lip
, sizeof(lip
));
1479 static void cpy_ip(uint8_t *dst
, uint8_t *src
)
1483 for (i
= 0, j
= 3; i
< 4; i
++, j
--)
1487 COMMAND_HANDLER(jlink_handle_config_ip_address_command
)
1489 uint8_t ip_address
[4];
1490 uint32_t subnet_mask
= 0;
1492 uint8_t subnet_bits
= 24;
1494 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1495 command_print(CMD_CTX
, "Reading configuration is not supported by the "
1500 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_ETHERNET
)) {
1501 command_print(CMD_CTX
, "Ethernet connectivity is not supported by the "
1507 show_config_ip_address(CMD_CTX
);
1509 if (!string_to_ip(CMD_ARGV
[0], ip_address
, &i
))
1510 return ERROR_COMMAND_SYNTAX_ERROR
;
1512 len
= strlen(CMD_ARGV
[0]);
1514 /* Check for format A.B.C.D/E. */
1516 if (CMD_ARGV
[0][i
] != '/')
1517 return ERROR_COMMAND_SYNTAX_ERROR
;
1519 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0] + i
+ 1, subnet_bits
);
1520 } else if (CMD_ARGC
> 1) {
1521 if (!string_to_ip(CMD_ARGV
[1], (uint8_t *)&subnet_mask
, &i
))
1522 return ERROR_COMMAND_SYNTAX_ERROR
;
1526 subnet_mask
= (uint32_t)(subnet_bits
< 32 ?
1527 ((1ULL << subnet_bits
) - 1) : 0xffffffff);
1529 cpy_ip(tmp_config
.ip_address
, ip_address
);
1530 cpy_ip(tmp_config
.subnet_mask
, (uint8_t *)&subnet_mask
);
1536 COMMAND_HANDLER(jlink_handle_config_reset_command
)
1538 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
))
1541 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
1547 COMMAND_HANDLER(jlink_handle_config_write_command
)
1551 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1552 command_print(CMD_CTX
, "Reading configuration is not supported by the "
1557 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_WRITE_CONFIG
)) {
1558 command_print(CMD_CTX
, "Writing configuration is not supported by the "
1563 if (!memcmp(&config
, &tmp_config
, sizeof(struct device_config
))) {
1564 command_print(CMD_CTX
, "Operation not performed due to no changes in "
1565 "the configuration.");
1569 ret
= jaylink_write_raw_config(devh
, (const uint8_t *)&tmp_config
);
1571 if (ret
!= JAYLINK_OK
) {
1572 LOG_ERROR("jaylink_write_raw_config() failed: %s.",
1573 jaylink_strerror_name(ret
));
1577 if (!read_device_config(&config
)) {
1578 LOG_ERROR("Failed to read device configuration for verification.");
1582 if (memcmp(&config
, &tmp_config
, sizeof(struct device_config
))) {
1583 LOG_ERROR("Verification of device configuration failed. Please check "
1588 memcpy(&tmp_config
, &config
, sizeof(struct device_config
));
1589 command_print(CMD_CTX
, "The new device configuration applies after power "
1590 "cycling the J-Link device.");
1595 COMMAND_HANDLER(jlink_handle_config_command
)
1597 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_READ_CONFIG
)) {
1598 command_print(CMD_CTX
, "Device doesn't support reading configuration.");
1603 show_config(CMD_CTX
);
1608 COMMAND_HANDLER(jlink_handle_emucom_write_command
)
1618 return ERROR_COMMAND_SYNTAX_ERROR
;
1620 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_EMUCOM
)) {
1621 LOG_ERROR("Device does not support EMUCOM.");
1625 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], channel
);
1627 tmp
= strlen(CMD_ARGV
[1]);
1630 LOG_ERROR("Data must be encoded as hexadecimal pairs.");
1631 return ERROR_COMMAND_ARGUMENT_INVALID
;
1634 buf
= malloc(tmp
/ 2);
1637 LOG_ERROR("Failed to allocate buffer.");
1641 dummy
= unhexify(buf
, CMD_ARGV
[1], tmp
/ 2);
1643 if (dummy
!= (tmp
/ 2)) {
1644 LOG_ERROR("Data must be encoded as hexadecimal pairs.");
1646 return ERROR_COMMAND_ARGUMENT_INVALID
;
1650 ret
= jaylink_emucom_write(devh
, channel
, buf
, &length
);
1654 if (ret
== JAYLINK_ERR_DEV_NOT_SUPPORTED
) {
1655 LOG_ERROR("Channel not supported by the device.");
1657 } else if (ret
!= JAYLINK_OK
) {
1658 LOG_ERROR("Failed to write to channel: %s.",
1659 jaylink_strerror_name(ret
));
1663 if (length
!= (tmp
/ 2))
1664 LOG_WARNING("Only %" PRIu32
" bytes written to the channel.", length
);
1669 COMMAND_HANDLER(jlink_handle_emucom_read_command
)
1678 return ERROR_COMMAND_SYNTAX_ERROR
;
1680 if (!jaylink_has_cap(caps
, JAYLINK_DEV_CAP_EMUCOM
)) {
1681 LOG_ERROR("Device does not support EMUCOM.");
1685 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], channel
);
1686 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
1688 buf
= malloc(length
* 3 + 1);
1691 LOG_ERROR("Failed to allocate buffer.");
1695 ret
= jaylink_emucom_read(devh
, channel
, buf
, &length
);
1697 if (ret
== JAYLINK_ERR_DEV_NOT_SUPPORTED
) {
1698 LOG_ERROR("Channel is not supported by the device.");
1701 } else if (ret
== JAYLINK_ERR_DEV_NOT_AVAILABLE
) {
1702 LOG_ERROR("Channel is not available for the requested amount of data. "
1703 "%" PRIu32
" bytes are avilable.", length
);
1706 } else if (ret
!= JAYLINK_OK
) {
1707 LOG_ERROR("Failed to read from channel: %s.",
1708 jaylink_strerror_name(ret
));
1713 tmp
= hexify((char *)buf
+ length
, buf
, length
, 2 * length
+ 1);
1715 if (tmp
!= 2 * length
) {
1716 LOG_ERROR("Failed to convert data into hexadecimal string.");
1721 command_print(CMD_CTX
, "%s", buf
+ length
);
1727 static const struct command_registration jlink_config_subcommand_handlers
[] = {
1730 .handler
= &jlink_handle_config_usb_address_command
,
1731 .mode
= COMMAND_EXEC
,
1732 .help
= "set the USB address",
1736 .name
= "targetpower",
1737 .handler
= &jlink_handle_config_target_power_command
,
1738 .mode
= COMMAND_EXEC
,
1739 .help
= "set the target power supply",
1744 .handler
= &jlink_handle_config_mac_address_command
,
1745 .mode
= COMMAND_EXEC
,
1746 .help
= "set the MAC Address",
1747 .usage
= "[ff:ff:ff:ff:ff:ff]",
1751 .handler
= &jlink_handle_config_ip_address_command
,
1752 .mode
= COMMAND_EXEC
,
1753 .help
= "set the IP address, where A.B.C.D is the IP address, "
1754 "E the bit of the subnet mask, F.G.H.I the subnet mask",
1755 .usage
= "[A.B.C.D[/E] [F.G.H.I]]",
1759 .handler
= &jlink_handle_config_reset_command
,
1760 .mode
= COMMAND_EXEC
,
1761 .help
= "undo configuration changes"
1765 .handler
= &jlink_handle_config_write_command
,
1766 .mode
= COMMAND_EXEC
,
1767 .help
= "write configuration to the device"
1769 COMMAND_REGISTRATION_DONE
1772 static const struct command_registration jlink_emucom_subcommand_handlers
[] = {
1775 .handler
= &jlink_handle_emucom_write_command
,
1776 .mode
= COMMAND_EXEC
,
1777 .help
= "write to a channel",
1778 .usage
= "<channel> <data>",
1782 .handler
= &jlink_handle_emucom_read_command
,
1783 .mode
= COMMAND_EXEC
,
1784 .help
= "read from a channel",
1785 .usage
= "<channel> <length>"
1787 COMMAND_REGISTRATION_DONE
1790 static const struct command_registration jlink_subcommand_handlers
[] = {
1793 .handler
= &jlink_handle_jlink_jtag_command
,
1794 .mode
= COMMAND_EXEC
,
1795 .help
= "select the JTAG command version",
1799 .name
= "targetpower",
1800 .handler
= &jlink_handle_target_power_command
,
1801 .mode
= COMMAND_EXEC
,
1802 .help
= "set the target power supply",
1807 .handler
= &jlink_handle_free_memory_command
,
1808 .mode
= COMMAND_EXEC
,
1809 .help
= "show free device memory"
1813 .handler
= &jlink_handle_hwstatus_command
,
1814 .mode
= COMMAND_EXEC
,
1815 .help
= "show the hardware status"
1819 .handler
= &jlink_usb_command
,
1820 .mode
= COMMAND_CONFIG
,
1821 .help
= "set the USB address of the device that should be used",
1826 .handler
= &jlink_serial_command
,
1827 .mode
= COMMAND_CONFIG
,
1828 .help
= "set the serial number of the device that should be used",
1829 .usage
= "<serial number>"
1833 .handler
= &jlink_handle_config_command
,
1834 .mode
= COMMAND_EXEC
,
1835 .help
= "access the device configuration. If no argument is given "
1836 "this will show the device configuration",
1837 .chain
= jlink_config_subcommand_handlers
,
1841 .mode
= COMMAND_EXEC
,
1842 .help
= "access EMUCOM channel",
1843 .chain
= jlink_emucom_subcommand_handlers
1845 COMMAND_REGISTRATION_DONE
1848 static const struct command_registration jlink_command_handlers
[] = {
1851 .mode
= COMMAND_ANY
,
1852 .help
= "perform jlink management",
1853 .chain
= jlink_subcommand_handlers
,
1855 COMMAND_REGISTRATION_DONE
1858 static int jlink_swd_init(void)
1860 iface
= JAYLINK_TIF_SWD
;
1865 static void jlink_swd_write_reg(uint8_t cmd
, uint32_t value
, uint32_t ap_delay_clk
)
1867 assert(!(cmd
& SWD_CMD_RnW
));
1868 jlink_swd_queue_cmd(cmd
, NULL
, value
, ap_delay_clk
);
1871 static void jlink_swd_read_reg(uint8_t cmd
, uint32_t *value
, uint32_t ap_delay_clk
)
1873 assert(cmd
& SWD_CMD_RnW
);
1874 jlink_swd_queue_cmd(cmd
, value
, 0, ap_delay_clk
);
1877 static int_least32_t jlink_swd_frequency(int_least32_t hz
)
1880 jlink_speed(hz
/ 1000);
1885 /***************************************************************************/
1886 /* J-Link tap functions */
1888 static unsigned tap_length
;
1889 /* In SWD mode use tms buffer for direction control */
1890 static uint8_t tms_buffer
[JLINK_TAP_BUFFER_SIZE
];
1891 static uint8_t tdi_buffer
[JLINK_TAP_BUFFER_SIZE
];
1892 static uint8_t tdo_buffer
[JLINK_TAP_BUFFER_SIZE
];
1894 struct pending_scan_result
{
1895 /** First bit position in tdo_buffer to read. */
1897 /** Number of bits to read. */
1899 /** Location to store the result */
1901 /** Offset in the destination buffer */
1902 unsigned buffer_offset
;
1905 #define MAX_PENDING_SCAN_RESULTS 256
1907 static int pending_scan_results_length
;
1908 static struct pending_scan_result pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
1910 static void jlink_tap_init(void)
1913 pending_scan_results_length
= 0;
1914 memset(tms_buffer
, 0, sizeof(tms_buffer
));
1915 memset(tdi_buffer
, 0, sizeof(tdi_buffer
));
1918 static void jlink_clock_data(const uint8_t *out
, unsigned out_offset
,
1919 const uint8_t *tms_out
, unsigned tms_offset
,
1920 uint8_t *in
, unsigned in_offset
,
1924 unsigned available_length
= JLINK_TAP_BUFFER_SIZE
- tap_length
/ 8;
1926 if (!available_length
||
1927 (in
&& pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
)) {
1928 if (jlink_flush() != ERROR_OK
)
1930 available_length
= JLINK_TAP_BUFFER_SIZE
;
1933 struct pending_scan_result
*pending_scan_result
=
1934 &pending_scan_results_buffer
[pending_scan_results_length
];
1936 unsigned scan_length
= length
> available_length
?
1937 available_length
: length
;
1940 buf_set_buf(out
, out_offset
, tdi_buffer
, tap_length
, scan_length
);
1942 buf_set_buf(tms_out
, tms_offset
, tms_buffer
, tap_length
, scan_length
);
1945 pending_scan_result
->first
= tap_length
;
1946 pending_scan_result
->length
= scan_length
;
1947 pending_scan_result
->buffer
= in
;
1948 pending_scan_result
->buffer_offset
= in_offset
;
1949 pending_scan_results_length
++;
1952 tap_length
+= scan_length
;
1953 out_offset
+= scan_length
;
1954 tms_offset
+= scan_length
;
1955 in_offset
+= scan_length
;
1956 length
-= scan_length
;
1957 } while (length
> 0);
1960 static int jlink_flush(void)
1968 jlink_last_state
= jtag_debug_state_machine(tms_buffer
, tdi_buffer
,
1969 tap_length
, jlink_last_state
);
1971 ret
= jaylink_jtag_io(devh
, tms_buffer
, tdi_buffer
, tdo_buffer
,
1972 tap_length
, jtag_command_version
);
1974 if (ret
!= JAYLINK_OK
) {
1975 LOG_ERROR("jaylink_jtag_io() failed: %s.", jaylink_strerror_name(ret
));
1977 return ERROR_JTAG_QUEUE_FAILED
;
1980 for (i
= 0; i
< pending_scan_results_length
; i
++) {
1981 struct pending_scan_result
*p
= &pending_scan_results_buffer
[i
];
1983 buf_set_buf(tdo_buffer
, p
->first
, p
->buffer
,
1984 p
->buffer_offset
, p
->length
);
1986 DEBUG_JTAG_IO("Pending scan result, length = %d.", p
->length
);
1994 static void fill_buffer(uint8_t *buf
, uint32_t val
, uint32_t len
)
1996 unsigned int tap_pos
= tap_length
;
1999 buf_set_u32(buf
, tap_pos
, 32, val
);
2005 buf_set_u32(buf
, tap_pos
, len
, val
);
2008 static void jlink_queue_data_out(const uint8_t *data
, uint32_t len
)
2010 const uint32_t dir_out
= 0xffffffff;
2013 bit_copy(tdi_buffer
, tap_length
, data
, 0, len
);
2015 fill_buffer(tdi_buffer
, 0, len
);
2017 fill_buffer(tms_buffer
, dir_out
, len
);
2021 static void jlink_queue_data_in(uint32_t len
)
2023 const uint32_t dir_in
= 0;
2025 fill_buffer(tms_buffer
, dir_in
, len
);
2029 static int jlink_swd_switch_seq(enum swd_special_seq seq
)
2036 LOG_DEBUG("SWD line reset");
2037 s
= swd_seq_line_reset
;
2038 s_len
= swd_seq_line_reset_len
;
2041 LOG_DEBUG("JTAG-to-SWD");
2042 s
= swd_seq_jtag_to_swd
;
2043 s_len
= swd_seq_jtag_to_swd_len
;
2046 LOG_DEBUG("SWD-to-JTAG");
2047 s
= swd_seq_swd_to_jtag
;
2048 s_len
= swd_seq_swd_to_jtag_len
;
2051 LOG_ERROR("Sequence %d not supported.", seq
);
2055 jlink_queue_data_out(s
, s_len
);
2060 static int jlink_swd_run_queue(void)
2065 LOG_DEBUG("Executing %d queued transactions.", pending_scan_results_length
);
2067 if (queued_retval
!= ERROR_OK
) {
2068 LOG_DEBUG("Skipping due to previous errors: %d.", queued_retval
);
2073 * A transaction must be followed by another transaction or at least 8 idle
2074 * cycles to ensure that data is clocked through the AP.
2076 jlink_queue_data_out(NULL
, 8);
2078 ret
= jaylink_swd_io(devh
, tms_buffer
, tdi_buffer
, tdo_buffer
, tap_length
);
2080 if (ret
!= JAYLINK_OK
) {
2081 LOG_ERROR("jaylink_swd_io() failed: %s.", jaylink_strerror_name(ret
));
2085 for (i
= 0; i
< pending_scan_results_length
; i
++) {
2086 int ack
= buf_get_u32(tdo_buffer
, pending_scan_results_buffer
[i
].first
, 3);
2088 if (ack
!= SWD_ACK_OK
) {
2089 LOG_DEBUG("SWD ack not OK: %d %s", ack
,
2090 ack
== SWD_ACK_WAIT
? "WAIT" : ack
== SWD_ACK_FAULT
? "FAULT" : "JUNK");
2091 queued_retval
= ack
== SWD_ACK_WAIT
? ERROR_WAIT
: ERROR_FAIL
;
2093 } else if (pending_scan_results_buffer
[i
].length
) {
2094 uint32_t data
= buf_get_u32(tdo_buffer
, 3 + pending_scan_results_buffer
[i
].first
, 32);
2095 int parity
= buf_get_u32(tdo_buffer
, 3 + 32 + pending_scan_results_buffer
[i
].first
, 1);
2097 if (parity
!= parity_u32(data
)) {
2098 LOG_ERROR("SWD: Read data parity mismatch.");
2099 queued_retval
= ERROR_FAIL
;
2103 if (pending_scan_results_buffer
[i
].buffer
)
2104 *(uint32_t *)pending_scan_results_buffer
[i
].buffer
= data
;
2110 ret
= queued_retval
;
2111 queued_retval
= ERROR_OK
;
2116 static void jlink_swd_queue_cmd(uint8_t cmd
, uint32_t *dst
, uint32_t data
, uint32_t ap_delay_clk
)
2118 uint8_t data_parity_trn
[DIV_ROUND_UP(32 + 1, 8)];
2119 if (tap_length
+ 46 + 8 + ap_delay_clk
>= sizeof(tdi_buffer
) * 8 ||
2120 pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
) {
2121 /* Not enough room in the queue. Run the queue. */
2122 queued_retval
= jlink_swd_run_queue();
2125 if (queued_retval
!= ERROR_OK
)
2128 cmd
|= SWD_CMD_START
| SWD_CMD_PARK
;
2130 jlink_queue_data_out(&cmd
, 8);
2132 pending_scan_results_buffer
[pending_scan_results_length
].first
= tap_length
;
2134 if (cmd
& SWD_CMD_RnW
) {
2135 /* Queue a read transaction. */
2136 pending_scan_results_buffer
[pending_scan_results_length
].length
= 32;
2137 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= dst
;
2139 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2141 /* Queue a write transaction. */
2142 pending_scan_results_buffer
[pending_scan_results_length
].length
= 0;
2143 jlink_queue_data_in(1 + 3 + 1);
2145 buf_set_u32(data_parity_trn
, 0, 32, data
);
2146 buf_set_u32(data_parity_trn
, 32, 1, parity_u32(data
));
2148 jlink_queue_data_out(data_parity_trn
, 32 + 1);
2151 pending_scan_results_length
++;
2153 /* Insert idle cycles after AP accesses to avoid WAIT. */
2154 if (cmd
& SWD_CMD_APnDP
)
2155 jlink_queue_data_out(NULL
, ap_delay_clk
);
2158 static const struct swd_driver jlink_swd
= {
2159 .init
= &jlink_swd_init
,
2160 .frequency
= &jlink_swd_frequency
,
2161 .switch_seq
= &jlink_swd_switch_seq
,
2162 .read_reg
= &jlink_swd_read_reg
,
2163 .write_reg
= &jlink_swd_write_reg
,
2164 .run
= &jlink_swd_run_queue
,
2167 static const char * const jlink_transports
[] = { "jtag", "swd", NULL
};
2169 struct jtag_interface jlink_interface
= {
2171 .commands
= jlink_command_handlers
,
2172 .transports
= jlink_transports
,
2174 .execute_queue
= &jlink_execute_queue
,
2175 .speed
= &jlink_speed
,
2176 .speed_div
= &jlink_speed_div
,
2178 .init
= &jlink_init
,
2179 .quit
= &jlink_quit
,
2180 .config_trace
= &config_trace
,
2181 .poll_trace
= &poll_trace
,