libftdi-git Archives

Subject: port libftdi to libusb-1.0 branch, master, updated. v0.17-234-g8b0b694

From: libftdi-git@xxxxxxxxxxxxxxxxxxxxxxx
To: libftdi-git@xxxxxxxxxxxxxxxxxxxxxxx
Date: Wed, 27 Jul 2011 18:16:15 +0200 (CEST)
The branch, master has been updated
       via  8b0b694fd737f5318adc29fa4cc48783a77a502e (commit)
      from  dc422d1bfd2adbfded632cc8e2e8065bf1afe529 (commit)


- Log -----------------------------------------------------------------
commit 8b0b694fd737f5318adc29fa4cc48783a77a502e
Author: Uwe Bonnes <bon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Date:   Wed Jul 20 11:12:52 2011 +0200

    Search for any devices with one of the default VID/PID, if no VID/PID if 
given

-----------------------------------------------------------------------

Summary of changes:
 examples/serial_test.c |  127 +++++++++++++++++++++++++++++++++++++----------
 1 files changed, 100 insertions(+), 27 deletions(-)

diff --git a/examples/serial_test.c b/examples/serial_test.c
index aec18d0..60c5a84 100644
--- a/examples/serial_test.c
+++ b/examples/serial_test.c
@@ -1,6 +1,6 @@
-/* serial_read.c
+/* serial_test.c
 
-   Read data via serial I/O
+   Read/write data via serial I/O
 
    This program is distributed under the GPL, version 2
 */
@@ -9,19 +9,35 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <getopt.h>
+#include <signal.h>
 #include <ftdi.h>
 
+static int exitRequested = 0;
+/*
+ * sigintHandler --
+ *
+ *    SIGINT handler, so we can gracefully exit when the user hits ctrl-C.
+ */
+static void
+sigintHandler(int signum)
+{
+    exitRequested = 1;
+}
+
 int main(int argc, char **argv)
 {
-    struct ftdi_context ftdic;
+    struct ftdi_context *ftdi;
     unsigned char buf[1024];
     int f, i;
-    int vid = 0x0403;
-    int pid = 0x6001;
+    int vid = 0;
+    int pid = 0;
     int baudrate = 115200;
     int interface = INTERFACE_ANY;
+    int do_write = 0;
+    unsigned int pattern;
+    int retval = EXIT_FAILURE;
 
-    while ((i = getopt(argc, argv, "i:v:p:b:")) != -1)
+    while ((i = getopt(argc, argv, "i:v:p:b:w::")) != -1)
     {
         switch (i)
         {
@@ -37,49 +53,106 @@ int main(int argc, char **argv)
             case 'b':
                 baudrate = strtoul(optarg, NULL, 0);
                 break;
+            case 'w':
+                do_write = 1;
+                pattern = strtoul(optarg, NULL, 0);
+                if (pattern > 0xff)
+                    fprintf(stderr, "Please provide a 8 bit pattern\n");
+                break;
             default:
-                fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] 
[-b baudrate]\n", *argv);
+                fprintf(stderr, "usage: %s [-i interface] [-v vid] [-p pid] 
[-b baudrate] [-w [pattern]]\n", *argv);
                 exit(-1);
         }
     }
 
     // Init
-    if (ftdi_init(&ftdic) < 0)
+    if ((ftdi = ftdi_new()) == 0)
     {
-        fprintf(stderr, "ftdi_init failed\n");
+        fprintf(stderr, "ftdi_new failed\n");
         return EXIT_FAILURE;
     }
 
-    // Select first interface
-    ftdi_set_interface(&ftdic, interface);
-
-    // Open device
-    f = ftdi_usb_open(&ftdic, vid, pid);
+    if (!vid && !pid && (interface == INTERFACE_ANY))
+    {
+        ftdi_set_interface(ftdi, INTERFACE_ANY);
+        struct ftdi_device_list *devlist;
+        int res;
+        if ((res = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0)
+        {
+            fprintf(stderr, "No FTDI with default VID/PID found\n");
+            goto do_deinit;
+        }
+        if (res == 1)
+        {
+            f = ftdi_usb_open_dev(ftdi,  devlist[0].dev);
+            if (f<0)
+            {
+                fprintf(stderr, "Unable to open device %d: (%s)",
+                        i, ftdi_get_error_string(ftdi));
+            }
+        }
+        ftdi_list_free(&devlist);
+        if (res > 1)
+        {
+            fprintf(stderr, "%d Devices found, please select Device with 
VID/PID\n", res);
+            /* TODO: List Devices*/
+            goto do_deinit;
+        }
+        if (res == 0)
+        {
+            fprintf(stderr, "No Devices found with default VID/PID\n");
+            goto do_deinit;
+        }
+    }
+    else
+    {
+        // Select interface
+        ftdi_set_interface(ftdi, interface);
+        
+        // Open device
+        f = ftdi_usb_open(ftdi, vid, pid);
+    }
     if (f < 0)
     {
-        fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, 
ftdi_get_error_string(&ftdic));
+        fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, 
ftdi_get_error_string(ftdi));
         exit(-1);
     }
 
     // Set baudrate
-    f = ftdi_set_baudrate(&ftdic, 115200);
+    f = ftdi_set_baudrate(ftdi, baudrate);
     if (f < 0)
     {
-        fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, 
ftdi_get_error_string(&ftdic));
+        fprintf(stderr, "unable to set baudrate: %d (%s)\n", f, 
ftdi_get_error_string(ftdi));
         exit(-1);
     }
+    
+    if (do_write)
+        for(i=0; i<1024; i++)
+            buf[i] = pattern;
 
-    // Read data forever
-    while ((f = ftdi_read_data(&ftdic, buf, sizeof(buf))) >= 0)
+    signal(SIGINT, sigintHandler);
+    while (!exitRequested)
     {
-        fprintf(stderr, "read %d bytes\n", f);
-        fwrite(buf, f, 1, stdout);
-        fflush(stderr);
-        fflush(stdout);
+        if (do_write)
+            f = ftdi_write_data(ftdi, buf, sizeof(buf));
+        else
+            f = ftdi_read_data(ftdi, buf, sizeof(buf));
+        if (f<0)
+            sleep(1);
+        else if(f> 0 && !do_write)
+        {
+            fprintf(stderr, "read %d bytes\n", f);
+            fwrite(buf, f, 1, stdout);
+            fflush(stderr);
+            fflush(stdout);
+        }
     }
+    signal(SIGINT, SIG_DFL);
+    retval =  EXIT_SUCCESS;
+            
+    ftdi_usb_close(ftdi);
+    do_deinit:
+    ftdi_free(ftdi);
 
-    ftdi_usb_close(&ftdic);
-    ftdi_deinit(&ftdic);
-
-    return 0;
+    return retval;
 }


hooks/post-receive
-- 
port libftdi to libusb-1.0

--
libftdi-git - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi-git+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread
  • port libftdi to libusb-1.0 branch, master, updated. v0.17-234-g8b0b694, libftdi-git <=