Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / dboard_iface.cpp @ af0543a8

History | View | Annotate | Download (12.6 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_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.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
class usrp1_dboard_iface : public dboard_iface {
36
public:
37

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

    
51
        //init the clock rate shadows
52
        this->set_clock_rate(UNIT_RX, this->get_clock_rates(UNIT_RX).front());
53
        this->set_clock_rate(UNIT_TX, this->get_clock_rates(UNIT_TX).front());
54
    }
55

    
56
    ~usrp1_dboard_iface()
57
    {
58
        /* NOP */
59
    }
60

    
61
    special_props_t get_special_props()
62
    {
63
        special_props_t props;
64
        props.soft_clock_divider = true;
65
        props.mangle_i2c_addrs = (_dboard_slot == usrp1_impl::DBOARD_SLOT_B);
66
        return props;
67
    }
68

    
69
    void write_aux_dac(unit_t, aux_dac_t, float);
70
    float read_aux_adc(unit_t, aux_adc_t);
71

    
72
    void set_pin_ctrl(unit_t, boost::uint16_t);
73
    void set_atr_reg(unit_t, atr_reg_t, boost::uint16_t);
74
    void set_gpio_ddr(unit_t, boost::uint16_t);
75
    void write_gpio(unit_t, boost::uint16_t);
76
    void set_gpio_debug(unit_t, int);
77
    boost::uint16_t read_gpio(unit_t);
78

    
79
    void write_i2c(boost::uint8_t, const byte_vector_t &);
80
    byte_vector_t read_i2c(boost::uint8_t, size_t);
81

    
82
    void write_spi(unit_t unit,
83
                   const spi_config_t &config,
84
                   boost::uint32_t data,
85
                   size_t num_bits);
86

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

    
92
    void set_clock_rate(unit_t, double);
93
    std::vector<double> get_clock_rates(unit_t);
94
    double get_clock_rate(unit_t);
95
    void set_clock_enabled(unit_t, bool);
96

    
97
private:
98
    usrp1_iface::sptr _iface;
99
    usrp1_clock_ctrl::sptr _clock;
100
    usrp1_codec_ctrl::sptr _codec;
101
    uhd::dict<unit_t, double> _clock_rates;
102
    const usrp1_impl::dboard_slot_t _dboard_slot;
103
    const dboard_id_t &_rx_dboard_id;
104
};
105

    
106
/***********************************************************************
107
 * Make Function
108
 **********************************************************************/
109
dboard_iface::sptr usrp1_impl::make_dboard_iface(usrp1_iface::sptr iface,
110
                                           usrp1_clock_ctrl::sptr clock,
111
                                           usrp1_codec_ctrl::sptr codec,
112
                                           dboard_slot_t dboard_slot,
113
                                           const dboard_id_t &rx_dboard_id
114
){
115
    return dboard_iface::sptr(new usrp1_dboard_iface(
116
        iface, clock, codec, dboard_slot, rx_dboard_id
117
    ));
118
}
119

    
120
/***********************************************************************
121
 * Clock Rates
122
 **********************************************************************/
123
static const dboard_id_t dbsrx_classic_id(0x0002);
124

    
125
/*
126
 * Daughterboard reference clock register
127
 *
128
 * Bit  7    - 1 turns on refclk, 0 allows IO use
129
 * Bits 6:0  - Divider value
130
 */
131
void usrp1_dboard_iface::set_clock_rate(unit_t unit, double rate)
132
{
133
    assert_has(this->get_clock_rates(unit), rate, "dboard clock rate");
134
    _clock_rates[unit] = rate;
135

    
136
    if (unit == UNIT_RX && _rx_dboard_id == dbsrx_classic_id){
137
        size_t divider = size_t(_clock->get_master_clock_freq()/rate);
138
        switch(_dboard_slot){
139
        case usrp1_impl::DBOARD_SLOT_A:
140
            _iface->poke32(FR_RX_A_REFCLK, (2*divider & 0x7f) | 0x80);
141
            break;
142

    
143
        case usrp1_impl::DBOARD_SLOT_B:
144
            _iface->poke32(FR_RX_B_REFCLK, (2*divider & 0x7f) | 0x80);
145
            break;
146
        }
147
    }
148
}
149

    
150
std::vector<double> usrp1_dboard_iface::get_clock_rates(unit_t unit)
151
{
152
    std::vector<double> rates;
153
    if (unit == UNIT_RX && _rx_dboard_id == dbsrx_classic_id){
154
        for (size_t div = 8; div <= 127; div++)
155
            rates.push_back(_clock->get_master_clock_freq() / div);
156
    }
157
    else{
158
        rates.push_back(_clock->get_master_clock_freq());
159
    }
160
    return rates;
161
}
162

    
163
double usrp1_dboard_iface::get_clock_rate(unit_t unit)
164
{
165
    return _clock_rates[unit];
166
}
167

    
168
void usrp1_dboard_iface::set_clock_enabled(unit_t, bool)
169
{
170
    //TODO we can only enable for special case anyway...
171
}
172

    
173
/***********************************************************************
174
 * GPIO
175
 **********************************************************************/
