Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / b100 / b100_impl.cpp @ 6ca39ad9

History | View | Annotate | Download (26 KB)

1 5dfc4581 Josh Blum
//
2 b7ff81c9 Josh Blum
// Copyright 2012 Ettus Research LLC
3 5dfc4581 Josh Blum
//
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 18abd4db Josh Blum
#include "apply_corrections.hpp"
19 5dfc4581 Josh Blum
#include "b100_impl.hpp"
20
#include "b100_ctrl.hpp"
21
#include "fpga_regs_standard.h"
22 ba088e27 Josh Blum
#include "usrp_i2c_addr.h"
23
#include "usrp_commands.h"
24 5dfc4581 Josh Blum
#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 5e01b9eb Nick Foster
#include <cstdio>
38 5dfc4581 Josh Blum
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 819dbc78 Ben Hilburn
const boost::uint16_t B100_PRODUCT_ID = 0x0002;
45 5dfc4581 Josh Blum
const boost::uint16_t FX2_VENDOR_ID    = 0x04b4;
46
const boost::uint16_t FX2_PRODUCT_ID   = 0x8613;
47 c09e8817 Josh Blum
static const boost::posix_time::milliseconds REENUMERATION_TIMEOUT_MS(3000);
48 5dfc4581 Josh Blum
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 3fb011a2 Josh Blum
    //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 5dfc4581 Josh Blum
63 5e01b9eb Nick Foster
    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 5dfc4581 Josh Blum
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 e30cf4ec Josh Blum
    size_t found = 0;
81 5dfc4581 Josh Blum
    BOOST_FOREACH(usb_device_handle::sptr handle, usb_device_handle::get_device_list(vid, pid)) {
82 3fb011a2 Josh Blum
        //extract the firmware path for the b100
83
        std::string b100_fw_image;
84
        try{
85 5f49df7c Josh Blum
            b100_fw_image = find_image_path(hint.get("fw", B100_FW_FILE_NAME));
86 3fb011a2 Josh Blum
        }
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 5dfc4581 Josh Blum
        }
94 3fb011a2 Josh Blum
        UHD_LOG << "the  firmware image: " << b100_fw_image << std::endl;
95
96
        usb_control::sptr control;
97 c35462ad Josh Blum
        try{control = usb_control::make(handle, 0);}
98 3fb011a2 Josh Blum
        catch(const uhd::exception &){continue;} //ignore claimed
99
100
        fx2_ctrl::make(control)->usrp_load_firmware(b100_fw_image);
101 e30cf4ec Josh Blum
        found++;
102 5dfc4581 Josh Blum
    }
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 c09e8817 Josh Blum
    const boost::system_time timeout_time = boost::get_system_time() + REENUMERATION_TIMEOUT_MS;
109
110
    //search for the device until found or timeout
111 e30cf4ec Josh Blum
    while (boost::get_system_time() < timeout_time and b100_addrs.empty() and found != 0)
112 c09e8817 Josh Blum
    {
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 5dfc4581 Josh Blum
        }
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 ba088e27 Josh Blum
    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 1463a78f Josh Blum
    size_t initialization_count = 0;
154
    b100_impl_constructor_begin:
155
    initialization_count++;
156
157 9c598203 Josh Blum
    _tree = property_tree::make();
158 5dfc4581 Josh Blum
159
    //extract the FPGA path for the B100
160
    std::string b100_fpga_image = find_image_path(
161 5f49df7c Josh Blum
        device_addr.has_key("fpga")? device_addr["fpga"] : B100_FPGA_FILE_NAME
162 5dfc4581 Josh Blum
    );
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 544f8012 Nick Foster
    //create control objects
179 c35462ad Josh Blum
    usb_control::sptr fx2_transport = usb_control::make(handle, 0);
180 ba088e27 Josh Blum
    _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 71810ad1 Josh Blum
185 63991f79 Nick Foster
    //load FPGA image, slave xfers are disabled while loading
