libftdi-git Archives

Subject: utility to write the EEPROM connected to a FTDI USB-chip branch, master, updated. v0.3-9-g996e9ee

From: libftdi-git@xxxxxxxxxxxxxxxxxxxxxxx
To: libftdi-git@xxxxxxxxxxxxxxxxxxxxxxx
Date: Mon, 16 Mar 2009 20:09:59 +0100 (CET)
The branch, master has been updated
       via  996e9eee10870815423301c8805545e45d106286 (commit)
      from  3cef01611b32299ef62e294db1a725c73ef7b6f8 (commit)


- Log -----------------------------------------------------------------
commit 996e9eee10870815423301c8805545e45d106286
Author: Thomas Jarosch <thomas.jarosch@xxxxxxxxxxxxx>
Date:   Mon Mar 16 20:09:32 2009 +0100

    Improve indentation and whitespace cleanup
    
    Reformatted using this astyle options:
    /usr/bin/astyle --indent=spaces=4 --indent-switches --brackets=break \
                    --convert-tabs --one-line=keep-statements 
--one-line=keep-blocks
    
    Should have done this for a long time...

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

Summary of changes:
 src/main.c |  234 +++++++++++++++++++++++++++++++++---------------------------
 1 files changed, 130 insertions(+), 104 deletions(-)

diff --git a/src/main.c b/src/main.c
index f42f642..fde53e8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -25,36 +25,38 @@
 #include <confuse.h>
 #include <ftdi.h>
 
