Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / usrp1_iface.cpp @ 8c5768ae

History | View | Annotate | Download (9.5 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
        if (ret < 0)
126
            std::cerr << "USRP: failed i2c write: " << ret << std::endl;
127
    }
128

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

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

    
140
        if ((ret < 0) || (unsigned)ret < (num_bytes)) {
141
            std::cerr << "USRP: failed i2c read: " << ret << std::endl;
142
            return byte_vector_t(num_bytes, 0xff); 
143
        }
144

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

    
149
        return out_bytes; 
150
    }
151

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

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

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

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

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

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

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

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

    
214
            return 0;
215
        }
216
    }
217

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

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

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

    
250
private:
251
    usrp_ctrl::sptr _ctrl_transport;
252
};
253

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