libftdi Archives

Subject: Re: Programming an FT245RL

From: Uwe Bonnes <bon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Thu, 27 Oct 2011 10:58:28 +0200
>>>>> "Karl" == Karl Cunningham <karlc@xxxxxxxxxx> writes:

    Karl> Compiled and installed lubftdi-1.0 from
    Karl> git://developer.intra2net.com/libftdi

> git remote -v
origin  git://developer.intra2net.com/libftdi (fetch)
origin  git://developer.intra2net.com/libftdi (push)

> git branch -r
  origin/HEAD -> origin/master
  origin/autoconf-removal
  origin/eeprom-new
  origin/get-library-version
  origin/libftdi-0.x
  origin/master
  origin/new-baudrate-code

> git checkout origin/eeprom-new -b eeprom-new

...

    Karl> Any ideas?

Most work has been done on the ftdi-1.0 branch. Probably Thomas is waiting
on feedback before merging the ftdi_eeprom branch

While ./ftdi_eeprom/ftdi_eeprom is run time configurable, examples/eeprom is 
only
compile time configurable, beside an eventual serial number, but may be
worth a look too. I have appended the tool based on examples/eeprom ( or
probably vice-versa) that I use to configure local devices. You can easily
add your device. Probably your device has a misconfigured EEPROM now. The
first thing to do is to erase the eeprom. This will not erase the serial
number.

Bye
-- 
Uwe Bonnes                bon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------
>From 4a6617b404268c548e87a167d039cd45c16259bc Mon Sep 17 00:00:00 2001
From: Uwe Bonnes <bon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 7 Oct 2011 19:16:24 +0200
Subject: Lokal file

---
 examples/CMakeLists.txt   |    2 +
 examples/configure_ftdi.c |  442 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 444 insertions(+), 0 deletions(-)
 create mode 100644 examples/configure_ftdi.c

diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 29646f5..9ce817c 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -22,6 +22,7 @@ if (EXAMPLES)
     add_executable(baud_test baud_test.c)
     add_executable(stream_test stream_test.c)
     add_executable(eeprom eeprom.c)
+    add_executable(configure_ftdi configure_ftdi.c)
 
     # Linkage
     target_link_libraries(simple ftdi)
@@ -34,6 +35,7 @@ if (EXAMPLES)
     target_link_libraries(baud_test ftdi)
     target_link_libraries(stream_test ftdi)
     target_link_libraries(eeprom ftdi)
+    target_link_libraries(configure_ftdi ftdi)
 
     # libftdi++ examples
     if(FTDI_BUILD_CPP)
