Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / mboard_impl.cpp @ 45d8240f

History | View | Annotate | Download (11.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 "usrp_commands.h"
20
#include "fpga_regs_standard.h"
21
#include <uhd/usrp/misc_utils.hpp>
22
#include <uhd/usrp/mboard_props.hpp>
23
#include <uhd/usrp/dboard_props.hpp>
24
#include <uhd/usrp/subdev_props.hpp>
25
#include <uhd/utils/warning.hpp>
26
#include <uhd/utils/assert.hpp>
27
#include <boost/assign/list_of.hpp>
28
#include <boost/foreach.hpp>
29
#include <boost/bind.hpp>
30
#include <iostream>
31

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

    
35
/***********************************************************************
36
 * Calculate the RX mux value:
37
 *    The I and Q mux values are intentionally reversed to flip I and Q
38
 *    to account for the reversal in the type conversion routines.
39
 **********************************************************************/
40
static int calc_rx_mux_pair(int adc_for_i, int adc_for_q){
41
    return (adc_for_i << 2) | (adc_for_q << 0); //shift reversal here
42
}
43

    
44
/*!
45
 *    3                   2                   1                   0
46
 *  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
47
 * +-----------------------+-------+-------+-------+-------+-+-----+
48
 * |      must be zero     | Q3| I3| Q2| I2| Q1| I1| Q0| I0|Z| NCH |
49
 * +-----------------------+-------+-------+-------+-------+-+-----+
50
 */
51
static boost::uint32_t calc_rx_mux(
52
    const subdev_spec_t &subdev_spec, wax::obj mboard
53
){
54
    //create look-up-table for mapping dboard name and connection type to ADC flags
55
    static const int ADC0 = 0, ADC1 = 1, ADC2 = 2, ADC3 = 3;
56
    static const uhd::dict<std::string, uhd::dict<subdev_conn_t, int> > name_to_conn_to_flag = boost::assign::map_list_of
57
        ("A", boost::assign::map_list_of
58
            (SUBDEV_CONN_COMPLEX_IQ, calc_rx_mux_pair(ADC0, ADC1)) //I and Q
59
            (SUBDEV_CONN_COMPLEX_QI, calc_rx_mux_pair(ADC1, ADC0)) //I and Q
60
            (SUBDEV_CONN_REAL_I,     calc_rx_mux_pair(ADC0, ADC0)) //I and Q (Q identical but ignored Z=1)
61
            (SUBDEV_CONN_REAL_Q,     calc_rx_mux_pair(ADC1, ADC1)) //I and Q (Q identical but ignored Z=1)
62
        )
63
        ("B", boost::assign::map_list_of
64
            (SUBDEV_CONN_COMPLEX_IQ, calc_rx_mux_pair(ADC2, ADC3)) //I and Q
65
            (SUBDEV_CONN_COMPLEX_QI, calc_rx_mux_pair(ADC3, ADC2)) //I and Q
66
            (SUBDEV_CONN_REAL_I,     calc_rx_mux_pair(ADC2, ADC2)) //I and Q (Q identical but ignored Z=1)
67
            (SUBDEV_CONN_REAL_Q,     calc_rx_mux_pair(ADC3, ADC3)) //I and Q (Q identical but ignored Z=1)
68
        )
69
    ;
70

    
71
    //extract the number of channels
72
    size_t nchan = subdev_spec.size();
73

    
74
    //calculate the channel flags
75
    int channel_flags = 0;
76
    size_t num_reals = 0, num_quads = 0;
77
    BOOST_FOREACH(const subdev_spec_pair_t &pair, subdev_spec){
78
        wax::obj dboard = mboard[named_prop_t(MBOARD_PROP_RX_DBOARD, pair.db_name)];
79
        wax::obj subdev = dboard[named_prop_t(DBOARD_PROP_SUBDEV, pair.sd_name)];
80
        subdev_conn_t conn = subdev[SUBDEV_PROP_CONNECTION].as<subdev_conn_t>();
81
        switch(conn){
82
        case SUBDEV_CONN_COMPLEX_IQ:
83
        case SUBDEV_CONN_COMPLEX_QI: num_quads++; break;
84
        case SUBDEV_CONN_REAL_I:
85
        case SUBDEV_CONN_REAL_Q:     num_reals++; break;
86
        }
87
        channel_flags = (channel_flags << 4) | name_to_conn_to_flag[pair.db_name][conn];
88
    }
89

    
90
    //calculate Z:
91
    //    for all real sources: Z = 1
92
    //    for all quadrature sources: Z = 0
93
    //    for mixed sources: warning + Z = 0
94
    int Z = (num_quads > 0)? 0 : 1;
95
    if (num_quads != 0 and num_reals != 0) uhd::print_warning(
96
        "Mixing real and quadrature rx subdevices is not supported.\n"
97
        "The Q input to the real source(s) will be non-zero.\n"
98
    );
99

    
100
    //calculate the rx mux value
101
    return ((channel_flags & 0xffff) << 4) | ((Z & 0x1) << 3) | ((nchan & 0x7) << 0);
102
}
103

    
104
/***********************************************************************
105
 * Calculate the TX mux value:
106
 *    The I and Q mux values are intentionally reversed to flip I and Q
107
 *    to account for the reversal in the type conversion routines.
108
 **********************************************************************/
109
static int calc_tx_mux_pair(int chn_for_i, int chn_for_q){
110
    return (chn_for_i << 4) | (chn_for_q << 0); //shift reversal here
111
}
112

    
113
/*!
114
 *    3                   2                   1                   0
115
 *  1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
116
 * +-----------------------+-------+-------+-------+-------+-+-----+
117
 * |                       | DAC1Q | DAC1I | DAC0Q | DAC0I |0| NCH |
118
 * +-----------------------------------------------+-------+-+-----+
119
 */
120
static boost::uint32_t calc_tx_mux(
121
    const subdev_spec_t &subdev_spec, wax::obj mboard
122
){
123
    //create look-up-table for mapping channel number and connection type to flags
124
    static const int ENB = 1 << 3, CHAN_I0 = 0, CHAN_Q0 = 1, CHAN_I1 = 2, CHAN_Q1 = 3;
125
    static const uhd::dict<size_t, uhd::dict<subdev_conn_t, int> > chan_to_conn_to_flag = boost::assign::map_list_of
126
        (0, boost::assign::map_list_of
127
            (SUBDEV_CONN_COMPLEX_IQ, calc_tx_mux_pair(CHAN_I0 | ENB, CHAN_Q0 | ENB))
128
            (SUBDEV_CONN_COMPLEX_QI, calc_tx_mux_pair(CHAN_Q0 | ENB, CHAN_I0 | ENB))
129
            (SUBDEV_CONN_REAL_I,     calc_tx_mux_pair(CHAN_I0 | ENB, 0            ))
130
            (SUBDEV_CONN_REAL_Q,     calc_tx_mux_pair(0,             CHAN_I0 | ENB))
131
        )
132
        (1, boost::assign::map_list_of
133
            (SUBDEV_CONN_COMPLEX_IQ, calc_tx_mux_pair(CHAN_I1 | ENB, CHAN_Q1 | ENB))
134
            (SUBDEV_CONN_COMPLEX_QI, calc_tx_mux_pair(CHAN_Q1 | ENB, CHAN_I1 | ENB))
135
            (SUBDEV_CONN_REAL_I,     calc_tx_mux_pair(CHAN_I1 | ENB, 0            ))
136
            (SUBDEV_CONN_REAL_Q,     calc_tx_mux_pair(0,             CHAN_I1 | ENB))
137
        )
138
    ;
139

    
140
    //extract the number of channels
141
    size_t nchan = subdev_spec.size();
142

    
143
    //calculate the channel flags
144
    int channel_flags = 0, chan = 0;
145
    BOOST_FOREACH(const subdev_spec_pair_t &pair, subdev_spec){
146
        wax::obj dboard = mboard[named_prop_t(MBOARD_PROP_TX_DBOARD, pair.db_name)];
147
        wax::obj subdev = dboard[named_prop_t(DBOARD_PROP_SUBDEV, pair.sd_name)];
148
        subdev_conn_t conn = subdev[SUBDEV_PROP_CONNECTION].as<subdev_conn_t>();
149

    
150
        //combine the channel flags: shift for slot A vs B
151
        if (pair.db_name == "A") channel_flags |= chan_to_conn_to_flag[chan][conn] << 0;
152
        if (pair.db_name == "B") channel_flags |= chan_to_conn_to_flag[chan][conn] << 8;
153

    
154
        //increment for the next channel
155
        chan++;
156
    }
157

    
158
    //calculate the tx mux value
159
    return ((channel_flags & 0xffff) << 4) | ((nchan & 0x7) << 0);
160
}
161

    
162
/***********************************************************************
163
 * Mboard Initialization
164
 **********************************************************************/
165
void usrp1_impl::mboard_init(void)
166
{
167
    _mboard_proxy = wax_obj_proxy::make(
168
                     boost::bind(&usrp1_impl::mboard_get, this, _1, _2),
169
                     boost::bind(&usrp1_impl::mboard_set, this, _1, _2));
170

    
171
    // Normal mode with no loopback or Rx counting
172
    _iface->poke32(FR_MODE, 0x00000000);
173
    _iface->poke32(FR_DEBUG_EN, 0x00000000);
174
    _iface->poke32(FR_RX_SAMPLE_RATE_DIV, 0x00000001);
175
    _iface->poke32(FR_TX_SAMPLE_RATE_DIV, 0x00000003);
176
    _iface->poke32(FR_DC_OFFSET_CL_EN, 0x0000000f);
177

    
178
    // Reset offset correction registers
179
    _iface->poke32(FR_ADC_OFFSET_0, 0x00000000);
180
    _iface->poke32(FR_ADC_OFFSET_1, 0x00000000);
181
    _iface->poke32(FR_ADC_OFFSET_2, 0x00000000);
182
    _iface->poke32(FR_ADC_OFFSET_3, 0x00000000);
183

    
184
    // Set default for RX format to 16-bit I&Q and no half-band filter bypass
185
    _iface->poke32(FR_RX_FORMAT, 0x00000300);
186

    
187
    // Set default for TX format to 16-bit I&Q
188
    _iface->poke32(FR_TX_FORMAT, 0x00000000);
189
}
190

    
191
void usrp1_impl::issue_stream_cmd(const stream_cmd_t &stream_cmd)
192
{
193
    if (stream_cmd.stream_mode == stream_cmd_t::STREAM_MODE_START_CONTINUOUS) {
194
        _iface->write_firmware_cmd(VRQ_FPGA_SET_RX_ENABLE, true, 0, 0, 0);
195
    }
196

    
197
    if (stream_cmd.stream_mode == stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS) {
198
        _iface->write_firmware_cmd(VRQ_FPGA_SET_RX_ENABLE, false, 0, 0, 0);
199
    }
200
}
201

    
202
/***********************************************************************
203
 * Mboard Get
204
 **********************************************************************/
205
static prop_names_t dboard_names = boost::assign::list_of("A")("B");
206

    
207
void usrp1_impl::mboard_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<mboard_prop_t>()){
213
    case MBOARD_PROP_NAME:
214
        val = std::string("usrp1 mboard");
215
        return;
216

    
217
    case MBOARD_PROP_OTHERS:
218
        val = prop_names_t();
219
        return;
220

    
221
    case MBOARD_PROP_RX_DBOARD:
222
        uhd::assert_has(dboard_names, key.name, "dboard name");
223
        if (key.name == "A") val = _rx_dboard_proxies[DBOARD_SLOT_A]->get_link();
224
        if (key.name == "B") val = _rx_dboard_proxies[DBOARD_SLOT_B]->get_link();
225
        return;
226

    
227
    case MBOARD_PROP_RX_DBOARD_NAMES:
228
        val = dboard_names;
229
        return;
230

    
231
    case MBOARD_PROP_TX_DBOARD:
232
        uhd::assert_has(dboard_names, key.name, "dboard name");
233
        if (key.name == "A") val = _tx_dboard_proxies[DBOARD_SLOT_A]->get_link();
234
        if (key.name == "B") val = _tx_dboard_proxies[DBOARD_SLOT_B]->get_link();
235
        return;
236

    
237
    case MBOARD_PROP_TX_DBOARD_NAMES:
238
        val = dboard_names;
239
        return;
240

    
241
    case MBOARD_PROP_RX_DSP:
242
        UHD_ASSERT_THROW(key.name == "");
243
        val = _rx_dsp_proxy->get_link();
244
        return;
245

    
246
    case MBOARD_PROP_RX_DSP_NAMES:
247
        val = prop_names_t(1, "");
248
        return;
249

    
250
    case MBOARD_PROP_TX_DSP:
251
        UHD_ASSERT_THROW(key.name == "");
252
        val = _tx_dsp_proxy->get_link();
253
        return;
254

    
255
    case MBOARD_PROP_TX_DSP_NAMES:
256
        val = prop_names_t(1, "");
257
        return;
258

    
259
    case MBOARD_PROP_CLOCK_CONFIG:
260
        val = _clock_config;
261
        return;
262

    
263
    case MBOARD_PROP_RX_SUBDEV_SPEC:
264
        val = _rx_subdev_spec;
265
        return;
266

    
267
    case MBOARD_PROP_TX_SUBDEV_SPEC:
268
        val = _tx_subdev_spec;
269
        return;
270

    
271
    default: UHD_THROW_PROP_GET_ERROR();
272
    }