176
void usrp1_dboard_iface::set_pin_ctrl(unit_t unit, boost::uint16_t value)
177
{
178
    switch(unit) {
179
    case UNIT_RX:
180
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
181
             _iface->poke32(FR_ATR_MASK_1, value);
182
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
183
             _iface->poke32(FR_ATR_MASK_3, value);
184
        break;
185
    case UNIT_TX:
186
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
187
            _iface->poke32(FR_ATR_MASK_0, value);
188
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
189
            _iface->poke32(FR_ATR_MASK_2, value);
190
        break;
191
    }
192
}
193

    
194
void usrp1_dboard_iface::set_gpio_ddr(unit_t unit, boost::uint16_t value)
195
{
196
    switch(unit) {
197
    case UNIT_RX:
198
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
199
            _iface->poke32(FR_OE_1, 0xffff0000 | value);
200
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
201
            _iface->poke32(FR_OE_3, 0xffff0000 | value);
202
        break;
203
    case UNIT_TX:
204
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
205
            _iface->poke32(FR_OE_0, 0xffff0000 | value);
206
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
207
            _iface->poke32(FR_OE_2, 0xffff0000 | value);
208
        break;
209
    }
210
}
211

    
212
void usrp1_dboard_iface::write_gpio(unit_t unit, boost::uint16_t value)
213
{
214
    switch(unit) {
215
    case UNIT_RX:
216
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
217
            _iface->poke32(FR_IO_1, 0xffff0000 | value);
218
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
219
            _iface->poke32(FR_IO_3, 0xffff0000 | value);
220
        break;
221
    case UNIT_TX:
222
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
223
            _iface->poke32(FR_IO_0, 0xffff0000 | value);
224
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
225
            _iface->poke32(FR_IO_2, 0xffff0000 | value);
226
        break;
227
    }
228
}
229

    
230
void usrp1_dboard_iface::set_gpio_debug(unit_t, int)
231
{
232
    /* NOP */
233
}
234

    
235
boost::uint16_t usrp1_dboard_iface::read_gpio(unit_t unit)
236
{
237
    boost::uint32_t out_value;
238

    
239
    if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
240
        out_value = _iface->peek32(1);
241
    else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
242
        out_value = _iface->peek32(2);
243
    else
244
        UHD_THROW_INVALID_CODE_PATH();
245

    
246
    switch(unit) {
247
    case UNIT_RX:
248
        return (boost::uint16_t)((out_value >> 16) & 0x0000ffff);
249
    case UNIT_TX:
250
        return (boost::uint16_t)((out_value >>  0) & 0x0000ffff);
251
    }
252
    UHD_ASSERT_THROW(false);
253
}
254

    
255
void usrp1_dboard_iface::set_atr_reg(unit_t unit,
256
                                     atr_reg_t atr, boost::uint16_t value)
257
{
258
    // Ignore unsupported states
259
    if ((atr == ATR_REG_IDLE) || (atr == ATR_REG_FULL_DUPLEX))
260
        return;
261

    
262
    switch(unit) {
263
    case UNIT_RX:
264
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
265
            _iface->poke32(FR_ATR_RXVAL_1, value);
266
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
267
            _iface->poke32(FR_ATR_RXVAL_3, value);
268
        break;
269
    case UNIT_TX:
270
        if (_dboard_slot == usrp1_impl::DBOARD_SLOT_A)
271
            _iface->poke32(FR_ATR_TXVAL_0, value);
272
        else if (_dboard_slot == usrp1_impl::DBOARD_SLOT_B)
273
            _iface->poke32(FR_ATR_TXVAL_2, value);
274
        break;
275
    }
276
}
277
/***********************************************************************
278
 * SPI
279
 **********************************************************************/
