aboutsummaryrefslogtreecommitdiffstats
path: root/sw/osmo-rfds.c
blob: b62836e1ed5f2910bc7f58f60b75facd1433591a (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
/*
 * osmo-rfds-ctrl.c
 *
 * Control software for the Pluto RF delay simulator
 *
 * Copyright (C) 2018 sysmocom GmbH
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>

#include <iio.h>

/*
 	Some info about Pluto use of IIO :

	Device: ad9361-phy	=> Chip control
	------------------

	in_voltage0_[…]: targets RX1
	in_voltage1_[…]: targets RX2 (AD9361 in 2RX2TX mode only)

	out_voltage0_[…]: targets TX1
	out_voltage1_[…]: targets TX2 (AD9361 in 2RX2TX mode only)

	out_altvoltage0_[…]: targets RX LO
	out_altvoltage1_[…]: targets TX LO


	Device: cf-ad9361-lpc	=> RX Path
	---------------------

	voltage 0 -> RX1 I
	voltage 1 -> RX1 Q


	Device: cf-ad9361-dds-core-lpc	=> TX Path + two tone synthesizer
	------------------------------

	voltage 0 -> TX1 I
	voltage 1 -> TX1 Q
	altvoltage[] -> DDS stuff
*/


struct app_options
{
	long long tx_freq;	/* Hz */
	long long rx_freq;	/* Hz */
	float tx_gain;
	float rx_gain;

	long long samp_rate;	/* Hz */

	int buf_cnt;
	int buf_size;

	int   echo_delay;
	float echo_scale;
};

struct app_state
{
	/* Options */
	struct app_options opts;

	/* Pluto device */
	struct {
		struct iio_context *ctx;
		struct iio_device  *phy;

		struct iio_device  *rx;
		struct iio_channel *rx_i;
		struct iio_channel *rx_q;
		struct iio_buffer  *rx_buf;

		struct iio_device  *tx;
		struct iio_channel *tx_i;
		struct iio_channel *tx_q;
		struct iio_buffer  *tx_buf;
	} pluto;
};


static void app_pluto_close(struct app_state *app);

static int
app_pluto_open(struct app_state *app)
{
	/* NULL init */
	memset(&app->pluto, 0x00, sizeof(app->pluto));

	/* Context */
	app->pluto.ctx = iio_create_context_from_uri("local:");
	if (!app->pluto.ctx) {
		fprintf(stderr, "[!] Failed to create IIO context\n");
		goto err;
	}

	/* Devices */
	app->pluto.phy = iio_context_find_device(app->pluto.ctx, "ad9361-phy");
	if (!app->pluto.phy) {
		fprintf(stderr, "[!] Failed to find AD9361 PHY device\n");
		goto err;
	}

	app->pluto.rx = iio_context_find_device(app->pluto.ctx, "cf-ad9361-lpc");
	if (!app->pluto.rx) {
		fprintf(stderr, "[!] Failed to find AD9361 RX device\n");
		goto err;
	}

	app->pluto.tx = iio_context_find_device(app->pluto.ctx, "cf-ad9361-dds-core-lpc");
	if (!app->pluto.tx) {
		fprintf(stderr, "[!] Failed to find AD9361 TX device\n");
		goto err;
	}

	/* Configure RX */
		/* Frequency / Gain / Sampling */
	iio_channel_attr_write_longlong(
		iio_device_find_channel(app->pluto.phy, "altvoltage0", true),
		"frequency",		/* RX LO frequency */
		app->opts.rx_freq
	);

	iio_channel_attr_write_double(
		iio_device_find_channel(app->pluto.phy, "voltage0", false),
		"hardwaregain",		/* RX gain */
		(double)app->opts.rx_gain
	);

	iio_channel_attr_write_longlong(
		iio_device_find_channel(app->pluto.phy, "voltage0", false),
		"sampling_frequency",	/* RX baseband rate */
		app->opts.samp_rate
	);

		/* Channel config */
	app->pluto.rx_i = iio_device_find_channel(app->pluto.rx, "voltage0", false);
	app->pluto.rx_q = iio_device_find_channel(app->pluto.rx, "voltage1", false);

	iio_channel_enable(app->pluto.rx_i);
	iio_channel_enable(app->pluto.rx_q);

	/* Configure TX */
		/* Frequency / Gain / Sampling */
	iio_channel_attr_write_longlong(
		iio_device_find_channel(app->pluto.phy, "altvoltage1", true),
		"frequency",		/* TX LO frequency */
		app->opts.tx_freq
	);

	iio_channel_attr_write_double(
		iio_device_find_channel(app->pluto.phy, "voltage0", true),
		"hardwaregain",		/* TX gain */
		(double)app->opts.tx_gain
	);

	iio_channel_attr_write_longlong(
		iio_device_find_channel(app->pluto.phy, "voltage0", true),
		"sampling_frequency",	/* TX baseband rate */
		app->opts.samp_rate
	);

		/* Channel config */
	app->pluto.tx_i = iio_device_find_channel(app->pluto.tx, "voltage0", true);
	app->pluto.tx_q = iio_device_find_channel(app->pluto.tx, "voltage1", true);

	iio_channel_enable(app->pluto.tx_i);
	iio_channel_enable(app->pluto.tx_q);

	/* Configure the ECHO path */
	uint16_t scale = (uint16_t)(0x4000 * app->opts.echo_scale);
	uint16_t delay = app->opts.echo_delay;
	uint32_t config = (((uint32_t)delay) << 16) | (uint32_t)scale;

	iio_device_reg_write(app->pluto.tx, 0x8000043c, config);	/* I combiner config */
	iio_device_reg_write(app->pluto.tx, 0x8000047c, config);	/* Q combiner config */

	return 0;

err:
	app_pluto_close(app);
	return -1;
}

static void
app_pluto_close(struct app_state *app)
{
	if (app->pluto.rx_i)
		iio_channel_disable(app->pluto.rx_i);

	if (app->pluto.rx_q)
		iio_channel_disable(app->pluto.rx_q);

	if (app->pluto.tx_i)
		iio_channel_disable(app->pluto.tx_i);

	if (app->pluto.tx_q)
		iio_channel_disable(app->pluto.tx_q);

	if (app->pluto.ctx)
		iio_context_destroy(app->pluto.ctx);

	memset(&app->pluto, 0x00, sizeof(app->pluto));
}

static int
app_pluto_start(struct app_state *app)
{
	/* Create buffers and start the streaming */
	iio_device_set_kernel_buffers_count(app->pluto.rx, app->opts.buf_cnt);
	app->pluto.rx_buf = iio_device_create_buffer(app->pluto.rx, app->opts.buf_size, false);
	if (!app->pluto.rx_buf) {
		fprintf(stderr, "[!] Could not create RX buffer");
		return -1;
	}

	iio_device_set_kernel_buffers_count(app->pluto.tx, app->opts.buf_cnt);
	app->pluto.tx_buf = iio_device_create_buffer(app->pluto.tx, app->opts.buf_size, false);
	if (!app->pluto.tx_buf) {
		fprintf(stderr, "[!] Could not create TX buffer");
		return -1;
	}

	/* Echo start */
	iio_device_reg_write(app->pluto.tx, 0x80000418, 0xa);		/* I mux to RF loopback */
	iio_device_reg_write(app->pluto.tx, 0x80000458, 0xa);		/* Q mux to RF loopback */

	return 0;
}

static void
app_pluto_stop(struct app_state *app)
{
	if (app->pluto.rx_buf)
		iio_buffer_destroy(app->pluto.rx_buf);

	if (app->pluto.tx_buf)
		iio_buffer_destroy(app->pluto.tx_buf);

	/* Force zero */
	iio_device_reg_write(app->pluto.tx, 0x80000418, 0);		/* I mux to zero */
	iio_device_reg_write(app->pluto.tx, 0x80000458, 0);		/* Q mux to zero */
}


/* ------------------------------------------------------------------------ */
/* Options                                                                  */
/* ------------------------------------------------------------------------ */

static void
opts_defaults(struct app_options *opts)
{
	memset(opts, 0x00, sizeof(struct app_options));

	opts->tx_freq = 1000000000LL;
	opts->rx_freq = 1000000000LL;
	opts->tx_gain = -40.0f;
	opts->rx_gain =  40.0f ;

	opts->samp_rate = 4000000LL;

	opts->buf_cnt = 4;
	opts->buf_size = 4096;

	opts->echo_scale = 0.25f;
	opts->echo_delay = 50;
}

static void
opts_help(const char *argv0)
{
	fprintf(stderr, "%s [options]\n", argv0);

	fprintf(stderr, " -t, --tx-freq      \n");
	fprintf(stderr, " -r, --rx-freq      \n");
	fprintf(stderr, " -T, --tx-gain      \n");
	fprintf(stderr, " -R, --rx-gain      \n");
	fprintf(stderr, " -s, --samplerate   \n");
	fprintf(stderr, " -c, --buffer-count \n");
	fprintf(stderr, " -b, --buffer-size  \n");
	fprintf(stderr, " -a, --amplitude    \n");
	fprintf(stderr, " -d, --delay        \n");
	fprintf(stderr, " -h, --help         \n");
}

static int
opts_parse(struct app_options *opts, int argc, char *argv[])
{
	const struct option long_options[] =
	{
		{ "tx-freq",      required_argument, 0, 't' },
		{ "rx-freq",      required_argument, 0, 'r' },
		{ "tx-gain",      required_argument, 0, 'T' },
		{ "rx-gain",      required_argument, 0, 'R' },
		{ "samplerate",   required_argument, 0, 's' },
		{ "buffer-count", required_argument, 0, 'c' },
		{ "buffer-size",  required_argument, 0, 'b' },
		{ "amplitude",    required_argument, 0, 'a' },
		{ "delay",        required_argument, 0, 'd' },
		{ "help",         no_argument,       0, 'h' },
		{0, 0, 0, 0}
	};
	const char *short_options = "t:r:T:R:s:c:b:a:d:h";

	while (1) {
		int optidx;
		int c = getopt_long (argc, argv, short_options, long_options, &optidx);

		if (c == -1)
			break;

		switch (c) {
		case 't':
			opts->tx_freq = strtoll(optarg, NULL, 10);
			break;

		case 'r':
			opts->rx_freq = strtoll(optarg, NULL, 10);
			break;

		case 'T':
			opts->tx_gain = strtof(optarg, NULL);
			break;

		case 'R':
			opts->rx_gain = strtof(optarg, NULL);
			break;

		case 's':
			opts->samp_rate = strtoll(optarg, NULL, 10);
			break;

		case 'c':
			opts->buf_cnt = strtol(optarg, NULL, 10);
			break;

		case 'b':
			opts->buf_size = strtol(optarg, NULL, 10);
			break;

		case 'a':
			opts->echo_scale = strtof(optarg, NULL);
			break;

		case 'd':
			opts->echo_delay = strtol(optarg, NULL, 10);
			break;

		case 'h':
			opts_help(argv[0]);
			return 1;

		default:
			fprintf(stderr, "Unknown option\n");
			return -1;
		}
	}

	return 0;
}

static void
opts_print(struct app_options *opts, FILE *fd)
{
	fprintf(fd, "[+] Options :\n");

	fprintf(fd, "  . TX frequency   : %.3lf MHz\n", (float)opts->tx_freq / 1e6);
	fprintf(fd, "  . TX gain        : %.1f dB\n",  opts->tx_gain);
	fprintf(fd, "  . RX frequency   : %.3lf MHz\n", (float)opts->rx_freq / 1e6);
	fprintf(fd, "  . RX gain        : %.1f dB\n",  opts->rx_gain);
	fprintf(fd, "\n");

	fprintf(fd, "  . Sample Rate    : %.3lf Msps\n", (float)opts->samp_rate / 1e6);
	fprintf(fd, "\n");

	fprintf(fd, "  . Buffer count   : %d\n", opts->buf_cnt);
	fprintf(fd, "  . Buffer size    : %d bytes\n", opts->buf_size);
	fprintf(fd, "\n");

	fprintf(fd, "  . Echo amplitude : %.1f\n", opts->echo_scale);
	fprintf(fd, "  . Echo delay     : %d samples\n", opts->echo_delay);
	fprintf(fd, "\n");
}


/* ------------------------------------------------------------------------ */
/* Main                                                                     */
/* ------------------------------------------------------------------------ */

int main(int argc, char *argv[])
{
	struct app_state _app, *app=&_app;
	int rv;

	/* Options */
	memset(app, 0x00, sizeof(struct app_state));
	opts_defaults(&app->opts);
	rv = opts_parse(&app->opts, argc, argv);
	if (rv)
		return rv < 0 ? rv : 0;
	opts_print(&app->opts, stderr);

	/* Open and configure pluto */
	rv = app_pluto_open(app);
	if (rv)
		goto err;

	/* Start streaming */
	app_pluto_start(app);

	/* Dummy loop */
	while (1)
	{
		memset(iio_buffer_start(app->pluto.tx_buf), 0x00, app->opts.buf_size);
		iio_buffer_push(app->pluto.tx_buf);
		iio_buffer_refill(app->pluto.rx_buf);
	}

err:
	/* Stop streaming */
	app_pluto_stop(app);

	/* Shutdown */
	app_pluto_close(app);

	return 0;
}