Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / b100 / b100_impl.cpp @ 63991f79

History | View | Annotate | Download (26 KB)

1
//
2
// Copyright 2012 Ettus Research LLC
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

    
18
#include "apply_corrections.hpp"
19
#include "b100_impl.hpp"
20
#include "b100_ctrl.hpp"
21
#include "fpga_regs_standard.h"
22
#include "usrp_i2c_addr.h"
23
#include "usrp_commands.h"
24
#include <uhd/transport/usb_control.hpp>
25
#include "ctrl_packet.hpp"
26
#include <uhd/utils/msg.hpp>
27
#include <uhd/exception.hpp>
28
#include <uhd/utils/static.hpp>
29
#include <uhd/utils/images.hpp>
30
#include <uhd/utils/safe_call.hpp>
31
#include <boost/format.hpp>
32
#include <boost/assign/list_of.hpp>
33
#include <boost/filesystem.hpp>
34
#include <boost/thread/thread.hpp>
35
#include <boost/lexical_cast.hpp>
36
#include "b100_regs.hpp"
37
#include <cstdio>
38

    
39
using namespace uhd;
40
using namespace uhd::usrp;
41
using namespace uhd::transport;
42

    
43
const boost::uint16_t B100_VENDOR_ID  = 0x2500;
44
const boost::uint16_t B100_PRODUCT_ID = 0x0002;
45
const boost::uint16_t FX2_VENDOR_ID    = 0x04b4;
46
const boost::uint16_t FX2_PRODUCT_ID   = 0x8613;
47
static const boost::posix_time::milliseconds REENUMERATION_TIMEOUT_MS(3000);
48

    
49
/***********************************************************************
50
 * Discovery
51
 **********************************************************************/
52
static device_addrs_t b100_find(const device_addr_t &hint)
53
{
54
    device_addrs_t b100_addrs;
55

    
56
    //return an empty list of addresses when type is set to non-b100
57
    if (hint.has_key("type") and hint["type"] != "b100") return b100_addrs;
58

    
59
    //Return an empty list of addresses when an address is specified,
60
    //since an address is intended for a different, non-USB, device.
61
    if (hint.has_key("addr")) return b100_addrs;
62

    
63
    unsigned int vid, pid;
64

    
65
    if(hint.has_key("vid") && hint.has_key("pid") && hint.has_key("type") && hint["type"] == "b100") {
66
        sscanf(hint.get("vid").c_str(), "%x", &vid);
67
        sscanf(hint.get("pid").c_str(), "%x", &pid);
68
    } else {
69
        vid = B100_VENDOR_ID;
70
        pid = B100_PRODUCT_ID;
71
    }
72

    
73
    // Important note:
74
    // The get device list calls are nested inside the for loop.
75
    // This allows the usb guts to decontruct when not in use,
76
    // so that re-enumeration after fw load can occur successfully.
77
    // This requirement is a courtesy of libusb1.0 on windows.
78

    
79
    //find the usrps and load firmware
80
    size_t found = 0;
81
    BOOST_FOREACH(usb_device_handle::sptr handle, usb_device_handle::get_device_list(vid, pid)) {
82
        //extract the firmware path for the b100
83
        std::string b100_fw_image;
84
        try{
85
            b100_fw_image = find_image_path(hint.get("fw", B100_FW_FILE_NAME));
86
        }
87
        catch(...){
88
            UHD_MSG(warning) << boost::format(
89
                "Could not locate B100 firmware.\n"
90
                "Please install the images package.\n"
91
            );
92
            return b100_addrs;
93
        }
94
        UHD_LOG << "the  firmware image: " << b100_fw_image << std::endl;
95

    
96
        usb_control::sptr control;
97
        try{control = usb_control::make(handle, 0);}
98
        catch(const uhd::exception &){continue;} //ignore claimed
99

    
100
        fx2_ctrl::make(control)->usrp_load_firmware(b100_fw_image);
101
        found++;
102
    }
103

    
104
    //get descriptors again with serial number, but using the initialized VID/PID now since we have firmware
105
    vid = B100_VENDOR_ID;
106
    pid = B100_PRODUCT_ID;
107

    
108
    const boost::system_time timeout_time = boost::get_system_time() + REENUMERATION_TIMEOUT_MS;
109

    
110
    //search for the device until found or timeout
111
    while (boost::get_system_time() < timeout_time and b100_addrs.empty() and found != 0)
112
    {
113
        BOOST_FOREACH(usb_device_handle::sptr handle, usb_device_handle::get_device_list(vid, pid))
114
        {
115
            usb_control::sptr control;
116
            try{control = usb_control::make(handle, 0);}
117
            catch(const uhd::exception &){continue;} //ignore claimed
118

    
119
            fx2_ctrl::sptr fx2_ctrl = fx2_ctrl::make(control);
120
            const mboard_eeprom_t mb_eeprom = mboard_eeprom_t(*fx2_ctrl, mboard_eeprom_t::MAP_B100);
121
            device_addr_t new_addr;
122
            new_addr["type"] = "b100";
123
            new_addr["name"] = mb_eeprom["name"];
124
            new_addr["serial"] = handle->get_serial();
125
            //this is a found b100 when the hint serial and name match or blank
126
            if (
127
                (not hint.has_key("name")   or hint["name"]   == new_addr["name"]) and
128
                (not hint.has_key("serial") or hint["serial"] == new_addr["serial"])
129
            ){
130
                b100_addrs.push_back(new_addr);
131
            }
132
        }
133
    }
134

    
135
    return b100_addrs;
136
}
137

    
138
/***********************************************************************
139
 * Make
140
 **********************************************************************/
