Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / mboard_impl.cpp @ 7b066a45

History | View | Annotate | Download (13.8 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 "usrp_i2c_addr.h"
23
#include <uhd/usrp/misc_utils.hpp>
24
#include <uhd/usrp/mboard_props.hpp>
25
#include <uhd/usrp/dboard_props.hpp>
26
#include <uhd/usrp/subdev_props.hpp>
27
#include <uhd/utils/warning.hpp>
28
#include <uhd/utils/assert.hpp>
29
#include <uhd/utils/images.hpp>
30
#include <boost/assign/list_of.hpp>
31
#include <boost/foreach.hpp>
32
#include <boost/bind.hpp>
33
#include <iostream>
34

    
35
using namespace uhd;
36
using namespace uhd::usrp;
37

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

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

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

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

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

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

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

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

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

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

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

    
157
        //increment for the next channel
158
        chan++;
159
    }
160

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

    
165
/*!
166
 * Capabilities Register
167
 *
168
 *    3                   2                   1                   0
169
 *  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
170
 * +-----------------------------------------------+-+-----+-+-----+
171
 * |               Reserved                        |T|DUCs |R|DDCs |
172
 * +-----------------------------------------------+-+-----+-+-----+
173
 */
174
static int num_ddcs(boost::uint32_t regval)
175
{
176
    return (regval >> 0) & 0x0007;
177
}
178

    
179
static int num_ducs(boost::uint32_t regval)
180
{
181
    return (regval >> 4) & 0x0007;
182
}
183

    
184
static bool has_rx_halfband(boost::uint32_t regval)
185
{
186
    return (regval >> 3) & 0x0001;
187
}
188

    
189
static bool has_tx_halfband(boost::uint32_t regval)
190
{
191
    return (regval >> 7) & 0x0001;
192
}
193

    
194
/***********************************************************************
195
 * Mboard Initialization
196
 **********************************************************************/
197
void usrp1_impl::mboard_init(void)
198
{
199
    _mboard_proxy = wax_obj_proxy::make(
200
                     boost::bind(&usrp1_impl::mboard_get, this, _1, _2),
201
                     boost::bind(&usrp1_impl::mboard_set, this, _1, _2));
202

    
203
    // Normal mode with no loopback or Rx counting
204
    _iface->poke32(FR_MODE, 0x00000000);
205
    _iface->poke32(FR_DEBUG_EN, 0x00000000);
206
    _iface->poke32(FR_RX_SAMPLE_RATE_DIV, 0x00000001);
207
    _iface->poke32(FR_TX_SAMPLE_RATE_DIV, 0x00000003);
208
    _iface->poke32(FR_DC_OFFSET_CL_EN, 0x0000000f);
209

    
210
    // Reset offset correction registers
211
    _iface->poke32(FR_ADC_OFFSET_0, 0x00000000);
212
    _iface->poke32(FR_ADC_OFFSET_1, 0x00000000);
213
    _iface->poke32(FR_ADC_OFFSET_2, 0x00000000);
214
    _iface->poke32(FR_ADC_OFFSET_3, 0x00000000);
215

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

    
219
    // Set default for TX format to 16-bit I&Q
220
    _iface->poke32(FR_TX_FORMAT, 0x00000000);
221

    
222
    // TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO 
223
    // 
224
    // Do something useful with the capabilities register
225
    //
226
    boost::uint32_t regval = _iface->peek32(FR_RB_CAPS);
227
    std::cout << "USRP1 Capabilities" << std::endl;
228
    std::cout << "    number of duc's: " << num_ddcs(regval) << std::endl;
229
    std::cout << "    number of ddc's: " << num_ducs(regval) << std::endl;
230
    std::cout << "    rx halfband:     " << has_rx_halfband(regval) << std::endl;
231
    std::cout << "    tx halfband:     " << has_tx_halfband(regval) << std::endl;
232
}
233

    
234
void usrp1_impl::issue_stream_cmd(const stream_cmd_t &stream_cmd)
235
{
236
    if (stream_cmd.stream_mode == stream_cmd_t::STREAM_MODE_START_CONTINUOUS) {
237
        _iface->write_firmware_cmd(VRQ_FPGA_SET_RX_ENABLE, true, 0, 0, 0);
238
    }
239

    
240
    if (stream_cmd.stream_mode == stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS) {
241
        _iface->write_firmware_cmd(VRQ_FPGA_SET_RX_ENABLE, false, 0, 0, 0);
242
    }
243
}
244

    
245
/***********************************************************************
246
 * Mboard Get
247
 **********************************************************************/
