Imported Upstream version 3.0.4
[debian/gnuradio] / gr-usrp / src / usrp1_sink_base.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2006 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_SINK_BASE_H
24 #define INCLUDED_USRP1_SINK_BASE_H
25
26 #include <gr_sync_block.h>
27 #include <stdexcept>
28
29 class usrp_standard_tx;
30
31
32 /*!
33  * \brief abstract interface to Universal Software Radio Peripheral Tx path (Rev 1)
34  */
35 class usrp1_sink_base : public gr_sync_block {
36  private:
37   usrp_standard_tx      *d_usrp;
38   int                    d_nunderruns;
39   
40  protected:
41   usrp1_sink_base (const std::string &name,
42                    gr_io_signature_sptr input_signature,
43                    int which_board,
44                    unsigned int interp_rate,
45                    int nchan,
46                    int mux,
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 convert between input item format and usrp native format
55    *
56    * \param input_items[in]             stream(s) of input items
57    * \param input_index[in]             starting index in input_items
58    * \param input_items_available[in]   number of items available starting at item[index]
59    * \param input_items_consumed[out]   number of input items consumed by copy
60    * \param usrp_buffer[out]            destination buffer
61    * \param usrp_buffer_length[in]      \p usrp_buffer length in bytes
62    * \param bytes_written[out]          number of bytes written into \p usrp_buffer
63    */
64   virtual void copy_to_usrp_buffer (gr_vector_const_void_star &input_items,
65                                     int  input_index,
66                                     int  input_items_available,
67                                     int  &input_items_consumed,
68                                     void *usrp_buffer,
69                                     int  usrp_buffer_length,
70                                     int  &bytes_written) = 0;
71
72  public:
73   //! magic value used on alternate register read interfaces
74   static const int READ_FAILED = -99999;
75
76
77   ~usrp1_sink_base ();
78
79   int work (int noutput_items,
80             gr_vector_const_void_star &input_items,
81             gr_vector_void_star &output_items);
82
83   bool start();
84   bool stop();
85
86   /*!
87    * \brief Set interpolator rate.  \p rate must be in [4, 1024] and a multiple of 4.
88    *
89    * The final complex sample rate across the USB is
90    *   dac_freq () / interp_rate () * nchannels ()
91    */
92   bool set_interp_rate (unsigned int rate);
93   bool set_nchannels (int nchan);
94   bool set_mux (int mux);
95
96   /*!
97    * \brief set the frequency of the digital up converter.
98    *
99    * \p channel must be 0.  \p freq is the center frequency in Hz.
100    * It must be in the range [-44M, 44M].  The frequency specified is
101    * quantized.  Use tx_freq to retrieve the actual value used.
102    */
103   bool set_tx_freq (int channel, double freq);
104
105   void set_verbose (bool verbose);
106
107   /*!
108    * \brief Set Programmable Gain Amplifier (PGA)
109    *
110    * \param which       which D/A [0,3]
111    * \param gain_in_db  gain value (linear in dB)
112    *
113    * gain is rounded to closest setting supported by hardware.
114    * Note that DAC 0 and DAC 1 share a gain setting as do DAC 2 and DAC 3.
115    * Setting DAC 0 affects DAC 1 and vice versa.  Same with DAC 2 and DAC 3.
116    *
117    * \returns true iff sucessful.
118    *
119    * \sa pga_min(), pga_max(), pga_db_per_step()
120    */
121   bool set_pga (int which, double gain_in_db);
122
123   /*!
124    * \brief Return programmable gain amplifier gain in dB.
125    *
126    * \param which       which D/A [0,3]
127    */
128   double pga (int which) const;
129
130   /*!
131    * \brief Return minimum legal PGA gain in dB.
132    */
133   double pga_min () const;
134
135   /*!
136    * \brief Return maximum legal PGA gain in dB.
137    */
138   double pga_max () const;
139
140   /*!
141    * \brief Return hardware step size of PGA (linear in dB).
142    */
143   double pga_db_per_step () const;
144
145
146   // ACCESSORS
147
148   long fpga_master_clock_freq() const;
149   long converter_rate() const;
150   long dac_rate() const { return converter_rate(); }    // alias
151   long dac_freq() const { return converter_rate(); }    // deprecated alias
152
153   unsigned int interp_rate () const;
154   int nchannels () const;
155   int mux () const;
156   double tx_freq (int channel) const;
157   int nunderruns () const { return d_nunderruns; }
158
159   /*!
160    * \brief Return daughterboard ID for given Rx daughterboard slot [0,1].
161    *
162    * \return daughterboard id >= 0 if successful
163    * \return -1 if no daugherboard
164    * \return -2 if invalid EEPROM on daughterboard
165    */
166   int daughterboard_id (int which_dboard) const;
167
168   /*!
169    * \brief Write auxiliary digital to analog converter.
170    *
171    * \param which_dboard        [0,1] which d'board
172    *                            N.B., SLOT_TX_A and SLOT_RX_A share the same AUX DAC's.
173    *                            SLOT_TX_B and SLOT_RX_B share the same AUX DAC's.
174    * \param which_dac           [2,3] TX slots must use only 2 and 3.
175    * \param value               [0,4095]
176    * \returns true iff successful
177    */
178   bool write_aux_dac (int which_board, int which_dac, int value);
179
180   /*!
181    * \brief Read auxiliary analog to digital converter.
182    *
183    * \param which_dboard        [0,1] which d'board
184    * \param which_adc           [0,1]
185    * \returns value in the range [0,4095] if successful, else READ_FAILED.
186    */
187   int read_aux_adc (int which_dboard, int which_adc);
188
189   /*!
190    * \brief Write EEPROM on motherboard or any daughterboard.
191    * \param i2c_addr            I2C bus address of EEPROM
192    * \param eeprom_offset       byte offset in EEPROM to begin writing
193    * \param buf                 the data to write
194    * \returns true iff sucessful
195    */
196   bool write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf);
197
198   /*!
199    * \brief Write EEPROM on motherboard or any daughterboard.
200    * \param i2c_addr            I2C bus address of EEPROM
201    * \param eeprom_offset       byte offset in EEPROM to begin reading
202    * \param len                 number of bytes to read
203    * \returns the data read if successful, else a zero length string.
204    */
205   std::string read_eeprom (int i2c_addr, int eeprom_offset, int len);
206
207   /*!
208    * \brief Write to I2C peripheral
209    * \param i2c_addr            I2C bus address (7-bits)
210    * \param buf                 the data to write
211    * \returns true iff successful
212    * Writes are limited to a maximum of of 64 bytes.
213    */
214   bool write_i2c (int i2c_addr, const std::string buf);
215
216   /*!
217    * \brief Read from I2C peripheral
218    * \param i2c_addr            I2C bus address (7-bits)
219    * \param len                 number of bytes to read
220    * \returns the data read if successful, else a zero length string.
221    * Reads are limited to a maximum of of 64 bytes.
222    */
223   std::string read_i2c (int i2c_addr, int len);
224
225   /*!
226    * \brief Set ADC offset correction
227    * \param which       which ADC[0,3]: 0 = RX_A I, 1 = RX_A Q...
228    * \param offset      16-bit value to subtract from raw ADC input.
229    */
230   bool set_adc_offset (int which, int offset);
231
232   /*!
233    * \brief Set DAC offset correction
234    * \param which       which DAC[0,3]: 0 = TX_A I, 1 = TX_A Q...
235    * \param offset      10-bit offset value (ambiguous format:  See AD9862 datasheet).
236    * \param offset_pin  1-bit value.  If 0 offset applied to -ve differential pin;
237    *                                  If 1 offset applied to +ve differential pin.
238    */
239   bool set_dac_offset (int which, int offset, int offset_pin);
240
241   /*!
242    * \brief Control ADC input buffer
243    * \param which       which ADC[0,3]
244    * \param bypass      if non-zero, bypass input buffer and connect input
245    *                    directly to switched cap SHA input of RxPGA.
246    */
247   bool set_adc_buffer_bypass (int which, bool bypass);
248
249   /*!
250    * \brief return the usrp's serial number.
251    *
252    * \returns non-zero length string iff successful.
253    */
254   std::string serial_number();
255
256   /*!
257    * \brief Write direction register (output enables) for pins that go to daughterboard.
258    *
259    * \param which_dboard        [0,1] which d'board
260    * \param value               value to write into register
261    * \param mask                which bits of value to write into reg
262    *
263    * Each d'board has 16-bits of general purpose i/o.
264    * Setting the bit makes it an output from the FPGA to the d'board.
265    *
266    * This register is initialized based on a value stored in the
267    * d'board EEPROM.  In general, you shouldn't be using this routine
268    * without a very good reason.  Using this method incorrectly will
269    * kill your USRP motherboard and/or daughterboard.
270    */
271   bool _write_oe (int which_dboard, int value, int mask);
272
273   /*!
274    * \brief Write daughterboard i/o pin value
275    *
276    * \param which_dboard        [0,1] which d'board
277    * \param value               value to write into register
278    * \param mask                which bits of value to write into reg
279    */
280   bool write_io (int which_dboard, int value, int mask);
281
282   /*!
283    * \brief Read daughterboard i/o pin value
284    *
285    * \param which_dboard        [0,1] which d'board
286    * \returns register value if successful, else READ_FAILED
287    */
288   int read_io (int which_dboard);
289
290   //
291   // internal routines...
292   // You probably shouldn't be using these...
293   //
294   /*!
295    * \brief Write FPGA register.
296    * \param regno       7-bit register number
297    * \param value       32-bit value
298    * \returns true iff successful
299    */
300   bool _write_fpga_reg (int regno, int value);  //< 7-bit regno, 32-bit value
301
302   /*!
303    * \brief Read FPGA register.
304    * \param regno       7-bit register number
305    * \returns register value if successful, else READ_FAILED
306    */
307   int  _read_fpga_reg (int regno);
308
309   /*!
310    * \brief Write AD9862 register.
311    * \param which_codec 0 or 1
312    * \param regno       6-bit register number
313    * \param value       8-bit value
314    * \returns true iff successful
315    */
316   bool _write_9862 (int which_codec, int regno, unsigned char value);
317
318   /*!
319    * \brief Read AD9862 register.
320    * \param which_codec 0 or 1
321    * \param regno       6-bit register number
322    * \returns register value if successful, else READ_FAILED
323    */
324   int  _read_9862 (int which_codec, int regno) const;
325
326   /*!
327    * \brief Write data to SPI bus peripheral.
328    *
329    * \param optional_header     0,1 or 2 bytes to write before buf.
330    * \param enables             bitmask of peripherals to write. See usrp_spi_defs.h
331    * \param format              transaction format.  See usrp_spi_defs.h SPI_FMT_*
332    * \param buf                 the data to write
333    * \returns true iff successful
334    * Writes are limited to a maximum of 64 bytes.
335    *
336    * If \p format specifies that optional_header bytes are present, they are
337    * written to the peripheral immediately prior to writing \p buf.
338    */
339   bool _write_spi (int optional_header, int enables, int format, std::string buf);
340
341   /*
342    * \brief Read data from SPI bus peripheral.
343    *
344    * \param optional_header     0,1 or 2 bytes to write before buf.
345    * \param enables             bitmask of peripheral to read. See usrp_spi_defs.h
346    * \param format              transaction format.  See usrp_spi_defs.h SPI_FMT_*
347    * \param len                 number of bytes to read.  Must be in [0,64].
348    * \returns the data read if sucessful, else a zero length string.
349    *
350    * Reads are limited to a maximum of 64 bytes.
351    *
352    * If \p format specifies that optional_header bytes are present, they
353    * are written to the peripheral first.  Then \p len bytes are read from
354    * the peripheral and returned.
355    */
356   std::string _read_spi (int optional_header, int enables, int format, int len);
357 };
358
359 #endif /* INCLUDED_USRP1_SINK_BASE_H */