aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/usrp/dboard/rhodium/rhodium_radio_ctrl_lo.cpp
blob: 8567229d8b9e1ca350f331a392c83b0bd5b21371 (plain)
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
//
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include "rhodium_radio_ctrl_impl.hpp"
#include "rhodium_constants.hpp"
#include <uhdlib/utils/narrow.hpp>
#include <uhd/utils/log.hpp>
#include <uhd/utils/algorithm.hpp>
#include <uhd/types/direction.hpp>
#include <uhd/exception.hpp>
#include <boost/format.hpp>

using namespace uhd;
using namespace uhd::usrp;
using namespace uhd::rfnoc;

namespace {
    constexpr int NUM_THRESHOLDS = 13;
    constexpr std::array<double, NUM_THRESHOLDS> FREQ_THRESHOLDS =
            {0.45e9, 0.5e9, 1e9, 1.5e9, 2e9, 2.5e9, 3e9, 3.55e9, 4e9, 4.5e9, 5e9, 5.5e9, 6e9};
    constexpr std::array<int, NUM_THRESHOLDS> LMX_GAIN_VALUES =
            {18, 18, 17, 17, 17, 16, 12, 11, 11, 11, 10, 13, 18};
    const std::array<int, NUM_THRESHOLDS> DSA_RX_GAIN_VALUES =
            {19, 19, 21, 21, 20, 20, 22, 21, 20, 22, 22, 24, 26};
    const std::array<int, NUM_THRESHOLDS> DSA_TX_GAIN_VALUES =
            {19, 19, 21, 21, 20, 20, 22, 21, 22, 24, 24, 26, 28};

    // Describes the lowband LO in terms of the master clock rate
    const std::map<double, double> MCR_TO_LOWBAND_IF = {
        {200e6,    1200e6},
        {245.76e6, 1228.8e6},
        {250e6,    1500e6},
    };

    // validation helpers

    std::vector<std::string> _get_lo_names()
    {
        return { RHODIUM_LO1, RHODIUM_LO2 };
    }

    void _validate_lo_name(const std::string& name, const std::string& function_name)
    {
        if (!uhd::has(_get_lo_names(), name) and name != radio_ctrl::ALL_LOS) {
            throw uhd::value_error(str(boost::format(
                "%s was called with an invalid LO name: %s")
                % function_name
                % name));
        }
    }

    // object agnostic helpers
    std::vector<std::string> _get_lo_sources(const std::string& name)
    {
        if (name == RHODIUM_LO1 or name == radio_ctrl::ALL_LOS) {
            return { "internal", "external" };
        } else {
            return { "internal" };
        }
    }
}

/******************************************************************************
 * Property Getters
 *****************************************************************************/

std::vector<std::string> rhodium_radio_ctrl_impl::get_tx_lo_names(
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_tx_lo_names(chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);

    return _get_lo_names();
}

std::vector<std::string> rhodium_radio_ctrl_impl::get_rx_lo_names(
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_rx_lo_names(chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);

    return _get_lo_names();
}

