Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / usrp1_impl.cpp @ ce3a95db

History | View | Annotate | Download (7.9 kB)

1
//
2
// Copyright 2010 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 "usrp1_impl.hpp"
19
#include "usrp1_ctrl.hpp"
20
#include "fpga_regs_standard.h"
21
#include "usrp_spi_defs.h"
22
#include <uhd/transport/usb_control.hpp>
23
#include <uhd/usrp/device_props.hpp>
24
#include <uhd/usrp/mboard_props.hpp>
25
#include <uhd/utils/warning.hpp>
26
#include <uhd/utils/assert.hpp>
27
#include <uhd/utils/static.hpp>
28
#include <uhd/utils/images.hpp>
29
#include <boost/format.hpp>
30
#include <boost/assign/list_of.hpp>
31
#include <boost/filesystem.hpp>
32
#include <boost/thread/thread.hpp>
33
#include <iostream>
34

    
35
using namespace uhd;
36
using namespace uhd::usrp;
37
using namespace uhd::transport;
38

    
39
const boost::uint16_t USRP1_VENDOR_ID  = 0xfffe;
40
const boost::uint16_t USRP1_PRODUCT_ID = 0x0002;
41
const boost::uint16_t FX2_VENDOR_ID    = 0x04b4;
42
const boost::uint16_t FX2_PRODUCT_ID   = 0x8613;
43

    
44
const std::vector<usrp1_impl::dboard_slot_t> usrp1_impl::_dboard_slots = boost::assign::list_of
45
    (usrp1_impl::DBOARD_SLOT_A)(usrp1_impl::DBOARD_SLOT_B)
46
;
47

    
48
/***********************************************************************
49
 * Discovery
50
 **********************************************************************/
51
static device_addrs_t usrp1_find(const device_addr_t &hint)
52
{
53
    device_addrs_t usrp1_addrs;
54

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

    
58
    //extract the firmware path for the USRP1
59
    std::string usrp1_fw_image;
60
    try{
61
        usrp1_fw_image = find_image_path(
62
            hint.has_key("fw")? hint["fw"] : "usrp1_fw.ihx"
63
        );
64
    }
65
    catch(const std::exception &e){
66
        uhd::print_warning(
67
            "Could not locate USRP1 firmware.\n"
68
            "Please install the images package.\n"
69
        );
70
        return usrp1_addrs;
71
    }
72
    //std::cout << "USRP1 firmware image: " << usrp1_fw_image << std::endl;
73

    
74
    boost::uint16_t vid = hint.has_key("uninit") ? FX2_VENDOR_ID : USRP1_VENDOR_ID;
75
    boost::uint16_t pid = hint.has_key("uninit") ? FX2_PRODUCT_ID : USRP1_PRODUCT_ID;
76

    
77
    //see what we got on the USB bus
78
    std::vector<usb_device_handle::sptr> device_list =
79
        usb_device_handle::get_device_list(vid, pid);
80

    
81
    if(device_list.size() == 0) return usrp1_addrs; //return nothing if no USRPs found
82

    
83
    //find the usrps and load firmware
84
    BOOST_FOREACH(usb_device_handle::sptr handle, device_list) {
85
            usb_control::sptr ctrl_transport = usb_control::make(handle);
86
            usrp_ctrl::sptr usrp_ctrl = usrp_ctrl::make(ctrl_transport);
87
            usrp_ctrl->usrp_load_firmware(usrp1_fw_image);
88
    }
89

    
90
    //get descriptors again with serial number, but using the initialized VID/PID now since we have firmware
91
    vid = USRP1_VENDOR_ID;
92
    pid = USRP1_PRODUCT_ID;
93
    device_list = usb_device_handle::get_device_list(vid, pid);
94

    
95
    BOOST_FOREACH(usb_device_handle::sptr handle, device_list) {
96
            device_addr_t new_addr;
97
            new_addr["type"] = "usrp1";
98
            new_addr["serial"] = handle->get_serial();
99
            usrp1_addrs.push_back(new_addr);
100
    }
101

    
102
    return usrp1_addrs;
103
}
104

    
105
/***********************************************************************
106
 * Make
107
 **********************************************************************/
