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