3 * Copyright 2004,2008,2009 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
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include <usrp_source_base.h>
28 #include <gr_io_signature.h>
29 #include <usrp/usrp_standard.h>
33 static const int OUTPUT_MULTIPLE_BYTES = 4 * 1024;
35 usrp_source_base::usrp_source_base (const std::string &name,
36 gr_io_signature_sptr output_signature,
38 unsigned int decim_rate,
44 const std::string fpga_filename,
45 const std::string firmware_filename
46 ) throw (std::runtime_error)
48 gr_make_io_signature (0, 0, 0),
52 d_usrp = usrp_standard_rx::make (which_board, decim_rate,
59 throw std::runtime_error ("can't open usrp");
61 set_usrp_basic(d_usrp);
63 // All calls to d_usrp->read must be multiples of 512 bytes.
64 // We jack this up to 4k to reduce overhead.
66 set_output_multiple (OUTPUT_MULTIPLE_BYTES / output_signature->sizeof_stream_item (0));
69 usrp_source_base::~usrp_source_base ()
74 usrp_source_base::sizeof_basic_sample() const
76 return usrp_standard_rx::format_width(d_usrp->format()) / 8;
80 usrp_source_base::work (int noutput_items,
81 gr_vector_const_void_star &input_items,
82 gr_vector_void_star &output_items)
84 static const int BUFSIZE = 4 * OUTPUT_MULTIPLE_BYTES;
85 unsigned char buf[BUFSIZE];
87 int output_items_produced;
91 while (output_index < noutput_items){
92 int nbytes = ninput_bytes_reqd_for_noutput_items (noutput_items - output_index);
93 nbytes = std::min (nbytes, BUFSIZE);
95 int result_nbytes = d_usrp->read (buf, nbytes, &overrun);
97 // fprintf (stderr, "usrp_source: overrun\n");
102 if (result_nbytes < 0) // We've got a problem. Usually board unplugged or powered down.
103 return -1; // Indicate we're done.
105 if (result_nbytes != nbytes){ // not really an error, but unexpected
106 fprintf (stderr, "usrp_source: short read. Expected %d, got %d\n",
107 nbytes, result_nbytes);
110 copy_from_usrp_buffer (output_items,
112 noutput_items - output_index, // output_items_available
113 output_items_produced, // [out]
115 result_nbytes, // usrp_buffer_length
116 bytes_read); // [out]
118 assert (output_index + output_items_produced <= noutput_items);
119 assert (bytes_read == result_nbytes);
121 output_index += output_items_produced;
124 return noutput_items;
129 usrp_source_base::set_decim_rate (unsigned int rate)
131 return d_usrp->set_decim_rate (rate);
135 usrp_source_base::set_nchannels (int nchan)
137 return d_usrp->set_nchannels (nchan);
141 usrp_source_base::set_mux (int mux)
143 return d_usrp->set_mux (mux);
147 usrp_source_base::determine_rx_mux_value(usrp_subdev_spec ss)
149 return d_usrp->determine_rx_mux_value(ss);
153 usrp_source_base::determine_rx_mux_value(usrp_subdev_spec ss_a, usrp_subdev_spec ss_b)
155 return d_usrp->determine_rx_mux_value(ss_a, ss_b);
159 usrp_source_base::set_rx_freq (int channel, double freq)
161 return d_usrp->set_rx_freq (channel, freq);
165 usrp_source_base::decim_rate () const
167 return d_usrp->decim_rate ();
171 usrp_source_base::nchannels () const
173 return d_usrp->nchannels ();
177 usrp_source_base::mux () const
179 return d_usrp->mux ();
183 usrp_source_base::rx_freq (int channel) const
185 return d_usrp->rx_freq (channel);
189 usrp_source_base::set_fpga_mode (int mode)
191 return d_usrp->set_fpga_mode (mode);
195 usrp_source_base::set_ddc_phase (int channel, int phase)
197 return d_usrp->set_ddc_phase(channel, phase);
202 usrp_source_base::set_format(unsigned int format)
204 return d_usrp->set_format(format);
208 usrp_source_base::format() const
210 return d_usrp->format();
214 usrp_source_base::make_format(int width, int shift, bool want_q, bool bypass_halfband)
216 return usrp_standard_rx::make_format(width, shift, want_q, bypass_halfband);
220 usrp_source_base::format_width(unsigned int format)
222 return usrp_standard_rx::format_width(format);
226 usrp_source_base::format_shift(unsigned int format)
228 return usrp_standard_rx::format_shift(format);
232 usrp_source_base::format_want_q(unsigned int format)
234 return usrp_standard_rx::format_want_q(format);
238 usrp_source_base::format_bypass_halfband(unsigned int format)
240 return usrp_standard_rx::format_bypass_halfband(format);
244 usrp_source_base::has_rx_halfband()
246 return d_usrp->has_rx_halfband();
250 usrp_source_base::has_tx_halfband()
252 return d_usrp->has_tx_halfband();
256 usrp_source_base::nddcs()
258 return d_usrp->nddcs();
262 usrp_source_base::nducs()
264 return d_usrp->nducs();
268 usrp_source_base::start()
270 return d_usrp->start();
274 usrp_source_base::stop()
276 return d_usrp->stop();
280 usrp_source_base::tune(int chan, db_base_sptr db, double target_freq, usrp_tune_result *result)
282 return d_usrp->tune(chan, db, target_freq, result);
286 usrp_source_base::pick_rx_subdevice()
289 USRP_DBID_FLEX_400_RX,
290 USRP_DBID_FLEX_900_RX,
291 USRP_DBID_FLEX_1200_RX,
292 USRP_DBID_FLEX_2400_RX,
294 USRP_DBID_TV_RX_REV_2,
295 USRP_DBID_TV_RX_REV_3,
296 USRP_DBID_TV_RX_MIMO,
297 USRP_DBID_TV_RX_REV_2_MIMO,
298 USRP_DBID_TV_RX_REV_3_MIMO,
303 std::vector<int> candidates(dbids, dbids+(sizeof(dbids)/sizeof(int)));
304 return pick_subdev(candidates);