3 * Copyright 2007 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <usrp_usb_interface.h>
31 #include <mb_class_registry.h>
32 #include <usrp_inband_usb_packet.h>
33 #include <fpga_regs_common.h>
35 #include <usrp_rx_stub.h>
37 #include "usrp_standard.h"
40 typedef usrp_inband_usb_packet transport_pkt;
42 #include <symbols_usrp_interface_cs.h>
43 #include <symbols_usrp_tx_cs.h>
44 #include <symbols_usrp_rx_cs.h>
45 static pmt_t s_shutdown = pmt_intern("%shutdown");
47 static const bool verbose = false;
51 * \brief Initializes the USB interface m-block.
53 * The \p user_arg should be a PMT dictionary which can contain optional
54 * arguments for the block, such as the decimatoin and interpolation rate.
56 usrp_usb_interface::usrp_usb_interface(mb_runtime *rt, const std::string &instance_name, pmt_t user_arg)
57 : mb_mblock(rt, instance_name, user_arg),
63 d_rbf("inband_tx_rx.rbf")
65 // Dictionary for arguments to all of the components
66 pmt_t usrp_dict = user_arg;
68 // Default TX/RX interface
69 std::string tx_interface = "usrp_tx";
70 std::string rx_interface = "usrp_rx";
72 if (pmt_is_dict(usrp_dict)) {
74 // The 'fake-usrp' key enables the TX and RX stubs if PMT_T
75 if(pmt_t fake_usrp = pmt_dict_ref(usrp_dict,
76 pmt_intern("fake-usrp"),
78 if(pmt_eqv(fake_usrp, PMT_T)) {
79 tx_interface = "usrp_tx_stub";
80 rx_interface = "usrp_rx_stub";
85 // Read the TX interpolations
86 if(pmt_t interp_tx = pmt_dict_ref(usrp_dict,
87 pmt_intern("interp-tx"),
89 if(!pmt_eqv(interp_tx, PMT_NIL))
90 d_interp_tx = pmt_to_long(interp_tx);
93 // Read the RX decimation rate
94 if(pmt_t decim_rx = pmt_dict_ref(usrp_dict,
95 pmt_intern("decim-rx"),
97 if(!pmt_eqv(decim_rx, PMT_NIL))
98 d_decim_rx = pmt_to_long(decim_rx);
102 if(pmt_t rbf = pmt_dict_ref(usrp_dict,
105 if(!pmt_eqv(rbf, PMT_NIL))
106 d_rbf = pmt_symbol_to_string(rbf);
109 // The RF center frequency
110 if(pmt_t rf_freq = pmt_dict_ref(usrp_dict,
111 pmt_intern("rf-freq"),
113 if(!pmt_eqv(rf_freq, PMT_NIL))
114 d_rf_freq = pmt_to_long(rf_freq);
119 std::cout << "[USRP_USB_INTERFACE] Setting USRP RBF to "
120 << d_rbf << std::endl;
122 std::cout << "[USRP_USB_INTERFACE] Setting TX interpolation to "
123 << d_interp_tx << std::endl;
125 std::cout << "[USRP_USB_INTERFACE] Setting RX interpolation to "
126 << d_decim_rx << std::endl;
128 std::cout << "[USRP_USB_INTERFACE] Using TX interface: "
129 << tx_interface << "\n";
131 std::cout << "[USRP_USB_INTERFACE] Using RX interface: "
132 << rx_interface << "\n";
136 d_cs = define_port("cs", "usrp-interface-cs", true, mb_port::EXTERNAL);
137 d_rx_cs = define_port("rx_cs", "usrp-rx-cs", false, mb_port::INTERNAL);
138 d_tx_cs = define_port("tx_cs", "usrp-tx-cs", false, mb_port::INTERNAL);
140 // Connect to TX and RX
141 define_component("tx", tx_interface, usrp_dict);
142 define_component("rx", rx_interface, usrp_dict);
143 connect("self", "rx_cs", "rx", "cs");
144 connect("self", "tx_cs", "tx", "cs");
146 // FIX ME: the code should query the FPGA to retrieve the number of channels and such
155 usrp_usb_interface::~usrp_usb_interface()
161 usrp_usb_interface::initial_transition()
167 * \brief Handles all incoming signals to the block from the lowest m-blocks
168 * which read/write to the bus, or the higher m-block which is the USRP server.
171 usrp_usb_interface::handle_message(mb_message_sptr msg)
173 pmt_t event = msg->signal(); // the "name" of the message
174 pmt_t port_id = msg->port_id(); // which port it came in on
175 pmt_t data = msg->data();
176 pmt_t invocation_handle;
178 if (pmt_eq(event, s_shutdown)) // ignore (for now)
181 //------------- CONTROL / STATUS -------------//
182 if (pmt_eq(port_id, d_cs->port_symbol())) {
184 //------------ OPEN --------------//
185 if (pmt_eq(event, s_cmd_usrp_open)){
186 handle_cmd_open(data);
189 //----------- CLOSE -------------//
190 else if (pmt_eq(event, s_cmd_usrp_close)) {
191 handle_cmd_close(data);
194 //---------- NTX CHAN ----------//
195 else if (pmt_eq(event, s_cmd_usrp_ntx_chan)) {
196 invocation_handle = pmt_nth(0, data);
197 d_cs->send(s_response_usrp_ntx_chan,
198 pmt_list2(invocation_handle,
199 pmt_from_long(d_ntx_chan)));
202 //---------- NRX CHAN ----------//
203 else if (pmt_eq(event, s_cmd_usrp_nrx_chan)) {
204 invocation_handle = pmt_nth(0, data);
205 d_cs->send(s_response_usrp_nrx_chan,
206 pmt_list2(invocation_handle,
207 pmt_from_long(d_nrx_chan)));
210 //------------ WRITE -----------//
211 else if(pmt_eq(event, s_cmd_usrp_write)) {
212 handle_cmd_write(data);
215 //-------- START READING --------//
216 else if(pmt_eq(event, s_cmd_usrp_start_reading)) {
217 handle_cmd_start_reading(data);
220 //-------- STOP READING --------//
221 else if(pmt_eq(event, s_cmd_usrp_stop_reading)) {
222 handle_cmd_stop_reading(data);
229 //---------------- RX ------------------//
230 if (pmt_eq(port_id, d_rx_cs->port_symbol())) {
232 // Relay reads back up
233 if(pmt_eq(event, s_response_usrp_rx_read)) {
234 d_cs->send(s_response_usrp_read, data);
241 //---------------- TX ------------------//
242 if (pmt_eq(port_id, d_tx_cs->port_symbol())) {
244 if(pmt_eq(event, s_response_usrp_tx_write)) {
246 pmt_t invocation_handle = pmt_nth(0, data);
247 pmt_t status = pmt_nth(1, data);
248 pmt_t channel = pmt_nth(2, data);
250 d_cs->send(s_response_usrp_write,
251 pmt_list3(invocation_handle,
262 std::cout << "[USRP_USB_INTERFACE] unhandled msg: " << msg << std::endl;
266 * \brief Called by the handle_message() method when the incoming signal is to
267 * open a USB connection to the USRP (cmd-usrp-open).
269 * The \p data parameter is a PMT list, where the elements are an invocation
270 * handle and the USRP number.
273 usrp_usb_interface::handle_cmd_open(pmt_t data)
275 pmt_t invocation_handle = pmt_nth(0, data);
276 long which_usrp = pmt_to_long(pmt_nth(1, data));
280 d_cs->send(s_response_usrp_open, pmt_list2(invocation_handle, PMT_T));
285 std::cout << "[USRP_USB_INTERFACE] Handling open request for USRP " << which_usrp << "\n";
287 // Open up a standard RX and TX for communication with the USRP
289 d_utx = usrp_standard_tx::make(which_usrp,
293 4096, // USB block size
294 16, // nblocks for async transfers
300 std::cout << "[USRP_USB_INTERFACE] Failed to open TX\n";
301 reply_data = pmt_list2(invocation_handle, PMT_F);
302 d_cs->send(s_response_usrp_open, reply_data);
306 if(!d_utx->set_tx_freq (0,d_rf_freq) || !d_utx->set_tx_freq(1,d_rf_freq)) { // try setting center freq to 0
308 std::cout << "[USRP_USB_INTERFACE] Failed to set center frequency on TX\n";
309 reply_data = pmt_list2(invocation_handle, PMT_F);
310 d_cs->send(s_response_usrp_open, reply_data);
314 if(!d_utx->set_mux(0xBA98)) {
316 std::cout << "[USRP_USB_INTERFACE] Failed to set TX mux\n";
317 reply_data = pmt_list2(invocation_handle, PMT_F);
318 d_cs->send(s_response_usrp_open, reply_data);
325 std::cout << "[USRP_USB_INTERFACE] Setup TX channel\n";
328 usrp_standard_rx::make (which_usrp,
332 0, // set blank mode to start
333 4096, // USB block size
334 16, // number of blocks for async transfers
339 std::cout << "[usrp_server] Failed to open RX\n";
340 reply_data = pmt_list2(invocation_handle, PMT_F);
341 d_cs->send(s_response_usrp_open, reply_data);
345 if(!d_urx->set_rx_freq (0, -d_rf_freq) || !d_urx->set_rx_freq(1, -d_rf_freq)) {
347 std::cout << "[usrp_server] Failed to set center frequency on RX\n";
348 reply_data = pmt_list2(invocation_handle, PMT_F);
349 d_cs->send(s_response_usrp_open, reply_data);
353 // Two channels ... this really needs to end up being set correctly by
354 // querying for what dboards are connected
355 if(!d_urx->set_mux(0x32103210)) {
357 std::cout << "[USRP_USB_INTERFACE] Failed to set RX mux\n";
358 reply_data = pmt_list2(invocation_handle, PMT_F);
359 d_cs->send(s_response_usrp_open, reply_data);
364 std::cout << "[USRP_USB_INTERFACE] Setup RX channel\n";
366 // d_utx->_write_fpga_reg(FR_DEBUG_EN,0xf);
367 // d_utx->_write_oe(0, 0xffff, 0xffff);
368 // d_urx->_write_oe(0, 0xffff, 0xffff);
369 // d_utx->_write_oe(1, 0xffff, 0xffff);
370 // d_urx->_write_oe(1, 0xffff, 0xffff);
372 d_cs->send(s_response_usrp_open, pmt_list2(invocation_handle, PMT_T));
376 * \brief Called by the handle_message() method when the incoming signal is to
377 * write data to the USB bus (cmd-usrp-write).
379 * The \p data parameter is a PMT list containing 3 mandatory elements in the
380 * following order: an invocation handle, channel, and a uniform vector
381 * representation of the packets.
384 usrp_usb_interface::handle_cmd_write(pmt_t data)
386 pmt_t invocation_handle = pmt_nth(0, data);
387 pmt_t channel = pmt_nth(1, data);
388 pmt_t pkts = pmt_nth(2, data);
390 pmt_t tx_handle = pmt_make_any(d_utx);
392 d_tx_cs->send(s_cmd_usrp_tx_write,
393 pmt_list4(invocation_handle,
402 * \brief Called by the handle_message() method when the incoming signal is to
403 * start reading data from the USB bus (cmd-usrp-start-reading).
405 * The \p data parameter is a PMT list with a single element: an invocation
406 * handle which can be returned with the response.
409 usrp_usb_interface::handle_cmd_start_reading(pmt_t data)
411 pmt_t invocation_handle = pmt_nth(0, data);
414 std::cout << "[USRP_USB_INTERFACE] Starting RX...\n";
419 pmt_t rx_handle = pmt_make_any(d_urx);
421 d_rx_cs->send(s_cmd_usrp_rx_start_reading, pmt_list2(PMT_NIL, rx_handle));
429 * \brief Called by the handle_message() method when the incoming signal is to
430 * stop reading data from the USB bus (cmd-usrp-stop-reading).
432 * The \p data parameter is a PMT list with a single element: an invocation
433 * handle which can be returned with the response.
436 usrp_usb_interface::handle_cmd_stop_reading(pmt_t data)
438 pmt_t invocation_handle = pmt_nth(0, data);
442 std::cout << "[USRP_USB_INTERFACE] Stopping RX...\n";
445 // Used to allow a read() being called by a lower layer to complete before
446 // stopping, else there can be partial data left on the bus and can generate
448 while(usrp_rx_stop) {usleep(1);}
453 std::cout << "[USRP_USB_INTERFACE] Stopping fake RX...\n";
454 usrp_rx_stop_stub = true; // extern to communicate with stub to wait
457 d_rx_reading = false;
463 * \brief Called by the handle_message() method when the incoming signal is to
464 * close the USB connection to the USRP.
466 * The \p data parameter is a PMT list with a single element: an invocation
467 * handle which can be returned with the response.
470 usrp_usb_interface::handle_cmd_close(pmt_t data)
472 pmt_t invocation_handle = pmt_nth(0, data);
475 handle_cmd_stop_reading(PMT_NIL);
478 d_cs->send(s_response_usrp_close, pmt_list2(invocation_handle, PMT_T));
483 std::cout << "[USRP_USB_INTERFACE] Handling close request for USRP\n";
491 d_cs->send(s_response_usrp_close, pmt_list2(invocation_handle, PMT_T));
497 REGISTER_MBLOCK_CLASS(usrp_usb_interface);