aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/usrp/e100/e100_ctrl.cpp
blob: eb529c9c145bd69e99870298ea756c329d42c18d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//
// Copyright 2011 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include "e100_ctrl.hpp"
#include "e100_regs.hpp"
#include <uhd/exception.hpp>
#include <uhd/utils/log.hpp>
#include <uhd/utils/msg.hpp>
#include <sys/ioctl.h> //ioctl
#include <fcntl.h> //open, close
#include <linux/usrp_e.h> //ioctl structures and constants
#include <boost/thread/thread.hpp> //sleep
#include <boost/thread/mutex.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <fstream>

using namespace uhd;

/***********************************************************************
 * Sysfs GPIO wrapper class
 **********************************************************************/
class gpio{
public:
    gpio(const int num, const std::string &dir) : _num(num){
        this->set_xport("export");
        this->set_dir(dir);
        _value_file.open(str(boost::format("/sys/class/gpio/gpio%d/value") % num).c_str(), std::ios_base::in | std::ios_base::out);
    }
    ~gpio(void){
        _value_file.close();
        this->set_dir("in");
        this->set_xport("unexport");
    }
    void operator()(const int val){
        _value_file << val << std::endl << std::flush;
    }
    int operator()(void){
        std::string val;
        std::getline(_value_file, val);
        _value_file.seekg(0);
        return int(val.at(0) - '0') & 0x1;
    }
private:
    void set_xport(const std::string &xport){
        std::ofstream export_file(("/sys/class/gpio/" + xport).c_str());
        export_file << _num << std::endl << std::flush;
        export_file.close();
    }
    void set_dir(const std::string &dir){
        std::ofstream dir_file(str(boost::format("/sys/class/gpio/gpio%d/direction") % _num).c_str());
        dir_file << dir << std::endl << std::flush;
        dir_file.close();
    }
    const int _num;
    std::fstream _value_file;
};

/***********************************************************************
 * Aux spi implementation
 **********************************************************************/
class aux_spi_iface_impl : public spi_iface{
public:
    aux_spi_iface_impl(void):
        spi_sclk_gpio(65, "out"),
        spi_sen_gpio(186, "out"),
        spi_mosi_gpio(145, "out"),
        spi_miso_gpio(147, "in"){}

    boost::uint32_t transact_spi(
        int, const spi_config_t &, //not used params
        boost::uint32_t bits,
        size_t num_bits,
        bool readback
    ){
        boost::uint32_t rb_bits = 0;
        this->spi_sen_gpio(0);

        for (size_t i = 0; i < num_bits; i++){
            this->spi_sclk_gpio(0);
            this->spi_mosi_gpio((bits >> (num_bits-i-1)) & 0x1);
            boost::this_thread::sleep(boost::posix_time::microseconds(10));
            if (readback) rb_bits = (rb_bits << 1) | this->spi_miso_gpio();
            this->spi_sclk_gpio(1);
            boost::this_thread::sleep(boost::posix_time::microseconds(10));
        }

        this->spi_sen_gpio(1);
        boost::this_thread::sleep(boost::posix_time::microseconds(100));
        return rb_bits;
    }

private:
    gpio spi_sclk_gpio, spi_sen_gpio, spi_mosi_gpio, spi_miso_gpio;
};

uhd::spi_iface::sptr e100_ctrl::make_aux_spi_iface(void){
    return uhd::spi_iface::sptr(new aux_spi_iface_impl());
}

/***********************************************************************
 * I2C device node implementation wrapper
 **********************************************************************/
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
class i2c_dev_iface : public i2c_iface{
public:
    i2c_dev_iface(const std::string &node){
        if ((_node_fd = ::open(node.c_str(), O_RDWR)) < 0){
            throw uhd::io_error("Failed to open " + node);
        }
    }

    ~i2c_dev_iface(void){
        ::close(_node_fd);
    }

    void write_i2c(boost::uint8_t addr, const byte_vector_t &bytes){
        byte_vector_t rw_bytes(bytes);

        //setup the message
        i2c_msg msg;
        msg.addr = addr;
        msg.flags = 0;
        msg.len = bytes.size();
        msg.buf = &rw_bytes.front();

        //setup the data
        i2c_rdwr_ioctl_data data;
        data.msgs = &msg;
        data.nmsgs = 1;

        //call the ioctl
        UHD_ASSERT_THROW(::ioctl(_node_fd, I2C_RDWR, &data) >= 0);
    }

    byte_vector_t read_i2c(boost::uint8_t addr, size_t num_bytes){
        byte_vector_t bytes(num_bytes);

        //setup the message
        i2c_msg msg;
        msg.addr = addr;
        msg.flags = I2C_M_RD;
        msg.len = bytes.size();
        msg.buf = &bytes.front();

        //setup the data
        i2c_rdwr_ioctl_data data;
        data.msgs = &msg;
        data.nmsgs = 1;

        //call the ioctl
        UHD_ASSERT_THROW(::ioctl(_node_fd, I2C_RDWR, &data) >= 0);

        return bytes;
    }

private: int _node_fd;
};

uhd::i2c_iface::sptr e100_ctrl::make_dev_i2c_iface(const std::string &node){
    return uhd::i2c_iface::sptr(new i2c_dev_iface(node));
}

