summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Schink <jaylink-dev@marcschink.de>2015-12-05 21:47:45 +0100
committerMarc Schink <jaylink-dev@marcschink.de>2015-12-05 22:06:49 +0100
commitcad8e23d4ddbfedf44483e48bbb282961e5571f5 (patch)
treeab2c81026a5b0b9cb3bd326a8744f736e5759f63
Add device_info.c
-rw-r--r--.gitignore1
-rw-r--r--device_info.c266
2 files changed, 267 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..fc58989
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+device_info
diff --git a/device_info.c b/device_info.c
new file mode 100644
index 0000000..6d38397
--- /dev/null
+++ b/device_info.c
@@ -0,0 +1,266 @@
+/*
+ * Copyright (C) 2015 Marc Schink <jaylink-dev@marcschink.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+#include <inttypes.h>
+#include <libjaylink/libjaylink.h>
+
+int main(int argc, char **argv)
+{
+ ssize_t ret;
+ struct jaylink_context *ctx;
+ struct jaylink_device **devs;
+ struct jaylink_device_handle *devh;
+ uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE];
+ struct jaylink_hardware_version hwver;
+ uint32_t interfaces;
+ uint32_t freq;
+ uint16_t div;
+ uint32_t div2;
+ uint8_t config[JAYLINK_DEV_CONFIG_SIZE];
+ bool use_serial_number;
+ uint32_t serial_number;
+ char *firmware_version;
+ uint32_t tmp;
+ size_t i;
+
+ if (argc > 2) {
+ printf("Usage: %s [serial number]\n", argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ use_serial_number = false;
+
+ if (argc > 1) {
+ if (sscanf(argv[1], "%" PRIu32, &serial_number) != 1) {
+ printf("Failed to parse serial number: %s.\n",
+ argv[1]);
+ return EXIT_FAILURE;
+ }
+
+ use_serial_number = true;
+ }
+
+ ret = jaylink_init(&ctx);
+
+ if (ret != JAYLINK_OK) {
+ printf("jaylink_init() failed: %s.\n",
+ jaylink_strerror_name(ret));
+ return EXIT_FAILURE;
+ }
+
+ ret = jaylink_get_device_list(ctx, &devs);
+
+ if (ret < 0) {
+ printf("jaylink_get_device_list() failed: %s.\n",
+ jaylink_strerror_name(ret));
+ jaylink_exit(ctx);
+ return EXIT_FAILURE;
+ }
+
+ for (i = 0; devs[i]; i++) {
+ devh = NULL;
+ ret = jaylink_device_get_serial_number(devs[i], &tmp);
+
+ if (ret != JAYLINK_OK) {
+ printf("jaylink_device_get_serial_number() failed: "
+ "%s.\n", jaylink_strerror_name(ret));
+ continue;
+ }
+
+ if (use_serial_number && serial_number != tmp)
+ continue;
+
+ ret = jaylink_open(devs[i], &devh);
+
+ if (ret == JAYLINK_OK) {
+ serial_number = tmp;
+ break;
+ }
+
+ printf("jaylink_open() failed: %s.\n",
+ jaylink_strerror_name(ret));
+ }
+
+ jaylink_free_device_list(devs, true);
+
+ if (!devh) {
+ printf("No J-Link device found.\n");
+ jaylink_exit(ctx);
+ return EXIT_SUCCESS;
+ }
+
+ printf("S/N: %012u\n", serial_number);
+
+ ret = jaylink_get_firmware_version(devh, &firmware_version);
+
+ if (ret > 0) {
+ printf("\nFirmware: %s\n", firmware_version);
+ free(firmware_version);
+ } else if (ret < 0) {
+ printf("jaylink_get_firmware_version() failed: %s.\n",
+ jaylink_strerror_name(ret));
+ jaylink_close(devh);
+ jaylink_exit(ctx);
+ return EXIT_FAILURE;
+ }
+
+ memset(caps, 0, JAYLINK_DEV_EXT_CAPS_SIZE);
+
+ ret = jaylink_get_caps(devh, caps);
+
+ if (ret != JAYLINK_OK) {
+ printf("jaylink_get_caps() failed: %s.\n",
+ jaylink_strerror_name(ret));
+ jaylink_close(devh);
+ jaylink_exit(ctx);
+ return EXIT_FAILURE;
+ }
+
+ if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
+ ret = jaylink_get_extended_caps(devh, caps);
+
+ if (ret != JAYLINK_OK) {
+ printf("jaylink_get_extended_caps() failed: %s.\n",
+ jaylink_strerror_name(ret));
+ jaylink_close(devh);
+ jaylink_exit(ctx);
+ return EXIT_FAILURE;
+ }
+ }
+
+ printf("\nDevice capabilities:\n");
+
+ for (i = 0; i < JAYLINK_DEV_EXT_CAPS_SIZE; i++) {
+ if (i > 0 && (i % 16) == 0)
+ printf("\n");
+
+ printf("%02x ", caps[i]);
+ }
+
+ printf("\n");
+
+ if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_HW_VERSION)) {
+ ret = jaylink_get_hardware_version(devh, &hwver);
+
+ if (ret != JAYLINK_OK) {
+ printf("jaylink_get_hardware_version() failed: %s.",
+ jaylink_strerror_name(ret));
+ jaylink_close(devh);
+ jaylink_exit(ctx);
+ return EXIT_FAILURE;
+ }
+
+ printf("\nHardware version: v%u.%02u (%u)\n", hwver.major,
+ hwver.minor, hwver.type);
+ }
+
+ if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SELECT_TIF)) {
+ ret = jaylink_get_available_interfaces(devh, &interfaces);
+
+ if (ret != JAYLINK_OK) {
+ printf("jaylink_get_available_interfaces() failed: "
+ "%s.\n", jaylink_strerror_name(ret));
+ jaylink_close(devh);
+ jaylink_exit(ctx);
+ return EXIT_FAILURE;
+ }
+
+ printf("\nTarget interfaces: %02x\n", interfaces);
+
+ if (interfaces & (1 << JAYLINK_TIF_JTAG))
+ printf(" - JTAG\n");
+
+ if (interfaces & (1 << JAYLINK_TIF_SWD))
+ printf(" - SWD\n");
+
+ if (interfaces & (1 << JAYLINK_TIF_BDM3))
+ printf(" - BDM3\n");
+
+ if (interfaces & (1 << JAYLINK_TIF_FINE))
+ printf(" - FINE\n");
+
+ if (interfaces & (1 << JAYLINK_TIF_2W_JTAG_PIC32))
+ printf(" - 2-wire JTAG for PIC32\n");
+ }
+
+ if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) {
+ ret = jaylink_get_speeds(devh, &freq, &div);
+
+ if (ret != JAYLINK_OK) {
+ printf("jaylink_get_speeds() failed: "
+ "%s.\n", jaylink_strerror_name(ret));
+ jaylink_close(devh);
+ jaylink_exit(ctx);
+ return EXIT_FAILURE;
+ }
+
+ printf("\nTarget interface speeds:\n");
+
+ if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING))
+ printf(" - Adaptive clocking\n");
+
+ printf(" - %u MHz / div; div >= %u\n", freq / 1000000, div);
+ }
+
+ if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SWO)) {
+ ret = jaylink_swo_get_speeds(devh, JAYLINK_SWO_MODE_UART,
+ &freq, &div2);
+
+ if (ret != JAYLINK_OK) {
+ printf("jaylink_swo_get_speeds() failed: "
+ "%s.\n", jaylink_strerror_name(ret));
+ jaylink_close(devh);
+ jaylink_exit(ctx);
+ return EXIT_FAILURE;
+ }
+
+ printf("\nSWO speeds: %u MHz / div; div >= %u\n",
+ freq / 1000000, div);
+ }
+
+ if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
+ ret = jaylink_read_raw_config(devh, config);
+
+ if (ret != JAYLINK_OK) {
+ printf("jaylink_read_raw_config() failed: %s.\n",
+ jaylink_strerror_name(ret));
+ jaylink_close(devh);
+ jaylink_exit(ctx);
+ return EXIT_FAILURE;
+ }
+
+ printf("\nDevice configuration:\n");
+
+ for (i = 0; i < JAYLINK_DEV_CONFIG_SIZE; i++) {
+ if (i > 0 && (i % 16) == 0)
+ printf("\n");
+
+ printf("%02x ", config[i]);
+ }
+
+ printf("\n");
+ }
+
+ jaylink_close(devh);
+ jaylink_exit(ctx);
+
+ return EXIT_SUCCESS;
+}