-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtestPlutoSDR.c
More file actions
342 lines (296 loc) · 9.62 KB
/
Copy pathtestPlutoSDR.c
File metadata and controls
342 lines (296 loc) · 9.62 KB
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
#include <iio.h>
#include <stdio.h>
#include <getopt.h> // for getopt_long support
#include <string.h> // for strcmp
static char* uri; // usb:2.2.5
static char* devicename; // ad9361-phy
static long long frequency;
static long long bandwidth;
static long long samplerate;
static char* receivefile;
static char* transmitfile;
static int printinfo;
static int verbose;
// Cmd line help message
void displayCmdUsage() {
puts("Usage: ./testPlutoSDR [URI] [OPTIONS] \n\
-u --uri Specify device URI connection info (e.g. usb:x.x.x, ip:xxx.xxx.xxx.xxx, serial:) \n\
-d --device Specify the device name (e.g. ad9361-phy) \n\
-p --print-info Print attributes and channel info for specified device, or all devices if none specified \n\
-v --verbose Prints additional output \n\
--help Display this message \n\
\n\
e.x. ./testPlutoSDR -u usb:2.2.5 -d ad9361-phy -p \n");
exit(1);
}
// Boilerplate getopt code to read cmd line arguments
void parseCmdArgs(int argc, char **argv) {
int c;
char *ptr;
while (1) {
int option_index = 0;
static struct option long_options[] = {
{"uri", required_argument, 0, 'u'},
{"device", required_argument, 0, 'd'},
{"frequency", required_argument, 0, 'f'},
{"bandwidth", required_argument, 0, 'b'},
{"sample-rate", required_argument, 0, 's'},
{"receive", required_argument, 0, 'r'},
{"transmit", required_argument, 0, 't'},
{"print-info", no_argument, &printinfo, 'p'},
{"verbose", no_argument, &verbose, 'v'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0},
};
c = getopt_long_only(argc, argv, "u:d:f:b:s:r:t:pvh", long_options, &option_index);
if (c == -1) { break; }
switch (c) {
case 0:
/* If this option set a flag, do nothing else now. */
if (long_options[option_index].flag != 0) { break; }
printf ("option %s", long_options[option_index].name);
if (optarg) { printf (" with arg %s", optarg); }
printf ("\n");
break;
case 'u':
uri = optarg;
break;
case 'd':
devicename = optarg;
break;
case 'f':
frequency = strtoul(optarg, &ptr, 10);
if (strcmp(ptr,"")) {
printf("Value %s of option %s is not a number \n", ptr, long_options[option_index].name);
exit(1);
}
break;
case 'b':
bandwidth = strtoul(optarg, &ptr, 10);
if (strcmp(ptr,"")) {
printf("Value %s of option %s is not a number \n", ptr, long_options[option_index].name);
exit(1);
}
break;
case 's':
samplerate = strtoul(optarg, &ptr, 10);
if (strcmp(ptr,"")) {
printf("Value %s of option %s is not a number \n", ptr, long_options[option_index].name);
exit(1);
}
break;
case 'r':
receivefile = optarg;
break;
case 't':
transmitfile = optarg;
break;
case 'p':
printinfo = 1;
break;
case 'v':
verbose = 1;
break;
default:
displayCmdUsage();
}
}
if (optind < argc) {
printf ("Unrecognized options: ");
while (optind < argc) {
printf ("%s ", argv[optind++]);
}
printf("\n"); //putchar ('\n');
displayCmdUsage();
}
return;
}
int receive(struct iio_context *ctx, char* file)
{
struct iio_device *dev;
struct iio_channel *rx0_i, *rx0_q;
struct iio_buffer *rxbuf;
FILE *fd;
fd = fopen(file, "w");
// tag::code[]
dev = iio_context_find_device(ctx, "cf-ad9361-lpc");
// end::code[]
rx0_i = iio_device_find_channel(dev, "voltage0", 0);
rx0_q = iio_device_find_channel(dev, "voltage1", 0);
iio_channel_enable(rx0_i);
iio_channel_enable(rx0_q);
rxbuf = iio_device_create_buffer(dev, 4096, false);
if (!rxbuf) {
perror("Could not create RX buffer");
exit(-1);
}
while (true) {
void *p_dat, *p_end, *t_dat;
ptrdiff_t p_inc;
iio_buffer_refill(rxbuf);
p_inc = iio_buffer_step(rxbuf);
p_end = iio_buffer_end(rxbuf);
for (p_dat = iio_buffer_first(rxbuf, rx0_i); p_dat < p_end; p_dat += p_inc, t_dat += p_inc) {
const int16_t i = ((int16_t*)p_dat)[0]; // Real (I)
const int16_t q = ((int16_t*)p_dat)[1]; // Imag (Q)
/* Process here */
fprintf(fd, "%i,%i,", i,q);
fflush(fd);
}
}
iio_buffer_destroy(rxbuf);
}
// Get all channels for this device
void printDeviceChannels(struct iio_device* dev) {
int num_channels = iio_device_get_channels_count(dev);
int j = 0;
for (j=0; j<num_channels; j++) {
struct iio_channel* chn;
chn = iio_device_get_channel(dev, j);
int num_attrs = iio_channel_get_attrs_count(chn);
printf("\n Channel %i, Attributes %i\n", j, num_attrs);
const char* id = iio_channel_get_id(chn);
const char* name = iio_channel_get_name(chn);
bool isoutput = iio_channel_is_output(chn);
bool isscanel = iio_channel_is_scan_element(chn);
printf(" %-30s: %s\n", "id", id);
printf(" %-30s: %s\n", "name", name);
if (isoutput) { printf(" %-30s: %s\n", "I/O", "output"); }
else { printf(" %-30s: %s\n", "I/O", "input"); }
printf(" %-30s: %i\n", "scan_element", isscanel);
// Get all attributes for this channel
int k = 0;
for (k=0; k<num_attrs; k++) {
const char* attr = iio_channel_get_attr(chn, k);
char buf[2048];
int ret = iio_channel_attr_read(chn, attr, buf, sizeof(buf));
if (ret > 0) {
printf(" %-30s: %s\n", attr, buf);
} else {
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "[ERROR] Unable to read attribute %s: %s\n", attr, buf);
}
}
}
}
// Get all attributes for this device
void printDeviceAttributes(struct iio_device* dev) {
int num_attrs = iio_device_get_attrs_count(dev);
int j = 0;
for (j=0; j<num_attrs; j++) {
const char* attr = iio_device_get_attr(dev, j);
char buf[2048];
int ret = iio_device_attr_read(dev, attr, buf, sizeof(buf));
if (ret > 0) {
printf(" %-30s: %s\n", attr, buf);
} else {
iio_strerror(-ret, buf, sizeof(buf));
fprintf(stderr, "[ERROR] Unable to read attribute %s: %s\n", attr, buf);
}
}
}
// Get 1 devices info
void printDeviceInfo(struct iio_device* dev) {
const char* id = iio_device_get_id(dev);
const char* name = iio_device_get_name(dev);
printf(" %-30s: %s\n", "id", id);
printf(" %-30s: %s\n", "name", name);
printDeviceAttributes(dev);
printDeviceChannels(dev);
}
// Get info for all devices in a given context
void printDevicesInfo(struct iio_context* ctx) {
int num_devs = iio_context_get_devices_count(ctx);
printf("\nDevices: %i\n",num_devs);
int i = 0;
for (i=0; i<num_devs; i++) {
struct iio_device* dev = iio_context_get_device(ctx, i);
const char* id = iio_device_get_id(dev);
const char* name = iio_device_get_name(dev);
printf("\n Device %i\n", i);
printf(" %-30s: %s\n", "id", id);
printf(" %-30s: %s\n", "name", name);
printDeviceAttributes(dev);
printDeviceChannels(dev);
}
}
// Helper to print errors and old/new values of attributes
int setAttribute(struct iio_channel* chn, char* attr, long long value) {
// read current value
long long val;
int ret = iio_channel_attr_read_longlong(chn, attr, &val);
if (ret != 0) { printf("[ERROR] Failed to read %s, %i\n", attr, ret); }
else { printf("Current %-30s : %llu\n", attr, val); }
// set new value
ret = iio_channel_attr_write_longlong(chn, attr, value); /* RX baseband rate 5 MSPS */
if (ret != 0) { printf("[ERROR] Failed to write %s, %i\n", attr, ret); }
else {
ret = iio_channel_attr_read_longlong(chn, attr, &val);
if (ret != 0) { printf("[ERROR] Failed to read %s, %i\n", attr, ret); }
else { printf("New %-34s : %llu\n", attr, val); }
}
}
int main (int argc, char **argv)
{
//if (argc == 1) { displayCmdUsage(); }
parseCmdArgs(argc, argv);
printf("#### Starting PlutoSDR Test ####\n");
if (!uri) {
struct iio_scan_context* scan_ctx = iio_create_scan_context(NULL, 0);
if (!scan_ctx) {
fprintf(stderr, "Unable to create scan context\n");
return;
}
struct iio_context_info **info;
ssize_t ret = iio_scan_context_get_info_list(scan_ctx, &info);
if (ret < 0) {
fprintf(stderr, "Unable to scan: %li\n", (long) ret);
iio_scan_context_destroy(scan_ctx);
}
if (ret == 0) {
printf("No contexts found.\n");
iio_context_info_list_free(info);
iio_scan_context_destroy(scan_ctx);
}
printf("Available contexts:\n");
int i = 0;
for (i = 0; i < (size_t)ret; i++) {
uri = iio_context_info_get_uri(info[i]);
printf(" %s\n", uri);
}
}
//ctx = iio_create_context_from_uri("ip:192.168.2.1");
struct iio_context* ctx = iio_create_context_from_uri(uri);
printf("Created context for uri %s\n",uri);
if (devicename) {
struct iio_device* dev = iio_context_find_device(ctx, devicename);
const char* name = iio_device_get_name(dev);
printf("Found device %s\n",name);
if (printinfo) { printDeviceInfo(dev); }
if (frequency != 0) {
struct iio_channel* chn = iio_device_find_channel(dev, "altvoltage0", true);
setAttribute(chn, "frequency", frequency);
}
// TODO which settings on hardware are the right ones for freq/bw/sr?
if (bandwidth != 0) {
printf("Not implemented\n");
}
if (samplerate != 0) {
struct iio_channel* chn = iio_device_find_channel(dev, "voltage0", false);
setAttribute(chn, "sampling_frequency", samplerate);
}
if (receivefile) {
printf("Press Ctrl-C to stop receiving data\n");
receive(ctx, receivefile);
printf("Finished receive()\n");
}
if (transmitfile) {
printf("Not implemented\n");
}
} else {
if (printinfo) { printDevicesInfo(ctx); }
}
iio_context_destroy(ctx);
printf("#### Finished PlutoSDR Test ####\n");
return 0;
}