-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSettings.cpp
More file actions
2213 lines (1913 loc) · 69.6 KB
/
Copy pathSettings.cpp
File metadata and controls
2213 lines (1913 loc) · 69.6 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
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
#include "SoapySidekiq.hpp"
#include <SoapySDR/Formats.hpp>
#include <cstring>
#include <cinttypes>
#include <iostream>
#include <vector>
#include <string>
#include <sidekiq_types.h>
#include <unistd.h>
/******************************************************************************/
/** This is the custom logging handler. If there were custom handling
required for logging messages, it should be handled here.
@param signum: the signal number that occurred
@return void
*/
void logging_handler( int32_t priority, const char *message )
{
//printf("<PRIORITY %" PRIi32 "> custom logger: %s", priority, message);
char* new_message = (char *)malloc(strlen(message) + 1);
strcpy(new_message, message);
// remove newline and or cr
size_t len = strlen(new_message); // Get the length of the string
if (len > 0 && new_message[len - 1] == '\n') {
new_message[len - 1] = '\0'; // Replace newline with null terminator
}
len = strlen(new_message); // Get the length of the string
//
if (len > 0 && new_message[len - 1] == '\r') {
new_message[len - 1] = '\0'; // Replace newline with null terminator
}
switch (priority)
{
case SKIQ_LOG_DEBUG:
SoapySDR_logf(SOAPY_SDR_DEBUG, "epiq-log: %s", new_message);
break;
case SKIQ_LOG_INFO:
SoapySDR_logf(SOAPY_SDR_INFO, "epiq-log: %s", new_message);
break;
case SKIQ_LOG_WARNING:
SoapySDR_logf(SOAPY_SDR_WARNING, "epiq-log: %s", new_message);
break;
case SKIQ_LOG_ERROR:
SoapySDR_logf(SOAPY_SDR_ERROR, "epiq-log: %s", new_message);
break;
default:
SoapySDR_logf(SOAPY_SDR_TRACE, "epiq-log undefined %s", new_message);
}
free(new_message);
}
/*****************************************************************************/
/** This is the callback function for once the data has completed being sent.
There is no guarantee that the complete callback will be in the order that
the data was sent, this function just increments the completion count and
signals the main thread that there is space available to send more packets.
@param status status of the transmit packet completed
@param p_block reference to the completed transmit block
@param p_user reference to the user data
@return void
*/
void SoapySidekiq::tx_complete(int32_t status, skiq_tx_block_t *p_data, uint32_t txIndex)
{
this->complete_count++;
// update the in use status of the packet just completed
tx_buf_mutex.lock();
if (p_tx_status[txIndex] != 1)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "status isn't 1");
}
p_tx_status[txIndex] = 0;
tx_buf_mutex.unlock();
// signal to the other thread that there may be space available now that a
// packet send has completed
{
// Signal the condition variable
pthread_mutex_lock(&space_avail_mutex);
space_avail = true;
pthread_cond_signal(&space_avail_cond);
pthread_mutex_unlock(&space_avail_mutex);
}
// SoapySDR_logf(SOAPY_SDR_TRACE, "leaving tx_complete");
}
void SoapySidekiq::tx_enabled(uint8_t card, int32_t status)
{
SoapySDR_logf(SOAPY_SDR_TRACE, "tx enable received");
// Signal the condition variable
pthread_mutex_lock(&tx_enabled_mutex);
pthread_cond_signal(&tx_enabled_cond);
pthread_mutex_unlock(&tx_enabled_mutex);
}
std::vector<SoapySDR::Kwargs> SoapySidekiq::sidekiq_devices;
bool SoapySidekiq::rx_running;
// compares two strings and if equal range and equal values per character
// returns true.
bool equalsIgnoreCase(const std::string& a, const std::string& b)
{
return std::equal(a.begin(), a.end(), b.begin(), b.end(),
[](char a, char b)
{
return std::tolower(a) == std::tolower(b);
});
}
// Constructor
SoapySidekiq::SoapySidekiq(const SoapySDR::Kwargs &args)
{
int status = 0;
uint8_t channels = 0;
skiq_iq_order_t iq_order;
int i;
/* Register our own logging function before initializing the library */
skiq_register_logging( logging_handler );
SoapySDR_logf(SOAPY_SDR_TRACE, "in constructor", card);
/* We need to set some default parameters in case the user does not */
rxUseShort = true;
txUseShort = true;
iq_swap = true;
counter = false;
debug_ctr = 0;
card = 0;
rx_block_size_in_words = 0;
rx_block_size_in_bytes = 0;
rx_payload_size_in_words = 1018;
rx_payload_size_in_bytes = 0;
rfTimeSource = true;
timetype = "rf_timestamp";
complete_count = 0;
rx_running = false;
if (args.count("card") != 0)
{
try
{
card = std::stoi(args.at("card"));
}
catch (const std::invalid_argument &)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "Requested card not found");
throw std::runtime_error("");
}
}
else
{
SoapySDR_logf(SOAPY_SDR_ERROR, "No cards found");
throw std::runtime_error("");
}
if (args.count("tx_block_size") != 0)
{
current_tx_block_size = std::stoi(args.at("tx_block_size"));
}
else
{
current_tx_block_size = DEFAULT_TX_BUFFER_LENGTH;
}
SoapySDR_logf(SOAPY_SDR_INFO, "TX block size set to %u", current_tx_block_size);
/* set the source to what is passed in */
if (args.count("clock_source") > 0)
{
setClockSource(args.at("clock_source"));
}
if (args.count("time_source") > 0)
{
setTimeSource(args.at("time_source"));
}
rx_hdl = skiq_rx_hdl_A1;
tx_hdl = skiq_tx_hdl_A1;
skiq_xport_type_t type = skiq_xport_type_auto;
skiq_xport_init_level_t level = skiq_xport_init_level_full;
SoapySDR_logf(SOAPY_SDR_INFO, "Sidekiq opening card %u", card);
/* init sidekiq */
status = skiq_init(type, level, &card, 1);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_init failed (card %u), status %d",
card, status);
throw std::runtime_error("");
}
status = skiq_write_chan_mode(card, skiq_chan_mode_single);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_write_chan_mode failed, card %u, status %d",
card, status);
throw std::runtime_error("");
}
SoapySDR_logf(SOAPY_SDR_TRACE, "channel mode set to single");
/* set default sample_rate and bandwidth */
this->rx_sample_rate = DEFAULT_SAMPLE_RATE;
this->tx_sample_rate = DEFAULT_SAMPLE_RATE;
setSampleRate(SOAPY_SDR_RX, DEFAULT_CHANNEL, static_cast<uint32_t>(this->rx_sample_rate));
setSampleRate(SOAPY_SDR_TX, DEFAULT_CHANNEL, static_cast<uint32_t>(this->tx_sample_rate));
this->rx_bandwidth = DEFAULT_BANDWIDTH;
this->tx_bandwidth = DEFAULT_BANDWIDTH;
setBandwidth(SOAPY_SDR_RX, DEFAULT_CHANNEL, static_cast<uint32_t>(this->rx_bandwidth));
setBandwidth(SOAPY_SDR_TX, DEFAULT_CHANNEL, static_cast<uint32_t>(this->tx_bandwidth));
/* set default frequency */
this->rx_center_frequency = DEFAULT_FREQUENCY;
setFrequency(SOAPY_SDR_RX, DEFAULT_CHANNEL, static_cast<uint64_t>(this->rx_center_frequency));
this->tx_center_frequency = DEFAULT_FREQUENCY;
setFrequency(SOAPY_SDR_TX, DEFAULT_CHANNEL, static_cast<uint64_t>(this->rx_center_frequency));
if (args.count("clock_source") > 0)
{
setClockSource(args.at("clock_source"));
}
if (args.count("time_source") > 0)
{
setTimeSource(args.at("time_source"));
}
status = skiq_read_parameters(card, &this->param);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_read_parameters failed, card %u, status %d",
card, status);
throw std::runtime_error("");
}
part = param.card_param.part_type;
part_str = skiq_part_string(part);
SoapySDR_logf(SOAPY_SDR_INFO, "card: %u, part type is %s", card, part_str.c_str());
/* set iq order to iq instead of qi */
if (iq_swap == true)
{
SoapySDR_logf(SOAPY_SDR_INFO, "card: %u, setting iq mode to I then Q", this->card);
iq_order = skiq_iq_order_iq;
}
else
{
SoapySDR_logf(SOAPY_SDR_INFO, "card: %u, setting iq mode to Q then I", this->card);
iq_order = skiq_iq_order_qi;
}
status = skiq_write_iq_order_mode(card, iq_order);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_write_iq_order_mode failed, (card %u), status %d",
card, status);
throw std::runtime_error("");
}
status = skiq_read_num_rx_chans(card, &channels);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_read_num_rx_chans failed, (card %u), status %d",
card, status);
throw std::runtime_error("");
}
num_rx_channels = channels;
status = skiq_read_num_tx_chans(card, &channels);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_read_num_tx_chans failed, (card %u), status %d",
card, status);
throw std::runtime_error("");
}
num_tx_channels = channels;
uint8_t tmp_resolution = 0;
/* Every card can have a different iq resolution. */
status = skiq_read_rx_iq_resolution(card, &tmp_resolution);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_read_rx_iq_resolution failed, "
"card: %u status: %d",
card, status);
throw std::runtime_error("");
}
this->resolution = tmp_resolution;
this->maxValue = (double) ((1 << (tmp_resolution-1))-1);
SoapySDR_logf(SOAPY_SDR_INFO, "card: %u, card resolution: %u bits, max ADC value: %u",
card, this->resolution, (uint32_t) this->maxValue);
// allocate for # blocks
p_tx_status = static_cast<int32_t*>(calloc(DEFAULT_NUM_BUFFERS, sizeof(*p_tx_status)));
if (p_tx_status == NULL)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "failed to allocate memory for TX status");
throw std::runtime_error("");
}
for (i = 0; i < DEFAULT_NUM_BUFFERS; i++)
{
p_tx_status[i] = 0;
}
// register the transmit complete callback
status = skiq_register_tx_complete_callback(card,
&SoapySidekiq::static_tx_complete_callback);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_register_tx_complete_callback failed, "
"card: %u status: %d",
card, status);
throw std::runtime_error("");
}
pthread_mutex_init(&space_avail_mutex, nullptr);
pthread_cond_init(&space_avail_cond, nullptr);
// register the transmit enabled callback
status = skiq_register_tx_enabled_callback(card,
&SoapySidekiq::static_tx_enabled_callback);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_register_tx_enabled_callback failed, "
"card: %u status: %d",
card, status);
throw std::runtime_error("");
}
pthread_mutex_init(&tx_enabled_mutex, nullptr);
pthread_cond_init(&tx_enabled_cond, nullptr);
SoapySDR_logf(SOAPY_SDR_TRACE, "leaving constructor", card);
}
// Destructor
SoapySidekiq::~SoapySidekiq(void)
{
SoapySDR_logf(SOAPY_SDR_TRACE, "In destructor", card);
if (NULL != p_tx_status)
{
free(p_tx_status);
p_tx_status = NULL;
}
skiq_exit();
}
/*******************************************************************
* Identification API
******************************************************************/
std::string SoapySidekiq::getDriverKey(void) const
{
SoapySDR_logf(SOAPY_SDR_TRACE, "getDriverKey");
return "Sidekiq";
}
std::string SoapySidekiq::getHardwareKey(void) const
{
SoapySDR_logf(SOAPY_SDR_TRACE, "getHardwareKey");
return part_str;
}
SoapySDR::Kwargs SoapySidekiq::getHardwareInfo(void) const
{
// key/value pairs for any useful information
// this also gets printed in --probe
SoapySDR::Kwargs args;
SoapySDR_logf(SOAPY_SDR_TRACE, "getHardwareInfo");
args["origin"] = "https://github.com/epiqsolutions/SoapySidekiq";
args["card_type"] = part_str;
args["card"] = std::to_string(card);
args["serial"] = serial;
args["rx_channels"] = std::to_string(num_rx_channels);
args["tx_channels"] = std::to_string(num_tx_channels);
return args;
}
/*******************************************************************
* Channels API
******************************************************************/
size_t SoapySidekiq::getNumChannels(const int dir) const
{
SoapySDR_logf(SOAPY_SDR_TRACE, "getNumChannels");
if (dir == SOAPY_SDR_RX)
{
return num_rx_channels;
}
else if (dir == SOAPY_SDR_TX)
{
return num_tx_channels;
}
else
{
SoapySDR_logf(SOAPY_SDR_ERROR, "invalid direction %d", dir);
throw std::runtime_error("");
}
return -1;
}
/*******************************************************************
* Antenna API
******************************************************************/
std::vector<std::string> SoapySidekiq::listAntennas(const int direction, const size_t channel) const {
std::vector<std::string> antennas;
SoapySDR_logf(SOAPY_SDR_TRACE, "listAntennas");
if (direction == SOAPY_SDR_RX)
{
if (channel >= skiq_rx_hdl_end)
{
antennas.push_back("NONE");
}
else
{
if (this->param.rx_param[channel].num_trx_rf_ports > 0)
{
antennas.push_back("TRX");
}
else if (this->param.rx_param[channel].num_fixed_rf_ports > 0)
{
antennas.push_back("RX");
}
else
{
antennas.push_back("NONE");
}
}
}
else if (direction == SOAPY_SDR_TX)
{
if (channel >= skiq_tx_hdl_end)
{
antennas.push_back("NONE");
}
else
{
if (this->param.tx_param[channel].num_trx_rf_ports > 0)
{
antennas.push_back("TRX");
}
else if (this->param.tx_param[channel].num_fixed_rf_ports > 0)
{
antennas.push_back("TX");
}
else
{
antennas.push_back("NONE");
}
}
}
return antennas;
}
/*******************************************************************
* Frontend corrections API
******************************************************************/
bool SoapySidekiq::hasDCOffsetMode(const int direction,
const size_t channel) const
{
int status = 0;
uint32_t mask = 0;
SoapySDR_logf(SOAPY_SDR_TRACE, "hasDCOffsetMode");
if (direction == SOAPY_SDR_RX)
{
status = skiq_read_rx_cal_types_avail(card, this->rx_hdl, &mask);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_read_rx_rx_cal_types_avail failed, "
"(card %u, handle %u, mask %d), status %d",
card, rx_hdl, mask, status);
throw std::runtime_error("");
}
if ((mask & skiq_rx_cal_type_dc_offset) == skiq_rx_cal_type_dc_offset)
{
SoapySDR_logf(SOAPY_SDR_INFO, "card: %u, handle %u, has DC Offest correction, "
"mask is: %d",
card, this->rx_hdl, mask);
return true;
}
else
{
SoapySDR_logf(SOAPY_SDR_INFO, "card: %u, handle %u, does not have DC Offest correction, "
"mask is: %d",
card, this->rx_hdl, mask);
return false;
}
}
else if (direction == SOAPY_SDR_TX)
{
// Tx has quadcal only
SoapySDR_logf(SOAPY_SDR_WARNING, "TX does not have DC offset mode");
}
else
{
SoapySDR_logf(SOAPY_SDR_ERROR, "invalid Direction %d", direction);
throw std::runtime_error("");
}
return false;
}
void SoapySidekiq::setDCOffsetMode(const int direction,
const size_t channel,
const bool automatic)
{
int status = 0;
SoapySDR_logf(SOAPY_SDR_TRACE, "setDCOffsetMode");
if (direction == SOAPY_SDR_RX)
{
this->rx_hdl = static_cast<skiq_rx_hdl_t>(channel);
skiq_rx_cal_mode_t cal_mode = skiq_rx_cal_mode_auto;
// if automatic is false, then we need to set the mode to manual
// otherwise set the mode to automatic
if (automatic == false)
{
cal_mode = skiq_rx_cal_mode_manual;
}
status = skiq_write_rx_cal_mode(card, this->rx_hdl, cal_mode);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_write_rx_cal_mode failed "
"(card %u, cal_mode %d), status %d",
card, cal_mode, status);
throw std::runtime_error("");
}
SoapySDR_logf(SOAPY_SDR_INFO, "channel: %u, setting DC offset correction to %s mode",
channel, (bool)cal_mode ? "automatic" : "manual");
}
else if (direction == SOAPY_SDR_TX)
{
// Tx has quadcal only
SoapySDR_logf(SOAPY_SDR_WARNING, "TX does not have DC offset mode");
}
else
{
SoapySDR_logf(SOAPY_SDR_ERROR, "invalid direction %d", direction);
throw std::runtime_error("");
}
}
bool SoapySidekiq::getDCOffsetMode(const int direction,
const size_t channel) const
{
skiq_rx_cal_mode_t cal_mode;
int status = 0;
SoapySDR_logf(SOAPY_SDR_TRACE, "getDCOffsetMode");
if (direction == SOAPY_SDR_RX)
{
status = skiq_read_rx_cal_mode(card, this->rx_hdl, &cal_mode);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_read_rx_cal_mode failure "
"(card %u, mode %d), status %d",
card, cal_mode, status);
throw std::runtime_error("");
}
SoapySDR_logf(SOAPY_SDR_INFO, "Channel %d, DC offest mode is %s",
channel, (bool)cal_mode ? "automatic" : "manual");
}
else if (direction == SOAPY_SDR_TX)
{
// Tx has quadcal only
SoapySDR_logf(SOAPY_SDR_WARNING, "TX does not have DC offset mode");
return false;
}
else
{
SoapySDR_logf(SOAPY_SDR_ERROR, "invalid direction %d", direction);
throw std::runtime_error("");
}
return (bool)cal_mode;
}
/*******************************************************************
* Gain API
******************************************************************/
std::vector<std::string> SoapySidekiq::listGains(const int direction, const size_t channel) const {
// list available gain elements,
std::vector<std::string> results;
SoapySDR_logf(SOAPY_SDR_TRACE, "listGains");
results.push_back("LNA");
return results;
}
// the Gain API is called for tx attenuation too.
bool SoapySidekiq::hasGainMode(const int direction, const size_t channel) const
{
SoapySDR_logf(SOAPY_SDR_TRACE, "hasGainMode");
// all Sidekiq cards have rx gain mode and tx attenuation mode
return true;
}
void SoapySidekiq::setGainMode(const int direction, const size_t channel,
const bool automatic)
{
int status = 0;
SoapySDR_logf(SOAPY_SDR_TRACE, "setGainMode");
if (direction == SOAPY_SDR_RX)
{
this->rx_hdl = static_cast<skiq_rx_hdl_t>(channel);
skiq_rx_gain_t mode =
automatic ? skiq_rx_gain_auto : skiq_rx_gain_manual;
status = skiq_write_rx_gain_mode(card, this->rx_hdl, mode);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR, "skiq_write_rx_gain_mode failed "
"(card %u, channel %d, mode %d), status %d",
card, channel, mode, status);
throw std::runtime_error("");
}
SoapySDR_logf(SOAPY_SDR_INFO, "card: %u, handle: %u, setting RX gain mode: %s",
card, this->rx_hdl, automatic ? "skiq_rx_gain_auto" :
"skiq_rx_gain_manual");
}
else if (direction == SOAPY_SDR_TX)
{
// Tx has no mode
SoapySDR_logf(SOAPY_SDR_WARNING, "TX does not have an attenuation mode");
}
else
{
SoapySDR_logf(SOAPY_SDR_ERROR, "invalid direction %d", direction);
throw std::runtime_error("");
}
}
bool SoapySidekiq::getGainMode(const int direction, const size_t channel) const
{
int status = 0;
SoapySDR_logf(SOAPY_SDR_TRACE, "getGainMode");
if (direction == SOAPY_SDR_RX)
{
skiq_rx_gain_t p_gain_mode;
status = skiq_read_rx_gain_mode(card, this->rx_hdl, &p_gain_mode);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR,
"skiq_read_rx_gain_mode failed, (card %u), status %d",
card, status);
throw std::runtime_error("");
}
SoapySDR_logf(SOAPY_SDR_INFO, "card: %u, handle: %u, RX gain mode: is %s",
card, this->rx_hdl, p_gain_mode ? "skiq_rx_gain_auto" :
"skiq_rx_gain_manual");
return p_gain_mode;
}
else if (direction == SOAPY_SDR_TX)
{
// Tx has no mode
SoapySDR_logf(SOAPY_SDR_WARNING, "TX does not have an attenuation mode");
}
else
{
SoapySDR_logf(SOAPY_SDR_ERROR, "invalid direction %d", direction);
throw std::runtime_error("");
}
return false;
}
void SoapySidekiq::setGain(const int direction,
const size_t channel,
const std::string &name,
const double value)
{
setGain(direction, channel, value);
}
void SoapySidekiq::setGain(const int direction,
const size_t channel,
const double value)
{
int status = 0;
SoapySDR_logf(SOAPY_SDR_TRACE, "setGain called with direction %d, channel %zu, value %.1f", direction, channel, value);
if (direction == SOAPY_SDR_RX)
{
this->rx_hdl = static_cast<skiq_rx_hdl_t>(channel);
// 1. Read current gain mode
skiq_rx_gain_t gain_mode;
status = skiq_read_rx_gain_mode(card, this->rx_hdl, &gain_mode);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR,
"skiq_read_rx_gain_mode failed (card %u, channel %zu), status %d",
card, channel, status);
throw std::runtime_error("");
}
// 2. Force manual mode if currently in auto
if (gain_mode == skiq_rx_gain_auto)
{
SoapySDR_logf(SOAPY_SDR_INFO,
"Gain mode was auto, switching to manual for explicit gain setting.");
status = skiq_write_rx_gain_mode(card, this->rx_hdl, skiq_rx_gain_manual);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR,
"skiq_write_rx_gain_mode failed (card %u, channel %zu), status %d",
card, channel, status);
throw std::runtime_error("");
}
}
// 3. Query gain index range from hardware
uint8_t gain_min = 0, gain_max = 0;
status = skiq_read_rx_gain_index_range(card, this->rx_hdl, &gain_min, &gain_max);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR,
"skiq_read_rx_gain_index_range failed (card %u, channel %zu), status %d",
card, channel, status);
throw std::runtime_error("");
}
// 4. Map value in dB to hardware index per device type
uint8_t gain_index = gain_min;
switch (part)
{
case skiq_mpcie:
case skiq_m2:
case skiq_m2_2280:
case skiq_z2:
case skiq_z3u:
gain_index = (uint8_t)std::round(value); // 1dB/step, 0..76
break;
case skiq_x4:
case skiq_x40:
case skiq_x2:
gain_index = (uint8_t)(195 + std::round(value * 2.0)); // 0.5dB/step, starts at 195
break;
case skiq_nv100:
case skiq_nvm2:
gain_index = (uint8_t)(187 + std::round(value * 2.0)); // 0.5dB/step, starts at 187
break;
default:
SoapySDR_logf(SOAPY_SDR_WARNING,
"Unknown card type: %u. Not setting gain.",
(uint8_t)part);
return;
}
// 5. Clamp gain index to allowed range
if (gain_index < gain_min) gain_index = gain_min;
if (gain_index > gain_max) gain_index = gain_max;
SoapySDR_logf(SOAPY_SDR_INFO,
"card: %u, handle: %u, Set RX gain: requested %.1f dB (gain_index: %u),"
" range: [%u-%u], set: %u",
card, this->rx_hdl, value, gain_index,
gain_min, gain_max, gain_index);
// 6. Actually set the gain
status = skiq_write_rx_gain(card, rx_hdl, gain_index);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR,
"skiq_write_rx_gain failed (card %u, gain_index %u), status %d",
card, gain_index, status);
throw std::runtime_error("");
}
}
else if (direction == SOAPY_SDR_TX)
{
uint16_t attenuation_index = 0;
uint32_t max_attenuation_index = this->param.tx_param[tx_hdl].atten_quarter_db_max;
switch (part)
{
case skiq_mpcie:
case skiq_m2:
case skiq_m2_2280:
case skiq_z2:
case skiq_z3u:
if ((value < 0) || (value > 89.75))
{
SoapySDR_logf(SOAPY_SDR_WARNING,
"card: %u, invalid requested attenuation: %3.0f dB,"
"acceptable range: 0 - 89.75 dB. No attenuation configured.",
card, value);
return;
}
attenuation_index = max_attenuation_index - (uint16_t)std::round(value * 4.0);
break;
case skiq_x4:
case skiq_x40:
case skiq_x2:
case skiq_nv100:
case skiq_nvm2:
if ((value < 0) || (value > 41.75))
{
SoapySDR_logf(SOAPY_SDR_WARNING,
"card: %u, invalid requested attenuation: %3.0f dB, acceptable range: 0 - 41.75 dB. No attenuation configured.",
card, value);
return;
}
attenuation_index = max_attenuation_index - (uint16_t)std::round(value * 4.0);
break;
default:
SoapySDR_logf(SOAPY_SDR_WARNING, "Unknown card type: %u. Not setting TX attenuation.", (uint8_t)part);
return;
}
status = skiq_write_tx_attenuation(card, tx_hdl, attenuation_index);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR,
"skiq_write_tx_gain failed, (card %u, value %f), status %d",
card, attenuation_index, status);
throw std::runtime_error("");
}
SoapySDR_logf(SOAPY_SDR_INFO, "card: %u, Setting tx attenuation: %2.2f dB, attenuation index: %d",
card, value, attenuation_index);
}
else
{
SoapySDR_logf(SOAPY_SDR_ERROR, "invalid direction %d", direction);
throw std::runtime_error("");
}
}
double SoapySidekiq::getGain(const int direction, const size_t channel, const std::string &name) const
{
return getGain(direction, channel);
}
double SoapySidekiq::getGain(const int direction, const size_t channel) const
{
int status = 0;
SoapySDR_logf(SOAPY_SDR_TRACE, "getGain, direction: %d channel: %zu", direction, channel);
if (direction == SOAPY_SDR_RX)
{
uint8_t gain_index;
status = skiq_read_rx_gain(card, static_cast<skiq_rx_hdl_t>(channel), &gain_index);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR,
"skiq_read_rx_gain failed (card %u), rx_hdl %zu, status %d",
card, channel, status);
throw std::runtime_error("");
}
switch (part)
{
case skiq_mpcie:
case skiq_m2:
case skiq_m2_2280:
case skiq_z2:
case skiq_z3u:
return static_cast<double>(gain_index); // 1dB/step
case skiq_x2:
case skiq_x4:
case skiq_x40:
return static_cast<double>(gain_index - 195) / 2.0; // 0.5dB/step
case skiq_nv100:
case skiq_nvm2:
return static_cast<double>(gain_index - 187) / 2.0;
default:
SoapySDR_logf(SOAPY_SDR_WARNING,
"card: %u, invalid card type %u",
card, (uint8_t)part);
break;
}
}
else if (direction == SOAPY_SDR_TX)
{
uint16_t attenuation_index = 0;
uint32_t max_attenuation_index = this->param.tx_param[tx_hdl].atten_quarter_db_max;
status = skiq_read_tx_attenuation(card,
static_cast<skiq_tx_hdl_t>(channel),
&attenuation_index);
if (status != 0)
{
SoapySDR_logf(SOAPY_SDR_ERROR,
"skiq_read_tx_attenuation failed (card %u), status %d",
card, status);
throw std::runtime_error("");
}
return (max_attenuation_index - static_cast<int>(attenuation_index)) / 4.0;
}
else
{
SoapySDR_logf(SOAPY_SDR_ERROR, "invalid direction %d", direction);
throw std::runtime_error("");
}
return 0;
}
SoapySDR::Range SoapySidekiq::getGainRange(const int direction,
const size_t channel,
const std::string & name) const
{
SoapySDR_log(SOAPY_SDR_TRACE, "getGainRange with name");
return getGainRange(direction, channel);
}
SoapySDR::Range SoapySidekiq::getGainRange(const int direction,
const size_t channel) const
{
SoapySDR_log(SOAPY_SDR_TRACE, "getGainRange");
if (direction == SOAPY_SDR_RX)
{
double gain_min = 0;
double gain_max = 0;
double step = 0;
// convert index to dB based upon the card type
switch (part)
{
// 0 to 76 [0 to 76 dB, 1 dB/step]
case skiq_mpcie:
case skiq_m2:
case skiq_m2_2280:
case skiq_z2:
case skiq_z3u:
gain_min = 0;
gain_max = 76;
step = 1;
break;
// 195 to 255 [0 to 30 dB, 0.5 dB/step]