Merged r10071:10164 from features/cppdb-test into trunk. Implements the fully native...
[debian/gnuradio] / usrp / host / lib / inband / usrp_usb_interface.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
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)
10  * any later version.
11  * 
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.
16  * 
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <usrp_usb_interface.h>
27
28 #include <iostream>
29 #include <vector>
30 #include <usb.h>
31 #include <mblock/class_registry.h>
32 #include <usrp_inband_usb_packet.h>
33 #include <fpga_regs_common.h>
34 #include "usrp_rx.h"
35 #include <usrp_rx_stub.h>
36 #include "usrp_tx.h"
37 #include "usrp_standard.h"
38 #include <stdio.h>
39
40 typedef usrp_inband_usb_packet transport_pkt;
41
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");
46
47 static const bool verbose = false;
48
49
50 /*!
51  * \brief Initializes the USB interface m-block.
52  *
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.
55  */
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),
58   d_fake_usrp(false),
59   d_rx_reading(false),
60   d_interp_tx(128),
61   d_decim_rx(128),
62   d_rf_freq(10e6),
63   d_rbf("inband_tx_rx.rbf")
64 {
65   // Dictionary for arguments to all of the components
66   pmt_t usrp_dict = user_arg;
67   
68   // Default TX/RX interface
69   std::string tx_interface = "usrp_tx";
70   std::string rx_interface = "usrp_rx";
71   
72   if (pmt_is_dict(usrp_dict)) {
73
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"), 
77                                       PMT_NIL)) {
78       if(pmt_eqv(fake_usrp, PMT_T)) {
79         tx_interface = "usrp_tx_stub";
80         rx_interface = "usrp_rx_stub";
81         d_fake_usrp=true;
82       }
83     }
84
85     // Read the TX interpolations
86     if(pmt_t interp_tx = pmt_dict_ref(usrp_dict, 
87                                       pmt_intern("interp-tx"), 
88                                       PMT_NIL)) {
89       if(!pmt_eqv(interp_tx, PMT_NIL)) 
90         d_interp_tx = pmt_to_long(interp_tx);
91     }
92     
93     // Read the RX decimation rate
94     if(pmt_t decim_rx = pmt_dict_ref(usrp_dict, 
95                                       pmt_intern("decim-rx"), 
96                                       PMT_NIL)) {
97       if(!pmt_eqv(decim_rx, PMT_NIL)) 
98         d_decim_rx = pmt_to_long(decim_rx);
99     }
100
101     // Read the RBF
102     if(pmt_t rbf = pmt_dict_ref(usrp_dict, 
103                                 pmt_intern("rbf"), 
104                                 PMT_NIL)) {
105       if(!pmt_eqv(rbf, PMT_NIL)) 
106         d_rbf = pmt_symbol_to_string(rbf);
107     }
108
109     // The RF center frequency
110     if(pmt_t rf_freq = pmt_dict_ref(usrp_dict, 
111                                 pmt_intern("rf-freq"), 
112                                 PMT_NIL)) {
113       if(!pmt_eqv(rf_freq, PMT_NIL)) 
114         d_rf_freq = pmt_to_long(rf_freq);
115     }
116   }
117   
118   if (verbose) {
119     std::cout << "[USRP_USB_INTERFACE] Setting USRP RBF to " 
120               << d_rbf << std::endl;
121     
122     std::cout << "[USRP_USB_INTERFACE] Setting TX interpolation to " 
123               << d_interp_tx << std::endl;
124           
125     std::cout << "[USRP_USB_INTERFACE] Setting RX interpolation to " 
126               << d_decim_rx << std::endl;
127
128     std::cout << "[USRP_USB_INTERFACE] Using TX interface: " 
129               << tx_interface << "\n";
130
131     std::cout << "[USRP_USB_INTERFACE] Using RX interface: " 
132               << rx_interface << "\n";
133
134   }
135
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);       
139
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");
145   
146   // FIXME: the code should query the FPGA to retrieve the number of channels and such
147   d_ntx_chan = 2;
148   d_nrx_chan = 2;
149 }
150
151 usrp_usb_interface::~usrp_usb_interface() 
152
153
154 }
155
156 void 
157 usrp_usb_interface::initial_transition()
158 {
159
160 }
161
162 /*!
163  * \brief Handles all incoming signals to the block from the lowest m-blocks
164  * which read/write to the bus, or the higher m-block which is the USRP server.
165  */
166 void
167 usrp_usb_interface::handle_message(mb_message_sptr msg)
168 {
169   pmt_t event = msg->signal();          // the "name" of the message
170   pmt_t port_id = msg->port_id();       // which port it came in on
171   pmt_t data = msg->data();
172   pmt_t invocation_handle;
173
174   if (pmt_eq(event, s_shutdown))        // ignore (for now)
175     return;
176
177   //------------- CONTROL / STATUS -------------//
178   if (pmt_eq(port_id, d_cs->port_symbol())) {   
179
180     //------------ OPEN --------------//
181     if (pmt_eq(event, s_cmd_usrp_open)){
182       handle_cmd_open(data);
183       return;
184     }
185     //----------- CLOSE -------------//
186     else if (pmt_eq(event, s_cmd_usrp_close)) {
187       handle_cmd_close(data);
188       return;
189     }
190     //---------- NTX CHAN ----------//
191     else if (pmt_eq(event, s_cmd_usrp_ntx_chan)) {
192       invocation_handle = pmt_nth(0, data);
193       d_cs->send(s_response_usrp_ntx_chan, 
194                  pmt_list2(invocation_handle, 
195                            pmt_from_long(d_ntx_chan)));
196       return;
197     }
198     //---------- NRX CHAN ----------//
199     else if (pmt_eq(event, s_cmd_usrp_nrx_chan)) {
200       invocation_handle = pmt_nth(0, data);
201       d_cs->send(s_response_usrp_nrx_chan, 
202                  pmt_list2(invocation_handle, 
203                            pmt_from_long(d_nrx_chan)));
204       return;
205     }
206     //------------ WRITE -----------//
207     else if(pmt_eq(event, s_cmd_usrp_write)) {
208       handle_cmd_write(data);
209       return;
210     }
211     //-------- START READING --------//
212     else if(pmt_eq(event, s_cmd_usrp_start_reading)) {
213       handle_cmd_start_reading(data);
214       return;
215     }
216     //-------- STOP READING --------//
217     else if(pmt_eq(event, s_cmd_usrp_stop_reading)) {
218       handle_cmd_stop_reading(data);
219       return;
220     }
221
222     goto unhandled;
223   }
224
225   //---------------- RX ------------------//
226   if (pmt_eq(port_id, d_rx_cs->port_symbol())) {        
227
228     // Relay reads back up
229     if(pmt_eq(event, s_response_usrp_rx_read))  {
230       d_cs->send(s_response_usrp_read, data);
231       return;
232     }
233
234     goto unhandled;
235   }
236   
237   //---------------- TX ------------------//
238   if (pmt_eq(port_id, d_tx_cs->port_symbol())) {        
239
240     if(pmt_eq(event, s_response_usrp_tx_write))  {
241
242       pmt_t invocation_handle = pmt_nth(0, data);
243       pmt_t status = pmt_nth(1, data);
244       pmt_t channel = pmt_nth(2, data);
245
246       d_cs->send(s_response_usrp_write,
247                  pmt_list3(invocation_handle,
248                            status,
249                            channel));
250
251       return;
252     }
253
254     goto unhandled;
255   }
256
257  unhandled:
258   std::cout << "[USRP_USB_INTERFACE] unhandled msg: " << msg << std::endl;
259 }
260
261 /*!
262  * \brief Called by the handle_message() method when the incoming signal is to
263  * open a USB connection to the USRP (cmd-usrp-open).
264  *
265  * The \p data parameter is a PMT list, where the elements are an invocation
266  * handle and the USRP number.
267  */
268 void
269 usrp_usb_interface::handle_cmd_open(pmt_t data)
270 {
271   pmt_t invocation_handle = pmt_nth(0, data);
272   long which_usrp = pmt_to_long(pmt_nth(1, data));
273   pmt_t reply_data;
274  
275   if(d_fake_usrp) {
276     d_cs->send(s_response_usrp_open, pmt_list2(invocation_handle, PMT_T));
277     return;
278   }
279
280   if (verbose)
281     std::cout << "[USRP_USB_INTERFACE] Handling open request for USRP " << which_usrp << "\n";
282
283   // Open up a standard RX and TX for communication with the USRP
284    
285   d_utx = usrp_standard_tx::make(which_usrp,
286                                  d_interp_tx,
287                                  1,                     // 1 channel
288                                  -1,          // mux
289                                  4096,        // USB block size
290                                  16,          // nblocks for async transfers
291                                  d_rbf
292                                  );
293   
294   if(d_utx==0) {
295     if (verbose)
296       std::cout << "[USRP_USB_INTERFACE] Failed to open TX\n";
297     reply_data = pmt_list2(invocation_handle, PMT_F);
298     d_cs->send(s_response_usrp_open, reply_data);
299     return;
300   }
301
302   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
303     if (verbose)
304       std::cout << "[USRP_USB_INTERFACE] Failed to set center frequency on TX\n";
305     reply_data = pmt_list2(invocation_handle, PMT_F);
306     d_cs->send(s_response_usrp_open, reply_data);
307     return;
308   }
309
310   if(!d_utx->set_mux(0xBA98)) {
311     if (verbose)
312       std::cout << "[USRP_USB_INTERFACE] Failed to set TX mux\n";
313     reply_data = pmt_list2(invocation_handle, PMT_F);
314     d_cs->send(s_response_usrp_open, reply_data);
315     return;
316   }
317
318   d_utx->start();
319
320   if (verbose)
321     std::cout << "[USRP_USB_INTERFACE] Setup TX channel\n";
322
323   d_urx =
324     usrp_standard_rx::make (which_usrp,
325                             d_decim_rx,         
326                             1,                  // nchan
327                             -1,           // mux
328                             0,            // set blank mode to start
329                             4096,         // USB block size
330                             16,           // number of blocks for async transfers
331           d_rbf);
332
333   if(!d_urx) {
334     if (verbose)
335       std::cout << "[usrp_server] Failed to open RX\n";
336     reply_data = pmt_list2(invocation_handle, PMT_F);
337     d_cs->send(s_response_usrp_open, reply_data);
338     return;
339   }
340
341   if(!d_urx->set_rx_freq (0, -d_rf_freq) || !d_urx->set_rx_freq(1, -d_rf_freq)) {
342     if (verbose)
343       std::cout << "[usrp_server] Failed to set center frequency on RX\n";
344     reply_data = pmt_list2(invocation_handle, PMT_F);
345     d_cs->send(s_response_usrp_open, reply_data);
346     return;
347   }
348   
349   // Two channels ... this really needs to end up being set correctly by
350   // querying for what dboards are connected
351   if(!d_urx->set_mux(0x32103210)) {
352     if (verbose)
353       std::cout << "[USRP_USB_INTERFACE] Failed to set RX mux\n";
354     reply_data = pmt_list2(invocation_handle, PMT_F);
355     d_cs->send(s_response_usrp_open, reply_data);
356     return;
357   }
358
359   if (verbose)
360     std::cout << "[USRP_USB_INTERFACE] Setup RX channel\n";
361     
362 //  d_utx->_write_fpga_reg(FR_DEBUG_EN,0xf);
363 //  d_utx->_write_oe(0, 0xffff, 0xffff);
364 //  d_urx->_write_oe(0, 0xffff, 0xffff);
365 //  d_utx->_write_oe(1, 0xffff, 0xffff);
366 //  d_urx->_write_oe(1, 0xffff, 0xffff);
367
368   d_cs->send(s_response_usrp_open, pmt_list2(invocation_handle, PMT_T));
369 }
370
371 /*!
372  * \brief Called by the handle_message() method when the incoming signal is to
373  * write data to the USB bus (cmd-usrp-write). 
374  *
375  * The \p data parameter is a PMT list containing 3 mandatory elements in the
376  * following order: an invocation handle, channel, and a uniform vector
377  * representation of the packets.
378  */
379 void
380 usrp_usb_interface::handle_cmd_write(pmt_t data)
381 {
382   pmt_t invocation_handle = pmt_nth(0, data);
383   pmt_t channel = pmt_nth(1, data);
384   pmt_t pkts = pmt_nth(2, data);
385
386   pmt_t tx_handle = pmt_make_any(d_utx);
387
388   d_tx_cs->send(s_cmd_usrp_tx_write, 
389                 pmt_list4(invocation_handle, 
390                           channel,
391                           pkts,
392                           tx_handle));
393 }
394
395 /*!
396  * \brief Called by the handle_message() method when the incoming signal is to
397  * start reading data from the USB bus (cmd-usrp-start-reading).
398  *
399  * The \p data parameter is a PMT list with a single element: an invocation
400  * handle which can be returned with the response.
401  */
402 void
403 usrp_usb_interface::handle_cmd_start_reading(pmt_t data)
404 {
405   pmt_t invocation_handle = pmt_nth(0, data);
406   
407   if(verbose)
408     std::cout << "[USRP_USB_INTERFACE] Starting RX...\n";
409
410   if(!d_fake_usrp)
411     d_urx->start();
412
413   pmt_t rx_handle = pmt_make_any(d_urx);
414
415   d_rx_cs->send(s_cmd_usrp_rx_start_reading, pmt_list2(PMT_NIL, rx_handle));
416
417   d_rx_reading = true;
418
419   return;
420 }
421
422 /*!
423  * \brief Called by the handle_message() method when the incoming signal is to
424  * stop reading data from the USB bus (cmd-usrp-stop-reading).
425  *
426  * The \p data parameter is a PMT list with a single element: an invocation
427  * handle which can be returned with the response.
428  */
429 void
430 usrp_usb_interface::handle_cmd_stop_reading(pmt_t data)
431 {
432   pmt_t invocation_handle = pmt_nth(0, data);
433   
434   if(!d_fake_usrp) {
435     if(verbose)
436       std::cout << "[USRP_USB_INTERFACE] Stopping RX...\n";
437     usrp_rx_stop = true;
438
439     // Used to allow a read() being called by a lower layer to complete before
440     // stopping, else there can be partial data left on the bus and can generate
441     // errors.
442     while(usrp_rx_stop) {usleep(1);}
443     d_urx->stop();
444   }
445   else {
446     if(verbose)
447       std::cout << "[USRP_USB_INTERFACE] Stopping fake RX...\n";
448     usrp_rx_stop_stub = true;  // extern to communicate with stub to wait
449   }
450
451   d_rx_reading = false;
452
453   return;
454 }
455
456 /*!
457  * \brief Called by the handle_message() method when the incoming signal is to
458  * close the USB connection to the USRP.
459  *
460  * The \p data parameter is a PMT list with a single element: an invocation
461  * handle which can be returned with the response.
462  */
463 void
464 usrp_usb_interface::handle_cmd_close(pmt_t data)
465 {
466   pmt_t invocation_handle = pmt_nth(0, data);
467
468   if(d_rx_reading)
469     handle_cmd_stop_reading(PMT_NIL);
470
471   if(d_fake_usrp) {
472     d_cs->send(s_response_usrp_close, pmt_list2(invocation_handle, PMT_T));
473     return;
474   }
475   
476   if (verbose)
477     std::cout << "[USRP_USB_INTERFACE] Handling close request for USRP\n";
478
479   d_utx.reset();
480   d_urx.reset();
481
482   d_cs->send(s_response_usrp_close, pmt_list2(invocation_handle, PMT_T));
483
484   // FIXME This seems like a _very_ strange place to be calling shutdown_all.
485   // That decision should be left to high-level code, not low-level code like this.
486   shutdown_all(PMT_T);
487 }
488
489
490 REGISTER_MBLOCK_CLASS(usrp_usb_interface);