273
}
274

    
275
/***********************************************************************
276
 * Mboard Set
277
 **********************************************************************/
278
void usrp1_impl::mboard_set(const wax::obj &key, const wax::obj &val)
279
{
280
    //handle the get request conditioned on the key
281
    switch(key.as<mboard_prop_t>()){
282

    
283
    case MBOARD_PROP_STREAM_CMD:
284
        issue_stream_cmd(val.as<stream_cmd_t>());
285
        return;
286

    
287
    case MBOARD_PROP_RX_SUBDEV_SPEC:
288
        _rx_subdev_spec = val.as<subdev_spec_t>();
289
        verify_rx_subdev_spec(_rx_subdev_spec, _mboard_proxy->get_link());
290
        //sanity check
291
        UHD_ASSERT_THROW(_rx_subdev_spec.size() <= 2);
292
        //set the mux and set the number of rx channels
293
        _iface->poke32(FR_RX_MUX, calc_rx_mux(_rx_subdev_spec, _mboard_proxy->get_link()));
294
        return;
295

    
296
    case MBOARD_PROP_TX_SUBDEV_SPEC:
297
        _tx_subdev_spec = val.as<subdev_spec_t>();
298
        verify_tx_subdev_spec(_tx_subdev_spec, _mboard_proxy->get_link());
299
        //sanity check
300
        UHD_ASSERT_THROW(_tx_subdev_spec.size() <= 2);
301
        //set the mux and set the number of tx channels
302
        _iface->poke32(FR_TX_MUX, calc_tx_mux(_tx_subdev_spec, _mboard_proxy->get_link()));
303
        return;
304

    
305
    default: UHD_THROW_PROP_SET_ERROR();
306
    }
307
}