280
/*!
281
 * Static function to convert a unit type to a spi slave device number.
282
 * \param unit the dboard interface unit type enum
283
 * \param slot the side (A or B) the dboard is attached
284
 * \return the slave device number
285
 */
286
static boost::uint32_t unit_to_otw_spi_dev(dboard_iface::unit_t unit,
287
                                           usrp1_impl::dboard_slot_t slot)
288
{
289
    switch(unit) {
290
    case dboard_iface::UNIT_TX:
291
        if (slot == usrp1_impl::DBOARD_SLOT_A)
292
            return SPI_ENABLE_TX_A;
293
        else if (slot == usrp1_impl::DBOARD_SLOT_B)
294
            return SPI_ENABLE_TX_B;
295
        else
296
            break;
297
    case dboard_iface::UNIT_RX:
298
        if (slot == usrp1_impl::DBOARD_SLOT_A)
299
            return SPI_ENABLE_RX_A;
300
        else if (slot == usrp1_impl::DBOARD_SLOT_B)
301
            return SPI_ENABLE_RX_B;
302
        else
303
            break;
304
    }
305
    throw std::invalid_argument("unknown unit type");
306
}
307

    
308
void usrp1_dboard_iface::write_spi(unit_t unit,
309
                                   const spi_config_t &config,
310
                                   boost::uint32_t data,
311
                                   size_t num_bits)
312
{
313
    _iface->transact_spi(unit_to_otw_spi_dev(unit, _dboard_slot),
314
                         config, data, num_bits, false);
315
}
316

    
317
boost::uint32_t usrp1_dboard_iface::read_write_spi(unit_t unit,
318
                                                   const spi_config_t &config,
319
                                                   boost::uint32_t data,
320
                                                   size_t num_bits)
321
{
322
    return _iface->transact_spi(unit_to_otw_spi_dev(unit, _dboard_slot),
323
                                config, data, num_bits, true);
324
}
325

    
326
/***********************************************************************
327
 * I2C
328
 **********************************************************************/
329
void usrp1_dboard_iface::write_i2c(boost::uint8_t addr,
330
                                   const byte_vector_t &bytes)
331
{
332
    return _iface->write_i2c(addr, bytes);
333
}
334

    
335
byte_vector_t usrp1_dboard_iface::read_i2c(boost::uint8_t addr,
336
                                           size_t num_bytes)
337
{
338
    return _iface->read_i2c(addr, num_bytes);
339
}
340

    
341
/***********************************************************************
342
 * Aux DAX/ADC
343
 **********************************************************************/
344
void usrp1_dboard_iface::write_aux_dac(dboard_iface::unit_t,
345
                                       aux_dac_t which, float value)
346
{
347
    //same aux dacs for each unit
348
    static const uhd::dict<aux_dac_t, usrp1_codec_ctrl::aux_dac_t>
349
        which_to_aux_dac = map_list_of
350
                                     (AUX_DAC_A, usrp1_codec_ctrl::AUX_DAC_A)
351
                                     (AUX_DAC_B, usrp1_codec_ctrl::AUX_DAC_B)
352
                                     (AUX_DAC_C, usrp1_codec_ctrl::AUX_DAC_C)
353
                                     (AUX_DAC_D, usrp1_codec_ctrl::AUX_DAC_D);
354

    
355
    _codec->write_aux_dac(which_to_aux_dac[which], value);
356
}
357

    
358
float usrp1_dboard_iface::read_aux_adc(dboard_iface::unit_t unit,
359
                                       aux_adc_t which)
360
{
361
    static const
362
    uhd::dict<unit_t, uhd::dict<aux_adc_t, usrp1_codec_ctrl::aux_adc_t> >
363
        unit_to_which_to_aux_adc = map_list_of(UNIT_RX, map_list_of
364
                                    (AUX_ADC_A, usrp1_codec_ctrl::AUX_ADC_A1)
365
                                    (AUX_ADC_B, usrp1_codec_ctrl::AUX_ADC_B1))
366
                                              (UNIT_TX, map_list_of
367
                                    (AUX_ADC_A, usrp1_codec_ctrl::AUX_ADC_A2)
368
                                    (AUX_ADC_B, usrp1_codec_ctrl::AUX_ADC_B2));
369

    
370
    return _codec->read_aux_adc(unit_to_which_to_aux_adc[unit][which]);
371
}