aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/usrp/usrp1/io_impl.cpp
blob: 3f3e74b80879056639d9141326869218c312f8ac (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
//
// 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;

/*
 * The FX2 firmware bursts data to the FPGA in 512 byte chunks so
 * maintain send state to make sure that happens.
 */
struct usrp1_send_state {
    uhd::transport::managed_send_buffer::sptr send_buff;
    size_t bytes_used;
    size_t bytes_free;
};

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

    bool get_recv_buff(managed_recv_buffer::sptr buff); 

    //state management for the vrt packet handler code
    vrt_packet_handler::recv_state packet_handler_recv_state;
    usrp1_send_state send_state;

    zero_copy_if::sptr data_transport;
    unsigned int count;
};

usrp1_impl::io_impl::io_impl(zero_copy_if::sptr zc_if)
 : packet_handler_recv_state(1), data_transport(zc_if), count(0)
{
    /* NOP */
}

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_transport));
}

/***********************************************************************
 * Data Send
 **********************************************************************/
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->send_state.send_buff == NULL) {
            _io_impl->send_state.send_buff = _data_transport->get_send_buff();
            if (_io_impl->send_state.send_buff == NULL) {
                return 0;
            }
            _io_impl->send_state.bytes_used = 0;
            _io_impl->send_state.bytes_free = _io_impl->send_state.send_buff->size();
        }

        size_t copy_samps =
               std::min(num_samps - total_samps_sent, _io_impl->send_state.bytes_free / _tx_otw_type.get_sample_size());

        const boost::uint8_t *io_mem =
                             reinterpret_cast<const boost::uint8_t *>(buffs[0]);

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

        // Type conversion and copy 
        convert_io_type_to_otw_type(
                     io_mem + total_samps_sent * io_type.size,
                     io_type,
                     otw_mem + _io_impl->send_state.bytes_used,
                     _tx_otw_type,
                     copy_samps);
 
        _io_impl->send_state.bytes_used += copy_samps * _tx_otw_type.get_sample_size();
        _io_impl->send_state.bytes_free -= copy_samps * _tx_otw_type.get_sample_size();

        if (_io_impl->send_state.bytes_free == 0) {
            _io_impl->send_state.send_buff->commit(_io_impl->send_state.bytes_used);
            _io_impl->send_state.send_buff = uhd::transport::managed_send_buffer::sptr();
        }

        total_samps_sent += copy_samps; 
 
        //check for underruns
        if (!(_io_impl->count++ % 1000)) {
            unsigned char underrun;
            int ret = _ctrl_transport->usrp_control_read(VRQ_GET_STATUS,
                                                         0,
                                                         GS_TX_UNDERRUN,
                                                         &underrun, sizeof(char));
            if (ret < 0)
                std::cerr << "error: underrun check failed" << std::endl;
            if (underrun)
                std::cerr << "U" << std::endl;
        }
    }

    return total_samps_sent;
}

/***********************************************************************
 * Data Recv
 **********************************************************************/
void _recv_helper(vrt_packet_handler::recv_state &state)
{
    size_t num_packet_words32 =
                       state.managed_buffs[0]->size() / sizeof(boost::uint32_t);

    const boost::uint32_t *data =
                        state.managed_buffs[0]->cast<const boost::uint32_t *>();

    state.copy_buffs[0] = reinterpret_cast<const boost::uint8_t *>(data);
    size_t num_payload_bytes = num_packet_words32 * sizeof(boost::uint32_t);
    state.size_of_copy_buffs = num_payload_bytes;
}

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(_io_impl->packet_handler_recv_state.width == 1);
    UHD_ASSERT_THROW(buffs.size() == 1);

    size_t sent_samps = 0;
    size_t nsamps_to_copy = 0;; 

    while (sent_samps < num_samps) {
        if (_io_impl->packet_handler_recv_state.size_of_copy_buffs == 0) {
            _io_impl->packet_handler_recv_state.fragment_offset_in_samps = 0;
            _io_impl->packet_handler_recv_state.managed_buffs[0] =
                                          _io_impl->data_transport->get_recv_buff();
 
            //timeout or something bad returns zero
            if (!_io_impl->packet_handler_recv_state.managed_buffs[0].get())
                return 0;
 
            _recv_helper(_io_impl->packet_handler_recv_state);
        }

        size_t bytes_per_item = _rx_otw_type.get_sample_size();
        size_t nsamps_available =
            _io_impl->packet_handler_recv_state.size_of_copy_buffs / bytes_per_item;
        nsamps_to_copy = std::min(num_samps, nsamps_available);
        size_t bytes_to_copy = nsamps_to_copy * bytes_per_item;

        convert_otw_type_to_io_type(
              _io_impl->packet_handler_recv_state.copy_buffs[0],
              _rx_otw_type,
              reinterpret_cast<boost::uint8_t *>(buffs[0]) + sent_samps * io_type.size,
              io_type,
              nsamps_to_copy);
 
        _io_impl->packet_handler_recv_state.copy_buffs[0] += bytes_to_copy; 
        _io_impl->packet_handler_recv_state.size_of_copy_buffs -= bytes_to_copy;

        sent_samps += nsamps_to_copy;

        //check for overruns
        if (!(_io_impl->count++ % 10000)) {
            unsigned char overrun;
            int ret = _ctrl_transport->usrp_control_read(
                                   VRQ_GET_STATUS,
                                   0,
                                   GS_RX_OVERRUN,
                                   &overrun, sizeof(char));
            if (ret < 0)
                std::cerr << "error: overrun check failed" << std::endl;
            if (overrun)
                std::cerr << "O" << std::endl;
        }
    }
    return sent_samps; 
}