/***********************************************************************
 * UART control implementation
 **********************************************************************/
#include <termios.h>
#include <cstring>
class uart_dev_iface : public uart_iface{
public:
    uart_dev_iface(const std::string &node){
        if ((_node_fd = ::open(node.c_str(), O_RDWR | O_NONBLOCK)) < 0){
            throw uhd::io_error("Failed to open " + node);
        }

        //init the tty settings w/ termios
        termios tio;
        std::memset(&tio,0,sizeof(tio));
        tio.c_iflag = IGNCR; //Ignore CR
        tio.c_oflag = OPOST | ONLCR; //Map NL to CR-NL on output
        tio.c_cflag = CS8 | CREAD | CLOCAL; // 8n1
        tio.c_lflag = 0;

        cfsetospeed(&tio, B115200);            // 115200 baud
        cfsetispeed(&tio, B115200);            // 115200 baud

        tcsetattr(_node_fd, TCSANOW, &tio);
    }

    void write_uart(const std::string &buf){
        const ssize_t ret = ::write(_node_fd, buf.c_str(), buf.size());
        if (size_t(ret) != buf.size()) UHD_LOG << ret;
    }

    std::string read_uart(double timeout){
        const boost::system_time exit_time = boost::get_system_time() + boost::posix_time::milliseconds(long(timeout*1000));

        std::string line;
        while(true){
            char ch;
            const ssize_t ret = ::read(_node_fd, &ch, 1);

            //got a character -> process it
            if (ret == 1){
                const bool flush  = ch == '\n' or ch == '\r';
                if (flush and line.empty()) continue; //avoid flushing on empty lines
                line += std::string(1, ch);
                if (flush) break;
            }

            //didnt get a character, check the timeout
            else if (boost::get_system_time() > exit_time){
                break;
            }

            //otherwise sleep for a bit
            else{
                boost::this_thread::sleep(boost::posix_time::milliseconds(10));
            }
        }
        return line;
    }

private: int _node_fd;
};

uhd::uart_iface::sptr e100_ctrl::make_gps_uart_iface(const std::string &node){
    return uhd::uart_iface::sptr(new uart_dev_iface(node));
}

/***********************************************************************
 * USRP-E100 control implementation
 **********************************************************************/
class e100_ctrl_impl : public e100_ctrl{
public:

    int get_file_descriptor(void){
        return _node_fd;
    }

    /*******************************************************************
     * Structors
     ******************************************************************/
    e100_ctrl_impl(const std::string &node){
        UHD_MSG(status) << "Opening device node " << node << "..." << std::endl;

        //open the device node and check file descriptor
        if ((_node_fd = ::open(node.c_str(), O_RDWR)) < 0){
            throw uhd::io_error("Failed to open " + node);
        }

        //check the module compatibility number
        int module_compat_num = ::ioctl(_node_fd, USRP_E_GET_COMPAT_NUMBER, NULL);
        if (module_compat_num != USRP_E_COMPAT_NUMBER){
            throw uhd::runtime_error(str(boost::format(
                "Expected module compatibility number 0x%x, but got 0x%x:\n"
                "The module build is not compatible with the host code build."
            ) % USRP_E_COMPAT_NUMBER % module_compat_num));
        }

        //perform a global reset after opening
        this->poke32(E100_REG_GLOBAL_RESET, 0);
    }

    ~e100_ctrl_impl(void){
        ::close(_node_fd);
    }

    /*******************************************************************
     * IOCTL: provides the communication base for all other calls
     ******************************************************************/
    void ioctl(int request, void *mem){
        boost::mutex::scoped_lock lock(_ioctl_mutex);

        if (::ioctl(_node_fd, request, mem) < 0){
            throw uhd::os_error(str(
                boost::format("ioctl failed with request %d") % request
            ));
        }
    }
    /*******************************************************************
     * Peek and Poke
     ******************************************************************/
    void poke32(wb_addr_type addr, boost::uint32_t value){
        //load the data struct
        usrp_e_ctl32 data;
        data.offset = addr;
        data.count = 1;
        data.buf[0] = value;

        //call the ioctl
        this->ioctl(USRP_E_WRITE_CTL32, &data);
    }

    void poke16(wb_addr_type addr, boost::uint16_t value){
        //load the data struct
        usrp_e_ctl16 data;
        data.offset = addr;
        data.count = 1;
        data.buf[0] = value;

        //call the ioctl
        this->ioctl(USRP_E_WRITE_CTL16, &data);
    }

    boost::uint32_t peek32(wb_addr_type addr){
        //load the data struct
        usrp_e_ctl32 data;
        data.offset = addr;
        data.count = 1;

        //call the ioctl
        this->ioctl(USRP_E_READ_CTL32, &data);

        return data.buf[0];
    }

    boost::uint16_t peek16(wb_addr_type addr){
        //load the data struct
        usrp_e_ctl16 data;
        data.offset = addr;
        data.count = 1;

        //call the ioctl
        this->ioctl(USRP_E_READ_CTL16, &data);

        return data.buf[0];
    }

private:
    int _node_fd;
    boost::mutex _ioctl_mutex;
};

/***********************************************************************
 * Public Make Function
 **********************************************************************/
e100_ctrl::sptr e100_ctrl::make(const std::string &node){
    return sptr(new e100_ctrl_impl(node));
}