186 71810ad1 Josh Blum
    this->enable_gpif(false);
187 ba088e27 Josh Blum
    _fx2_ctrl->usrp_load_fpga(b100_fpga_image);
188 8bd255c5 Josh Blum
    _fx2_ctrl->usrp_fpga_reset(false); //active low reset
189
    _fx2_ctrl->usrp_fpga_reset(true);
190 ba088e27 Josh Blum
191 5dfc4581 Josh Blum
    //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 ba088e27 Josh Blum
    _ctrl_transport = usb_zero_copy::make(
199 5dfc4581 Josh Blum
        handle,
200 c35462ad Josh Blum
        4, 8, //interface, endpoint
201
        3, 4, //interface, endpoint
202 5dfc4581 Josh Blum
        ctrl_xport_args
203
    );
204 8bd255c5 Josh Blum
    while (_ctrl_transport->get_recv_buff(0.0)){} //flush ctrl xport
205 63991f79 Nick Foster
    this->enable_gpif(true);
206 5dfc4581 Josh Blum
207 ba088e27 Josh Blum
    ////////////////////////////////////////////////////////////////////
208 71810ad1 Josh Blum
    // Initialize FPGA wishbone communication
209 ba088e27 Josh Blum
    ////////////////////////////////////////////////////////////////////
210 6f4fb279 Josh Blum
    _fpga_ctrl = b100_ctrl::make(_ctrl_transport);
211 71810ad1 Josh Blum
    _fpga_ctrl->poke32(B100_REG_GLOBAL_RESET, 0); //global fpga reset
212 1463a78f Josh Blum
    //perform a test peek operation
213
    try{
214
        _fpga_ctrl->peek32(0);
215
    }
216
    //try reset once in the case of failure
217 dfaf1f93 Josh Blum
    catch(const uhd::exception &){
218 1463a78f Josh Blum
        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 71810ad1 Josh Blum
    this->check_fpga_compat(); //check after reset and making control
228 544f8012 Nick Foster
229
    ////////////////////////////////////////////////////////////////////
230 93e8f2eb Nick Foster
    // 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 544f8012 Nick Foster
    // 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 c35462ad Josh Blum
            2, 6,          // IN interface, endpoint
251
            1, 2,          // OUT interface, endpoint
252 544f8012 Nick Foster
            data_xport_args    // param hints
253 b7ff81c9 Josh Blum
        ),
254
        B100_MAX_PKT_BYTE_LIMIT
255 544f8012 Nick Foster
    );
256 8bd255c5 Josh Blum
    while (_data_transport->get_recv_buff(0.0)){} //flush data xport
257 544f8012 Nick Foster
258
    ////////////////////////////////////////////////////////////////////
259 ba088e27 Josh Blum
    // Initialize the properties tree
260
    ////////////////////////////////////////////////////////////////////
261
    _tree->create<std::string>("/name").set("B-Series Device");
262 da40a1ae Josh Blum
    const fs_path mb_path = "/mboards/0";
263 a6ec6e25 Nicholas Corgan
    _tree->create<std::string>(mb_path / "name").set("B100");
264
    _tree->create<std::string>(mb_path / "codename").set("B-Hundo");
265 f4dc4bf7 Josh Blum
    _tree->create<std::string>(mb_path / "load_eeprom")
266
        .subscribe(boost::bind(&fx2_ctrl::usrp_load_eeprom, _fx2_ctrl, _1));
267 ba088e27 Josh Blum
268
    ////////////////////////////////////////////////////////////////////
269
    // setup the mboard eeprom
270
    ////////////////////////////////////////////////////////////////////
271 1a695f94 Josh Blum
    const mboard_eeprom_t mb_eeprom(*_fx2_ctrl, mboard_eeprom_t::MAP_B100);
272 ba088e27 Josh Blum
    _tree->create<mboard_eeprom_t>(mb_path / "eeprom")
