aboutsummaryrefslogtreecommitdiff
path: root/libjaylink/swo.c
blob: 6037f64b8f981a6f0d7e17fca4bed89381de6fed (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * This file is part of the libjaylink project.
 *
 * 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 2 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 <stdint.h>
#include <stdbool.h>

#include "libjaylink.h"
#include "libjaylink-internal.h"

/**
 * @file
 *
 * Serial Wire Output (SWO) functions.
 */

/** @cond PRIVATE */
#define CMD_SWO			0xeb

#define SWO_CMD_START		0x64
#define SWO_CMD_STOP		0x65
#define SWO_CMD_READ		0x66
#define SWO_CMD_GET_SPEEDS	0x6e

#define SWO_PARAM_MODE		0x01
#define SWO_PARAM_BAUDRATE	0x02
#define SWO_PARAM_READ_SIZE	0x03
#define SWO_PARAM_BUFFER_SIZE	0x04

#define SWO_ERR			0x80000000
/** @endcond */

/**
 * Start SWO capture.
 *
 * @note This function must be used only if the device has the
 *       #JAYLINK_DEV_CAP_SWO capability.
 *
 * @param[in,out] devh Device handle.
 * @param[in] mode Mode to capture data with.
 * @param[in] baudrate Baudrate to capture data in bit per second.
 * @param[in] size Device internal buffer size in bytes to use for capturing.
 *
 * @retval JAYLINK_OK Success.
 * @retval JAYLINK_ERR_ARG Invalid arguments.
 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
 * @retval JAYLINK_ERR_IO Input/output error.
 * @retval JAYLINK_ERR_DEV Unspecified device error.
 * @retval JAYLINK_ERR Other error conditions.
 *
 * @see jaylink_swo_get_speeds()
 * @see jaylink_get_free_memory()
 *
 * @since 0.1.0
 */
JAYLINK_API int jaylink_swo_start(struct jaylink_device_handle *devh,
		enum jaylink_swo_mode mode, uint32_t baudrate, uint32_t size)
{
	int ret;
	struct jaylink_context *ctx;
	uint8_t buf[32];
	uint32_t status;

	if (!devh || !baudrate || !size)
		return JAYLINK_ERR_ARG;

	if (mode != JAYLINK_SWO_MODE_UART)
		return JAYLINK_ERR_ARG;

	ctx = devh->dev->ctx;
	ret = transport_start_write_read(devh, 21, 4, true);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_start_write_read() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	buf[0] = CMD_SWO;
	buf[1] = SWO_CMD_START;

	buf[2] = 0x04;
	buf[3] = SWO_PARAM_MODE;
	buffer_set_u32(buf, mode, 4);

	buf[8] = 0x04;
	buf[9] = SWO_PARAM_BAUDRATE;
	buffer_set_u32(buf, baudrate, 10);

	buf[14] = 0x04;
	buf[15] = SWO_PARAM_BUFFER_SIZE;
	buffer_set_u32(buf, size, 16);

	buf[20] = 0x00;

	ret = transport_write(devh, buf, 21);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_write() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	ret = transport_read(devh, buf, 4);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_read() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	status = buffer_get_u32(buf, 0);

	if (status > 0) {
		log_err(ctx, "Failed to start capture: 0x%x.", status);
		return JAYLINK_ERR_DEV;
	}

	return JAYLINK_OK;
}

/**
 * Stop SWO capture.
 *
 * @note This function must be used only if the device has the
 *       #JAYLINK_DEV_CAP_SWO capability.
 *
 * @param[in,out] devh Device handle.
 *
 * @retval JAYLINK_OK Success.
 * @retval JAYLINK_ERR_ARG Invalid arguments.
 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
 * @retval JAYLINK_ERR_IO Input/output error.
 * @retval JAYLINK_ERR_DEV Unspecified device error.
 * @retval JAYLINK_ERR Other error conditions.
 *
 * @see jaylink_swo_start()
 *
 * @since 0.1.0
 */
JAYLINK_API int jaylink_swo_stop(struct jaylink_device_handle *devh)
{
	int ret;
	struct jaylink_context *ctx;
	uint8_t buf[4];
	uint32_t status;

	if (!devh)
		return JAYLINK_ERR_ARG;

	ctx = devh->dev->ctx;
	ret = transport_start_write_read(devh, 3, 4, true);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_start_write_read() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	buf[0] = CMD_SWO;
	buf[1] = SWO_CMD_STOP;
	buf[2] = 0x00;

	ret = transport_write(devh, buf, 3);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_write() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	ret = transport_read(devh, buf, 4);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_read() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	status = buffer_get_u32(buf, 0);

	if (status > 0) {
		log_err(ctx, "Failed to stop capture: 0x%x.", status);
		return JAYLINK_ERR_DEV;
	}

	return JAYLINK_OK;
}

/**
 * Read SWO trace data.
 *
 * @note This function must be used only if the device has the
 *       #JAYLINK_DEV_CAP_SWO capability.
 *
 * @param[in,out] devh Device handle.
 * @param[out] buffer Buffer to store trace data on success. Its content is
 *                    undefined on failure.
 * @param[in,out] length Maximum number of bytes to read. On success, the value
 *                       gets updated with the actual number of bytes read. The
 *                       value is undefined on failure.
 *
 * @retval JAYLINK_OK Success.
 * @retval JAYLINK_ERR_ARG Invalid arguments.
 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
 * @retval JAYLINK_ERR_PROTO Protocol violation.
 * @retval JAYLINK_ERR_IO Input/output error.
 * @retval JAYLINK_ERR_DEV Unspecified device error.
 * @retval JAYLINK_ERR Other error conditions.
 *
 * @see jaylink_swo_start()
 *
 * @since 0.1.0
 */
JAYLINK_API int jaylink_swo_read(struct jaylink_device_handle *devh,
		uint8_t *buffer, uint32_t *length)
{
	int ret;
	struct jaylink_context *ctx;
	uint8_t buf[32];
	uint32_t status;
	uint32_t tmp;

	if (!devh || !buffer || !length)
		return JAYLINK_ERR_ARG;

	ctx = devh->dev->ctx;
	ret = transport_start_write_read(devh, 9, 8, true);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_start_write_read() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	buf[0] = CMD_SWO;
	buf[1] = SWO_CMD_READ;

	buf[2] = 0x04;
	buf[3] = SWO_PARAM_READ_SIZE;
	buffer_set_u32(buf, *length, 4);

	buf[8] = 0x00;

	ret = transport_write(devh, buf, 9);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_write() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	ret = transport_read(devh, buf, 8);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_read() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	status = buffer_get_u32(buf, 0);
	tmp = buffer_get_u32(buf, 4);

	if (tmp > *length) {
		log_err(ctx, "Received %u bytes but only %u bytes were "
			"requested.", tmp, *length);
		return JAYLINK_ERR_PROTO;
	}

	*length = tmp;

	if (tmp > 0) {
		ret = transport_start_read(devh, tmp);

		if (ret != JAYLINK_OK) {
			log_err(ctx, "transport_start_read() failed: %s.",
				jaylink_strerror(ret));
			return ret;
		}

		ret = transport_read(devh, buffer, tmp);

		if (ret != JAYLINK_OK) {
			log_err(ctx, "transport_read() failed: %s.",
				jaylink_strerror(ret));
			return ret;
		}
	}

	if (status > 0) {
		log_err(ctx, "Failed to read data: 0x%x.", status);
		return JAYLINK_ERR_DEV;
	}

	return JAYLINK_OK;
}

/**
 * Retrieve SWO speeds.

 * The speeds are calculated as follows:
 *
 * @par
 * <tt>speeds = @a freq / n</tt> with <tt>n >= @a min_div</tt> and
 * <tt>n <= @a max_div</tt>, where @p n is an integer
 *
 * Assuming, for example, a base frequency @a freq of 4500 kHz, a minimum
 * divider @a min_div of 1 and a maximum divider @a max_div of 8 then the
 * highest possible SWO speed is 4500 kHz / 1 = 4500 kHz. The next highest
 * speed is 2250 kHz for a divider of 2, and so on. Accordingly, the lowest
 * possible speed is 4500 kHz / 8 = 562.5 kHz.
 *
 * @note This function must be used only if the device has the
 *       #JAYLINK_DEV_CAP_SWO capability.
 *
 * @param[in,out] devh Device handle.
 * @param[in] mode Capture mode to retrieve speeds for.
 * @param[out] speed Speed information on success, and undefined on failure.
 *
 * @retval JAYLINK_OK Success.
 * @retval JAYLINK_ERR_ARG Invalid arguments.
 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
 * @retval JAYLINK_ERR_PROTO Protocol violation.
 * @retval JAYLINK_ERR_IO Input/output error.
 * @retval JAYLINK_ERR_DEV Unspecified device error.
 * @retval JAYLINK_ERR Other error conditions.
 *
 * @since 0.1.0
 */
JAYLINK_API int jaylink_swo_get_speeds(struct jaylink_device_handle *devh,
		enum jaylink_swo_mode mode, struct jaylink_swo_speed *speed)
{
	int ret;
	struct jaylink_context *ctx;
	uint8_t buf[24];
	uint32_t tmp;
	uint32_t length;

	if (!devh || !speed)
		return JAYLINK_ERR_ARG;

	if (mode != JAYLINK_SWO_MODE_UART)
		return JAYLINK_ERR_ARG;

	ctx = devh->dev->ctx;
	ret = transport_start_write_read(devh, 9, 4, true);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_start_write_read() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	buf[0] = CMD_SWO;
	buf[1] = SWO_CMD_GET_SPEEDS;

	buf[2] = 0x04;
	buf[3] = SWO_PARAM_MODE;
	buffer_set_u32(buf, mode, 4);

	buf[8] = 0x00;

	ret = transport_write(devh, buf, 9);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_write() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	ret = transport_read(devh, buf, 4);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_read() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	tmp = buffer_get_u32(buf, 0);

	if (tmp & SWO_ERR) {
		log_err(ctx, "Failed to retrieve speed information: 0x%x.",
			tmp);
		return JAYLINK_ERR_DEV;
	}

	length = tmp;

	if (length != 28) {
		log_err(ctx, "Unexpected number of bytes received: %u.",
			length);
		return JAYLINK_ERR_PROTO;
	}

	length = length - 4;
	ret = transport_start_read(devh, length);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_start_read() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	ret = transport_read(devh, buf, length);

	if (ret != JAYLINK_OK) {
		log_err(ctx, "transport_read() failed: %s.",
			jaylink_strerror(ret));
		return ret;
	}

	speed->freq = buffer_get_u32(buf, 4);
	speed->min_div = buffer_get_u32(buf, 8);

	if (!speed->min_div) {
		log_err(ctx, "Minimum frequency divider is zero.");
		return JAYLINK_ERR_PROTO;
	}

	speed->max_div = buffer_get_u32(buf, 12);

	if (speed->max_div < speed->min_div) {
		log_err(ctx, "Maximum frequency divider is less than minimum "
			"frequency divider.");
		return JAYLINK_ERR_PROTO;
	}

	speed->min_prescaler = buffer_get_u32(buf, 16);
	speed->max_prescaler = buffer_get_u32(buf, 20);

	if (speed->max_prescaler < speed->min_prescaler) {
		log_err(ctx, "Maximum prescaler is less than minimum "
			"prescaler.");
		return JAYLINK_ERR_PROTO;
	}

	return JAYLINK_OK;
}