Imported Upstream version 3.0
[debian/gnuradio] / usrp / host / lib / usrp_standard.h
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 2, 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_STANDARD_H
24 #define INCLUDED_USRP_STANDARD_H
25
26 #include <usrp_basic.h>
27
28 class usrp_standard_common
29 {
30   int                   d_fpga_caps;            // capability register val
31
32 protected:
33   usrp_standard_common(usrp_basic *parent);
34
35 public:
36   /*!
37    *\brief does the FPGA implement the final Rx half-band filter?
38    * If it doesn't, the maximum decimation factor with proper gain 
39    * is 1/2 of what it would otherwise be.
40    */
41   bool has_rx_halfband() const;
42
43   /*!
44    * \brief number of digital downconverters implemented in the FPGA
45    * This will be 0, 1, 2 or 4.
46    */
47   int nddcs() const;
48
49   /*!
50    *\brief does the FPGA implement the initial Tx half-band filter?
51    */
52   bool has_tx_halfband() const;
53
54   /*!
55    * \brief number of digital upconverters implemented in the FPGA
56    * This will be 0, 1, or 2.
57    */
58   int nducs() const;
59 };
60
61 /*!
62  * \brief standard usrp RX class.  
63  *
64  * Assumes digital down converter in FPGA
65  */
66 class usrp_standard_rx : public usrp_basic_rx, usrp_standard_common
67 {
68  private:
69   static const int      MAX_CHAN = 4;
70   unsigned int          d_decim_rate;
71   int                   d_nchan;
72   int                   d_sw_mux;
73   int                   d_hw_mux;
74   double                d_rx_freq[MAX_CHAN];
75
76  protected:
77   usrp_standard_rx (int which_board,
78                     unsigned int decim_rate,
79                     int nchan = 1,
80                     int mux = -1,
81                     int mode = 0,
82                     int fusb_block_size = 0,
83                     int fusb_nblocks = 0,
84                     const std::string fpga_filename = "",
85                     const std::string firmware_filename = ""
86                     );  // throws if trouble
87
88   bool write_hw_mux_reg ();
89
90  public:
91
92   enum {
93     FPGA_MODE_NORMAL     = 0x00,
94     FPGA_MODE_LOOPBACK   = 0x01,
95     FPGA_MODE_COUNTING   = 0x02,
96     FPGA_MODE_COUNTING_32BIT   = 0x04
97   };
98
99   ~usrp_standard_rx ();
100
101   /*!
102    * \brief invokes constructor, returns instance or 0 if trouble
103    *
104    * \param which_board      Which USRP board on usb (not particularly useful; use 0)
105    * \param fusb_block_size  fast usb xfer block size.  Must be a multiple of 512. 
106    *                         Use zero for a reasonable default.
107    * \param fusb_nblocks     number of fast usb URBs to allocate.  Use zero for a reasonable default. 
108    */
109   static usrp_standard_rx *make (int which_board,
110                                  unsigned int decim_rate,
111                                  int nchan = 1,
112                                  int mux = -1,
113                                  int mode = 0,
114                                  int fusb_block_size = 0,
115                                  int fusb_nblocks = 0,
116                                  const std::string fpga_filename = "",
117                                  const std::string firmware_filename = ""
118                                  );
119   /*!
120    * \brief Set decimator rate.  \p rate MUST BE EVEN and in [8, 256].
121    *
122    * The final complex sample rate across the USB is
123    *   adc_freq () / decim_rate () * nchannels ()
124    */
125   bool set_decim_rate  (unsigned int rate);
126
127   /*!
128    * \brief Set number of active channels.  \p nchannels must be 1, 2 or 4.
129    *
130    * The final complex sample rate across the USB is
131    *   adc_freq () / decim_rate () * nchannels ()
132    */
133   bool set_nchannels (int nchannels);
134
135   /*!
136    * \brief Set input mux configuration.
137    *
138    * This determines which ADC (or constant zero) is connected to 
139    * each DDC input.  There are 4 DDCs.  Each has two inputs.
140    *
141    * <pre>
142    * Mux value:
143    *
144    *    3                   2                   1                       
145    *  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
146    * +-------+-------+-------+-------+-------+-------+-------+-------+
147    * |   Q3  |   I3  |   Q2  |   I2  |   Q1  |   I1  |   Q0  |   I0  |
148    * +-------+-------+-------+-------+-------+-------+-------+-------+
149    *
150    * Each 4-bit I field is either 0,1,2,3
151    * Each 4-bit Q field is either 0,1,2,3 or 0xf (input is const zero)
152    * All Q's must be 0xf or none of them may be 0xf
153    * </pre>
154    */
155   bool set_mux  (int mux);
156
157   /*!
158    * \brief set the frequency of the digital down converter.
159    *
160    * \p channel must be in the range [0,3].  \p freq is the center
161    * frequency in Hz.  \p freq may be either negative or postive.
162    * The frequency specified is quantized.  Use rx_freq to retrieve
163    * the actual value used.
164    */
165   bool set_rx_freq (int channel, double freq);  
166
167   /*!
168    * \brief set fpga mode
169    */
170   bool set_fpga_mode (int mode);
171
172   /*!
173    * \brief Set the digital down converter phase register.
174    *
175    * \param channel     which ddc channel [0, 3]
176    * \param phase       32-bit integer phase value.
177    */
178   bool set_ddc_phase(int channel, int phase);
179
180   /*!
181    * \brief Specify Rx data format.
182    *
183    * \param format      format specifier
184    *
185    * Rx data format control register
186    *
187    *     3                   2                   1                       
188    *   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
189    *  +-----------------------------------------+-+-+---------+-------+
190    *  |          Reserved (Must be zero)        |B|Q|  WIDTH  | SHIFT |
191    *  +-----------------------------------------+-+-+---------+-------+
192    *
193    *  SHIFT specifies arithmetic right shift [0, 15]
194    *  WIDTH specifies bit-width of I & Q samples across the USB [1, 16] (not all valid)
195    *  Q     if set deliver both I & Q, else just I
196    *  B     if set bypass half-band filter.
197    *
198    * Right now the acceptable values are:
199    *
200    *   B  Q  WIDTH  SHIFT
201    *   0  1    16     0
202    *   0  1     8     8
203    *
204    * More valid combos to come.
205    *
206    * Default value is 0x00000300  16-bits, 0 shift, deliver both I & Q.
207    */
208   bool set_format(unsigned int format);
209
210   static unsigned int make_format(int width=16, int shift=0,
211                                   bool want_q=true, bool bypass_halfband=false);
212   static int format_width(unsigned int format);
213   static int format_shift(unsigned int format);
214   static bool format_want_q(unsigned int format);
215   static bool format_bypass_halfband(unsigned int format);
216
217   // ACCESSORS
218   unsigned int decim_rate () const;
219   double rx_freq (int channel) const;
220   int nchannels () const;
221   int mux () const;
222   unsigned int format () const;
223
224   // called in base class to derived class order
225   bool start ();
226   bool stop ();
227 };
228
229 // ----------------------------------------------------------------
230
231 /*!
232  * \brief standard usrp TX class.
233  *
234  * Uses digital upconverter (coarse & fine modulators) in AD9862...
235  */
236 class usrp_standard_tx : public usrp_basic_tx, usrp_standard_common
237 {
238  public:
239   enum coarse_mod_t {
240     CM_NEG_FDAC_OVER_4,         // -32 MHz
241     CM_NEG_FDAC_OVER_8,         // -16 MHz
242     CM_OFF,
243     CM_POS_FDAC_OVER_8,         // +16 MHz
244     CM_POS_FDAC_OVER_4          // +32 MHz
245   };
246
247  protected:
248   static const int      MAX_CHAN = 2;
249   unsigned int          d_interp_rate;
250   int                   d_nchan;
251   int                   d_sw_mux;
252   int                   d_hw_mux;
253   double                d_tx_freq[MAX_CHAN];
254   coarse_mod_t          d_coarse_mod[MAX_CHAN];
255   unsigned char         d_tx_modulator_shadow[MAX_CHAN];
256
257   virtual bool set_coarse_modulator (int channel, coarse_mod_t cm);
258   usrp_standard_tx::coarse_mod_t coarse_modulator (int channel) const;
259
260  protected:
261   usrp_standard_tx (int which_board,
262                     unsigned int interp_rate,
263                     int nchan = 1,
264                     int mux = -1,
265                     int fusb_block_size = 0,
266                     int fusb_nblocks = 0,
267                     const std::string fpga_filename = "",
268                     const std::string firmware_filename = ""
269                     );  // throws if trouble
270
271   bool write_hw_mux_reg ();
272
273  public:
274   ~usrp_standard_tx ();
275
276   /*!
277    * \brief invokes constructor, returns instance or 0 if trouble
278    *
279    * \param which_board      Which USRP board on usb (not particularly useful; use 0)
280    * \param fusb_block_size  fast usb xfer block size.  Must be a multiple of 512. 
281    *                         Use zero for a reasonable default.
282    * \param fusb_nblocks     number of fast usb URBs to allocate.  Use zero for a reasonable default. 
283    */
284   static usrp_standard_tx *make (int which_board,
285                                  unsigned int interp_rate,
286                                  int nchan = 1,
287                                  int mux = -1,
288                                  int fusb_block_size = 0,
289                                  int fusb_nblocks = 0,
290                                  const std::string fpga_filename = "",
291                                  const std::string firmware_filename = ""
292                                  );
293
294   /*!
295    * \brief Set interpolator rate.  \p rate must be in [4, 512] and a multiple of 4.
296    *
297    * The final complex sample rate across the USB is
298    *   dac_freq () / interp_rate () * nchannels ()
299    */
300   virtual bool set_interp_rate (unsigned int rate);
301
302   /*!
303    * \brief Set number of active channels.  \p nchannels must be 1 or 2.
304    *
305    * The final complex sample rate across the USB is
306    *   dac_freq () / decim_rate () * nchannels ()
307    */
308   bool set_nchannels  (int nchannels);
309
310   /*!
311    * \brief Set output mux configuration.
312    *
313    * <pre>
314    *     3                   2                   1                       
315    *   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
316    *  +-------------------------------+-------+-------+-------+-------+
317    *  |                               | DAC3  | DAC2  | DAC1  |  DAC0 |
318    *  +-------------------------------+-------+-------+-------+-------+
319    * 
320    *  There are two interpolators with complex inputs and outputs.
321    *  There are four DACs.
322    * 
323    *  Each 4-bit DACx field specifies the source for the DAC and
324    *  whether or not that DAC is enabled.  Each subfield is coded
325    *  like this: 
326    * 
327    *     3 2 1 0
328    *    +-+-----+
329    *    |E|  N  |
330    *    +-+-----+
331    * 
332    *  Where E is set if the DAC is enabled, and N specifies which
333    *  interpolator output is connected to this DAC.
334    * 
335    *   N   which interp output
336    *  ---  -------------------
337    *   0   chan 0 I
338    *   1   chan 0 Q
339    *   2   chan 1 I
340    *   3   chan 1 Q
341    * </pre>
342    */
343   bool set_mux  (int mux);
344
345   /*!
346    * \brief set the frequency of the digital up converter.
347    *
348    * \p channel must be in the range [0,1].  \p freq is the center
349    * frequency in Hz.  It must be in the range [-44M, 44M].
350    * The frequency specified is quantized.  Use tx_freq to retrieve
351    * the actual value used.
352    */
353   virtual bool set_tx_freq (int channel, double freq);  // chan: [0,1]
354
355   // ACCESSORS
356   unsigned int interp_rate () const;
357   double tx_freq (int channel) const;
358   int nchannels () const;
359   int mux () const;
360
361   // called in base class to derived class order
362   bool start ();
363   bool stop ();
364 };
365
366 #endif /* INCLUDED_USRP_STANDARD_H */