static int jlink_speed(int speed)
{
int ret;
- uint32_t freq;
- uint16_t divider;
+ struct jaylink_speed tmp;
int max_speed;
if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) {
- ret = jaylink_get_speeds(devh, &freq, ÷r);
+ ret = jaylink_get_speeds(devh, &tmp);
if (ret != JAYLINK_OK) {
LOG_ERROR("jaylink_get_speeds() failed: %s.",
return ERROR_JTAG_DEVICE_ERROR;
}
- freq = freq / 1000;
- max_speed = freq / divider;
+ tmp.freq /= 1000;
+ max_speed = tmp.freq / tmp.div;
} else {
max_speed = JLINK_MAX_SPEED;
}
static int jlink_register(void)
{
int ret;
- int i;
+ size_t i;
bool handle_found;
+ size_t count;
if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER))
return ERROR_OK;
- ret = jaylink_register(devh, &conn, connlist, NULL, NULL);
+ ret = jaylink_register(devh, &conn, connlist, &count);
- if (ret < 0) {
+ if (ret != JAYLINK_OK) {
LOG_ERROR("jaylink_register() failed: %s.",
jaylink_strerror_name(ret));
return ERROR_FAIL;
handle_found = false;
- for (i = 0; i < ret; i++) {
+ for (i = 0; i < count; i++) {
if (connlist[i].handle == conn.handle) {
handle_found = true;
break;
return true;
}
-static int jaylink_log_handler(const struct jaylink_context *ctx, int level,
- const char *format, va_list args, void *user_data)
+static int jaylink_log_handler(const struct jaylink_context *ctx,
+ enum jaylink_log_level level, const char *format, va_list args,
+ void *user_data)
{
enum log_levels tmp;
enum jaylink_usb_address address;
size_t length;
+ LOG_DEBUG("Using libjaylink %s (compiled with %s).",
+ jaylink_version_package_get_string(), JAYLINK_VERSION_PACKAGE_STRING);
+
ret = jaylink_init(&jayctx);
if (ret != JAYLINK_OK) {
return ERROR_JTAG_INIT_FAILED;
}
- ret = jaylink_get_device_list(jayctx, &devs);
+ ret = jaylink_discovery_scan(jayctx, 0);
- if (ret < 0) {
- LOG_ERROR("jaylink_get_device_list() failed: %s.",
+ if (ret != JAYLINK_OK) {
+ LOG_ERROR("jaylink_discovery_scan() failed: %s.",
+ jaylink_strerror_name(ret));
+ jaylink_exit(jayctx);
+ return ERROR_JTAG_INIT_FAILED;
+ }
+
+ ret = jaylink_get_devices(jayctx, &devs, NULL);
+
+ if (ret != JAYLINK_OK) {
+ LOG_ERROR("jaylink_get_devices() failed: %s.",
jaylink_strerror_name(ret));
jaylink_exit(jayctx);
return ERROR_JTAG_INIT_FAILED;
LOG_ERROR("Failed to open device: %s.", jaylink_strerror_name(ret));
}
- jaylink_free_device_list(devs, 1);
+ jaylink_free_devices(devs, true);
if (!found_device) {
LOG_ERROR("No J-Link device found.");
}
}
- jtag_command_version = JAYLINK_JTAG_V2;
+ jtag_command_version = JAYLINK_JTAG_VERSION_2;
if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_HW_VERSION)) {
ret = jaylink_get_hardware_version(devh, &hwver);
LOG_INFO("Hardware version: %u.%02u", hwver.major, hwver.minor);
if (hwver.major >= 5)
- jtag_command_version = JAYLINK_JTAG_V3;
+ jtag_command_version = JAYLINK_JTAG_VERSION_3;
}
if (iface == JAYLINK_TIF_SWD) {
conn.handle = 0;
conn.pid = 0;
- conn.hid = 0;
+ strcpy(conn.hid, "0.0.0.0");
conn.iid = 0;
conn.cid = 0;
static int jlink_quit(void)
{
int ret;
+ size_t count;
if (trace_enabled) {
ret = jaylink_swo_stop(devh);
}
if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER)) {
- ret = jaylink_unregister(devh, &conn, connlist, NULL, NULL);
+ ret = jaylink_unregister(devh, &conn, connlist, &count);
- if (ret < 0)
+ if (ret != JAYLINK_OK)
LOG_ERROR("jaylink_unregister() failed: %s.",
jaylink_strerror_name(ret));
}
COMMAND_HANDLER(jlink_serial_command)
{
+ int ret;
+
if (CMD_ARGC != 1) {
command_print(CMD_CTX, "Need exactly one argument for jlink serial.");
return ERROR_COMMAND_SYNTAX_ERROR;
}
- if (sscanf(CMD_ARGV[0], "%" SCNd32, &serial_number) != 1) {
+ ret = jaylink_parse_serial_number(CMD_ARGV[0], &serial_number);
+
+ if (ret == JAYLINK_ERR) {
command_print(CMD_CTX, "Invalid serial number: %s.", CMD_ARGV[0]);
return ERROR_FAIL;
+ } else if (ret != JAYLINK_OK) {
+ command_print(CMD_CTX, "jaylink_parse_serial_number() failed: %s.",
+ jaylink_strerror_name(ret));
+ return ERROR_FAIL;
}
use_serial_number = true;
if (!CMD_ARGC) {
switch (jtag_command_version) {
- case JAYLINK_JTAG_V2:
+ case JAYLINK_JTAG_VERSION_2:
version = 2;
break;
- case JAYLINK_JTAG_V3:
+ case JAYLINK_JTAG_VERSION_3:
version = 3;
break;
default:
switch (tmp) {
case 2:
- jtag_command_version = JAYLINK_JTAG_V2;
+ jtag_command_version = JAYLINK_JTAG_VERSION_2;
break;
case 3:
- jtag_command_version = JAYLINK_JTAG_V3;
+ jtag_command_version = JAYLINK_JTAG_VERSION_3;
break;
default:
command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
return ERROR_OK;
}
+COMMAND_HANDLER(jlink_handle_emucom_write_command)
+{
+ int ret;
+ size_t tmp;
+ uint32_t channel;
+ uint32_t length;
+ uint8_t *buf;
+ size_t dummy;
+
+ if (CMD_ARGC != 2)
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
+ if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
+ LOG_ERROR("Device does not support EMUCOM.");
+ return ERROR_FAIL;
+ }
+
+ COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
+
+ tmp = strlen(CMD_ARGV[1]);
+
+ if (tmp % 2 != 0) {
+ LOG_ERROR("Data must be encoded as hexadecimal pairs.");
+ return ERROR_COMMAND_ARGUMENT_INVALID;
+ }
+
+ buf = malloc(tmp / 2);
+
+ if (!buf) {
+ LOG_ERROR("Failed to allocate buffer.");
+ return ERROR_FAIL;
+ }
+
+ dummy = unhexify(buf, CMD_ARGV[1], tmp / 2);
+
+ if (dummy != (tmp / 2)) {
+ LOG_ERROR("Data must be encoded as hexadecimal pairs.");
+ free(buf);
+ return ERROR_COMMAND_ARGUMENT_INVALID;
+ }
+
+ length = tmp / 2;
+ ret = jaylink_emucom_write(devh, channel, buf, &length);
+
+ free(buf);
+
+ if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
+ LOG_ERROR("Channel not supported by the device.");
+ return ERROR_FAIL;
+ } else if (ret != JAYLINK_OK) {
+ LOG_ERROR("Failed to write to channel: %s.",
+ jaylink_strerror_name(ret));
+ return ERROR_FAIL;
+ }
+
+ if (length != (tmp / 2))
+ LOG_WARNING("Only %" PRIu32 " bytes written to the channel.", length);
+
+ return ERROR_OK;
+}
+
+COMMAND_HANDLER(jlink_handle_emucom_read_command)
+{
+ int ret;
+ uint32_t channel;
+ uint32_t length;
+ uint8_t *buf;
+ size_t tmp;
+
+ if (CMD_ARGC != 2)
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
+ if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
+ LOG_ERROR("Device does not support EMUCOM.");
+ return ERROR_FAIL;
+ }
+
+ COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
+ COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
+
+ buf = malloc(length * 3 + 1);
+
+ if (!buf) {
+ LOG_ERROR("Failed to allocate buffer.");
+ return ERROR_FAIL;
+ }
+
+ ret = jaylink_emucom_read(devh, channel, buf, &length);
+
+ if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
+ LOG_ERROR("Channel is not supported by the device.");
+ free(buf);
+ return ERROR_FAIL;
+ } else if (ret == JAYLINK_ERR_DEV_NOT_AVAILABLE) {
+ LOG_ERROR("Channel is not available for the requested amount of data. "
+ "%" PRIu32 " bytes are avilable.", length);
+ free(buf);
+ return ERROR_FAIL;
+ } else if (ret != JAYLINK_OK) {
+ LOG_ERROR("Failed to read from channel: %s.",
+ jaylink_strerror_name(ret));
+ free(buf);
+ return ERROR_FAIL;
+ }
+
+ tmp = hexify((char *)buf + length, buf, length, 2 * length + 1);
+
+ if (tmp != 2 * length) {
+ LOG_ERROR("Failed to convert data into hexadecimal string.");
+ free(buf);
+ return ERROR_FAIL;
+ }
+
+ command_print(CMD_CTX, "%s", buf + length);
+ free(buf);
+
+ return ERROR_OK;
+}
+
static const struct command_registration jlink_config_subcommand_handlers[] = {
{
.name = "usb",
COMMAND_REGISTRATION_DONE
};
+static const struct command_registration jlink_emucom_subcommand_handlers[] = {
+ {
+ .name = "write",
+ .handler = &jlink_handle_emucom_write_command,
+ .mode = COMMAND_EXEC,
+ .help = "write to a channel",
+ .usage = "<channel> <data>",
+ },
+ {
+ .name = "read",
+ .handler = &jlink_handle_emucom_read_command,
+ .mode = COMMAND_EXEC,
+ .help = "read from a channel",
+ .usage = "<channel> <length>"
+ },
+ COMMAND_REGISTRATION_DONE
+};
+
static const struct command_registration jlink_subcommand_handlers[] = {
{
.name = "jtag",
"this will show the device configuration",
.chain = jlink_config_subcommand_handlers,
},
+ {
+ .name = "emucom",
+ .mode = COMMAND_EXEC,
+ .help = "access EMUCOM channel",
+ .chain = jlink_emucom_subcommand_handlers
+ },
COMMAND_REGISTRATION_DONE
};