Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp2 / dboard_interface.cpp @ 389a72ef

History | View | Annotate | Download (9.03 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 "usrp2_impl.hpp"
19
#include "usrp2_regs.hpp"
20
#include <uhd/types/dict.hpp>
21
#include <uhd/utils/assert.hpp>
22
#include <boost/assign/list_of.hpp>
23
#include <algorithm>
24

    
25
using namespace uhd::usrp;
26

    
27
class usrp2_dboard_interface : public dboard_interface{
28
public:
29
    usrp2_dboard_interface(usrp2_impl *impl);
30
    ~usrp2_dboard_interface(void);
31

    
32
    void write_aux_dac(unit_t, int, int);
33
    int read_aux_adc(unit_t, int);
34

    
35
    void set_atr_reg(unit_t, atr_reg_t, boost::uint16_t);
36
    void set_gpio_ddr(unit_t, boost::uint16_t);
37
    boost::uint16_t read_gpio(unit_t);
38

    
39
    void write_i2c(int, const byte_vector_t &);
40
    byte_vector_t read_i2c(int, size_t);
41

    
42
    double get_clock_rate(unit_t);
43
    void set_clock_enabled(unit_t, bool);
44
    bool get_clock_enabled(unit_t);
45

    
46
    void write_spi(
47
        unit_t unit,
48
        const spi_config_t &config,
49
        boost::uint32_t data,
50
        size_t num_bits
51
    );
52

    
53
    boost::uint32_t read_write_spi(
54
        unit_t unit,
55
        const spi_config_t &config,
56
        boost::uint32_t data,
57
        size_t num_bits
58
    );
59

    
60
private:
61
    usrp2_impl *_impl;
62
    boost::uint32_t _ddr_shadow;
63
};
64

    
65
/***********************************************************************
66
 * Make Function
67
 **********************************************************************/
68
dboard_interface::sptr make_usrp2_dboard_interface(usrp2_impl *impl){
69
    return dboard_interface::sptr(new usrp2_dboard_interface(impl));
70
}
71

    
72
/***********************************************************************
73
 * Structors
74
 **********************************************************************/
75
usrp2_dboard_interface::usrp2_dboard_interface(usrp2_impl *impl){
76
    _impl = impl;
77
    _ddr_shadow = 0;
78

    
79
    //set the selection mux to use atr
80
    boost::uint32_t new_sels = 0x0;
81
    for(size_t i = 0; i < 16; i++){
82
        new_sels |= FRF_GPIO_SEL_ATR << (i*2);
83
    }
84
    _impl->poke32(FR_GPIO_TX_SEL, new_sels);
85
    _impl->poke32(FR_GPIO_RX_SEL, new_sels);
86
}
87

    
88
usrp2_dboard_interface::~usrp2_dboard_interface(void){
89
    /* NOP */
90
}
91

    
92
/***********************************************************************
93
 * Clocks
94
 **********************************************************************/
95
double usrp2_dboard_interface::get_clock_rate(unit_t){
96
    return _impl->get_master_clock_freq();
97
}
98

    
99
void usrp2_dboard_interface::set_clock_enabled(unit_t, bool){
100
    //TODO
101
}
102

    
103
bool usrp2_dboard_interface::get_clock_enabled(unit_t){
104
    return false; //TODO
105
}
106

    
107
/***********************************************************************
108
 * GPIO
109
 **********************************************************************/
110
static int unit_to_shift(dboard_interface::unit_t unit){
111
    switch(unit){
112
    case dboard_interface::UNIT_RX: return 0;
113
    case dboard_interface::UNIT_TX: return 16;
114
    }
115
    throw std::runtime_error("unknown unit type");
116
}
117

    
118
void usrp2_dboard_interface::set_gpio_ddr(unit_t unit, boost::uint16_t value){
119
    _ddr_shadow = \
120
        (_ddr_shadow & ~(0xffff << unit_to_shift(unit))) |
121
        (boost::uint32_t(value) << unit_to_shift(unit));
122
    _impl->poke32(FR_GPIO_DDR, _ddr_shadow);
123
}
124

    
125
boost::uint16_t usrp2_dboard_interface::read_gpio(unit_t unit){
126
    return boost::uint16_t(_impl->peek32(FR_GPIO_IO) >> unit_to_shift(unit));
127
}
128

    
129
void usrp2_dboard_interface::set_atr_reg(unit_t unit, atr_reg_t atr, boost::uint16_t value){
130
    //define mapping of unit to atr regs to register address
131
    static const uhd::dict<
132
        unit_t, uhd::dict<atr_reg_t, boost::uint32_t>
133
    > unit_to_atr_to_addr = boost::assign::map_list_of
134
        (UNIT_RX, boost::assign::map_list_of
135
            (ATR_REG_IDLE,        FR_ATR_IDLE_RXSIDE)
136
            (ATR_REG_TX_ONLY,     FR_ATR_INTX_RXSIDE)
137
            (ATR_REG_RX_ONLY,     FR_ATR_INRX_RXSIDE)
138
            (ATR_REG_FULL_DUPLEX, FR_ATR_FULL_RXSIDE)
139
        )
140
        (UNIT_TX, boost::assign::map_list_of
141
            (ATR_REG_IDLE,        FR_ATR_IDLE_TXSIDE)
142
            (ATR_REG_TX_ONLY,     FR_ATR_INTX_TXSIDE)
143
            (ATR_REG_RX_ONLY,     FR_ATR_INRX_TXSIDE)
144
            (ATR_REG_FULL_DUPLEX, FR_ATR_FULL_TXSIDE)
145
        )
146
    ;
147
    _impl->poke16(unit_to_atr_to_addr[unit][atr], value);
148
}
149

    
150
/***********************************************************************
151
 * SPI
152
 **********************************************************************/
153
/*!
154
 * Static function to convert a unit type enum
155
 * to an over-the-wire value for the spi device.
156
 * \param unit the dboard interface unit type enum
157
 * \return an over the wire representation
158
 */
159
static boost::uint8_t unit_to_otw_spi_dev(dboard_interface::unit_t unit){
160
    switch(unit){
161
    case dboard_interface::UNIT_TX: return SPI_SS_TX_DB;
162
    case dboard_interface::UNIT_RX: return SPI_SS_RX_DB;
163
    }
164
    throw std::invalid_argument("unknown unit type");
165
}
166

    
167
void usrp2_dboard_interface::write_spi(
168
    unit_t unit,
169
    const spi_config_t &config,
170
    boost::uint32_t data,
171
    size_t num_bits
172
){
173
    _impl->transact_spi(unit_to_otw_spi_dev(unit), config, data, num_bits, false /*no rb*/);
174
}
175

    
176
boost::uint32_t usrp2_dboard_interface::read_write_spi(
177
    unit_t unit,
178
    const spi_config_t &config,
179
    boost::uint32_t data,
180
    size_t num_bits
181
){
182
    return _impl->transact_spi(unit_to_otw_spi_dev(unit), config, data, num_bits, true /*rb*/);
183
}
184

    
185
/***********************************************************************
186
 * I2C
187
 **********************************************************************/
188
void usrp2_dboard_interface::write_i2c(int i2c_addr, const byte_vector_t &buf){
189
    //setup the out data
190
    usrp2_ctrl_data_t out_data;
191
    out_data.id = htonl(USRP2_CTRL_ID_WRITE_THESE_I2C_VALUES_BRO);
192
    out_data.data.i2c_args.addr = i2c_addr;
193
    out_data.data.i2c_args.bytes = buf.size();
194

    
195
    //limitation of i2c transaction size
196
    ASSERT_THROW(buf.size() <= sizeof(out_data.data.i2c_args.data));
197

    
198
    //copy in the data
199
    std::copy(buf.begin(), buf.end(), out_data.data.i2c_args.data);
200

    
201
    //send and recv
202
    usrp2_ctrl_data_t in_data = _impl->ctrl_send_and_recv(out_data);
203
    ASSERT_THROW(htonl(in_data.id) == USRP2_CTRL_ID_COOL_IM_DONE_I2C_WRITE_DUDE);
204
}
205

    
206
dboard_interface::byte_vector_t usrp2_dboard_interface::read_i2c(int i2c_addr, size_t num_bytes){
207
    //setup the out data
208
    usrp2_ctrl_data_t out_data;
209
    out_data.id = htonl(USRP2_CTRL_ID_DO_AN_I2C_READ_FOR_ME_BRO);
210
    out_data.data.i2c_args.addr = i2c_addr;
211
    out_data.data.i2c_args.bytes = num_bytes;
212

    
213
    //limitation of i2c transaction size
214
    ASSERT_THROW(num_bytes <= sizeof(out_data.data.i2c_args.data));
215

    
216
    //send and recv
217
    usrp2_ctrl_data_t in_data = _impl->ctrl_send_and_recv(out_data);
218
    ASSERT_THROW(htonl(in_data.id) == USRP2_CTRL_ID_HERES_THE_I2C_DATA_DUDE);
219
    ASSERT_THROW(in_data.data.i2c_args.addr = num_bytes);
220

    
221
    //copy out the data
222
    byte_vector_t result(num_bytes);
223
    std::copy(in_data.data.i2c_args.data, in_data.data.i2c_args.data + num_bytes, result.begin());
224
    return result;
225
}
226

    
227
/***********************************************************************
228
 * Aux DAX/ADC
229
 **********************************************************************/
230
/*!
231
 * Static function to convert a unit type enum
232
 * to an over-the-wire value for the usrp2 control.
233
 * \param unit the dboard interface unit type enum
234
 * \return an over the wire representation
235
 */
236
static boost::uint8_t unit_to_otw(dboard_interface::unit_t unit){
237
    switch(unit){
238
    case dboard_interface::UNIT_TX: return USRP2_DIR_TX;
239
    case dboard_interface::UNIT_RX: return USRP2_DIR_RX;
240
    }
241
    throw std::invalid_argument("unknown unit type");
242
}
243

    
244
void usrp2_dboard_interface::write_aux_dac(unit_t unit, int which, int value){
245
    //setup the out data
246
    usrp2_ctrl_data_t out_data;
247
    out_data.id = htonl(USRP2_CTRL_ID_WRITE_THIS_TO_THE_AUX_DAC_BRO);
248
    out_data.data.aux_args.dir = unit_to_otw(unit);
249
    out_data.data.aux_args.which = which;
250
    out_data.data.aux_args.value = htonl(value);
251

    
252
    //send and recv
253
    usrp2_ctrl_data_t in_data = _impl->ctrl_send_and_recv(out_data);
254
    ASSERT_THROW(htonl(in_data.id) == USRP2_CTRL_ID_DONE_WITH_THAT_AUX_DAC_DUDE);
255
}
256

    
257
int usrp2_dboard_interface::read_aux_adc(unit_t unit, int which){
258
    //setup the out data
259
    usrp2_ctrl_data_t out_data;
260
    out_data.id = htonl(USRP2_CTRL_ID_READ_FROM_THIS_AUX_ADC_BRO);
261
    out_data.data.aux_args.dir = unit_to_otw(unit);
262
    out_data.data.aux_args.which = which;
263

    
264
    //send and recv
265
    usrp2_ctrl_data_t in_data = _impl->ctrl_send_and_recv(out_data);
266
    ASSERT_THROW(htonl(in_data.id) == USRP2_CTRL_ID_DONE_WITH_THAT_AUX_ADC_DUDE);
267
    return ntohl(in_data.data.aux_args.value);
268
}