aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/usrp/dboard/zbx/zbx_dboard.cpp
blob: d41302c8fce8374f26cd209d631189895c1d2230 (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
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
//
// Copyright 2020 Ettus Research, a National Instruments Brand
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include <uhd/types/direction.hpp>
#include <uhd/types/eeprom.hpp>
#include <uhd/utils/algorithm.hpp>
#include <uhd/utils/assert_has.hpp>
#include <uhd/utils/log.hpp>
#include <uhd/utils/math.hpp>
#include <uhdlib/usrp/dboard/zbx/zbx_dboard.hpp>
#include <uhdlib/utils/narrow.hpp>
#include <cstdlib>
#include <sstream>

namespace uhd { namespace usrp { namespace zbx {

/******************************************************************************
 * Structors
 *****************************************************************************/
zbx_dboard_impl::zbx_dboard_impl(register_iface& reg_iface,
    const size_t reg_base_address,
    time_accessor_fn_type&& time_accessor,
    const size_t db_idx,
    const std::string& radio_slot,
    const std::string& rpc_prefix,
    const std::string& unique_id,
    uhd::usrp::x400_rpc_iface::sptr mb_rpcc,
    uhd::usrp::zbx_rpc_iface::sptr rpcc,
    uhd::rfnoc::x400::rfdc_control::sptr rfdcc,
    uhd::property_tree::sptr tree)
    : _unique_id(unique_id)
    , _regs(reg_iface)
    , _reg_base_address(reg_base_address)
    , _time_accessor(time_accessor)
    , _radio_slot(radio_slot)
    , _db_idx(db_idx)
    , _rpc_prefix(rpc_prefix)
    , _mb_rpcc(mb_rpcc)
    , _rpcc(rpcc)
    , _rfdcc(rfdcc)
    , _tree(tree)
    , _rfdc_rate(_rpcc->get_dboard_sample_rate())
    , _prc_rate(_rpcc->get_dboard_prc_rate())
{
    RFNOC_LOG_TRACE("Entering zbx_dboard_impl ctor...");
    RFNOC_LOG_TRACE("Radio slot: " << _radio_slot);

    _tx_gain_profile_api = std::make_shared<rf_control::enumerated_gain_profile>(
        ZBX_GAIN_PROFILES, ZBX_GAIN_PROFILE_DEFAULT, ZBX_NUM_CHANS);
    _rx_gain_profile_api = std::make_shared<rf_control::enumerated_gain_profile>(
        ZBX_GAIN_PROFILES, ZBX_GAIN_PROFILE_DEFAULT, ZBX_NUM_CHANS);

    _expert_container =
        uhd::experts::expert_factory::create_container("zbx_radio_" + _radio_slot);
    _init_cpld();
    _init_peripherals();
    // Prop tree requires the initialization of certain peripherals
    _init_prop_tree();
    _expert_container->resolve_all();
}

zbx_dboard_impl::~zbx_dboard_impl()
{
    RFNOC_LOG_TRACE("zbx_dboard::dtor() ");
}

void zbx_dboard_impl::deinit()
{
    _wb_ifaces.clear();
}

void zbx_dboard_impl::set_command_time(uhd::time_spec_t time, const size_t chan)
{
    // When the command time gets updated, import it into the expert graph
    get_tree()
        ->access<time_spec_t>(fs_path("dboard") / "rx_frontends" / chan / "time/cmd")
        .set(time);
}

std::string zbx_dboard_impl::get_unique_id() const
{
    return _unique_id;
}


/******************************************************************************
 * API Calls
 *****************************************************************************/
void zbx_dboard_impl::set_tx_antenna(const std::string& ant, const size_t chan)
{
    RFNOC_LOG_TRACE("Setting TX antenna to " << ant << " for chan " << chan);
    if (!TX_ANTENNA_NAME_COMPAT_MAP.count(ant)) {
        assert_has(TX_ANTENNAS, ant, "tx antenna");
    }
    const fs_path fe_path = _get_frontend_path(TX_DIRECTION, chan);

    _tree->access<std::string>(fe_path / "antenna" / "value").set(ant);
}

void zbx_dboard_impl::set_rx_antenna(const std::string& ant, const size_t chan)
{
    RFNOC_LOG_TRACE("Setting RX antenna to " << ant << " for chan " << chan);
    if (!RX_ANTENNA_NAME_COMPAT_MAP.count(ant)) {
        assert_has(RX_ANTENNAS, ant, "rx antenna");
    }

    const fs_path fe_path = _get_frontend_path(RX_DIRECTION, chan);

    _tree->access<std::string>(fe_path / "antenna" / "value").set(ant);
}

double zbx_dboard_impl::set_tx_frequency(const double req_freq, const size_t chan)
{
    const fs_path fe_path = _get_frontend_path(TX_DIRECTION, chan);

    _tree->access<double>(fe_path / "freq").set(req_freq);

    // Our power manager sets a new gain value via the API, based on its new calculations.
    // Since the expert nodes are protected by a mutex, it will hang if we try to call
    // update_power() from inside the expert resolve methods (resolve() -> update_power()
    // -> set_tx_gain -> resolve())
    _tx_pwr_mgr.at(chan)->update_power();

    return _tree->access<double>(fe_path / "freq").get();
}

double zbx_dboard_impl::set_rx_frequency(const double req_freq, const size_t chan)
{
    const fs_path fe_path = _get_frontend_path(RX_DIRECTION, chan);

    _tree->access<double>(fe_path / "freq").set(req_freq);

    // Our power manager sets a new gain value via the API, based on its new calculations.
    // Since the expert nodes are protected by a mutex, it will hang if we try to call
    // update_power() from inside the expert resolve methods (resolve() -> update_power()
    // -> set_rx_gain -> resolve())
    _rx_pwr_mgr.at(chan)->update_power();

    return _tree->access<double>(fe_path / "freq").get();
}

double zbx_dboard_impl::set_tx_bandwidth(const double bandwidth, const size_t chan)
{
    const double bw = get_tx_bandwidth(chan);
    if (!uhd::math::frequencies_are_equal(bandwidth, bw)) {
        RFNOC_LOG_WARNING("Invalid analog bandwidth: " << (bandwidth / 1e6) << " MHz.");
    }
    return bw;
}

double zbx_dboard_impl::get_tx_bandwidth(size_t chan)
{
    return _tree
        ->access<double>(_get_frontend_path(TX_DIRECTION, chan) / "bandwidth/value")
        .get();
}

double zbx_dboard_impl::set_rx_bandwidth(const double bandwidth, const size_t chan)
{
    const double bw = get_rx_bandwidth(chan);
    if (!uhd::math::frequencies_are_equal(bandwidth, bw)) {
        RFNOC_LOG_WARNING("Invalid analog bandwidth: " << (bandwidth / 1e6) << " MHz.");
    }
    return bw;
}

double zbx_dboard_impl::get_rx_bandwidth(size_t chan)
{
    return _tree
        ->access<double>(_get_frontend_path(RX_DIRECTION, chan) / "bandwidth/value")
        .get();
}

double zbx_dboard_impl::set_tx_gain(
    const double gain, const std::string& name_, const size_t chan)
{
    // We have to accept the empty string for "all", because that's widely used
    // (e.g. by multi_usrp)
    const std::string name   = name_.empty() ? ZBX_GAIN_STAGE_ALL : name_;
    const fs_path gains_path = _get_frontend_path(TX_DIRECTION, chan) / "gains";
    const auto gain_profile  = _tx_gain_profile_api->get_gain_profile(chan);
    // Default gain profile: Setting anything other than 'all' is forbidden
    if (gain_profile == ZBX_GAIN_PROFILE_DEFAULT && name != ZBX_GAIN_STAGE_ALL) {
        throw uhd::key_error("Invalid gain name for gain profile 'default': " + name);
    }
    // Also, when the gain name is all, we have to be in default mode.
    if (gain_profile != ZBX_GAIN_PROFILE_DEFAULT && name == ZBX_GAIN_STAGE_ALL) {
        throw uhd::key_error(
            "Setting overall gain is only valid in gain profile 'default'!");
    }
    // The combination of the no-ATR profile, and any gain name other than 'table'
    // is not valid.
    if (gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR && name != ZBX_GAIN_STAGE_TABLE) {
        throw uhd::key_error("set_tx_gain(): Invalid combination of gain profile "
                             + gain_profile + " and gain name " + name);
    }
    // First, we handle the 'table' gain name. It's handled a bit differently
    // than the rest.
    if (name == ZBX_GAIN_STAGE_TABLE) {
        static const uhd::meta_range_t table_range(0, 255, 1);
        const uint8_t table_idx = uhd::narrow<uint8_t>(table_range.clip(gain, true));
        if (gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR) {
            _cpld->set_sw_config(chan, zbx_cpld_ctrl::atr_mode_target::DSA, table_idx);
            return static_cast<double>(table_idx);
        }
        if (gain_profile == ZBX_GAIN_PROFILE_MANUAL
            || gain_profile == ZBX_GAIN_PROFILE_CPLD) {
            _cpld->set_tx_gain_switches(chan, ATR_ADDR_TX, table_idx);
            _cpld->set_tx_gain_switches(chan, ATR_ADDR_XX, table_idx);
            return static_cast<double>(table_idx);
        }
        // That covers all the gain profiles for gain name 'table'.
        UHD_THROW_INVALID_CODE_PATH();
    }
    // Sanity check key. Note we do this after the previous gain stage, because
    // it's not a property node.
    if (!_tree->exists(gains_path / name)) {
        throw uhd::key_error("Invalid TX gain stage: " + name);
    }
    // This leaves directly setting either the DSAs or the amplifier. This is
    // possible in both the manual and CPLD gain profiles.
    return _tree->access<double>(gains_path / name / "value").set(gain).get();
}

double zbx_dboard_impl::set_rx_gain(
    const double gain, const std::string& name_, const size_t chan)
{
    // We have to accept the empty string for "all", because that's widely used
    // (e.g. by multi_usrp).
    const std::string name   = name_.empty() ? ZBX_GAIN_STAGE_ALL : name_;
    const fs_path gains_path = _get_frontend_path(RX_DIRECTION, chan) / "gains";
    const auto gain_profile  = _rx_gain_profile_api->get_gain_profile(chan);

    // Default gain profile: Setting anything other than ZBX_GAIN_STAGE_ALL is forbidden
    if (gain_profile == ZBX_GAIN_PROFILE_DEFAULT && name != ZBX_GAIN_STAGE_ALL) {
        throw uhd::key_error("Invalid gain name for gain profile 'default': " + name);
    }
    // Also, when the gain name is all, we have to be in default mode.
    if (gain_profile != ZBX_GAIN_PROFILE_DEFAULT && name == ZBX_GAIN_STAGE_ALL) {
        throw uhd::key_error(
            "Setting overall gain is only valid in gain profile 'default'!");
    }
    // The combination of the no-ATR profile, and any gain name other than 'table'
    // is not valid.
    if (gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR && name != ZBX_GAIN_STAGE_TABLE) {
        throw uhd::key_error("set_rx_gain(): Invalid combination of gain profile "
                             + gain_profile + " and gain name " + name);
    }
    // First, we handle the 'table' gain name. It's a bit different from the
    // rest.
    if (name == ZBX_GAIN_STAGE_TABLE) {
        static const uhd::meta_range_t table_range(0, 255, 1);
        const uint8_t table_idx = uhd::narrow<uint8_t>(table_range.clip(gain, true));
        if (gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR) {
            _cpld->set_sw_config(chan, zbx_cpld_ctrl::atr_mode_target::DSA, table_idx);
            return static_cast<double>(table_idx);
        }
        if (gain_profile == ZBX_GAIN_PROFILE_MANUAL
            || gain_profile == ZBX_GAIN_PROFILE_CPLD) {
            _cpld->set_rx_gain_switches(chan, ATR_ADDR_RX, table_idx);
            _cpld->set_rx_gain_switches(chan, ATR_ADDR_XX, table_idx);
            return static_cast<double>(table_idx);
        }
        // That covers all the gain profiles for gain name 'table'.
        UHD_THROW_INVALID_CODE_PATH();
    }
    // Sanity check key. Note we do this after the previous gain stage, because
    // it's not a property node.
    if (!_tree->exists(gains_path / name / "value")) {
        throw uhd::key_error("Invalid RX gain stage: " + name);
    }
    return _tree->access<double>(gains_path / name / "value").set(gain).get();
}

double zbx_dboard_impl::set_tx_gain(const double gain, const size_t chan)
{
    const auto gain_profile = _tx_gain_profile_api->get_gain_profile(chan);
    if (gain_profile == ZBX_GAIN_PROFILE_MANUAL) {
        const std::string err_msg = "When using 'manual' gain mode, calling "
                                    "set_tx_gain() without a gain name is not allowed!";
        RFNOC_LOG_ERROR(err_msg);
        throw uhd::runtime_error(err_msg);
    }
    if (gain_profile == ZBX_GAIN_PROFILE_CPLD
        || gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR) {
        return set_tx_gain(gain, ZBX_GAIN_STAGE_TABLE, chan);
    }
    return set_tx_gain(gain, ZBX_GAIN_STAGE_ALL, chan);
}

double zbx_dboard_impl::set_rx_gain(const double gain, const size_t chan)
{
    const auto gain_profile = _rx_gain_profile_api->get_gain_profile(chan);
    if (gain_profile == ZBX_GAIN_PROFILE_MANUAL) {
        const std::string err_msg = "When using 'manual' gain mode, calling "
                                    "set_rx_gain() without a gain name is not allowed!";
        RFNOC_LOG_ERROR(err_msg);
        throw uhd::runtime_error(err_msg);
    }
    if (gain_profile == ZBX_GAIN_PROFILE_CPLD
        || gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR) {
        return set_rx_gain(gain, ZBX_GAIN_STAGE_TABLE, chan);
    }
    return set_rx_gain(gain, ZBX_GAIN_STAGE_ALL, chan);
}

double zbx_dboard_impl::get_tx_gain(const size_t chan)
{
    const auto gain_profile = _tx_gain_profile_api->get_gain_profile(chan);
    if (gain_profile == ZBX_GAIN_PROFILE_CPLD
        || gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR) {
        return get_tx_gain(ZBX_GAIN_STAGE_TABLE, chan);
    }
    if (gain_profile == ZBX_GAIN_PROFILE_DEFAULT) {
        return get_tx_gain(ZBX_GAIN_STAGE_ALL, chan);
    }
    throw uhd::runtime_error(
        "get_tx_gain(): When in 'manual' gain profile, a gain name is required!");
}

double zbx_dboard_impl::get_rx_gain(const size_t chan)
{
    const auto gain_profile = _rx_gain_profile_api->get_gain_profile(chan);
    if (gain_profile == ZBX_GAIN_PROFILE_CPLD
        || gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR) {
        return get_rx_gain(ZBX_GAIN_STAGE_TABLE, chan);
    }
    if (gain_profile == ZBX_GAIN_PROFILE_DEFAULT) {
        return get_rx_gain(ZBX_GAIN_STAGE_ALL, chan);
    }
    throw uhd::runtime_error(
        "get_rx_gain(): When in 'manual' gain profile, a gain name is required!");
}

double zbx_dboard_impl::get_tx_gain(const std::string& name_, const size_t chan)
{
    // We have to accept the empty string for "all", because that's widely used
    // (e.g. by multi_usrp)
    const std::string name   = name_.empty() ? ZBX_GAIN_STAGE_ALL : name_;
    const fs_path gains_path = _get_frontend_path(TX_DIRECTION, chan) / "gains";
    const auto gain_profile  = _tx_gain_profile_api->get_gain_profile(chan);
    // Overall gain: Only reliable in 'default' mode. We warn, not throw, in
    // the other modes. That's because reading back the overall gain is common
    // diagnostic for many existing applications.
    if (name == ZBX_GAIN_STAGE_ALL && gain_profile != ZBX_GAIN_PROFILE_DEFAULT) {
        RFNOC_LOG_WARNING("get_tx_gain(): Trying to read back overall gain in "
                          "non-default gain profile is undefined.");
    }
    // Table gain: Returns the current DSA table index.
    if (name == ZBX_GAIN_STAGE_TABLE) {
        return static_cast<double>(
            _cpld->get_current_config(chan, zbx_cpld_ctrl::atr_mode_target::DSA));
    }
    // Otherwise: DSA or amp. Sanity check key is valid. Because the table gain
    // is not a property tree node, this check comes after the previous if-clause.
    if (!_tree->exists(gains_path / name / "value")) {
        RFNOC_LOG_ERROR("get_tx_gain(): Invalid gain name `" << name << "'");
        throw uhd::key_error(std::string("get_tx_gain(): Invalid gain name: ") + name);
    }
    // We're not yet done: If we're in CPLD/table profiles, we peek the current
    // DSA settings and apply them to the local cache.
    // Note: This means we have a different behaviour between directly accessing
    // the prop tree, or accessing the C++ API.
    if ((name == ZBX_GAIN_STAGE_DSA1 || name == ZBX_GAIN_STAGE_DSA2)
        && (gain_profile == ZBX_GAIN_PROFILE_CPLD
            || gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR)) {
        const uint8_t idx =
            (gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR)
                ? _cpld->get_current_config(chan, zbx_cpld_ctrl::atr_mode_target::DSA)
                : ATR_ADDR_TX;
        constexpr bool update_cache = true; // Make sure to peek the actual value
        const auto dsa = (name == ZBX_GAIN_STAGE_DSA1) ? zbx_cpld_ctrl::dsa_type::DSA1
                                                       : zbx_cpld_ctrl::dsa_type::DSA2;
        const uint8_t dsa_val = _cpld->get_tx_dsa(chan, idx, dsa, update_cache);
        // Update the tree because we're good citizens, and if we switch the
        // gain profile from 'table' to 'manual', we want everything to be
        // consistent. This will not cause a poke to the CPLD, b/c the experts
        // won't write gains in this gain profile.
        // Note that the other DSA values in the tree are not updated automatically,
        // which is why we can't write DSA values to the CPLD in this mode. If
        // we want to allow writing DSA values in this mode, we need to update
        // everything here, or put some more cleverness into the programming
        // expert.
        _tree->access<double>(gains_path / name / "value")
            .set(ZBX_TX_DSA_MAX_ATT - dsa_val);
    }
    // Now return the value from the tree
    return _tree->access<double>(gains_path / name / "value").get();
}

double zbx_dboard_impl::get_rx_gain(const std::string& name_, const size_t chan)
{
    // We have to accept the empty string for "all", because that's widely used
    // (e.g. by multi_usrp)
    const std::string name   = name_.empty() ? ZBX_GAIN_STAGE_ALL : name_;
    const fs_path gains_path = _get_frontend_path(RX_DIRECTION, chan) / "gains";
    const auto gain_profile = _rx_gain_profile_api->get_gain_profile(chan);
    // Overall gain: Only reliable in 'default' mode. We warn, not throw, in
    // the other modes. That's because reading back the overall gain is common
    // diagnostic for many existing applications.
    if (name == ZBX_GAIN_STAGE_ALL && gain_profile != ZBX_GAIN_PROFILE_DEFAULT) {
        RFNOC_LOG_WARNING("get_rx_gain(): Trying to read back overall gain in "
                          "non-default gain profile is undefined.");
    }
    // Table gain: Returns the current DSA table index.
    if (name == ZBX_GAIN_STAGE_TABLE) {
        return static_cast<double>(
            _cpld->get_current_config(chan, zbx_cpld_ctrl::atr_mode_target::DSA));
    }
    // Otherwise: DSA. Sanity check key is valid. Because the table gain is not
    // a property tree node, this check comes after the previous if-clause.
    if (!_tree->exists(gains_path / name / "value")) {
        RFNOC_LOG_ERROR("get_rx_gain(): Invalid gain name `" << name << "'");
        throw uhd::key_error(std::string("get_rx_gain(): Invalid gain name: ") + name);
    }
    // We're not yet done: If we're in CPLD/table profiles, we peek the current
    // DSA settings and apply them to the local cache.
    // Note: This means we have a different behaviour between directly accessing
    // the prop tree, or accessing the C++ API.
    if (gain_profile == ZBX_GAIN_PROFILE_CPLD
        || gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR) {
        const uint8_t idx =
            (gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR)
                ? _cpld->get_current_config(chan, zbx_cpld_ctrl::atr_mode_target::DSA)
                : ATR_ADDR_RX;
        constexpr bool update_cache = true; // Make sure to peek the actual value
        static const std::map<std::string, zbx_cpld_ctrl::dsa_type> dsa_map{
            {ZBX_GAIN_STAGE_DSA1, zbx_cpld_ctrl::dsa_type::DSA1},
            {ZBX_GAIN_STAGE_DSA2, zbx_cpld_ctrl::dsa_type::DSA2},
            {ZBX_GAIN_STAGE_DSA3A, zbx_cpld_ctrl::dsa_type::DSA3A},
            {ZBX_GAIN_STAGE_DSA3B, zbx_cpld_ctrl::dsa_type::DSA3B},
        };
        const auto dsa        = dsa_map.at(name);
        const uint8_t dsa_val = _cpld->get_rx_dsa(chan, idx, dsa, update_cache);
        // Update the tree because we're good citizens, and if we switch the
        // gain profile from 'table' to 'manual', we want everything to be
        // consistent. This will not cause a poke to the CPLD, b/c the experts
        // won't write gains in this gain profile.
        // Note that the other DSA values in the tree are not updated automatically,
        // which is why we can't write DSA values to the CPLD in this profile. If
        // we want to allow writing DSA values in this profile, we need to update
        // everything here, or put some more cleverness into the programming
        // expert.
        _tree->access<double>(gains_path / name / "value")
            .set(static_cast<double>(ZBX_RX_DSA_MAX_ATT - dsa_val));
    }
    return _tree->access<double>(gains_path / name / "value").get();
}

std::vector<std::string> zbx_dboard_impl::get_tx_gain_names(const size_t chan) const
{
    UHD_ASSERT_THROW(chan < ZBX_NUM_CHANS);
    const std::string gain_profile = _tx_gain_profile_api->get_gain_profile(chan);

    if (gain_profile == ZBX_GAIN_PROFILE_DEFAULT) {
        return {ZBX_GAIN_STAGE_ALL};
    }
    if (gain_profile == ZBX_GAIN_PROFILE_CPLD
        || gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR) {
        return {ZBX_GAIN_STAGE_TABLE};
    }
    return ZBX_TX_GAIN_STAGES;
}

std::vector<std::string> zbx_dboard_impl::get_rx_gain_names(const size_t chan) const
{
    UHD_ASSERT_THROW(chan < ZBX_NUM_CHANS);
    const std::string gain_profile = _rx_gain_profile_api->get_gain_profile(chan);

    if (gain_profile == ZBX_GAIN_PROFILE_DEFAULT) {
        return {ZBX_GAIN_STAGE_ALL};
    }
    if (gain_profile == ZBX_GAIN_PROFILE_CPLD
        || gain_profile == ZBX_GAIN_PROFILE_CPLD_NOATR) {
        return {ZBX_GAIN_STAGE_TABLE};
    }
    return ZBX_RX_GAIN_STAGES;
}

const std::string zbx_dboard_impl::get_tx_lo_source(
    const std::string& name, const size_t chan)
{
    const fs_path fe_path = _get_frontend_path(TX_DIRECTION, chan);
    if (!_tree->exists(fe_path / "ch" / name)) {
        throw uhd::value_error("get_tx_lo_source(): Invalid LO name: " + name);
    }

    const zbx_lo_source_t lo_source =
        _tree->access<zbx_lo_source_t>(fe_path / "ch" / name / "source").get();
    return lo_source == zbx_lo_source_t::internal ? "internal" : "external";
}

const std::string zbx_dboard_impl::get_rx_lo_source(
    const std::string& name, const size_t chan)
{
    const fs_path fe_path = _get_frontend_path(RX_DIRECTION, chan);
    if (!_tree->exists(fe_path / "ch" / name)) {
        throw uhd::value_error("get_rx_lo_source(): Invalid LO name: " + name);
    }

    const zbx_lo_source_t lo_source =
        _tree->access<zbx_lo_source_t>(fe_path / "ch" / name / "source").get();
    return lo_source == zbx_lo_source_t::internal ? "internal" : "external";
}

void zbx_dboard_impl::set_rx_lo_source(
    const std::string& src, const std::string& name, const size_t chan)
{
    RFNOC_LOG_TRACE("set_rx_lo_source(name=" << name << ", src=" << src << ")");
    const fs_path fe_path = _get_frontend_path(RX_DIRECTION, chan);
    if (!_tree->exists(fe_path / "ch" / name)) {
        throw uhd::value_error("set_rx_lo_source(): Invalid LO name: " + name);
    }

    _tree->access<zbx_lo_source_t>(fe_path / "ch" / name / "source")
        .set(src == "internal" ? zbx_lo_source_t::internal : zbx_lo_source_t::external);
}

void zbx_dboard_impl::set_tx_lo_source(
    const std::string& src, const std::string& name, const size_t chan)
{
    RFNOC_LOG_TRACE("set_tx_lo_source(name=" << name << ", src=" << src << ")");
    const fs_path fe_path = _get_frontend_path(TX_DIRECTION, chan);
    if (!_tree->exists(fe_path / "ch" / name)) {
        throw uhd::value_error("set_tx_lo_source(): Invalid LO name: " + name);
    }

    _tree->access<zbx_lo_source_t>(fe_path / "ch" / name / "source")
        .set(src == "internal" ? zbx_lo_source_t::internal : zbx_lo_source_t::external);
}

double zbx_dboard_impl::set_tx_lo_freq(
    double freq, const std::string& name, const size_t chan)
{
    RFNOC_LOG_TRACE("set_tx_lo_freq(freq=" << freq << ", name=" << name << ")");
    const fs_path fe_path = _get_frontend_path(TX_DIRECTION, chan);
    assert_has(ZBX_LOS, name);

    return _tree->access<double>(fe_path / "los" / name / "freq" / "value").set(freq).get();
}

double zbx_dboard_impl::get_tx_lo_freq(const std::string& name, const size_t chan)
{
    RFNOC_LOG_TRACE("get_tx_lo_freq(name=" << name << ")");
    const fs_path fe_path = _get_frontend_path(TX_DIRECTION, chan);
    assert_has(ZBX_LOS, name);

    return _tree->access<double>(fe_path / "los" / name / "freq" / "value").get();
}

freq_range_t zbx_dboard_impl::_get_lo_freq_range(
    const std::string& name, const size_t /*chan*/) const
{
    if (name == ZBX_LO1 || name == ZBX_LO2) {
        // Note this doesn't include the LO step size. The LO step size is only
        // used when the LO frequencies are automatically calculated (which is
        // the normal use case). When setting LO frequencies manually, it is
        // possible to set LOs to values outside of the step size.
        return freq_range_t{LMX2572_MIN_FREQ, LMX2572_MAX_FREQ};
    }
    if (name == RFDC_NCO) {
        // It might make sense to constrain the possible NCO values more, since
        // the bandpass filters for IF2 only allow a certain range. Note that LO1
        // and LO2 freq ranges are also constrained by their analog filters.
        // But in principle, this is the range for the NCO... so why not.
        return freq_range_t{0.0, _rfdc_rate};
    }
    throw uhd::value_error("Invalid LO name: " + name);
}

double zbx_dboard_impl::set_rx_lo_freq(
    double freq, const std::string& name, const size_t chan)
{
    RFNOC_LOG_TRACE("set_rx_lo_freq(freq=" << freq << ", name=" << name << ")");
    const fs_path fe_path = _get_frontend_path(RX_DIRECTION, chan);
    assert_has(ZBX_LOS, name);

    return _tree->access<double>(fe_path / "los" / name / "freq" / "value")
        .set(freq)
        .get();
}

double zbx_dboard_impl::get_rx_lo_freq(const std::string& name, size_t chan)
{
    RFNOC_LOG_TRACE("get_rx_lo_freq(name=" << name << ")");
    const fs_path fe_path = _get_frontend_path(RX_DIRECTION, chan);
    assert_has(ZBX_LOS, name);

    return _tree->access<double>(fe_path / "los" / name / "freq" / "value").get();
}

std::string zbx_dboard_impl::get_tx_antenna(size_t chan) const
{
    const fs_path fe_path = _get_frontend_path(TX_DIRECTION, chan);
    return _tree->access<std::string>(fe_path / "antenna" / "value").get();
}

std::string zbx_dboard_impl::get_rx_antenna(size_t chan) const
{
    const fs_path fe_path = _get_frontend_path(RX_DIRECTION, chan);
    return _tree->access<std::string>(fe_path / "antenna" / "value").get();
}

double zbx_dboard_impl::get_tx_frequency(size_t chan)
{
    const fs_path fe_path = _get_frontend_path(TX_DIRECTION, chan);
    return _tree->access<double>(fe_path / "freq").get();
}

double zbx_dboard_impl::get_rx_frequency(size_t chan)
{
    const fs_path fe_path = _get_frontend_path(RX_DIRECTION, chan);
    return _tree->access<double>(fe_path / "freq").get();
}

void zbx_dboard_impl::set_tx_tune_args(const uhd::device_addr_t&, const size_t)
{
    RFNOC_LOG_TRACE("tune_args not supported by this radio.");
}

void zbx_dboard_impl::set_rx_tune_args(const uhd::device_addr_t&, const size_t)
{
    RFNOC_LOG_TRACE("tune_args not supported by this radio.");
}

void zbx_dboard_impl::set_rx_agc(const bool, const size_t)
{
    throw uhd::not_implemented_error("set_rx_agc() is not supported on this radio!");
}

uhd::gain_range_t zbx_dboard_impl::get_tx_gain_range(
    const std::string& name, const size_t chan) const
{
    // We have to accept the empty string for "all", because that's widely used
    // (e.g. by multi_usrp)
    if (!name.empty() && name != ZBX_GAIN_STAGE_ALL) {
        throw uhd::value_error(
            std::string("get_tx_gain_range(): Unknown gain name '") + name + "'!");
    }
    return get_tx_gain_range(chan);
}

uhd::gain_range_t zbx_dboard_impl::get_rx_gain_range(
    const std::string& name, const size_t chan) const
{
    // We have to accept the empty string for "all", because that's widely used
    // (e.g. by multi_usrp)
    if (!name.empty() && name != ZBX_GAIN_STAGE_ALL) {
        throw uhd::value_error(
            std::string("get_rx_gain_range(): Unknown gain name '") + name + "'!");
    }
    return get_rx_gain_range(chan);
}

void zbx_dboard_impl::set_rx_lo_export_enabled(bool, const std::string&, const size_t)
{
    throw uhd::not_implemented_error(
        "set_rx_lo_export_enabled is not supported on this radio");
}

bool zbx_dboard_impl::get_rx_lo_export_enabled(const std::string&, const size_t)
{
    return false;
}

void zbx_dboard_impl::set_tx_lo_export_enabled(bool, const std::string&, const size_t)
{
    throw uhd::not_implemented_error(
        "set_rx_lo_export_enabled is not supported on this radio");
}

bool zbx_dboard_impl::get_tx_lo_export_enabled(const std::string&, const size_t)
{
    return false;
}

/******************************************************************************
 * EEPROM API
 *****************************************************************************/
eeprom_map_t zbx_dboard_impl::get_db_eeprom()
{
    return _mb_rpcc->get_db_eeprom(_db_idx);
}

size_t zbx_dboard_impl::get_chan_from_dboard_fe(
    const std::string& fe, const uhd::direction_t) const
{
    if (fe == "0") {
        return 0;
    }
    if (fe == "1") {
        return 1;
    }
    throw uhd::key_error(std::string("[X400] Invalid frontend: ") + fe);
}

std::string zbx_dboard_impl::get_dboard_fe_from_chan(
    const size_t chan, const uhd::direction_t) const
{
    if (chan == 0) {
        return "0";
    }
    if (chan == 1) {
        return "1";
    }
    throw uhd::lookup_error(
        std::string("[X400] Invalid channel: ") + std::to_string(chan));
}

/*********************************************************************
 *   Private misc/calculative helper functions
 **********************************************************************/

bool zbx_dboard_impl::_get_all_los_locked(const direction_t dir, const size_t chan)
{
    const fs_path fe_path = _get_frontend_path(dir, chan);

    const bool is_lo1_enabled = _tree->access<bool>(fe_path / ZBX_LO1 / "enabled").get();
    const bool is_lo1_locked =
        _lo_ctrl_map.at(zbx_lo_ctrl::lo_string_to_enum(dir, chan, ZBX_LO1))
            ->get_lock_status();
    // LO2 is always enabled via center frequency tuning, but users may manually disable
    // it
    const bool is_lo2_enabled = _tree->access<bool>(fe_path / ZBX_LO2 / "enabled").get();
    const bool is_lo2_locked =
        _lo_ctrl_map.at(zbx_lo_ctrl::lo_string_to_enum(dir, chan, ZBX_LO2))
            ->get_lock_status();
    // We only care about the lock status if it's enabled (lowband center frequency)
    // That means we have set it to true if is_lo[1,2]_enabled is *false*, but check for
    // the lock if is_lo[1,2]_enabled is *true*
    return (!is_lo1_enabled || is_lo1_locked) && (!is_lo2_enabled || is_lo2_locked);
}

fs_path zbx_dboard_impl::_get_frontend_path(
    const direction_t dir, const size_t chan_idx) const
{
    UHD_ASSERT_THROW(chan_idx < ZBX_NUM_CHANS);
    const std::string frontend = dir == TX_DIRECTION ? "tx_frontends" : "rx_frontends";
    return fs_path("dboard") / frontend / chan_idx;
}

std::vector<uhd::usrp::pwr_cal_mgr::sptr>& zbx_dboard_impl::get_pwr_mgr(
    uhd::direction_t trx)
{
    switch (trx) {
        case uhd::RX_DIRECTION:
            return _rx_pwr_mgr;
        case uhd::TX_DIRECTION:
            return _tx_pwr_mgr;
        default:
            UHD_THROW_INVALID_CODE_PATH();
    }
}

}}} // namespace uhd::usrp::zbx