Commits

Anonymous committed 9581c2c

added set_interface to fx2

  • Participants
  • Parent commits 81875ad

Comments (0)

Files changed (6)

File examples/fx2/.gitignore

 build
+*.pyc

File examples/fx2/cpp/fx2.cpp

 #include "fx2.h"
 
 
-fx2::fx2():dev_handle(NULL) {
+fx2::fx2():dev_handle(NULL),interface(0),alt_setting(0) {
 
  int rv=libusb_init(&libusb_ctx);
  assert(!rv);
 void fx2::open(int vid,int pid) {
     dev_handle=libusb_open_device_with_vid_pid (libusb_ctx,vid,pid);
     assert(dev_handle);
-    if (dev_handle) {
-     int rv=libusb_claim_interface(dev_handle,0);
-     assert(!rv);
-     rv=libusb_set_interface_alt_setting(dev_handle,0,0);
-     assert(!rv);
+    int rv=libusb_claim_interface(dev_handle,interface);
+    assert(!rv);
+    rv=libusb_set_interface_alt_setting(dev_handle,interface,alt_setting);
+    assert(!rv);
+}
+void fx2::set_interface(int iface, int alt){
+    assert(dev_handle);
+    if (interface != iface) {
+        libusb_release_interface(dev_handle,this->interface);
     }
+    int rv=libusb_claim_interface(dev_handle,iface);
+    assert(!rv);
+    rv=libusb_set_interface_alt_setting(dev_handle,iface,alt);
+    assert(!rv);
+    this->interface=iface;
+    this->alt_setting=alt;
 }
-
 void fx2::close() {
     assert(dev_handle);
-    libusb_release_interface(dev_handle,0);
+    libusb_release_interface(dev_handle,interface);
     libusb_close(dev_handle);
     dev_handle=NULL;
+    interface=0;alt_setting=0;
 }
 
 

File examples/fx2/cpp/fx2.h

     private:
         libusb_context* libusb_ctx;
         libusb_device_handle* dev_handle;
+        int interface,alt_setting;
         
     public:
         fx2();
         ~fx2();
-        
+        /**
+         * open
+         * vid = vendor id
+         * pid = product id
+         *
+         * Opens device with vid,pid and claims alt 0 of interface 0
+         **/
         void open(int vid=VID,int pid=PID);
+        void set_interface(int interface, int alt_setting);
         bool isopen() { return dev_handle != NULL; }
         void close();
         int do_usb_command(char* buf, int size, unsigned char type, unsigned char request, unsigned short value, unsigned short index, unsigned short length );

File examples/fx2/fx2load/__init__.py

+# Copyright (C) 2008 Ubixum, Inc. 
+#
+# This library is free software; you can redistribute it and/or
+#
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+# 
+# This library 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
+# Lesser General Public License for more details.
+# 
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+from time import sleep
+from fx2 import fx2
+
+f=fx2.fx2()
+
+# use this functions 1st
+
+def openfx2(vid=0x04b4,pid=0x0082):
+    f.open(vid,pid)
+
+def reset_device(reset):
+ print reset and "Put device in reset" or "Set device to run"
+ write_ram (0xe600,reset and '\x01' or '\x00', 1)
+ 
+def write_ram(addr,data,length):
+ transferred=0
+ while(transferred<length):
+  this_transfer_size=length-transferred>1024 and 1024 or length-transferred
+  buf=data[transferred:]
+  ret=f.do_usb_command( buf,
+    0x40,
+    0xa0,
+    addr+transferred, 0,
+    this_transfer_size )
+  if (ret>0):
+   print "wrote %d bytes" % ret
+   transferred+=ret
+  else:
+   print "Error: %d" % ret
+   return
+
+def reset_bix(filename):
+ """
+  Use this function to reset your firmware.  You'll need to reopen the device afterward.
+ """
+ reset_device(True)
+ bix=open(filename).read()
+ print "loading bix file of length: %d" % len(bix) 
+ write_ram( 0, bix,len(bix) );
+ reset_device(False)
+ print "Closing device after reset"
+ f.close();
+

File examples/fx2/py/__init__.py

-# Copyright (C) 2008 Ubixum, Inc. 
-#
-# This library is free software; you can redistribute it and/or
-#
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 2.1 of the License, or (at your option) any later version.
-# 
-# This library 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
-# Lesser General Public License for more details.
-# 
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-
-from time import sleep
-from fx2 import fx2
-
-f=fx2.fx2()
-
-# use this functions 1st
-
-def openfx2(vid=0x04b4,pid=0x0082):
-    f.open(vid,pid)
-
-def reset_device(reset):
- print reset and "Put device in reset" or "Set device to run"
- write_ram (0xe600,reset and '\x01' or '\x00', 1)
- 
-def write_ram(addr,data,length):
- transferred=0
- while(transferred<length):
-  this_transfer_size=length-transferred>1024 and 1024 or length-transferred
-  buf=data[transferred:]
-  ret=f.do_usb_command( buf,
-    0x40,
-    0xa0,
-    addr+transferred, 0,
-    this_transfer_size )
-  if (ret>0):
-   print "wrote %d bytes" % ret
-   transferred+=ret
-  else:
-   print "Error: %d" % ret
-   return
-
-def reset_bix(filename):
- """
-  Use this function to reset your firmware.  You'll need to reopen the device afterward.
- """
- reset_device(True)
- bix=open(filename).read()
- print "loading bix file of length: %d" % len(bix) 
- write_ram( 0, bix,len(bix) );
- reset_device(False)
- print "Closing device after reset"
- f.close();
-

File examples/fx2/setup.py

  name='fx2',
  version='0.1',
  ext_modules=[fx2_mod],
- packages=['fx2load', 'fx2'],
- package_dir={'fx2load':'py','fx2':'cpp'}
+ packages=['fx2load','fx2'],
+ package_dir={'fx2':'cpp'}
 )