Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / usrp1_iface.cpp @ c30cbf65

History | View | Annotate | Download (9.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 "usrp_commands.h"
20
#include <uhd/utils/assert.hpp>
21
#include <uhd/utils/byteswap.hpp>
22
#include <boost/format.hpp>
23
#include <stdexcept>
24
#include <iostream>
25
#include <iomanip>
26

    
27
using namespace uhd;
28
using namespace uhd::transport;
29

    
30
static const bool iface_debug = false;
31

    
32
class usrp1_iface_impl : public usrp1_iface{
33
public:
34
    /*******************************************************************
35
     * Structors
36
     ******************************************************************/
37
    usrp1_iface_impl(usrp_ctrl::sptr ctrl_transport)
38
    {
39
        _ctrl_transport = ctrl_transport; 
40
    }
41

    
42
    ~usrp1_iface_impl(void)
43
    {
44
        /* NOP */
45
    }
46

    
47
    /*******************************************************************
48
     * Peek and Poke
49
     ******************************************************************/
50
    void poke32(boost::uint32_t addr, boost::uint32_t value)
51
    {
52
        boost::uint32_t swapped = byteswap(value);
53

    
54
        if (iface_debug) {
55
            std::cout.fill('0');
56
            std::cout << "poke32(";
57
            std::cout << std::dec << std::setw(2) << addr << ", 0x";
58
            std::cout << std::hex << std::setw(8) << value << ")" << std::endl;
59
        }
60

    
61
        boost::uint8_t w_index_h = SPI_ENABLE_FPGA & 0xff;
62
        boost::uint8_t w_index_l = (SPI_FMT_MSB | SPI_FMT_HDR_1) & 0xff;
63

    
64
        int ret =_ctrl_transport->usrp_control_write(
65
                                          VRQ_SPI_WRITE,
66
                                          addr & 0x7f,
67
                                          (w_index_h << 8) | (w_index_l << 0),
68
                                          (unsigned char*) &swapped,
69
                                          sizeof(boost::uint32_t));
70

    
71
        if (ret < 0)
72
            std::cerr << "USRP: failed memory write: " << ret << std::endl;
73
    }
74

    
75
    void poke16(boost::uint32_t, boost::uint16_t)
76
    {
77
        //fpga only handles 32 bit writes
78
        std::cerr << "USRP: unsupported operation: poke16()" << std::endl;
79
    }
80

    
81
    boost::uint32_t peek32(boost::uint32_t addr)
82
    {
83
        uint32_t value_out;
84

    
85
        boost::uint8_t w_index_h = SPI_ENABLE_FPGA & 0xff;
86
        boost::uint8_t w_index_l = (SPI_FMT_MSB | SPI_FMT_HDR_1) & 0xff;
87

    
88
        int ret = _ctrl_transport->usrp_control_read(
89
                                          VRQ_SPI_READ,
90
                                          0x80 | (addr & 0x7f),
91
                                          (w_index_h << 8) | (w_index_l << 0),
92
                                          (unsigned char*) &value_out,
93
                                          sizeof(boost::uint32_t));
94

    
95
        if (ret < 0)
96
            std::cerr << "USRP: failed memory read: " << ret << std::endl;
97

    
98
        return byteswap(value_out);
99
    }
100

    
101
    boost::uint16_t peek16(boost::uint32_t addr)
102
    {
103
        uint32_t val = peek32(addr);
104
        return boost::uint16_t(val & 0xff);
105
    }
106

    
107
    /*******************************************************************
108
     * I2C
109
     ******************************************************************/
110
    static const size_t max_i2c_data_bytes = 64;
111

    
112
    void write_i2c(boost::uint8_t addr, const byte_vector_t &bytes)
113
    {
114
        UHD_ASSERT_THROW(bytes.size() < max_i2c_data_bytes);
115

    
116
        unsigned char buff[max_i2c_data_bytes];
117
        std::copy(bytes.begin(), bytes.end(), buff); 
118

    
119
        int ret = _ctrl_transport->usrp_control_write(VRQ_I2C_WRITE,
120
                                                      addr & 0xff,
121
                                                      0,
122
                                                      buff,
123
                                                      bytes.size());
124

    
125
        // TODO throw and catch i2c failures during eeprom read
126
        if (iface_debug && (ret < 0))
127
            std::cerr << "USRP: failed i2c write: " << ret << std::endl;
128
    }
129

    
130
    byte_vector_t read_i2c(boost::uint8_t addr, size_t num_bytes)
131
    {
132
        UHD_ASSERT_THROW(num_bytes < max_i2c_data_bytes);
133

    
134
        unsigned char buff[max_i2c_data_bytes];
135
        int ret = _ctrl_transport->usrp_control_read(VRQ_I2C_READ,
136
                                                     addr & 0xff,
137
                                                     0,
138
                                                     buff,
139
                                                     num_bytes);
140

    
141
        // TODO throw and catch i2c failures during eeprom read
142
        if (iface_debug && ((ret < 0) || (unsigned)ret < (num_bytes))) {
143
            std::cerr << "USRP: failed i2c read: " << ret << std::endl;
144
            return byte_vector_t(num_bytes, 0xff); 
145
        }
146

    
147
        byte_vector_t out_bytes;
148
        for (size_t i = 0; i < num_bytes; i++)
149
            out_bytes.push_back(buff[i]);
150

    
151
        return out_bytes; 
152
    }
153

    
154
    /*******************************************************************
155
     * SPI
156
     *
157
     * For non-readback transactions use the SPI_WRITE command, which is
158
     * simpler and uses the USB control buffer for OUT data. No data
159
     * needs to be returned.
160
     *
161
     * For readback transactions use SPI_TRANSACT, which places up to
162
     * 4 bytes of OUT data in the device request fields and uses the
163
     * control buffer for IN data.
164
     ******************************************************************/
165
    boost::uint32_t transact_spi(int which_slave,
166
                                 const spi_config_t &,
167
                                 boost::uint32_t bits,
168
                                 size_t num_bits,
169
                                 bool readback)
170
    {
171
        UHD_ASSERT_THROW((num_bits <= 32) && !(num_bits % 8));
172
        size_t num_bytes = num_bits / 8;
173

    
174
        // Byteswap on num_bytes
175
        unsigned char buff[4] = { 0 };
176
        for (size_t i = 1; i <= num_bytes; i++)
177
            buff[num_bytes - i] = (bits >> ((i - 1) * 8)) & 0xff;
178

    
179
        if (readback) {
180
            boost::uint8_t w_len_h = which_slave & 0xff;
181
            boost::uint8_t w_len_l = num_bytes & 0xff;
182

    
183
            int ret = _ctrl_transport->usrp_control_read(
184
                                         VRQ_SPI_TRANSACT,
185
                                         (buff[0] << 8) | (buff[1] << 0), 
186
                                         (buff[2] << 8) | (buff[3] << 0),
187
                                         buff,
188
                                         (w_len_h << 8) | (w_len_l << 0));
189

    
190
            if (ret < 0) {
191
                std::cout << "USRP: failed SPI readback transaction: "
192
                          << std::dec << ret << std::endl;
193
            }
194

    
195
            boost::uint32_t val = (((boost::uint32_t)buff[0]) <<  0) |
196
                                  (((boost::uint32_t)buff[1]) <<  8) |
197
                                  (((boost::uint32_t)buff[2]) << 16) |
198
                                  (((boost::uint32_t)buff[3]) << 24);
199
            return val; 
200
        }
201
        else {
202
            boost::uint8_t w_index_h = which_slave & 0xff;
203
            boost::uint8_t w_index_l = (SPI_FMT_MSB | SPI_FMT_HDR_0) & 0xff;
204

    
205
            int ret =_ctrl_transport->usrp_control_write(
206
                                          VRQ_SPI_WRITE,
207
                                          0x00,
208
                                          (w_index_h << 8) | (w_index_l << 0),
209
                                          buff, num_bytes);
210

    
211
            if (ret < 0) {
212
                std::cout << "USRP: failed SPI transaction: "
213
                          << std::dec << ret << std::endl;
214
            }
215

    
216
            return 0;
217
        }
218
    }
219

    
220
    /*******************************************************************
221
     * Firmware 
222
     *
223
     * This call is deprecated.
224
     ******************************************************************/
225
    void write_firmware_cmd(boost::uint8_t request,
226
                            boost::uint16_t value,
227
                            boost::uint16_t index,
228
                            unsigned char *buff,
229
                            boost::uint16_t length)
230
    {
231
        int ret;
232

    
233
        if (request & 0x80) {
234
            ret = _ctrl_transport->usrp_control_read(request,
235
                                                     value,
236
                                                     index,
237
                                                     buff,
238
                                                     length);
239
        }
240
        else {
241
            ret = _ctrl_transport->usrp_control_write(request,
242
                                                      value,
243
                                                      index,
244
                                                      buff,
245
                                                      length);
246
        }
247

    
248
        if (ret < 0)
249
            std::cerr << "USRP: failed firmware command: " << ret << std::endl;
250
    }
251

    
252
private:
253
    usrp_ctrl::sptr _ctrl_transport;
254
};
255

    
256
/***********************************************************************
257
 * Public Make Function
258
 **********************************************************************/
259
usrp1_iface::sptr usrp1_iface::make(usrp_ctrl::sptr ctrl_transport)
260
{
261
    return sptr(new usrp1_iface_impl(ctrl_transport));
262
}