273
        .set(mb_eeprom)
274
        .subscribe(boost::bind(&b100_impl::set_mb_eeprom, this, _1));
275
276
    ////////////////////////////////////////////////////////////////////
277
    // create clock control objects
278
    ////////////////////////////////////////////////////////////////////
279
    //^^^ clock created up top, just reg props here... ^^^
280
    _tree->create<double>(mb_path / "tick_rate")
281
        .publish(boost::bind(&b100_clock_ctrl::get_fpga_clock_rate, _clock_ctrl))
282
        .subscribe(boost::bind(&b100_impl::update_tick_rate, this, _1));
283
284
    ////////////////////////////////////////////////////////////////////
285
    // create codec control objects
286
    ////////////////////////////////////////////////////////////////////
287
    _codec_ctrl = b100_codec_ctrl::make(_fpga_spi_ctrl);
288 da40a1ae Josh Blum
    const fs_path rx_codec_path = mb_path / "rx_codecs/A";
289
    const fs_path tx_codec_path = mb_path / "tx_codecs/A";
290 ba088e27 Josh Blum
    _tree->create<std::string>(rx_codec_path / "name").set("ad9522");
291
    _tree->create<meta_range_t>(rx_codec_path / "gains/pga/range").set(b100_codec_ctrl::rx_pga_gain_range);
292
    _tree->create<double>(rx_codec_path / "gains/pga/value")
293
        .coerce(boost::bind(&b100_impl::update_rx_codec_gain, this, _1));
294
    _tree->create<std::string>(tx_codec_path / "name").set("ad9522");
295
    _tree->create<meta_range_t>(tx_codec_path / "gains/pga/range").set(b100_codec_ctrl::tx_pga_gain_range);
296
    _tree->create<double>(tx_codec_path / "gains/pga/value")
297
        .subscribe(boost::bind(&b100_codec_ctrl::set_tx_pga_gain, _codec_ctrl, _1))
298
        .publish(boost::bind(&b100_codec_ctrl::get_tx_pga_gain, _codec_ctrl));
299
300
    ////////////////////////////////////////////////////////////////////
301
    // and do the misc mboard sensors
302
    ////////////////////////////////////////////////////////////////////
303 80dbdef7 Josh Blum
    _tree->create<sensor_value_t>(mb_path / "sensors/ref_locked")
304
        .publish(boost::bind(&b100_impl::get_ref_locked, this));
305 ba088e27 Josh Blum
306
    ////////////////////////////////////////////////////////////////////
307
    // create frontend control objects
308
    ////////////////////////////////////////////////////////////////////
309
    _rx_fe = rx_frontend_core_200::make(_fpga_ctrl, B100_REG_SR_ADDR(B100_SR_RX_FRONT));
310
    _tx_fe = tx_frontend_core_200::make(_fpga_ctrl, B100_REG_SR_ADDR(B100_SR_TX_FRONT));
311 bd08f403 Josh Blum
312 ba088e27 Josh Blum
    _tree->create<subdev_spec_t>(mb_path / "rx_subdev_spec")
313
        .subscribe(boost::bind(&b100_impl::update_rx_subdev_spec, this, _1));
314
    _tree->create<subdev_spec_t>(mb_path / "tx_subdev_spec")
315
        .subscribe(boost::bind(&b100_impl::update_tx_subdev_spec, this, _1));
316
317 dedfa652 Josh Blum
    const fs_path rx_fe_path = mb_path / "rx_frontends" / "A";
318 fbc5b54f Josh Blum
    const fs_path tx_fe_path = mb_path / "tx_frontends" / "A";
319 dedfa652 Josh Blum
320
    _tree->create<std::complex<double> >(rx_fe_path / "dc_offset" / "value")
321 bd08f403 Josh Blum
        .coerce(boost::bind(&rx_frontend_core_200::set_dc_offset, _rx_fe, _1))
