Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp2 / mboard_impl.cpp @ 1a86e65a

History | View | Annotate | Download (11.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 <boost/bind.hpp>
26
#include <iostream>
27

    
28
using namespace uhd;
29
using namespace uhd::usrp;
30

    
31
/***********************************************************************
32
 * Structors
33
 **********************************************************************/
34
usrp2_mboard_impl::usrp2_mboard_impl(
35
    size_t index,
36
    transport::udp_simple::sptr ctrl_transport,
37
    size_t recv_frame_size
38
):
39
    _index(index),
40
    _recv_frame_size(recv_frame_size),
41
    _iface(usrp2_iface::make(ctrl_transport))
42
{
43
    //contruct the interfaces to mboard perifs
44
    _clock_ctrl = usrp2_clock_ctrl::make(_iface);
45
    _codec_ctrl = usrp2_codec_ctrl::make(_iface);
46
    _serdes_ctrl = usrp2_serdes_ctrl::make(_iface);
47

    
48
    //TODO move to dsp impl...
49
    //load the allowed decim/interp rates
50
    //_USRP2_RATES = range(4, 128+1, 1) + range(130, 256+1, 2) + range(260, 512+1, 4)
51
    _allowed_decim_and_interp_rates.clear();
52
    for (size_t i = 4; i <= 128; i+=1){
53
        _allowed_decim_and_interp_rates.push_back(i);
54
    }
55
    for (size_t i = 130; i <= 256; i+=2){
56
        _allowed_decim_and_interp_rates.push_back(i);
57
    }
58
    for (size_t i = 260; i <= 512; i+=4){
59
        _allowed_decim_and_interp_rates.push_back(i);
60
    }
61

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

    
67
    //init the rx control registers
68
    _iface->poke32(U2_REG_RX_CTRL_NSAMPS_PER_PKT, _recv_frame_size);
69
    _iface->poke32(U2_REG_RX_CTRL_NCHANNELS, 1);
70
    _iface->poke32(U2_REG_RX_CTRL_CLEAR_OVERRUN, 1); //reset
71
    _iface->poke32(U2_REG_RX_CTRL_VRT_HEADER, 0
72
        | (0x1 << 28) //if data with stream id
73
        | (0x1 << 26) //has trailer
74
        | (0x3 << 22) //integer time other
75
        | (0x1 << 20) //fractional time sample count
76
    );
77
    _iface->poke32(U2_REG_RX_CTRL_VRT_STREAM_ID, 0);
78
    _iface->poke32(U2_REG_RX_CTRL_VRT_TRAILER, 0);
79
    _iface->poke32(U2_REG_TIME64_TPS, size_t(get_master_clock_freq()));
80

    
81
    //init the tx control registers
82
    _iface->poke32(U2_REG_TX_CTRL_NUM_CHAN, 0);    //1 channel
83
    _iface->poke32(U2_REG_TX_CTRL_CLEAR_STATE, 1); //reset
84
    _iface->poke32(U2_REG_TX_CTRL_REPORT_SID, 1);  //sid 1 (different from rx)
85
    _iface->poke32(U2_REG_TX_CTRL_POLICY, U2_FLAG_TX_CTRL_POLICY_NEXT_PACKET);
86

    
87
    //init the ddc
88
    init_ddc_config();
89

    
90
    //init the duc
91
    init_duc_config();
92

    
93
    //initialize the clock configuration
94
    init_clock_config();
95

    
96
    //init the codec before the dboard
97
    codec_init();
98

    
99
    //init the tx and rx dboards (do last)
100
    dboard_init();
101

    
102
    //set default subdev specs
103
    (*this)[MBOARD_PROP_RX_SUBDEV_SPEC] = subdev_spec_t();
104
    (*this)[MBOARD_PROP_TX_SUBDEV_SPEC] = subdev_spec_t();
105
}
106

    
107
usrp2_mboard_impl::~usrp2_mboard_impl(void){
108
    /* NOP */
109
}
110

    
111
/***********************************************************************
112
 * Helper Methods
113
 **********************************************************************/
114
void usrp2_mboard_impl::init_clock_config(void){
115
    //setup the clock configuration settings
116
    _clock_config.ref_source = clock_config_t::REF_INT;
117
    _clock_config.pps_source = clock_config_t::PPS_SMA;
118
    _clock_config.pps_polarity = clock_config_t::PPS_NEG;
119

    
120
    //update the clock config (sends a control packet)
121
    update_clock_config();
122
}
123

    
124
void usrp2_mboard_impl::update_clock_config(void){
125
    boost::uint32_t pps_flags = 0;
126

    
127
    //translate pps source enums
128
    switch(_clock_config.pps_source){
129
    case clock_config_t::PPS_SMA:  pps_flags |= U2_FLAG_TIME64_PPS_SMA;  break;
130
    case clock_config_t::PPS_MIMO: pps_flags |= U2_FLAG_TIME64_PPS_MIMO; break;
131
    default: throw std::runtime_error("usrp2: unhandled clock configuration pps source");
132
    }
133

    
134
    //translate pps polarity enums
135
    switch(_clock_config.pps_polarity){
136
    case clock_config_t::PPS_POS: pps_flags |= U2_FLAG_TIME64_PPS_POSEDGE; break;
137
    case clock_config_t::PPS_NEG: pps_flags |= U2_FLAG_TIME64_PPS_NEGEDGE; break;
138
    default: throw std::runtime_error("usrp2: unhandled clock configuration pps polarity");
139
    }
140

    
141
    //set the pps flags
142
    _iface->poke32(U2_REG_TIME64_FLAGS, pps_flags);
143

    
144
    //clock source ref 10mhz
145
    switch(_clock_config.ref_source){
146
    case clock_config_t::REF_INT : _iface->poke32(U2_REG_MISC_CTRL_CLOCK, 0x10); break;
147
    case clock_config_t::REF_SMA : _iface->poke32(U2_REG_MISC_CTRL_CLOCK, 0x1C); break;
148
    case clock_config_t::REF_MIMO: _iface->poke32(U2_REG_MISC_CTRL_CLOCK, 0x15); break;
149
    default: throw std::runtime_error("usrp2: unhandled clock configuration reference source");
150
    }
151

    
152
    //clock source ref 10mhz
153
    bool use_external = _clock_config.ref_source != clock_config_t::REF_INT;
154
    _clock_ctrl->enable_external_ref(use_external);
155
}
156

    
157
void usrp2_mboard_impl::set_time_spec(const time_spec_t &time_spec, bool now){
158
    //set the ticks
159
    _iface->poke32(U2_REG_TIME64_TICKS, time_spec.get_tick_count(get_master_clock_freq()));
160

    
161
    //set the flags register
162
    boost::uint32_t imm_flags = (now)? U2_FLAG_TIME64_LATCH_NOW : U2_FLAG_TIME64_LATCH_NEXT_PPS;
163
    _iface->poke32(U2_REG_TIME64_IMM, imm_flags);
164

    
165
    //set the seconds (latches in all 3 registers)
166
    _iface->poke32(U2_REG_TIME64_SECS, boost::uint32_t(time_spec.get_full_secs()));
167
}
168

    
169
void usrp2_mboard_impl::handle_overflow(void){
170
    _iface->poke32(U2_REG_RX_CTRL_CLEAR_OVERRUN, 1);
171
    if (_continuous_streaming){ //re-issue the stream command if already continuous
172
        this->issue_ddc_stream_cmd(stream_cmd_t::STREAM_MODE_START_CONTINUOUS);
173
    }
174
}
175

    
176
void usrp2_mboard_impl::issue_ddc_stream_cmd(const stream_cmd_t &stream_cmd){
177
    _continuous_streaming = stream_cmd.stream_mode == stream_cmd_t::STREAM_MODE_START_CONTINUOUS;
178
    _iface->poke32(U2_REG_RX_CTRL_STREAM_CMD, dsp_type1::calc_stream_cmd_word(
179
        stream_cmd, _recv_frame_size
180
    ));
181
    _iface->poke32(U2_REG_RX_CTRL_TIME_SECS,  boost::uint32_t(stream_cmd.time_spec.get_full_secs()));
182
    _iface->poke32(U2_REG_RX_CTRL_TIME_TICKS, stream_cmd.time_spec.get_tick_count(get_master_clock_freq()));
183
}
184

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

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

    
193
    //handle the get request conditioned on the key
194
    switch(key.as<mboard_prop_t>()){
195
    case MBOARD_PROP_NAME:
196
        val = str(boost::format("usrp2 mboard%d - rev %s") % _index % _iface->mb_eeprom["rev"]);
197
        return;
198

    
199
    case MBOARD_PROP_OTHERS:
200
        val = prop_names_t();
201
        return;
202

    
203
    case MBOARD_PROP_RX_DBOARD:
204
        UHD_ASSERT_THROW(key.name == dboard_name);
205
        val = _rx_dboard_proxy->get_link();
206
        return;
207

    
208
    case MBOARD_PROP_RX_DBOARD_NAMES:
209
        val = prop_names_t(1, dboard_name);
210
        return;
211

    
212
    case MBOARD_PROP_TX_DBOARD:
213
        UHD_ASSERT_THROW(key.name == dboard_name);
214
        val = _tx_dboard_proxy->get_link();
215
        return;
216

    
217
    case MBOARD_PROP_TX_DBOARD_NAMES:
218
        val = prop_names_t(1, dboard_name);
219
        return;
220

    
221
    case MBOARD_PROP_RX_DSP:
222
        UHD_ASSERT_THROW(key.name == "");
223
        val = _rx_dsp_proxy->get_link();
224
        return;
225

    
226
    case MBOARD_PROP_RX_DSP_NAMES:
227
        val = prop_names_t(1, "");
228
        return;
229

    
230
    case MBOARD_PROP_TX_DSP:
231
        UHD_ASSERT_THROW(key.name == "");
232
        val = _tx_dsp_proxy->get_link();
233
        return;
234

    
235
    case MBOARD_PROP_TX_DSP_NAMES:
236
        val = prop_names_t(1, "");
237
        return;
238

    
239
    case MBOARD_PROP_CLOCK_CONFIG:
240
        val = _clock_config;
241
        return;
242

    
243
    case MBOARD_PROP_TIME_NOW:{
244
            usrp2_iface::pair64 time64(
245
                _iface->peek64(U2_REG_TIME64_SECS_RB, U2_REG_TIME64_TICKS_RB)
246
            );
247
            val = time_spec_t(
248
                time64.first, time64.second, get_master_clock_freq()
249
            );
250
        }
251
        return;
252

    
253
    case MBOARD_PROP_RX_SUBDEV_SPEC:
254
        val = _rx_subdev_spec;
255
        return;
256

    
257
    case MBOARD_PROP_TX_SUBDEV_SPEC:
258
        val = _tx_subdev_spec;
259
        return;
260

    
261
    case MBOARD_PROP_EEPROM_MAP:
262
        val = _iface->mb_eeprom;
263
        return;
264

    
265
    default: UHD_THROW_PROP_GET_ERROR();
266
    }
267
}
268

    
269
/***********************************************************************
270
 * MBoard Set Properties
271
 **********************************************************************/
