Merged r10712:10765 from jcorgan/gpio into trunk. Adds out-of-band and streaming...
[debian/gnuradio] / usrp2 / host / include / usrp2 / usrp2.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,2009 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   // FIXME: get from firmware include
68   static const int GPIO_TX_BANK = 0;
69   static const int GPIO_RX_BANK = 1;
70   
71   class usrp2 : boost::noncopyable
72   {
73   public:
74     static const unsigned int MAX_CHAN = 30;
75
76     /*!
77      * Shared pointer to this class
78      */ 
79     typedef boost::shared_ptr<usrp2> sptr;
80  
81     /*! 
82      * Static function to return an instance of usrp2 as a shared pointer
83      *
84      * \param ifc   Network interface name, e.g., "eth0"
85      * \param addr  Network mac address, e.g., "01:23:45:67:89:ab", "89:ab" or "".
86      *              If \p addr is HH:HH, it's treated as if it were 00:50:c2:85:HH:HH
87      *              "" will autoselect a USRP2 if there is only a single one on the local ethernet.
88      */
89     static sptr make(const std::string &ifc, const std::string &addr="");
90
91     /*!
92      * Class destructor
93      */
94     ~usrp2();  
95
96     /*!
97      * Returns the MAC address associated with this USRP
98      */
99     std::string mac_addr();
100
101     /*!
102      * Returns the GbE interface name associated with this USRP
103      */
104     std::string interface_name();
105
106     /*
107      * ----------------------------------------------------------------
108      * Rx configuration and control
109      * ----------------------------------------------------------------
110      */
111
112     /*!
113      * Set receiver gain
114      * \param gain in dB (more or less)
115      */
116     bool set_rx_gain(double gain);
117
118     //! return minimum Rx gain 
119     double rx_gain_min();
120
121     //! return maximum Rx gain 
122     double rx_gain_max();
123
124     //! return Rx gain db_per_step
125     double rx_gain_db_per_step();
126
127     /*!
128      * \brief Set receive daughterboard LO offset frequency
129      */
130     bool set_rx_lo_offset(double frequency);
131
132     /*!
133      * Set receiver center frequency
134      */
135     bool set_rx_center_freq(double frequency, tune_result *result);
136
137     //! return minimum Rx center frequency
138     double rx_freq_min();
139
140     //! return maximum Rx center frequency
141     double rx_freq_max();
142
143     /*!
144      * Set receiver sample rate decimation
145      */
146     bool set_rx_decim(int decimation_factor);
147
148     //! Return current decimation factor
149     int rx_decim();
150
151     /*!
152      * Set receiver IQ magnitude scaling
153      */
154     bool set_rx_scale_iq(int scale_i, int scale_q);
155
156     /*!
157      * Set received sample format
158      *   
159      *    domain: complex or real
160      *      type: floating, fixed point, or raw
161      *     depth: bits per sample
162      *
163      * Sets format over the wire for samples from USRP2.
164      */
165     // bool set_rx_format(...);
166
167     /*!
168      * Start streaming receive mode.  USRP2 will send a continuous stream of
169      * DSP pipeline samples to host.  Call rx_samples(...) to access.
170      * 
171      * \param channel          Stream channel number (0-30)
172      * \param items_per_frame  Number of 32-bit items per frame.
173      */
174     bool start_rx_streaming(unsigned int channel=0, unsigned int items_per_frame=0);
175   
176     /*!
177      * Stop streaming receive mode.
178      */
179     bool stop_rx_streaming(unsigned int channel=0);
180
181     /*!
182      * \brief Receive data from the specified channel
183      * This method is used to receive all data: streaming or discrete.
184      */
185     bool rx_samples(unsigned int channel, rx_sample_handler *handler);
186
187     /*!
188      * Returns number of times receive overruns have occurred
189      */
190     unsigned int rx_overruns();
191     
192     /*!
193      * Returns total number of missing frames from overruns.
194      */
195     unsigned int rx_missing();
196
197     /*
198      * ----------------------------------------------------------------
199      * Tx configuration and control
200      * ----------------------------------------------------------------
201      */
202
203     /*!
204      * Set transmitter gain
205      */
206     bool set_tx_gain(double gain);
207
208     //! return minimum Tx gain 
209     double tx_gain_min();
210
211     //! return maximum Tx gain 
212     double tx_gain_max();
213
214     //! return Tx gain db_per_step
215     double tx_gain_db_per_step();
216
217     /*
218      * \brief Set transmit daughterboard LO offset frequency
219      */
220     bool set_tx_lo_offset(double frequency);
221
222     /*!
223      * Set transmitter center frequency
224      */
225     bool set_tx_center_freq(double frequency, tune_result *result);
226
227     //! return minimum Tx center frequency
228     double tx_freq_min();
229
230     //! return maximum Tx center frequency
231     double tx_freq_max();
232
233     /*!
234      * Set transmitter sample rate interpolation
235      */
236     bool set_tx_interp(int interpolation_factor);
237
238     //! Return current interpolation factor
239     int tx_interp();
240
241     /*
242      * \brief Calculate default scale_iq for given interpolation rate
243      */
244     void default_tx_scale_iq(int interpolation_factor, int *scale_i, int *scale_q);
245
246     /*!
247      * Set transmit IQ magnitude scaling
248      */
249     bool set_tx_scale_iq(int scale_i, int scale_q);
250
251     /*!
252      * Set transmit sample format
253      *   
254      *    domain: complex or real
255      *      type: floating, fixed point, or raw
256      *     depth: bits per sample
257      *
258      * Sets format over the wire for samples to USRP2.
259      */
260     // bool set_tx_format(...);
261
262     /*!
263      * \brief transmit complex<float> samples to USRP2
264      *
265      * \param channel specifies the channel to send them to
266      * \param samples are the samples to transmit.  They should be in the range [-1.0, +1.0]
267      * \param nsamples is the number of samples to transmit
268      * \param metadata provides the timestamp and flags
269      *
270      * The complex<float> samples are converted to the appropriate 
271      * "on the wire" representation, depending on the current USRP2
272      * configuration.  Typically, this is big-endian 16-bit I & Q.
273      */
274     bool tx_32fc(unsigned int channel,
275                  const std::complex<float> *samples,
276                  size_t nsamples,
277                  const tx_metadata *metadata);
278
279     /*!
280      * \brief transmit complex<int16_t> samples to USRP2
281      *
282      * \param channel specifies the channel to send them to
283      * \param samples are the samples to transmit
284      * \param nsamples is the number of samples to transmit
285      * \param metadata provides the timestamp and flags
286      *
287      * The complex<int16_t> samples are converted to the appropriate
288      * "on the wire" representation, depending on the current USRP2
289      * configuration.  Typically, this is big-endian 16-bit I & Q.
290      */
291     bool tx_16sc(unsigned int channel,
292                  const std::complex<int16_t> *samples,
293                  size_t nsamples,
294                  const tx_metadata *metadata);
295
296     /*!
297      * \brief transmit raw uint32_t data items to USRP2
298      *
299      * The caller is responsible for ensuring that the items are
300      * formatted appropriately for the USRP2 and its configuration.
301      * This method is used primarily by the system itself.  Users
302      * should call tx_32fc or tx_16sc instead.
303      *
304      * \param channel specifies the channel to send them to
305      * \param items are the data items to transmit
306      * \param nitems is the number of items to transmit
307      * \param metadata provides the timestamp and flags
308      */
309     bool tx_raw(unsigned int channel,
310                 const uint32_t *items,
311                 size_t nitems,
312                 const tx_metadata *metadata);
313
314     /*
315      * ----------------------------------------------------------------
316      *  miscellaneous methods
317      * ----------------------------------------------------------------
318      */
319
320     /*!
321      * \brief MIMO configuration
322      *
323      * \param flags from usrp2_mimo_config.h
324      *
325      * <pre>
326      *   one of these:
327      *
328      *     MC_WE_DONT_LOCK
329      *     MC_WE_LOCK_TO_SMA
330      *     MC_WE_LOCK_TO_MIMO
331      *
332      *   and optionally this:
333      *
334      *     MC_PROVIDE_CLK_TO_MIMO
335      * </pre>
336      */
337     bool config_mimo(int flags);
338
339
340     //! Get frequency of master oscillator in Hz
341     bool fpga_master_clock_freq(long *freq);
342
343     // Get Sampling rate of A/D converter in Hz
344     bool adc_rate(long *rate);
345
346     // Get Sampling rate of D/A converter in Hz
347     bool dac_rate(long *rate);
348
349     /*!
350      * \brief Get Tx daughterboard ID
351      *
352      * \param[out] dbid returns the daughterboard id.
353      *
354      * daughterboard id >= 0 if successful, -1 if no daugherboard installed,
355      * -2 if invalid EEPROM on daughterboard.
356      */
357     bool tx_daughterboard_id(int *dbid);
358
359     /*!
360      * \brief Get Rx daughterboard ID
361      *
362      * \param[out] dbid returns the daughterboard id.
363      *
364      * daughterboard id >= 0 if successful, -1 if no daugherboard installed,
365      * -2 if invalid EEPROM on daughterboard.
366      */
367     bool rx_daughterboard_id(int *dbid);
368
369     /*
370      * ----------------------------------------------------------------
371      *  Low level methods
372      * ----------------------------------------------------------------
373      */
374
375     /*!
376      * Burn new mac address into EEPROM on USRP2
377      *
378      * \param new_addr  Network mac address, e.g., "01:23:45:67:89:ab" or "89:ab".
379      *                  If \p addr is HH:HH, it's treated as if it were 00:50:c2:85:HH:HH
380      */
381     bool burn_mac_addr(const std::string &new_addr);
382
383     /*!
384      * Reset master time to 0 at next PPS edge
385      */
386     bool sync_to_pps();
387
388     /*!
389      * Reset master time to 0 at every PPS edge
390      */
391     bool sync_every_pps(bool enable);
392
393     /*!
394      * Read memory from Wishbone bus as 32-bit words.  Handles endian swapping if needed.
395      *
396      * \param addr      32-bit aligned address.  Only the lower 16-bits are significant.
397      * \param words     Number of 32-bit words
398      * 
399      * \returns         Vector of 32-bit read values
400      *
401      * WARNING: Attempts to read memory from addresses that do not correspond to RAM or
402      * memory-mapped peripherals may cause the USRP2 to hang, requiring a power cycle.
403      * 
404      */
405     std::vector<uint32_t> peek32(uint32_t addr, uint32_t words);
406
407     /*!
408      * Write memory to Wishbone bus as 32-bit words.  Handles endian swapping if needed.
409      *
410      * \param addr      32-bit aligned address.  Only the lower 16-bits are significant
411      * \param data      Vector of 32-bit values to write.
412      *
413      * \returns true iff successful
414      *
415      * WARNING: Attempts to read memory from addresses that do not correspond to RAM or
416      * memory-mapped peripherals may cause the USRP2 to hang, requiring a power cycle.
417      * 
418      */
419     bool poke32(uint32_t addr, const std::vector<uint32_t> &data);
420
421     /*!
422      * Set daughterboard GPIO data direction register.
423      *
424      * \param bank      GPIO_TX_BANK or GPIO_RX_BANK
425      * \param value     16-bits, 0=FPGA input, 1=FPGA output
426      * \param mask      16-bits, 0=ignore, 1=set
427      *
428      * \returns true iff successful
429      *
430      * WARNING: Improper usage of this function may result in damage to the USRP2
431      *
432      */
433     bool set_gpio_ddr(int bank, uint16_t value, uint16_t mask);
434
435     /*!
436      * Set daughterboard GPIO output selection register.  For those GPIO pins that
437      * are configured as outputs in the DDR, this settings configures the source
438      * of the pin value.
439      *
440      * \param bank      GPIO_TX_BANK or GPIO_RX_BANK
441      * \param sels      Exactly 16 character MSB->LSB string. For each position:
442      *                    '.' = ignore this bit, i.e., leave current value
443      *                    'a' = Output ATR value
444      *                    's' = Output host software controlled value
445      *                    '0' = Output FPGA debug bus 0 value
446      *                    '1' = Output FPGA debug bus 1 value
447      *
448      * \returns true iff successful
449      *
450      * WARNING: Improper usage of this function may result in damage to the USRP2
451      *
452      */
453     bool set_gpio_sels(int bank, std::string sels);
454
455     /*!
456      * Set daughterboard GPIO pin values.
457      *
458      * \param bank     GPIO_TX_BANK or GPIO_RX_BANK
459      * \param value    16 bits, 0=low, 1=high
460      * \param mask     16 bits, 0=ignore, 1=set
461      *
462      * \returns true iff successful
463      *
464      * WARNING: Improper usage of this function may result in damage to the USRP2
465      *
466      */
467     bool write_gpio(int bank, uint16_t value, uint16_t mask);
468
469     /*!
470      * Read daughterboard GPIO pin values
471      *
472      * \param bank     GPIO_TX_BANK or GPIO_RX_BANK
473      * \param value    pointer to uint16_t to hold read results
474      *
475      * \returns true iff successful
476      *
477      */
478     bool read_gpio(int bank, uint16_t *value);
479
480     /*!
481      * Set GPIO streaming mode
482      *
483      * Individually enables streaming GPIO pins through LSBs of DSP
484      * samples.
485      *
486      * On receive, io_rx[15] replaces I[0], io_rx[14] replaces Q[0]
487      * On transmit, I[0] maps to io_tx[15], Q[0] maps to io_tx[14]
488      * (Transmit streaming is not yet implemented.)
489      *
490      * The selected GPIO pins must have been set as inputs or outputs
491      * and, for transmit, set to software control.
492      *
493      * When enabled, the replaced DSP sample LSBs become 0.
494      *
495      * \param bank     GPIO_TX_BANK or GPIO_RX_BANK
496      * \param enable   enable[0] controls I channel LSB
497      *                 enable[1] controls Q channel LSB
498      *
499      * \returns true iff successful
500      *
501      * WARNING: Improper usage of this function may result in damage to the USRP2
502      *
503      */
504     bool enable_gpio_streaming(int bank, int enable);
505
506 #if 0   // not yet implemented
507     /*!
508      * \brief Write EEPROM on motherboard or any daughterboard.
509      * \param i2c_addr          I2C bus address of EEPROM
510      * \param eeprom_offset     byte offset in EEPROM to begin writing
511      * \param buf               the data to write
512      * \returns true iff sucessful
513      */
514     bool write_eeprom (int i2c_addr, int eeprom_offset, const std::string &buf);
515
516     /*!
517      * \brief Read EEPROM on motherboard or any daughterboard.
518      * \param i2c_addr          I2C bus address of EEPROM
519      * \param eeprom_offset     byte offset in EEPROM to begin reading
520      * \param len               number of bytes to read
521      * \returns the data read if successful, else a zero length string.
522      */
523     std::string read_eeprom (int i2c_addr, int eeprom_offset, int len);
524
525     /*!
526      * \brief Write to I2C peripheral
527      * \param i2c_addr          I2C bus address (7-bits)
528      * \param buf               the data to write
529      * \returns true iff successful
530      * Writes are limited to a maximum of of 64 bytes.
531      */
532     bool write_i2c (int i2c_addr, const std::string &buf);
533
534     /*!
535      * \brief Read from I2C peripheral
536      * \param i2c_addr          I2C bus address (7-bits)
537      * \param len               number of bytes to read
538      * \returns the data read if successful, else a zero length string.
539      * Reads are limited to a maximum of 64 bytes.
540      */
541     std::string read_i2c (int i2c_addr, int len);
542
543     /*!
544      * \brief Write data to SPI bus peripheral.
545      *
546      * \param optional_header   0,1 or 2 bytes to write before buf.
547      * \param enables           bitmask of peripherals to write. See usrp_spi_defs.h
548      * \param format            transaction format.  See usrp_spi_defs.h SPI_FMT_*
549      * \param buf               the data to write
550      * \returns true iff successful
551      * Writes are limited to a maximum of 64 bytes.
552      *
553      * If \p format specifies that optional_header bytes are present, they are
554      * written to the peripheral immediately prior to writing \p buf.
555      */
556     bool write_spi (int optional_header, int enables, int format, const std::string &buf);
557
558     /*
559      * \brief Read data from SPI bus peripheral.
560      *
561      * \param optional_header   0,1 or 2 bytes to write before buf.
562      * \param enables           bitmask of peripheral to read. See usrp_spi_defs.h
563      * \param format            transaction format.  See usrp_spi_defs.h SPI_FMT_*
564      * \param len               number of bytes to read.  Must be in [0,64].
565      * \returns the data read if sucessful, else a zero length string.
566      *
567      * Reads are limited to a maximum of 64 bytes.
568      *
569      * If \p format specifies that optional_header bytes are present, they
570      * are written to the peripheral first.  Then \p len bytes are read from
571      * the peripheral and returned.
572      */
573     std::string read_spi (int optional_header, int enables, int format, int len);
574 #endif
575
576
577     class impl;         // implementation details
578
579   private:
580     // Static function to retrieve or create usrp2 instance
581     static sptr find_existing_or_make_new(const std::string &ifc, props *p);
582
583     // Only class members can instantiate this class
584     usrp2(const std::string &ifc, props *p);
585   
586     // All private state is held in opaque pointer
587     std::auto_ptr<impl> d_impl;
588   };
589
590 };
591
592 std::ostream& operator<<(std::ostream &os, const usrp2::props &x);
593
594
595 #endif /* INCLUDED_USRP2_H */