322
        .set(std::complex<double>(0.0, 0.0));
323 dedfa652 Josh Blum
    _tree->create<bool>(rx_fe_path / "dc_offset" / "enable")
324 bd08f403 Josh Blum
        .subscribe(boost::bind(&rx_frontend_core_200::set_dc_offset_auto, _rx_fe, _1))
325
        .set(true);
326 dedfa652 Josh Blum
    _tree->create<std::complex<double> >(rx_fe_path / "iq_balance" / "value")
327 a6264508 Josh Blum
        .subscribe(boost::bind(&rx_frontend_core_200::set_iq_balance, _rx_fe, _1))
328 f91e247d Josh Blum
        .set(std::complex<double>(0.0, 0.0));
329 dedfa652 Josh Blum
    _tree->create<std::complex<double> >(tx_fe_path / "dc_offset" / "value")
330 bd08f403 Josh Blum
        .coerce(boost::bind(&tx_frontend_core_200::set_dc_offset, _tx_fe, _1))
331
        .set(std::complex<double>(0.0, 0.0));
332 dedfa652 Josh Blum
    _tree->create<std::complex<double> >(tx_fe_path / "iq_balance" / "value")
333 a6264508 Josh Blum
        .subscribe(boost::bind(&tx_frontend_core_200::set_iq_balance, _tx_fe, _1))
334 f91e247d Josh Blum
        .set(std::complex<double>(0.0, 0.0));
335 bd08f403 Josh Blum
336 ba088e27 Josh Blum
    ////////////////////////////////////////////////////////////////////
337
    // create rx dsp control objects
338
    ////////////////////////////////////////////////////////////////////
339
    _rx_dsps.push_back(rx_dsp_core_200::make(
340
        _fpga_ctrl, B100_REG_SR_ADDR(B100_SR_RX_DSP0), B100_REG_SR_ADDR(B100_SR_RX_CTRL0), B100_RX_SID_BASE + 0
341
    ));
342
    _rx_dsps.push_back(rx_dsp_core_200::make(
343
        _fpga_ctrl, B100_REG_SR_ADDR(B100_SR_RX_DSP1), B100_REG_SR_ADDR(B100_SR_RX_CTRL1), B100_RX_SID_BASE + 1
344
    ));
345
    for (size_t dspno = 0; dspno < _rx_dsps.size(); dspno++){
346 df910401 Josh Blum
        _rx_dsps[dspno]->set_link_rate(B100_LINK_RATE_BPS);
347 ba088e27 Josh Blum
        _tree->access<double>(mb_path / "tick_rate")
348
            .subscribe(boost::bind(&rx_dsp_core_200::set_tick_rate, _rx_dsps[dspno], _1));
349 da40a1ae Josh Blum
        fs_path rx_dsp_path = mb_path / str(boost::format("rx_dsps/%u") % dspno);
350 ae9e89d7 Josh Blum
        _tree->create<meta_range_t>(rx_dsp_path / "rate/range")
351
            .publish(boost::bind(&rx_dsp_core_200::get_host_rates, _rx_dsps[dspno]));
352 ba088e27 Josh Blum
        _tree->create<double>(rx_dsp_path / "rate/value")
353 ba0323c6 Josh Blum
            .set(1e6) //some default
354 ba088e27 Josh Blum
            .coerce(boost::bind(&rx_dsp_core_200::set_host_rate, _rx_dsps[dspno], _1))
355 ba0323c6 Josh Blum
            .subscribe(boost::bind(&b100_impl::update_rx_samp_rate, this, dspno, _1));
356 ba088e27 Josh Blum
        _tree->create<double>(rx_dsp_path / "freq/value")
357
            .coerce(boost::bind(&rx_dsp_core_200::set_freq, _rx_dsps[dspno], _1));
358
        _tree->create<meta_range_t>(rx_dsp_path / "freq/range")
359
            .publish(boost::bind(&rx_dsp_core_200::get_freq_range, _rx_dsps[dspno]));
360
        _tree->create<stream_cmd_t>(rx_dsp_path / "stream_cmd")
361
            .subscribe(boost::bind(&rx_dsp_core_200::issue_stream_command, _rx_dsps[dspno], _1));
362
    }
