Imported Upstream version 3.2.2
[debian/gnuradio] / gr-usrp / src / usrp_source_base.h
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 #ifndef INCLUDED_USRP_SOURCE_BASE_H
24 #define INCLUDED_USRP_SOURCE_BASE_H
25
26 #include <usrp_base.h>
27 #include <stdexcept>
28 #include <usrp_tune_result.h>
29 #include <usrp_dbid.h>
30
31 class usrp_standard_rx;
32
33 /*!
34  * \brief abstract interface to Universal Software Radio Peripheral Rx path (Rev 1)
35  */
36 class usrp_source_base : public usrp_base {
37  private:
38   boost::shared_ptr<usrp_standard_rx>   d_usrp;
39   int                    d_noverruns;
40   
41  protected:
42   usrp_source_base (const std::string &name,
43                      gr_io_signature_sptr output_signature,
44                      int which_board,
45                      unsigned int decim_rate,
46                      int nchan,
47                      int mux,
48                      int mode,
49                      int fusb_block_size,
50                      int fusb_nblocks,
51                      const std::string fpga_filename,
52                      const std::string firmware_filename
53                      ) throw (std::runtime_error);
54
55   /*!
56    * \brief return number of usrp input bytes required to produce noutput items.
57    */
58   virtual int ninput_bytes_reqd_for_noutput_items (int noutput_items) = 0;
59
60   /*!
61    * \brief number of bytes in a low-level sample
62    */
63   unsigned int sizeof_basic_sample() const;
64
65   /*!
66    * \brief convert between native usrp format and output item format
67    *
68    * \param[out] output_items           stream(s) of output items
69    * \param[in] output_index            starting index in output_items
70    * \param[in] output_items_available  number of empty items available at item[index]
71    * \param[out] output_items_produced  number of items produced by copy
72    * \param[in] usrp_buffer             source buffer
73    * \param[in] usrp_buffer_length      number of bytes available in \p usrp_buffer
74    * \param[out] bytes_read             number of bytes read from \p usrp_buffer
75    *
76    * The copy must consume all bytes available.  That is, \p bytes_read must equal
77    * \p usrp_buffer_length.
78    */
79   virtual void copy_from_usrp_buffer (gr_vector_void_star &output_items,
80                                       int output_index,
81                                       int output_items_available,
82                                       int &output_items_produced,
83                                       const void *usrp_buffer,
84                                       int usrp_buffer_length,
85                                       int &bytes_read) = 0;
86
87  public:
88   ~usrp_source_base ();
89
90   int work (int noutput_items,
91             gr_vector_const_void_star &input_items,
92             gr_vector_void_star &output_items);
93
94   /*!
95    * \brief Set decimator rate.  \p rate must be EVEN and in [8, 256].
96    *
97    * The final complex sample rate across the USB is
98    *   adc_freq () / decim_rate ()
99    */
100   bool set_decim_rate (unsigned int rate);
101   bool set_nchannels (int nchan);
102   bool set_mux (int mux);
103   int determine_rx_mux_value(usrp_subdev_spec ss);
104   int determine_rx_mux_value(usrp_subdev_spec ss_a, usrp_subdev_spec ss_b);
105
106   /*!
107    * \brief set the center frequency of the digital down converter.
108    *
109    * \p channel must be 0.  \p freq is the center frequency in Hz.
110    * It must be in the range [-FIXME, FIXME].  The frequency specified is
111    * quantized.  Use rx_freq to retrieve the actual value used.
112    */
113   bool set_rx_freq (int channel, double freq);
114
115   /*!
116    * \brief set fpga special modes
117    */
118   bool set_fpga_mode (int mode);
119
120   /*!
121    * \brief Set the digital down converter phase register.
122    *
123    * \param channel     which ddc channel [0, 3]
124    * \param phase       32-bit integer phase value.
125    */
126   bool set_ddc_phase(int channel, int phase);
127
128   long adc_rate() const { return converter_rate(); }   // alias
129   long adc_freq() const { return converter_rate(); }   // deprecated alias
130
131   unsigned int decim_rate () const;
132   int nchannels () const;
133   int mux () const;
134   double rx_freq (int channel) const;
135   int noverruns () const { return d_noverruns; }
136
137   bool has_rx_halfband();
138   bool has_tx_halfband();
139   int nddcs();
140   int nducs();
141
142   /*!
143    * \brief Called to enable drivers, etc for i/o devices.
144    *
145    * This allows a block to enable an associated driver to begin
146    * transfering data just before we start to execute the scheduler.
147    * The end result is that this reduces latency in the pipeline when
148    * dealing with audio devices, usrps, etc.
149    */
150   bool start();
151
152   /*!
153    * \brief Called to disable drivers, etc for i/o devices.
154    */
155   bool stop();
156
157   /*!
158    * \brief Specify Rx data format.
159    *
160    * \param format      format specifier
161    *
162    * Rx data format control register
163    *
164    *     3                   2                   1                       
165    *   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
166    *  +-----------------------------------------+-+-+---------+-------+
167    *  |          Reserved (Must be zero)        |B|Q|  WIDTH  | SHIFT |
168    *  +-----------------------------------------+-+-+---------+-------+
169    *
170    *  SHIFT specifies arithmetic right shift [0, 15]
171    *  WIDTH specifies bit-width of I & Q samples across the USB [1, 16] (not all valid)
172    *  Q     if set deliver both I & Q, else just I
173    *  B     if set bypass half-band filter.
174    *
175    * Right now the acceptable values are:
176    *
177    *   B  Q  WIDTH  SHIFT
178    *   0  1    16     0
179    *   0  1     8     8
180    *
181    * More valid combos to come.
182    *
183    * Default value is 0x00000300  16-bits, 0 shift, deliver both I & Q.
184    */
185   bool set_format(unsigned int format);
186
187   /*!
188    * \brief return current format
189    */
190   unsigned int format () const;
191
192   static unsigned int make_format(int width=16, int shift=0,
193                                   bool want_q=true, bool bypass_halfband=false);
194   static int format_width(unsigned int format);
195   static int format_shift(unsigned int format);
196   static bool format_want_q(unsigned int format);
197   static bool format_bypass_halfband(unsigned int format);
198
199   /*!
200    * \brief High-level "tune" method.  Works for the single channel case.
201    *
202    * This method adjusts both the daughterboard LO and the DDC so that
203    * target_freq ends up at DC in the complex baseband samples.
204    *
205    * \param chan  which DDC channel we're controlling (almost always 0).
206    * \param db    the daughterboard we're controlling.
207    * \param target_freq the RF frequency we want at DC in the complex baseband.
208    * \param[out] result details how the hardware was configured.
209    *
210    * \returns true iff everything was successful.
211    */
212   bool tune(int chan, db_base_sptr db, double target_freq, usrp_tune_result *result);
213
214   /*!
215    * \brief Select suitable Rx daughterboard
216    */
217   usrp_subdev_spec pick_rx_subdevice();
218 };
219
220 #endif /* INCLUDED_USRP_SOURCE_BASE_H */