diff --git a/examples/configure_ftdi.c b/examples/configure_ftdi.c
new file mode 100644
index 0000000..0653373
--- /dev/null
+++ b/examples/configure_ftdi.c
@@ -0,0 +1,442 @@
+/* LIBFTDI EEPROM access example
+
+   This program is distributed under the GPL, version 2
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <ftdi.h>
+
+enum IKDA_TYPES 
+{LLIF0 = 0, LLIF1 = 1, LLBBC10 = 2, FTDIJTAG = 3, RS422TOUSB =4,
+ KNOB2USB= 5, LLBBC_CONN = 6, CPS_CONN = 7,QM07 = 8, USB2CAN = 9,
+ L_MOTCTL = 10, FT2232TEST = 11, QM07_CONN = 12, TUNKNOWN = -1};
+void usage(char * name)
+{
+    fprintf(stderr, "usage: %s [options]\n", name);
+    fprintf(stderr, "\t-t [type]\n");
+    fprintf(stderr, "\t-d\tDump EEPROM only. Work with default values for 128 
Byte "
+            "EEPROM\n\t\tor for 256 Byte EEPROM if some [num] is given\n");
+    fprintf(stderr, "\t-c Run on already programmed device\n");
+    fprintf(stderr, "\t-n [number]\n");
+    fprintf(stderr, "\t-e erase\n");
+    fprintf(stderr, "\t-p <number> Search for device with PID == number\n");
+    fprintf(stderr, "\t-v <number> Search for device with VID == number\n");
+    fprintf(stderr, "\t-P <string? Search for device with given "
+            "product description\n");
+    fprintf(stderr, "\t-S <string? Search for device with given "
+            "serial number\n");
+    fprintf(stderr, "\t-r Read Device\n");
+    fprintf(stderr, "\t-d Dump EEPROM only\n");
+    fprintf(stderr, "[type] can be FTDIJTAG, LLBBC10, LLBBC_CONN, ");
+    fprintf(stderr, "LLBBC_INTERFACE0, LLBBC_INTERFACE1, QM07(_PU|-PU), 
RS422TOUSB, ");
+    fprintf(stderr, "KNOB2USB, CPS_CONN, USB2CAN, L_MOTCTL, FT2232TEST, 
QM07_CONN\n");
+}
+
+void dump_eeprom(struct ftdi_context *ftdi)
+{
+    unsigned char buf[256];
+    int f, i, j, value;
+    int size = 0;
+
+    ftdi_get_eeprom_value(ftdi, CHIP_SIZE, & value);
+    if (value <0)
+    {
+        fprintf(stderr, "No EEPROM found or EEPROM empty\n");
+        fprintf(stderr, "On empty EEPROM, use -w option to write default 
values\n");
+        return;
+    }
+
+    fprintf(stderr, "Chip type %d ftdi_eeprom_size: %d\n", ftdi->type, 
+            value);
+    if (ftdi->type == TYPE_R)
+        size = 0xa0;
+    else
+        size = value;
+    ftdi_get_eeprom_buf(ftdi, buf, size);
+    for(i=0; i < size; i += 16)
+    {
+       fprintf(stdout,"0x%03x:", i);
+       
+       for (j = 0; j< 8; j++)
+           fprintf(stdout," %02x", buf[i+j]);
+       fprintf(stdout," ");
+       for (; j< 16; j++)
+           fprintf(stdout," %02x", buf[i+j]);
+       fprintf(stdout," ");
+       for (j = 0; j< 8; j++)
+           fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.');
+       fprintf(stdout," ");
+       for (; j< 16; j++)
+           fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.');
+       fprintf(stdout,"\n");
+    }
+
+    f = ftdi_eeprom_decode(ftdi, 1);
+    if (f < 0)
+    {
+        fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n", 
+               f, ftdi_get_error_string(ftdi));
+        exit(-1);
+    }
+
+    ftdi_usb_close(ftdi);
+    ftdi_free(ftdi);
+}
+
+int main(int argc, char **argv)
+{
+    struct ftdi_context *ftdi;
+    int f, i;
+    int vid = 0;
+    int pid = 0;
+    char *desc    = NULL;
+    char *serial  = NULL;
+    char serial_string[5];
+    int use_configured = 0;
+    int erase = 0;
+    int do_read = 0;
+    int num = 0;
+    int dump = 0;
+    char *type_string = NULL;
+    enum IKDA_TYPES type = TUNKNOWN;
+
+    if ((ftdi = ftdi_new()) == 0)
+    {
+       fprintf(stderr, "Failed to allocate ftdi structure :%s \n", 
+                  ftdi_get_error_string(ftdi));
+        return EXIT_FAILURE;
+    }
+
+    while ((i = getopt(argc, argv, "cden:v:p:l:P:t:S:r")) != -1)
+    {
+        switch (i)
+        {
+        case 'c':
+            use_configured = 1;
+            break;
+        case 'd':
+            dump = 1;
+            break;
+        case 'e':
+            erase = 1;
+            break;
+        case 'v':
+               vid = strtoul(optarg, NULL, 0);
+               break;
+       case 'p':
+               pid = strtoul(optarg, NULL, 0);
+               break;
+       case 'n':
+                if (sscanf(optarg, "%d",&num) !=1)
+                    num = 0;
+                break;
+       case 't':
+               type_string = optarg;
+               break;
+       case 'P':
+                desc = optarg;
+               break;
+       case 'S':
+               serial = optarg;
+               break;
+       case 'r':
+               do_read  = 1;
+               break;
+       default:
+            usage(argv[0]);
+            exit(-1);
+        }
+    }
+
+    if ((num <1 || num >9999) && !erase)
+    {
+        fprintf(stderr,"Ungueltige oder keine Seriennummer,"
+                "daher wird nur angezeigt\n");
+        do_read  = 1;
+        dump = 1;
+    }
+    else
+        sprintf(serial_string,"%04d", num);
+
+
+    if(!type_string)
+        type = TUNKNOWN;
+    else if (!strcasecmp(type_string,"LLBBC_INTERFACE0"))
+        type = LLIF0;
+    else if (!strcasecmp(type_string,"LLBBC_INTERFACE1"))
+        type = LLIF1;
+    else if (!strcasecmp(type_string,"LLBBC10"))
+        type = LLBBC10;
+    else if (!strcasecmp(type_string,"FTDIJTAG"))
+        type = FTDIJTAG;
+    else if (!strcasecmp(type_string,"RS422TOUSB"))
+        type = RS422TOUSB;
+    else if (!strcasecmp(type_string,"KNOB2USB"))
+        type = KNOB2USB;
+    else if (!strcasecmp(type_string,"LLBBC_CONN"))
+        type = LLBBC_CONN;
+    else if ((!strcasecmp(type_string,"CPS_CONN")) ||
+             (!strcasecmp(type_string,"CPS-CONN")))
+        type = CPS_CONN;
+    else if ((!strcasecmp(type_string,"QM07")) ||
+             (!strcasecmp(type_string,"QM07_PU"))||
+             (!strcasecmp(type_string,"QM07-PU")))
+        type = QM07;
+    else if (!strcasecmp(type_string,"USB2CAN"))
+        type = USB2CAN;
+    else if (!strcasecmp(type_string,"L_MOTCTL"))
+        type = L_MOTCTL;
+    else if (!strcasecmp(type_string,"FT2232TEST"))
+        type = FT2232TEST;
+    else if (!strcasecmp(type_string,"QM07_CONN"))
+        type = QM07_CONN;
+
+    if(!vid)
+        vid = 0x403;
+    switch(type)
+    {
+    case KNOB2USB:
+    case RS422TOUSB:
+    case USB2CAN:
+    case QM07:
+        if(!desc)
+            desc="Dual RS232";
+    case LLIF0:
+    case LLIF1:
+    case LLBBC10:
+    case FTDIJTAG:
+    case L_MOTCTL:
+    case FT2232TEST:
+        if (!pid)
+            pid = 0x6010;
+        if(!desc)
+            desc="Dual RS232-HS";
+        break;
+    case LLBBC_CONN:
+    case CPS_CONN:
+    case QM07_CONN:
+        if (!pid)
+            pid = 0x6001;
+        if(!desc)
+            desc="FT232R USB UART";
+        break;
+    default:
+        if (!pid)
+            pid = 0x6010;
+        if(!do_read && !erase)
+        {
+            fprintf(stderr,"Bitte Schnittstellenbezeichnung angeben\n");
+            exit(-1);
+        }
+    }
+    if(use_configured)
+    {
+        switch(type)
+        {
+        case LLIF0:
+            desc="LLBBC_INTERFACE0";
+            break;
+        case LLIF1:
+            desc="LLBBC_INTERFACE1";
+            break;
+        case LLBBC10:
+            desc="LLBBC10";
+            break;
+        case FTDIJTAG:
+            desc="FTDIJTAG";
+            break;
+        case RS422TOUSB:
+            desc="RS422TOUSB";
+            break;
+        case KNOB2USB:
+            desc="KNOB2USB";
+            break;
+        case LLBBC_CONN:
+            desc="LLBBC_CONN";
+            break;
+        case CPS_CONN:
+             desc="CPS_CONN";
+             break;
+        case QM07:
+             desc="QM07_PU";
+             break;
+        case QM07_CONN:
+             desc="QM07_CONN";
+             break;
+        case USB2CAN:
+             desc="USB2CAN";
+             break;
+        case L_MOTCTL:
+             desc="L_MOTCTL";
+             break;
+        case FT2232TEST:
+             desc="FT2232TEST";
+             break;
+        default:
+            desc = NULL;
+            break;
+        }
+    }
+
+    // Select first interface
+    ftdi_set_interface(ftdi, INTERFACE_ANY);
+
+    // Open device
+    f = ftdi_usb_open_desc(ftdi, vid, pid, desc, serial);
+    if (f < 0)
+    {
+        fprintf(stderr, "Device VID 0x%04x PID 0x%04x", vid, pid);
+        if(desc)
+            fprintf(stderr, " Desc %s", desc);
+        if(serial)
+            fprintf(stderr, " Serial %s", serial);
+        fprintf(stderr, "\n");
+        fprintf(stderr, "unable to open ftdi device: %d (%s)\n", 
+               f, ftdi_get_error_string(ftdi));
+
+        exit(-1);
+    }
+
+    if (do_read)
+    {
+        f = ftdi_read_eeprom(ftdi);
+        if (f < 0)
+        {
+            fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n", 
+                    f, ftdi_get_error_string(ftdi));
+            exit(-1);
+        }
+    }
+    else if (erase)
+    {
+        f = ftdi_erase_eeprom(ftdi);
+        if (f < 0)
+        {
+            fprintf(stderr, "ftdi_erase_eeprom: %d (%s)\n", 
+                    f, ftdi_get_error_string(ftdi));
+            exit(-1);
+        }
+        return 0;
+    }
+    else
+    {
+        int res = 0;
+        ftdi_eeprom_initdefaults(ftdi, "IKDA", desc, serial_string);
+       switch (type)
+        {
+        case LLIF0:
+            res |= ftdi_set_eeprom_value(ftdi, SELF_POWERED, 1);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_A_TYPE, 
CHANNEL_IS_FIFO);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_A_DRIVER, DRIVER_VCP);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_B_TYPE, 
CHANNEL_IS_FIFO);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_B_DRIVER, DRIVER_VCP);
+            res |= ftdi_set_eeprom_value(ftdi, GROUP1_DRIVE,  DRIVE_16MA);
+            res |= ftdi_set_eeprom_value(ftdi, SUSPEND_PULL_DOWNS, 1);
+            break;
+        case LLIF1:
+            res |= ftdi_set_eeprom_value(ftdi, SELF_POWERED, 1);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_A_TYPE, 
CHANNEL_IS_FIFO);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_A_DRIVER, DRIVER_VCP);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_B_TYPE, 
CHANNEL_IS_FIFO);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_B_DRIVER, DRIVER_VCP);
+            res |= ftdi_set_eeprom_value(ftdi, GROUP1_DRIVE,  DRIVE_16MA);
+            res |= ftdi_set_eeprom_value(ftdi, SUSPEND_PULL_DOWNS, 1);
+            break;
+        case LLBBC10:
+            res |= ftdi_set_eeprom_value(ftdi, SELF_POWERED, 1);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_A_TYPE, 
CHANNEL_IS_FIFO);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_A_DRIVER, DRIVER_VCP);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_B_TYPE, 
CHANNEL_IS_FIFO);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_B_DRIVER, DRIVER_VCP);
+            res |= ftdi_set_eeprom_value(ftdi, GROUP1_DRIVE,  DRIVE_16MA);
+            res |= ftdi_set_eeprom_value(ftdi, SUSPEND_PULL_DOWNS, 1);
+            break;
+        case FTDIJTAG:
+            res |= ftdi_set_eeprom_value(ftdi, SELF_POWERED, 0);
+            res |= ftdi_set_eeprom_value(ftdi, MAX_POWER, 500);
+            res |= ftdi_set_eeprom_value(ftdi, GROUP1_DRIVE,  DRIVE_16MA);
+            break;
+         case RS422TOUSB:
+            res |= ftdi_set_eeprom_value(ftdi, MAX_POWER, 500);
+            break;
+         case USB2CAN:
+           res |= ftdi_set_eeprom_value(ftdi, MAX_POWER, 500);
+            break;
+         case KNOB2USB:
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_B_TYPE, 
CHANNEL_IS_FIFO);
+            break;
+         case LLBBC_CONN:
+            break;
+         case CPS_CONN:
+            break;
+         case QM07_CONN:
+             break;
+         case QM07:
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_B_TYPE, 
CHANNEL_IS_FIFO);
+           break;
+        case L_MOTCTL:
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_A_TYPE, 
CHANNEL_IS_FIFO);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_B_TYPE, 
CHANNEL_IS_FIFO);
+            break;
+        case FT2232TEST:
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_A_TYPE, 
CHANNEL_IS_FIFO);
+            res |= ftdi_set_eeprom_value(ftdi, CHANNEL_B_TYPE, 
CHANNEL_IS_FIFO);
+            res |= ftdi_set_eeprom_value(ftdi, MAX_POWER, 500);
+            break;
+        case TUNKNOWN:
+            fprintf(stderr, " Beende, da unerwartet\n");
+            exit (-2);
+        }
+        if(!dump)
+        {
+            int value;
+            f=(ftdi_erase_eeprom(ftdi));
+            if (f < 0)
+            {
+                fprintf(stderr, "ftdi_erase_eeprom: %d (%s)\n", 
+                        f, ftdi_get_error_string(ftdi));
+                exit(-1);
+            }
+            if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
+            {
+                fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
+                        f, ftdi_get_error_string(ftdi));
+            }
+            if (value == -1)
+                fprintf(stderr, "No EEPROM\n");
+            else if (value == 0)
+                fprintf(stderr, "Internal EEPROM\n");
+            else
+                fprintf(stderr, "Found 93x%02x\n", value);
+        }
+            
+        f=(ftdi_eeprom_build(ftdi));
+        if (f < 0)
+        {
+            fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n", 
+                    f, ftdi_get_error_string(ftdi));
+            exit(-1);
+        }
+    }
+    
+    if (dump)
+        dump_eeprom(ftdi);
+    else
+    {
+       fprintf(stderr,"Manufacturer: IKDA\n");
+        fprintf(stderr,"Product: %s\n", desc);
+        fprintf(stderr,"Serial: %s\n", serial_string);
+        f = ftdi_write_eeprom(ftdi);
+        {
+            fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n", 
+                    f, ftdi_get_error_string(ftdi));
+            exit(-1);
+        }
+    }
+    return 0;
+}
-- 
1.7.3.4


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

Current Thread