141
static device::sptr b100_make(const device_addr_t &device_addr){
142
    return device::sptr(new b100_impl(device_addr));
143
}
144

    
145
UHD_STATIC_BLOCK(register_b100_device){
146
    device::register_device(&b100_find, &b100_make);
147
}
148

    
149
/***********************************************************************
150
 * Structors
151
 **********************************************************************/
152
b100_impl::b100_impl(const device_addr_t &device_addr){
153
    size_t initialization_count = 0;
154
    b100_impl_constructor_begin:
155
    initialization_count++;
156

    
157
    _tree = property_tree::make();
158

    
159
    //extract the FPGA path for the B100
160
    std::string b100_fpga_image = find_image_path(
161
        device_addr.has_key("fpga")? device_addr["fpga"] : B100_FPGA_FILE_NAME
162
    );
163

    
164
    //try to match the given device address with something on the USB bus
165
    std::vector<usb_device_handle::sptr> device_list =
166
        usb_device_handle::get_device_list(B100_VENDOR_ID, B100_PRODUCT_ID);
167

    
168
    //locate the matching handle in the device list
169
    usb_device_handle::sptr handle;
170
    BOOST_FOREACH(usb_device_handle::sptr dev_handle, device_list) {
171
        if (dev_handle->get_serial() == device_addr["serial"]){
172
            handle = dev_handle;
173
            break;
174
        }
175
    }
176
    UHD_ASSERT_THROW(handle.get() != NULL); //better be found
177

    
178
    //create control objects
179
    usb_control::sptr fx2_transport = usb_control::make(handle, 0);
180
    _fx2_ctrl = fx2_ctrl::make(fx2_transport);
181
    this->check_fw_compat(); //check after making fx2
182
    //-- setup clock after making fx2 and before loading fpga --//
183
    _clock_ctrl = b100_clock_ctrl::make(_fx2_ctrl, device_addr.cast<double>("master_clock_rate", B100_DEFAULT_TICK_RATE));
184

    
185
    //load FPGA image, slave xfers are disabled while loading
186
    this->enable_gpif(false);
187
    _fx2_ctrl->usrp_load_fpga(b100_fpga_image);
188
    _fx2_ctrl->usrp_fpga_reset(false); //active low reset
189
    _fx2_ctrl->usrp_fpga_reset(true);
190

    
191
    //create the control transport
192
    device_addr_t ctrl_xport_args;
193
    ctrl_xport_args["recv_frame_size"] = boost::lexical_cast<std::string>(CTRL_PACKET_LENGTH);
194
    ctrl_xport_args["num_recv_frames"] = "16";
195
    ctrl_xport_args["send_frame_size"] = boost::lexical_cast<std::string>(CTRL_PACKET_LENGTH);
196
    ctrl_xport_args["num_send_frames"] = "4";
197

    
198
    _ctrl_transport = usb_zero_copy::make(
199
        handle,
200
        4, 8, //interface, endpoint
201
        3, 4, //interface, endpoint
202
        ctrl_xport_args
203
    );
204
    while (_ctrl_transport->get_recv_buff(0.0)){} //flush ctrl xport
205
    this->enable_gpif(true);
206

    
207
    ////////////////////////////////////////////////////////////////////
208
    // Initialize FPGA wishbone communication
209
    ////////////////////////////////////////////////////////////////////
210
    _fpga_ctrl = b100_ctrl::make(_ctrl_transport);
211
    _fpga_ctrl->poke32(B100_REG_GLOBAL_RESET, 0); //global fpga reset
212
    //perform a test peek operation
213
    try{
214
        _fpga_ctrl->peek32(0);
215
    }
216
    //try reset once in the case of failure
217
    catch(const uhd::exception &){
218
        if (initialization_count > 1) throw;
219
        UHD_MSG(warning) <<
220
            "The control endpoint was left in a bad state.\n"
221
            "Attempting endpoint re-enumeration...\n" << std::endl;
222
        _fpga_ctrl.reset();
223
        _ctrl_transport.reset();
224
        _fx2_ctrl->usrp_fx2_reset();
225
        goto b100_impl_constructor_begin;
226
    }
227
    this->check_fpga_compat(); //check after reset and making control
228

    
229
    ////////////////////////////////////////////////////////////////////
230
    // Initialize peripherals after reset
231
    ////////////////////////////////////////////////////////////////////
232
    _fpga_i2c_ctrl = i2c_core_100::make(_fpga_ctrl, B100_REG_SLAVE(3));
233
    _fpga_spi_ctrl = spi_core_100::make(_fpga_ctrl, B100_REG_SLAVE(2));
234

    
235
    ////////////////////////////////////////////////////////////////////
236
    // Create data transport
237
    // This happens after FPGA ctrl instantiated so any junk that might
238
    // be in the FPGAs buffers doesn't get pulled into the transport
239
    // before being cleared.
240
    ////////////////////////////////////////////////////////////////////
241
    device_addr_t data_xport_args;
242
    data_xport_args["recv_frame_size"] = device_addr.get("recv_frame_size", "16384");
243
    data_xport_args["num_recv_frames"] = device_addr.get("num_recv_frames", "16");
244
    data_xport_args["send_frame_size"] = device_addr.get("send_frame_size", "16384");
245
    data_xport_args["num_send_frames"] = device_addr.get("num_send_frames", "16");
246

    
247
    _data_transport = usb_zero_copy::make_wrapper(
248
        usb_zero_copy::make(
249
            handle,        // identifier
250
            2, 6,          // IN interface, endpoint
251
            1, 2,          // OUT interface, endpoint
252
            data_xport_args    // param hints
253
        ),
254
        B100_MAX_PKT_BYTE_LIMIT
255
    );
256
    while (_data_transport->get_recv_buff(0.0)){} //flush data xport
257

    
258
    ////////////////////////////////////////////////////////////////////
259
    // Initialize the properties tree
260
    ////////////////////////////////////////////////////////////////////
261
    _tree->create<std::string>("/name").set("B-Series Device");
262
    const fs_path mb_path = "/mboards/0";
263
    _tree->create<std::string>(mb_path / "name").set("B100 (B-Hundo)");
264
    _tree->create<std::string>(mb_path / "load_eeprom")
265
        .subscribe(boost::bind(&fx2_ctrl::usrp_load_eeprom, _fx2_ctrl, _1));
266

    
267
    ////////////////////////////////////////////////////////////////////
268
    // setup the mboard eeprom
269
    ////////////////////////////////////////////////////////////////////
270
    const mboard_eeprom_t mb_eeprom(*_fx2_ctrl, mboard_eeprom_t::MAP_B100);
271
    _tree->create<mboard_eeprom_t>(mb_path / "eeprom")
272
        .set(mb_eeprom)
273
        .subscribe(boost::bind(&b100_impl::set_mb_eeprom, this, _1));
274

    
275
    ////////////////////////////////////////////////////////////////////
276
    // create clock control objects
277
    ////////////////////////////////////////////////////////////////////
278
    //^^^ clock created up top, just reg props here... ^^^
279
    _tree->create<double>(mb_path / "tick_rate")
280
        .publish(boost::bind(&b100_clock_ctrl::get_fpga_clock_rate, _clock_ctrl))
281
        .subscribe(boost::bind(&b100_impl::update_tick_rate, this, _1));
282

    
283
    ////////////////////////////////////////////////////////////////////
284
    // create codec control objects
285
    ////////////////////////////////////////////////////////////////////
286
    _codec_ctrl = b100_codec_ctrl::make(_fpga_spi_ctrl);
287
    const fs_path rx_codec_path = mb_path / "rx_codecs/A";
288
    const fs_path tx_codec_path = mb_path / "tx_codecs/A";
289
    _tree->create<std::string>(rx_codec_path / "name").set("ad9522");
290
    _tree->create<meta_range_t>(rx_codec_path / "gains/pga/range").set(b100_codec_ctrl::rx_pga_gain_range);
291
    _tree->create<double>(rx_codec_path / "gains/pga/value")
292
        .coerce(boost::bind(&b100_impl::update_rx_codec_gain, this, _1));
293
    _tree->create<std::string>(tx_codec_path / "name").set("ad9522");
294
    _tree->create<meta_range_t>(tx_codec_path / "gains/pga/range").set(b100_codec_ctrl::tx_pga_gain_range);
295
    _tree->create<double>(tx_codec_path / "gains/pga/value")
296
        .subscribe(boost::bind(&b100_codec_ctrl::set_tx_pga_gain, _codec_ctrl, _1))
297
        .publish(boost::bind(&b100_codec_ctrl::get_tx_pga_gain, _codec_ctrl));
298

    
299
    ////////////////////////////////////////////////////////////////////
300
    // and do the misc mboard sensors
301
    ////////////////////////////////////////////////////////////////////
302
    _tree->create<sensor_value_t>(mb_path / "sensors/ref_locked")
303
        .publish(boost::bind(&b100_impl::get_ref_locked, this));
304

    
305
    ////////////////////////////////////////////////////////////////////
306
    // create frontend control objects
307
    ////////////////////////////////////////////////////////////////////
308
    _rx_fe = rx_frontend_core_200::make(_fpga_ctrl, B100_REG_SR_ADDR(B100_SR_RX_FRONT));
309
    _tx_fe = tx_frontend_core_200::make(_fpga_ctrl, B100_REG_SR_ADDR(B100_SR_TX_FRONT));
310

    
311
    _tree->create<subdev_spec_t>(mb_path / "rx_subdev_spec")
312
        .subscribe(boost::bind(&b100_impl::update_rx_subdev_spec, this, _1));
313
    _tree->create<subdev_spec_t>(mb_path / "tx_subdev_spec")
314
        .subscribe(boost::bind(&b100_impl::update_tx_subdev_spec, this, _1));
315

    
316
    const fs_path rx_fe_path = mb_path / "rx_frontends" / "A";
317
    const fs_path tx_fe_path = mb_path / "tx_frontends" / "A";
318

    
319
    _tree->create<std::complex<double> >(rx_fe_path / "dc_offset" / "value")
320
        .coerce(boost::bind(&rx_frontend_core_200::set_dc_offset, _rx_fe, _1))
321
        .set(std::complex<double>(0.0, 0.0));
322
    _tree->create<bool>(rx_fe_path / "dc_offset" / "enable")
323
        .subscribe(boost::bind(&rx_frontend_core_200::set_dc_offset_auto, _rx_fe, _1))
324
        .set(true);
325
    _tree->create<std::complex<double> >(rx_fe_path / "iq_balance" / "value")
326
        .subscribe(boost::bind(&rx_frontend_core_200::set_iq_balance, _rx_fe, _1))
327
        .set(std::complex<double>(0.0, 0.0));
328
    _tree->create<std::complex<double> >(tx_fe_path / "dc_offset" / "value")
329
        .coerce(boost::bind(&tx_frontend_core_200::set_dc_offset, _tx_fe, _1))
330
        .set(std::complex<double>(0.0, 0.0));
331
    _tree->create<std::complex<double> >(tx_fe_path / "iq_balance" / "value")
332
        .subscribe(boost::bind(&tx_frontend_core_200::set_iq_balance, _tx_fe, _1))
333
        .set(std::complex<double>(0.0, 0.0));
334

    
335
    ////////////////////////////////////////////////////////////////////
336
    // create rx dsp control objects
337
    ////////////////////////////////////////////////////////////////////
338
    _rx_dsps.push_back(rx_dsp_core_200::make(
339
        _fpga_ctrl, B100_REG_SR_ADDR(B100_SR_RX_DSP0), B100_REG_SR_ADDR(B100_SR_RX_CTRL0), B100_RX_SID_BASE + 0
340
    ));
341
    _rx_dsps.push_back(rx_dsp_core_200::make(
342
        _fpga_ctrl, B100_REG_SR_ADDR(B100_SR_RX_DSP1), B100_REG_SR_ADDR(B100_SR_RX_CTRL1), B100_RX_SID_BASE + 1
343
    ));
344
    for (size_t dspno = 0; dspno < _rx_dsps.size(); dspno++){
345
        _rx_dsps[dspno]->set_link_rate(B100_LINK_RATE_BPS);
346
        _tree->access<double>(mb_path / "tick_rate")
347
            .subscribe(boost::bind(&rx_dsp_core_200::set_tick_rate, _rx_dsps[dspno], _1));
348
        fs_path rx_dsp_path = mb_path / str(boost::format("rx_dsps/%u") % dspno);
349
        _tree->create<meta_range_t>(rx_dsp_path / "rate/range")
350
            .publish(boost::bind(&rx_dsp_core_200::get_host_rates, _rx_dsps[dspno]));
351
        _tree->create<double>(rx_dsp_path / "rate/value")
352
            .set(1e6) //some default
353
            .coerce(boost::bind(&rx_dsp_core_200::set_host_rate, _rx_dsps[dspno], _1))
354
            .subscribe(boost::bind(&b100_impl::update_rx_samp_rate, this, dspno, _1));
355
        _tree->create<double>(rx_dsp_path / "freq/value")
356
            .coerce(boost::bind(&rx_dsp_core_200::set_freq, _rx_dsps[dspno], _1));
357
        _tree->create<meta_range_t>(rx_dsp_path / "freq/range")
358
            .publish(boost::bind(&rx_dsp_core_200::get_freq_range, _rx_dsps[dspno]));
359
        _tree->create<stream_cmd_t>(rx_dsp_path / "stream_cmd")
360
            .subscribe(boost::bind(&rx_dsp_core_200::issue_stream_command, _rx_dsps[dspno], _1));
361
    }
362

    
363
    ////////////////////////////////////////////////////////////////////
364
    // create tx dsp control objects
365
    ////////////////////////////////////////////////////////////////////
366
    _tx_dsp = tx_dsp_core_200::make(
367
        _fpga_ctrl, B100_REG_SR_ADDR(B100_SR_TX_DSP), B100_REG_SR_ADDR(B100_SR_TX_CTRL), B100_TX_ASYNC_SID
368
    );
369
    _tx_dsp->set_link_rate(B100_LINK_RATE_BPS);
370
    _tree->access<double>(mb_path / "tick_rate")
371
        .subscribe(boost::bind(&tx_dsp_core_200::set_tick_rate, _tx_dsp, _1));
372
    _tree->create<meta_range_t>(mb_path / "tx_dsps/0/rate/range")
373
        .publish(boost::bind(&tx_dsp_core_200::get_host_rates, _tx_dsp));
374
    _tree->create<double>(mb_path / "tx_dsps/0/rate/value")
375
        .set(1e6) //some default
376
        .coerce(boost::bind(&tx_dsp_core_200::set_host_rate, _tx_dsp, _1))
377
        .subscribe(boost::bind(&b100_impl::update_tx_samp_rate, this, 0, _1));
378
    _tree->create<double>(mb_path / "tx_dsps/0/freq/value")
379
        .coerce(boost::bind(&tx_dsp_core_200::set_freq, _tx_dsp, _1));
380
    _tree->create<meta_range_t>(mb_path / "tx_dsps/0/freq/range")
381
        .publish(boost::bind(&tx_dsp_core_200::get_freq_range, _tx_dsp));
382

    
383
    ////////////////////////////////////////////////////////////////////
384
    // create time control objects
385
    ////////////////////////////////////////////////////////////////////
386
    time64_core_200::readback_bases_type time64_rb_bases;
387
    time64_rb_bases.rb_hi_now = B100_REG_RB_TIME_NOW_HI;
388
    time64_rb_bases.rb_lo_now = B100_REG_RB_TIME_NOW_LO;
389
    time64_rb_bases.rb_hi_pps = B100_REG_RB_TIME_PPS_HI;
390
    time64_rb_bases.rb_lo_pps = B100_REG_RB_TIME_PPS_LO;
391
    _time64 = time64_core_200::make(
392
        _fpga_ctrl, B100_REG_SR_ADDR(B100_SR_TIME64), time64_rb_bases
393
    );
394
    _tree->access<double>(mb_path / "tick_rate")
395
        .subscribe(boost::bind(&time64_core_200::set_tick_rate, _time64, _1));
396
    _tree->create<time_spec_t>(mb_path / "time/now")
397
        .publish(boost::bind(&time64_core_200::get_time_now, _time64))
398
        .subscribe(boost::bind(&time64_core_200::set_time_now, _time64, _1));
399
    _tree->create<time_spec_t>(mb_path / "time/pps")
400
        .publish(boost::bind(&time64_core_200::get_time_last_pps, _time64))
401
        .subscribe(boost::bind(&time64_core_200::set_time_next_pps, _time64, _1));
402
    //setup time source props
403
    _tree->create<std::string>(mb_path / "time_source/value")
404
        .subscribe(boost::bind(&time64_core_200::set_time_source, _time64, _1));
405
    _tree->create<std::vector<std::string> >(mb_path / "time_source/options")
406
        .publish(boost::bind(&time64_core_200::get_time_sources, _time64));
407
    //setup reference source props
408
    _tree->create<std::string>(mb_path / "clock_source/value")
409
        .subscribe(boost::bind(&b100_impl::update_clock_source, this, _1));
410
    static const std::vector<std::string> clock_sources = boost::assign::list_of("internal")("external")("auto");
411
    _tree->create<std::vector<std::string> >(mb_path / "clock_source/options").set(clock_sources);
412

    
413
    ////////////////////////////////////////////////////////////////////
414
    // create user-defined control objects
415
    ////////////////////////////////////////////////////////////////////
416
    _user = user_settings_core_200::make(_fpga_ctrl, B100_REG_SR_ADDR(B100_SR_USER_REGS));
417
    _tree->create<user_settings_core_200::user_reg_t>(mb_path / "user/regs")
418
        .subscribe(boost::bind(&user_settings_core_200::set_reg, _user, _1));
419

    
420
    ////////////////////////////////////////////////////////////////////
421
    // create dboard control objects
422
    ////////////////////////////////////////////////////////////////////
423

    
424
    //read the dboard eeprom to extract the dboard ids
425
    dboard_eeprom_t rx_db_eeprom, tx_db_eeprom, gdb_eeprom;
426
    rx_db_eeprom.load(*_fpga_i2c_ctrl, I2C_ADDR_RX_A);
427
    tx_db_eeprom.load(*_fpga_i2c_ctrl, I2C_ADDR_TX_A);
428
    gdb_eeprom.load(*_fpga_i2c_ctrl, I2C_ADDR_TX_A ^ 5);
429

    
430
    //create the properties and register subscribers
431
    _tree->create<dboard_eeprom_t>(mb_path / "dboards/A/rx_eeprom")
432
        .set(rx_db_eeprom)
433
        .subscribe(boost::bind(&b100_impl::set_db_eeprom, this, "rx", _1));
434
    _tree->create<dboard_eeprom_t>(mb_path / "dboards/A/tx_eeprom")
435
        .set(tx_db_eeprom)
436
        .subscribe(boost::bind(&b100_impl::set_db_eeprom, this, "tx", _1));
437
    _tree->create<dboard_eeprom_t>(mb_path / "dboards/A/gdb_eeprom")
438
        .set(gdb_eeprom)
439
        .subscribe(boost::bind(&b100_impl::set_db_eeprom, this, "gdb", _1));
440

    
441
    //create a new dboard interface and manager
442
    _dboard_iface = make_b100_dboard_iface(_fpga_ctrl, _fpga_i2c_ctrl, _fpga_spi_ctrl, _clock_ctrl, _codec_ctrl);
443
    _tree->create<dboard_iface::sptr>(mb_path / "dboards/A/iface").set(_dboard_iface);
444
    _dboard_manager = dboard_manager::make(
445
        rx_db_eeprom.id, tx_db_eeprom.id, gdb_eeprom.id,
446
        _dboard_iface, _tree->subtree(mb_path / "dboards/A")
447
    );
448

    
449
    //bind frontend corrections to the dboard freq props
450
    const fs_path db_tx_fe_path = mb_path / "dboards" / "A" / "tx_frontends";
451
    BOOST_FOREACH(const std::string &name, _tree->list(db_tx_fe_path)){
452
        _tree->access<double>(db_tx_fe_path / name / "freq" / "value")
453
            .subscribe(boost::bind(&b100_impl::set_tx_fe_corrections, this, _1));
454
    }
455
    const fs_path db_rx_fe_path = mb_path / "dboards" / "A" / "rx_frontends";
456
    BOOST_FOREACH(const std::string &name, _tree->list(db_rx_fe_path)){
457
        _tree->access<double>(db_rx_fe_path / name / "freq" / "value")
458
            .subscribe(boost::bind(&b100_impl::set_rx_fe_corrections, this, _1));
459
    }
460

    
461
    //initialize io handling
462
    this->io_init();
463

    
464
    ////////////////////////////////////////////////////////////////////
465
    // do some post-init tasks
466
    ////////////////////////////////////////////////////////////////////
467
    this->update_rates();
468

    
469
    _tree->access<double>(mb_path / "tick_rate") //now subscribe the clock rate setter
470
        .subscribe(boost::bind(&b100_clock_ctrl::set_fpga_clock_rate, _clock_ctrl, _1));
471

    
472
    //reset cordic rates and their properties to zero
473
    BOOST_FOREACH(const std::string &name, _tree->list(mb_path / "rx_dsps")){
474
        _tree->access<double>(mb_path / "rx_dsps" / name / "freq" / "value").set(0.0);
475
    }
476
    BOOST_FOREACH(const std::string &name, _tree->list(mb_path / "tx_dsps")){
477
        _tree->access<double>(mb_path / "tx_dsps" / name / "freq" / "value").set(0.0);
478
    }
479

    
480
    _tree->access<subdev_spec_t>(mb_path / "rx_subdev_spec").set(subdev_spec_t("A:" + _tree->list(mb_path / "dboards/A/rx_frontends").at(0)));
481
    _tree->access<subdev_spec_t>(mb_path / "tx_subdev_spec").set(subdev_spec_t("A:" + _tree->list(mb_path / "dboards/A/tx_frontends").at(0)));
482
    _tree->access<std::string>(mb_path / "clock_source/value").set("internal");
483
    _tree->access<std::string>(mb_path / "time_source/value").set("none");
484
}
485

    
486
b100_impl::~b100_impl(void){
487
    //set an empty async callback now that we deconstruct
488
    _fpga_ctrl->set_async_cb(b100_ctrl::async_cb_type());
489
}
490

    
491
void b100_impl::check_fw_compat(void){
492
    unsigned char data[4]; //useless data buffer
493
    const boost::uint16_t fw_compat_num = _fx2_ctrl->usrp_control_read(
494
        VRQ_FW_COMPAT, 0, 0, data, sizeof(data)
495
    );
496
    if (fw_compat_num != B100_FW_COMPAT_NUM){
497
        throw uhd::runtime_error(str(boost::format(
498
            "Expected firmware compatibility number 0x%x, but got 0x%x:\n"
499
            "The firmware build is not compatible with the host code build."
500
        ) % B100_FW_COMPAT_NUM % fw_compat_num));
501
    }
502
}
503

    
504
void b100_impl::check_fpga_compat(void){
505
    const boost::uint32_t fpga_compat_num = _fpga_ctrl->peek32(B100_REG_RB_COMPAT);
506
    boost::uint16_t fpga_major = fpga_compat_num >> 16, fpga_minor = fpga_compat_num & 0xffff;
507
    if (fpga_major == 0){ //old version scheme
508
        fpga_major = fpga_minor;
509
        fpga_minor = 0;
510
    }
511
    if (fpga_major != B100_FPGA_COMPAT_NUM){
512
        throw uhd::runtime_error(str(boost::format(
513
            "Expected FPGA compatibility number %d, but got %d:\n"
514
            "The FPGA build is not compatible with the host code build."
515
        ) % int(B100_FPGA_COMPAT_NUM) % fpga_major));
516
    }
517
    _tree->create<std::string>("/mboards/0/fpga_version").set(str(boost::format("%u.%u") % fpga_major % fpga_minor));
518
}
519

    
520
double b100_impl::update_rx_codec_gain(const double gain){
521
    //set gain on both I and Q, readback on one
522
    //TODO in the future, gains should have individual control
523
    _codec_ctrl->set_rx_pga_gain(gain, 'A');
524
    _codec_ctrl->set_rx_pga_gain(gain, 'B');
525
    return _codec_ctrl->get_rx_pga_gain('A');
526
}
527

    
528
void b100_impl::set_mb_eeprom(const uhd::usrp::mboard_eeprom_t &mb_eeprom){
529
    mb_eeprom.commit(*_fx2_ctrl, mboard_eeprom_t::MAP_B100);
530
}
531

    
532
void b100_impl::set_db_eeprom(const std::string &type, const uhd::usrp::dboard_eeprom_t &db_eeprom){
533
    if (type == "rx") db_eeprom.store(*_fpga_i2c_ctrl, I2C_ADDR_RX_A);
534
    if (type == "tx") db_eeprom.store(*_fpga_i2c_ctrl, I2C_ADDR_TX_A);
535
    if (type == "gdb") db_eeprom.store(*_fpga_i2c_ctrl, I2C_ADDR_TX_A ^ 5);
536
}
537

    
538
void b100_impl::update_clock_source(const std::string &source){
539
    if      (source == "auto")     _clock_ctrl->use_auto_ref();
540
    else if (source == "internal") _clock_ctrl->use_internal_ref();
541
    else if (source == "external") _clock_ctrl->use_external_ref();
542
    else throw uhd::runtime_error("unhandled clock configuration reference source: " + source);
543
}
544

    
545
////////////////// some GPIF preparation related stuff /////////////////
546
void b100_impl::enable_gpif(const bool en) {
547
    _fx2_ctrl->usrp_control_write(VRQ_ENABLE_GPIF, en ? 1 : 0, 0, 0, 0);
548
}
549

    
550
void b100_impl::clear_fpga_fifo(void) {
551
    _fx2_ctrl->usrp_control_write(VRQ_CLEAR_FPGA_FIFO, 0, 0, 0, 0);
552
}
553

    
554
sensor_value_t b100_impl::get_ref_locked(void){
555
    const bool lock = _clock_ctrl->get_locked();
556
    return sensor_value_t("Ref", lock, "locked", "unlocked");
557
}
558

    
559
void b100_impl::set_rx_fe_corrections(const double lo_freq){
560
    apply_rx_fe_corrections(this->get_tree()->subtree("/mboards/0"), "A", lo_freq);
561
}
562

    
563
void b100_impl::set_tx_fe_corrections(const double lo_freq){
564
    apply_tx_fe_corrections(this->get_tree()->subtree("/mboards/0"), "A", lo_freq);
565
}