Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp2 / mboard_impl.cpp @ 11c83c60

History | View | Annotate | Download (12.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 "usrp2_impl.hpp"
19
#include "usrp2_regs.hpp"
20
#include <uhd/usrp/misc_utils.hpp>
21
#include <uhd/usrp/dsp_utils.hpp>
22
#include <uhd/usrp/mboard_props.hpp>
23
#include <uhd/utils/assert.hpp>
24
#include <uhd/utils/algorithm.hpp>
25
#include <uhd/types/mac_addr.hpp>
26
#include <uhd/types/dict.hpp>
27
#include <boost/bind.hpp>
28
#include <boost/assign/list_of.hpp>
29
#include <boost/asio/ip/address_v4.hpp>
30
#include <iostream>
31

    
32
using namespace uhd;
33
using namespace uhd::usrp;
34

    
35
/***********************************************************************
36
 * Structors
37
 **********************************************************************/
38
usrp2_mboard_impl::usrp2_mboard_impl(
39
    size_t index,
40
    transport::udp_simple::sptr ctrl_transport,
41
    const usrp2_io_helper &io_helper
42
):
43
    _index(index),
44
    _io_helper(io_helper)
45
{
46
    //make a new interface for usrp2 stuff
47
    _iface = usrp2_iface::make(ctrl_transport);
48

    
49
    //extract the mboard rev numbers
50
    _rev_lo = _iface->read_eeprom(USRP2_I2C_ADDR_MBOARD, USRP2_EE_MBOARD_REV_LSB, 1).at(0);
51
    _rev_hi = _iface->read_eeprom(USRP2_I2C_ADDR_MBOARD, USRP2_EE_MBOARD_REV_MSB, 1).at(0);
52

    
53
    //contruct the interfaces to mboard perifs
54
    _clock_ctrl = usrp2_clock_ctrl::make(_iface);
55
    _codec_ctrl = usrp2_codec_ctrl::make(_iface);
56
    _serdes_ctrl = usrp2_serdes_ctrl::make(_iface);
57

    
58
    //TODO move to dsp impl...
59
    //load the allowed decim/interp rates
60
    //_USRP2_RATES = range(4, 128+1, 1) + range(130, 256+1, 2) + range(260, 512+1, 4)
61
    _allowed_decim_and_interp_rates.clear();
62
    for (size_t i = 4; i <= 128; i+=1){
63
        _allowed_decim_and_interp_rates.push_back(i);
64
    }
65
    for (size_t i = 130; i <= 256; i+=2){
66
        _allowed_decim_and_interp_rates.push_back(i);
67
    }
68
    for (size_t i = 260; i <= 512; i+=4){
69
        _allowed_decim_and_interp_rates.push_back(i);
70
    }
71

    
72
    //Issue a stop streaming command (in case it was left running).
73
    //Since this command is issued before the networking is setup,
74
    //most if not all junk packets will never make it to the socket.
75
    this->issue_ddc_stream_cmd(stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS);
76

    
77
    //init the rx control registers
78
    _iface->poke32(U2_REG_RX_CTRL_NSAMPS_PER_PKT, _io_helper.get_max_recv_samps_per_packet());
79
    _iface->poke32(U2_REG_RX_CTRL_NCHANNELS, 1);
80
    _iface->poke32(U2_REG_RX_CTRL_CLEAR_OVERRUN, 1); //reset
81
    _iface->poke32(U2_REG_RX_CTRL_VRT_HEADER, 0
82
        | (0x1 << 28) //if data with stream id
83
        | (0x1 << 26) //has trailer
84
        | (0x3 << 22) //integer time other
85
        | (0x1 << 20) //fractional time sample count
86
    );
87
    _iface->poke32(U2_REG_RX_CTRL_VRT_STREAM_ID, 0);
88
    _iface->poke32(U2_REG_RX_CTRL_VRT_TRAILER, 0);
89
    _iface->poke32(U2_REG_TIME64_TPS, size_t(get_master_clock_freq()));
90

    
91
    //init the tx control registers
92
    _iface->poke32(U2_REG_TX_CTRL_NUM_CHAN, 0);    //1 channel
93
    _iface->poke32(U2_REG_TX_CTRL_CLEAR_STATE, 1); //reset
94
    _iface->poke32(U2_REG_TX_CTRL_REPORT_SID, 1);  //sid 1 (different from rx)
95
    _iface->poke32(U2_REG_TX_CTRL_POLICY, U2_FLAG_TX_CTRL_POLICY_NEXT_PACKET);
96

    
97
    //init the ddc
98
    init_ddc_config();
99

    
100
    //init the duc
101
    init_duc_config();
102

    
103
    //initialize the clock configuration
104
    init_clock_config();
105

    
106
    //init the codec before the dboard
107
    codec_init();
108

    
109
    //init the tx and rx dboards (do last)
110
    dboard_init();
111

    
112
    //set default subdev specs
113
    (*this)[MBOARD_PROP_RX_SUBDEV_SPEC] = subdev_spec_t();
114
    (*this)[MBOARD_PROP_TX_SUBDEV_SPEC] = subdev_spec_t();
115
}
116

    
117
usrp2_mboard_impl::~usrp2_mboard_impl(void){
118
    /* NOP */
119
}
120

    
121
/***********************************************************************
122
 * Helper Methods
123
 **********************************************************************/
124
void usrp2_mboard_impl::init_clock_config(void){
125
    //setup the clock configuration settings
126
    _clock_config.ref_source = clock_config_t::REF_INT;
127
    _clock_config.pps_source = clock_config_t::PPS_SMA;
128
    _clock_config.pps_polarity = clock_config_t::PPS_NEG;
129

    
130
    //update the clock config (sends a control packet)
131
    update_clock_config();
132
}
133

    
134
void usrp2_mboard_impl::update_clock_config(void){
135
    boost::uint32_t pps_flags = 0;
136

    
137
    //translate pps source enums
138
    switch(_clock_config.pps_source){
139
    case clock_config_t::PPS_SMA:  pps_flags |= U2_FLAG_TIME64_PPS_SMA;  break;
140
    case clock_config_t::PPS_MIMO: pps_flags |= U2_FLAG_TIME64_PPS_MIMO; break;
141
    default: throw std::runtime_error("usrp2: unhandled clock configuration pps source");
142
    }
143

    
144
    //translate pps polarity enums
145
    switch(_clock_config.pps_polarity){
146
    case clock_config_t::PPS_POS: pps_flags |= U2_FLAG_TIME64_PPS_POSEDGE; break;
147
    case clock_config_t::PPS_NEG: pps_flags |= U2_FLAG_TIME64_PPS_NEGEDGE; break;
148
    default: throw std::runtime_error("usrp2: unhandled clock configuration pps polarity");
149
    }
150

    
151
    //set the pps flags
152
    _iface->poke32(U2_REG_TIME64_FLAGS, pps_flags);
153

    
154
    //clock source ref 10mhz
155
    switch(_clock_config.ref_source){
156
    case clock_config_t::REF_INT : _iface->poke32(U2_REG_MISC_CTRL_CLOCK, 0x10); break;
157
    case clock_config_t::REF_SMA : _iface->poke32(U2_REG_MISC_CTRL_CLOCK, 0x1C); break;
158
    case clock_config_t::REF_MIMO: _iface->poke32(U2_REG_MISC_CTRL_CLOCK, 0x15); break;
159
    default: throw std::runtime_error("usrp2: unhandled clock configuration reference source");
160
    }
161

    
162
    //clock source ref 10mhz
163
    bool use_external = _clock_config.ref_source != clock_config_t::REF_INT;
164
    _clock_ctrl->enable_external_ref(use_external);
165
}
166

    
167
void usrp2_mboard_impl::set_time_spec(const time_spec_t &time_spec, bool now){
168
    //set the ticks
169
    _iface->poke32(U2_REG_TIME64_TICKS, time_spec.get_tick_count(get_master_clock_freq()));
170

    
171
    //set the flags register
172
    boost::uint32_t imm_flags = (now)? U2_FLAG_TIME64_LATCH_NOW : U2_FLAG_TIME64_LATCH_NEXT_PPS;
173
    _iface->poke32(U2_REG_TIME64_IMM, imm_flags);
174

    
175
    //set the seconds (latches in all 3 registers)
176
    _iface->poke32(U2_REG_TIME64_SECS, boost::uint32_t(time_spec.get_full_secs()));
177
}
178

    
179
void usrp2_mboard_impl::issue_ddc_stream_cmd(const stream_cmd_t &stream_cmd){
180
    _iface->poke32(U2_REG_RX_CTRL_STREAM_CMD, dsp_type1::calc_stream_cmd_word(
181
        stream_cmd, _io_helper.get_max_recv_samps_per_packet()
182
    ));
183
    _iface->poke32(U2_REG_RX_CTRL_TIME_SECS,  boost::uint32_t(stream_cmd.time_spec.get_full_secs()));
184
    _iface->poke32(U2_REG_RX_CTRL_TIME_TICKS, stream_cmd.time_spec.get_tick_count(get_master_clock_freq()));
185
}
186

    
187
/***********************************************************************
188
 * MBoard Get Properties
189
 **********************************************************************/
190
static const std::string dboard_name = "0";
191

    
192
void usrp2_mboard_impl::get(const wax::obj &key_, wax::obj &val){
193
    named_prop_t key = named_prop_t::extract(key_);
194

    
195
    //handle the other props
196
    if (key_.type() == typeid(std::string)){
197
        if (key.as<std::string>() == "mac-addr"){
198
            byte_vector_t bytes = _iface->read_eeprom(USRP2_I2C_ADDR_MBOARD, USRP2_EE_MBOARD_MAC_ADDR, 6);
199
            val = mac_addr_t::from_bytes(bytes).to_string();
200
            return;
201
        }
202

    
203
        if (key.as<std::string>() == "ip-addr"){
204
            boost::asio::ip::address_v4::bytes_type bytes;
205
            std::copy(_iface->read_eeprom(USRP2_I2C_ADDR_MBOARD, USRP2_EE_MBOARD_IP_ADDR, 4), bytes);
206
            val = boost::asio::ip::address_v4(bytes).to_string();
207
            return;
208
        }
209
    }
210

    
211
    //handle the get request conditioned on the key
212
    switch(key.as<mboard_prop_t>()){
213
    case MBOARD_PROP_NAME:
214
        val = str(boost::format("usrp2 mboard%d - rev %d:%d") % _index % _rev_hi % _rev_lo);
215
        return;
216

    
217
    case MBOARD_PROP_OTHERS:{
218
            prop_names_t others = boost::assign::list_of
219
                ("mac-addr")
220
                ("ip-addr")
221
            ;
222
            val = others;
223
        }
224
        return;
225

    
226
    case MBOARD_PROP_RX_DBOARD:
227
        UHD_ASSERT_THROW(key.name == dboard_name);
228
        val = _rx_dboard_proxy->get_link();
229
        return;
230

    
231
    case MBOARD_PROP_RX_DBOARD_NAMES:
232
        val = prop_names_t(1, dboard_name);
233
        return;
234

    
235
    case MBOARD_PROP_TX_DBOARD:
236
        UHD_ASSERT_THROW(key.name == dboard_name);
237
        val = _tx_dboard_proxy->get_link();
238
        return;
239

    
240
    case MBOARD_PROP_TX_DBOARD_NAMES:
241
        val = prop_names_t(1, dboard_name);
242
        return;
243

    
244
    case MBOARD_PROP_RX_DSP:
245
        UHD_ASSERT_THROW(key.name == "");
246
        val = _rx_dsp_proxy->get_link();
247
        return;
248

    
249
    case MBOARD_PROP_RX_DSP_NAMES:
250
        val = prop_names_t(1, "");
251
        return;
252

    
253
    case MBOARD_PROP_TX_DSP:
254
        UHD_ASSERT_THROW(key.name == "");
255
        val = _tx_dsp_proxy->get_link();
256
        return;
257

    
258
    case MBOARD_PROP_TX_DSP_NAMES:
259
        val = prop_names_t(1, "");
260
        return;
261

    
262
    case MBOARD_PROP_CLOCK_CONFIG:
263
        val = _clock_config;
264
        return;
265

    
266
    case MBOARD_PROP_TIME_NOW:{
267
            usrp2_iface::pair64 time64(
268
                _iface->peek64(U2_REG_TIME64_SECS_RB, U2_REG_TIME64_TICKS_RB)
269
            );
270
            val = time_spec_t(
271
                time64.first, time64.second, get_master_clock_freq()
272
            );
273
        }
274
        return;
275

    
276
    case MBOARD_PROP_RX_SUBDEV_SPEC:
277
        val = _rx_subdev_spec;
278
        return;
279

    
280
    case MBOARD_PROP_TX_SUBDEV_SPEC:
281
        val = _tx_subdev_spec;
282
        return;
283

    
284
    default: UHD_THROW_PROP_GET_ERROR();
285
    }
286
}
287

    
288
/***********************************************************************
289
 * MBoard Set Properties
290
 **********************************************************************/
291
void usrp2_mboard_impl::set(const wax::obj &key, const wax::obj &val){
292
    //handle the other props
293
    if (key.type() == typeid(std::string)){
294
        if (key.as<std::string>() == "mac-addr"){
295
            byte_vector_t bytes = mac_addr_t::from_string(val.as<std::string>()).to_bytes();
296
            _iface->write_eeprom(USRP2_I2C_ADDR_MBOARD, USRP2_EE_MBOARD_MAC_ADDR, bytes);
297
            return;
298
        }
299

    
300
        if (key.as<std::string>() == "ip-addr"){
301
            byte_vector_t bytes(4);
302
            std::copy(boost::asio::ip::address_v4::from_string(val.as<std::string>()).to_bytes(), bytes);
303
            _iface->write_eeprom(USRP2_I2C_ADDR_MBOARD, USRP2_EE_MBOARD_IP_ADDR, bytes);
304
            return;
305
        }
306
    }
307

    
308
    //handle the get request conditioned on the key
309
    switch(key.as<mboard_prop_t>()){
310

    
311
    case MBOARD_PROP_CLOCK_CONFIG:
312
        _clock_config = val.as<clock_config_t>();
313
        update_clock_config();
314
        return;
315

    
316
    case MBOARD_PROP_TIME_NOW:
317
        set_time_spec(val.as<time_spec_t>(), true);
318
        return;
319

    
320
    case MBOARD_PROP_TIME_NEXT_PPS:
321
        set_time_spec(val.as<time_spec_t>(), false);
322
        return;
323

    
324
    case MBOARD_PROP_STREAM_CMD:
325
        issue_ddc_stream_cmd(val.as<stream_cmd_t>());
326
        return;
327

    
328
    case MBOARD_PROP_RX_SUBDEV_SPEC:
329
        _rx_subdev_spec = val.as<subdev_spec_t>();
330
        verify_rx_subdev_spec(_rx_subdev_spec, this->get_link());
331
        //sanity check
332
        UHD_ASSERT_THROW(_rx_subdev_spec.size() == 1);
333
        //set the mux
334
        _iface->poke32(U2_REG_DSP_RX_MUX, dsp_type1::calc_rx_mux_word(
335
            _dboard_manager->get_rx_subdev(_rx_subdev_spec.front().sd_name)[SUBDEV_PROP_CONNECTION].as<subdev_conn_t>()
336
        ));
337
        return;
338

    
339
    case MBOARD_PROP_TX_SUBDEV_SPEC:
340
        _tx_subdev_spec = val.as<subdev_spec_t>();
341
        verify_tx_subdev_spec(_tx_subdev_spec, this->get_link());
342
        //sanity check
343
        UHD_ASSERT_THROW(_tx_subdev_spec.size() == 1);
344
        //set the mux
345
        _iface->poke32(U2_REG_DSP_TX_MUX, dsp_type1::calc_tx_mux_word(
346
            _dboard_manager->get_tx_subdev(_tx_subdev_spec.front().sd_name)[SUBDEV_PROP_CONNECTION].as<subdev_conn_t>()
347
        ));
348
        return;
349

    
350
    default: UHD_THROW_PROP_SET_ERROR();
351
    }
352
}