For USRP2, implement auto scaling of TX pipeline such that [-1.0 1.0] input to
[debian/gnuradio] / usrp2 / host / include / usrp2 / usrp2.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef INCLUDED_USRP2_H
20 #define INCLUDED_USRP2_H
21
22 #include <boost/shared_ptr.hpp>
23 #include <boost/utility.hpp>
24 #include <vector>
25 #include <complex>
26 #include <usrp2/rx_sample_handler.h>
27 #include <usrp2/tune_result.h>
28
29
30 /*
31  * N.B., The interfaces described here are still in flux.
32  *
33  * We will keep all the code in the tree up-to-date with regard to changes
34  * here, but reserve the right to change this on a whim.
35  */
36
37 namespace usrp2 {
38
39   /*!
40    * Structure to hold properties of USRP2 hardware devices.
41    *
42    */
43   struct props
44   {
45     std::string addr;
46     uint16_t hw_rev;
47     uint8_t fpga_md5sum[16];
48     uint8_t sw_md5sum[16];
49   };
50
51   typedef std::vector<props> props_vector_t;
52
53   /*!
54    * \brief Search the ethernet for all USRP2s or for a specific USRP2.
55    *
56    * \param ifc is the name of the OS ethernet interface (e.g., "eth0")
57    * \param mac_addr is the MAC address of the desired USRP2, or "" to search for all.
58    * mac_addr must be either a zero length string, "", or must be of the form
59    * "01:02:03:04:05:06" or "05:06".
60    *
61    * \returns a vector of properties, 1 entry for each matching USRP2 found.
62    */
63   props_vector_t find(const std::string &ifc, const std::string &mac_addr=""); 
64
65   class tune_result;
66   
67   class usrp2 : boost::noncopyable
68   {
69   public:
70     static const unsigned int MAX_CHAN = 30;
71
72     /*!
73      * Shared pointer to this class
74      */ 
75     typedef boost::shared_ptr<usrp2> sptr;
76  
77     /*! 
78      * Static function to return an instance of usrp2 as a shared pointer
79      *
80      * \param ifc   Network interface name, e.g., "eth0"
81      * \param addr  Network mac address, e.g., "01:23:45:67:89:ab", "89:ab" or "".
82      *              If \p addr is HH:HH, it's treated as if it were 00:50:c2:85:HH:HH
83      *              "" will autoselect a USRP2 if there is only a single one on the local ethernet.
84      */
85     static sptr make(const std::string &ifc, const std::string &addr="");
86
87     /*!
88      * Class destructor
89      */
90     ~usrp2();  
91
92     /*!
93      * Returns the MAC address associated with this USRP
94      */
95     std::string mac_addr();
96
97     /*
98      * ----------------------------------------------------------------
99      * Rx configuration and control
100      * ----------------------------------------------------------------
101      */
102
103     /*!
104      * Set receiver gain
105      * \param gain in dB (more or less)
106      */
107     bool set_rx_gain(double gain);
108
109     //! return minimum Rx gain 
110     double rx_gain_min();
111
112     //! return maximum Rx gain 
113     double rx_gain_max();
114
115     //! return Rx gain db_per_step
116     double rx_gain_db_per_step();
117
118     /*!
119      * Set receiver center frequency
120      */
121     bool set_rx_center_freq(double frequency, tune_result *result);
122
123     //! return minimum Rx center frequency
124     double rx_freq_min();
125
126     //! return maximum Rx center frequency
127     double rx_freq_max();
128
129     /*!
130      * Set receiver sample rate decimation
131      */
132     bool set_rx_decim(int decimation_factor);
133
134     //! Return current decimation factor
135     int rx_decim();
136
137     /*!
138      * Set receiver IQ magnitude scaling
139      */
140     bool set_rx_scale_iq(int scale_i, int scale_q);
141
142     /*!
143      * Set received sample format
144      *   
145      *    domain: complex or real
146      *      type: floating, fixed point, or raw
147      *     depth: bits per sample
148      *
149      * Sets format over the wire for samples from USRP2.
150      */
151     // bool set_rx_format(...);
152
153     /*!
154      * Start streaming receive mode.  USRP2 will send a continuous stream of
155      * DSP pipeline samples to host.  Call rx_samples(...) to access.
156      * 
157      * \param channel          Stream channel number (0-30)
158      * \param items_per_frame  Number of 32-bit items per frame.
159      */
160     bool start_rx_streaming(unsigned int channel=0, unsigned int items_per_frame=0);
161   
162     /*!
163      * Stop streaming receive mode.
164      */
165     bool stop_rx_streaming(unsigned int channel=0);
166
167     /*!
168      * \brief Receive data from the specified channel
169      * This method is used to receive all data: streaming or discrete.
170      */
171     bool rx_samples(unsigned int channel, rx_sample_handler *handler);
172
173     /*!
174      * Returns number of times receive overruns have occurred
175      */
176     unsigned int rx_overruns();
177     
178     /*!
179      * Returns total number of missing frames from overruns.
180      */
181     unsigned int rx_missing();
182
183     /*
184      * ----------------------------------------------------------------
185      * Tx configuration and control
186      * ----------------------------------------------------------------
187      */
188
189     /*!
190      * Set transmitter gain
191      */
192     bool set_tx_gain(double gain);
193
194     //! return minimum Tx gain 
195     double tx_gain_min();
196
197     //! return maximum Tx gain 
198     double tx_gain_max();
199
200     //! return Tx gain db_per_step
201     double tx_gain_db_per_step();
202
203     /*!
204      * Set transmitter center frequency
205      */
206     bool set_tx_center_freq(double frequency, tune_result *result);
207
208     //! return minimum Tx center frequency
209     double tx_freq_min();
210
211     //! return maximum Tx center frequency
212     double tx_freq_max();
213
214     /*!
215      * Set transmitter sample rate interpolation
216      */
217     bool set_tx_interp(int interpolation_factor);
218
219     //! Return current interpolation factor
220     int tx_interp();
221
222     /*
223      * \brief Calculate default scale_iq for given interpolation rate
224      */
225     void default_tx_scale_iq(int interpolation_factor, int *scale_i, int *scale_q);
226
227     /*!
228      * Set transmit IQ magnitude scaling
229      */
230     bool set_tx_scale_iq(int scale_i, int scale_q);
231
232     /*!
233      * Set transmit sample format
234      *   
235      *    domain: complex or real
236      *      type: floating, fixed point, or raw
237      *     depth: bits per sample
238      *
239      * Sets format over the wire for samples to USRP2.
240      */
241     // bool set_tx_format(...);
242
243     /*!
244      * \brief transmit complex<float> samples to USRP2
245      *
246      * \param channel specifies the channel to send them to
247      * \param samples are the samples to transmit.  They should be in the range [-1.0, +1.0]
248      * \param nsamples is the number of samples to transmit
249      * \param metadata provides the timestamp and flags
250      *
251      * The complex<float> samples are converted to the appropriate 
252      * "on the wire" representation, depending on the current USRP2
253      * configuration.  Typically, this is big-endian 16-bit I & Q.
254      */
255     bool tx_32fc(unsigned int channel,
256                  const std::complex<float> *samples,
257                  size_t nsamples,
258                  const tx_metadata *metadata);
259
260     /*!
261      * \brief transmit complex<int16_t> samples to USRP2
262      *
263      * \param channel specifies the channel to send them to
264      * \param samples are the samples to transmit
265      * \param nsamples is the number of samples to transmit
266      * \param metadata provides the timestamp and flags
267      *
268      * The complex<int16_t> samples are converted to the appropriate
269      * "on the wire" representation, depending on the current USRP2
270      * configuration.  Typically, this is big-endian 16-bit I & Q.
271      */
272     bool tx_16sc(unsigned int channel,
273                  const std::complex<int16_t> *samples,
274                  size_t nsamples,
275                  const tx_metadata *metadata);
276
277     /*!
278      * \brief transmit raw uint32_t data items to USRP2
279      *
280      * The caller is responsible for ensuring that the items are
281      * formatted appropriately for the USRP2 and its configuration.
282      * This method is used primarily by the system itself.  Users
283      * should call tx_32fc or tx_16sc instead.
284      *
285      * \param channel specifies the channel to send them to
286      * \param items are the data items to transmit
287      * \param nitems is the number of items to transmit
288      * \param metadata provides the timestamp and flags
289      */
290     bool tx_raw(unsigned int channel,
291                 const uint32_t *items,
292                 size_t nitems,
293                 const tx_metadata *metadata);
294
295     /*
296      * ----------------------------------------------------------------
297      *  miscellaneous methods
298      * ----------------------------------------------------------------
299      */
300
301     /*!
302      * \brief MIMO configuration
303      *
304      * \param flags from usrp2_mimo_config.h
305      *
306      * <pre>
307      *   one of these:
308      *
309      *     MC_WE_DONT_LOCK
310      *     MC_WE_LOCK_TO_SMA
311      *     MC_WE_LOCK_TO_MIMO
312      *
313      *   and optionally this:
314      *
315      *     MC_PROVIDE_CLK_TO_MIMO
316      * </pre>
317      */
318     bool config_mimo(int flags);
319
320
321     //! Get frequency of master oscillator in Hz
322     bool fpga_master_clock_freq(long *freq);
323
324     // Get Sampling rate of A/D converter in Hz
325     bool adc_rate(long *rate);
326
327     // Get Sampling rate of D/A converter in Hz
328     bool dac_rate(long *rate);
329
330     /*!
331      * \brief Get Tx daughterboard ID
332      *
333      * \param[out] dbid returns the daughterboard id.
334      *
335      * daughterboard id >= 0 if successful, -1 if no daugherboard installed,
336      * -2 if invalid EEPROM on daughterboard.
337      */
338     bool tx_daughterboard_id(int *dbid);
339
340     /*!
341      * \brief Get Rx daughterboard ID
342      *
343      * \param[out] dbid returns the daughterboard id.
344      *
345      * daughterboard id >= 0 if successful, -1 if no daugherboard installed,
346      * -2 if invalid EEPROM on daughterboard.
347      */
348     bool rx_daughterboard_id(int *dbid);
349
350     /*
351      * ----------------------------------------------------------------
352      *  Low level methods
353      * ----------------------------------------------------------------
354      */
355
356     /*!
357      * Burn new mac address into EEPROM on USRP2
358      *
359      * \param new_addr  Network mac address, e.g., "01:23:45:67:89:ab" or "89:ab".
360      *                  If \p addr is HH:HH, it's treated as if it were 00:50:c2:85:HH:HH
361      */
362     bool burn_mac_addr(const std::string &new_addr);
363
364     /*!
365      * Reset master time to 0 at next PPS rising edge
366      */
367     bool sync_to_pps();
368
369     /*!
370      * Read memory from Wishbone bus as 32-bit words.  Handles endian swapping if needed.
371      *
372      * \param addr      32-bit aligned address.  Only the lower 16-bits are significant.
373      * \param words     Number of 32-bit words
374      * 
375      * \returns         Vector of 32-bit read values
376      *
377      * WARNING: Attempts to read memory from addresses that do not correspond to RAM or
378      * memory-mapped peripherals may cause the USRP2 to hang, requiring a power cycle.
379      * 
380      */
381     std::vector<uint32_t> peek32(uint32_t addr, uint32_t words);
382
383     /*!
384      * Write memory to Wishbone bus as 32-bit words.  Handles endian swapping if needed.
385      *
386      * \param addr      32-bit aligned address.  Only the lower 16-bits are significant
387      * \param data      Vector of 32-bit values to write.
388      *
389      * \returns true iff successful
390      *
391      * WARNING: Attempts to read memory from addresses that do not correspond to RAM or
392      * memory-mapped peripherals may cause the USRP2 to hang, requiring a power cycle.
393      * 
394      */
395     bool poke32(uint32_t addr, const std::vector<uint32_t> &data);
396
397 #if 0   // not yet implemented
398     /*!
399      * \brief Write EEPROM on motherboard or any daughterboard.
400      * \param i2c_addr          I2C bus address of EEPROM
401      * \param eeprom_offset     byte offset in EEPROM to begin writing
402      * \param buf               the data to write
403      * \returns true iff sucessful
404      */
405     bool write_eeprom (int i2c_addr, int eeprom_offset, const std::string &buf);
406
407     /*!
408      * \brief Read EEPROM on motherboard or any daughterboard.
409      * \param i2c_addr          I2C bus address of EEPROM
410      * \param eeprom_offset     byte offset in EEPROM to begin reading
411      * \param len               number of bytes to read
412      * \returns the data read if successful, else a zero length string.
413      */
414     std::string read_eeprom (int i2c_addr, int eeprom_offset, int len);
415
416     /*!
417      * \brief Write to I2C peripheral
418      * \param i2c_addr          I2C bus address (7-bits)
419      * \param buf               the data to write
420      * \returns true iff successful
421      * Writes are limited to a maximum of of 64 bytes.
422      */
423     bool write_i2c (int i2c_addr, const std::string &buf);
424
425     /*!
426      * \brief Read from I2C peripheral
427      * \param i2c_addr          I2C bus address (7-bits)
428      * \param len               number of bytes to read
429      * \returns the data read if successful, else a zero length string.
430      * Reads are limited to a maximum of 64 bytes.
431      */
432     std::string read_i2c (int i2c_addr, int len);
433
434     /*!
435      * \brief Write data to SPI bus peripheral.
436      *
437      * \param optional_header   0,1 or 2 bytes to write before buf.
438      * \param enables           bitmask of peripherals to write. See usrp_spi_defs.h
439      * \param format            transaction format.  See usrp_spi_defs.h SPI_FMT_*
440      * \param buf               the data to write
441      * \returns true iff successful
442      * Writes are limited to a maximum of 64 bytes.
443      *
444      * If \p format specifies that optional_header bytes are present, they are
445      * written to the peripheral immediately prior to writing \p buf.
446      */
447     bool write_spi (int optional_header, int enables, int format, const std::string &buf);
448
449     /*
450      * \brief Read data from SPI bus peripheral.
451      *
452      * \param optional_header   0,1 or 2 bytes to write before buf.
453      * \param enables           bitmask of peripheral to read. See usrp_spi_defs.h
454      * \param format            transaction format.  See usrp_spi_defs.h SPI_FMT_*
455      * \param len               number of bytes to read.  Must be in [0,64].
456      * \returns the data read if sucessful, else a zero length string.
457      *
458      * Reads are limited to a maximum of 64 bytes.
459      *
460      * If \p format specifies that optional_header bytes are present, they
461      * are written to the peripheral first.  Then \p len bytes are read from
462      * the peripheral and returned.
463      */
464     std::string read_spi (int optional_header, int enables, int format, int len);
465 #endif
466
467
468     class impl;         // implementation details
469
470   private:
471     // Static function to retrieve or create usrp2 instance
472     static sptr find_existing_or_make_new(const std::string &ifc, props *p);
473
474     // Only class members can instantiate this class
475     usrp2(const std::string &ifc, props *p);
476   
477     // All private state is held in opaque pointer
478     std::auto_ptr<impl> d_impl;
479   };
480
481 };
482
483 std::ostream& operator<<(std::ostream &os, const usrp2::props &x);
484
485
486 #endif /* INCLUDED_USRP2_H */