3 * Copyright 2004,2008 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_standard.h>
32 static const int OUTPUT_MULTIPLE_BYTES = 4 * 1024;
34 usrp_source_base::usrp_source_base (const std::string &name,
35 gr_io_signature_sptr output_signature,
37 unsigned int decim_rate,
43 const std::string fpga_filename,
44 const std::string firmware_filename
45 ) throw (std::runtime_error)
47 gr_make_io_signature (0, 0, 0),
51 d_usrp = usrp_standard_rx::make (which_board, decim_rate,
58 throw std::runtime_error ("can't open usrp");
60 set_usrp_basic(d_usrp);
62 // All calls to d_usrp->read must be multiples of 512 bytes.
63 // We jack this up to 4k to reduce overhead.
65 set_output_multiple (OUTPUT_MULTIPLE_BYTES / output_signature->sizeof_stream_item (0));
68 usrp_source_base::~usrp_source_base ()
73 usrp_source_base::sizeof_basic_sample() const
75 return usrp_standard_rx::format_width(d_usrp->format()) / 8;
79 usrp_source_base::work (int noutput_items,
80 gr_vector_const_void_star &input_items,
81 gr_vector_void_star &output_items)
83 static const int BUFSIZE = 4 * OUTPUT_MULTIPLE_BYTES;
84 unsigned char buf[BUFSIZE];
86 int output_items_produced;
90 while (output_index < noutput_items){
91 int nbytes = ninput_bytes_reqd_for_noutput_items (noutput_items - output_index);
92 nbytes = std::min (nbytes, BUFSIZE);
94 int result_nbytes = d_usrp->read (buf, nbytes, &overrun);
96 // fprintf (stderr, "usrp_source: overrun\n");
101 if (result_nbytes < 0) // We've got a problem. Usually board unplugged or powered down.
102 return -1; // Indicate we're done.
104 if (result_nbytes != nbytes){ // not really an error, but unexpected
105 fprintf (stderr, "usrp_source: short read. Expected %d, got %d\n",
106 nbytes, result_nbytes);
109 copy_from_usrp_buffer (output_items,
111 noutput_items - output_index, // output_items_available
112 output_items_produced, // [out]
114 result_nbytes, // usrp_buffer_length
115 bytes_read); // [out]
117 assert (output_index + output_items_produced <= noutput_items);
118 assert (bytes_read == result_nbytes);
120 output_index += output_items_produced;
123 return noutput_items;
128 usrp_source_base::set_decim_rate (unsigned int rate)
130 return d_usrp->set_decim_rate (rate);
134 usrp_source_base::set_nchannels (int nchan)
136 return d_usrp->set_nchannels (nchan);
140 usrp_source_base::set_mux (int mux)
142 return d_usrp->set_mux (mux);
146 usrp_source_base::determine_rx_mux_value(usrp_subdev_spec ss)
148 return d_usrp->determine_rx_mux_value(ss);
152 usrp_source_base::set_rx_freq (int channel, double freq)
154 return d_usrp->set_rx_freq (channel, freq);
158 usrp_source_base::decim_rate () const
160 return d_usrp->decim_rate ();
164 usrp_source_base::nchannels () const
166 return d_usrp->nchannels ();
170 usrp_source_base::mux () const
172 return d_usrp->mux ();
176 usrp_source_base::rx_freq (int channel) const
178 return d_usrp->rx_freq (channel);
182 usrp_source_base::set_fpga_mode (int mode)
184 return d_usrp->set_fpga_mode (mode);
188 usrp_source_base::set_ddc_phase (int channel, int phase)
190 return d_usrp->set_ddc_phase(channel, phase);
195 usrp_source_base::set_format(unsigned int format)
197 return d_usrp->set_format(format);
201 usrp_source_base::format() const
203 return d_usrp->format();
207 usrp_source_base::make_format(int width, int shift, bool want_q, bool bypass_halfband)
209 return usrp_standard_rx::make_format(width, shift, want_q, bypass_halfband);
213 usrp_source_base::format_width(unsigned int format)
215 return usrp_standard_rx::format_width(format);
219 usrp_source_base::format_shift(unsigned int format)
221 return usrp_standard_rx::format_shift(format);
225 usrp_source_base::format_want_q(unsigned int format)
227 return usrp_standard_rx::format_want_q(format);
231 usrp_source_base::format_bypass_halfband(unsigned int format)
233 return usrp_standard_rx::format_bypass_halfband(format);
237 usrp_source_base::has_rx_halfband()
239 return d_usrp->has_rx_halfband();
243 usrp_source_base::has_tx_halfband()
245 return d_usrp->has_tx_halfband();
249 usrp_source_base::nddcs()
251 return d_usrp->nddcs();
255 usrp_source_base::nducs()
257 return d_usrp->nducs();
261 usrp_source_base::start()
263 return d_usrp->start();
267 usrp_source_base::stop()
269 return d_usrp->stop();
273 usrp_source_base::tune(int chan, db_base_sptr db, double target_freq, usrp_tune_result *result)
275 return d_usrp->tune(chan, db, target_freq, result);
279 usrp_source_base::pick_rx_subdevice()
282 USRP_DBID_FLEX_400_RX,
283 USRP_DBID_FLEX_900_RX,
284 USRP_DBID_FLEX_1200_RX,
285 USRP_DBID_FLEX_2400_RX,
287 USRP_DBID_TV_RX_REV_2,
289 USRP_DBID_DBS_RX_REV_2_1,
293 std::vector<int> candidates(dbids, dbids+(sizeof(dbids)/sizeof(int)));
294 return pick_subdev(candidates);