Commits

Anonymous committed 0246c96

Ability of fx2 driver example to open by index.

Changed open method to have an optional index parameter. This allows
opening more than one device at a time. Also removed asserts so python
mappings don't seg fault.

  • Participants
  • Parent commits 5bed038

Comments (0)

Files changed (3)

File examples/fx2/cpp/fx2.cpp

  libusb_exit(libusb_ctx);
 }
 
+#define CHECK_OPEN(r) if (!dev_handle) {\
+    printf ( "Device not opened.\n" ); \
+    return r;\
+    }
 
-void fx2::open(int vid,int pid) {
-    dev_handle=libusb_open_device_with_vid_pid (libusb_ctx,vid,pid);
-    assert(dev_handle);
-    int rv=libusb_claim_interface(dev_handle,0);
-    assert(!rv);
-    interface=0;
-    rv=libusb_set_interface_alt_setting(dev_handle,0,0);
-    assert(!rv);
-    alt_setting=0;
+
+void fx2::open(int vid,int pid,int idx) {
+
+    libusb_device **list;
+    int devices = libusb_get_device_list( libusb_ctx, &list );
+    int cur_idx=0;
+    for ( int i=0;i<devices;++i) {
+       libusb_device_descriptor dscr;
+       if ( !libusb_get_device_descriptor ( list[i], &dscr ) ) {
+          if ( dscr.idVendor == vid && dscr.idProduct == pid ) {
+             if ( idx == cur_idx++ ) {
+                int rv = libusb_open( list[i], &dev_handle);
+                if (!rv) {
+                    rv=libusb_claim_interface(dev_handle,0);
+                    if (!rv) {                     interface=0;
+                     rv=libusb_set_interface_alt_setting(dev_handle,0,0);
+                     if (rv) {
+                        libusb_close(dev_handle);
+                        dev_handle=NULL;
+                     }
+                     alt_setting=0;
+                   } else {
+                    libusb_close(dev_handle);
+                    dev_handle=NULL;
+                   }
+                } else {
+                    printf ( "Unable to open device idx: %d, ret: %d\n", idx, rv );
+                }
+             }
+          }
+       }
+    }
     
+    if (!dev_handle) {
+        printf ( "Device not opened.\n" );
+    }
+    libusb_free_device_list(list,1);
 }
 void fx2::set_interface(int iface, int alt){
-    assert(dev_handle);
+    CHECK_OPEN()
     if (interface != iface) {
         libusb_release_interface(dev_handle,interface);
         int rv=libusb_claim_interface(dev_handle,iface);
     alt_setting=alt;
 }
 void fx2::close() {
-    assert(dev_handle);
+    CHECK_OPEN()
     libusb_release_interface(dev_handle,interface);
     libusb_close(dev_handle);
     dev_handle=NULL;
 
 
 int 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 ) {
- assert(dev_handle);
+ CHECK_OPEN(-1)
  return libusb_control_transfer (
     dev_handle,
     type,
 }
 
 int fx2::clear_halt(char ep) {
-    assert(dev_handle);
+    CHECK_OPEN(-1)
     return libusb_clear_halt(dev_handle,(unsigned char)ep);
 }
 
 int fx2::reset() {
-    assert(dev_handle);
+    CHECK_OPEN(-1)
     int rv=libusb_reset_device(dev_handle);
     if (rv==LIBUSB_ERROR_NO_DEVICE) {
         printf ( "Device Changed.  Closing\n");
 }
 
 int fx2::set_configuration(int configuration) {
-    assert(dev_handle);
+    CHECK_OPEN(-1)
     libusb_release_interface(dev_handle,interface);
     int rv=libusb_set_configuration(dev_handle,configuration);
     if (!rv) {
 
 
 bool fx2::ep_bulk(char* buf, int size, unsigned char ep, int timeout) {
- assert(dev_handle);
+ CHECK_OPEN(-1)
  int transferred;
  int rv=libusb_bulk_transfer ( dev_handle, ep, (unsigned char*)buf, size, &transferred, timeout );
 

File examples/fx2/cpp/fx2.h

          * open
          * vid = vendor id
          * pid = product id
+         * idx = nth device if there are more than one.
          *
          * Opens device with vid,pid and claims alt 0 of interface 0
          **/
-        void open(int vid=VID,int pid=PID);
+        void open(int vid=VID,int pid=PID, int idx=0);
         void set_interface(int interface, int alt_setting);
         bool isopen() { return dev_handle != NULL; }
         void close();

File examples/fx2/fx2load/__init__.py

 
 # use this functions 1st
 
-def openfx2(vid=0x04b4,pid=0x0082):
-    f.open(vid,pid)
+def openfx2(vid=0x04b4,pid=0x0082,idx=0):
+    f.open(vid,pid,idx)
 
 def reset_device(reset):
  print reset and "Put device in reset" or "Set device to run"