363 5dfc4581 Josh Blum
364 ba088e27 Josh Blum
    ////////////////////////////////////////////////////////////////////
365
    // create tx dsp control objects
366
    ////////////////////////////////////////////////////////////////////
367
    _tx_dsp = tx_dsp_core_200::make(
368
        _fpga_ctrl, B100_REG_SR_ADDR(B100_SR_TX_DSP), B100_REG_SR_ADDR(B100_SR_TX_CTRL), B100_TX_ASYNC_SID
369
    );
370 df910401 Josh Blum
    _tx_dsp->set_link_rate(B100_LINK_RATE_BPS);
371 ba088e27 Josh Blum
    _tree->access<double>(mb_path / "tick_rate")
372
        .subscribe(boost::bind(&tx_dsp_core_200::set_tick_rate, _tx_dsp, _1));
373 ae9e89d7 Josh Blum
    _tree->create<meta_range_t>(mb_path / "tx_dsps/0/rate/range")
374
        .publish(boost::bind(&tx_dsp_core_200::get_host_rates, _tx_dsp));
375 ba088e27 Josh Blum
    _tree->create<double>(mb_path / "tx_dsps/0/rate/value")
376 ba0323c6 Josh Blum
        .set(1e6) //some default
377 ba088e27 Josh Blum
        .coerce(boost::bind(&tx_dsp_core_200::set_host_rate, _tx_dsp, _1))
378 ba0323c6 Josh Blum
        .subscribe(boost::bind(&b100_impl::update_tx_samp_rate, this, 0, _1));
379 ba088e27 Josh Blum
    _tree->create<double>(mb_path / "tx_dsps/0/freq/value")
380
        .coerce(boost::bind(&tx_dsp_core_200::set_freq, _tx_dsp, _1));
381
    _tree->create<meta_range_t>(mb_path / "tx_dsps/0/freq/range")
382
        .publish(boost::bind(&tx_dsp_core_200::get_freq_range, _tx_dsp));
383
384
    ////////////////////////////////////////////////////////////////////
385
    // create time control objects
386
    ////////////////////////////////////////////////////////////////////
387
    time64_core_200::readback_bases_type time64_rb_bases;
388 5eec31fa Josh Blum
    time64_rb_bases.rb_hi_now = B100_REG_RB_TIME_NOW_HI;
389
    time64_rb_bases.rb_lo_now = B100_REG_RB_TIME_NOW_LO;
390
    time64_rb_bases.rb_hi_pps = B100_REG_RB_TIME_PPS_HI;
391
    time64_rb_bases.rb_lo_pps = B100_REG_RB_TIME_PPS_LO;
392 ba088e27 Josh Blum
    _time64 = time64_core_200::make(
393
        _fpga_ctrl, B100_REG_SR_ADDR(B100_SR_TIME64), time64_rb_bases
394
    );
395
    _tree->access<double>(mb_path / "tick_rate")
396
        .subscribe(boost::bind(&time64_core_200::set_tick_rate, _time64, _1));
397
    _tree->create<time_spec_t>(mb_path / "time/now")
398
        .publish(boost::bind(&time64_core_200::get_time_now, _time64))
399
        .subscribe(boost::bind(&time64_core_200::set_time_now, _time64, _1));
400
    _tree->create<time_spec_t>(mb_path / "time/pps")
401
        .publish(boost::bind(&time64_core_200::get_time_last_pps, _time64))
402
        .subscribe(boost::bind(&time64_core_200::set_time_next_pps, _time64, _1));
403
    //setup time source props
404
    _tree->create<std::string>(mb_path / "time_source/value")
