Fixed libusb1 configure bug, libusb1 updates for previous usrp_prims integration
[debian/gnuradio] / usrp / host / include / usrp / usrp_prims.h.in
1 struct libusb_context;
2
3 static const int USRP_HASH_SIZE = 16;
4
5 enum usrp_load_status_t { ULS_ERROR = 0, ULS_OK, ULS_ALREADY_LOADED };
6
7 /*!
8  * \brief initialize libusb; probe busses and devices.
9  * If new_context is set to true, initiate and returns new libusb_context.
10  * If new_context is set to false, intiate default context if not already
11  * initiated and return NULL. It is NOT safe to call more than once with
12  * new_context set to true since a new context is initiated each time.
13  */
14
15 void usrp_one_time_init (libusb_context **ctx = NULL);
16
17 /*
18  * force a rescan of the buses and devices
19  */
20 void usrp_rescan ();
21
22 /*!
23  * \brief locate Nth (zero based) USRP device in system.
24  * Return pointer or 0 if not found.
25  *
26  * The following kinds of devices are considered USRPs:
27  *
28  *   unconfigured USRP (no firwmare loaded)
29  *   configured USRP (firmware loaded)
30  *   unconfigured Cypress FX2 (only if fx2_ok_p is true)
31  */
32 libusb_device *usrp_find_device (int nth, bool fx2_ok_p = false, libusb_context *ctx = NULL);
33
34 bool usrp_usrp_p (libusb_device *q);            //< is this a USRP
35 bool usrp_usrp0_p (libusb_device *q);           //< is this a USRP Rev 0
36 bool usrp_usrp1_p (libusb_device *q);           //< is this a USRP Rev 1
37 bool usrp_usrp2_p (libusb_device *q);           //< is this a USRP Rev 2
38 int  usrp_hw_rev (libusb_device *q);            //< return h/w rev code
39
40 bool usrp_fx2_p (libusb_device *q);                     //< is this an unconfigured Cypress FX2
41
42 bool usrp_unconfigured_usrp_p (libusb_device *q);       //< some kind of unconfigured USRP
43 bool usrp_configured_usrp_p (libusb_device *q); //< some kind of configured USRP
44
45 /*!
46  * \brief given a libusb_device return an instance of the appropriate libusb_device_handle
47  *
48  * These routines claim the specified interface and select the
49  * correct alternate interface.  (USB nomenclature is totally screwed!)
50  *
51  * If interface can't be opened, or is already claimed by some other
52  * process, 0 is returned.
53  */
54 libusb_device_handle *usrp_open_cmd_interface (libusb_device *dev);
55 libusb_device_handle *usrp_open_rx_interface (libusb_device *dev);
56 libusb_device_handle *usrp_open_tx_interface (libusb_device *dev);
57
58 /*!
59  * \brief close interface.
60  */
61 bool usrp_close_interface (libusb_device_handle *udh);
62
63 /*!
64  * \brief load intel hex format file into USRP/Cypress FX2 (8051).
65  *
66  * The filename extension is typically *.ihx
67  *
68  * Note that loading firmware may cause the device to renumerate.  I.e.,
69  * change its configuration, invalidating the current device handle.
70  */
71
72 usrp_load_status_t 
73 usrp_load_firmware (libusb_device_handle *udh, const char *filename, bool force);
74
75 /*!
76  * \brief load intel hex format file into USRP FX2 (8051).
77  *
78  * The filename extension is typically *.ihx
79  *
80  * Note that loading firmware may cause the device to renumerate.  I.e.,
81  * change its configuration, invalidating the current device handle.
82  * If the result is ULS_OK, usrp_load_firmware_nth delays 1 second
83  * then rescans the busses and devices.
84  */
85 usrp_load_status_t
86 usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx = NULL);
87
88 /*!
89  * \brief load fpga configuration bitstream
90  */
91 usrp_load_status_t
92 usrp_load_fpga (libusb_device_handle *udh, const char *filename, bool force);
93
94 /*!
95  * \brief load the regular firmware and fpga bitstream in the Nth USRP.
96  *
97  * This is the normal starting point...
98  */
99 bool usrp_load_standard_bits (int nth, bool force,
100                               const std::string fpga_filename = "",
101                               const std::string firmware_filename = "",
102                               libusb_context *ctx = NULL);
103
104 /*!
105  * \brief copy the given \p hash into the USRP hash slot \p which.
106  * The usrp implements two hash slots, 0 and 1.
107  */
108 bool usrp_set_hash (libusb_device_handle *udh, int which,
109                     const unsigned char hash[USRP_HASH_SIZE]);
110
111 /*!
112  * \brief retrieve the \p hash from the USRP hash slot \p which.
113  * The usrp implements two hash slots, 0 and 1.
114  */
115 bool usrp_get_hash (libusb_device_handle *udh, int which,
116                     unsigned char hash[USRP_HASH_SIZE]);
117
118 bool usrp_write_fpga_reg (libusb_device_handle *udh, int reg, int value);
119 bool usrp_read_fpga_reg (libusb_device_handle *udh, int reg, int *value);
120 bool usrp_set_fpga_reset (libusb_device_handle *udh, bool on);
121 bool usrp_set_fpga_tx_enable (libusb_device_handle *udh, bool on);
122 bool usrp_set_fpga_rx_enable (libusb_device_handle *udh, bool on);
123 bool usrp_set_fpga_tx_reset (libusb_device_handle *udh, bool on);
124 bool usrp_set_fpga_rx_reset (libusb_device_handle *udh, bool on);
125 bool usrp_set_led (libusb_device_handle *udh, int which, bool on);
126
127 bool usrp_check_rx_overrun (libusb_device_handle *udh, bool *overrun_p);
128 bool usrp_check_tx_underrun (libusb_device_handle *udh, bool *underrun_p);
129
130 // i2c_read and i2c_write are limited to a maximum len of 64 bytes.
131
132 bool usrp_i2c_write (libusb_device_handle *udh, int i2c_addr,
133                      const void *buf, int len);
134
135 bool usrp_i2c_read (libusb_device_handle *udh, int i2c_addr,
136                     void *buf, int len);
137
138 // spi_read and spi_write are limited to a maximum of 64 bytes
139 // See usrp_spi_defs.h for more info
140
141 bool usrp_spi_write (libusb_device_handle *udh,
142                      int optional_header, int enables, int format,
143                      const void *buf, int len);
144
145 bool usrp_spi_read (libusb_device_handle *udh,
146                      int optional_header, int enables, int format,
147                      void *buf, int len);
148
149
150 bool usrp_9862_write (libusb_device_handle *udh,
151                       int which_codec,                  // [0,  1]
152                       int regno,                        // [0, 63]
153                       int value);                       // [0, 255]     
154
155 bool usrp_9862_read (libusb_device_handle *udh,
156                      int which_codec,                   // [0,  1]
157                      int regno,                         // [0, 63]
158                      unsigned char *value);             // [0, 255]
159
160 /*!
161  * \brief Write multiple 9862 regs at once.
162  *
163  * \p buf contains alternating register_number, register_value pairs.
164  * \p len must be even and is the length of buf in bytes.
165  */
166 bool usrp_9862_write_many (libusb_device_handle *udh, int which_codec,
167                            const unsigned char *buf, int len);
168                            
169
170 /*!
171  * \brief write specified regs to all 9862's in the system
172  */
173 bool usrp_9862_write_many_all (libusb_device_handle *udh,
174                                const unsigned char *buf, int len);
175                            
176
177 // Write 24LC024 / 24LC025 EEPROM on motherboard or daughterboard.
178 // Which EEPROM is determined by i2c_addr.  See i2c_addr.h
179
180 bool usrp_eeprom_write (libusb_device_handle *udh, int i2c_addr,
181                         int eeprom_offset, const void *buf, int len);
182
183
184 // Read 24LC024 / 24LC025 EEPROM on motherboard or daughterboard.
185 // Which EEPROM is determined by i2c_addr.  See i2c_addr.h
186
187 bool usrp_eeprom_read (libusb_device_handle *udh, int i2c_addr,
188                        int eeprom_offset, void *buf, int len);
189
190
191 // Slot specific i/o routines
192
193 /*!
194  * \brief write to the specified aux dac.
195  *
196  * \p slot: which Tx or Rx slot to write.
197  *    N.B., SLOT_TX_A and SLOT_RX_A share the same AUX DAC's
198  *          SLOT_TX_B and SLOT_RX_B share the same AUX DAC's
199  *
200  * \p which_dac: [0,3]  RX slots must use only 0 and 1.
201  *                      TX slots must use only 2 and 3.
202  *
203  * AUX DAC 3 is really the 9862 sigma delta output.
204  *
205  * \p value to write to aux dac.  All dacs take straight
206  * binary values.  Although dacs 0, 1 and 2 are 8-bit and dac 3 is 12-bit,
207  * the interface is in terms of 12-bit values [0,4095]
208  */
209 bool usrp_write_aux_dac (libusb_device_handle *uhd, int slot,
210                          int which_dac, int value);
211
212 /*!
213  * \brief Read the specified aux adc
214  *
215  * \p slot: which Tx or Rx slot to read aux dac
216  * \p which_adc: [0,1]  which of the two adcs to read
217  * \p *value: return value, 12-bit straight binary.
218  */
219 bool usrp_read_aux_adc (libusb_device_handle *udh, int slot,
220                         int which_adc, int *value);
221
222
223 /*!
224  * \brief usrp daughterboard id to name mapping
225  */
226 const std::string usrp_dbid_to_string (int dbid);
227
228
229 enum usrp_dbeeprom_status_t { UDBE_OK, UDBE_BAD_SLOT, UDBE_NO_EEPROM, UDBE_INVALID_EEPROM };
230
231 struct usrp_dboard_eeprom {
232   unsigned short        id;             // d'board identifier code
233   unsigned short        oe;             // fpga output enables:
234                                         //   If bit set, i/o pin is an output from FPGA.
235   short                 offset[2];      // ADC/DAC offset correction
236 };
237
238 /*!
239  * \brief Read and return parsed daughterboard eeprom
240  */
241 usrp_dbeeprom_status_t
242 usrp_read_dboard_eeprom (libusb_device_handle *udh,
243                          int slot_id, usrp_dboard_eeprom *eeprom);
244
245 /*!
246  * \brief write ADC/DAC offset calibration constants to d'board eeprom
247  */
248 bool usrp_write_dboard_offsets (libusb_device_handle *udh, int slot_id,
249                                 short offset0, short offset1);
250
251 /*!
252  * \brief return a usrp's serial number.
253  *
254  * Note that this only works on a configured usrp.
255  * \returns non-zero length string iff successful.
256  */
257 std::string usrp_serial_number(libusb_device_handle *udh);
258
259 /*
260  * Internal functions 
261  */
262
263 libusb_device_handle *
264 usrp_open_interface(libusb_device *dev, int interface, int altinterface);
265
266 int write_cmd (libusb_device_handle *udh, int request, int value, int index,
267                unsigned char *bytes, int len);
268
269 /*
270  * Compatibility functions 
271  */
272
273 libusb_device *_get_usb_device (libusb_device_handle *udh);
274
275 libusb_device_descriptor _get_usb_device_descriptor (libusb_device *q);
276
277 int _get_usb_string_descriptor (libusb_device_handle *udh, int index,
278                                 unsigned char* data, int length);
279
280 int _usb_control_transfer (libusb_device_handle *udh, int request_type,
281                            int request, int value, int index,
282                            unsigned char *data, int length,
283                            unsigned int timeout);
284
285 #endif /* _USRP_PRIMS_H_ */