-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathad9361.cpp
More file actions
401 lines (367 loc) · 14.4 KB
/
ad9361.cpp
File metadata and controls
401 lines (367 loc) · 14.4 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
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
#include "ad9361.hpp"
#include <SoapySDR/Logger.hpp>
#include <sstream>
#include <vector>
enum class IQ : char {
i = 'i',
q = 'q'
};
std::vector<std::string> split_string(std::string str) {
std::istringstream iss(str);
std::string word;
std::vector<std::string> words;
while (iss >> word) {
words.push_back(word);
}
return words;
}
std::string channel_voltage_name(uint8_t channel, IQ iq) {
std::string attr = "voltage";
if (channel == 0) {
if (iq == IQ::i) {
return attr + "0";
}
if (iq == IQ::q) {
return attr + "1";
}
} else if (channel == 1) {
if (iq == IQ::i) {
return attr + "2";
}
if (iq == IQ::q) {
return attr + "3";
}
}
throw std::runtime_error("channel could be only 0 or 1 ");
}
int AD9361::set_frequency(long long freq, bool output) {
if (output == true) {
return set_channel_param(lo_channel_output, "frequency", freq);
}
return set_channel_param(lo_channel_input, "frequency", freq);
}
int AD9361::set_bandwidth_frequency(long long freq, bool output) {
if (output == true) {
return set_channel_param(phy_channel_output, "rf_bandwidth", freq);
}
return set_channel_param(phy_channel_input, "rf_bandwidth", freq);
}
double AD9361::get_bandwidth_frequency(bool output) {
iio_channel* chan = iio_device_find_channel(ad9361_phy, "voltage0", output);
const struct iio_attr* attr = iio_channel_find_attr(chan, "rf_bandwidth");
double value;
iio_attr_read_double(attr, &value);
return value;
}
double AD9361::get_sample_rate(bool output) {
if (output == true) {
return get_channel_param(phy_channel_output, "sampling_frequency");
}
return get_channel_param(phy_channel_input, "sampling_frequency");
}
double AD9361::get_frequency(bool output) {
if (output == true) {
return get_channel_param(lo_channel_output, "frequency");
}
return get_channel_param(lo_channel_input, "frequency");
}
int AD9361::set_gain(uint8_t channel, double value, bool output) {
iio_channel* chan;
if (channel == 0) {
chan = iio_device_find_channel(ad9361_phy, "voltage0", output);
} else if (channel == 1) {
chan = iio_device_find_channel(ad9361_phy, "voltage1", output);
} else {
throw std::runtime_error("can't create context. check url");
}
const struct iio_attr* attr = iio_channel_find_attr(chan, "hardwaregain");
try {
iio_attr_write_double(attr, value);
} catch (...) {
}
return 0;
}
double AD9361::get_gain(uint8_t channel, bool output) {
iio_channel* chan;
if (channel == 0) {
chan = iio_device_find_channel(ad9361_phy, "voltage0", output);
} else if (channel == 1) {
chan = iio_device_find_channel(ad9361_phy, "voltage1", output);
} else {
throw std::runtime_error("can't create context. check url");
}
const struct iio_attr* attr = iio_channel_find_attr(chan, "hardwaregain");
double value;
iio_attr_read_double(attr, &value);
return value;
}
void AD9361::set_gain_mode(uint8_t channel, bool output, bool automatic) {
iio_channel* chan;
if (channel == 0) {
chan = iio_device_find_channel(ad9361_phy, "voltage0", output);
} else if (channel == 1) {
chan = iio_device_find_channel(ad9361_phy, "voltage1", output);
} else {
throw std::runtime_error("can't create context. check url");
}
const struct iio_attr* attr = iio_channel_find_attr(chan, "gain_control_mode");
iio_attr_write_string(attr, automatic ? "slow_attack" : "manual");
}
bool AD9361::get_gain_mode(uint8_t channel, bool output) {
iio_channel* chan;
if (channel == 0) {
chan = iio_device_find_channel(ad9361_phy, "voltage0", output);
} else if (channel == 1) {
chan = iio_device_find_channel(ad9361_phy, "voltage1", output);
} else {
throw std::runtime_error("can't create context. check url");
}
const struct iio_attr* attr = iio_channel_find_attr(chan, "gain_control_mode");
char buf[64];
iio_attr_read_raw(attr, buf, sizeof(buf));
std::string mode(buf);
SoapySDR_logf(SOAPY_SDR_DEBUG, "current gain mode is %s ", buf);
return mode != "manual";
}
int AD9361::set_sample_rate(long long freq, bool output) {
if (output == true) {
return set_channel_param(phy_channel_output, "sampling_frequency", freq);
}
return set_channel_param(phy_channel_input, "sampling_frequency", freq);
}
AD9361::AD9361(std::string url) {
SoapySDR_logf(SOAPY_SDR_DEBUG, "constructor start");
ctx = iio_create_context(NULL, url.c_str());
int error = iio_err(ctx);
if (error != 0) {
throw std::runtime_error("can't create context. check url");
}
ad9361_phy = iio_context_find_device(ctx, "ad9361-phy");
if (!ad9361_phy) {
throw std::runtime_error("No ad9361-phy found");
}
phy_channel_input = iio_device_find_channel(ad9361_phy, "voltage0", false);
phy_channel_output = iio_device_find_channel(ad9361_phy, "voltage0", true);
lo_channel_input = iio_device_find_channel(ad9361_phy, "altvoltage0", true);
if (!lo_channel_input) {
throw std::runtime_error("No lo_channel_input found");
}
lo_channel_output = iio_device_find_channel(ad9361_phy, "altvoltage1", true);
if (!lo_channel_output) {
throw std::runtime_error("No lo_channel_output found");
}
device_output = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
if (!device_output) {
throw std::runtime_error("No device_input");
}
device_input = iio_context_find_device(ctx, "cf-ad9361-lpc");
if (!device_input) {
throw std::runtime_error("No device_input");
}
rx_mask = iio_create_channels_mask(iio_device_get_channels_count(device_input));
if (!rx_mask) {
throw std::runtime_error("No rx_mask");
}
tx_mask = iio_create_channels_mask(iio_device_get_channels_count(device_output));
if (!tx_mask) {
throw std::runtime_error("No rx_mask");
}
SoapySDR_logf(SOAPY_SDR_DEBUG, "AD9361 constructor end");
}
AD9361::~AD9361() {
SoapySDR_logf(SOAPY_SDR_DEBUG, "AD9361 destructor start");
if (rx_mask) {
iio_channels_mask_destroy(rx_mask);
SoapySDR_logf(SOAPY_SDR_DEBUG, "rx_mask destroed ");
}
if (rx_stream) {
iio_stream_destroy(rx_stream);
SoapySDR_logf(SOAPY_SDR_DEBUG, "rx_stream destroed ");
}
if (rx_buffer) {
iio_buffer_destroy(rx_buffer);
SoapySDR_logf(SOAPY_SDR_DEBUG, "rx_buffer destroed ");
}
if (tx_mask) {
iio_channels_mask_destroy(tx_mask);
SoapySDR_logf(SOAPY_SDR_DEBUG, "tx_mask destroed ");
}
if (tx_stream) {
iio_stream_destroy(tx_stream);
SoapySDR_logf(SOAPY_SDR_DEBUG, "tx_stream destroed ");
}
if (tx_buffer) {
iio_buffer_destroy(tx_buffer);
SoapySDR_logf(SOAPY_SDR_DEBUG, "tx_buffer destroed ");
}
if (ctx) {
iio_context_destroy(ctx);
}
SoapySDR_logf(SOAPY_SDR_DEBUG, "AD9361 destructor end");
}
int AD9361::set_channel_param(iio_channel* channel, const char* key, long long value) {
const struct iio_attr* attr = iio_channel_find_attr(channel, key);
return iio_attr_write_longlong(attr, value);
}
int AD9361::set_channel_param_double(iio_channel* channel, const char* key, double value) {
const struct iio_attr* attr = iio_channel_find_attr(channel, key);
return iio_attr_write_double(attr, value);
}
long long AD9361::get_channel_param(iio_channel* channel, const char* key) {
const struct iio_attr* attr = iio_channel_find_attr(channel, key);
long long val;
iio_attr_read_longlong(attr, &val);
return val;
}
size_t AD9361::get_rx_sample_size() {
ssize_t sample_rate = iio_device_get_sample_size(device_input, rx_mask);
return static_cast<size_t>(sample_rate);
}
void AD9361::rx_channel_enable(uint8_t channel) {
SoapySDR_logf(SOAPY_SDR_DEBUG, "rx_channel_enable");
if (!device_input) {
throw std::runtime_error("devce_input is not created");
}
rx_chan[channel].rx_ch_i = iio_device_find_channel(device_input, channel_voltage_name(channel, IQ::i).c_str(), false);
if (!rx_chan[channel].rx_ch_i) {
throw std::runtime_error("unable to get I channel");
}
rx_chan[channel].rx_ch_q = iio_device_find_channel(device_input, channel_voltage_name(channel, IQ::q).c_str(), false);
if (!rx_chan[channel].rx_ch_q) {
throw std::runtime_error("unable to get Q channel");
}
iio_channel_enable(rx_chan[channel].rx_ch_i, rx_mask);
iio_channel_enable(rx_chan[channel].rx_ch_q, rx_mask);
rx_buffer = iio_device_create_buffer(device_input, 0, rx_mask);
if (iio_err(rx_buffer) != 0) {
throw std::runtime_error("No rx_buffer");
}
rx_stream = iio_buffer_create_stream(rx_buffer, 4, BLOCK_SIZE);
if (iio_err(rx_stream) != 0) {
throw std::runtime_error("No rx_stream");
}
SoapySDR_logf(SOAPY_SDR_DEBUG, "rx_channel_enable end");
}
void AD9361::rx_channel_disable(uint8_t channel) {
rx_chan[channel].rx_ch_i = iio_device_find_channel(device_input, channel_voltage_name(channel, IQ::i).c_str(), false);
if (!rx_chan[channel].rx_ch_i) {
throw std::runtime_error("unable to get I channel");
}
rx_chan[channel].rx_ch_q = iio_device_find_channel(device_input, channel_voltage_name(channel, IQ::q).c_str(), false);
if (!rx_chan[channel].rx_ch_q) {
throw std::runtime_error("unable to get Q channel");
}
if (rx_mask && rx_chan[channel].rx_ch_i && rx_chan[channel].rx_ch_q) {
iio_channel_disable(rx_chan[channel].rx_ch_i, rx_mask);
iio_channel_disable(rx_chan[channel].rx_ch_q, rx_mask);
SoapySDR_logf(SOAPY_SDR_DEBUG, "rx_channel_disabled");
}
}
void AD9361::tx_channel_enable(uint8_t channel) {
SoapySDR_logf(SOAPY_SDR_DEBUG, "tx_channel_enable");
if (!device_output) {
throw std::runtime_error("devce_input is not created");
}
tx_chan[channel].tx_ch_i = iio_device_find_channel(device_output, channel_voltage_name(channel, IQ::i).c_str(), true);
if (!tx_chan[channel].tx_ch_i) {
throw std::runtime_error("unable to get I channel");
}
tx_chan[channel].tx_ch_q = iio_device_find_channel(device_output, channel_voltage_name(channel, IQ::q).c_str(), true);
if (!tx_chan[channel].tx_ch_q) {
throw std::runtime_error("unable to get Q channel");
}
iio_channel_enable(tx_chan[channel].tx_ch_i, tx_mask);
iio_channel_enable(tx_chan[channel].tx_ch_q, tx_mask);
tx_buffer = iio_device_create_buffer(device_output, 0, tx_mask);
if (iio_err(tx_buffer) != 0) {
throw std::runtime_error("No tx_buffer");
}
tx_stream = iio_buffer_create_stream(tx_buffer, 4, BLOCK_SIZE);
if (iio_err(tx_stream) != 0) {
throw std::runtime_error("No tx_stream");
}
SoapySDR_logf(SOAPY_SDR_DEBUG, "tx_channel_enable end");
}
void AD9361::tx_channel_disable(uint8_t channel) {
tx_chan[channel].tx_ch_i = iio_device_find_channel(device_output, channel_voltage_name(channel, IQ::i).c_str(), true);
if (!tx_chan[channel].tx_ch_i) {
throw std::runtime_error("unable to get I channel");
}
tx_chan[channel].tx_ch_q = iio_device_find_channel(device_output, channel_voltage_name(channel, IQ::q).c_str(), true);
if (!tx_chan[channel].tx_ch_q) {
throw std::runtime_error("unable to get Q channel");
}
if (tx_mask && tx_chan[channel].tx_ch_i && tx_chan[channel].tx_ch_q) {
iio_channel_disable(tx_chan[channel].tx_ch_i, tx_mask);
iio_channel_disable(tx_chan[channel].tx_ch_q, tx_mask);
SoapySDR_logf(SOAPY_SDR_DEBUG, "tx_channel_disabled");
}
}
BlockPointer AD9361::prepare_next_block() {
const iio_block* rx_block = iio_stream_get_next_block(rx_stream);
int err = iio_err(rx_block);
if (err) {
throw std::runtime_error("unable to receive block");
}
int16_t* p_end = reinterpret_cast<int16_t*>(iio_block_end(rx_block));
iio_channel* ch = (rx_chan[0].rx_ch_i) ? rx_chan[0].rx_ch_i : rx_chan[1].rx_ch_i;
if (!ch) {
throw std::runtime_error("can't prepare block. wrong channel selected");
}
int16_t* p_start = reinterpret_cast<int16_t*>(iio_block_first(rx_block, ch));
return {p_start, p_end};
}
BlockPointer AD9361::prepare_next_block_tx() {
const iio_block* tx_block = iio_stream_get_next_block(tx_stream);
int err = iio_err(tx_block);
if (err) {
throw std::runtime_error("unable to receive block");
}
int16_t* p_end = reinterpret_cast<int16_t*>(iio_block_end(tx_block));
iio_channel* ch = (tx_chan[0].tx_ch_i) ? tx_chan[0].tx_ch_i : tx_chan[1].tx_ch_i;
if (!ch) {
throw std::runtime_error("can't prepare block. wrong channel selected");
}
int16_t* p_start = reinterpret_cast<int16_t*>(iio_block_first(tx_block, ch));
return {p_start, p_end};
}
std::vector<std::string> AD9361::get_available_rf_ports(uint8_t channel, bool output) {
iio_channel* chan;
if (channel == 0) {
chan = iio_device_find_channel(ad9361_phy, "voltage0", output);
} else if (channel == 1) {
chan = iio_device_find_channel(ad9361_phy, "voltage1", output);
} else {
throw std::runtime_error("can't create context. check url");
}
const struct iio_attr* attr = iio_channel_find_attr(chan, "rf_port_select_available");
char buf[500];
iio_attr_read_raw(attr, buf, sizeof(buf));
return split_string(buf);
}
ssize_t AD9361::rf_port_select(uint8_t channel, bool output, std::string rf_port) {
iio_channel* chan;
if (channel == 0) {
chan = iio_device_find_channel(ad9361_phy, "voltage0", output);
} else if (channel == 1) {
chan = iio_device_find_channel(ad9361_phy, "voltage1", output);
} else {
throw std::runtime_error("can't create context. check url");
}
const struct iio_attr* attr = iio_channel_find_attr(chan, "rf_port_select");
return iio_attr_write_string(attr, rf_port.c_str());
}
std::string AD9361::get_rf_port(uint8_t channel, bool output) {
iio_channel* chan;
if (channel == 0) {
chan = iio_device_find_channel(ad9361_phy, "voltage0", output);
} else if (channel == 1) {
chan = iio_device_find_channel(ad9361_phy, "voltage1", output);
} else {
throw std::runtime_error("can't create context. check url");
}
const struct iio_attr* attr = iio_channel_find_attr(chan, "rf_port_select");
char buf[500];
iio_attr_read_raw(attr, buf, sizeof(buf));
return std::string(buf);
}