1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
//
// Copyright 2010-2013 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <libusb.h>
#include <sstream>
#include <string>
#include <cmath>
#include <cstring>
#include <boost/cstdint.hpp>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
#include <boost/program_options.hpp>
#include <boost/thread/thread.hpp>
#include <boost/functional/hash.hpp>
#include <b200_iface.hpp>
#include <uhd/transport/usb_control.hpp>
#include <uhd/transport/usb_device_handle.hpp>
#include <uhd/exception.hpp>
#include <uhd/utils/images.hpp>
namespace po = boost::program_options;
namespace fs = boost::filesystem;
//!used with lexical cast to parse a hex string
template <class T> struct to_hex{
T value;
operator T() const {return value;}
friend std::istream& operator>>(std::istream& in, to_hex& out){
in >> std::hex >> out.value;
return in;
}
};
//!parse hex-formatted ASCII text into an int
boost::uint16_t atoh(const std::string &string){
if (string.substr(0, 2) == "0x"){
return boost::lexical_cast<to_hex<boost::uint16_t> >(string);
}
return boost::lexical_cast<boost::uint16_t>(string);
}
int reset_usb()
{
/* Okay, first, we need to discover what the path is to the ehci and
* xhci device files. */
std::set<fs::path> path_list;
path_list.insert("/sys/bus/pci/drivers/xhci-pci/");
path_list.insert("/sys/bus/pci/drivers/ehci-pci/");
path_list.insert("/sys/bus/pci/drivers/xhci_hcd/");
path_list.insert("/sys/bus/pci/drivers/ehci_hcd/");
/* Check each of the possible paths above to find which ones this system
* uses. */
for(std::set<fs::path>::iterator found = path_list.begin();
found != path_list.end(); ++found) {
if(fs::exists(*found)) {
fs::path devpath = *found;
std::set<fs::path> globbed;
/* Now, glob all of the files in the directory. */
fs::directory_iterator end_itr;
for(fs::directory_iterator itr(devpath); itr != end_itr; ++itr) {
globbed.insert((*itr).path());
}
/* Check each file path string to see if it is a device file. */
for(std::set<fs::path>::iterator it = globbed.begin();
it != globbed.end(); ++it) {
std::string file = fs::path((*it).filename()).string();
if(file.compare(0, 5, "0000:") == 0) {
/* Un-bind the device. */
std::fstream unbind((devpath.string() + "unbind").c_str(),
std::fstream::out);
unbind << file;
unbind.close();
/* Re-bind the device. */
std::cout << "Re-binding: " << file << " in "
<< devpath.string() << std::endl;
std::fstream bind((devpath.string() + "bind").c_str(),
std::fstream::out);
bind << file;
bind.close();
}
}
}
}
return 0;
}
uhd::transport::usb_device_handle::sptr open_device(const boost::uint16_t vid, const boost::uint16_t pid)
{
std::vector<uhd::transport::usb_device_handle::sptr> handles;
uhd::transport::usb_device_handle::sptr handle;
handles = uhd::transport::usb_device_handle::get_device_list(vid, pid); // try caller's VID/PID first
if (handles.size() == 0)
handles = uhd::transport::usb_device_handle::get_device_list(FX3_VID, FX3_DEFAULT_PID); // try default Cypress FX3 VID/PID next
if (handles.size() == 0)
handles = uhd::transport::usb_device_handle::get_device_list(FX3_VID, FX3_REENUM_PID); // try reenumerated Cypress FX3 VID/PID next
if (handles.size() == 0)
handles = uhd::transport::usb_device_handle::get_device_list(B200_VENDOR_ID, B200_PRODUCT_ID); // try default B200 VID/PID last
if (handles.size() > 0)
handle = handles[0];
if (!handle)
std::cerr << "Cannot open device" << std::endl;
return handle;
}
b200_iface::sptr make_b200_iface(const uhd::transport::usb_device_handle::sptr &handle)
{
uhd::transport::usb_control::sptr usb_ctrl = uhd::transport::usb_control::make(handle, 0);
b200_iface::sptr b200 = b200_iface::make(usb_ctrl);
if (!b200)
std::cerr << "Cannot create device interface" << std::endl;
return b200;
}
boost::int32_t main(boost::int32_t argc, char *argv[]) {
boost::uint16_t vid, pid;
std::string pid_str, vid_str, fw_file, fpga_file;
po::options_description visible("Allowed options");
visible.add_options()
("help,h", "help message")
("vid,v", po::value<std::string>(&vid_str),
"Specify VID of device to use.")
("pid,p", po::value<std::string>(&pid_str),
"Specify PID of device to use.")
("speed,S", "Read back the USB mode currently in use.")
("reset-device,D", "Reset the B2xx Device.")
("reset-fpga,F", "Reset the FPGA (does not require re-programming.")
("reset-usb,U", "Reset the USB subsystem on your host computer.")
("init-device,I", "Initialize a B2xx device.")
("load-fw,W", po::value<std::string>(&fw_file),
"Load a firmware (hex) file into the FX3.")
("load-fpga,L", po::value<std::string>(&fpga_file),
"Load a FPGA (bin) file into the FPGA.")
;
// Hidden options provided for testing - use at your own risk!
po::options_description hidden("Hidden options");
hidden.add_options()
("uninit-device,U", "Uninitialize a B2xx device.")
("read-eeprom,R", "Read first 8 bytes of EEPROM")
("erase-eeprom,E", "Erase first 8 bytes of EEPROM");
po::options_description desc;
desc.add(visible);
desc.add(hidden);
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")){
std::cout << boost::format("B2xx Utility Program %s") % visible << std::endl;
return ~0;
} else if (vm.count("reset-usb")) {
return reset_usb();
}
uhd::transport::usb_device_handle::sptr handle;
uhd::transport::usb_control::sptr usb_ctrl;
b200_iface::sptr b200;
vid = B200_VENDOR_ID; // Default
pid = B200_PRODUCT_ID; // Default
if (vm.count("vid"))
vid = atoh(vid_str);
if (vm.count("pid"))
pid = atoh(pid_str);
// open the device
handle = open_device(vid, pid);
if (!handle)
return -1;
std::cout << "B2xx detected..." << std::flush;
// make the interface
b200 = make_b200_iface(handle);
if (!b200)
return -1;
std::cout << " Control of B2xx granted..." << std::endl << std::endl;
// if we are supposed to load a new firmware image and one already exists, reset and load the new one
if (vm.count("load-fw") && handle->firmware_loaded())
{
std::cout << "Overwriting existing firmware" << std::endl;
// reset the device
b200->reset_fx3();
// re-open device
b200.reset();
usb_ctrl.reset();
handle.reset();
usleep(2000000); // wait 2 seconds for FX3 to reset
handle = open_device(vid, pid);
if (!handle)
return -1;
b200 = make_b200_iface(handle);
if (!b200)
return -1;
}
// Check to make sure firmware is loaded
if (!(handle->firmware_loaded()))
{
std::cout << "Loading firmware" << std::endl;
if (fw_file.empty())
fw_file = uhd::find_image_path(B200_FW_FILE_NAME);
// load firmware
b200->load_firmware(fw_file);
// re-open device
b200.reset();
usb_ctrl.reset();
handle.reset();
handle = open_device(vid, pid);
if (!handle)
return -1;
b200 = make_b200_iface(handle);
if (!b200)
return -1;
}
// Added for testing purposes - not exposed
if (vm.count("read-eeprom"))
{
uhd::byte_vector_t data;
try {
data = b200->read_eeprom(0x0, 0x0, 8);
} catch (std::exception &e) {
std::cerr << "Exception while reading EEPROM: " << e.what() << std::endl;
return -1;
}
for (int i = 0; i < 8; i++)
std::cout << i << ": " << boost::format("0x%X") % (int)data[i] << std::endl;
return 0;
}
// Added for testing purposes - not exposed
if (vm.count("erase-eeprom"))
{
uhd::byte_vector_t bytes(8);
memset(&bytes[0], 0xFF, 8);
try {
b200->write_eeprom(0x0, 0x0, bytes);
} catch (uhd::exception &e) {
std::cerr << "Exception while writing to EEPROM: " << e.what() << std::endl;
return -1;
}
// verify
uhd::byte_vector_t read_bytes(8);
try {
read_bytes = b200->read_eeprom(0x0, 0x0, 8);
} catch (uhd::exception &e) {
std::cerr << "Exception while reading from EEPROM: " << e.what() << std::endl;
return -1;
}
bool verified = true;
for (int i = 0; i < 8; i++) {
if (bytes[i] != read_bytes[i]) {
verified = false;
std::cerr << "Expected: " << bytes[i] << ", Got: " << read_bytes[i] << std::endl;
}
}
if (!verified) {
std::cerr << "Verification failed" << std::endl;
return -1;
}
std::cout << "Erase Successful!" << std::endl;
return 0;
}
// Added for testing purposes - not exposed
if (vm.count("uninit-device"))
{
// uninitialize the device
uhd::byte_vector_t bytes(8);
memset(&bytes[0], 0xFF, 8);
try {
b200->write_eeprom(0x0, 0x0, bytes);
} catch (uhd::exception &e) {
std::cerr << "Exception while writing to EEPROM: " << e.what() << std::endl;
return -1;
}
std::cout << "EEPROM uninitialized, resetting device..."
<< std::endl << std::endl;
// reset the device
try {
b200->reset_fx3();
} catch (uhd::exception &e) {
std::cerr << "Exception while resetting FX3: " << e.what() << std::endl;
return -1;
}
std::cout << "Uninitialization Process Complete."
<< std::endl << std::endl;
return 0;
}
/* If we are initializing the device, the VID/PID should default to the
* Cypress VID/PID for the initial FW load, but we can initialize from any state. */
if (vm.count("init-device"))
{
/* Now, initialize the device. */
uhd::byte_vector_t bytes(8);
bytes[0] = 0x43;
bytes[1] = 0x59;
bytes[2] = 0x14;
bytes[3] = 0xB2;
bytes[4] = (B200_PRODUCT_ID & 0xff);
bytes[5] = (B200_PRODUCT_ID >> 8);
bytes[6] = (B200_VENDOR_ID & 0xff);
bytes[7] = (B200_VENDOR_ID >> 8);
try {
b200->write_eeprom(0x0, 0x0, bytes);
} catch (uhd::exception &e) {
std::cerr << "Exception while writing to EEPROM: " << e.what() << std::endl;
return -1;
}
std::cout << "EEPROM initialized, resetting device..."
<< std::endl << std::endl;
/* Reset the device! */
try {
b200->reset_fx3();
} catch (const std::exception &e) {
std::cerr << "Exceptions while resetting device: " << e.what() << std::endl;
return -1;
}
std::cout << "Initialization Process Complete."
<< std::endl << std::endl;
return 0;
}
boost::uint8_t data_buffer[16];
memset(data_buffer, 0x0, sizeof(data_buffer));
if (vm.count("speed")){
boost::uint8_t speed;
try {speed = b200->get_usb_speed();}
catch (uhd::exception &e) {
std::cerr << "Exception while getting USB speed: " << e.what() << std::endl;
return -1;
}
std::cout << "Currently operating at USB " << (int) speed << std::endl;
} else if (vm.count("reset-device")) {
try {b200->reset_fx3();}
catch (uhd::exception &e) {
std::cerr << "Exception while resetting FX3: " << e.what() << std::endl;
return -1;
}
} else if (vm.count("reset-fpga")) {
try {b200->set_fpga_reset_pin(true);}
catch (uhd::exception &e) {
std::cerr << "Exception while resetting FPGA: " << e.what() << std::endl;
return -1;
}
} else if (vm.count("load-fw")) {
std::cout << "Firmware load complete, releasing USB interface..."
<< std::endl;
} else if (vm.count("load-fpga")) {
std::cout << "Loading FPGA image (" << fpga_file << ")" << std::endl;
uint32_t fx3_state;
try {fx3_state = b200->load_fpga(fpga_file);} // returns 0 on success, or FX3 state on error
catch (uhd::exception &e) {
std::cerr << "Exception while loading FPGA: " << e.what() << std::endl;
return ~0;
}
if (fx3_state != 0) {
std::cerr << std::flush << "Error loading FPGA. FX3 state: "
<< fx3_state << std::endl;
return ~0;
}
std::cout << "FPGA load complete, releasing USB interface..."
<< std::endl;
} else {
std::cout << boost::format("B2xx Utility Program %s") % visible << std::endl;
return ~0;
}
std::cout << "Operation complete! I did it! I did it!" << std::endl;
return 0;
}
|