Imported Upstream version 3.0
[debian/gnuradio] / usrp / host / swig / prims.i
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2003,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 /*
24  * Low level primitives for directly messing with USRP hardware.
25  *
26  * If you're trying to use the USRP, you'll probably want to take a
27  * look at the usrp_standard_rx and usrp_standard_tx classes.  They
28  * hide a bunch of low level details and provide high performance
29  * streaming i/o.
30  *
31  * This interface is built on top of libusb, which allegedly works under
32  * Linux, *BSD and Mac OS/X.  http://libusb.sourceforge.net
33  */
34
35 %include <stl.i>        // pick up string stuff
36
37
38 %{
39 #include <usrp_prims.h>
40 %}
41
42
43 enum usrp_load_status_t { ULS_ERROR = 0, ULS_OK, ULS_ALREADY_LOADED };
44
45 struct usb_dev_handle;
46 struct usb_device;
47
48 /*!
49  * \brief initialize libusb; probe busses and devices.
50  * Safe to call more than once.
51  */
52 void usrp_one_time_init ();
53
54 void usrp_rescan ();
55
56 /*!
57  * \brief locate Nth (zero based) USRP device in system.
58  * Return pointer or 0 if not found.
59  *
60  * The following kinds of devices are considered USRPs:
61  *
62  *   unconfigured USRP (no firwmare loaded)
63  *   configured USRP (firmware loaded)
64  *   unconfigured Cypress FX2 (only if fx2_ok_p is true)
65  */
66 struct usb_device *usrp_find_device (int nth, bool fx2_ok_p = false);
67
68 bool usrp_usrp_p (struct usb_device *q);                //< is this a USRP
69 bool usrp_usrp0_p (struct usb_device *q);               //< is this a USRP Rev 0
70 bool usrp_usrp1_p (struct usb_device *q);               //< is this a USRP Rev 1
71 bool usrp_usrp2_p (struct usb_device *q);               //< is this a USRP Rev 2
72 int  usrp_hw_rev (struct usb_device *q);                //< return h/w rev code
73 bool usrp_fx2_p (struct usb_device *q);                 //< is this an unconfigured Cypress FX2
74
75 bool usrp_unconfigured_usrp_p (struct usb_device *q);   //< some kind of unconfigured USRP
76 bool usrp_configured_usrp_p (struct usb_device *q);     //< some kind of configured USRP
77
78 /*!
79  * \brief given a usb_device return an instance of the appropriate usb_dev_handle
80  *
81  * These routines claim the specified interface and select the
82  * correct alternate interface.  (USB nomenclature is totally screwed!)
83  *
84  * If interface can't be opened, or is already claimed by some other
85  * process, 0 is returned.
86  */
87 struct usb_dev_handle *usrp_open_cmd_interface (struct usb_device *dev);
88 struct usb_dev_handle *usrp_open_rx_interface (struct usb_device *dev);
89 struct usb_dev_handle *usrp_open_tx_interface (struct usb_device *dev);
90
91 /*!
92  * \brief close interface.
93  */
94 bool usrp_close_interface (struct usb_dev_handle *udh);
95
96 /*!
97  * \brief load intel hex format file into USRP/Cypress FX2 (8051).
98  *
99  * The filename extension is typically *.ihx
100  *
101  * Note that loading firmware may cause the device to renumerate.  I.e.,
102  * change its configuration, invalidating the current device handle.
103  */
104
105 usrp_load_status_t 
106 usrp_load_firmware (struct usb_dev_handle *udh, const char *filename, bool force);
107
108 /*!
109  * \brief load intel hex format file into USRP FX2 (8051).
110  *
111  * The filename extension is typically *.ihx
112  *
113  * Note that loading firmware may cause the device to renumerate.  I.e.,
114  * change its configuration, invalidating the current device handle.
115  * If the result is ULS_OK, usrp_load_firmware_nth delays 1 second
116  * then rescans the busses and devices.
117  */
118 usrp_load_status_t
119 usrp_load_firmware_nth (int nth, const char *filename, bool force);
120
121 /*!
122  * \brief load fpga configuration bitstream
123  */
124 usrp_load_status_t
125 usrp_load_fpga (struct usb_dev_handle *udh, const char *filename, bool force);
126
127 /*!
128  * \brief load the regular firmware and fpga bitstream in the Nth USRP.
129  *
130  * This is the normal starting point...
131  */
132 bool usrp_load_standard_bits (int nth, bool force);
133
134
135 %include <fpga_regs_common.h>
136 %include <fpga_regs_standard.h>
137
138
139 bool usrp_write_fpga_reg (struct usb_dev_handle *udh, int reg, int value);
140
141 %inline %{
142
143 int 
144 usrp_read_fpga_reg (struct usb_dev_handle *udh, int reg)
145 {
146   int value;
147   bool ok = usrp_read_fpga_reg (udh, reg, &value);
148   if (ok)
149     return value;
150   else
151     return -999;
152 }
153
154 %}
155
156 bool usrp_set_fpga_reset (struct usb_dev_handle *udh, bool on);
157 bool usrp_set_fpga_tx_enable (struct usb_dev_handle *udh, bool on);
158 bool usrp_set_fpga_rx_enable (struct usb_dev_handle *udh, bool on);
159 bool usrp_set_fpga_tx_reset (struct usb_dev_handle *udh, bool on);
160 bool usrp_set_fpga_rx_reset (struct usb_dev_handle *udh, bool on);
161 bool usrp_set_led (struct usb_dev_handle *udh, int which, bool on);
162
163 bool usrp_check_rx_overrun (struct usb_dev_handle *udh, bool *overrun_p);
164 bool usrp_check_tx_underrun (struct usb_dev_handle *udh, bool *underrun_p);
165
166 // i2c_read and i2c_write are limited to a maximum len of 64 bytes.
167
168 bool usrp_i2c_write (struct usb_dev_handle *udh, int i2c_addr,
169                      void *buf, int len);
170
171 bool usrp_i2c_read (struct usb_dev_handle *udh, int i2c_addr,
172                     void *buf, int len);
173
174 // spi_read and spi_write are limited to a maximum of 64 bytes
175 // See usrp_spi_defs.h for more info
176
177 bool usrp_spi_write (struct usb_dev_handle *udh,
178                      int optional_header, int enables, int format,
179                      unsigned char *buf, int len);
180
181 bool usrp_spi_read (struct usb_dev_handle *udh,
182                      int optional_header, int enables, int format,
183                      unsigned char *buf, int len);
184
185
186 bool usrp_9862_write (struct usb_dev_handle *udh,
187                       int which_codec,                  // [0,  1]
188                       int regno,                        // [0, 63]
189                       int value);                       // [0, 255]     
190
191 %inline %{
192
193 int 
194 usrp_9862_read (struct usb_dev_handle *udh, int which_codec, int reg)
195 {
196   unsigned char value;
197   bool ok = usrp_9862_read (udh, which_codec, reg, &value);
198   if (ok)
199     return value;
200   else
201     return -999;
202 }
203
204 %}
205
206 %inline %{
207
208 bool 
209 usrp_eeprom_write (struct usb_dev_handle *udh, int i2c_addr,
210                    int eeprom_offset, const std::string buf)
211 {
212   return usrp_eeprom_write (udh, i2c_addr, eeprom_offset,
213                             buf.data (), buf.size ());
214 }
215   
216 std::string
217 usrp_eeprom_read (struct usb_dev_handle *udh, int i2c_addr,
218                   int eeprom_offset, int len)
219 {
220   if (len <= 0)
221     return "";
222   
223   char buf[len];
224
225   if (!usrp_eeprom_read (udh, i2c_addr, eeprom_offset, buf, len))
226     return "";
227
228   return std::string (buf, len);
229 }
230
231 %}
232
233 bool usrp_write_aux_dac (struct usb_dev_handle *uhd, int slot,
234                          int which_dac, int value);
235
236 %inline %{
237
238 int usrp_read_aux_adc (struct usb_dev_handle *udh, int slot, int which_adc)
239 {
240   int value;
241   bool ok = usrp_read_aux_adc (udh, slot, which_adc, &value);
242   if (ok)
243     return value;
244   else
245     return -999;
246 }
247
248 %}
249
250 /*!
251  * \brief return a usrp's serial number.
252  *
253  * Note that this only works on a configured usrp.
254  * \returns non-zero length string iff successful.
255  */
256 std::string usrp_serial_number(struct usb_dev_handle *udh);
257
258 /*!
259  * \brief usrp daughterboard id to name mapping
260  */
261 const std::string usrp_dbid_to_string (int dbid);
262
263 %inline %{
264 #include "../../firmware/include/fpga_regs_common.h"
265 #include "../../firmware/include/fpga_regs_standard.h"
266 %}