Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / dboard_iface.cpp @ 4357f5d3

History | View | Annotate | Download (13.7 KB)

1
//
2
// Copyright 2010-2011 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_iface.hpp"
19
#include "usrp1_impl.hpp"
20
#include "fpga_regs_common.h"
21
#include "usrp_spi_defs.h"
22
#include "fpga_regs_standard.h"
23
#include "clock_ctrl.hpp"
24
#include "codec_ctrl.hpp"
25
#include <uhd/usrp/dboard_iface.hpp>
26
#include <uhd/types/dict.hpp>
27
#include <uhd/utils/assert_has.hpp>
28
#include <boost/assign/list_of.hpp>
29
#include <iostream>
30

    
31
using namespace uhd;
32
using namespace uhd::usrp;
33
using namespace boost::assign;
34

    
35
static const dboard_id_t tvrx_id(0x0040);
36

    
37
class usrp1_dboard_iface : public dboard_iface {
38
public:
39

    
40
    usrp1_dboard_iface(usrp1_iface::sptr iface,
41
                       usrp1_clock_ctrl::sptr clock,
42
                       usrp1_codec_ctrl::sptr codec,
43
                       usrp1_impl::dboard_slot_t dboard_slot,
44
                       const dboard_id_t &rx_dboard_id
45
    ):
46
        _dboard_slot(dboard_slot),
47
        _rx_dboard_id(rx_dboard_id)
48
    {
49
        _iface = iface;
50
        _clock = clock;
51
        _codec = codec;
52

    
53
        //init the clock rate shadows
54
        this->set_clock_rate(UNIT_RX, this->get_clock_rates(UNIT_RX).front());
55
        this->set_clock_rate(UNIT_TX, this->get_clock_rates(UNIT_TX).front());
56
        
57
        //yes this is evil but it's necessary for TVRX to work on USRP1
58
        if(_rx_dboard_id == tvrx_id) _codec->bypass_adc_buffers(false);
59
        //else _codec->bypass_adc_buffers(false); //don't think this is necessary
60
    }
61

    
62
    ~usrp1_dboard_iface()
63
    {
64
        /* NOP */
65
    }
66

    
67
    special_props_t get_special_props()
68
    {
69
        special_props_t props;
70
        props.soft_clock_divider = true;
71
        props.mangle_i2c_addrs = (_dboard_slot == usrp1_impl::DBOARD_SLOT_B);
72
        return props;
73
    }
74

    
75
    void write_aux_dac(unit_t, aux_dac_t, double);
76
    double read_aux_adc(unit_t, aux_adc_t);
77

    
78
    void _set_pin_ctrl(unit_t, boost::uint16_t);
79
    void _set_atr_reg(unit_t, atr_reg_t, boost::uint16_t);
80
    void _set_gpio_ddr(unit_t, boost::uint16_t);
81
    void _set_gpio_out(unit_t, boost::uint16_t);
82
    void set_gpio_debug(unit_t, int);
83
    boost::uint16_t read_gpio(unit_t);
84

    
85
    void write_i2c(boost::uint8_t, const byte_vector_t &);
86
    byte_vector_t read_i2c(boost::uint8_t, size_t);
87

    
88
    void write_spi(unit_t unit,
89
                   const spi_config_t &config,
90
                   boost::uint32_t data,
91
                   size_t num_bits);
92

    
93
    boost::uint32_t read_write_spi(unit_t unit,
94
                                   const spi_config_t &config,
95
                                   boost::uint32_t data,
96
                                   size_t num_bits);
97

    
98
    void set_clock_rate(unit_t, double);
99
    std::vector<double> get_clock_rates(unit_t);
100
    double get_clock_rate(unit_t);
101
    void set_clock_enabled(unit_t, bool);
102
    double get_codec_rate(unit_t);
103

    
104
private:
105
    usrp1_iface::sptr _iface;
106
    usrp1_clock_ctrl::sptr _clock;
107
    usrp1_codec_ctrl::sptr _codec;
108
    uhd::dict<unit_t, double> _clock_rates;
109
    const usrp1_impl::dboard_slot_t _dboard_slot;
110
    const dboard_id_t &_rx_dboard_id;
111
};
112

    
113
/***********************************************************************
114
 * Make Function
115
 **********************************************************************/