405
        .subscribe(boost::bind(&time64_core_200::set_time_source, _time64, _1));
406
    _tree->create<std::vector<std::string> >(mb_path / "time_source/options")
407
        .publish(boost::bind(&time64_core_200::get_time_sources, _time64));
408
    //setup reference source props
409 4bcab9c5 Josh Blum
    _tree->create<std::string>(mb_path / "clock_source/value")
410
        .subscribe(boost::bind(&b100_impl::update_clock_source, this, _1));
411
    static const std::vector<std::string> clock_sources = boost::assign::list_of("internal")("external")("auto");
412
    _tree->create<std::vector<std::string> >(mb_path / "clock_source/options").set(clock_sources);
413 ba088e27 Josh Blum
414
    ////////////////////////////////////////////////////////////////////
415 781cafa8 Josh Blum
    // create user-defined control objects
416
    ////////////////////////////////////////////////////////////////////
417
    _user = user_settings_core_200::make(_fpga_ctrl, B100_REG_SR_ADDR(B100_SR_USER_REGS));
418
    _tree->create<user_settings_core_200::user_reg_t>(mb_path / "user/regs")
419
        .subscribe(boost::bind(&user_settings_core_200::set_reg, _user, _1));
420
421
    ////////////////////////////////////////////////////////////////////
422 ba088e27 Josh Blum
    // create dboard control objects
423
    ////////////////////////////////////////////////////////////////////
424
425
    //read the dboard eeprom to extract the dboard ids
426
    dboard_eeprom_t rx_db_eeprom, tx_db_eeprom, gdb_eeprom;
427
    rx_db_eeprom.load(*_fpga_i2c_ctrl, I2C_ADDR_RX_A);
428
    tx_db_eeprom.load(*_fpga_i2c_ctrl, I2C_ADDR_TX_A);
429
    gdb_eeprom.load(*_fpga_i2c_ctrl, I2C_ADDR_TX_A ^ 5);
430
431
    //create the properties and register subscribers
432
    _tree->create<dboard_eeprom_t>(mb_path / "dboards/A/rx_eeprom")
433
        .set(rx_db_eeprom)
434
        .subscribe(boost::bind(&b100_impl::set_db_eeprom, this, "rx", _1));
435
    _tree->create<dboard_eeprom_t>(mb_path / "dboards/A/tx_eeprom")
436
        .set(tx_db_eeprom)
437
        .subscribe(boost::bind(&b100_impl::set_db_eeprom, this, "tx", _1));
438
    _tree->create<dboard_eeprom_t>(mb_path / "dboards/A/gdb_eeprom")
439
        .set(gdb_eeprom)
440
        .subscribe(boost::bind(&b100_impl::set_db_eeprom, this, "gdb", _1));
441
442
    //create a new dboard interface and manager
443
    _dboard_iface = make_b100_dboard_iface(_fpga_ctrl, _fpga_i2c_ctrl, _fpga_spi_ctrl, _clock_ctrl, _codec_ctrl);
444
    _tree->create<dboard_iface::sptr>(mb_path / "dboards/A/iface").set(_dboard_iface);
445
    _dboard_manager = dboard_manager::make(
446 d9035414 Josh Blum
        rx_db_eeprom.id, tx_db_eeprom.id, gdb_eeprom.id,
447
        _dboard_iface, _tree->subtree(mb_path / "dboards/A")
448 ba088e27 Josh Blum
    );
449 5dfc4581 Josh Blum
450 18abd4db Josh Blum
    //bind frontend corrections to the dboard freq props
451
    const fs_path db_tx_fe_path = mb_path / "dboards" / "A" / "tx_frontends";
452
    BOOST_FOREACH(const std::string &name, _tree->list(db_tx_fe_path)){
453
        _tree->access<double>(db_tx_fe_path / name / "freq" / "value")
454
            .subscribe(boost::bind(&b100_impl::set_tx_fe_corrections, this, _1));
455
    }
