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