std::vector<std::string> rhodium_radio_ctrl_impl::get_tx_lo_sources(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_tx_lo_sources(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_tx_lo_sources");

    return _get_lo_sources(name);
}

std::vector<std::string> rhodium_radio_ctrl_impl::get_rx_lo_sources(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_rx_lo_sources(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_rx_lo_sources");

    return _get_lo_sources(name);
}

freq_range_t rhodium_radio_ctrl_impl::_get_lo_freq_range(const std::string &name) const
{
    if (name == RHODIUM_LO1) {
        return freq_range_t{ RHODIUM_LO1_MIN_FREQ, RHODIUM_LO1_MAX_FREQ };
    } else if (name == RHODIUM_LO2) {
        // The Lowband LO is a fixed frequency
        return freq_range_t{ _get_lowband_lo_freq(), _get_lowband_lo_freq() };
    } else {
        throw uhd::runtime_error(
            "LO frequency range must be retrieved for each stage individually");
    }
}

freq_range_t rhodium_radio_ctrl_impl::get_tx_lo_freq_range(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_tx_lo_freq_range(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_tx_lo_freq_range");

    return _get_lo_freq_range(name);
}

freq_range_t rhodium_radio_ctrl_impl::get_rx_lo_freq_range(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_rx_lo_freq_range(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_rx_lo_freq_range");

    return _get_lo_freq_range(name);
}

/******************************************************************************
 * Frequency Control
 *****************************************************************************/

double rhodium_radio_ctrl_impl::set_tx_lo_freq(
    const double freq,
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "set_tx_lo_freq(freq=" << freq << ", name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "set_tx_lo_freq");

    if (name == ALL_LOS) {
        throw uhd::runtime_error("LO frequency must be set for each stage individually");
    }
    if (name == RHODIUM_LO2) {
        UHD_LOG_WARNING(unique_id(), "The Lowband LO cannot be tuned");
        return _get_lowband_lo_freq();
    }

    const auto sd_enabled = _get_spur_dodging_enabled(TX_DIRECTION);
    const auto sd_threshold = _get_spur_dodging_threshold(TX_DIRECTION);

    _tx_lo_freq = _tx_lo->set_frequency(freq, sd_enabled, sd_threshold);
    set_tx_lo_gain(_get_lo_dsa_setting(_tx_lo_freq, TX_DIRECTION), RHODIUM_LO1, chan);
    set_tx_lo_power(_get_lo_power_setting(_tx_lo_freq), RHODIUM_LO1, chan);
    _cpld->set_tx_lo_path(_tx_lo_freq);

    return _tx_lo_freq;
}

double rhodium_radio_ctrl_impl::set_rx_lo_freq(
    const double freq,
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "set_rx_lo_freq(freq=" << freq << ", name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "set_rx_lo_freq");

    if (name == ALL_LOS) {
        throw uhd::runtime_error("LO frequency must be set for each stage individually");
    }
    if (name == RHODIUM_LO2) {
        UHD_LOG_WARNING(unique_id(), "The Lowband LO cannot be tuned");
        return _get_lowband_lo_freq();
    }

    const auto sd_enabled = _get_spur_dodging_enabled(RX_DIRECTION);
    const auto sd_threshold = _get_spur_dodging_threshold(RX_DIRECTION);

    _rx_lo_freq = _rx_lo->set_frequency(freq, sd_enabled, sd_threshold);
    set_rx_lo_gain(_get_lo_dsa_setting(_rx_lo_freq, RX_DIRECTION), RHODIUM_LO1, chan);
    set_rx_lo_power(_get_lo_power_setting(_rx_lo_freq), RHODIUM_LO1, chan);
    _cpld->set_rx_lo_path(_rx_lo_freq);

    return _rx_lo_freq;
}

double rhodium_radio_ctrl_impl::get_tx_lo_freq(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_tx_lo_freq(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_tx_lo_freq");

    if (name == ALL_LOS) {
        throw uhd::runtime_error(
            "LO frequency must be retrieved for each stage individually");
    }

    return (name == RHODIUM_LO1) ? _tx_lo_freq : _get_lowband_lo_freq();
}

double rhodium_radio_ctrl_impl::get_rx_lo_freq(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_rx_lo_freq(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_rx_lo_freq");

    if (name == ALL_LOS) {
        throw uhd::runtime_error(
            "LO frequency must be retrieved for each stage individually");
    }

    return (name == RHODIUM_LO1) ? _rx_lo_freq : _get_lowband_lo_freq();
}

/******************************************************************************
 * Source Control
 *****************************************************************************/

void rhodium_radio_ctrl_impl::set_tx_lo_source(
    const std::string& src,
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "set_tx_lo_source(src=" << src << ", name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "set_tx_lo_source");

    if (name == RHODIUM_LO2) {
        if (src != "internal") {
            throw uhd::value_error("The Lowband LO can only be set to internal");
        }
        return;
    }

    if (src == "internal") {
        _tx_lo->set_output_enable(lmx2592_iface::output_t::RF_OUTPUT_A, true);
        _cpld->set_tx_lo_source(rhodium_cpld_ctrl::tx_lo_input_sel_t::TX_LO_INPUT_SEL_INTERNAL);
    } else if (src == "external") {
        _tx_lo->set_output_enable(lmx2592_iface::output_t::RF_OUTPUT_A, false);
        _cpld->set_tx_lo_source(rhodium_cpld_ctrl::tx_lo_input_sel_t::TX_LO_INPUT_SEL_EXTERNAL);
    } else {
        throw uhd::value_error(str(boost::format("set_tx_lo_source was called with an invalid LO source: %s Valid sources are [internal, external]") % src));
    }

    _update_corrections(src, get_tx_frequency(0), TX_DIRECTION);

    _tx_lo_source = src;
}

void rhodium_radio_ctrl_impl::set_rx_lo_source(
    const std::string& src,
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "set_rx_lo_source(src=" << src << ", name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "set_tx_lo_source");

    if (name == RHODIUM_LO2) {
        if (src != "internal") {
            throw uhd::value_error("The Lowband LO can only be set to internal");
        }
        return;
    }

    if (src == "internal") {
        _rx_lo->set_output_enable(lmx2592_iface::output_t::RF_OUTPUT_A, true);
        _cpld->set_rx_lo_source(rhodium_cpld_ctrl::rx_lo_input_sel_t::RX_LO_INPUT_SEL_INTERNAL);
    } else if (src == "external") {
        _rx_lo->set_output_enable(lmx2592_iface::output_t::RF_OUTPUT_A, false);
        _cpld->set_rx_lo_source(rhodium_cpld_ctrl::rx_lo_input_sel_t::RX_LO_INPUT_SEL_EXTERNAL);
    } else {
        throw uhd::value_error(str(boost::format("set_rx_lo_source was called with an invalid LO source: %s Valid sources for LO1 are [internal, external]") % src));
    }

    _update_corrections(src, get_rx_frequency(0), RX_DIRECTION);

    _rx_lo_source = src;
}

const std::string rhodium_radio_ctrl_impl::get_tx_lo_source(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_tx_lo_source(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_tx_lo_source");

    return (name == RHODIUM_LO1 or name == ALL_LOS) ? _tx_lo_source : "internal";
}

const std::string rhodium_radio_ctrl_impl::get_rx_lo_source(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_rx_lo_source(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_rx_lo_source");

    return (name == RHODIUM_LO1 or name == ALL_LOS) ? _rx_lo_source : "internal";
}

/******************************************************************************
 * Export Control
 *****************************************************************************/

void rhodium_radio_ctrl_impl::_set_lo1_export_enabled(
    const bool enabled,
    const direction_t dir
) {
    auto& _lo_ctrl = (dir == RX_DIRECTION) ? _rx_lo : _tx_lo;
    _lo_ctrl->set_output_enable(lmx2592_iface::output_t::RF_OUTPUT_B, enabled);
    if (_lo_dist_present) {
        const auto direction = (dir == RX_DIRECTION) ? "RX" : "TX";
        _rpcc->notify_with_token(_rpc_prefix + "enable_lo_export", direction, enabled);
    }
}

void rhodium_radio_ctrl_impl::set_tx_lo_export_enabled(
    const bool enabled,
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "set_tx_lo_export_enabled(enabled=" << enabled << ", name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "set_tx_lo_export_enabled");

    if (name == RHODIUM_LO2) {
        if (enabled) {
            throw uhd::value_error("The lowband LO cannot be exported");
        }
        return;
    }

    _set_lo1_export_enabled(enabled, TX_DIRECTION);
    _tx_lo_exported = enabled;
}

void rhodium_radio_ctrl_impl::set_rx_lo_export_enabled(
    const bool enabled,
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "set_rx_lo_export_enabled(enabled=" << enabled << ", name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "set_rx_lo_export_enabled");

    if (name == RHODIUM_LO2) {
        if (enabled) {
            throw uhd::value_error("The lowband LO cannot be exported");
        }
        return;
    }

    _set_lo1_export_enabled(enabled, RX_DIRECTION);
    _rx_lo_exported = enabled;
}

bool rhodium_radio_ctrl_impl::get_tx_lo_export_enabled(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_tx_lo_export_enabled(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_tx_lo_export_enabled");

    return (name == RHODIUM_LO1 or name == ALL_LOS) ? _tx_lo_exported : false;
}

bool rhodium_radio_ctrl_impl::get_rx_lo_export_enabled(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_rx_lo_export_enabled(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_rx_lo_export_enabled");

    return (name == RHODIUM_LO1 or name == ALL_LOS) ? _rx_lo_exported : false;
}

/******************************************************************************
 * LO Distribution Control
 *****************************************************************************/

void rhodium_radio_ctrl_impl::_validate_output_port(const std::string& port_name, const std::string& function_name)
{
    if (!_lo_dist_present) {
        throw uhd::runtime_error(str(boost::format(
            "%s can only be called if the LO distribution board was detected") % function_name));
    }

    if (!uhd::has(LO_OUTPUT_PORT_NAMES, port_name)) {
        throw uhd::value_error(str(boost::format(
            "%s was called with an invalid LO output port: %s Valid ports are [LO_OUT_0, LO_OUT_1, LO_OUT_2, LO_OUT_3]")
            % function_name % port_name));
    }
}

void rhodium_radio_ctrl_impl::_set_lo_output_enabled(
    const bool enabled,
    const std::string& port_name,
    const direction_t dir
) {
    auto direction = (dir == RX_DIRECTION) ? "RX" : "TX";
    auto name_iter = std::find(LO_OUTPUT_PORT_NAMES.begin(), LO_OUTPUT_PORT_NAMES.end(), port_name);
    auto index = std::distance(LO_OUTPUT_PORT_NAMES.begin(), name_iter);

    _rpcc->notify_with_token(_rpc_prefix + "enable_lo_output", direction, index, enabled);
    auto out_enabled = (dir == RX_DIRECTION) ? _lo_dist_rx_out_enabled : _lo_dist_tx_out_enabled;
    out_enabled[index] = enabled;
}

void rhodium_radio_ctrl_impl::set_tx_lo_output_enabled(
    const bool enabled,
    const std::string& port_name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "set_tx_lo_output_enabled(enabled=" << enabled << ", port_name=" << port_name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_output_port(port_name, "set_tx_lo_output_enabled");

    _set_lo_output_enabled(enabled, port_name, TX_DIRECTION);
}

void rhodium_radio_ctrl_impl::set_rx_lo_output_enabled(
    const bool enabled,
    const std::string& port_name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "set_rx_lo_output_enabled(enabled=" << enabled << ", port_name=" << port_name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_output_port(port_name, "set_rx_lo_output_enabled");

    _set_lo_output_enabled(enabled, port_name, RX_DIRECTION);
}

bool rhodium_radio_ctrl_impl::_get_lo_output_enabled(
    const std::string& port_name,
    const direction_t dir
) {
    auto name_iter = std::find(LO_OUTPUT_PORT_NAMES.begin(), LO_OUTPUT_PORT_NAMES.end(), port_name);
    auto index = std::distance(LO_OUTPUT_PORT_NAMES.begin(), name_iter);

    auto out_enabled = (dir == RX_DIRECTION) ? _lo_dist_rx_out_enabled : _lo_dist_tx_out_enabled;
    return out_enabled[index];
}

bool rhodium_radio_ctrl_impl::get_tx_lo_output_enabled(
    const std::string& port_name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_tx_lo_output_enabled(port_name=" << port_name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_output_port(port_name, "get_tx_lo_output_enabled");

    return _get_lo_output_enabled(port_name, TX_DIRECTION);
}

bool rhodium_radio_ctrl_impl::get_rx_lo_output_enabled(
    const std::string& port_name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_rx_lo_output_enabled(port_name=" << port_name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_output_port(port_name, "get_rx_lo_output_enabled");

    return _get_lo_output_enabled(port_name, RX_DIRECTION);
}

/******************************************************************************
 * Gain Control
 *****************************************************************************/

double rhodium_radio_ctrl_impl::set_tx_lo_gain(
    double gain,
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "set_tx_lo_gain(gain=" << gain << ", name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "set_tx_lo_gain");

    if (name == ALL_LOS) {
        throw uhd::runtime_error("LO gain must be set for each stage individually");
    }
    if (name == RHODIUM_LO2) {
        UHD_LOG_WARNING(unique_id(), "The Lowband LO does not have configurable gain");
        return 0.0;
    }

    auto index = _get_lo_gain_range().clip(gain);

    _cpld->set_lo_gain(index, TX_DIRECTION);
    _lo_tx_gain = index;
    return _lo_tx_gain;
}

double rhodium_radio_ctrl_impl::set_rx_lo_gain(
    double gain,
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "set_rx_lo_gain(gain=" << gain << ", name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "set_rx_lo_gain");

    if (name == ALL_LOS) {
        throw uhd::runtime_error("LO gain must be set for each stage individually");
    }
    if (name == RHODIUM_LO2) {
        UHD_LOG_WARNING(unique_id(), "The Lowband LO does not have configurable gain");
        return 0.0;
    }

    auto index = _get_lo_gain_range().clip(gain);

    _cpld->set_lo_gain(index, RX_DIRECTION);
    _lo_rx_gain = index;
    return _lo_rx_gain;
}

double rhodium_radio_ctrl_impl::get_tx_lo_gain(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_tx_lo_gain(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_tx_lo_gain");

    if (name == ALL_LOS) {
        throw uhd::runtime_error("LO gain must be retrieved for each stage individually");
    }

    return (name == RHODIUM_LO1) ? _lo_rx_gain : 0.0;
}

double rhodium_radio_ctrl_impl::get_rx_lo_gain(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_rx_lo_gain(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_rx_lo_gain");

    if (name == ALL_LOS) {
        throw uhd::runtime_error("LO gain must be retrieved for each stage individually");
    }

    return (name == RHODIUM_LO1) ? _lo_tx_gain : 0.0;
}

/******************************************************************************
 * Output Power Control
 *****************************************************************************/

double rhodium_radio_ctrl_impl::_set_lo1_power(
    const double power,
    const direction_t dir
) {
    auto& _lo_ctrl = (dir == RX_DIRECTION) ? _rx_lo : _tx_lo;
    auto coerced_power = static_cast<uint32_t>(_get_lo_power_range().clip(power, true));

    _lo_ctrl->set_output_power(lmx2592_iface::RF_OUTPUT_A, coerced_power);
    return coerced_power;
}

double rhodium_radio_ctrl_impl::set_tx_lo_power(
    const double power,
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "set_tx_lo_power(power=" << power << ", name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "set_tx_lo_power");

    if (name == ALL_LOS) {
        throw uhd::runtime_error(
            "LO output power must be set for each stage individually");
    }
    if (name == RHODIUM_LO2) {
        UHD_LOG_WARNING(unique_id(), "The Lowband LO does not have configurable output power");
        return 0.0;
    }

    _lo_tx_power = _set_lo1_power(power, TX_DIRECTION);
    return _lo_tx_power;
}

double rhodium_radio_ctrl_impl::set_rx_lo_power(
    const double power,
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "set_rx_lo_power(power=" << power << ", name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "set_rx_lo_power");

    if (name == ALL_LOS) {
        throw uhd::runtime_error(
            "LO output power must be set for each stage individually");
    }
    if (name == RHODIUM_LO2) {
        UHD_LOG_WARNING(unique_id(), "The Lowband LO does not have configurable output power");
        return 0.0;
    }

    _lo_rx_power = _set_lo1_power(power, RX_DIRECTION);
    return _lo_rx_power;
}

double rhodium_radio_ctrl_impl::get_tx_lo_power(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_tx_lo_power(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_tx_lo_power");

    if (name == ALL_LOS) {
        throw uhd::runtime_error(
            "LO output power must be retrieved for each stage individually");
    }

    return (name == RHODIUM_LO1) ? _lo_tx_power : 0.0;
}

double rhodium_radio_ctrl_impl::get_rx_lo_power(
    const std::string& name,
    const size_t chan
) {
    UHD_LOG_TRACE(unique_id(), "get_rx_lo_power(name=" << name << ", chan=" << chan << ")");
    UHD_ASSERT_THROW(chan == 0);
    _validate_lo_name(name, "get_rx_lo_power");

    if (name == ALL_LOS) {
        throw uhd::runtime_error(
            "LO output power must be retrieved for each stage individually");
    }

    return (name == RHODIUM_LO1) ? _lo_rx_power : 0.0;
}

/******************************************************************************
 * Helper Functions
 *****************************************************************************/

double rhodium_radio_ctrl_impl::_get_lowband_lo_freq() const
{
    return MCR_TO_LOWBAND_IF.at(_master_clock_rate);
}

uhd::gain_range_t rhodium_radio_ctrl_impl::_get_lo_gain_range()
{
    return gain_range_t(LO_MIN_GAIN, LO_MAX_GAIN, LO_GAIN_STEP);
}

uhd::gain_range_t rhodium_radio_ctrl_impl::_get_lo_power_range()
{
    return gain_range_t(LO_MIN_POWER, LO_MAX_POWER, LO_POWER_STEP);
}

int rhodium_radio_ctrl_impl::_get_lo_dsa_setting(const double freq, const direction_t dir) {
    int index = 0;
    while (freq > FREQ_THRESHOLDS[index+1]) {
        index++;
    }

    const double freq_low = FREQ_THRESHOLDS[index];
    const double freq_high = FREQ_THRESHOLDS[index+1];

    const auto& gain_table = (dir == RX_DIRECTION) ? DSA_RX_GAIN_VALUES : DSA_TX_GAIN_VALUES;
    const double gain_low = gain_table[index];
    const double gain_high = gain_table[index+1];


    const double slope = (gain_high - gain_low) / (freq_high - freq_low);
    const double gain_at_freq = gain_low + (freq - freq_low) * slope;

    UHD_LOG_TRACE(unique_id(), "Interpolated DSA Gain is " << gain_at_freq);
    return static_cast<int>(std::round(gain_at_freq));
}

unsigned int rhodium_radio_ctrl_impl::_get_lo_power_setting(double freq) {
    int index = 0;
    while (freq > FREQ_THRESHOLDS[index+1]) {
        index++;
    }

    const double freq_low = FREQ_THRESHOLDS[index];
    const double freq_high = FREQ_THRESHOLDS[index+1];
    const double power_low = LMX_GAIN_VALUES[index];
    const double power_high = LMX_GAIN_VALUES[index+1];


    const double slope = (power_high - power_low) / (freq_high - freq_low);
    const double power_at_freq = power_low + (freq - freq_low) * slope;

    UHD_LOG_TRACE(unique_id(), "Interpolated LMX Power is " << power_at_freq);
    return uhd::narrow_cast<unsigned int>(std::lround(power_at_freq));
}