456
    const fs_path db_rx_fe_path = mb_path / "dboards" / "A" / "rx_frontends";
457
    BOOST_FOREACH(const std::string &name, _tree->list(db_rx_fe_path)){
458
        _tree->access<double>(db_rx_fe_path / name / "freq" / "value")
459
            .subscribe(boost::bind(&b100_impl::set_rx_fe_corrections, this, _1));
460
    }
461
462 ba088e27 Josh Blum
    //initialize io handling
463
    this->io_init();
464 5dfc4581 Josh Blum
465 ba088e27 Josh Blum
    ////////////////////////////////////////////////////////////////////
466
    // do some post-init tasks
467
    ////////////////////////////////////////////////////////////////////
468 ba0323c6 Josh Blum
    this->update_rates();
469 5dfc4581 Josh Blum
470 ba0323c6 Josh Blum
    _tree->access<double>(mb_path / "tick_rate") //now subscribe the clock rate setter
471
        .subscribe(boost::bind(&b100_clock_ctrl::set_fpga_clock_rate, _clock_ctrl, _1));
472 5dfc4581 Josh Blum
473 bd7e53d3 Josh Blum
    //reset cordic rates and their properties to zero
474
    BOOST_FOREACH(const std::string &name, _tree->list(mb_path / "rx_dsps")){
475
        _tree->access<double>(mb_path / "rx_dsps" / name / "freq" / "value").set(0.0);
476
    }
477
    BOOST_FOREACH(const std::string &name, _tree->list(mb_path / "tx_dsps")){
478
        _tree->access<double>(mb_path / "tx_dsps" / name / "freq" / "value").set(0.0);
479
    }
480
481 d9035414 Josh Blum
    _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)));
482
    _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)));
483 4bcab9c5 Josh Blum
    _tree->access<std::string>(mb_path / "clock_source/value").set("internal");
484 ba088e27 Josh Blum
    _tree->access<std::string>(mb_path / "time_source/value").set("none");
