Imported Upstream version 3.0.4
[debian/gnuradio] / gr-usrp / src / usrp1_source_base.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 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 <usrp1_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 usrp1_source_base::usrp1_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   : gr_sync_block (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 usrp1");
59
60   // All calls to d_usrp->read must be multiples of 512 bytes.
61   // We jack this up to 4k to reduce overhead.
62
63   set_output_multiple (OUTPUT_MULTIPLE_BYTES / output_signature->sizeof_stream_item (0));
64 }
65
66 usrp1_source_base::~usrp1_source_base ()
67 {
68   delete d_usrp;
69 }
70
71 unsigned int
72 usrp1_source_base::sizeof_basic_sample() const
73 {
74   return usrp_standard_rx::format_width(d_usrp->format()) / 8;
75 }
76
77 bool
78 usrp1_source_base::start()
79 {
80   return d_usrp->start();
81 }
82
83 bool
84 usrp1_source_base::stop()
85 {
86   return d_usrp->stop();
87 }
88
89 int
90 usrp1_source_base::work (int noutput_items,
91                          gr_vector_const_void_star &input_items,
92                          gr_vector_void_star &output_items)
93 {
94   static const int BUFSIZE = 4 * OUTPUT_MULTIPLE_BYTES;
95   unsigned char buf[BUFSIZE];
96   int output_index = 0;
97   int output_items_produced;
98   int bytes_read;
99   bool overrun;
100
101   while (output_index < noutput_items){
102     int nbytes = ninput_bytes_reqd_for_noutput_items (noutput_items - output_index);
103     nbytes = std::min (nbytes, BUFSIZE);
104
105     int result_nbytes = d_usrp->read (buf, nbytes, &overrun);
106     if (overrun){
107       // fprintf (stderr, "usrp1_source: overrun\n");
108       fputs ("uO", stderr);
109       d_noverruns++;
110     }
111
112     if (result_nbytes < 0)      // We've got a problem.  Usually board unplugged or powered down.
113       return -1;                // Indicate we're done.
114
115     if (result_nbytes != nbytes){       // not really an error, but unexpected
116       fprintf (stderr, "usrp1_source: short read.  Expected %d, got %d\n",
117                nbytes, result_nbytes);
118     }
119
120     copy_from_usrp_buffer (output_items,
121                            output_index,
122                            noutput_items - output_index,   // output_items_available
123                            output_items_produced,          // [out]
124                            buf,                            // usrp_buffer
125                            result_nbytes,                  // usrp_buffer_length
126                            bytes_read);                    // [out]
127
128     assert (output_index + output_items_produced <= noutput_items);
129     assert (bytes_read == result_nbytes);
130
131     output_index += output_items_produced;
132   }
133
134   return noutput_items;
135 }
136
137
138 bool
139 usrp1_source_base::set_decim_rate (unsigned int rate)
140 {
141   return d_usrp->set_decim_rate (rate);
142 }
143
144 bool
145 usrp1_source_base::set_nchannels (int nchan)
146 {
147   return d_usrp->set_nchannels (nchan);
148 }
149
150 bool
151 usrp1_source_base::set_mux (int mux)
152 {
153   return d_usrp->set_mux (mux);
154 }
155
156 bool
157 usrp1_source_base::set_rx_freq (int channel, double freq)
158 {
159   return d_usrp->set_rx_freq (channel, freq);
160 }
161
162 long
163 usrp1_source_base::fpga_master_clock_freq() const
164 {
165   return d_usrp->fpga_master_clock_freq();
166 }
167
168 long
169 usrp1_source_base::converter_rate() const
170 {
171   return d_usrp->converter_rate();
172 }
173
174 unsigned int
175 usrp1_source_base::decim_rate () const
176 {
177   return d_usrp->decim_rate ();
178 }
179
180 int
181 usrp1_source_base::nchannels () const
182 {
183   return d_usrp->nchannels ();
184 }
185
186 int
187 usrp1_source_base::mux () const
188 {
189   return d_usrp->mux ();
190 }
191
192 double
193 usrp1_source_base::rx_freq (int channel) const
194 {
195   return d_usrp->rx_freq (channel);
196 }
197
198 bool
199 usrp1_source_base::set_fpga_mode (int mode)
200 {
201   return d_usrp->set_fpga_mode (mode);
202 }
203
204 bool
205 usrp1_source_base::set_ddc_phase (int channel, int phase)
206 {
207   return d_usrp->set_ddc_phase(channel, phase);
208 }
209
210 bool
211 usrp1_source_base::set_dc_offset_cl_enable(int bits, int mask)
212 {
213   return d_usrp->set_dc_offset_cl_enable(bits, mask);
214 }
215
216 void
217 usrp1_source_base::set_verbose (bool verbose)
218 {  
219   d_usrp->set_verbose (verbose);
220 }
221
222 bool
223 usrp1_source_base::write_aux_dac (int which_dboard, int which_dac, int value)
224 {
225   return d_usrp->write_aux_dac (which_dboard, which_dac, value);
226 }
227
228 int
229 usrp1_source_base::read_aux_adc (int which_dboard, int which_adc)
230 {
231   return d_usrp->read_aux_adc (which_dboard, which_adc);
232 }
233
234 bool
235 usrp1_source_base::write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf)
236 {
237   return d_usrp->write_eeprom (i2c_addr, eeprom_offset, buf);
238 }
239
240 std::string
241 usrp1_source_base::read_eeprom (int i2c_addr, int eeprom_offset, int len)
242 {
243   return d_usrp->read_eeprom (i2c_addr, eeprom_offset, len);
244 }
245
246 bool
247 usrp1_source_base::write_i2c (int i2c_addr, const std::string buf)
248 {
249   return d_usrp->write_i2c (i2c_addr, buf);
250 }
251
252 std::string
253 usrp1_source_base::read_i2c (int i2c_addr, int len)
254 {
255   return d_usrp->read_i2c (i2c_addr, len);
256 }
257
258 bool
259 usrp1_source_base::set_pga (int which, double gain)
260 {
261   return d_usrp->set_pga (which, gain);
262 }
263
264 double
265 usrp1_source_base::pga (int which) const
266 {
267   return d_usrp->pga (which);
268 }
269
270 double
271 usrp1_source_base::pga_min () const
272 {
273   return d_usrp->pga_min ();
274 }
275
276 double
277 usrp1_source_base::pga_max () const
278 {
279   return d_usrp->pga_max ();
280 }
281
282 double
283 usrp1_source_base::pga_db_per_step () const
284 {
285   return d_usrp->pga_db_per_step ();
286 }
287
288 int
289 usrp1_source_base::daughterboard_id (int which) const
290 {
291   return d_usrp->daughterboard_id (which);
292 }
293
294
295 bool
296 usrp1_source_base::set_adc_offset (int which, int offset)
297 {
298   return d_usrp->set_adc_offset (which, offset);
299 }
300
301 bool
302 usrp1_source_base::set_dac_offset (int which, int offset, int offset_pin)
303 {
304   return d_usrp->set_dac_offset (which, offset, offset_pin);
305 }
306
307 bool
308 usrp1_source_base::set_adc_buffer_bypass (int which, bool bypass)
309 {
310   return d_usrp->set_adc_buffer_bypass (which, bypass);
311 }
312
313 std::string
314 usrp1_source_base::serial_number()
315 {
316   return d_usrp->serial_number();
317 }
318
319 bool
320 usrp1_source_base::_write_oe (int which_dboard, int value, int mask)
321 {
322   return d_usrp->_write_oe (which_dboard, value, mask);
323 }
324
325 bool
326 usrp1_source_base::write_io (int which_dboard, int value, int mask)
327 {
328   return d_usrp->write_io (which_dboard, value, mask);
329 }
330
331 int
332 usrp1_source_base::read_io (int which_dboard)
333 {
334   return d_usrp->read_io (which_dboard);
335 }
336
337
338
339
340 // internal routines...
341
342 bool
343 usrp1_source_base::_write_fpga_reg (int regno, int value)
344 {
345   return d_usrp->_write_fpga_reg (regno, value);
346 }
347
348 bool
349 usrp1_source_base::_write_fpga_reg_masked (int regno, int value, int mask)
350 {
351   return d_usrp->_write_fpga_reg_masked (regno, value, mask);
352 }
353
354 int
355 usrp1_source_base::_read_fpga_reg (int regno)
356 {
357   return d_usrp->_read_fpga_reg (regno);
358 }
359
360 bool
361 usrp1_source_base::_write_9862 (int which_codec, int regno, unsigned char value)
362 {
363   return d_usrp->_write_9862 (which_codec, regno, value);
364 }
365
366 int
367 usrp1_source_base::_read_9862 (int which_codec, int regno) const
368 {
369   return d_usrp->_read_9862 (which_codec, regno);
370 }
371
372 bool
373 usrp1_source_base::_write_spi (int optional_header, int enables,
374                                int format, std::string buf)
375 {
376   return d_usrp->_write_spi (optional_header, enables, format, buf);
377 }
378
379 std::string
380 usrp1_source_base::_read_spi (int optional_header, int enables, int format, int len)
381 {
382   return d_usrp->_read_spi (optional_header, enables, format, len);
383 }
384
385 bool
386 usrp1_source_base::set_format(unsigned int format)
387 {
388   return d_usrp->set_format(format);
389 }
390
391 unsigned int
392 usrp1_source_base::format() const
393 {
394   return d_usrp->format();
395 }
396
397 unsigned int
398 usrp1_source_base::make_format(int width, int shift, bool want_q, bool bypass_halfband)
399 {
400   return usrp_standard_rx::make_format(width, shift, want_q, bypass_halfband);
401 }
402
403 int
404 usrp1_source_base::format_width(unsigned int format)
405 {
406   return usrp_standard_rx::format_width(format);
407 }
408
409 int
410 usrp1_source_base::format_shift(unsigned int format)
411 {
412   return usrp_standard_rx::format_shift(format);
413 }
414
415 bool
416 usrp1_source_base::format_want_q(unsigned int format)
417 {
418   return usrp_standard_rx::format_want_q(format);
419 }
420
421 bool
422 usrp1_source_base::format_bypass_halfband(unsigned int format)
423 {
424   return usrp_standard_rx::format_bypass_halfband(format);
425 }