aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/usrp/usrp1/io_impl.cpp
blob: 5e206b3d5635d6c30be6a5d52b208fecd0ef18d1 (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
//
// Copyright 2010 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 "../../transport/vrt_packet_handler.hpp"
#include "usrp_commands.h"
#include "usrp1_impl.hpp"
#include <uhd/utils/thread_priority.hpp>
#include <uhd/transport/convert_types.hpp>
#include <uhd/transport/bounded_buffer.hpp>
#include <boost/bind.hpp>
#include <boost/format.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <iostream>

using namespace uhd;
using namespace uhd::usrp;
using namespace uhd::transport;
namespace asio = boost::asio;

struct usrp1_send_state {
    uhd::transport::managed_send_buffer::sptr send_buff;
    size_t bytes_written;
    size_t underrun_poll_samp_count;

    size_t bytes_free()
    {
        if (send_buff != NULL)
            return send_buff->size() - bytes_written;
        else
            return 0;
    }
};

struct usrp1_recv_state {
    uhd::transport::managed_recv_buffer::sptr recv_buff;
    size_t bytes_read;
    size_t overrun_poll_samp_count;

    size_t bytes_avail()
    {
        if (recv_buff != NULL)
            return recv_buff->size() - bytes_read;
        else
            return 0;
    }
};

/***********************************************************************
 * IO Implementation Details
 **********************************************************************/
struct usrp1_impl::io_impl {
    io_impl();
    ~io_impl(void);

    //state handling for buffer management 
    usrp1_recv_state recv_state;
    usrp1_send_state send_state;

    //send transport management 
    bool get_send_buffer(zero_copy_if::sptr zc_if);
    size_t copy_convert_send_samps(const void *buff, size_t num_samps,
                              size_t sample_offset, const io_type_t io_type,
                              otw_type_t otw_type);
    bool conditional_buff_commit(bool force);
    bool check_underrun(usrp_ctrl::sptr ctrl_if,
                        size_t poll_interval, bool force);

    //recv transport management 
    bool get_recv_buffer(zero_copy_if::sptr zc_if);
    size_t copy_convert_recv_samps(void *buff, size_t num_samps,
                              size_t sample_offset, const io_type_t io_type,
                              otw_type_t otw_type);
    bool check_overrun(usrp_ctrl::sptr ctrl_if,
                        size_t poll_interval, bool force);
};

usrp1_impl::io_impl::io_impl()
{
    send_state.send_buff = uhd::transport::managed_send_buffer::sptr();
    recv_state.recv_buff = uhd::transport::managed_recv_buffer::sptr();
}

usrp1_impl::io_impl::~io_impl(void)
{
   /* NOP */
}

void usrp1_impl::io_init(void)
{
    _rx_otw_type.width = 16;
    _rx_otw_type.shift = 0;
    _rx_otw_type.byteorder = otw_type_t::BO_LITTLE_ENDIAN;

    _tx_otw_type.width = 16;
    _tx_otw_type.shift = 0;
    _tx_otw_type.byteorder = otw_type_t::BO_LITTLE_ENDIAN;

    _io_impl = UHD_PIMPL_MAKE(io_impl, ());
}

/***********************************************************************
 * Data Send
 **********************************************************************/
bool usrp1_impl::io_impl::get_send_buffer(zero_copy_if::sptr zc_if)
{
    if (send_state.send_buff == NULL) {

        send_state.send_buff = zc_if->get_send_buff();
        if (send_state.send_buff == NULL)
            return false;

        send_state.bytes_written = 0;
    }

    return true;
}

size_t usrp1_impl::io_impl::copy_convert_send_samps(const void *buff,
                                                    size_t num_samps,
                                                    size_t sample_offset,
                                                    const io_type_t io_type,
                                                    otw_type_t otw_type)
{
    UHD_ASSERT_THROW(send_state.bytes_free() % otw_type.get_sample_size() == 0);

    size_t samps_free = send_state.bytes_free() / otw_type.get_sample_size();
    size_t copy_samps = std::min(num_samps - sample_offset, samps_free); 

    const boost::uint8_t *io_mem =
        reinterpret_cast<const boost::uint8_t *>(buff);

    boost::uint8_t *otw_mem = send_state.send_buff->cast<boost::uint8_t *>();

    convert_io_type_to_otw_type(io_mem + sample_offset * io_type.size,
                                io_type,
                                otw_mem + send_state.bytes_written,
                                otw_type,
                                copy_samps);

    send_state.bytes_written += copy_samps * otw_type.get_sample_size();
    send_state.underrun_poll_samp_count += copy_samps;

    return copy_samps;
}

bool usrp1_impl::io_impl::conditional_buff_commit(bool force)
{
    if (send_state.bytes_written % 512)
        return false;

    if (force || send_state.bytes_free() == 0) {
        send_state.send_buff->commit(send_state.bytes_written);
        send_state.send_buff = uhd::transport::managed_send_buffer::sptr();
        return true;
    }
    
    return false;
}

bool usrp1_impl::io_impl::check_underrun(usrp_ctrl::sptr ctrl_if,
                                         size_t poll_interval,
                                         bool force)
{
    unsigned char underrun = 0;

    bool ready_to_poll = send_state.underrun_poll_samp_count > poll_interval;

    if (force || ready_to_poll) {
        int ret = ctrl_if->usrp_control_read(VRQ_GET_STATUS,
                                             0,
                                             GS_TX_UNDERRUN,
                                             &underrun, sizeof(char));
        if (ret < 0)
            std::cerr << "USRP: underrun check failed" << std::endl;
        if (underrun)
            std::cerr << "U" << std::endl;

        send_state.underrun_poll_samp_count = 0;
    }

    return (bool) underrun;
}

size_t usrp1_impl::send(const std::vector<const void *> &buffs,
                        size_t num_samps,
                        const tx_metadata_t &,
                        const io_type_t &io_type,
                        send_mode_t)
{
    UHD_ASSERT_THROW(buffs.size() == 1);

    size_t total_samps_sent = 0;

    while (total_samps_sent < num_samps) {
        if (!_io_impl->get_send_buffer(_data_transport))
            return 0;

        total_samps_sent += _io_impl->copy_convert_send_samps(buffs[0],
                                                              num_samps,
                                                              total_samps_sent,
                                                              io_type,
                                                              _tx_otw_type);
        if (total_samps_sent == num_samps)
            _io_impl->conditional_buff_commit(true);
        else
            _io_impl->conditional_buff_commit(false);

        _io_impl->check_underrun(_ctrl_transport,
                                 _tx_samps_per_poll_interval, false);
    }

    return total_samps_sent;
}

/***********************************************************************
 * Data Recv
 **********************************************************************/
bool usrp1_impl::io_impl::get_recv_buffer(zero_copy_if::sptr zc_if)
{
    if ((recv_state.recv_buff == NULL) || (recv_state.bytes_avail() == 0)) {

        recv_state.recv_buff = zc_if->get_recv_buff();
        if (recv_state.recv_buff == NULL)
            return false;

        recv_state.bytes_read = 0;
    }

    return true;
}

size_t usrp1_impl::io_impl::copy_convert_recv_samps(void *buff,
                                                    size_t num_samps,
                                                    size_t sample_offset,
                                                    const io_type_t io_type,
                                                    otw_type_t otw_type)
{
    UHD_ASSERT_THROW(recv_state.bytes_avail() % otw_type.get_sample_size() == 0);

    size_t samps_avail = recv_state.bytes_avail() / otw_type.get_sample_size();
    size_t copy_samps = std::min(num_samps - sample_offset, samps_avail); 

    const boost::uint8_t *otw_mem =
        recv_state.recv_buff->cast<const boost::uint8_t *>();

    boost::uint8_t *io_mem = reinterpret_cast<boost::uint8_t *>(buff);

    convert_otw_type_to_io_type(otw_mem + recv_state.bytes_read,
                                otw_type,
                                io_mem + sample_offset * io_type.size,
                                io_type,
                                copy_samps);

    recv_state.bytes_read += copy_samps * otw_type.get_sample_size();
    recv_state.overrun_poll_samp_count += copy_samps;

    return copy_samps;
}

bool usrp1_impl::io_impl::check_overrun(usrp_ctrl::sptr ctrl_if,
                                        size_t poll_interval,
                                        bool force)
{
    unsigned char overrun = 0;

    bool ready_to_poll = recv_state.overrun_poll_samp_count > poll_interval;

    if (force || ready_to_poll) {
        int ret = ctrl_if->usrp_control_read(VRQ_GET_STATUS,
                                             0,
                                             GS_RX_OVERRUN,
                                             &overrun, sizeof(char));
        if (ret < 0)
            std::cerr << "USRP: overrrun check failed" << std::endl;
        if (overrun)
            std::cerr << "O" << std::endl;

        recv_state.overrun_poll_samp_count = 0;
    }

    return (bool) overrun;
}

size_t usrp1_impl::recv(const std::vector<void *> &buffs,
                        size_t num_samps,
                        rx_metadata_t &,
                        const io_type_t &io_type,
                        recv_mode_t,
                        size_t)
{
    UHD_ASSERT_THROW(buffs.size() == 1);

    size_t total_samps_recv = 0;

    while (total_samps_recv < num_samps) {

        if (!_io_impl->get_recv_buffer(_data_transport))
            return 0;

        total_samps_recv += _io_impl->copy_convert_recv_samps(buffs[0],
                                                              num_samps,
                                                              total_samps_recv,
                                                              io_type,
                                                              _rx_otw_type);
        _io_impl->check_overrun(_ctrl_transport,
                                _rx_samps_per_poll_interval, false);
    }

    return total_samps_recv; 
}