summaryrefslogtreecommitdiff
path: root/device_info.c
blob: 3385d6e09fc5d9ee81f2b1119cad1f2847de13fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * 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>

#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))

static const struct {
	enum jaylink_target_interface iface;
	const char *name;
} target_ifaces[] = {
	{
		.iface = JAYLINK_TIF_JTAG,
		.name = "JTAG"
	},
	{
		.iface = JAYLINK_TIF_SWD,
		.name = "SWD"
	},
	{
		.iface = JAYLINK_TIF_BDM3,
		.name = "BDM3"
	},
	{
		.iface = JAYLINK_TIF_FINE,
		.name = "FINE"
	},
	{
		.iface = JAYLINK_TIF_2W_JTAG_PIC32,
		.name = "2-wire JTAG for PIC32"
	}
};

static void print_interfaces(struct jaylink_device_handle *devh,
		uint32_t interfaces)
{
	size_t i;

	printf("\nTarget interfaces: %02x\n", interfaces);

	for (i = 0; i < ARRAY_SIZE(target_ifaces); i++) {
		if (interfaces & (1 << target_ifaces[i].iface))
			printf(" - %s\n", target_ifaces[i].name);
	}
}

static bool print_interface_speeds(struct jaylink_device_handle *devh,
		uint32_t interfaces)
{
	int ret;
	size_t i;
	struct jaylink_speed speed;

	for (i = 0; i < ARRAY_SIZE(target_ifaces); i++) {
		if (!(interfaces & (1 << target_ifaces[i].iface)))
			continue;

		ret = jaylink_select_interface(devh, target_ifaces[i].iface,
			NULL);

		if (ret != JAYLINK_OK) {
			printf("jaylink_select_interface() failed: %s.\n",
				jaylink_strerror_name(ret));
			return false;
		}

		ret = jaylink_get_speeds(devh, &speed);

		if (ret != JAYLINK_OK) {
			printf("jaylink_get_speeds() failed: %s.\n",
				jaylink_strerror_name(ret));
			return false;
		}

		printf(" - %s: %" PRIu32 " MHz / div; div >= %u\n",
			target_ifaces[i].name, speed.freq / 1000000,
			speed.div);
	}

	return true;
}

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;
	struct jaylink_swo_speed swo_speed;
	uint8_t config[JAYLINK_DEV_CONFIG_SIZE];
	bool use_serial_number;
	uint32_t serial_number;
	char *firmware_version;
	size_t length;
	bool device_found;
	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) {
		ret = jaylink_parse_serial_number(argv[1], &serial_number);

		if (ret != JAYLINK_OK) {
			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_discovery_scan(ctx, 0);

	if (ret != JAYLINK_OK) {
		printf("jaylink_discovery_scan() failed: %s.\n",
			jaylink_strerror_name(ret));
		jaylink_exit(ctx);
		return EXIT_FAILURE;
	}

	ret = jaylink_get_devices(ctx, &devs, NULL);

	if (ret != JAYLINK_OK) {
		printf("jaylink_get_device_list() failed: %s.\n",
			jaylink_strerror_name(ret));
		jaylink_exit(ctx);
		return EXIT_FAILURE;
	}

	device_found = false;

	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;
			device_found = true;
			break;
		}

		printf("jaylink_open() failed: %s.\n",
			jaylink_strerror_name(ret));
	}

	jaylink_free_devices(devs, true);

	if (!device_found) {
		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, &length);

	if (ret != JAYLINK_OK) {
                printf("jaylink_get_firmware_version() failed: %s.\n",
                        jaylink_strerror_name(ret));
                jaylink_close(devh);
                jaylink_exit(ctx);
                return EXIT_FAILURE;
	} else if (length > 0) {
		printf("\nFirmware: %s\n", firmware_version);
		free(firmware_version);
	}

	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));
			return false;
		}
	} else {
		interfaces = (1 << JAYLINK_TIF_JTAG);
	}

	print_interfaces(devh, interfaces);

	if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) {
		printf("\nTarget interface speeds:\n");

		if (!print_interface_speeds(devh, interfaces)) {
			jaylink_close(devh);
			jaylink_exit(ctx);
			return EXIT_FAILURE;
		}

		if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING))
			printf(" - Adaptive clocking\n");
	}

	if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SWO)) {
		ret = jaylink_swo_get_speeds(devh, JAYLINK_SWO_MODE_UART,
			&swo_speed);

		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 and div <= %u\n",
			swo_speed.freq / 1000000, swo_speed.min_div,
			swo_speed.max_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;
}