35 #include <libjaylink/libjaylink.h>
37 static struct jaylink_context *
jayctx;
38 static struct jaylink_device_handle *
devh;
39 static struct jaylink_connection
conn;
40 static struct jaylink_connection
connlist[JAYLINK_MAX_CONNECTIONS];
42 static uint8_t
caps[JAYLINK_DEV_EXT_CAPS_SIZE];
47 static enum jaylink_target_interface
iface = JAYLINK_TIF_JTAG;
50 #define JLINK_MAX_SPEED 12000
51 #define JLINK_TAP_BUFFER_SIZE 2048
56 #define SWO_MAX_FREQ_DEV 0.03
106 const uint8_t *tms_out,
unsigned int tms_offset,
107 uint8_t *in,
unsigned int in_offset,
124 LOG_DEBUG_IO(
"runtest %i cycles, end in %i",
cmd->cmd.runtest->num_cycles,
125 cmd->cmd.runtest->end_state);
148 cmd->cmd.pathmove->num_states,
149 cmd->cmd.pathmove->path[
cmd->cmd.pathmove->num_states - 1]);
156 LOG_DEBUG_IO(
"%s type:%d",
cmd->cmd.scan->ir_scan ?
"IRSCAN" :
"DRSCAN",
160 while (
cmd->cmd.scan->num_fields > 0
161 &&
cmd->cmd.scan->fields[
cmd->cmd.scan->num_fields - 1].num_bits == 0) {
162 cmd->cmd.scan->num_fields--;
163 LOG_DEBUG(
"discarding trailing empty field");
166 if (!
cmd->cmd.scan->num_fields) {
172 if (
cmd->cmd.scan->ir_scan) {
197 unsigned int scan_size = 0;
199 for (
unsigned int i = 0; i <
cmd->cmd.scan->num_fields; i++, field++) {
205 cmd->cmd.scan->num_fields,
221 uint8_t last_bit = 0;
224 uint8_t tms_bits = 0x01;
270 (
cmd->cmd.scan->ir_scan) ?
"IR" :
"DR", scan_size,
302 LOG_ERROR(
"BUG: Unknown JTAG command type encountered");
331 struct jaylink_speed tmp;
334 if (jaylink_has_cap(
caps, JAYLINK_DEV_CAP_GET_SPEEDS)) {
335 ret = jaylink_get_speeds(
devh, &tmp);
337 if (ret != JAYLINK_OK) {
338 LOG_ERROR(
"jaylink_get_speeds() failed: %s",
339 jaylink_strerror(ret));
344 max_speed = tmp.freq / tmp.div;
350 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING)) {
351 LOG_ERROR(
"Adaptive clocking is not supported by the device");
355 speed = JAYLINK_SPEED_ADAPTIVE_CLOCKING;
356 }
else if (speed > max_speed) {
357 LOG_INFO(
"Reduced speed from %d kHz to %d kHz (maximum)", speed,
362 ret = jaylink_set_speed(
devh, speed);
364 if (ret != JAYLINK_OK) {
365 LOG_ERROR(
"jaylink_set_speed() failed: %s",
366 jaylink_strerror(ret));
391 ret = jaylink_read_raw_config(
devh, (uint8_t *)cfg);
393 if (ret != JAYLINK_OK) {
394 LOG_ERROR(
"jaylink_read_raw_config() failed: %s",
395 jaylink_strerror(ret));
413 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_SELECT_TIF)) {
414 if (
iface != JAYLINK_TIF_JTAG) {
415 LOG_ERROR(
"Device supports JTAG transport only");
422 ret = jaylink_get_available_interfaces(
devh, &interfaces);
424 if (ret != JAYLINK_OK) {
425 LOG_ERROR(
"jaylink_get_available_interfaces() failed: %s",
426 jaylink_strerror(ret));
430 if (!(interfaces & (1 <<
iface))) {
431 LOG_ERROR(
"Selected transport is not supported by the device");
438 LOG_ERROR(
"jaylink_select_interface() failed: %s",
439 jaylink_strerror(ret));
453 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_REGISTER))
458 if (ret != JAYLINK_OK) {
459 LOG_ERROR(
"jaylink_register() failed: %s", jaylink_strerror(ret));
463 handle_found =
false;
465 for (i = 0; i <
count; i++) {
473 LOG_ERROR(
"Registration failed: maximum number of connections on the "
491 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
494 ret = jaylink_get_free_memory(
devh, &tmp);
496 if (ret != JAYLINK_OK) {
497 LOG_ERROR(
"jaylink_get_free_memory() failed: %s",
498 jaylink_strerror(ret));
503 LOG_ERROR(
"Not enough free device internal memory: %" PRIu32
" bytes", tmp);
511 LOG_DEBUG(
"Adjusted SWD transaction buffer size to %u bytes",
519 enum jaylink_log_level level,
const char *format, va_list args,
525 case JAYLINK_LOG_LEVEL_ERROR:
528 case JAYLINK_LOG_LEVEL_WARNING:
535 case JAYLINK_LOG_LEVEL_INFO:
536 case JAYLINK_LOG_LEVEL_DEBUG:
539 case JAYLINK_LOG_LEVEL_DEBUG_IO:
559 retval = jaylink_device_get_usb_bus_ports(dev, &bus, &ports, &num_ports);
561 if (retval == JAYLINK_ERR_NOT_SUPPORTED) {
563 }
else if (retval != JAYLINK_OK) {
564 LOG_WARNING(
"jaylink_device_get_usb_bus_ports() failed: %s",
565 jaylink_strerror(retval));
578 int ret = jaylink_discovery_scan(
jayctx, ifaces);
579 if (ret != JAYLINK_OK) {
580 LOG_ERROR(
"jaylink_discovery_scan() failed: %s", jaylink_strerror(ret));
586 struct jaylink_device **
devs;
587 ret = jaylink_get_devices(
jayctx, &
devs, &num_devices);
589 if (ret != JAYLINK_OK) {
590 LOG_ERROR(
"jaylink_get_devices() failed: %s", jaylink_strerror(ret));
599 LOG_ERROR(
"Multiple devices found, specify the desired device");
601 for (
size_t i = 0;
devs[i]; i++) {
603 ret = jaylink_device_get_serial_number(
devs[i], &
serial);
604 if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
606 }
else if (ret != JAYLINK_OK) {
607 LOG_WARNING(
"jaylink_device_get_serial_number() failed: %s",
608 jaylink_strerror(ret));
611 char name[JAYLINK_NICKNAME_MAX_LENGTH];
612 int name_ret = jaylink_device_get_nickname(
devs[i],
name);
613 if (name_ret == JAYLINK_OK)
619 jaylink_free_devices(
devs,
true);
624 *found_device =
false;
626 uint32_t serial_number;
627 ret = jaylink_parse_serial_number(adapter_serial, &serial_number);
628 if (ret != JAYLINK_OK)
631 for (
size_t i = 0;
devs[i]; i++) {
632 struct jaylink_device *dev =
devs[i];
634 if (adapter_serial) {
640 char nickname[JAYLINK_NICKNAME_MAX_LENGTH];
641 ret = jaylink_device_get_nickname(dev, nickname);
642 if (ret != JAYLINK_OK || strcmp(nickname, adapter_serial) != 0) {
647 ret = jaylink_device_get_serial_number(dev, &tmp);
648 if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
650 }
else if (ret != JAYLINK_OK) {
651 LOG_WARNING(
"jaylink_device_get_serial_number() failed: %s",
652 jaylink_strerror(ret));
656 if (serial_number != tmp)
662 enum jaylink_usb_address
address;
663 ret = jaylink_device_get_usb_address(dev, &
address);
665 if (ret == JAYLINK_ERR_NOT_SUPPORTED) {
667 }
else if (ret != JAYLINK_OK) {
668 LOG_WARNING(
"jaylink_device_get_usb_address() failed: %s",
669 jaylink_strerror(ret));
680 ret = jaylink_open(dev, &
devh);
682 if (ret == JAYLINK_OK) {
683 *found_device =
true;
687 LOG_ERROR(
"Failed to open device: %s", jaylink_strerror(ret));
690 jaylink_free_devices(
devs,
true);
698 char *firmware_version;
699 struct jaylink_hardware_version hwver;
700 struct jaylink_hardware_status hwstatus;
703 LOG_DEBUG(
"Using libjaylink %s (compiled with %s)",
704 jaylink_version_package_get_string(), JAYLINK_VERSION_PACKAGE_STRING);
706 if (!jaylink_library_has_cap(JAYLINK_CAP_HIF_USB) &&
use_usb_address) {
707 LOG_ERROR(
"J-Link driver does not support USB devices");
711 ret = jaylink_init(&
jayctx);
713 if (ret != JAYLINK_OK) {
714 LOG_ERROR(
"jaylink_init() failed: %s", jaylink_strerror(ret));
720 if (ret != JAYLINK_OK) {
721 LOG_ERROR(
"jaylink_log_set_callback() failed: %s",
722 jaylink_strerror(ret));
752 ret = jaylink_get_firmware_version(
devh, &firmware_version, &
length);
754 if (ret != JAYLINK_OK) {
755 LOG_ERROR(
"jaylink_get_firmware_version() failed: %s",
756 jaylink_strerror(ret));
762 free(firmware_version);
764 LOG_WARNING(
"Device responds empty firmware version string");
767 memset(
caps, 0, JAYLINK_DEV_EXT_CAPS_SIZE);
770 if (ret != JAYLINK_OK) {
771 LOG_ERROR(
"jaylink_get_caps() failed: %s", jaylink_strerror(ret));
777 if (jaylink_has_cap(
caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
778 ret = jaylink_get_extended_caps(
devh,
caps);
780 if (ret != JAYLINK_OK) {
781 LOG_ERROR(
"jaylink_get_extended_caps() failed: %s",
782 jaylink_strerror(ret));
791 if (jaylink_has_cap(
caps, JAYLINK_DEV_CAP_GET_HW_VERSION)) {
792 ret = jaylink_get_hardware_version(
devh, &hwver);
794 if (ret != JAYLINK_OK) {
795 LOG_ERROR(
"Failed to retrieve hardware version: %s",
796 jaylink_strerror(ret));
802 LOG_INFO(
"Hardware version: %u.%02u", hwver.major, hwver.minor);
804 if (hwver.major >= 5)
808 if (
iface == JAYLINK_TIF_SWD) {
822 if (jaylink_has_cap(
caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
824 LOG_ERROR(
"Failed to read device configuration data");
833 ret = jaylink_get_hardware_status(
devh, &hwstatus);
835 if (ret != JAYLINK_OK) {
836 LOG_ERROR(
"jaylink_get_hardware_status() failed: %s",
837 jaylink_strerror(ret));
843 LOG_INFO(
"VTarget = %u.%03u V", hwstatus.target_voltage / 1000,
844 hwstatus.target_voltage % 1000);
848 strcpy(
conn.hid,
"0.0.0.0");
874 if (
iface == JAYLINK_TIF_JTAG) {
899 ret = jaylink_swo_stop(
devh);
901 if (ret != JAYLINK_OK)
902 LOG_ERROR(
"jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
905 if (jaylink_has_cap(
caps, JAYLINK_DEV_CAP_REGISTER)) {
908 if (ret != JAYLINK_OK)
909 LOG_ERROR(
"jaylink_unregister() failed: %s",
910 jaylink_strerror(ret));
926 uint8_t tms_scan_bits;
943 for (
unsigned int i = 0; i < num_states; i++) {
950 LOG_ERROR(
"BUG: %s -> %s isn't a valid TAP transition",
968 for (
unsigned int i = 0; i < num_cycles; i++) {
1012 LOG_DEBUG(
"TRST: %i, SRST: %i", trst, srst);
1016 jaylink_set_reset(
devh);
1019 jaylink_clear_reset(
devh);
1022 jaylink_jtag_clear_trst(
devh);
1025 jaylink_jtag_set_trst(
devh);
1043 LOG_WARNING(
"DEPRECATED! Using the USB address is deprecated, use the serial number instead");
1048 if (tmp > JAYLINK_USB_ADDRESS_3) {
1062 struct jaylink_hardware_status
status;
1064 ret = jaylink_get_hardware_status(
devh, &
status);
1066 if (ret != JAYLINK_OK) {
1068 jaylink_strerror(ret));
1073 status.target_voltage / 1000,
status.target_voltage % 1000);
1079 if (
status.target_voltage < 1500)
1090 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY)) {
1096 ret = jaylink_get_free_memory(
devh, &tmp);
1098 if (ret != JAYLINK_OK) {
1100 jaylink_strerror(ret));
1112 unsigned int version;
1115 case JAYLINK_JTAG_VERSION_2:
1118 case JAYLINK_JTAG_VERSION_3:
1153 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1161 int ret = jaylink_get_hardware_info(
devh, JAYLINK_HW_INFO_TARGET_POWER,
1164 if (ret != JAYLINK_OK) {
1176 int ret = jaylink_set_target_power(
devh, enable);
1178 if (ret != JAYLINK_OK) {
1180 jaylink_strerror(ret));
1230 "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1242 const char *current_target_power;
1250 current_target_power =
"off";
1252 current_target_power =
"on";
1256 current_target_power);
1267 if (jaylink_has_cap(
caps, JAYLINK_DEV_CAP_SET_TARGET_POWER))
1270 if (jaylink_has_cap(
caps, JAYLINK_DEV_CAP_ETHERNET)) {
1285 if (ret != JAYLINK_OK) {
1286 LOG_ERROR(
"jaylink_swo_read() failed: %s", jaylink_strerror(ret));
1300 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
1303 ret = jaylink_get_free_memory(
devh, &tmp);
1305 if (ret != JAYLINK_OK) {
1306 LOG_ERROR(
"jaylink_get_free_memory() failed: %s",
1307 jaylink_strerror(ret));
1311 if (tmp > 0x3fff || tmp <= 0x600)
1316 return tmp & 0xffffff00;
1320 uint32_t trace_freq, uint16_t *prescaler)
1322 unsigned int presc = (traceclkin_freq + trace_freq / 2) / trace_freq;
1327 unsigned int max_deviation = (traceclkin_freq * 3) / 100;
1328 if (presc * trace_freq < traceclkin_freq - max_deviation ||
1329 presc * trace_freq > traceclkin_freq + max_deviation)
1338 unsigned int traceclkin_freq,
unsigned int *trace_freq,
1339 uint16_t *prescaler)
1345 for (divider = speed.min_div; divider <= speed.max_div; divider++) {
1346 *trace_freq = speed.freq / divider;
1352 deviation = fabs(1.0 - ((
double)*trace_freq * presc / traceclkin_freq));
1364 uint32_t port_size,
unsigned int *trace_freq,
1365 unsigned int traceclkin_freq, uint16_t *prescaler)
1369 struct jaylink_swo_speed speed;
1376 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_SWO)) {
1380 LOG_ERROR(
"Trace capturing is not supported by the device");
1384 ret = jaylink_swo_stop(
devh);
1386 if (ret != JAYLINK_OK) {
1387 LOG_ERROR(
"jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
1403 LOG_ERROR(
"Selected pin protocol is not supported");
1410 LOG_ERROR(
"Not enough free device memory to start trace capturing");
1414 ret = jaylink_swo_get_speeds(
devh, JAYLINK_SWO_MODE_UART, &speed);
1416 if (ret != JAYLINK_OK) {
1417 LOG_ERROR(
"jaylink_swo_get_speeds() failed: %s",
1418 jaylink_strerror(ret));
1422 if (*trace_freq > 0) {
1423 divider = speed.freq / *trace_freq;
1424 min_freq = speed.freq / speed.max_div;
1425 max_freq = speed.freq / speed.min_div;
1427 if (*trace_freq > max_freq) {
1428 LOG_INFO(
"Given SWO frequency too high, using %" PRIu32
" Hz instead",
1430 *trace_freq = max_freq;
1431 }
else if (*trace_freq < min_freq) {
1432 LOG_INFO(
"Given SWO frequency too low, using %" PRIu32
" Hz instead",
1434 *trace_freq = min_freq;
1435 }
else if (*trace_freq != speed.freq / divider) {
1436 *trace_freq = speed.freq / divider;
1438 LOG_INFO(
"Given SWO frequency is not supported by the device, "
1439 "using %u Hz instead", *trace_freq);
1444 LOG_ERROR(
"SWO frequency is not suitable. Please choose a "
1445 "different frequency or use auto-detection");
1449 LOG_INFO(
"Trying to auto-detect SWO frequency");
1453 LOG_ERROR(
"Maximum permitted frequency deviation of %.02f %% "
1455 LOG_ERROR(
"Auto-detection of SWO frequency failed");
1459 LOG_INFO(
"Using SWO frequency of %u Hz", *trace_freq);
1462 ret = jaylink_swo_start(
devh, JAYLINK_SWO_MODE_UART, *trace_freq,
1465 if (ret != JAYLINK_OK) {
1466 LOG_ERROR(
"jaylink_start_swo() failed: %s", jaylink_strerror(ret));
1470 LOG_DEBUG(
"Using %" PRIu32
" bytes device memory for trace capturing",
1485 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1497 if (tmp > JAYLINK_USB_ADDRESS_3) {
1514 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1520 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1531 }
else if (!strcmp(
CMD_ARGV[0],
"off")) {
1553 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1559 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_ETHERNET)) {
1570 if ((strlen(str) != 17) || (str[2] !=
':' || str[5] !=
':' ||
1571 str[8] !=
':' || str[11] !=
':' || str[14] !=
':')) {
1576 for (i = 5; i >= 0; i--) {
1577 addr[i] = strtoul(str, &e, 16);
1586 if (!(0x01 &
addr[0])) {
1603 const char *s_save = s;
1609 for (i = 0; i < 4; i++) {
1610 lip[i] = strtoul(s, &e, 10);
1612 if (*e !=
'.' && i != 3)
1619 memcpy(ip, lip,
sizeof(lip));
1624 static void cpy_ip(uint8_t *dst, uint8_t *src)
1628 for (i = 0, j = 3; i < 4; i++, j--)
1637 uint8_t subnet_bits = 24;
1639 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1645 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_ETHERNET)) {
1678 ((1ULL << subnet_bits) - 1) : 0xffffffff);
1689 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_READ_CONFIG))
1702 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1708 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_WRITE_CONFIG)) {
1716 "the configuration");
1720 ret = jaylink_write_raw_config(
devh, (
const uint8_t *)&
tmp_config);
1722 if (ret != JAYLINK_OK) {
1723 LOG_ERROR(
"jaylink_write_raw_config() failed: %s",
1724 jaylink_strerror(ret));
1729 LOG_ERROR(
"Failed to read device configuration for verification");
1734 LOG_ERROR(
"Verification of device configuration failed. Please check "
1741 "cycling the J-Link device");
1748 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1771 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_EMUCOM)) {
1772 LOG_ERROR(
"Device does not support EMUCOM");
1781 LOG_ERROR(
"Data must be encoded as hexadecimal pairs");
1785 buf = malloc(tmp / 2);
1794 if (
dummy != (tmp / 2)) {
1795 LOG_ERROR(
"Data must be encoded as hexadecimal pairs");
1801 ret = jaylink_emucom_write(
devh, channel, buf, &
length);
1805 if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1806 LOG_ERROR(
"Channel not supported by the device");
1808 }
else if (ret != JAYLINK_OK) {
1809 LOG_ERROR(
"Failed to write to channel: %s", jaylink_strerror(ret));
1830 if (!jaylink_has_cap(
caps, JAYLINK_DEV_CAP_EMUCOM)) {
1831 LOG_ERROR(
"Device does not support EMUCOM");
1838 buf = malloc(
length * 3 + 1);
1845 ret = jaylink_emucom_read(
devh, channel, buf, &
length);
1847 if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1848 LOG_ERROR(
"Channel is not supported by the device");
1851 }
else if (ret == JAYLINK_ERR_DEV_NOT_AVAILABLE) {
1852 LOG_ERROR(
"Channel is not available for the requested amount of data. "
1853 "%" PRIu32
" bytes are available",
length);
1856 }
else if (ret != JAYLINK_OK) {
1857 LOG_ERROR(
"Failed to read from channel: %s", jaylink_strerror(ret));
1865 LOG_ERROR(
"Failed to convert data into hexadecimal string");
1879 .handler = &jlink_handle_config_usb_address_command,
1881 .help =
"set the USB address",
1885 .name =
"targetpower",
1886 .handler = &jlink_handle_config_target_power_command,
1888 .help =
"set the target power supply",
1893 .handler = &jlink_handle_config_mac_address_command,
1895 .help =
"set the MAC Address",
1896 .usage =
"[ff:ff:ff:ff:ff:ff]",
1900 .handler = &jlink_handle_config_ip_address_command,
1902 .help =
"set the IP address, where A.B.C.D is the IP address, "
1903 "E the bit of the subnet mask, F.G.H.I the subnet mask",
1904 .usage =
"[A.B.C.D[/E] [F.G.H.I]]",
1908 .handler = &jlink_handle_config_reset_command,
1910 .help =
"undo configuration changes",
1915 .handler = &jlink_handle_config_write_command,
1917 .help =
"write configuration to the device",
1926 .handler = &jlink_handle_emucom_write_command,
1928 .help =
"write to a channel",
1929 .usage =
"<channel> <data>",
1933 .handler = &jlink_handle_emucom_read_command,
1935 .help =
"read from a channel",
1936 .usage =
"<channel> <length>"
1944 .handler = &jlink_handle_jlink_jtag_command,
1946 .help =
"select the JTAG command version",
1950 .name =
"targetpower",
1951 .handler = &jlink_handle_target_power_command,
1953 .help =
"set the target power supply",
1954 .usage =
"[0|1|on|off]"
1958 .handler = &jlink_handle_free_memory_command,
1960 .help =
"show free device memory",
1965 .handler = &jlink_handle_hwstatus_command,
1967 .help =
"show the hardware status",
1972 .handler = &jlink_usb_command,
1974 .help =
"set the USB address of the device that should be used",
1979 .handler = &jlink_handle_config_command,
1981 .help =
"access the device configuration. If no argument is given "
1982 "this will show the device configuration",
1989 .help =
"access EMUCOM channel",
2000 .help =
"perform jlink management",
2009 iface = JAYLINK_TIF_SWD;
2052 #define MAX_PENDING_SCAN_RESULTS 256
2066 const uint8_t *tms_out,
unsigned int tms_offset,
2067 uint8_t *in,
unsigned int in_offset,
2073 if (!available_length ||
2084 unsigned int scan_length =
length > available_length ?
2085 available_length :
length;
2101 out_offset += scan_length;
2102 tms_offset += scan_length;
2103 in_offset += scan_length;
2124 if (ret != JAYLINK_OK) {
2125 LOG_ERROR(
"jaylink_jtag_io() failed: %s", jaylink_strerror(ret));
2160 const uint32_t dir_out = 0xffffffff;
2173 const uint32_t dir_in = 0;
2221 LOG_ERROR(
"Sequence %d not supported", seq);
2250 if (ret != JAYLINK_OK) {
2251 LOG_ERROR(
"jaylink_swd_io() failed: %s", jaylink_strerror(ret));
2269 LOG_ERROR(
"SWD: Read data parity mismatch");
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
bool adapter_usb_location_equal(uint8_t dev_bus, uint8_t *port_path, size_t path_len)
unsigned int adapter_get_speed_khz(void)
Retrieves the clock speed of the adapter in kHz.
const char * adapter_usb_get_location(void)
@ TPIU_PIN_PROTOCOL_ASYNC_UART
asynchronous output with NRZ coding
void * buf_set_buf(const void *_src, unsigned int src_start, void *_dst, unsigned int dst_start, unsigned int len)
size_t hexify(char *hex, const uint8_t *bin, size_t count, size_t length)
Convert binary data into a string of hexadecimal pairs.
size_t unhexify(uint8_t *bin, const char *hex, size_t count)
Convert a string of hexadecimal pairs into its binary representation.
static void bit_copy(uint8_t *dst, unsigned int dst_offset, const uint8_t *src, unsigned int src_offset, unsigned int bit_count)
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
void command_print(struct command_invocation *cmd, const char *format,...)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_COMMAND_ARGUMENT_INVALID
enum scan_type jtag_scan_type(const struct scan_command *cmd)
#define TPIU_ACPR_MAX_SWOSCALER
uint64_t buffer
Pointer to data buffer to send over SPI.
uint32_t size
Size of dw_spi_transaction::buffer.
uint32_t buffer_size
Size of dw_spi_program::buffer.
uint32_t address
Starting address. Sector aligned.
enum tap_state tap_get_end_state(void)
For more information,.
enum tap_state tap_state_transition(enum tap_state cur_state, bool tms)
Function tap_state_transition takes a current TAP state and returns the next state according to the t...
const char * tap_state_name(enum tap_state state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
int tap_get_tms_path_len(enum tap_state from, enum tap_state to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
void tap_set_end_state(enum tap_state new_end_state)
This function sets the state of an "end state follower" which tracks the state that any cable driver ...
int tap_set_end_state_stable(enum tap_state new_end_state)
Similar as tap_set_end_state(), checks new_end_state is stable.
enum tap_state tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
int tap_get_tms_path(enum tap_state from, enum tap_state to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
#define tap_set_state(new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
static enum tap_state jtag_debug_state_machine(const void *tms_buf, const void *tdi_buf, unsigned int tap_len, enum tap_state start_tap_state)
Prints verbose TAP state transitions for the given TMS/TDI buffers.
static void cpy_ip(uint8_t *dst, uint8_t *src)
static void jlink_queue_data_in(uint32_t len)
static uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE]
static int jlink_execute_sleep(struct jtag_command *cmd)
#define MAX_PENDING_SCAN_RESULTS
static int config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol, uint32_t port_size, unsigned int *trace_freq, unsigned int traceclkin_freq, uint16_t *prescaler)
static int poll_trace(uint8_t *buf, size_t *size)
static void show_config_ip_address(struct command_invocation *cmd)
static int jlink_quit(void)
static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE]
static int jlink_khz(int khz, int *jtag_speed)
static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
static void jlink_queue_data_out(const uint8_t *data, uint32_t len)
static const struct command_registration jlink_config_subcommand_handlers[]
struct pending_scan_result __attribute__
static int jlink_init(void)
static void jlink_reset(int trst, int srst)
static bool use_usb_location
static int jlink_swd_switch_seq(enum swd_special_seq seq)
static bool detect_swo_freq_and_prescaler(struct jaylink_swo_speed speed, unsigned int traceclkin_freq, unsigned int *trace_freq, uint16_t *prescaler)
static int jaylink_log_handler(const struct jaylink_context *ctx, enum jaylink_log_level level, const char *format, va_list args, void *user_data)
static int jlink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
static bool string_to_ip(const char *s, uint8_t *ip, int *pos)
static int jlink_execute_scan(struct jtag_command *cmd)
static bool calculate_swo_prescaler(unsigned int traceclkin_freq, uint32_t trace_freq, uint16_t *prescaler)
static void show_config_usb_address(struct command_invocation *cmd)
static void fill_buffer(uint8_t *buf, uint32_t val, uint32_t len)
static int jlink_stableclocks(unsigned int num_cycles)
#define JLINK_TAP_BUFFER_SIZE
static int jlink_open_device(uint32_t ifaces, bool *found_device)
static int jlink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
static int jlink_speed_div(int speed, int *khz)
static int jlink_swd_run_queue(void)
static enum jaylink_jtag_version jtag_command_version
static int jlink_register(void)
COMMAND_HANDLER(jlink_usb_command)
static struct jaylink_connection connlist[JAYLINK_MAX_CONNECTIONS]
static enum jaylink_usb_address usb_address
static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS]
static int jlink_reset_safe(int trst, int srst)
static const struct command_registration jlink_emucom_subcommand_handlers[]
static bool jlink_usb_location_equal(struct jaylink_device *dev)
static struct jtag_interface jlink_interface
static bool read_device_config(struct device_config *cfg)
static int select_interface(void)
static const struct command_registration jlink_command_handlers[]
static int jlink_speed(int speed)
static int jlink_execute_command(struct jtag_command *cmd)
static bool use_usb_address
static struct jaylink_connection conn
static int jlink_execute_statemove(struct jtag_command *cmd)
static int jlink_clock_data(const uint8_t *out, unsigned int out_offset, const uint8_t *tms_out, unsigned int tms_offset, uint8_t *in, unsigned int in_offset, unsigned int length)
Queue data to go out and in, flushing the queue as many times as necessary.
static int jlink_runtest(unsigned int num_cycles)
static void jlink_tap_init(void)
static int jlink_execute_runtest(struct jtag_command *cmd)
static int jlink_swd_init(void)
static struct jaylink_device_handle * devh
static struct device_config config
static void show_config_mac_address(struct command_invocation *cmd)
static int jlink_execute_queue(struct jtag_command *cmd_queue)
static void show_config_target_power(struct command_invocation *cmd)
static int jlink_state_move(void)
static struct jaylink_context * jayctx
static int jlink_flush(void)
static int jlink_execute_stableclocks(struct jtag_command *cmd)
static struct device_config tmp_config
static void show_config(struct command_invocation *cmd)
static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE]
static enum jaylink_target_interface iface
static bool adjust_swd_buffer_size(void)
static unsigned int tap_length
static enum tap_state jlink_last_state
static const struct command_registration jlink_subcommand_handlers[]
static unsigned int swd_buffer_size
struct adapter_driver jlink_adapter_driver
static uint32_t calculate_trace_buffer_size(void)
static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE]
static const struct swd_driver jlink_swd
static int jlink_execute_pathmove(struct jtag_command *cmd)
static int pending_scan_results_length
static bool trace_enabled
static int jlink_path_move(unsigned int num_states, enum tap_state *path)
void jtag_sleep(uint32_t us)
#define ERROR_JTAG_DEVICE_ERROR
tap_state
Defines JTAG Test Access Port states.
#define ERROR_JTAG_QUEUE_FAILED
#define ERROR_JTAG_INIT_FAILED
#define ERROR_JTAG_TRANSITION_INVALID
#define ERROR_JTAG_NOT_IMPLEMENTED
static struct libusb_device ** devs
The usb device list.
void log_vprintf_lf(enum log_levels level, const char *file, unsigned int line, const char *function, const char *format, va_list args)
#define LOG_DEBUG_IO(expr ...)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
target_addr_t addr
Start address to search for the control block.
Represents a driver for a debugging interface.
const char *const name
The name of the interface driver.
When run_command is called, a new instance will be created on the stack, filled with the proper value...
const char * usage
a string listing the options and arguments, required or optional
Represents a driver for a debugging interface.
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
unsigned int buffer_offset
Offset in the destination buffer.
uint8_t swd_cmd
SWD command.
int first
First bit position in tdo_buffer to read.
void * buffer
Location to store the result.
int length
Number of bits to read.
uint8_t * buffer
Location to store the result.
This structure defines a single scan field in the scan.
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
const uint8_t * out_value
A pointer to value to be scanned into the device.
unsigned int num_bits
The number of bits this field specifies.
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
static const unsigned int swd_seq_dormant_to_swd_len
static const uint8_t swd_seq_dormant_to_jtag[]
Dormant-to-JTAG sequence.
static const uint8_t swd_seq_dormant_to_swd[]
Dormant-to-SWD sequence.
static const uint8_t swd_seq_jtag_to_dormant[]
JTAG-to-dormant sequence.
static bool swd_cmd_returns_ack(uint8_t cmd)
Test if we can rely on ACK returned by SWD command.
static int swd_ack_to_error_code(uint8_t ack)
Convert SWD ACK value returned from DP to OpenOCD error code.
static uint8_t swd_cmd(bool is_read, bool is_ap, uint8_t regnum)
Construct a "cmd" byte, in lSB bit order, which swd_driver.read_reg() and swd_driver....
static const unsigned int swd_seq_jtag_to_swd_len
static const unsigned int swd_seq_line_reset_len
static const unsigned int swd_seq_dormant_to_jtag_len
static const unsigned int swd_seq_swd_to_dormant_len
static const uint8_t swd_seq_line_reset[]
SWD Line reset.
static const uint8_t swd_seq_jtag_to_swd[]
JTAG-to-SWD sequence.
static const uint8_t swd_seq_swd_to_jtag[]
SWD-to-JTAG sequence.
static const unsigned int swd_seq_swd_to_jtag_len
static const unsigned int swd_seq_jtag_to_dormant_len
static const uint8_t swd_seq_swd_to_dormant[]
SWD-to-dormant sequence.
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
static int parity_u32(uint32_t x)
Calculate the (even) parity of a 32-bit datum.
static unsigned int parity(unsigned int v)