Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.2 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/utils/assert.hpp>
25
#include <uhd/utils/static.hpp>
26
#include <uhd/utils/images.hpp>
27
#include <boost/format.hpp>
28
#include <boost/assign/list_of.hpp>
29
#include <boost/filesystem.hpp>
30
#include <boost/thread/thread.hpp>
31
#include <iostream>
32

    
33
using namespace uhd;
34
using namespace uhd::usrp;
35
using namespace uhd::transport;
36

    
37
const std::vector<usrp1_impl::dboard_slot_t> usrp1_impl::_dboard_slots = boost::assign::list_of
38
    (usrp1_impl::DBOARD_SLOT_A)(usrp1_impl::DBOARD_SLOT_B)
39
;
40

    
41
/***********************************************************************
42
 * Discovery
43
 **********************************************************************/
44
static device_addrs_t usrp1_find(const device_addr_t &hint)
45
{
46
    device_addrs_t usrp1_addrs;
47

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

    
51
    //extract the firmware path for the USRP1
52
    std::string usrp1_fw_image = find_image_path(
53
        hint.has_key("fw")? hint["fw"] : "usrp1_fw.ihx"
54
    );
55
    std::cout << "USRP1 firmware image: " << usrp1_fw_image << std::endl;
56

    
57
    //see what we got on the USB bus
58
    std::vector<usb_device_handle::sptr> device_list =
59
        usb_device_handle::get_device_list();
60

    
61
    //find the usrps and load firmware
62
    BOOST_FOREACH(usb_device_handle::sptr handle, device_list) {
63
        if (handle->get_vendor_id() == 0xfffe &&
64
            handle->get_product_id() == 0x0002) {
65

    
66
            usb_control::sptr ctrl_transport = usb_control::make(handle);
67
            usrp_ctrl::sptr usrp_ctrl = usrp_ctrl::make(ctrl_transport);
68
            usrp_ctrl->usrp_load_firmware(usrp1_fw_image);
69
        }
70
    }
71

    
72
    //wait for things to settle
73
    boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
74

    
75
    //get descriptors again with serial number
76
    device_list = usb_device_handle::get_device_list();
77

    
78
    BOOST_FOREACH(usb_device_handle::sptr handle, device_list) {
79
        if (handle->get_vendor_id() == 0xfffe &&
80
            handle->get_product_id() == 0x0002) {
81

    
82
            device_addr_t new_addr;
83
            new_addr["type"] = "usrp1";
84
            new_addr["serial"] = handle->get_serial();
85
            usrp1_addrs.push_back(new_addr);
86
        }
87
    }
88

    
89
    return usrp1_addrs;
90
}
91

    
92
/***********************************************************************
93
 * Make
94
 **********************************************************************/
95
static device::sptr usrp1_make(const device_addr_t &device_addr)
96
{
97
    //extract the FPGA path for the USRP1
98
    std::string usrp1_fpga_image = find_image_path(
99
        device_addr.has_key("fpga")? device_addr["fpga"] : "usrp1_fpga.rbf"
100
    );
101
    std::cout << "USRP1 FPGA image: " << usrp1_fpga_image << std::endl;
102

    
103
    //try to match the given device address with something on the USB bus
104
    std::vector<usb_device_handle::sptr> device_list =
105
        usb_device_handle::get_device_list();
106

    
107
    //create data and control transports
108
    usb_zero_copy::sptr data_transport;
109
    usrp_ctrl::sptr usrp_ctrl;
110

    
111
    BOOST_FOREACH(usb_device_handle::sptr handle, device_list) {
112
        if (handle->get_vendor_id() == 0xfffe &&
113
            handle->get_product_id() == 0x0002 &&
114
            handle->get_serial() == device_addr["serial"]) {
115

    
116
            usb_control::sptr ctrl_transport = usb_control::make(handle);
117
            usrp_ctrl = usrp_ctrl::make(ctrl_transport);
118
            usrp_ctrl->usrp_load_fpga(usrp1_fpga_image);
119

    
120
            data_transport = usb_zero_copy::make(handle,        // identifier
121
                                                 6,             // IN endpoint
122
                                                 2,             // OUT endpoint
123
                                                 2 * (1 << 20), // buffer size
124
                                                 16384);        // transfer size
125
            break;
126
        }
127
    }
128

    
129
    //create the usrp1 implementation guts
130
    return device::sptr(new usrp1_impl(data_transport, usrp_ctrl));
131
}
132

    
133
UHD_STATIC_BLOCK(register_usrp1_device){
134
    device::register_device(&usrp1_find, &usrp1_make);
135
}
136

    
137
/***********************************************************************
138
 * Structors
139
 **********************************************************************/
140
usrp1_impl::usrp1_impl(uhd::transport::usb_zero_copy::sptr data_transport,
141
                       usrp_ctrl::sptr ctrl_transport)
142
 : _data_transport(data_transport), _ctrl_transport(ctrl_transport)
143
{
144
    _iface = usrp1_iface::make(ctrl_transport);
145

    
146
    //create clock interface
147
    _clock_ctrl = usrp1_clock_ctrl::make(_iface);
148

    
149
    //create codec interface
150
    _codec_ctrls[DBOARD_SLOT_A] = usrp1_codec_ctrl::make(
151
        _iface, _clock_ctrl, SPI_ENABLE_CODEC_A
152
    );
153
    _codec_ctrls[DBOARD_SLOT_B] = usrp1_codec_ctrl::make(
154
        _iface, _clock_ctrl, SPI_ENABLE_CODEC_B
155
    );
156

    
157
    //initialize the codecs
158
    codec_init();
159

    
160
    //initialize the mboard
161
    mboard_init();
162

    
163
    //initialize the dboards 
164
    dboard_init();
165

    
166
    //initialize the dsps
167
    rx_dsp_init();
168

    
169
    //initialize the dsps
170
    tx_dsp_init();
171

    
172
    //initialize the send/recv
173
    io_init();
174

    
175
    //turn on the transmitter
176
    _ctrl_transport->usrp_tx_enable(true);
177
}
178

    
179
usrp1_impl::~usrp1_impl(void){
180
    /* NOP */
181
}
182

    
183
bool usrp1_impl::recv_async_msg(uhd::async_metadata_t &, size_t timeout_ms){
184
    //dummy fill-in for the recv_async_msg
185
    boost::this_thread::sleep(boost::posix_time::milliseconds(timeout_ms));
186
    return false;
187
}
188

    
189
/***********************************************************************
190
 * Device Get
191
 **********************************************************************/
192
void usrp1_impl::get(const wax::obj &key_, wax::obj &val)
193
{
194
    named_prop_t key = named_prop_t::extract(key_);
195

    
196
    //handle the get request conditioned on the key
197
    switch(key.as<device_prop_t>()){
198
    case DEVICE_PROP_NAME:
199
        val = std::string("usrp1 device");
200
        return;
201

    
202
    case DEVICE_PROP_MBOARD:
203
        UHD_ASSERT_THROW(key.name == "");
204
        val = _mboard_proxy->get_link();
205
        return;
206

    
207
    case DEVICE_PROP_MBOARD_NAMES:
208
        val = prop_names_t(1, ""); //vector of size 1 with empty string
209
        return;
210

    
211
    default: UHD_THROW_PROP_GET_ERROR();
212
    }
213
}
214

    
215
/***********************************************************************
216
 * Device Set
217
 **********************************************************************/
218
void usrp1_impl::set(const wax::obj &, const wax::obj &)
219
{
220
    UHD_THROW_PROP_SET_ERROR();
221
}