-int main(int argc, char *argv[]) {
+int main(int argc, char *argv[])
+{
     /*
-       configuration options
+    configuration options
     */
-    cfg_opt_t opts[] = {
-       CFG_INT("vendor_id", 0, 0),
-       CFG_INT("product_id", 0, 0),
-       CFG_BOOL("self_powered", cfg_true, 0),
-       CFG_BOOL("remote_wakeup", cfg_true, 0),
-       CFG_BOOL("BM_type_chip", cfg_true, 0),
-       CFG_BOOL("in_is_isochronous", cfg_false, 0),
-       CFG_BOOL("out_is_isochronous", cfg_false, 0),
-       CFG_BOOL("suspend_pull_downs", cfg_false, 0),
-       CFG_BOOL("use_serial", cfg_false, 0),
-       CFG_BOOL("change_usb_version", cfg_false, 0),
-       CFG_INT("usb_version", 0, 0),
-       CFG_INT("max_power", 0, 0),
-       CFG_STR("manufacturer", "Acme Inc.", 0),
-       CFG_STR("product", "USB Serial Converter", 0),
-       CFG_STR("serial", "08-15", 0),
-       CFG_STR("filename", "", 0),
-       CFG_BOOL("flash_raw", cfg_false, 0),
-       CFG_END()
+    cfg_opt_t opts[] =
+    {
+        CFG_INT("vendor_id", 0, 0),
+        CFG_INT("product_id", 0, 0),
+        CFG_BOOL("self_powered", cfg_true, 0),
+        CFG_BOOL("remote_wakeup", cfg_true, 0),
+        CFG_BOOL("BM_type_chip", cfg_true, 0),
+        CFG_BOOL("in_is_isochronous", cfg_false, 0),
+        CFG_BOOL("out_is_isochronous", cfg_false, 0),
+        CFG_BOOL("suspend_pull_downs", cfg_false, 0),
+        CFG_BOOL("use_serial", cfg_false, 0),
+        CFG_BOOL("change_usb_version", cfg_false, 0),
+        CFG_INT("usb_version", 0, 0),
+        CFG_INT("max_power", 0, 0),
+        CFG_STR("manufacturer", "Acme Inc.", 0),
+        CFG_STR("product", "USB Serial Converter", 0),
+        CFG_STR("serial", "08-15", 0),
+        CFG_STR("filename", "", 0),
+        CFG_BOOL("flash_raw", cfg_false, 0),
+        CFG_END()
     };
     cfg_t *cfg;
-    
+
     /*
-       normal variables
-    */ 
-    int        _read = 0, _erase = 0, _flash = 0;
+    normal variables
+    */
+    int _read = 0, _erase = 0, _flash = 0;
     unsigned char eeprom_buf[128];
     char *filename;
     int size_check;
@@ -66,32 +68,37 @@ int main(int argc, char *argv[]) {
 
     printf("\nFTDI eeprom generator v%s\n", VERSION);
     printf ("(c) Intra2net AG <opensource@xxxxxxxxxxxxx>\n");
-    
-    if (argc != 2 && argc != 3) {
-       printf("Syntax: %s [commands] config-file\n", argv[0]);
-       printf("Valid commands:\n");
-       printf("--read-eeprom           Read eeprom and write to -filename- 
from config-file\n");
-       printf("--erase-eeprom          Erase eeprom\n");
-       printf("--flash-eeprom          Flash eeprom\n");
-       exit (-1);
+
+    if (argc != 2 && argc != 3)
+    {
+        printf("Syntax: %s [commands] config-file\n", argv[0]);
+        printf("Valid commands:\n");
+        printf("--read-eeprom  Read eeprom and write to -filename- from 
config-file\n");
+        printf("--erase-eeprom  Erase eeprom\n");
+        printf("--flash-eeprom  Flash eeprom\n");
+        exit (-1);
     }
-    
-    if (argc == 3) {
-       if (strcmp(argv[1], "--read-eeprom") == 0)
-           _read = 1;
-       if (strcmp(argv[1], "--erase-eeprom") == 0)
-           _erase = 1;
-       if (strcmp(argv[1], "--flash-eeprom") == 0)
-           _flash = 1;
-       
-       argc_filename = 2;
-    } else {
-       argc_filename = 1;
+
+    if (argc == 3)
+    {
+        if (strcmp(argv[1], "--read-eeprom") == 0)
+            _read = 1;
+        if (strcmp(argv[1], "--erase-eeprom") == 0)
+            _erase = 1;
+        if (strcmp(argv[1], "--flash-eeprom") == 0)
+            _flash = 1;
+
+        argc_filename = 2;
+    }
+    else
+    {
+        argc_filename = 1;
     }
 
-    if ((fp = fopen(argv[argc_filename], "r")) == NULL) {
-       printf ("Can't open configuration file\n");
-       exit (-1);
+    if ((fp = fopen(argv[argc_filename], "r")) == NULL)
+    {
+        printf ("Can't open configuration file\n");
+        exit (-1);
     }
     fclose (fp);
 
@@ -100,7 +107,7 @@ int main(int argc, char *argv[]) {
     filename = cfg_getstr(cfg, "filename");
 
     if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0)
-       printf("Hint: Self powered devices should have a max_power setting of 
0.\n");
+        printf("Hint: Self powered devices should have a max_power setting of 
0.\n");
 
     ftdi_init(&ftdi);
     ftdi_eeprom_initdefaults (&eeprom);
@@ -126,27 +133,32 @@ int main(int argc, char *argv[]) {
     eeprom.serial = cfg_getstr(cfg, "serial");
 
 
-    if (_read > 0 || _erase > 0 || _flash > 0) {
-       i = ftdi_usb_open(&ftdi, eeprom.vendor_id, eeprom.product_id);
-       
-       if (i == 0) {
-          printf("EEPROM size: %d\n", ftdi.eeprom_size);
+    if (_read > 0 || _erase > 0 || _flash > 0)
+    {
+        i = ftdi_usb_open(&ftdi, eeprom.vendor_id, eeprom.product_id);
+
+        if (i == 0)
+        {
+            printf("EEPROM size: %d\n", ftdi.eeprom_size);
+        }
+        else
+        {
+            printf("Unable to find FTDI devices under given vendor/product id: 
0x%X/0x%X\n", eeprom.vendor_id, eeprom.product_id);
+            printf("Error code: %d (%s)\n", i, ftdi_get_error_string(&ftdi));
+            printf("Retrying with default FTDI id.\n");
+
+            i = ftdi_usb_open(&ftdi, 0x0403, 0x6001);
+            if (i != 0)
+            {
+                printf("Error: %s\n", ftdi.error_str);
+                exit (-1);
+            }
         }
-       else {
-           printf("Unable to find FTDI devices under given vendor/product id: 
0x%X/0x%X\n", eeprom.vendor_id, eeprom.product_id);
-           printf("Error code: %d (%s)\n", i, ftdi_get_error_string(&ftdi));
-           printf("Retrying with default FTDI id.\n");
-
-           i = ftdi_usb_open(&ftdi, 0x0403, 0x6001);
-           if (i != 0) {
-               printf("Error: %s\n", ftdi.error_str);
-               exit (-1);
-           }
-       }
     }
 
-    if (_read > 0) {
-      printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(&ftdi, (char 
*)eeprom_buf));
+    if (_read > 0)
+    {
+        printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(&ftdi, (char 
*)eeprom_buf));
 
         ftdi_eeprom_decode(&eeprom, eeprom_buf, ftdi.eeprom_size);
         /* Debug output */
@@ -168,63 +180,77 @@ int main(int argc, char *argv[]) {
         printf("serial = \"%s\"\n", eeprom.serial);
         */
 
-       if (filename != NULL && strlen(filename) > 0) {
-           FILE *fp = fopen (filename, "wb");
-           fwrite (eeprom_buf, 1, 128, fp);
-           fclose (fp);
-       } else {
-           printf("Warning: Not writing eeprom, you must supply a valid 
filename\n");
-       }
+        if (filename != NULL && strlen(filename) > 0)
+        {
+            FILE *fp = fopen (filename, "wb");
+            fwrite (eeprom_buf, 1, 128, fp);
+            fclose (fp);
+        }
+        else
+        {
+            printf("Warning: Not writing eeprom, you must supply a valid 
filename\n");
+        }
 
-       goto cleanup;
+        goto cleanup;
     }
 
-    if (_erase > 0) {
-       printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(&ftdi));
-    }    
+    if (_erase > 0)
+    {
+        printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(&ftdi));
+    }
 
     size_check = ftdi_eeprom_build(&eeprom, eeprom_buf);
-    if (size_check == -1) {
-       printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for 
your strings).\n");
-       printf ("You need to short your string by: %d bytes\n", size_check);
-       goto cleanup;
-    } else {
-       printf ("Used eeprom space: %d bytes\n", 128-size_check); 
+    if (size_check == -1)
+    {
+        printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for 
your strings).\n");
+        printf ("You need to short your string by: %d bytes\n", size_check);
+        goto cleanup;
+    }
+    else
+    {
+        printf ("Used eeprom space: %d bytes\n", 128-size_check);
     }
 
-    if (_flash > 0) {
-      if (cfg_getbool(cfg, "flash_raw")) {
-        if (filename != NULL && strlen(filename) > 0) {
-          FILE *fp = fopen(filename, "rb");
-          fread(eeprom_buf, 1, 128, fp);
-          fclose(fp);
+    if (_flash > 0)
+    {
+        if (cfg_getbool(cfg, "flash_raw"))
+        {
+            if (filename != NULL && strlen(filename) > 0)
+            {
+                FILE *fp = fopen(filename, "rb");
+                fread(eeprom_buf, 1, 128, fp);
+                fclose(fp);
+            }
         }
-      }
-      printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(&ftdi, (char 
*)eeprom_buf));
+        printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(&ftdi, (char 
*)eeprom_buf));
     }
 
     // Write to file?
-    if (filename != NULL && strlen(filename) > 0) {
+    if (filename != NULL && strlen(filename) > 0)
+    {
         fp = fopen(filename, "w");
-       if (fp == NULL) {
-           printf ("Can't write eeprom file.\n");
-           exit (-1);
-       } else 
-           printf ("Writing to file: %s\n", filename);
-       
-       fwrite(eeprom_buf, 128, 1, fp);
-       fclose(fp);
+        if (fp == NULL)
+        {
+            printf ("Can't write eeprom file.\n");
+            exit (-1);
+        }
+        else
+            printf ("Writing to file: %s\n", filename);
+
+        fwrite(eeprom_buf, 128, 1, fp);
+        fclose(fp);
     }
 
 cleanup:
-    if (_read > 0 || _erase > 0 || _flash > 0) {
+    if (_read > 0 || _erase > 0 || _flash > 0)
+    {
         printf("FTDI close: %d\n", ftdi_usb_close(&ftdi));
     }
 
     ftdi_deinit (&ftdi);
-    
+
     cfg_free(cfg);
-    
+
     printf("\n");
     return 0;
 }


hooks/post-receive
-- 
utility to write the EEPROM connected to a FTDI USB-chip

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

Current Thread
  • utility to write the EEPROM connected to a FTDI USB-chip branch, master, updated. v0.3-9-g996e9ee, libftdi-git <=