forked from gridrf/SoapyNH7020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlutoSDR_Settings.cpp
522 lines (383 loc) · 14.5 KB
/
PlutoSDR_Settings.cpp
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include "SoapyPlutoSDR.hpp"
#ifdef HAS_AD9361_IIO
#include <ad9361.h>
#endif
static iio_context *ctx = nullptr;
/* static scratch mem for strings */
static char tmpstr[64];
/* helper function generating channel names */
static char* get_ch_name(const char* type, int id)
{
snprintf(tmpstr, sizeof(tmpstr), "%s%d", type, id);
return tmpstr;
}
SoapyPlutoSDR::SoapyPlutoSDR( const SoapySDR::Kwargs &args ):
dev(nullptr), rx_dev(nullptr),tx_dev(nullptr), decimation(false), interpolation(false), rx_stream(nullptr)
{
int port = 0;
gainMode = false;
if (args.count("label") != 0)
SoapySDR_logf( SOAPY_SDR_INFO, "Opening %s...", args.at("label").c_str());
if(args.count("port")!=0){
port = atoi(args.at("port").c_str());
}
if(ctx == nullptr)
{
if(args.count("hostname")!=0){
ctx = iio_create_network_context(args.at("hostname").c_str());
}else{
ctx = iio_create_network_context("192.168.2.1");
}
}
if (ctx == nullptr) {
SoapySDR_logf(SOAPY_SDR_ERROR, "not device found.");
throw std::runtime_error("not device found");
}
dev = iio_context_find_device(ctx, "ad9361-phy");
rx_dev = iio_context_find_device(ctx, "cf-ad9361-lpc");
tx_dev = iio_context_find_device(ctx, "cf-ad9361-dds-core-lpc");
this->setAntenna(SOAPY_SDR_RX, 0, "A_BALANCED");
this->setAntenna(SOAPY_SDR_TX, 0, "A");
}
SoapyPlutoSDR::~SoapyPlutoSDR(void){
long long samplerate=0;
if(decimation){
iio_channel_attr_read_longlong(iio_device_find_channel(dev, get_ch_name("voltage", 0), false),"sampling_frequency",&samplerate);
iio_channel_attr_write_longlong(iio_device_find_channel(rx_dev, get_ch_name("voltage", 0), false),"sampling_frequency", samplerate);
}
if(interpolation){
iio_channel_attr_read_longlong(iio_device_find_channel(dev, get_ch_name("voltage", 0), true),"sampling_frequency",&samplerate);
iio_channel_attr_write_longlong(iio_device_find_channel(tx_dev, get_ch_name("voltage", 0), true),"sampling_frequency", samplerate);
}
if(ctx)
{
iio_context_destroy(ctx);
ctx = nullptr;
}
}
/*******************************************************************
* Identification API
******************************************************************/
std::string SoapyPlutoSDR::getDriverKey( void ) const
{
return "PlutoSDR";
}
std::string SoapyPlutoSDR::getHardwareKey( void ) const
{
return "NH7020";
}
SoapySDR::Kwargs SoapyPlutoSDR::getHardwareInfo( void ) const
{
SoapySDR::Kwargs info;
unsigned int major, minor;
char git_tag[8];
iio_library_get_version(&major, &minor, git_tag);
char lib_ver[100];
snprintf(lib_ver, 100, "%u.%u (git tag: %s)", major, minor, git_tag);
info["library_version"] = lib_ver;
iio_context_get_version(ctx, &major, &minor, git_tag);
char backend_ver[100];
snprintf(backend_ver, 100, "%u.%u (git tag: %s)", major, minor, git_tag);
info["backend_version"] = backend_ver;
unsigned int nb_ctx_attrs = iio_context_get_attrs_count(ctx);
for (unsigned int i = 0; i < nb_ctx_attrs; i++) {
const char *key, *value;
iio_context_get_attr(ctx, i, &key, &value);
info[key] = value;
}
return info;
}
/*******************************************************************
* Channels API
******************************************************************/
size_t SoapyPlutoSDR::getNumChannels( const int dir ) const
{
return(2);
}
bool SoapyPlutoSDR::getFullDuplex( const int direction, const size_t channel ) const
{
return(true);
}
/*******************************************************************
* Settings API
******************************************************************/
SoapySDR::ArgInfoList SoapyPlutoSDR::getSettingInfo(void) const
{
SoapySDR::ArgInfoList setArgs;
return setArgs;
}
void SoapyPlutoSDR::writeSetting(const std::string &key, const std::string &value)
{
}
std::string SoapyPlutoSDR::readSetting(const std::string &key) const
{
std::string info;
return info;
}
/*******************************************************************
* Antenna API
******************************************************************/
std::vector<std::string> SoapyPlutoSDR::listAntennas( const int direction, const size_t channel ) const
{
std::vector<std::string> options;
if(direction == SOAPY_SDR_RX) options.push_back( "A_BALANCED" );
if(direction == SOAPY_SDR_TX) options.push_back( "A" );
return(options);
}
void SoapyPlutoSDR::setAntenna( const int direction, const size_t channel, const std::string &name )
{
if (direction == SOAPY_SDR_RX) {
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
iio_channel_attr_write(iio_device_find_channel(dev, get_ch_name("voltage", channel), false), "rf_port_select", name.c_str());
}
else if (direction == SOAPY_SDR_TX) {
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
iio_channel_attr_write(iio_device_find_channel(dev, get_ch_name("voltage", channel), true), "rf_port_select", name.c_str());
}
}
std::string SoapyPlutoSDR::getAntenna( const int direction, const size_t channel ) const
{
std::string options;
if (direction == SOAPY_SDR_RX) {
options = "A_BALANCED";
}
else if (direction == SOAPY_SDR_TX) {
options = "A";
}
return options;
}
/*******************************************************************
* Frontend corrections API
******************************************************************/
bool SoapyPlutoSDR::hasDCOffsetMode( const int direction, const size_t channel ) const
{
return(true);
}
/*******************************************************************
* Gain API
******************************************************************/
std::vector<std::string> SoapyPlutoSDR::listGains( const int direction, const size_t channel ) const
{
std::vector<std::string> options;
options.push_back("PGA");
return(options);
}
bool SoapyPlutoSDR::hasGainMode(const int direction, const size_t channel) const
{
if (direction == SOAPY_SDR_RX)
return true;
return false;
}
void SoapyPlutoSDR::setGainMode( const int direction, const size_t channel, const bool automatic )
{
gainMode = automatic;
if(direction==SOAPY_SDR_RX){
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
if (gainMode) {
iio_channel_attr_write(iio_device_find_channel(dev, get_ch_name("voltage", channel), false), "gain_control_mode", "slow_attack");
}else{
iio_channel_attr_write(iio_device_find_channel(dev, get_ch_name("voltage", channel), false), "gain_control_mode", "manual");
}
}
}
bool SoapyPlutoSDR::getGainMode(const int direction, const size_t channel) const
{
return gainMode;
}
void SoapyPlutoSDR::setGain( const int direction, const size_t channel, const double value )
{
long long gain = (long long) value;
if(direction==SOAPY_SDR_RX){
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
iio_channel_attr_write_longlong(iio_device_find_channel(dev, get_ch_name("voltage", channel), false),"hardwaregain", gain);
}
else if(direction==SOAPY_SDR_TX){
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
gain = gain - 89;
iio_channel_attr_write_longlong(iio_device_find_channel(dev, get_ch_name("voltage", channel), true),"hardwaregain", gain);
}
}
void SoapyPlutoSDR::setGain( const int direction, const size_t channel, const std::string &name, const double value )
{
this->setGain(direction,channel,value);
}
double SoapyPlutoSDR::getGain( const int direction, const size_t channel, const std::string &name ) const
{
long long gain = 0;
if(direction==SOAPY_SDR_RX){
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
if(iio_channel_attr_read_longlong(iio_device_find_channel(dev, get_ch_name("voltage", channel), false),"hardwaregain",&gain )!=0)
return 0;
}
else if(direction==SOAPY_SDR_TX){
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
if(iio_channel_attr_read_longlong(iio_device_find_channel(dev, get_ch_name("voltage", channel), true),"hardwaregain",&gain )!=0)
return 0;
gain = gain + 89;
}
return double(gain);
}
SoapySDR::Range SoapyPlutoSDR::getGainRange( const int direction, const size_t channel, const std::string &name ) const
{
if(direction==SOAPY_SDR_RX)
return(SoapySDR::Range(0, 73));
return(SoapySDR::Range(0,89));
}
/*******************************************************************
* Frequency API
******************************************************************/
void SoapyPlutoSDR::setFrequency( const int direction, const size_t channel, const std::string &name, const double frequency, const SoapySDR::Kwargs &args )
{
long long freq = (long long)frequency;
if(direction==SOAPY_SDR_RX){
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
iio_channel_attr_write_longlong(iio_device_find_channel(dev, "altvoltage0", true),"frequency", freq);
}
else if(direction==SOAPY_SDR_TX){
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
iio_channel_attr_write_longlong(iio_device_find_channel(dev, "altvoltage1", true),"frequency", freq);
}
}
double SoapyPlutoSDR::getFrequency( const int direction, const size_t channel, const std::string &name ) const
{
long long freq;
if(direction==SOAPY_SDR_RX){
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
if(iio_channel_attr_read_longlong(iio_device_find_channel(dev, "altvoltage0", true),"frequency",&freq )!=0)
return 0;
}
else if(direction==SOAPY_SDR_TX){
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
if(iio_channel_attr_read_longlong(iio_device_find_channel(dev, "altvoltage1", true),"frequency",&freq )!=0)
return 0;
}
return double(freq);
}
SoapySDR::ArgInfoList SoapyPlutoSDR::getFrequencyArgsInfo(const int direction, const size_t channel) const
{
SoapySDR::ArgInfoList freqArgs;
return freqArgs;
}
std::vector<std::string> SoapyPlutoSDR::listFrequencies( const int direction, const size_t channel ) const
{
std::vector<std::string> names;
names.push_back( "RF" );
return(names);
}
SoapySDR::RangeList SoapyPlutoSDR::getFrequencyRange( const int direction, const size_t channel, const std::string &name ) const
{
return(SoapySDR::RangeList( 1, SoapySDR::Range( 70000000, 6000000000ull ) ) );
}
/*******************************************************************
* Sample Rate API
******************************************************************/
void SoapyPlutoSDR::setSampleRate( const int direction, const size_t channel, const double rate )
{
long long samplerate =(long long) rate;
if(direction==SOAPY_SDR_RX){
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
decimation = false;
if (samplerate<(25e6 / 48)) {
if (samplerate * 8 < (25e6 / 48)) {
SoapySDR_logf(SOAPY_SDR_CRITICAL, "sample rate is not supported.");
}
decimation = true;
samplerate = samplerate * 8;
}
iio_channel_attr_write_longlong(iio_device_find_channel(dev, get_ch_name("voltage", channel), false),"sampling_frequency", samplerate);
iio_channel_attr_write_longlong(iio_device_find_channel(rx_dev, get_ch_name("voltage", channel), false), "sampling_frequency", decimation?samplerate/8:samplerate);
if(rx_stream)
rx_stream->set_buffer_size_by_samplerate(decimation ? samplerate / 8 : samplerate);
}
else if(direction==SOAPY_SDR_TX){
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
interpolation = false;
if (samplerate<(25e6 / 48)) {
if (samplerate * 8 < (25e6 / 48)) {
SoapySDR_logf(SOAPY_SDR_CRITICAL, "sample rate is not supported.");
}
interpolation = true;
samplerate = samplerate * 8;
}
iio_channel_attr_write_longlong(iio_device_find_channel(dev, get_ch_name("voltage", channel), true),"sampling_frequency", samplerate);
iio_channel_attr_write_longlong(iio_device_find_channel(tx_dev, get_ch_name("voltage", channel), true), "sampling_frequency", interpolation?samplerate / 8:samplerate);
}
#ifdef HAS_AD9361_IIO
if(ad9361_set_bb_rate(dev,(unsigned long)samplerate))
SoapySDR_logf(SOAPY_SDR_ERROR, "Unable to set BB rate.");
#endif
}
double SoapyPlutoSDR::getSampleRate( const int direction, const size_t channel ) const
{
long long samplerate;
if(direction==SOAPY_SDR_RX){
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
if(iio_channel_attr_read_longlong(iio_device_find_channel(rx_dev, get_ch_name("voltage", channel), false),"sampling_frequency",&samplerate )!=0)
return 0;
}
else if(direction==SOAPY_SDR_TX){
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
if(iio_channel_attr_read_longlong(iio_device_find_channel(tx_dev, get_ch_name("voltage", channel), true),"sampling_frequency",&samplerate)!=0)
return 0;
}
return double(samplerate);
}
std::vector<double> SoapyPlutoSDR::listSampleRates( const int direction, const size_t channel ) const
{
std::vector<double> options;
options.push_back(65105);//25M/48/8+1
options.push_back(1e6);
options.push_back(2e6);
options.push_back(3e6);
options.push_back(4e6);
options.push_back(5e6);
options.push_back(6e6);
options.push_back(7e6);
options.push_back(8e6);
options.push_back(9e6);
options.push_back(10e6);
return(options);
}
void SoapyPlutoSDR::setBandwidth( const int direction, const size_t channel, const double bw )
{
long long bandwidth = (long long) bw;
if(direction==SOAPY_SDR_RX){
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
iio_channel_attr_write_longlong(iio_device_find_channel(dev, get_ch_name("voltage", channel), false),"rf_bandwidth", bandwidth);
}
else if(direction==SOAPY_SDR_TX){
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
iio_channel_attr_write_longlong(iio_device_find_channel(dev, get_ch_name("voltage", channel), true),"rf_bandwidth", bandwidth);
}
}
double SoapyPlutoSDR::getBandwidth( const int direction, const size_t channel ) const
{
long long bandwidth;
if(direction==SOAPY_SDR_RX){
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
if(iio_channel_attr_read_longlong(iio_device_find_channel(dev, get_ch_name("voltage", channel), false),"rf_bandwidth",&bandwidth )!=0)
return 0;
}
else if(direction==SOAPY_SDR_TX){
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
if(iio_channel_attr_read_longlong(iio_device_find_channel(dev, get_ch_name("voltage", channel), true),"rf_bandwidth",&bandwidth )!=0)
return 0;
}
return double(bandwidth);
}
std::vector<double> SoapyPlutoSDR::listBandwidths( const int direction, const size_t channel ) const
{
std::vector<double> options;
options.push_back(0.2e6);
options.push_back(1e6);
options.push_back(2e6);
options.push_back(3e6);
options.push_back(4e6);
options.push_back(5e6);
options.push_back(6e6);
options.push_back(7e6);
options.push_back(8e6);
options.push_back(9e6);
options.push_back(10e6);
return(options);
}