Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / mboard_impl.cpp @ 98510590

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 "fpga_regs_common.h"
22
#include <uhd/usrp/misc_utils.hpp>
23
#include <uhd/usrp/mboard_props.hpp>
24
#include <uhd/usrp/dboard_props.hpp>
25
#include <uhd/usrp/subdev_props.hpp>
26
#include <uhd/utils/warning.hpp>
27
#include <uhd/utils/assert.hpp>
28
#include <boost/assign/list_of.hpp>
29
#include <boost/foreach.hpp>
30
#include <boost/bind.hpp>
31
#include <iostream>
32

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
208
void usrp1_impl::mboard_get(const wax::obj &key_, wax::obj &val)
209
{
210
    named_prop_t key = named_prop_t::extract(key_);
211

    
212
    //handle the get request conditioned on the key
213
    switch(key.as<mboard_prop_t>()){
214
    case MBOARD_PROP_NAME:
215
        val = std::string("usrp1 mboard");
216
        return;
217

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
306
    default: UHD_THROW_PROP_SET_ERROR();
307
    }
308
}