Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / mboard_impl.cpp @ 4c7a66cf

History | View | Annotate | Download (14.3 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 <boost/thread/thread.hpp>
34
#include <iostream>
35

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
255
    if(key_.type() == typeid(std::string)) {
256
      if(key.as<std::string>() == "serial") {
257
        uhd::byte_vector_t buf;
258
        buf.insert(buf.begin(), 248);
259
        boost::this_thread::sleep(boost::posix_time::milliseconds(100));
260
        _iface->write_i2c(I2C_DEV_EEPROM, buf);
261
        boost::this_thread::sleep(boost::posix_time::milliseconds(100));
262
        buf = _iface->read_i2c(I2C_DEV_EEPROM, 8);
263
        val = std::string(buf.begin(), buf.end());
264
      }
265

    
266
      return;
267
           }
268

    
269
    //handle the get request conditioned on the key
270
    switch(key.as<mboard_prop_t>()){
271
    case MBOARD_PROP_NAME:
272
        val = std::string("usrp1 mboard");
273
        return;
274

    
275
    case MBOARD_PROP_OTHERS:
276
        val = prop_names_t();
277
        return;
278

    
279
    case MBOARD_PROP_RX_DBOARD:
280
        uhd::assert_has(dboard_names, key.name, "dboard name");
281
        if (key.name == "A") val = _rx_dboard_proxies[DBOARD_SLOT_A]->get_link();
282
        if (key.name == "B") val = _rx_dboard_proxies[DBOARD_SLOT_B]->get_link();
283
        return;
284

    
285
    case MBOARD_PROP_RX_DBOARD_NAMES:
286
        val = dboard_names;
287
        return;
288

    
289
    case MBOARD_PROP_TX_DBOARD:
290
        uhd::assert_has(dboard_names, key.name, "dboard name");
291
        if (key.name == "A") val = _tx_dboard_proxies[DBOARD_SLOT_A]->get_link();
292
        if (key.name == "B") val = _tx_dboard_proxies[DBOARD_SLOT_B]->get_link();
293
        return;
294

    
295
    case MBOARD_PROP_TX_DBOARD_NAMES:
296
        val = dboard_names;
297
        return;
298

    
299
    case MBOARD_PROP_RX_DSP:
300
        UHD_ASSERT_THROW(key.name == "");
301
        val = _rx_dsp_proxy->get_link();
302
        return;
303

    
304
    case MBOARD_PROP_RX_DSP_NAMES:
305
        val = prop_names_t(1, "");
306
        return;
307

    
308
    case MBOARD_PROP_TX_DSP:
309
        UHD_ASSERT_THROW(key.name == "");
310
        val = _tx_dsp_proxy->get_link();
311
        return;
312

    
313
    case MBOARD_PROP_TX_DSP_NAMES:
314
        val = prop_names_t(1, "");
315
        return;
316

    
317
    case MBOARD_PROP_CLOCK_CONFIG:
318
        val = _clock_config;
319
        return;
320

    
321
    case MBOARD_PROP_RX_SUBDEV_SPEC:
322
        val = _rx_subdev_spec;
323
        return;
324

    
325
    case MBOARD_PROP_TX_SUBDEV_SPEC:
326
        val = _tx_subdev_spec;
327
        return;
328

    
329
    default: UHD_THROW_PROP_GET_ERROR();
330
    }
331
}
332

    
333
/***********************************************************************
334
 * Mboard Set
335
 **********************************************************************/
336
void usrp1_impl::mboard_set(const wax::obj &key, const wax::obj &val)
337
{
338
    if(key.type() == typeid(std::string)) {
339
      if(key.as<std::string>() == "load_eeprom") {
340
        std::string usrp1_eeprom_image = val.as<std::string>();
341
        std::cout << "USRP1 EEPROM image: " << usrp1_eeprom_image << std::endl;
342
        _ctrl_transport->usrp_load_eeprom(val.as<std::string>());
343
      }
344

    
345
      if(key.as<std::string>() == "serial") {
346
        std::string sernum = val.as<std::string>();
347
        uhd::byte_vector_t buf(sernum.begin(), sernum.end());
348
        buf.insert(buf.begin(), 248);
349
        _iface->write_i2c(I2C_DEV_EEPROM, buf);
350
      }
351

    
352
      return;
353
           }
354

    
355
    //handle the get request conditioned on the key
356
    switch(key.as<mboard_prop_t>()){
357

    
358
    case MBOARD_PROP_STREAM_CMD:
359
        issue_stream_cmd(val.as<stream_cmd_t>());
360
        return;
361

    
362
    case MBOARD_PROP_RX_SUBDEV_SPEC:
363
        _rx_subdev_spec = val.as<subdev_spec_t>();
364
        verify_rx_subdev_spec(_rx_subdev_spec, _mboard_proxy->get_link());
365
        //sanity check
366
        UHD_ASSERT_THROW(_rx_subdev_spec.size() <= 2);
367
        //set the mux and set the number of rx channels
368
        _iface->poke32(FR_RX_MUX, calc_rx_mux(_rx_subdev_spec, _mboard_proxy->get_link()));
369
        return;
370

    
371
    case MBOARD_PROP_TX_SUBDEV_SPEC:
372
        _tx_subdev_spec = val.as<subdev_spec_t>();
373
        verify_tx_subdev_spec(_tx_subdev_spec, _mboard_proxy->get_link());
374
        //sanity check
375
        UHD_ASSERT_THROW(_tx_subdev_spec.size() <= 2);
376
        //set the mux and set the number of tx channels
377
        _iface->poke32(FR_TX_MUX, calc_tx_mux(_tx_subdev_spec, _mboard_proxy->get_link()));
378
        return;
379

    
380
    default: UHD_THROW_PROP_SET_ERROR();
381
    }
382
}