X-Git-Url: http://developer.intra2net.com/git/?a=blobdiff_plain;f=ftdi_eeprom%2Fmain.c;h=ad7de9388193996157b9f0e91acd0d558ac9660d;hb=be4bae37b3f851d7e06610fe474d84a3b2371efb;hp=03b24b3065f6d79bd17e61456787526ab520f88e;hpb=d327f924c20f61561facfd9537fc495beb3fc315;p=libftdi diff --git a/ftdi_eeprom/main.c b/ftdi_eeprom/main.c index 03b24b3..ad7de93 100644 --- a/ftdi_eeprom/main.c +++ b/ftdi_eeprom/main.c @@ -16,7 +16,6 @@ /* TODO: - - Use new eeprom get/set functions - Remove 128 bytes limit - Merge Uwe's eeprom tool. Current features: - Init eeprom defaults based upon eeprom type @@ -108,13 +107,13 @@ int main(int argc, char *argv[]) CFG_INT("product_id", 0, 0), CFG_BOOL("self_powered", cfg_true, 0), CFG_BOOL("remote_wakeup", cfg_true, 0), - CFG_STR_LIST("chip_type", "{BM,R,other}", 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("default_pid", 0x6001, 0), CFG_INT("max_power", 0, 0), CFG_STR("manufacturer", "Acme Inc.", 0), CFG_STR("product", "USB Serial Converter", 0), @@ -143,7 +142,9 @@ int main(int argc, char *argv[]) normal variables */ int _read = 0, _erase = 0, _flash = 0; - unsigned char eeprom_buf[128]; // TODO: Kill this and look for other hardcoded places of 128 bytes + + int my_eeprom_size = 0; + unsigned char *eeprom_buf = NULL; char *filename; int size_check; int i, argc_filename; @@ -168,11 +169,15 @@ int main(int argc, char *argv[]) { if (strcmp(argv[1], "--read-eeprom") == 0) _read = 1; - if (strcmp(argv[1], "--erase-eeprom") == 0) + else if (strcmp(argv[1], "--erase-eeprom") == 0) _erase = 1; - if (strcmp(argv[1], "--flash-eeprom") == 0) + else if (strcmp(argv[1], "--flash-eeprom") == 0) _flash = 1; - + else + { + printf ("Can't open configuration file\n"); + exit (-1); + } argc_filename = 2; } else @@ -201,21 +206,67 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - ftdi_eeprom_initdefaults (ftdi, "Acme Inc.", "FTDI Chip", NULL); + if (_read > 0 || _erase > 0 || _flash > 0) + { + int vendor_id = cfg_getint(cfg, "vendor_id"); + int product_id = cfg_getint(cfg, "product_id"); - eeprom_set_value(ftdi, VENDOR_ID, cfg_getint(cfg, "vendor_id")); - eeprom_set_value(ftdi, PRODUCT_ID, cfg_getint(cfg, "product_id")); + i = ftdi_usb_open(ftdi, vendor_id, product_id); + + if (i != 0) + { + int default_pid = cfg_getint(cfg, "default_pid"); + printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", vendor_id, product_id); + printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi)); + printf("Retrying with default FTDI pid=%#04x.\n", default_pid); + + i = ftdi_usb_open(ftdi, 0x0403, default_pid); + if (i != 0) + { + printf("Error: %s\n", ftdi->error_str); + exit (-1); + } + } + } + ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"), + cfg_getstr(cfg, "product"), + cfg_getstr(cfg, "serial")); + + printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi)); + eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size); + // TODO: Do we know the eeprom size already? + printf("EEPROM size: %d\n", my_eeprom_size); + + if (_read > 0) + { + ftdi_eeprom_decode(ftdi, 0 /* debug: 1 */); + + eeprom_buf = malloc(my_eeprom_size); + ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size); + + if (eeprom_buf == NULL) + { + fprintf(stderr, "Malloc failed, aborting\n"); + goto cleanup; + } + if (filename != NULL && strlen(filename) > 0) + { + + FILE *fp = fopen (filename, "wb"); + fwrite (eeprom_buf, 1, my_eeprom_size, fp); + fclose (fp); + } + else + { + printf("Warning: Not writing eeprom, you must supply a valid filename\n"); + } - // TODO: Support all chip types - char *type = cfg_getstr(cfg, "chip_type"); - if (!strcmp(type, "BM")) { - ftdi->type = TYPE_BM; - } else if (!strcmp(type, "R")) { - ftdi->type = TYPE_R; - } else { - ftdi->type = TYPE_AM; + goto cleanup; } + eeprom_set_value(ftdi, VENDOR_ID, cfg_getint(cfg, "vendor_id")); + eeprom_set_value(ftdi, PRODUCT_ID, cfg_getint(cfg, "product_id")); + eeprom_set_value(ftdi, SELF_POWERED, cfg_getbool(cfg, "self_powered")); eeprom_set_value(ftdi, REMOTE_WAKEUP, cfg_getbool(cfg, "remote_wakeup")); eeprom_set_value(ftdi, MAX_POWER, cfg_getint(cfg, "max_power")); @@ -229,9 +280,6 @@ int main(int argc, char *argv[]) eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version")); - ftdi->eeprom->manufacturer = cfg_getstr(cfg, "manufacturer"); - ftdi->eeprom->product = cfg_getstr(cfg, "product"); - ftdi->eeprom->serial = cfg_getstr(cfg, "serial"); eeprom_set_value(ftdi, HIGH_CURRENT, cfg_getbool(cfg, "high_current")); eeprom_set_value(ftdi, CBUS_FUNCTION_0, str_to_cbus(cfg_getstr(cfg, "cbus0"), 13)); eeprom_set_value(ftdi, CBUS_FUNCTION_1, str_to_cbus(cfg_getstr(cfg, "cbus1"), 13)); @@ -249,74 +297,14 @@ int main(int argc, char *argv[]) if (cfg_getbool(cfg, "invert_ri")) invert |= INVERT_RI; eeprom_set_value(ftdi, INVERT, invert); - if (_read > 0 || _erase > 0 || _flash > 0) - { - int vendor_id = 0, product_id = 0; - eeprom_get_value(ftdi, VENDOR_ID, &vendor_id); - eeprom_get_value(ftdi, PRODUCT_ID, &product_id); - - i = ftdi_usb_open(ftdi, vendor_id, product_id); - - if (i == 0) - { - // TODO: Do we know the eeprom size already? - 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", vendor_id, 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)); - - ftdi_eeprom_decode(ftdi, 0); - /* Debug output */ - /* - const char* chip_types[] = {"other", "BM", "R"}; - printf("vendor_id = \"%04x\"\n", eeprom->vendor_id); - printf("product_id = \"%04x\"\n", eeprom->product_id); - printf("chip_type = \"%s\"\n", - (eeprom->chip_type > 0x06) || (eeprom->chip_type & 0x01) ? "unknown": - chip_types[eeprom->chip_type>>1]); - printf("self_powered = \"%s\"\n", eeprom->self_powered?"true":"false"); - printf("remote_wakeup = \"%s\"\n", eeprom->remote_wakeup?"true":"false"); - printf("max_power = \"%d\"\n", eeprom->max_power); - printf("in_is_isochronous = \"%s\"\n", eeprom->in_is_isochronous?"true":"false"); - printf("out_is_isochronous = \"%s\"\n", eeprom->out_is_isochronous?"true":"false"); - printf("suspend_pull_downs = \"%s\"\n", eeprom->suspend_pull_downs?"true":"false"); - printf("use_serial = \"%s\"\n", eeprom->use_serial?"true":"false"); - printf("change_usb_version = \"%s\"\n", eeprom->change_usb_version?"true":"false"); - printf("usb_version = \"%d\"\n", eeprom->usb_version); - printf("manufacturer = \"%s\"\n", eeprom->manufacturer); - printf("product = \"%s\"\n", eeprom->product); - 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"); - } - - goto cleanup; - } + eeprom_set_value(ftdi, CHANNEL_A_DRIVER, DRIVER_VCP); + eeprom_set_value(ftdi, CHANNEL_B_DRIVER, DRIVER_VCP); + eeprom_set_value(ftdi, CHANNEL_C_DRIVER, DRIVER_VCP); + eeprom_set_value(ftdi, CHANNEL_D_DRIVER, DRIVER_VCP); + eeprom_set_value(ftdi, CHANNEL_A_RS485, 0); + eeprom_set_value(ftdi, CHANNEL_B_RS485, 0); + eeprom_set_value(ftdi, CHANNEL_C_RS485, 0); + eeprom_set_value(ftdi, CHANNEL_D_RS485, 0); if (_erase > 0) { @@ -335,7 +323,7 @@ int main(int argc, char *argv[]) } else { - printf ("Used eeprom space: %d bytes\n", 128-size_check); + printf ("Used eeprom space: %d bytes\n", my_eeprom_size-size_check); } if (_flash > 0) @@ -344,16 +332,19 @@ int main(int argc, char *argv[]) { if (filename != NULL && strlen(filename) > 0) { + eeprom_buf = malloc(my_eeprom_size); FILE *fp = fopen(filename, "rb"); - fread(eeprom_buf, 1, 128, fp); + fread(eeprom_buf, 1, my_eeprom_size, fp); fclose(fp); + + ftdi_set_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size); } } printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi)); } // Write to file? - if (filename != NULL && strlen(filename) > 0) + if (filename != NULL && strlen(filename) > 0 && !cfg_getbool(cfg, "flash_raw")) { fp = fopen(filename, "w"); if (fp == NULL) @@ -364,17 +355,24 @@ int main(int argc, char *argv[]) else printf ("Writing to file: %s\n", filename); - fwrite(eeprom_buf, 128, 1, fp); + if (eeprom_buf == NULL) + eeprom_buf = malloc(my_eeprom_size); + ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size); + + fwrite(eeprom_buf, my_eeprom_size, 1, fp); fclose(fp); } cleanup: + if (eeprom_buf) + free(eeprom_buf); if (_read > 0 || _erase > 0 || _flash > 0) { printf("FTDI close: %d\n", ftdi_usb_close(ftdi)); } ftdi_deinit (ftdi); + ftdi_free (ftdi); cfg_free(cfg);