248
static prop_names_t dboard_names = boost::assign::list_of("A")("B");
249

    
250
void usrp1_impl::mboard_get(const wax::obj &key_, wax::obj &val)
251
{
252
    named_prop_t key = named_prop_t::extract(key_);
253

    
254
    //handle the get request conditioned on the key
255
    switch(key.as<mboard_prop_t>()){
256
    case MBOARD_PROP_NAME:
257
        val = std::string("usrp1 mboard");
258
        return;
259

    
260
    case MBOARD_PROP_OTHERS:
261
        val = prop_names_t();
262
        return;
263

    
264
    case MBOARD_PROP_RX_DBOARD:
265
        uhd::assert_has(dboard_names, key.name, "dboard name");
266
        if (key.name == "A") val = _rx_dboard_proxies[DBOARD_SLOT_A]->get_link();
267
        if (key.name == "B") val = _rx_dboard_proxies[DBOARD_SLOT_B]->get_link();
268
        return;
269

    
270
    case MBOARD_PROP_RX_DBOARD_NAMES:
271
        val = dboard_names;
272
        return;
273

    
274
    case MBOARD_PROP_TX_DBOARD:
275
        uhd::assert_has(dboard_names, key.name, "dboard name");
276
        if (key.name == "A") val = _tx_dboard_proxies[DBOARD_SLOT_A]->get_link();
277
        if (key.name == "B") val = _tx_dboard_proxies[DBOARD_SLOT_B]->get_link();
278
        return;
279

    
280
    case MBOARD_PROP_TX_DBOARD_NAMES:
281
        val = dboard_names;
282
        return;
283

    
284
    case MBOARD_PROP_RX_DSP:
285
        UHD_ASSERT_THROW(key.name == "");
286
        val = _rx_dsp_proxy->get_link();
287
        return;
288

    
289
    case MBOARD_PROP_RX_DSP_NAMES:
290
        val = prop_names_t(1, "");
291
        return;
292

    
293
    case MBOARD_PROP_TX_DSP:
294
        UHD_ASSERT_THROW(key.name == "");
295
        val = _tx_dsp_proxy->get_link();
296
        return;
297

    
298
    case MBOARD_PROP_TX_DSP_NAMES:
299
        val = prop_names_t(1, "");
300
        return;
301

    
302
    case MBOARD_PROP_CLOCK_CONFIG:
303
        val = _clock_config;
304
        return;
305

    
306
    case MBOARD_PROP_RX_SUBDEV_SPEC:
307
        val = _rx_subdev_spec;
308
        return;
309

    
310
    case MBOARD_PROP_TX_SUBDEV_SPEC:
311
        val = _tx_subdev_spec;
312
        return;
313

    
314
    default: UHD_THROW_PROP_GET_ERROR();
315
    }
316
}
317

    
318
/***********************************************************************
319
 * Mboard Set
320
 **********************************************************************/
321
void usrp1_impl::mboard_set(const wax::obj &key, const wax::obj &val)
322
{
323
    if(key.type() == typeid(std::string)) {
324
      if(key.as<std::string>() == "load_eeprom") {
325
        std::string usrp1_eeprom_image = val.as<std::string>();
326
        std::cout << "USRP1 EEPROM image: " << usrp1_eeprom_image << std::endl;
327
        _ctrl_transport->usrp_load_eeprom(val.as<std::string>());
328
      }
329

    
330
      if(key.as<std::string>() == "serial") {
331
        std::string sernum = val.as<std::string>();
332
        uhd::byte_vector_t buf(sernum.begin(), sernum.end());
333
        buf.insert(buf.begin(), 248);
334
        _iface->write_i2c(I2C_DEV_EEPROM, buf);
335
      }
336

    
337
      return;
338
           }
339

    
340
    //handle the get request conditioned on the key
341
    switch(key.as<mboard_prop_t>()){
342

    
343
    case MBOARD_PROP_STREAM_CMD:
344
        issue_stream_cmd(val.as<stream_cmd_t>());
345
        return;
346

    
347
    case MBOARD_PROP_RX_SUBDEV_SPEC:
348
        _rx_subdev_spec = val.as<subdev_spec_t>();
349
        verify_rx_subdev_spec(_rx_subdev_spec, _mboard_proxy->get_link());
350
        //sanity check
351
        UHD_ASSERT_THROW(_rx_subdev_spec.size() <= 2);
352
        //set the mux and set the number of rx channels
353
        _iface->poke32(FR_RX_MUX, calc_rx_mux(_rx_subdev_spec, _mboard_proxy->get_link()));
354
        return;
355

    
356
    case MBOARD_PROP_TX_SUBDEV_SPEC:
357
        _tx_subdev_spec = val.as<subdev_spec_t>();
358
        verify_tx_subdev_spec(_tx_subdev_spec, _mboard_proxy->get_link());
359
        //sanity check
360
        UHD_ASSERT_THROW(_tx_subdev_spec.size() <= 2);
361
        //set the mux and set the number of tx channels
362
        _iface->poke32(FR_TX_MUX, calc_tx_mux(_tx_subdev_spec, _mboard_proxy->get_link()));
363
        return;
364

    
365
    default: UHD_THROW_PROP_SET_ERROR();
366
    }
367
}