116
dboard_iface::sptr usrp1_impl::make_dboard_iface(usrp1_iface::sptr iface,
117
                                           usrp1_clock_ctrl::sptr clock,
118
                                           usrp1_codec_ctrl::sptr codec,
119
                                           dboard_slot_t dboard_slot,
120
                                           const dboard_id_t &rx_dboard_id
121
){
122
    return dboard_iface::sptr(new usrp1_dboard_iface(
123
        iface, clock, codec, dboard_slot, rx_dboard_id
124
    ));
125
}
126

    
127
/***********************************************************************
128
 * Clock Rates
129
 **********************************************************************/
130
static const dboard_id_t dbsrx_classic_id(0x0002);
131

    
132
/*
133
 * Daughterboard reference clock register
134
 *
135
 * Bit  7    - 1 turns on refclk, 0 allows IO use
136
 * Bits 6:0  - Divider value
137
 */
138
void usrp1_dboard_iface::set_clock_rate(unit_t unit, double rate)
139
{
140
    assert_has(this->get_clock_rates(unit), rate, "dboard clock rate");
141
    _clock_rates[unit] = rate;
142

    
143
    if (unit == UNIT_RX && _rx_dboard_id == dbsrx_classic_id){
144
        size_t divider = size_t(_clock->get_master_clock_freq()/rate);
145
        switch(_dboard_slot){
146
        case usrp1_impl::DBOARD_SLOT_A:
147
            _iface->poke32(FR_RX_A_REFCLK, (divider & 0x7f) | 0x80);
148
            break;
149

    
150
        case usrp1_impl::DBOARD_SLOT_B:
151
            _iface->poke32(FR_RX_B_REFCLK, (divider & 0x7f) | 0x80);
152
            break;
153
        }
154
    }
155
}
156

    
157
std::vector<double> usrp1_dboard_iface::get_clock_rates(unit_t unit)
158
{
159
    std::vector<double> rates;
160
    if (unit == UNIT_RX && _rx_dboard_id == dbsrx_classic_id){
161
        for (size_t div = 1; div <= 127; div++)
162
            rates.push_back(_clock->get_master_clock_freq() / div);
163
    }
164
    else{
165
        rates.push_back(_clock->get_master_clock_freq());
166
    }
167
    return rates;
168
}
169

    
170
double usrp1_dboard_iface::get_clock_rate(unit_t unit)
171
{
172
    return _clock_rates[unit];
173
}
174

    
175
void usrp1_dboard_iface::set_clock_enabled(unit_t, bool)
176
{
177
    //TODO we can only enable for special case anyway...
178
}
179

    
180
double usrp1_dboard_iface::get_codec_rate(unit_t){
181
    return _clock->get_master_clock_freq();
182
}
183

    
184
/***********************************************************************
185
 * GPIO
186
 **********************************************************************/
