]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blame - fx2lib/examples/fx2/cpp/fx2.cpp
Don't ship the .git/ directory in the tarball.
[sigrok-firmware-fx2lafw.git] / fx2lib / examples / fx2 / cpp / fx2.cpp
CommitLineData
3608c106
UH
1/**
2 * Copyright (C) 2009 Ubixum, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 **/
18
19#include <cstdio>
20#include <cassert>
21
22#include "fx2.h"
23
24
25fx2::fx2():dev_handle(NULL) {
26
27 int rv=libusb_init(&libusb_ctx);
28 assert(!rv);
29 libusb_set_debug(libusb_ctx,0);
30}
31
32void fx2::set_debug_level(int n) {
33 libusb_set_debug(libusb_ctx,n);
34}
35
36fx2::~fx2() {
37
38 if (isopen()) close();
39
40 libusb_exit(libusb_ctx);
41}
42
43#define CHECK_OPEN(r) if (!dev_handle) {\
44 printf ( "Device not opened.\n" ); \
45 return r;\
46 }
47
48
49void fx2::open(int vid,int pid,int idx) {
50
51 libusb_device **list;
52 int devices = libusb_get_device_list( libusb_ctx, &list );
53 int cur_idx=0;
54 for ( int i=0;i<devices;++i) {
55 libusb_device_descriptor dscr;
56 if ( !libusb_get_device_descriptor ( list[i], &dscr ) ) {
57 if ( dscr.idVendor == vid && dscr.idProduct == pid ) {
58 if ( idx == cur_idx++ ) {
59 int rv = libusb_open( list[i], &dev_handle);
60 if (!rv) {
61 rv=libusb_claim_interface(dev_handle,0);
62 if (!rv) { interface=0;
63 rv=libusb_set_interface_alt_setting(dev_handle,0,0);
64 if (rv) {
65 libusb_close(dev_handle);
66 dev_handle=NULL;
67 }
68 alt_setting=0;
69 } else {
70 libusb_close(dev_handle);
71 dev_handle=NULL;
72 }
73 } else {
74 printf ( "Unable to open device idx: %d, ret: %d\n", idx, rv );
75 }
76 }
77 }
78 }
79 }
80
81 if (!dev_handle) {
82 printf ( "Device not opened.\n" );
83 }
84 libusb_free_device_list(list,1);
85}
86void fx2::set_interface(int iface, int alt){
87 CHECK_OPEN()
88 if (interface != iface) {
89 libusb_release_interface(dev_handle,interface);
90 int rv=libusb_claim_interface(dev_handle,iface);
91 assert(!rv);
92 this->interface=iface;
93 }
94 int rv=libusb_set_interface_alt_setting(dev_handle,interface,alt);
95 assert(!rv);
96 alt_setting=alt;
97}
98void fx2::close() {
99 CHECK_OPEN()
100 libusb_release_interface(dev_handle,interface);
101 libusb_close(dev_handle);
102 dev_handle=NULL;
103 interface=0;alt_setting=0;
104}
105
106
107int fx2::do_usb_command(char* buf, int size, unsigned char type, unsigned char request, unsigned short value, unsigned short index, unsigned short length, int timeout ) {
108 CHECK_OPEN(-1)
109 return libusb_control_transfer (
110 dev_handle,
111 type,
112 request,
113 value,
114 index,
115 (unsigned char*)buf,
116 length,
117 timeout);
118}
119
120int fx2::clear_halt(char ep) {
121 CHECK_OPEN(-1)
122 return libusb_clear_halt(dev_handle,(unsigned char)ep);
123}
124
125int fx2::reset() {
126 CHECK_OPEN(-1)
127 int rv=libusb_reset_device(dev_handle);
128 if (rv==LIBUSB_ERROR_NO_DEVICE) {
129 printf ( "Device Changed. Closing\n");
130 libusb_close(dev_handle);
131 interface=0;alt_setting=0;
132 }
133 return rv;
134}
135
136int fx2::set_configuration(int configuration) {
137 CHECK_OPEN(-1)
138 libusb_release_interface(dev_handle,interface);
139 int rv=libusb_set_configuration(dev_handle,configuration);
140 if (!rv) {
141 libusb_claim_interface(dev_handle,interface);
142 }
143}
144
145
146bool fx2::ep_bulk(char* buf, int size, unsigned char ep, int timeout) {
147 CHECK_OPEN(-1)
148 int transferred;
149 int rv=libusb_bulk_transfer ( dev_handle, ep, (unsigned char*)buf, size, &transferred, timeout );
150
151 if (!rv) return true;
152
153 if (rv==LIBUSB_ERROR_TIMEOUT) {
154 printf ( "Transfer Timeout. %d bytes transferred.\n", transferred );
155 } else if (rv<0) {
156 printf ( "Transfer Error: %d\n", rv );
157 }
158
159 return false;
160
161}