272
void usrp2_mboard_impl::set(const wax::obj &key, const wax::obj &val){
273

    
274
    //handle the get request conditioned on the key
275
    switch(key.as<mboard_prop_t>()){
276

    
277
    case MBOARD_PROP_CLOCK_CONFIG:
278
        _clock_config = val.as<clock_config_t>();
279
        update_clock_config();
280
        return;
281

    
282
    case MBOARD_PROP_TIME_NOW:
283
        set_time_spec(val.as<time_spec_t>(), true);
284
        return;
285

    
286
    case MBOARD_PROP_TIME_NEXT_PPS:
287
        set_time_spec(val.as<time_spec_t>(), false);
288
        return;
289

    
290
    case MBOARD_PROP_STREAM_CMD:
291
        issue_ddc_stream_cmd(val.as<stream_cmd_t>());
292
        return;
293

    
294
    case MBOARD_PROP_RX_SUBDEV_SPEC:
295
        _rx_subdev_spec = val.as<subdev_spec_t>();
296
        verify_rx_subdev_spec(_rx_subdev_spec, this->get_link());
297
        //sanity check
298
        UHD_ASSERT_THROW(_rx_subdev_spec.size() == 1);
299
        //set the mux
300
        _iface->poke32(U2_REG_DSP_RX_MUX, dsp_type1::calc_rx_mux_word(
301
            _dboard_manager->get_rx_subdev(_rx_subdev_spec.front().sd_name)[SUBDEV_PROP_CONNECTION].as<subdev_conn_t>()
302
        ));
303
        return;
304

    
305
    case MBOARD_PROP_TX_SUBDEV_SPEC:
306
        _tx_subdev_spec = val.as<subdev_spec_t>();
307
        verify_tx_subdev_spec(_tx_subdev_spec, this->get_link());
308
        //sanity check
309
        UHD_ASSERT_THROW(_tx_subdev_spec.size() == 1);
310
        //set the mux
311
        _iface->poke32(U2_REG_DSP_TX_MUX, dsp_type1::calc_tx_mux_word(
312
            _dboard_manager->get_tx_subdev(_tx_subdev_spec.front().sd_name)[SUBDEV_PROP_CONNECTION].as<subdev_conn_t>()
313
        ));
314
        return;
315

    
316
    case MBOARD_PROP_EEPROM_MAP:
317
        // Step1: commit the map, writing only those values set.
318
        // Step2: readback the entire eeprom map into the iface.
319
        val.as<mboard_eeprom_t>().commit(*_iface, mboard_eeprom_t::MAP_NXXX);
320
        _iface->mb_eeprom = mboard_eeprom_t(*_iface, mboard_eeprom_t::MAP_NXXX);
321
        return;
322

    
323
    default: UHD_THROW_PROP_SET_ERROR();
324
    }
325
}