Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / b100 / b100_ctrl.cpp @ 54caee78

History | View | Annotate | Download (9.69 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 "../../transport/super_recv_packet_handler.hpp"
19
#include "b100_ctrl.hpp"
20
#include "b100_impl.hpp"
21
#include <uhd/transport/usb_zero_copy.hpp>
22
#include <uhd/transport/zero_copy.hpp>
23
#include <uhd/transport/vrt_if_packet.hpp>
24
#include <uhd/utils/thread_priority.hpp>
25
#include <uhd/utils/msg.hpp>
26
#include <uhd/types/metadata.hpp>
27
#include <uhd/types/serial.hpp>
28
#include "ctrl_packet.hpp"
29
#include <boost/thread.hpp>
30
#include <uhd/exception.hpp>
31

    
32
using namespace uhd::transport;
33
using namespace uhd;
34

    
35
bool b100_ctrl_debug = false;
36

    
37
class b100_ctrl_impl : public b100_ctrl {
38
public:
39
    b100_ctrl_impl(uhd::transport::usb_zero_copy::sptr ctrl_transport) : 
40
        sync_ctrl_fifo(2),
41
        async_msg_fifo(100),
42
        _ctrl_transport(ctrl_transport),
43
        _seq(0)
44
    {
45
        boost::barrier spawn_barrier(2);
46
        viking_marauders.create_thread(boost::bind(&b100_ctrl_impl::viking_marauder_loop, this, boost::ref(spawn_barrier)));
47
        spawn_barrier.wait();
48
    }
49
    
50
    int write(boost::uint32_t addr, const ctrl_data_t &data);
51
    ctrl_data_t read(boost::uint32_t addr, size_t len);
52
    
53
    ~b100_ctrl_impl(void) {
54
        viking_marauders.interrupt_all();
55
        viking_marauders.join_all();
56
    }
57
    
58
    bool get_ctrl_data(ctrl_data_t &pkt_data, double timeout);
59
    bool recv_async_msg(uhd::async_metadata_t &async_metadata, double timeout);
60
    
61
private:
62
    int send_pkt(boost::uint16_t *cmd);
63
    
64
    //änd hërë wë gö ä-Vïkïng för äsynchronous control packets
65
    void viking_marauder_loop(boost::barrier &);
66
    bounded_buffer<ctrl_data_t> sync_ctrl_fifo;
67
    bounded_buffer<async_metadata_t> async_msg_fifo;
68
    boost::thread_group viking_marauders;
69
    
70
    uhd::transport::usb_zero_copy::sptr _ctrl_transport;
71
    boost::uint8_t _seq;
72
};
73

    
74
/***********************************************************************
75
 * helper functions for packing/unpacking control packets
76
 **********************************************************************/
77
void pack_ctrl_pkt(boost::uint16_t *pkt_buff,
78
                          const ctrl_pkt_t &pkt){
79
    //first two bits are OP
80
    //next six bits are CALLBACKS
81
    //next 8 bits are SEQUENCE
82
    //next 16 bits are LENGTH (16-bit word)
83
    //next 32 bits are ADDRESS (16-bit word LSW)
84
    //then DATA (28 16-bit words)
85
    pkt_buff[0] = (boost::uint16_t(pkt.pkt_meta.op) << 14) | (boost::uint16_t(pkt.pkt_meta.callbacks) << 8) | pkt.pkt_meta.seq;
86
    pkt_buff[1] = pkt.pkt_meta.len;
87
    pkt_buff[2] = (pkt.pkt_meta.addr & 0x00000FFF);
88
    pkt_buff[3] = 0x0000; //address high bits always 0 on this device
89
    
90
    for(size_t i = 0; i < pkt.data.size(); i++) {
91
        pkt_buff[4+i] = pkt.data[i];
92
    }
93
}
94

    
95
void unpack_ctrl_pkt(const boost::uint16_t *pkt_buff,
96
                            ctrl_pkt_t &pkt){
97
    pkt.pkt_meta.seq = pkt_buff[0] & 0xFF;
98
    pkt.pkt_meta.op = CTRL_PKT_OP_READ; //really this is useless
99
    pkt.pkt_meta.len = pkt_buff[1];
100
    pkt.pkt_meta.callbacks = 0; //callbacks aren't implemented yet
101
    pkt.pkt_meta.addr = pkt_buff[2] | boost::uint32_t(pkt_buff[3] << 16);
102
    
103
    //let's check this so we don't go pushing 64K of crap onto the pkt
104
    if(pkt.pkt_meta.len > CTRL_PACKET_DATA_LENGTH) {
105
        throw uhd::runtime_error("Received control packet too long");
106
    }
107

    
108
    for(int i = 4; i < 4+pkt.pkt_meta.len; i++) pkt.data.push_back(pkt_buff[i]);
109
}
110

    
111
int b100_ctrl_impl::send_pkt(boost::uint16_t *cmd) {
112
    managed_send_buffer::sptr sbuf = _ctrl_transport->get_send_buff();
113
    if(!sbuf.get()) {
114
        throw uhd::runtime_error("Control channel send error");
115
    }
116
        
117
    //FIXME there's a better way to do this
118
    for(size_t i = 0; i < (CTRL_PACKET_LENGTH / sizeof(boost::uint16_t)); i++) {
119
        sbuf->cast<boost::uint16_t *>()[i] = cmd[i];
120
    }
121
    sbuf->commit(CTRL_PACKET_LENGTH); //fixed size transaction
122
    return 0;
123
}
124

    
125
int b100_ctrl_impl::write(boost::uint32_t addr, const ctrl_data_t &data) {
126
    UHD_ASSERT_THROW(data.size() <= (CTRL_PACKET_DATA_LENGTH / sizeof(boost::uint16_t)));
127
    ctrl_pkt_t pkt;
128
    pkt.data = data;
129
    pkt.pkt_meta.op = CTRL_PKT_OP_WRITE;
130
    pkt.pkt_meta.callbacks = 0;
131
    pkt.pkt_meta.seq = _seq++;
132
    pkt.pkt_meta.len = pkt.data.size();
133
    pkt.pkt_meta.addr = addr;
134
    boost::uint16_t pkt_buff[CTRL_PACKET_LENGTH / sizeof(boost::uint16_t)];
135
        
136
    pack_ctrl_pkt(pkt_buff, pkt);
137
    size_t result = send_pkt(pkt_buff);
138
    return result;
139
}
140

    
141
ctrl_data_t b100_ctrl_impl::read(boost::uint32_t addr, size_t len) {
142
    UHD_ASSERT_THROW(len <= (CTRL_PACKET_DATA_LENGTH / sizeof(boost::uint16_t)));
143

    
144
    ctrl_pkt_t pkt;
145
    pkt.pkt_meta.op = CTRL_PKT_OP_READ;
146
    pkt.pkt_meta.callbacks = 0;
147
    pkt.pkt_meta.seq = _seq++;
148
    pkt.pkt_meta.len = len;
149
    pkt.pkt_meta.addr = addr;
150
    boost::uint16_t pkt_buff[CTRL_PACKET_LENGTH / sizeof(boost::uint16_t)];
151

    
152
    pack_ctrl_pkt(pkt_buff, pkt);
153
    send_pkt(pkt_buff);
154
    
155
    //loop around waiting for the response to appear
156
    while(!get_ctrl_data(pkt.data, 0.05));
157

    
158
    return pkt.data;
159
}
160

    
161
/***********************************************************************
162
 * Viking marauders go pillaging for asynchronous control packets in the
163
 * control response endpoint. Sync packets go in sync_ctrl_fifo,
164
 * async TX error messages go in async_msg_fifo. sync_ctrl_fifo should
165
 * never have more than 1 message in it, since it's expected that we'll
166
 * wait for a control operation to finish before starting another one.
167
 **********************************************************************/
168
void b100_ctrl_impl::viking_marauder_loop(boost::barrier &spawn_barrier) {
169
    spawn_barrier.wait();
170
    set_thread_priority_safe();
171
    
172
    while (not boost::this_thread::interruption_requested()){
173
        managed_recv_buffer::sptr rbuf = _ctrl_transport->get_recv_buff();
174
        if(!rbuf.get()) continue; //that's ok, there are plenty of villages to pillage!
175
        const boost::uint16_t *pkt_buf = rbuf->cast<const boost::uint16_t *>();
176
        
177
        if(pkt_buf[0] >> 8 == CTRL_PACKET_HEADER_MAGIC) {
178
            //so it's got a control packet header, let's parse it.
179
            ctrl_pkt_t pkt;
180
            unpack_ctrl_pkt(pkt_buf, pkt);
181
        
182
            if(pkt.pkt_meta.seq != boost::uint8_t(_seq - 1)) {
183
                throw uhd::runtime_error("Sequence error on control channel");
184
            }
185
            if(pkt.pkt_meta.len > (CTRL_PACKET_LENGTH - CTRL_PACKET_HEADER_LENGTH)) {
186
                throw uhd::runtime_error("Control channel packet length too long");
187
            }
188
        
189
            //push it onto the queue
190
            sync_ctrl_fifo.push_with_wait(pkt.data);
191
        } else { //it's an async status pkt
192
            //extract the vrt header packet info
193
            vrt::if_packet_info_t if_packet_info;
194
            if_packet_info.num_packet_words32 = rbuf->size()/sizeof(boost::uint32_t);
195
            const boost::uint32_t *vrt_hdr = rbuf->cast<const boost::uint32_t *>();
196
            vrt::if_hdr_unpack_le(vrt_hdr, if_packet_info);
197
            
198
            if(    if_packet_info.sid == 0 
199
               and if_packet_info.packet_type != vrt::if_packet_info_t::PACKET_TYPE_DATA){
200
                //fill in the async metadata
201
                async_metadata_t metadata;
202
                metadata.channel = 0;
203
                metadata.has_time_spec = if_packet_info.has_tsi and if_packet_info.has_tsf;
204
                metadata.time_spec = time_spec_t(
205
                    time_t(if_packet_info.tsi), size_t(if_packet_info.tsf), 64e6 //FIXME get from clock_ctrl
206
                );
207
                metadata.event_code = async_metadata_t::event_code_t(sph::get_context_code(vrt_hdr, if_packet_info));
208
                //print the famous U, and push the metadata into the message queue
209
                if (metadata.event_code & 
210
                    ( async_metadata_t::EVENT_CODE_UNDERFLOW 
211
                    | async_metadata_t::EVENT_CODE_UNDERFLOW_IN_PACKET) )
212
                    UHD_MSG(fastpath) << "U";
213
                    
214
                if (metadata.event_code & 
215
                    ( async_metadata_t::EVENT_CODE_SEQ_ERROR
216
                    | async_metadata_t::EVENT_CODE_SEQ_ERROR_IN_BURST) )
217
                    UHD_MSG(fastpath) << "S";
218
                    
219
                async_msg_fifo.push_with_pop_on_full(metadata);
220
                continue;
221
            }
222
            throw uhd::runtime_error("Control: unknown async response");
223
        }
224
    }
225
}
226

    
227
bool b100_ctrl_impl::get_ctrl_data(ctrl_data_t &pkt_data, double timeout){
228
    boost::this_thread::disable_interruption di; //disable because the wait can throw
229
    return sync_ctrl_fifo.pop_with_timed_wait(pkt_data, timeout);
230
}
231

    
232
bool b100_ctrl_impl::recv_async_msg(uhd::async_metadata_t &async_metadata, double timeout) {
233
    boost::this_thread::disable_interruption di; //disable because the wait can throw
234
    return async_msg_fifo.pop_with_timed_wait(async_metadata, timeout);
235
}
236

    
237
/***********************************************************************
238
 * Public make function for b100_ctrl interface
239
 **********************************************************************/
240
b100_ctrl::sptr b100_ctrl::make(uhd::transport::usb_zero_copy::sptr ctrl_transport){
241
    return sptr(new b100_ctrl_impl(ctrl_transport));
242
}