-
Notifications
You must be signed in to change notification settings - Fork 21
/
bladeRF_Settings.cpp
1642 lines (1447 loc) · 60.4 KB
/
bladeRF_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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of the bladeRF project:
* http://www.github.com/nuand/bladeRF
*
* Copyright (C) 2015-2018 Josh Blum
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "bladeRF_SoapySDR.hpp"
#include <SoapySDR/Logger.hpp>
#include <algorithm> //find
#include <stdexcept>
#include <cstdio>
#include <cmath>
//! convert bladerf range to a soapysdr range
static SoapySDR::Range toRange(const bladerf_range* range)
{
return SoapySDR::Range(range->min*range->scale, range->max*range->scale, range->step*range->scale);
}
/*******************************************************************
* Device init/shutdown
******************************************************************/
bladeRF_SoapySDR::bladeRF_SoapySDR(const bladerf_devinfo &devinfo):
_isBladeRF1(false),
_rxSampRate(1.0),
_txSampRate(1.0),
_inTxBurst(false),
_rxFloats(false),
_txFloats(false),
_rxOverflow(false),
_rxNextTicks(0),
_txNextTicks(0),
_timeNsOffset(0),
_rxBuffSize(0),
_txBuffSize(0),
_rxMinTimeoutMs(0),
_xb200Mode("disabled"),
_samplingMode("internal"),
_loopbackMode("disabled"),
_dev(NULL)
{
bladerf_devinfo info = devinfo;
SoapySDR::logf(SOAPY_SDR_INFO, "bladerf_open_with_devinfo()");
int ret = bladerf_open_with_devinfo(&_dev, &info);
if (ret < 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_open_with_devinfo() returned %s", _err2str(ret).c_str());
throw std::runtime_error("bladerf_open_with_devinfo() failed " + _err2str(ret));
}
_isBladeRF1 = std::string(bladerf_get_board_name(_dev)) == "bladerf1";
_isBladeRF2 = std::string(bladerf_get_board_name(_dev)) == "bladerf2";
bladerf_serial serial;
ret = bladerf_get_serial_struct(_dev, &serial);
if (ret == 0) SoapySDR::logf(SOAPY_SDR_INFO, "bladerf_get_serial() = %s", serial.serial);
//initialize the sample rates to something
this->setSampleRate(SOAPY_SDR_RX, 0, 4e6);
this->setSampleRate(SOAPY_SDR_TX, 0, 4e6);
}
bladeRF_SoapySDR::~bladeRF_SoapySDR(void)
{
SoapySDR::logf(SOAPY_SDR_INFO, "bladerf_close()");
if (_dev != NULL) bladerf_close(_dev);
}
/*******************************************************************
* Identification API
******************************************************************/
std::string bladeRF_SoapySDR::getHardwareKey(void) const
{
return bladerf_get_board_name(_dev);
}
SoapySDR::Kwargs bladeRF_SoapySDR::getHardwareInfo(void) const
{
SoapySDR::Kwargs info;
{
bladerf_serial serial;
int ret = bladerf_get_serial_struct(_dev, &serial);
if (ret == 0) info["serial"] = serial.serial;
}
{
bladerf_fpga_size fpgaSize = BLADERF_FPGA_UNKNOWN;
int ret = bladerf_get_fpga_size(_dev, &fpgaSize);
char fpgaStr[100];
sprintf(fpgaStr, "%u", int(fpgaSize));
if (ret == 0) info["fpga_size"] = fpgaStr;
}
{
struct bladerf_version verInfo;
int ret = bladerf_fw_version(_dev, &verInfo);
if (ret == 0) info["fw_version"] = verInfo.describe;
}
{
struct bladerf_version verInfo;
int ret = bladerf_fpga_version(_dev, &verInfo);
if (ret == 0) info["fpga_version"] = verInfo.describe;
}
return info;
}
/*******************************************************************
* Channels API
******************************************************************/
size_t bladeRF_SoapySDR::getNumChannels(const int direction) const
{
return bladerf_get_channel_count(_dev, (direction == SOAPY_SDR_RX)?BLADERF_RX:BLADERF_TX);
}
bool bladeRF_SoapySDR::getFullDuplex(const int, const size_t) const
{
return true;
}
/*******************************************************************
* Antenna API
******************************************************************/
std::vector<std::string> bladeRF_SoapySDR::listAntennas(const int direction, const size_t channel) const
{
return {BLADERF_CHANNEL_IS_TX(_toch(direction, channel))?"TX":"RX"};
}
void bladeRF_SoapySDR::setAntenna(const int, const size_t, const std::string &)
{
return; //nothing to set, ignore it
}
std::string bladeRF_SoapySDR::getAntenna(const int direction, const size_t channel) const
{
return this->listAntennas(direction, channel).front();
}
/*******************************************************************
* Calibration API
******************************************************************/
bool bladeRF_SoapySDR::hasDCOffset(const int, const size_t) const
{
return true;
}
void bladeRF_SoapySDR::setDCOffset(const int direction, const size_t channel, const std::complex<double> &offset)
{
int ret = 0;
int16_t i = 0;
int16_t q = 0;
if (offset.real() > 1.0)
i = int16_t(1.0 * 2048);
else
i = int16_t(offset.real() * 2048);
if (offset.imag() > 1.0)
q = int16_t(1.0 * 2048);
else
q = int16_t(offset.imag() * 2048);
ret = bladerf_set_correction(_dev, _toch(direction, channel), BLADERF_CORR_LMS_DCOFF_I, i);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_correction(%f) returned %s", i, _err2str(ret).c_str());
throw std::runtime_error("setDCOffset() " + _err2str(ret));
}
ret = bladerf_set_correction(_dev, _toch(direction, channel), BLADERF_CORR_LMS_DCOFF_Q, q);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_correction(%f) returned %s", q, _err2str(ret).c_str());
throw std::runtime_error("setDCOffset() " + _err2str(ret));
}
}
std::complex<double> bladeRF_SoapySDR::getDCOffset(const int direction, const size_t channel) const
{
int ret = 0;
int16_t i = 0;
int16_t q = 0;
ret = bladerf_get_correction(_dev, _toch(direction, channel), BLADERF_CORR_LMS_DCOFF_I, &i);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_correction() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getDCOffset() " + _err2str(ret));
}
ret = bladerf_get_correction(_dev, _toch(direction, channel), BLADERF_CORR_LMS_DCOFF_Q, &q);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_correction() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getDCOffset() " + _err2str(ret));
}
std::complex<double> z(i / 2048.0f, q / 2048.0f);
return z;
}
bool bladeRF_SoapySDR::hasIQBalance(const int, const size_t channel) const
{
return true;
}
void bladeRF_SoapySDR::setIQBalance(const int direction, const size_t channel, const std::complex<double> &balance)
{
int ret = 0;
int16_t gain = 0;
int16_t phase = 0;
if (balance.real() > 1.0)
gain = int16_t(1.0 * 4096);
else
gain = int16_t(balance.real() * 4096);
if (balance.imag() > 1.0)
phase = int16_t(1.0 * 4096);
else
phase = int16_t(balance.imag() * 4096);
ret = bladerf_set_correction(_dev, _toch(direction, channel), BLADERF_CORR_FPGA_GAIN, gain);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_correction(%f) returned %s", gain, _err2str(ret).c_str());
throw std::runtime_error("setIQBalance() " + _err2str(ret));
}
ret = bladerf_set_correction(_dev, _toch(direction, channel), BLADERF_CORR_FPGA_PHASE, phase);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_correction(%f) returned %s", phase, _err2str(ret).c_str());
throw std::runtime_error("setIQBalance() " + _err2str(ret));
}
}
std::complex<double> bladeRF_SoapySDR::getIQBalance(const int direction, const size_t channel) const
{
int ret = 0;
int16_t gain = 0;
int16_t phase = 0;
ret = bladerf_get_correction(_dev, _toch(direction, channel), BLADERF_CORR_FPGA_GAIN, &gain);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_correction() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getIQBalance() " + _err2str(ret));
}
ret = bladerf_get_correction(_dev, _toch(direction, channel), BLADERF_CORR_FPGA_PHASE, &phase);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_correction() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getIQBalance() " + _err2str(ret));
}
std::complex<double> z(gain / 4096.0f, phase / 4096.0f);
return z;
}
/*******************************************************************
* Gain API
******************************************************************/
bool bladeRF_SoapySDR::hasGainMode(const int direction, const size_t channel) const
{
if (_toch(direction, channel) != BLADERF_CHANNEL_RX(channel)) {
return false;
} else {
/* This actually depends on a lot of things, including presence of a LUT
* table, so best to determine dynamically.
*/
bladerf_gain_mode mode;
int ret;
ret = bladerf_get_gain_mode(_dev, _toch(direction, channel), &mode);
if (ret != 0) {
return false;
}
/* Test if it will take automatic mode */
ret = bladerf_set_gain_mode(_dev, _toch(direction, channel), BLADERF_GAIN_AUTOMATIC);
if (ret != 0) {
return false;
}
/* We're good - restore it to the original mode */
ret = bladerf_set_gain_mode(_dev, _toch(direction, channel), mode);
if (ret != 0) {
return false;
}
return true;
}
}
void bladeRF_SoapySDR::setGainMode(const int direction, const size_t channel, const bool automatic)
{
if (direction == SOAPY_SDR_TX) return; //not supported on tx
bladerf_gain_mode gain_mode = automatic ? BLADERF_GAIN_AUTOMATIC : BLADERF_GAIN_MANUAL;
const int ret = bladerf_set_gain_mode(_dev, _toch(direction, channel), gain_mode);
if (ret != 0 and automatic) //only throw when mode is automatic, manual is default even when call bombs
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_gain_mode(%s) returned %s", automatic?"automatic":"manual", _err2str(ret).c_str());
throw std::runtime_error("setGainMode() " + _err2str(ret));
}
}
bool bladeRF_SoapySDR::getGainMode(const int direction, const size_t channel) const
{
if (direction == SOAPY_SDR_TX) return false; //not supported on tx
bladerf_gain_mode gain_mode;
int ret = bladerf_get_gain_mode(_dev, _toch(direction, channel), &gain_mode);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_gain_mode() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getGainMode() " + _err2str(ret));
}
return gain_mode == BLADERF_GAIN_AUTOMATIC;
}
std::vector<std::string> bladeRF_SoapySDR::listGains(const int direction, const size_t channel) const
{
#define MAX_STAGES 8
const char *stages[MAX_STAGES];
int ret = bladerf_get_gain_stages(_dev, _toch(direction, channel), (const char **)&stages, MAX_STAGES);
if (ret < 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_gain_stages() returned %s", _err2str(ret).c_str());
throw std::runtime_error("listGains() " + _err2str(ret));
}
std::vector<std::string> options;
for (int i = 0; i < ret; i++) options.push_back(stages[i]);
return options;
}
void bladeRF_SoapySDR::setGain(const int direction, const size_t channel, const double value)
{
const int ret = bladerf_set_gain(_dev, _toch(direction, channel), bladerf_gain(std::round(value)));
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_gain(%f) returned %s", value, _err2str(ret).c_str());
throw std::runtime_error("setGain() " + _err2str(ret));
}
}
void bladeRF_SoapySDR::setGain(const int direction, const size_t channel, const std::string &name, const double value)
{
int ret = bladerf_set_gain_stage(_dev, _toch(direction, channel), name.c_str(), bladerf_gain(std::round(value)));
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_gain_stage(%s, %f) returned %s", name.c_str(), value, _err2str(ret).c_str());
throw std::runtime_error("setGain("+name+") " + _err2str(ret));
}
}
double bladeRF_SoapySDR::getGain(const int direction, const size_t channel) const
{
bladerf_gain gain(0);
const int ret = bladerf_get_gain(_dev, _toch(direction, channel), &gain);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_gain() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getGain() " + _err2str(ret));
}
return double(gain);
}
double bladeRF_SoapySDR::getGain(const int direction, const size_t channel, const std::string &name) const
{
bladerf_gain gain(0);
int ret = bladerf_get_gain_stage(_dev, _toch(direction, channel), name.c_str(), &gain);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_gain_stage(%s) returned %s", name.c_str(), _err2str(ret).c_str());
throw std::runtime_error("getGain("+name+") " + _err2str(ret));
}
return double(gain);
}
SoapySDR::Range bladeRF_SoapySDR::getGainRange(const int direction, const size_t channel) const
{
const bladerf_range* range(nullptr);
int ret = bladerf_get_gain_range(_dev, _toch(direction, channel), &range);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_gain_range() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getGainRange()" + _err2str(ret));
}
return toRange(range);
}
SoapySDR::Range bladeRF_SoapySDR::getGainRange(const int direction, const size_t channel, const std::string &name) const
{
const bladerf_range* range(nullptr);
int ret = bladerf_get_gain_stage_range(_dev, _toch(direction, channel), name.c_str(), &range);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_gain_stage_range(%s) returned %s", name.c_str(), _err2str(ret).c_str());
throw std::runtime_error("getGainRange("+name+")" + _err2str(ret));
}
return toRange(range);
}
/*******************************************************************
* Frequency API
******************************************************************/
void bladeRF_SoapySDR::setFrequency(const int direction, const size_t channel, const std::string &name, const double frequency, const SoapySDR::Kwargs &args)
{
if (name == "BB") return; //for compatibility
if (name != "RF") throw std::runtime_error("setFrequency("+name+") unknown name");
//NOTE on quick tunes:
// - this is available on BladeRF2, not BladeRF1
// - there can be up to NUM_BBP_FASTLOCK_PROFILES (256) quick tunes in both directions.
// bladerf2_get_quick_tune returns BLADERF_ERR_UNEXPECTED if you try to get more.
// - to clear previous quick tunes, the RFIC should have its state reset which is currently done
// when the FPGA is loaded or reloaded. To have the ability to live reset the index to 0,
// the ADI AXI core might have to be modified.
//if "saveQuickTune" == "1", set the frequency and store the quick tune parameter.
//NOTE that it's possible to overwrite previous quick tune parameter.
auto saveQuickTuneIter = args.find("saveQuickTune");
if (saveQuickTuneIter != args.end() && saveQuickTuneIter->second == "1")
{
if (!_isBladeRF2)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "saveQuickTune is only available for BladeRF2.");
throw std::runtime_error("saveQuickTune is only available for BladeRF2.");
}
setRfFrequency(direction, channel, frequency);
auto quickTune = getQuickTune(direction, channel);
if (quickTune == nullptr)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "Cannot set frequency for retune.");
throw std::runtime_error("Cannot set frequency for retune.");
}
auto quickTuneIter = _quickTunesByDirChanAndFreq.find({ direction, channel, frequency });
if (quickTuneIter != _quickTunesByDirChanAndFreq.end()) delete quickTuneIter->second;
_quickTunesByDirChanAndFreq[{direction, channel, frequency}] = quickTune;
return;
}
//Else, if "reuseQuickTune" == "1", we'll see if a corresponding quick tune parameter exists.
//If it does, we'll use it. Else, we throw an exception.
//If "timestamp" is specified, the quick tune will be scheduled at that time, else it'll be done at once.
auto reuseQuickTuneIter = args.find("reuseQuickTune");
if (reuseQuickTuneIter != args.end() && reuseQuickTuneIter->second == "1") {
if (!_isBladeRF2)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "reuseQuickTune is only available for BladeRF2.");
throw std::runtime_error("reuseQuickTune is only available for BladeRF2.");
}
auto quickTuneIter = _quickTunesByDirChanAndFreq.find({ direction, channel, frequency });
if (quickTuneIter == _quickTunesByDirChanAndFreq.end())
{
SoapySDR::logf(SOAPY_SDR_ERROR, "Unkown quick tune for frequency %f and channel %d", frequency, channel);
throw std::runtime_error("Unkown quick tune");
}
auto value = args.find("timestamp");
long long timestamp = value == args.end() ? 0 : std::stoll(value->second);
retune(direction, channel, timestamp, quickTuneIter->second);
return;
}
//Else, we simply set the RF frequency.
setRfFrequency(direction, channel, frequency);
}
void bladeRF_SoapySDR::setRfFrequency(const int direction, const size_t channel, const double frequency)
{
int ret = bladerf_set_frequency(_dev, _toch(direction, channel), bladerf_frequency(std::round(frequency)));
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_frequency(%f) returned %s", frequency, _err2str(ret).c_str());
throw std::runtime_error("setFrequency(RF) " + _err2str(ret));
}
}
double bladeRF_SoapySDR::getFrequency(const int direction, const size_t channel, const std::string &name) const
{
if (name == "BB") return 0.0; //for compatibility
if (name != "RF") throw std::runtime_error("getFrequency("+name+") unknown name");
bladerf_frequency freq(0);
int ret = bladerf_get_frequency(_dev, _toch(direction, channel), &freq);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_frequency() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getFrequency("+name+") " + _err2str(ret));
}
return double(freq);
}
std::vector<std::string> bladeRF_SoapySDR::listFrequencies(const int, const size_t channel) const
{
return {"RF"};
}
SoapySDR::RangeList bladeRF_SoapySDR::getFrequencyRange(const int direction, const size_t channel, const std::string &name) const
{
if (name == "BB") return SoapySDR::RangeList(1, SoapySDR::Range(0.0, 0.0)); //for compatibility
if (name != "RF") throw std::runtime_error("getFrequencyRange("+name+") unknown name");
const bladerf_range* range(nullptr);
int ret = bladerf_get_frequency_range(_dev, _toch(direction, channel), &range);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_frequency_range() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getFrequencyRange() " + _err2str(ret));
}
return {toRange(range)};
}
bladerf_quick_tune* bladeRF_SoapySDR::getQuickTune(const int direction, const size_t channel) const
{
bladerf_quick_tune* quick_tune = new bladerf_quick_tune();
bladerf_channel ch = _toch(direction, channel);
int ret = bladerf_get_quick_tune(_dev, ch, quick_tune);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_quick_tune() returned %s", _err2str(ret).c_str());
delete quick_tune;
return nullptr;
}
return quick_tune;
}
void bladeRF_SoapySDR::retune(const int direction, const size_t channel, long long timestamp, bladerf_quick_tune* quickTune)
{
bladerf_channel ch = _toch(direction, channel);
int ret = bladerf_schedule_retune(_dev, ch, timestamp, 0 /* frequency not needed for retune */, quickTune);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_schedule_retune() returned %s", _err2str(ret).c_str());
throw std::runtime_error("retune() " + _err2str(ret));
}
}
/*******************************************************************
* Sample Rate API
******************************************************************/
void bladeRF_SoapySDR::setSampleRate(const int direction, const size_t channel, const double rate)
{
bladerf_rational_rate ratRate;
ratRate.integer = uint64_t(rate);
ratRate.den = uint64_t(1 << 14); //arbitrary denominator -- should be big enough
ratRate.num = uint64_t(rate - ratRate.integer) * ratRate.den;
//stash the approximate hardware time so it can be restored
const long long timeNow = this->getHardwareTime();
int ret = bladerf_set_rational_sample_rate(_dev, _toch(direction, channel), &ratRate, NULL);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_rational_sample_rate(%f) returned %s", rate, _err2str(ret).c_str());
throw std::runtime_error("setSampleRate() " + _err2str(ret));
}
//stash the actual rate
const double actual = this->getSampleRate(direction, channel);
if (direction == SOAPY_SDR_RX)
{
_rxSampRate = actual;
this->updateRxMinTimeoutMs();
}
if (direction == SOAPY_SDR_TX)
{
_txSampRate = actual;
}
//restore the previous hardware time setting (after rate stash)
this->setHardwareTime(timeNow);
SoapySDR::logf(SOAPY_SDR_INFO, "setSampleRate(%s, %d, %f MHz), actual = %f MHz", direction==SOAPY_SDR_RX?"Rx":"Tx", int(channel), rate/1e6, actual/1e6);
}
double bladeRF_SoapySDR::getSampleRate(const int direction, const size_t channel) const
{
bladerf_rational_rate ratRate;
int ret = bladerf_get_rational_sample_rate(_dev, _toch(direction, channel), &ratRate);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_rational_sample_rate() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getSampleRate() " + _err2str(ret));
}
return double(ratRate.integer) + (double(ratRate.num)/double(ratRate.den));
}
SoapySDR::RangeList bladeRF_SoapySDR::getSampleRateRange(const int direction, const size_t channel) const
{
const bladerf_range* range(nullptr);
int ret = bladerf_get_sample_rate_range(_dev, _toch(direction, channel), &range);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_sample_rate_range() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getSampleRateRange() " + _err2str(ret));
}
//create useful ranges based on the overall range
//these values were suggested by the authors in the gr-osmosdr plugin for bladerf
const auto overallRange = toRange(range);
SoapySDR::RangeList ranges;
ranges.emplace_back(overallRange.minimum()/1.0, overallRange.maximum()/4.0, overallRange.maximum()/16.0);
ranges.emplace_back(overallRange.maximum()/4.0, overallRange.maximum()/2.0, overallRange.maximum()/8.0);
ranges.emplace_back(overallRange.maximum()/2.0, overallRange.maximum()/1.0, overallRange.maximum()/4.0);
return ranges;
}
std::vector<double> bladeRF_SoapySDR::listSampleRates(const int direction, const size_t channel) const
{
//deprecated list of sample rates, just iterate though the ranges and build a list
std::vector<double> rates;
for (const auto &range : this->getSampleRateRange(direction, channel))
{
for (double rate = range.minimum(); rate <= range.maximum(); rate += range.step())
{
rates.push_back(rate);
}
}
return rates;
}
/*******************************************************************
* Bandwidth API
******************************************************************/
void bladeRF_SoapySDR::setBandwidth(const int direction, const size_t channel, const double bw)
{
//bypass the filter when sufficiently large BW is selected
if (bw > this->getBandwidthRange(direction, channel).back().maximum())
{
bladerf_set_lpf_mode(_dev, _toch(direction, channel), BLADERF_LPF_BYPASSED);
return;
}
//otherwise set to normal and configure the filter bandwidth
bladerf_set_lpf_mode(_dev, _toch(direction, channel), BLADERF_LPF_NORMAL);
int ret = bladerf_set_bandwidth(_dev, _toch(direction, channel), bladerf_bandwidth(std::round(bw)), NULL);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_bandwidth(%f) returned %s", bw, _err2str(ret).c_str());
throw std::runtime_error("setBandwidth() " + _err2str(ret));
}
}
double bladeRF_SoapySDR::getBandwidth(const int direction, const size_t channel) const
{
bladerf_bandwidth bw(0);
int ret = bladerf_get_bandwidth(_dev, _toch(direction, channel), &bw);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_bandwidth() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getBandwidth() " + _err2str(ret));
}
return double(bw);
}
SoapySDR::RangeList bladeRF_SoapySDR::getBandwidthRange(const int direction, const size_t channel) const
{
const bladerf_range* range(nullptr);
int ret = bladerf_get_bandwidth_range(_dev, _toch(direction, channel), &range);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_bandwidth_range() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getBandwidthRange() " + _err2str(ret));
}
return {toRange(range)};
}
std::vector<double> bladeRF_SoapySDR::listBandwidths(const int direction, const size_t channel) const
{
//this is a deprecated call, it should be removed in the future
//for bladerfv2, return a simple 2 element list based on the available range
if (this->getNumChannels(direction) == 2)
{
const auto ranges = this->getBandwidthRange(direction, channel);
return {ranges.front().minimum(), ranges.back().maximum()};
}
//for bladerfv1 these were the chosen bw options
//but the authors removed it in gr-osmosdr
//so thats why its not present in the ranges API
std::vector<double> options = {0.75, 0.875, 1.25, 1.375, 1.5, 1.92, 2.5, 2.75, 3, 3.5, 4.375, 5, 6, 7, 10, 14};
for (auto &option : options) option *= 2e6;
return options;
}
/*******************************************************************
* Clocking API
******************************************************************/
void bladeRF_SoapySDR::setMasterClockRate(const double rate)
{
if (! _isBladeRF2) return;
int ret = bladerf_set_pll_refclk(_dev, uint64_t(rate));
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_pll_refclk() returned %s", _err2str(ret).c_str());
throw std::runtime_error("setMasterClockRate() " + _err2str(ret));
}
}
double bladeRF_SoapySDR::getMasterClockRate(void) const
{
if (! _isBladeRF2) return 0;
uint64_t rate(0);
int ret = bladerf_get_pll_refclk(_dev, &rate);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_pll_refclk() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getMasterClockRate() " + _err2str(ret));
}
return double(rate);
}
SoapySDR::RangeList bladeRF_SoapySDR::getMasterClockRates(void) const
{
if (! _isBladeRF2) return SoapySDR::RangeList();
const bladerf_range* range(nullptr);
int ret = bladerf_get_pll_refclk_range(_dev, &range);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_pll_refclk_range() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getMasterClockRates() " + _err2str(ret));
}
return {toRange(range)};
}
std::vector<std::string> bladeRF_SoapySDR::listClockSources(void) const
{
std::vector<std::string> clocks;
clocks.push_back("internal");
if (_isBladeRF2) clocks.push_back("ref_in");
return clocks;
}
void bladeRF_SoapySDR::setClockSource(const std::string &source)
{
if (! _isBladeRF2) return;
bool enable = (source == "ref_in");
int ret = bladerf_set_pll_enable(_dev, enable);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_pll_enable() returned %s", _err2str(ret).c_str());
throw std::runtime_error("setClockSource() " + _err2str(ret));
}
}
std::string bladeRF_SoapySDR::getClockSource(void) const
{
if (! _isBladeRF2) return "internal";
bool enabled(false);
int ret = bladerf_get_pll_enable(_dev, &enabled);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_pll_enable() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getClockSource() " + _err2str(ret));
}
if (enabled) return "ref_in";
else return "internal";
}
/*******************************************************************
* Time API
******************************************************************/
bool bladeRF_SoapySDR::hasHardwareTime(const std::string &what) const
{
if (not what.empty()) return SoapySDR::Device::hasHardwareTime(what);
return true;
}
long long bladeRF_SoapySDR::getHardwareTime(const std::string &what) const
{
if (not what.empty()) return SoapySDR::Device::getHardwareTime(what);
uint64_t ticksNow = 0;
const int ret = bladerf_get_timestamp(_dev, BLADERF_RX, &ticksNow);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_timestamp() returned %s", _err2str(ret).c_str());
throw std::runtime_error("getHardwareTime() " + _err2str(ret));
}
return _rxTicksToTimeNs(ticksNow);
}
void bladeRF_SoapySDR::setHardwareTime(const long long timeNs, const std::string &what)
{
if (not what.empty()) return SoapySDR::Device::setHardwareTime(timeNs, what);
//reset the counters with GPIO and stash the offset
//this is the same as setting the time because
//we maintain the offset math within the driver
int ret = 0;
uint32_t original = 0;
ret |= bladerf_config_gpio_read(_dev, &original);
ret |= bladerf_config_gpio_write(_dev, original & ~(BLADERF_GPIO_TIMESTAMP));
ret |= bladerf_config_gpio_write(_dev, original | BLADERF_GPIO_TIMESTAMP);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_config_gpio_read/write() returned %s", _err2str(ret).c_str());
throw std::runtime_error("setHardwareTime() " + _err2str(ret));
}
_timeNsOffset = timeNs;
}
/*******************************************************************
* Sensor API
******************************************************************/
std::vector<std::string> bladeRF_SoapySDR::listSensors(void) const
{
std::vector<std::string> sensors;
if (_isBladeRF2) sensors.push_back("RFIC_TEMP");
return sensors;
}
SoapySDR::ArgInfo bladeRF_SoapySDR::getSensorInfo(const std::string &key) const
{
if (key == "RFIC_TEMP")
{
SoapySDR::ArgInfo info;
info.key = key;
info.value = "0";
info.name = "RFIC Temperature";
info.description = "Temperature in degrees C";
info.units = "C";
info.type = SoapySDR::ArgInfo::FLOAT;
return info;
}
else throw std::runtime_error("getSensorInfo(" + key + ") unknown sensor");
}
std::string bladeRF_SoapySDR::readSensor(const std::string &key) const
{
if (key == "RFIC_TEMP")
{
float val(0);
int ret = bladerf_get_rfic_temperature(_dev, &val);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_rfic_temperature() returned %s", _err2str(ret).c_str());
throw std::runtime_error("readSensor("+key+") " + _err2str(ret));
}
return std::to_string(val);
}
else throw std::runtime_error("readSensor(" + key + ") unknown sensor");
}
std::vector<std::string> bladeRF_SoapySDR::listSensors(const int direction, const size_t channel) const
{
std::vector<std::string> sensors;
if (_isBladeRF2 and direction == SOAPY_SDR_RX) sensors.push_back("PRE_RSSI");
if (_isBladeRF2 and direction == SOAPY_SDR_RX) sensors.push_back("SYM_RSSI");
return sensors;
}
SoapySDR::ArgInfo bladeRF_SoapySDR::getSensorInfo(const int direction, const size_t, const std::string &key) const
{
if (key == "PRE_RSSI" and direction == SOAPY_SDR_RX)
{
SoapySDR::ArgInfo info;
info.key = key;
info.value = "0";
info.name = "Preamble RSSI";
info.description = "Preamble RSSI in dB (first calculated RSSI result)";
info.units = "dB";
info.type = SoapySDR::ArgInfo::FLOAT;
return info;
}
else if (key == "SYM_RSSI" and direction == SOAPY_SDR_RX)
{
SoapySDR::ArgInfo info;
info.key = key;
info.value = "0";
info.name = "Symbol RSSI";
info.description = "Symbol RSSI in dB (most recent RSSI result)";
info.units = "dB";
info.type = SoapySDR::ArgInfo::FLOAT;
return info;
}
else throw std::runtime_error("getSensorInfo(" + key + ") unknown sensor");
}
std::string bladeRF_SoapySDR::readSensor(const int direction, const size_t channel, const std::string &key) const
{
if (key == "PRE_RSSI" or key == "SYM_RSSI")
{
int32_t pre_rssi(0), sym_rssi(0);
int ret = bladerf_get_rfic_rssi(_dev, _toch(direction, channel), &pre_rssi, &sym_rssi);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_get_rfic_rssi() returned %s", _err2str(ret).c_str());
throw std::runtime_error("readSensor("+key+") " + _err2str(ret));
}
return std::to_string((key[0] == 'P')?pre_rssi:sym_rssi);
}
else throw std::runtime_error("readSensor(" + key + ") unknown sensor");
}
/*******************************************************************
* Register API
******************************************************************/
std::vector<std::string> bladeRF_SoapySDR::listRegisterInterfaces(void) const
{
std::vector<std::string> ifaces;
if (_isBladeRF1) ifaces.push_back("LMS");
if (_isBladeRF2) ifaces.push_back("RFIC");
return ifaces;
}
void bladeRF_SoapySDR::writeRegister(const std::string &name, const unsigned addr, const unsigned value)
{
if (name == "LMS")
{
const int ret = bladerf_lms_write(_dev, uint8_t(addr), uint8_t(value));
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_lms_write(0x%x) returned %s", addr, _err2str(ret).c_str());
throw std::runtime_error("writeRegister() " + _err2str(ret));
}
}
else if (name == "RFIC")
{
const int ret = bladerf_set_rfic_register(_dev, uint16_t(addr), uint8_t(value));
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_set_rfic_register(0x%x) returned %s", addr, _err2str(ret).c_str());
throw std::runtime_error("writeRegister() " + _err2str(ret));
}
}
else throw std::runtime_error("writeRegister(" + name + ") unknown register interface");
}
unsigned bladeRF_SoapySDR::readRegister(const std::string &name, const unsigned addr) const
{
if (name == "LMS")
{
uint8_t value = 0;
const int ret = bladerf_lms_read(_dev, uint8_t(addr), &value);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_lms_read(0x%x) returned %s", addr, _err2str(ret).c_str());