aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/usrp/dboard/magnesium/magnesium_radio_ctrl_impl.cpp
blob: c1fc055af8e9dba932b80a12c8d0146dee29c988 (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
//
// Copyright 2017 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0
//

#include "magnesium_radio_ctrl_impl.hpp"
#include "magnesium_constants.hpp"
#include "magnesium_gain_table.hpp"
#include <uhd/utils/log.hpp>
#include <uhd/rfnoc/node_ctrl_base.hpp>
#include <uhd/transport/chdr.hpp>
#include <uhd/utils/math.hpp>
#include <uhd/types/direction.hpp>
#include <uhd/types/eeprom.hpp>
#include <uhd/exception.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/make_shared.hpp>
#include <boost/format.hpp>
#include <sstream>

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

namespace {
    /**************************************************************************
     * ADF4351 Controls
     *************************************************************************/
    /*!
     * \param lo_iface Reference to the LO object
     * \param freq Frequency (in Hz) of the tone to be generated from the LO
     * \param ref_clock_freq Frequency (in Hz) of the reference clock at the
     *                       PLL input of the LO
     * \param int_n_mode Integer-N mode on or off
     */
    double _lo_set_frequency(
        adf435x_iface::sptr lo_iface,
        const double freq,
        const double ref_clock_freq,
        const bool int_n_mode
    ) {
        UHD_LOG_TRACE("MG/ADF4351",
            "Attempting to tune low band LO to " << freq <<
            " Hz with ref clock freq " << ref_clock_freq);
        lo_iface->set_feedback_select(adf435x_iface::FB_SEL_DIVIDED);
        lo_iface->set_reference_freq(ref_clock_freq);
        lo_iface->set_prescaler(adf435x_iface::PRESCALER_4_5);
        const double actual_freq = lo_iface->set_frequency(freq, int_n_mode);
        lo_iface->set_output_power(
            adf435x_iface::RF_OUTPUT_A,
            adf435x_iface::OUTPUT_POWER_2DBM
        );
        lo_iface->set_output_power(
            adf435x_iface::RF_OUTPUT_B,
            adf435x_iface::OUTPUT_POWER_2DBM
        );
        lo_iface->set_charge_pump_current(
                adf435x_iface::CHARGE_PUMP_CURRENT_0_31MA);
        return actual_freq;
    }

    /*! Configure and enable LO
     *
     * Will tune it to requested frequency and enable outputs.
     *
     * \param lo_iface Reference to the LO object
     * \param lo_freq Frequency (in Hz) of the tone to be generated from the LO
     * \param ref_clock_freq Frequency (in Hz) of the reference clock at the
     *                       PLL input of the LO
     * \param int_n_mode Integer-N mode on or off
     * \returns the actual frequency the LO is running at
     */
    double _lo_enable(
        adf435x_iface::sptr lo_iface,
        const double lo_freq,
        const double ref_clock_freq,
        const bool int_n_mode
    ) {
        const double actual_lo_freq =
            _lo_set_frequency(lo_iface, lo_freq, ref_clock_freq, int_n_mode);
        lo_iface->set_output_enable(adf435x_iface::RF_OUTPUT_A, true);
        lo_iface->set_output_enable(adf435x_iface::RF_OUTPUT_B, true);
        lo_iface->commit();
        return actual_lo_freq;
    }

    /*! Disable LO
     */
    void _lo_disable(adf435x_iface::sptr lo_iface)
    {
        lo_iface->set_output_enable(adf435x_iface::RF_OUTPUT_A, false);
        lo_iface->set_output_enable(adf435x_iface::RF_OUTPUT_B, false);
        lo_iface->commit();
    }

    // TODO: remove this helper when there are only 2 radios
    fs_path master_fe_base_path(const std::string &radio_slot)
    {
        if (radio_slot == "B") {
            return fs_path("dboards") / "A";
        }
        if (radio_slot == "D") {
            return fs_path("dboards") / "C";
        }
        UHD_THROW_INVALID_CODE_PATH();
    }
}


/******************************************************************************
 * Structors
 *****************************************************************************/
UHD_RFNOC_RADIO_BLOCK_CONSTRUCTOR(magnesium_radio_ctrl)
{
    UHD_LOG_TRACE(unique_id(), "Entering magnesium_radio_ctrl_impl ctor...");
    UHD_LOG_DEBUG(unique_id(), "Note: Running in one-block-per-channel mode!");
    const char radio_slot_name[4] = {'A', 'B', 'C', 'D'};
    _radio_slot = radio_slot_name[get_block_id().get_block_count()];
    UHD_LOG_TRACE(unique_id(), "Radio slot: " << _radio_slot);
    _master = _radio_slot == "A" or _radio_slot == "C";
    UHD_LOG_DEBUG(unique_id(),
        "Radio type: " << (_master ? "master" : "slave"));
    _rpc_prefix =
        (_radio_slot == "A" or _radio_slot == "B") ? "db_0_" : "db_1_";

    _init_defaults();
    _init_peripherals();
    _init_prop_tree();
}

magnesium_radio_ctrl_impl::~magnesium_radio_ctrl_impl()
{
    UHD_LOG_TRACE(unique_id(), "magnesium_radio_ctrl_impl::dtor() ");
}


/******************************************************************************
 * API Calls
 *****************************************************************************/
double magnesium_radio_ctrl_impl::set_rate(double rate)
{
    // TODO: implement
    if (rate != get_rate()) {
        UHD_LOG_WARNING(unique_id(),
                "Attempting to set sampling rate to invalid value " << rate);
    }
    return get_rate();
}

void magnesium_radio_ctrl_impl::set_tx_antenna(
        const std::string &ant,
        const size_t chan
) {
    if (ant != get_tx_antenna(chan)) {
        throw uhd::value_error(str(
            boost::format("[%s] Requesting invalid TX antenna value: %s")
            % unique_id()
            % ant
        ));
    }
    // We can't actually set the TX antenna, so let's stop here.
}

void magnesium_radio_ctrl_impl::set_rx_antenna(
        const std::string &ant,
        const size_t chan
) {
    UHD_ASSERT_THROW(chan <= MAGNESIUM_NUM_CHANS);
    if (std::find(MAGNESIUM_RX_ANTENNAS.begin(),
                  MAGNESIUM_RX_ANTENNAS.end(),
                  ant) == MAGNESIUM_RX_ANTENNAS.end()) {
        throw uhd::value_error(str(
            boost::format("[%s] Requesting invalid RX antenna value: %s")
            % unique_id()
            % ant
        ));
    }
    UHD_LOG_TRACE(unique_id(),
        "Setting RX antenna to " << ant << " for chan " << chan);
    magnesium_cpld_ctrl::chan_sel_t chan_sel  =
        _master ? magnesium_cpld_ctrl::CHAN1 : magnesium_cpld_ctrl::CHAN2;
    _update_atr_switches(chan_sel, RX_DIRECTION, ant);

    radio_ctrl_impl::set_rx_antenna(ant, chan); // we don't use _master here since each radio has one antenna.
}

double magnesium_radio_ctrl_impl::set_tx_frequency(
        const double freq,
        const size_t chan
) {
    // Note: There is only one LO per tx or TX, so changing frequency will
    // affect the adjacent channel in the same direction. We have to make sure
    // that getters will always tell the truth! This is true for low and high
    // bands.
    UHD_LOG_TRACE(unique_id(),
        "set_tx_frequency(f=" << freq << ", chan=" << chan << ")");
    if (not _master) {
        const fs_path master_tx_fe_path =
            master_fe_base_path(_radio_slot) / fs_path("tx_frontends") / chan;
        UHD_LOG_DEBUG(unique_id(),
                      "Slave setting TX frequency");
        return _tree->access<double>(master_tx_fe_path / "freq" / "value")
            .set(freq)
            .get();
    }
    // We need to set the switches on both channels, because they share an LO.
    // This way, if we tune channel 0 it will not put channel 1 into a bad
    // state.
    _update_tx_freq_switches(freq, _tx_bypass_amp, magnesium_cpld_ctrl::BOTH);
    //double ad9371_freq = freq;
    double if_freq = 0.0;
    auto lo_iface = _tx_lo;

    if (freq < MAGNESIUM_LOWBAND_FREQ) { // Low band
        if_freq = MAGNESIUM_TX_IF_FREQ ;
        const double lo_freq = if_freq - freq;
        const bool int_n_mode = false; // FIXME no hardcode
        //const double actual_lo_freq =
            _lo_enable(lo_iface, lo_freq, _master_clock_rate, int_n_mode);
        //ad9371_freq = actual_lo_freq - freq;
    } else {
        _lo_disable(lo_iface);
    }

    //const double actual_ad9371_freq =
        _ad9371->set_frequency(freq, chan, TX_DIRECTION);
    radio_ctrl_impl::set_tx_frequency(freq, chan);
    return freq; // FIXME calc the actual frequency
}

double magnesium_radio_ctrl_impl::set_rx_frequency(
        const double freq,
        const size_t chan
) {
    // Note: There is only one LO per RX or TX, so changing frequency will
    // affect the adjacent channel in the same direction. We have to make sure
    // that getters will always tell the truth! This is true for low and high
    // bands.
    UHD_LOG_TRACE(unique_id(),
        "set_rx_frequency(f=" << freq << ", chan=" << chan << ")");
    if (not _master) {
        const fs_path master_rx_fe_path =
            master_fe_base_path(_radio_slot) / fs_path("rx_frontends") / chan;
        UHD_LOG_DEBUG(unique_id(),
                      "Slave setting RX frequency");
        return _tree->access<double>(master_rx_fe_path / "freq" / "value")
            .set(freq)
            .get();
    }
    // We need to set the switches on both channels, because they share an LO.
    // This way, if we tune channel 0 it will not put channel 1 into a bad
    // state.
    _update_rx_freq_switches(freq, _rx_bypass_lnas, magnesium_cpld_ctrl::BOTH);
    //double ad9371_freq = freq;
    double if_freq = 0.0;
    auto lo_iface = _rx_lo;

    if (freq < MAGNESIUM_LOWBAND_FREQ) { // Low band
        if_freq = MAGNESIUM_RX_IF_FREQ ;
        const double lo_freq = if_freq - freq;
        const bool int_n_mode = false; // FIXME no hardcode
        //const double actual_lo_freq =
            _lo_enable(lo_iface, lo_freq, _master_clock_rate, int_n_mode);
        //ad9371_freq = actual_lo_freq - freq;
    } else {
        _lo_disable(lo_iface);
    }

    //const double actual_ad9371_freq =
    _ad9371->set_frequency(freq, chan, RX_DIRECTION);
    radio_ctrl_impl::set_rx_frequency(freq, chan);
    return freq; // FIXME calc the actual frequency
}

double magnesium_radio_ctrl_impl::get_tx_frequency(
    const size_t chan)
{
    UHD_LOG_TRACE(unique_id(),
                  "get_tx_frequency(chan=" << chan << ")");
    if (not _master) {
        const fs_path master_tx_fe_path =
            master_fe_base_path(_radio_slot) / fs_path("tx_frontends") / chan;
        UHD_LOG_TRACE(unique_id(), "Slave getting TX frequency");
        return _tree->access<double>(master_tx_fe_path / "freq" / "value").get();
    }
    return radio_ctrl_impl::get_tx_frequency(chan);// only master can get frequency chan here is always 0.
}

double magnesium_radio_ctrl_impl::get_rx_frequency(
    const size_t chan)
{
    UHD_LOG_TRACE(unique_id(),
                  "get_rx_frequency(chan=" << chan << ")");
    if (not _master) {
        const fs_path master_rx_fe_path =
            master_fe_base_path(_radio_slot) / fs_path("rx_frontends") / chan;
        UHD_LOG_TRACE(unique_id(), "Slave getting RX frequency");
        return _tree->access<double>(master_rx_fe_path / "freq" / "value").get();
    }
    return radio_ctrl_impl::get_rx_frequency(chan); // only master can get frequency chan here is always 0.
}

double magnesium_radio_ctrl_impl::set_rx_bandwidth(
        const double bandwidth,
        const size_t chan
) {
    radio_ctrl_impl::set_rx_bandwidth(bandwidth, chan);
    return _ad9371->set_bandwidth(bandwidth, chan, RX_DIRECTION);
}

double magnesium_radio_ctrl_impl::set_tx_bandwidth(
        const double bandwidth,
        const size_t chan
) {
    //radio_ctrl_impl::set_rx_bandwidth(bandwidth, chan);
    return _ad9371->set_bandwidth(bandwidth, chan, TX_DIRECTION);
}

double magnesium_radio_ctrl_impl::set_tx_gain(
        const double gain,
        const size_t chan
) {
    UHD_LOG_TRACE(unique_id(),
        "set_tx_gain(gain=" << gain << ", chan=" << chan << ")");
    const double coerced_gain = _set_all_gain(
        gain,
        this->get_tx_frequency(chan),
        chan,
        TX_DIRECTION
    );
    radio_ctrl_impl::set_tx_gain(coerced_gain, chan);
    return coerced_gain;
}

double magnesium_radio_ctrl_impl::set_rx_gain(
        const double gain,
        const size_t chan
) {
    UHD_LOG_TRACE(unique_id(),
        "set_rx_gain(gain=" << gain << ", chan=" << chan << ")");
    const double coerced_gain = _set_all_gain(
        gain,
        this->get_rx_frequency(chan),
        chan,
        RX_DIRECTION
    );
    radio_ctrl_impl::set_rx_gain(coerced_gain, chan);
    return coerced_gain;
}

std::vector<std::string> magnesium_radio_ctrl_impl::get_rx_lo_names(
        const size_t /*chan*/
) {
    return std::vector<std::string>{};
}

std::vector<std::string> magnesium_radio_ctrl_impl::get_rx_lo_sources(
        const std::string &/*name*/,
        const size_t /*chan*/
) {
    return std::vector<std::string>{};
}

freq_range_t magnesium_radio_ctrl_impl::get_rx_lo_freq_range(
        const std::string &/*name*/,
        const size_t /*chan*/
) {
    return freq_range_t{};
}

void magnesium_radio_ctrl_impl::set_rx_lo_source(
        const std::string &/*src*/,
        const std::string &/*name*/,
        const size_t /*chan*/
) {
    // FIXME
}

const std::string magnesium_radio_ctrl_impl::get_rx_lo_source(
        const std::string &/*name*/,
        const size_t /*chan*/
) {
    return ""; // FIXME
}

double magnesium_radio_ctrl_impl::set_rx_lo_freq(
        double /*freq*/,
        const std::string &/*name*/,
        const size_t /*chan*/
) {
    return 0.0; // FIXME
}

double magnesium_radio_ctrl_impl::get_rx_lo_freq(
        const std::string &/*name*/,
        const size_t /*chan*/
) {
    return 0.0; // FIXME
}

size_t magnesium_radio_ctrl_impl::get_chan_from_dboard_fe(
    const std::string &fe, const direction_t /* dir */
) {
    return boost::lexical_cast<size_t>(fe);
}

std::string magnesium_radio_ctrl_impl::get_dboard_fe_from_chan(
    const size_t chan,
    const direction_t /* dir */
) {
    return std::to_string(chan);
}

double magnesium_radio_ctrl_impl::get_output_samp_rate(size_t /* port */)
{
    return MAGNESIUM_RADIO_RATE;
}

void magnesium_radio_ctrl_impl::set_rpc_client(
    uhd::rpc_client::sptr rpcc,
    const uhd::device_addr_t &block_args
) {
    _rpcc = rpcc;
    _block_args = block_args;
    UHD_LOG_TRACE(unique_id(), "Instantiating AD9371 control object...");
    _ad9371 = magnesium_ad9371_iface::uptr(
        new magnesium_ad9371_iface(
            _rpcc,
            (_radio_slot == "A" or _radio_slot == "B") ? 0 : 1
        )
    );

    // Note: MCR gets set during the init() call (prior to this), which takes
    // in arguments from the device args. So if block_args contains a
    // master_clock_rate key, then it should better be whatever the device is
    // configured to do.
    _master_clock_rate = _rpcc->request_with_token<double>(
            _rpc_prefix + "get_master_clock_rate");
    if (block_args.cast<double>("master_clock_rate", _master_clock_rate)
            != _master_clock_rate) {
        throw uhd::runtime_error(str(
            boost::format("Master clock rate mismatch. Device returns %f MHz, "
                          "but should have been %f MHz.")
            % (_master_clock_rate / 1e6)
            % (block_args.cast<double>(
                    "master_clock_rate", _master_clock_rate) / 1e6)
        ));
    }
    UHD_LOG_DEBUG(unique_id(),
        "Master Clock Rate is: " << (_master_clock_rate / 1e6) << " MHz.");
    radio_ctrl_impl::set_rate(_master_clock_rate);

    // EEPROM paths subject to change FIXME
    const size_t db_idx = get_block_id().get_block_count();
    _tree->access<eeprom_map_t>(_root_path / "eeprom")
        .add_coerced_subscriber([this, db_idx](const eeprom_map_t& db_eeprom){
            this->_rpcc->notify_with_token("set_db_eeprom", db_idx, db_eeprom);
        })
        .set_publisher([this, db_idx](){
            return this->_rpcc->request_with_token<eeprom_map_t>(
                "get_db_eeprom", db_idx
            );
        })
    ;

    // Init sensors
    for (const auto &dir : std::vector<direction_t>{RX_DIRECTION, TX_DIRECTION}) {
        for (size_t chan_idx = 0; chan_idx < 1 /* num channels FIXME */; chan_idx++) {
            _init_mpm_sensors(dir, chan_idx);
        }
    }
}

bool magnesium_radio_ctrl_impl::get_lo_lock_status(
    const direction_t dir
) {
    if (not (bool(_rpcc))) {
        UHD_LOG_DEBUG(unique_id(),
            "Reported no LO lock due to lack of RPC connection.");
        return false;
    }

    const std::string trx = (dir == RX_DIRECTION) ? "rx" : "tx";
    const size_t chan = 0; // They're the same after all
    const double freq = (dir == RX_DIRECTION) ?
        get_rx_frequency(chan) :
        get_tx_frequency(chan);

    bool lo_lock = _rpcc->request_with_token<bool>(
        _rpc_prefix + "get_ad9371_lo_lock", trx);
    UHD_LOG_TRACE(unique_id(),
        "AD9371 " << trx << " LO reports lock: " << (lo_lock ? "Yes" : "No"));
    if (lo_lock && freq < MAGNESIUM_LOWBAND_FREQ) {
        lo_lock = lo_lock && _rpcc->request_with_token<bool>(
            _rpc_prefix + "get_lowband_lo_lock", trx);
        UHD_LOG_TRACE(unique_id(),
            "ADF4351 " << trx << " LO reports lock: "
            << (lo_lock ? "Yes" : "No"));
    }

    return lo_lock;
}

UHD_RFNOC_BLOCK_REGISTER(magnesium_radio_ctrl, "MagnesiumRadio");