485
}
486 5dfc4581 Josh Blum
487 ba088e27 Josh Blum
b100_impl::~b100_impl(void){
488 6f4fb279 Josh Blum
    //set an empty async callback now that we deconstruct
489
    _fpga_ctrl->set_async_cb(b100_ctrl::async_cb_type());
490 ba088e27 Josh Blum
}
491 5dfc4581 Josh Blum
492 ba088e27 Josh Blum
void b100_impl::check_fw_compat(void){
493
    unsigned char data[4]; //useless data buffer
494
    const boost::uint16_t fw_compat_num = _fx2_ctrl->usrp_control_read(
495
        VRQ_FW_COMPAT, 0, 0, data, sizeof(data)
496
    );
497
    if (fw_compat_num != B100_FW_COMPAT_NUM){
498
        throw uhd::runtime_error(str(boost::format(
499
            "Expected firmware compatibility number 0x%x, but got 0x%x:\n"
500
            "The firmware build is not compatible with the host code build."
501
        ) % B100_FW_COMPAT_NUM % fw_compat_num));
502
    }
503
}
504 5dfc4581 Josh Blum
505 ba088e27 Josh Blum
void b100_impl::check_fpga_compat(void){
506 0e7c8c38 Josh Blum
    const boost::uint32_t fpga_compat_num = _fpga_ctrl->peek32(B100_REG_RB_COMPAT);
507
    boost::uint16_t fpga_major = fpga_compat_num >> 16, fpga_minor = fpga_compat_num & 0xffff;
508
    if (fpga_major == 0){ //old version scheme
509
        fpga_major = fpga_minor;
510
        fpga_minor = 0;
511
    }
512
    if (fpga_major != B100_FPGA_COMPAT_NUM){
513 ba088e27 Josh Blum
        throw uhd::runtime_error(str(boost::format(
514 0e7c8c38 Josh Blum
            "Expected FPGA compatibility number %d, but got %d:\n"
515 ba088e27 Josh Blum
            "The FPGA build is not compatible with the host code build."
516 0e7c8c38 Josh Blum
        ) % int(B100_FPGA_COMPAT_NUM) % fpga_major));
517 ba088e27 Josh Blum
    }
518 0e7c8c38 Josh Blum
    _tree->create<std::string>("/mboards/0/fpga_version").set(str(boost::format("%u.%u") % fpga_major % fpga_minor));
519 5dfc4581 Josh Blum
}
520
521 ba088e27 Josh Blum
double b100_impl::update_rx_codec_gain(const double gain){
522
    //set gain on both I and Q, readback on one
523
    //TODO in the future, gains should have individual control
524
    _codec_ctrl->set_rx_pga_gain(gain, 'A');
525
    _codec_ctrl->set_rx_pga_gain(gain, 'B');
526
    return _codec_ctrl->get_rx_pga_gain('A');
527 5dfc4581 Josh Blum
}
528
529 ba088e27 Josh Blum
void b100_impl::set_mb_eeprom(const uhd::usrp::mboard_eeprom_t &mb_eeprom){
530 1a695f94 Josh Blum
    mb_eeprom.commit(*_fx2_ctrl, mboard_eeprom_t::MAP_B100);
531 5dfc4581 Josh Blum
}
532
533 ba088e27 Josh Blum
void b100_impl::set_db_eeprom(const std::string &type, const uhd::usrp::dboard_eeprom_t &db_eeprom){
534
    if (type == "rx") db_eeprom.store(*_fpga_i2c_ctrl, I2C_ADDR_RX_A);
535
    if (type == "tx") db_eeprom.store(*_fpga_i2c_ctrl, I2C_ADDR_TX_A);
536
    if (type == "gdb") db_eeprom.store(*_fpga_i2c_ctrl, I2C_ADDR_TX_A ^ 5);
537
}
538 5dfc4581 Josh Blum
539 4bcab9c5 Josh Blum
void b100_impl::update_clock_source(const std::string &source){
540 ba088e27 Josh Blum
    if      (source == "auto")     _clock_ctrl->use_auto_ref();
541
    else if (source == "internal") _clock_ctrl->use_internal_ref();
542 0e3240ec Josh Blum
    else if (source == "external") _clock_ctrl->use_external_ref();
543 ba088e27 Josh Blum
    else throw uhd::runtime_error("unhandled clock configuration reference source: " + source);
544
}
545 5dfc4581 Josh Blum
546 ba088e27 Josh Blum
////////////////// some GPIF preparation related stuff /////////////////
547 f4dc4bf7 Josh Blum
void b100_impl::enable_gpif(const bool en) {
548 ba088e27 Josh Blum
    _fx2_ctrl->usrp_control_write(VRQ_ENABLE_GPIF, en ? 1 : 0, 0, 0, 0);
549
}
550 5dfc4581 Josh Blum
551 f4dc4bf7 Josh Blum
void b100_impl::clear_fpga_fifo(void) {
552 ba088e27 Josh Blum
    _fx2_ctrl->usrp_control_write(VRQ_CLEAR_FPGA_FIFO, 0, 0, 0, 0);
553 5dfc4581 Josh Blum
}
554 80dbdef7 Josh Blum
555
sensor_value_t b100_impl::get_ref_locked(void){
556
    const bool lock = _clock_ctrl->get_locked();
557
    return sensor_value_t("Ref", lock, "locked", "unlocked");
558
}
559 18abd4db Josh Blum
560
void b100_impl::set_rx_fe_corrections(const double lo_freq){
561
    apply_rx_fe_corrections(this->get_tree()->subtree("/mboards/0"), "A", lo_freq);
562
}
563
564
void b100_impl::set_tx_fe_corrections(const double lo_freq){
565
    apply_tx_fe_corrections(this->get_tree()->subtree("/mboards/0"), "A", lo_freq);
566
}