187
void usrp1_dboard_iface::_set_pin_ctrl(unit_t unit, boost::uint16_t value)
188
{
189
    switch(unit) {
190
    case UNIT_RX:
191
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
192
             _iface->poke32(FR_ATR_MASK_1, value);
193
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
194
             _iface->poke32(FR_ATR_MASK_3, value);
195
        break;
196
    case UNIT_TX:
197
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
198
            _iface->poke32(FR_ATR_MASK_0, value);
199
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
200
            _iface->poke32(FR_ATR_MASK_2, value);
201
        break;
202
    }
203
}
204

    
205
void usrp1_dboard_iface::_set_gpio_ddr(unit_t unit, boost::uint16_t value)
206
{
207
    switch(unit) {
208
    case UNIT_RX:
209
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
210
            _iface->poke32(FR_OE_1, 0xffff0000 | value);
211
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
212
            _iface->poke32(FR_OE_3, 0xffff0000 | value);
213
        break;
214
    case UNIT_TX:
215
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
216
            _iface->poke32(FR_OE_0, 0xffff0000 | value);
217
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
218
            _iface->poke32(FR_OE_2, 0xffff0000 | value);
219
        break;
220
    }
221
}
222

    
223
void usrp1_dboard_iface::_set_gpio_out(unit_t unit, boost::uint16_t value)
224
{
225
    switch(unit) {
226
    case UNIT_RX:
227
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
228
            _iface->poke32(FR_IO_1, 0xffff0000 | value);
229
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
230
            _iface->poke32(FR_IO_3, 0xffff0000 | value);
231
        break;
232
    case UNIT_TX:
233
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
234
            _iface->poke32(FR_IO_0, 0xffff0000 | value);
235
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
236
            _iface->poke32(FR_IO_2, 0xffff0000 | value);
237
        break;
238
    }
239
}
240

    
241
void usrp1_dboard_iface::set_gpio_debug(unit_t, int)
242
{
243
    /* NOP */
244
}
245

    
246
boost::uint16_t usrp1_dboard_iface::read_gpio(unit_t unit)
247
{
248
    boost::uint32_t out_value;
249

    
250
    if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
251
        out_value = _iface->peek32(1);
252
    else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
253
        out_value = _iface->peek32(2);
254
    else
255
        UHD_THROW_INVALID_CODE_PATH();
256

    
257
    switch(unit) {
258
    case UNIT_RX:
259
        return (boost::uint16_t)((out_value >> 16) & 0x0000ffff);
260
    case UNIT_TX:
261
        return (boost::uint16_t)((out_value >>  0) & 0x0000ffff);
262
    }
263
    UHD_ASSERT_THROW(false);
264
}
265

    
266
void usrp1_dboard_iface::_set_atr_reg(unit_t unit,
267
                                     atr_reg_t atr, boost::uint16_t value)
268
{
269
    // Ignore unsupported states
270
    if ((atr == ATR_REG_IDLE) || (atr == ATR_REG_TX_ONLY))
271
        return;
272
    if(atr == ATR_REG_RX_ONLY) {
273
        switch(unit) {
274
        case UNIT_RX:
275
            if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
276
                _iface->poke32(FR_ATR_RXVAL_1, value);
277
            else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
278
                _iface->poke32(FR_ATR_RXVAL_3, value);
279
            break;
280
        case UNIT_TX:
281
            if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
282
                _iface->poke32(FR_ATR_RXVAL_0, value);
283
            else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
284
                _iface->poke32(FR_ATR_RXVAL_2, value);
285
            break;
286
        }
287
    } else if (atr == ATR_REG_FULL_DUPLEX) {
288
        switch(unit) {
289
        case UNIT_RX:
290
            if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
291
                _iface->poke32(FR_ATR_TXVAL_1, value);
292
            else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
293
                _iface->poke32(FR_ATR_TXVAL_3, value);
294
            break;
295
        case UNIT_TX:
296
            if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
297
                _iface->poke32(FR_ATR_TXVAL_0, value);
298
            else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
299
                _iface->poke32(FR_ATR_TXVAL_2, value);
300
            break;
301
        }
302
    }
303
}
304
/***********************************************************************
305
 * SPI
306
 **********************************************************************/
307
/*!
308
 * Static function to convert a unit type to a spi slave device number.
309
 * \param unit the dboard interface unit type enum
310
 * \param slot the side (A or B) the dboard is attached
311
 * \return the slave device number
312
 */
313
static boost::uint32_t unit_to_otw_spi_dev(dboard_iface::unit_t unit,
314
                                           usrp1_impl::dboard_slot_t slot)
