Imported Upstream version 3.0.4
[debian/gnuradio] / gr-usrp / src / usrp1_source_base.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 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_USRP1_SOURCE_BASE_H
24 #define INCLUDED_USRP1_SOURCE_BASE_H
25
26 #include <gr_sync_block.h>
27 #include <stdexcept>
28
29 class usrp_standard_rx;
30
31 /*!
32  * \brief abstract interface to Universal Software Radio Peripheral Rx path (Rev 1)
33  */
34 class usrp1_source_base : public gr_sync_block {
35  private:
36   usrp_standard_rx      *d_usrp;
37   int                    d_noverruns;
38   
39  protected:
40   usrp1_source_base (const std::string &name,
41                      gr_io_signature_sptr output_signature,
42                      int which_board,
43                      unsigned int decim_rate,
44                      int nchan,
45                      int mux,
46                      int mode,
47                      int fusb_block_size,
48                      int fusb_nblocks,
49                      const std::string fpga_filename,
50                      const std::string firmware_filename
51                      ) throw (std::runtime_error);
52
53   /*!
54    * \brief return number of usrp input bytes required to produce noutput items.
55    */
56   virtual int ninput_bytes_reqd_for_noutput_items (int noutput_items) = 0;
57
58   /*!
59    * \brief number of bytes in a low-level sample
60    */
61   unsigned int sizeof_basic_sample() const;
62
63   /*!
64    * \brief convert between native usrp format and output item format
65    *
66    * \param output_items[out]           stream(s) of output items
67    * \param output_index[in]            starting index in output_items
68    * \param output_items_available[in]  number of empty items available at item[index]
69    * \param output_items_produced[out]  number of items produced by copy
70    * \param usrp_buffer[in]             source buffer
71    * \param usrp_buffer_length[in]      number of bytes available in \p usrp_buffer
72    * \param bytes_read[out]             number of bytes read from \p usrp_buffer
73    *
74    * The copy must consume all bytes available.  That is, \p bytes_read must equal
75    * \p usrp_buffer_length.
76    */
77   virtual void copy_from_usrp_buffer (gr_vector_void_star &output_items,
78                                       int output_index,
79                                       int output_items_available,
80                                       int &output_items_produced,
81                                       const void *usrp_buffer,
82                                       int usrp_buffer_length,
83                                       int &bytes_read) = 0;
84
85  public:
86   //! magic value used on alternate register read interfaces
87   static const int READ_FAILED = -99999;
88
89   ~usrp1_source_base ();
90
91   int work (int noutput_items,
92             gr_vector_const_void_star &input_items,
93             gr_vector_void_star &output_items);
94
95   bool start();
96   bool stop();
97
98   /*!
99    * \brief Set decimator rate.  \p rate must be EVEN and in [8, 256].
100    *
101    * The final complex sample rate across the USB is
102    *   adc_freq () / decim_rate ()
103    */
104   bool set_decim_rate (unsigned int rate);
105   bool set_nchannels (int nchan);
106   bool set_mux (int mux);
107
108   /*!
109    * \brief set the center frequency of the digital down converter.
110    *
111    * \p channel must be 0.  \p freq is the center frequency in Hz.
112    * It must be in the range [-FIXME, FIXME].  The frequency specified is
113    * quantized.  Use rx_freq to retrieve the actual value used.
114    */
115   bool set_rx_freq (int channel, double freq);
116
117   /*!
118    * \brief set fpga special modes
119    */
120   bool set_fpga_mode (int mode);
121
122   void set_verbose (bool verbose);
123
124   /*!
125    * \brief Set the digital down converter phase register.
126    *
127    * \param channel     which ddc channel [0, 3]
128    * \param phase       32-bit integer phase value.
129    */
130   bool set_ddc_phase(int channel, int phase);
131
132   /*!
133    * \brief Set Programmable Gain Amplifier (PGA)
134    *
135    * \param which       which A/D [0,3]
136    * \param gain_in_db  gain value (linear in dB)
137    *
138    * gain is rounded to closest setting supported by hardware.
139    *
140    * \returns true iff sucessful.
141    *
142    * \sa pga_min(), pga_max(), pga_db_per_step()
143    */
144   bool set_pga (int which, double gain_in_db);
145
146   /*!
147    * \brief Return programmable gain amplifier gain setting in dB.
148    *
149    * \param which       which A/D [0,3]
150    */
151   double pga (int which) const;
152
153   /*!
154    * \brief Return minimum legal PGA setting in dB.
155    */
156   double pga_min () const;
157
158   /*!
159    * \brief Return maximum legal PGA setting in dB.
160    */
161   double pga_max () const;
162
163   /*!
164    * \brief Return hardware step size of PGA (linear in dB).
165    */
166   double pga_db_per_step () const;
167
168   // ACCESSORS
169
170   long fpga_master_clock_freq() const;
171   long converter_rate() const;
172   long adc_rate() const { return converter_rate(); }   // alias
173   long adc_freq() const { return converter_rate(); }   // deprecated alias
174
175   unsigned int decim_rate () const;
176   int nchannels () const;
177   int mux () const;
178   double rx_freq (int channel) const;
179   int noverruns () const { return d_noverruns; }
180
181   /*!
182    * \brief Return daughterboard ID for given Rx daughterboard slot [0,1].
183    *
184    * \return daughterboard id >= 0 if successful
185    * \return -1 if no daugherboard
186    * \return -2 if invalid EEPROM on daughterboard
187    */
188   int daughterboard_id (int which_dboard) const;
189
190   /*!
191    * \brief Write auxiliary digital to analog converter.
192    *
193    * \param which_dboard        [0,1] which d'board
194    *                            N.B., SLOT_TX_A and SLOT_RX_A share the same AUX DAC's.
195    *                            SLOT_TX_B and SLOT_RX_B share the same AUX DAC's.
196    * \param which_dac           [2,3] TX slots must use only 2 and 3.
197    * \param value               [0,4095]
198    * \returns true iff successful
199    */
200   bool write_aux_dac (int which_board, int which_dac, int value);
201
202   /*!
203    * \brief Read auxiliary analog to digital converter.
204    *
205    * \param which_dboard        [0,1] which d'board
206    * \param which_adc           [0,1]
207    * \returns value in the range [0,4095] if successful, else READ_FAILED.
208    */
209   int read_aux_adc (int which_dboard, int which_adc);
210
211   /*!
212    * \brief Write EEPROM on motherboard or any daughterboard.
213    * \param i2c_addr            I2C bus address of EEPROM
214    * \param eeprom_offset       byte offset in EEPROM to begin writing
215    * \param buf                 the data to write
216    * \returns true iff sucessful
217    */
218   bool write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf);
219
220   /*!
221    * \brief Write EEPROM on motherboard or any daughterboard.
222    * \param i2c_addr            I2C bus address of EEPROM
223    * \param eeprom_offset       byte offset in EEPROM to begin reading
224    * \param len                 number of bytes to read
225    * \returns the data read if successful, else a zero length string.
226    */
227   std::string read_eeprom (int i2c_addr, int eeprom_offset, int len);
228
229   /*!
230    * \brief Write to I2C peripheral
231    * \param i2c_addr            I2C bus address (7-bits)
232    * \param buf                 the data to write
233    * \returns true iff successful
234    * Writes are limited to a maximum of of 64 bytes.
235    */
236   bool write_i2c (int i2c_addr, const std::string buf);
237
238   /*!
239    * \brief Read from I2C peripheral
240    * \param i2c_addr            I2C bus address (7-bits)
241    * \param len                 number of bytes to read
242    * \returns the data read if successful, else a zero length string.
243    * Reads are limited to a maximum of of 64 bytes.
244    */
245   std::string read_i2c (int i2c_addr, int len);
246
247   /*!
248    * \brief Set ADC offset correction
249    * \param which       which ADC[0,3]: 0 = RX_A I, 1 = RX_A Q...
250    * \param offset      16-bit value to subtract from raw ADC input.
251    */
252   bool set_adc_offset (int which, int offset);
253
254   /*!
255    * \brief Set DAC offset correction
256    * \param which       which DAC[0,3]: 0 = TX_A I, 1 = TX_A Q...
257    * \param offset      10-bit offset value (ambiguous format:  See AD9862 datasheet).
258    * \param offset_pin  1-bit value.  If 0 offset applied to -ve differential pin;
259    *                                  If 1 offset applied to +ve differential pin.
260    */
261   bool set_dac_offset (int which, int offset, int offset_pin);
262
263   /*!
264    * \brief Control ADC input buffer
265    * \param which       which ADC[0,3]
266    * \param bypass      if non-zero, bypass input buffer and connect input
267    *                    directly to switched cap SHA input of RxPGA.
268    */
269   bool set_adc_buffer_bypass (int which, bool bypass);
270
271   /*!
272    * \brief return the usrp's serial number.
273    *
274    * \returns non-zero length string iff successful.
275    */
276   std::string serial_number();
277
278   /*!
279    * \brief Write direction register (output enables) for pins that go to daughterboard.
280    *
281    * \param which_dboard        [0,1] which d'board
282    * \param value               value to write into register
283    * \param mask                which bits of value to write into reg
284    *
285    * Each d'board has 16-bits of general purpose i/o.
286    * Setting the bit makes it an output from the FPGA to the d'board.
287    *
288    * This register is initialized based on a value stored in the
289    * d'board EEPROM.  In general, you shouldn't be using this routine
290    * without a very good reason.  Using this method incorrectly will
291    * kill your USRP motherboard and/or daughterboard.
292    */
293   bool _write_oe (int which_dboard, int value, int mask);
294
295   /*!
296    * \brief Write daughterboard i/o pin value
297    *
298    * \param which_dboard        [0,1] which d'board
299    * \param value               value to write into register
300    * \param mask                which bits of value to write into reg
301    */
302   bool write_io (int which_dboard, int value, int mask);
303
304   /*!
305    * \brief Read daughterboard i/o pin value
306    *
307    * \param which_dboard        [0,1] which d'board
308    * \returns register value if successful, else READ_FAILED
309    */
310   int read_io (int which_dboard);
311
312   /*!
313    * \brief Enable/disable automatic DC offset removal control loop in FPGA
314    *
315    * \param bits  which control loops to enable
316    * \param mask  which \p bits to pay attention to
317    *
318    * If the corresponding bit is set, enable the automatic DC
319    * offset correction control loop.
320    *
321    * <pre>
322    * The 4 low bits are significant:
323    *
324    *   ADC0 = (1 << 0)
325    *   ADC1 = (1 << 1)
326    *   ADC2 = (1 << 2)
327    *   ADC3 = (1 << 3)
328    * </pre>
329    *
330    * By default the control loop is enabled on all ADC's.
331    */
332   bool set_dc_offset_cl_enable(int bits, int mask);
333
334   /*!
335    * \brief Specify Rx data format.
336    *
337    * \param format      format specifier
338    *
339    * Rx data format control register
340    *
341    *     3                   2                   1                       
342    *   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
343    *  +-----------------------------------------+-+-+---------+-------+
344    *  |          Reserved (Must be zero)        |B|Q|  WIDTH  | SHIFT |
345    *  +-----------------------------------------+-+-+---------+-------+
346    *
347    *  SHIFT specifies arithmetic right shift [0, 15]
348    *  WIDTH specifies bit-width of I & Q samples across the USB [1, 16] (not all valid)
349    *  Q     if set deliver both I & Q, else just I
350    *  B     if set bypass half-band filter.
351    *
352    * Right now the acceptable values are:
353    *
354    *   B  Q  WIDTH  SHIFT
355    *   0  1    16     0
356    *   0  1     8     8
357    *
358    * More valid combos to come.
359    *
360    * Default value is 0x00000300  16-bits, 0 shift, deliver both I & Q.
361    */
362   bool set_format(unsigned int format);
363
364   /*!
365    * \brief return current format
366    */
367   unsigned int format () const;
368
369   static unsigned int make_format(int width=16, int shift=0,
370                                   bool want_q=true, bool bypass_halfband=false);
371   static int format_width(unsigned int format);
372   static int format_shift(unsigned int format);
373   static bool format_want_q(unsigned int format);
374   static bool format_bypass_halfband(unsigned int format);
375
376   // ----------------------------------------------------------------
377   // internal routines...  
378   // You probably shouldn't be using these...
379   // ----------------------------------------------------------------
380
381   /*!
382    * \brief Write FPGA register.
383    * \param regno       7-bit register number
384    * \param value       32-bit value
385    * \returns true iff successful
386    */
387   bool _write_fpga_reg (int regno, int value);  //< 7-bit regno, 32-bit value
388
389   /*!
390    * \brief Write FPGA register masked.
391    * \param regno       7-bit register number
392    * \param value       16-bit value
393    * \param mask                16-bit mask
394    * \returns true iff successful
395    */
396   bool _write_fpga_reg_masked (int regno, int value, int mask); //< 7-bit regno, 16-bit value, 16-bit mask
397
398   /*!
399    * \brief Read FPGA register.
400    * \param regno       7-bit register number
401    * \returns register value if successful, else READ_FAILED
402    */
403   int  _read_fpga_reg (int regno);
404
405   /*!
406    * \brief Write AD9862 register.
407    * \param which_codec 0 or 1
408    * \param regno       6-bit register number
409    * \param value       8-bit value
410    * \returns true iff successful
411    */
412   bool _write_9862 (int which_codec, int regno, unsigned char value);
413
414   /*!
415    * \brief Read AD9862 register.
416    * \param which_codec 0 or 1
417    * \param regno       6-bit register number
418    * \returns register value if successful, else READ_FAILED
419    */
420   int  _read_9862 (int which_codec, int regno) const;
421
422   /*!
423    * \brief Write data to SPI bus peripheral.
424    *
425    * \param optional_header     0,1 or 2 bytes to write before buf.
426    * \param enables             bitmask of peripherals to write. See usrp_spi_defs.h
427    * \param format              transaction format.  See usrp_spi_defs.h SPI_FMT_*
428    * \param buf                 the data to write
429    * \returns true iff successful
430    * Writes are limited to a maximum of 64 bytes.
431    *
432    * If \p format specifies that optional_header bytes are present, they are
433    * written to the peripheral immediately prior to writing \p buf.
434    */
435   bool _write_spi (int optional_header, int enables, int format, std::string buf);
436
437   /*
438    * \brief Read data from SPI bus peripheral.
439    *
440    * \param optional_header     0,1 or 2 bytes to write before buf.
441    * \param enables             bitmask of peripheral to read. See usrp_spi_defs.h
442    * \param format              transaction format.  See usrp_spi_defs.h SPI_FMT_*
443    * \param len                 number of bytes to read.  Must be in [0,64].
444    * \returns the data read if sucessful, else a zero length string.
445    *
446    * Reads are limited to a maximum of 64 bytes.
447    *
448    * If \p format specifies that optional_header bytes are present, they
449    * are written to the peripheral first.  Then \p len bytes are read from
450    * the peripheral and returned.
451    */
452   std::string _read_spi (int optional_header, int enables, int format, int len);
453 };
454
455 #endif /* INCLUDED_USRP1_SOURCE_BASE_H */