Merged r10071:10164 from features/cppdb-test into trunk. Implements the fully native...
[debian/gnuradio] / gr-usrp / src / usrp_source_base.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,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
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <usrp_source_base.h>
28 #include <gr_io_signature.h>
29 #include <usrp_standard.h>
30 #include <assert.h>
31
32 static const int OUTPUT_MULTIPLE_BYTES = 4 * 1024;
33
34 usrp_source_base::usrp_source_base (const std::string &name,
35                                       gr_io_signature_sptr output_signature,
36                                       int which_board,
37                                       unsigned int decim_rate,
38                                       int nchan,
39                                       int mux,
40                                       int mode,
41                                       int fusb_block_size,
42                                       int fusb_nblocks,
43                                       const std::string fpga_filename,
44                                       const std::string firmware_filename
45                                       ) throw (std::runtime_error)
46   : usrp_base(name,
47               gr_make_io_signature (0, 0, 0),
48               output_signature),
49     d_noverruns (0)
50 {
51   d_usrp = usrp_standard_rx::make (which_board, decim_rate,
52                                    nchan, mux, mode,
53                                    fusb_block_size,
54                                    fusb_nblocks,
55                                    fpga_filename,
56                                    firmware_filename);
57   if (d_usrp == 0)
58     throw std::runtime_error ("can't open usrp");
59
60   set_usrp_basic(d_usrp);
61
62   // All calls to d_usrp->read must be multiples of 512 bytes.
63   // We jack this up to 4k to reduce overhead.
64
65   set_output_multiple (OUTPUT_MULTIPLE_BYTES / output_signature->sizeof_stream_item (0));
66 }
67
68 usrp_source_base::~usrp_source_base ()
69 {
70 }
71
72 unsigned int
73 usrp_source_base::sizeof_basic_sample() const
74 {
75   return usrp_standard_rx::format_width(d_usrp->format()) / 8;
76 }
77
78 int
79 usrp_source_base::work (int noutput_items,
80                          gr_vector_const_void_star &input_items,
81                          gr_vector_void_star &output_items)
82 {
83   static const int BUFSIZE = 4 * OUTPUT_MULTIPLE_BYTES;
84   unsigned char buf[BUFSIZE];
85   int output_index = 0;
86   int output_items_produced;
87   int bytes_read;
88   bool overrun;
89
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);
93
94     int result_nbytes = d_usrp->read (buf, nbytes, &overrun);
95     if (overrun){
96       // fprintf (stderr, "usrp_source: overrun\n");
97       fputs ("uO", stderr);
98       d_noverruns++;
99     }
100
101     if (result_nbytes < 0)      // We've got a problem.  Usually board unplugged or powered down.
102       return -1;                // Indicate we're done.
103
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);
107     }
108
109     copy_from_usrp_buffer (output_items,
110                            output_index,
111                            noutput_items - output_index,   // output_items_available
112                            output_items_produced,          // [out]
113                            buf,                            // usrp_buffer
114                            result_nbytes,                  // usrp_buffer_length
115                            bytes_read);                    // [out]
116
117     assert (output_index + output_items_produced <= noutput_items);
118     assert (bytes_read == result_nbytes);
119
120     output_index += output_items_produced;
121   }
122
123   return noutput_items;
124 }
125
126
127 bool
128 usrp_source_base::set_decim_rate (unsigned int rate)
129 {
130   return d_usrp->set_decim_rate (rate);
131 }
132
133 bool
134 usrp_source_base::set_nchannels (int nchan)
135 {
136   return d_usrp->set_nchannels (nchan);
137 }
138
139 bool
140 usrp_source_base::set_mux (int mux)
141 {
142   return d_usrp->set_mux (mux);
143 }
144
145 int
146 usrp_source_base::determine_rx_mux_value(usrp_subdev_spec ss)
147 {
148   return d_usrp->determine_rx_mux_value(ss);
149 }
150
151 bool
152 usrp_source_base::set_rx_freq (int channel, double freq)
153 {
154   return d_usrp->set_rx_freq (channel, freq);
155 }
156
157 unsigned int
158 usrp_source_base::decim_rate () const
159 {
160   return d_usrp->decim_rate ();
161 }
162
163 int
164 usrp_source_base::nchannels () const
165 {
166   return d_usrp->nchannels ();
167 }
168
169 int
170 usrp_source_base::mux () const
171 {
172   return d_usrp->mux ();
173 }
174
175 double
176 usrp_source_base::rx_freq (int channel) const
177 {
178   return d_usrp->rx_freq (channel);
179 }
180
181 bool
182 usrp_source_base::set_fpga_mode (int mode)
183 {
184   return d_usrp->set_fpga_mode (mode);
185 }
186
187 bool
188 usrp_source_base::set_ddc_phase (int channel, int phase)
189 {
190   return d_usrp->set_ddc_phase(channel, phase);
191 }
192
193
194 bool
195 usrp_source_base::set_format(unsigned int format)
196 {
197   return d_usrp->set_format(format);
198 }
199
200 unsigned int
201 usrp_source_base::format() const
202 {
203   return d_usrp->format();
204 }
205
206 unsigned int
207 usrp_source_base::make_format(int width, int shift, bool want_q, bool bypass_halfband)
208 {
209   return usrp_standard_rx::make_format(width, shift, want_q, bypass_halfband);
210 }
211
212 int
213 usrp_source_base::format_width(unsigned int format)
214 {
215   return usrp_standard_rx::format_width(format);
216 }
217
218 int
219 usrp_source_base::format_shift(unsigned int format)
220 {
221   return usrp_standard_rx::format_shift(format);
222 }
223
224 bool
225 usrp_source_base::format_want_q(unsigned int format)
226 {
227   return usrp_standard_rx::format_want_q(format);
228 }
229
230 bool
231 usrp_source_base::format_bypass_halfband(unsigned int format)
232 {
233   return usrp_standard_rx::format_bypass_halfband(format);
234 }
235
236 bool
237 usrp_source_base::has_rx_halfband()
238 {
239   return d_usrp->has_rx_halfband();
240 }
241  
242 bool
243 usrp_source_base::has_tx_halfband()
244 {
245   return d_usrp->has_tx_halfband();
246 }
247
248 int
249 usrp_source_base::nddcs()
250 {
251   return d_usrp->nddcs();
252 }
253
254 int
255 usrp_source_base::nducs()
256 {
257   return d_usrp->nducs();
258 }
259
260 bool
261 usrp_source_base::start()
262 {
263   return d_usrp->start();
264 }
265
266 bool
267 usrp_source_base::stop()
268 {
269   return d_usrp->stop();
270 }
271
272 bool
273 usrp_source_base::tune(int chan, db_base_sptr db, double target_freq, usrp_tune_result *result)
274 {
275   return d_usrp->tune(chan, db, target_freq, result);
276 }
277
278 usrp_subdev_spec
279 usrp_source_base::pick_rx_subdevice()
280 {
281   int dbids[] = {
282     USRP_DBID_FLEX_400_RX,
283     USRP_DBID_FLEX_900_RX,
284     USRP_DBID_FLEX_1200_RX,
285     USRP_DBID_FLEX_2400_RX,
286     USRP_DBID_TV_RX,
287     USRP_DBID_TV_RX_REV_2,
288     USRP_DBID_DBS_RX,
289     USRP_DBID_DBS_RX_REV_2_1,
290     USRP_DBID_BASIC_RX
291   };
292
293   std::vector<int> candidates(dbids, dbids+(sizeof(dbids)/sizeof(int)));
294   return pick_subdev(candidates);
295 }