315
{
316
    switch(unit) {
317
    case dboard_iface::UNIT_TX:
318
        if (slot == usrp1_impl::DBOARD_SLOT_A)
319
            return SPI_ENABLE_TX_A;
320
        else if (slot == usrp1_impl::DBOARD_SLOT_B)
321
            return SPI_ENABLE_TX_B;
322
        else
323
            break;
324
    case dboard_iface::UNIT_RX:
325
        if (slot == usrp1_impl::DBOARD_SLOT_A)
326
            return SPI_ENABLE_RX_A;
327
        else if (slot == usrp1_impl::DBOARD_SLOT_B)
328
            return SPI_ENABLE_RX_B;
329
        else
330
            break;
331
    }
332
    UHD_THROW_INVALID_CODE_PATH();
333
}
334

    
335
void usrp1_dboard_iface::write_spi(unit_t unit,
336
                                   const spi_config_t &config,
337
                                   boost::uint32_t data,
338
                                   size_t num_bits)
339
{
340
    _iface->transact_spi(unit_to_otw_spi_dev(unit, _dboard_slot),
341
                         config, data, num_bits, false);
342
}
343

    
344
boost::uint32_t usrp1_dboard_iface::read_write_spi(unit_t unit,
345
                                                   const spi_config_t &config,
346
                                                   boost::uint32_t data,
347
                                                   size_t num_bits)
348
{
349
    return _iface->transact_spi(unit_to_otw_spi_dev(unit, _dboard_slot),
350
                                config, data, num_bits, true);
351
}
352

    
353
/***********************************************************************
354
 * I2C
355
 **********************************************************************/
356
void usrp1_dboard_iface::write_i2c(boost::uint8_t addr,
357
                                   const byte_vector_t &bytes)
358
{
359
    return _iface->write_i2c(addr, bytes);
360
}
361

    
362
byte_vector_t usrp1_dboard_iface::read_i2c(boost::uint8_t addr,
363
                                           size_t num_bytes)
364
{
365
    return _iface->read_i2c(addr, num_bytes);
366
}
367

    
368
/***********************************************************************
369
 * Aux DAX/ADC
370
 **********************************************************************/
371
void usrp1_dboard_iface::write_aux_dac(dboard_iface::unit_t,
372
                                       aux_dac_t which, double value)
373
{
374
    //same aux dacs for each unit
375
    static const uhd::dict<aux_dac_t, usrp1_codec_ctrl::aux_dac_t>
376
        which_to_aux_dac = map_list_of
377
                                     (AUX_DAC_A, usrp1_codec_ctrl::AUX_DAC_A)
378
                                     (AUX_DAC_B, usrp1_codec_ctrl::AUX_DAC_B)
379
                                     (AUX_DAC_C, usrp1_codec_ctrl::AUX_DAC_C)
380
                                     (AUX_DAC_D, usrp1_codec_ctrl::AUX_DAC_D);
381

    
382
    _codec->write_aux_dac(which_to_aux_dac[which], value);
383
}
384

    
385
double usrp1_dboard_iface::read_aux_adc(dboard_iface::unit_t unit,
386
                                       aux_adc_t which)
387
{
388
    static const
389
    uhd::dict<unit_t, uhd::dict<aux_adc_t, usrp1_codec_ctrl::aux_adc_t> >
390
        unit_to_which_to_aux_adc = map_list_of(UNIT_RX, map_list_of
391
                                    (AUX_ADC_A, usrp1_codec_ctrl::AUX_ADC_A1)
392
                                    (AUX_ADC_B, usrp1_codec_ctrl::AUX_ADC_B1))
393
                                              (UNIT_TX, map_list_of
394
                                    (AUX_ADC_A, usrp1_codec_ctrl::AUX_ADC_A2)
395
                                    (AUX_ADC_B, usrp1_codec_ctrl::AUX_ADC_B2));
396

    
397
    return _codec->read_aux_adc(unit_to_which_to_aux_adc[unit][which]);
398
}