108
static device::sptr usrp1_make(const device_addr_t &device_addr)
109
{
110
    //extract the FPGA path for the USRP1
111
    std::string usrp1_fpga_image = find_image_path(
112
        device_addr.has_key("fpga")? device_addr["fpga"] : "usrp1_fpga.rbf"
113
    );
114
    //std::cout << "USRP1 FPGA image: " << usrp1_fpga_image << std::endl;
115

    
116
    //try to match the given device address with something on the USB bus
117
    std::vector<usb_device_handle::sptr> device_list =
118
        usb_device_handle::get_device_list(USRP1_VENDOR_ID, USRP1_PRODUCT_ID);
119

    
120
    //create data and control transports
121
    usb_zero_copy::sptr data_transport;
122
    usrp_ctrl::sptr usrp_ctrl;
123

    
124

    
125
    BOOST_FOREACH(usb_device_handle::sptr handle, device_list) {
126
        if (handle->get_serial() == device_addr["serial"]) {
127
            usb_control::sptr ctrl_transport = usb_control::make(handle);
128
            usrp_ctrl = usrp_ctrl::make(ctrl_transport);
129
            usrp_ctrl->usrp_load_fpga(usrp1_fpga_image);
130

    
131
            data_transport = usb_zero_copy::make(handle,        // identifier
132
                                                 6,             // IN endpoint
133
                                                 2,             // OUT endpoint
134
                                                 2 * (1 << 20), // buffer size
135
                                                 16384);        // transfer size
136
            break;
137
        }
138
    }
139

    
140
    //create the usrp1 implementation guts
141
    return device::sptr(new usrp1_impl(data_transport, usrp_ctrl));
142
}
143

    
144
UHD_STATIC_BLOCK(register_usrp1_device){
145
    device::register_device(&usrp1_find, &usrp1_make);
146
}
147

    
148
/***********************************************************************
149
 * Structors
150
 **********************************************************************/
151
usrp1_impl::usrp1_impl(uhd::transport::usb_zero_copy::sptr data_transport,
152
                       usrp_ctrl::sptr ctrl_transport)
153
 : _data_transport(data_transport), _ctrl_transport(ctrl_transport)
154
{
155
    _iface = usrp1_iface::make(ctrl_transport);
156

    
157
    //create clock interface
158
    _clock_ctrl = usrp1_clock_ctrl::make(_iface);
159

    
160
    //create codec interface
161
    _codec_ctrls[DBOARD_SLOT_A] = usrp1_codec_ctrl::make(
162
        _iface, _clock_ctrl, SPI_ENABLE_CODEC_A
163
    );
164
    _codec_ctrls[DBOARD_SLOT_B] = usrp1_codec_ctrl::make(
165
        _iface, _clock_ctrl, SPI_ENABLE_CODEC_B
166
    );
167

    
168
    //initialize the codecs
169
    codec_init();
170

    
171
    //initialize the mboard
172
    mboard_init();
173

    
174
    //initialize the dboards 
175
    dboard_init();
176

    
177
    //initialize the dsps
178
    rx_dsp_init();
179

    
180
    //initialize the dsps
181
    tx_dsp_init();
182

    
183
    //initialize the send/recv
184
    io_init();
185

    
186
    //turn on the transmitter
187
    _ctrl_transport->usrp_tx_enable(true);
188

    
189
    //init the subdev specs
190
    this->mboard_set(MBOARD_PROP_RX_SUBDEV_SPEC, subdev_spec_t());
191
    this->mboard_set(MBOARD_PROP_TX_SUBDEV_SPEC, subdev_spec_t());
192
}
193

    
194
usrp1_impl::~usrp1_impl(void){
195
    /* NOP */
196
}
197

    
198
bool usrp1_impl::recv_async_msg(uhd::async_metadata_t &, size_t timeout_ms){
199
    //dummy fill-in for the recv_async_msg
200
    boost::this_thread::sleep(boost::posix_time::milliseconds(timeout_ms));
201
    return false;
202
}
203

    
204
/***********************************************************************
205
 * Device Get
206
 **********************************************************************/
207
void usrp1_impl::get(const wax::obj &key_, wax::obj &val)
208
{
209
    named_prop_t key = named_prop_t::extract(key_);
210

    
211
    //handle the get request conditioned on the key
212
    switch(key.as<device_prop_t>()){
213
    case DEVICE_PROP_NAME:
214
        val = std::string("usrp1 device");
215
        return;
216

    
217
    case DEVICE_PROP_MBOARD:
218
        UHD_ASSERT_THROW(key.name == "");
219
        val = _mboard_proxy->get_link();
220
        return;
221

    
222
    case DEVICE_PROP_MBOARD_NAMES:
223
        val = prop_names_t(1, ""); //vector of size 1 with empty string
224
        return;
225

    
226
    default: UHD_THROW_PROP_GET_ERROR();
227
    }
228
}
229

    
230
/***********************************************************************
231
 * Device Set
232
 **********************************************************************/
233
void usrp1_impl::set(const wax::obj &, const wax::obj &)
234
{
235
    UHD_THROW_PROP_SET_ERROR();
236
}