Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / usrp1 / usrp1_iface.cpp @ 54debe30

History | View | Annotate | Download (9.4 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(" << std::dec << addr << ", 0x";
57
            std::cout << std::hex << std::setw(8) << value << ")" << std::endl;
58
        }
59

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

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

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

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

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

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

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

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

    
97
        return byteswap(value_out);
98
    }
99

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

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

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

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

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

    
124
        if (ret < 0)
125
            std::cerr << "USRP: failed i2c write: " << ret << std::endl;
126
    }
127

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

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

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

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

    
148
        return out_bytes; 
149
    }
150

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

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

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

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

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

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

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

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

    
213
            return 0;
214
        }
215
    }
216

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

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

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

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

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