X-Git-Url: http://developer.intra2net.com/git/?p=libftdi;a=blobdiff_plain;f=ftdi_eeprom%2Fmain.c;h=b27922338889f4a53793d4b203f7b5238780e041;hp=5127eeba74b1ec6ffc8bbb79426a682830a00c54;hb=f6ac3c2570f8e9c4d63ad3cb287404c7b0b91223;hpb=d5696b2ccaeec9fffcb2660e44817e31378f860a diff --git a/ftdi_eeprom/main.c b/ftdi_eeprom/main.c index 5127eeb..b279223 100644 --- a/ftdi_eeprom/main.c +++ b/ftdi_eeprom/main.c @@ -2,7 +2,7 @@ main.c - description ------------------- begin : Mon Apr 7 12:05:22 CEST 2003 - copyright : (C) 2003-2011 by Intra2net AG and the libftdi developers + copyright : (C) 2003-2014 by Intra2net AG and the libftdi developers email : opensource@intra2net.com ***************************************************************************/ @@ -37,26 +37,76 @@ #include #include +#include #include #include -static int str_to_cbus(char *str, int max_allowed) +static int parse_cbus(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result) { - #define MAX_OPTION 14 - const char* options[MAX_OPTION] = { - "TXDEN", "PWREN", "RXLED", "TXLED", "TXRXLED", "SLEEP", - "CLK48", "CLK24", "CLK12", "CLK6", - "IO_MODE", "BITBANG_WR", "BITBANG_RD", "SPECIAL"}; + static const char* options[] = + { + "TXDEN", "PWREN", "RXLED", "TXLED", "TXRXLED", "SLEEP", "CLK48", + "CLK24", "CLK12", "CLK6", "IOMODE", "BB_WR", "BB_RD" + }; + int i; - max_allowed += 1; - if (max_allowed > MAX_OPTION) max_allowed = MAX_OPTION; - for (i=0; i\n", program); + fprintf(stderr, "Valid Options:\n"); + fprintf(stderr, "--device Specify device to open by description string. One of:\n"); + fprintf(stderr, " d:\n"); + fprintf(stderr, " i::\n"); + fprintf(stderr, " i:::\n"); + fprintf(stderr, " s:::\n"); + fprintf(stderr, "--read-eeprom Read eeprom and write to -filename- from config-file\n"); + fprintf(stderr, "--erase-eeprom Erase eeprom\n"); + fprintf(stderr, "--flash-eeprom Flash eeprom\n"); +} + int main(int argc, char *argv[]) { /* @@ -121,11 +185,25 @@ int main(int argc, char *argv[]) CFG_STR("filename", "", 0), CFG_BOOL("flash_raw", cfg_false, 0), CFG_BOOL("high_current", cfg_false, 0), - CFG_STR_LIST("cbus0", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), - CFG_STR_LIST("cbus1", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), - CFG_STR_LIST("cbus2", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), - CFG_STR_LIST("cbus3", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), - CFG_STR_LIST("cbus4", "{TXDEN,PWRON,RXLED,TXLED,TX_RX_LED,SLEEP,CLK48,CLK24,CLK12,CLK6}", 0), + CFG_INT_CB("cbus0", -1, 0, parse_cbus), + CFG_INT_CB("cbus1", -1, 0, parse_cbus), + CFG_INT_CB("cbus2", -1, 0, parse_cbus), + CFG_INT_CB("cbus3", -1, 0, parse_cbus), + CFG_INT_CB("cbus4", -1, 0, parse_cbus), + CFG_INT_CB("cbush0", -1, 0, parse_cbush), + CFG_INT_CB("cbush1", -1, 0, parse_cbush), + CFG_INT_CB("cbush2", -1, 0, parse_cbush), + CFG_INT_CB("cbush3", -1, 0, parse_cbush), + CFG_INT_CB("cbush4", -1, 0, parse_cbush), + CFG_INT_CB("cbush5", -1, 0, parse_cbush), + CFG_INT_CB("cbush6", -1, 0, parse_cbush), + CFG_INT_CB("cbush7", -1, 0, parse_cbush), + CFG_INT_CB("cbush8", -1, 0, parse_cbush), + CFG_INT_CB("cbush9", -1, 0, parse_cbush), + CFG_INT_CB("cbusx0", -1, 0, parse_cbusx), + CFG_INT_CB("cbusx1", -1, 0, parse_cbusx), + CFG_INT_CB("cbusx2", -1, 0, parse_cbusx), + CFG_INT_CB("cbusx3", -1, 0, parse_cbusx), CFG_BOOL("invert_txd", cfg_false, 0), CFG_BOOL("invert_rxd", cfg_false, 0), CFG_BOOL("invert_rts", cfg_false, 0), @@ -141,14 +219,20 @@ int main(int argc, char *argv[]) /* normal variables */ - int _read = 0, _erase = 0, _flash = 0; + enum { + COMMAND_READ = 1, + COMMAND_ERASE, + COMMAND_FLASH + } command = 0; + const char *cfg_filename = NULL; + const char *device_description = NULL; const int max_eeprom_size = 256; int my_eeprom_size = 0; unsigned char *eeprom_buf = NULL; char *filename; int size_check; - int i, argc_filename; + int i; FILE *fp; struct ftdi_context *ftdi = NULL; @@ -156,37 +240,46 @@ int main(int argc, char *argv[]) printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING); printf ("(c) Intra2net AG and the libftdi developers \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 == 3) - { - if (strcmp(argv[1], "--read-eeprom") == 0) - _read = 1; - else if (strcmp(argv[1], "--erase-eeprom") == 0) - _erase = 1; - else if (strcmp(argv[1], "--flash-eeprom") == 0) - _flash = 1; + for (i = 1; i < argc; i++) { + if (*argv[i] != '-') + { + cfg_filename = argv[i]; + } + else if (!strcmp(argv[i], "--device")) + { + if (i+1 >= argc) + { + usage(argv[0]); + exit(-1); + } + device_description = argv[++i]; + } + else if (!strcmp(argv[i], "--read-eeprom")) + { + command = COMMAND_READ; + } + else if (!strcmp(argv[i], "--erase-eeprom")) + { + command = COMMAND_ERASE; + } + else if (!strcmp(argv[i], "--flash-eeprom")) + { + command = COMMAND_FLASH; + } else { - printf ("Can't open configuration file\n"); - exit (-1); + usage(argv[0]); + exit(-1); } - argc_filename = 2; } - else + + if (!cfg_filename) { - argc_filename = 1; + usage(argv[0]); + exit(-1); } - if ((fp = fopen(argv[argc_filename], "r")) == NULL) + if ((fp = fopen(cfg_filename, "r")) == NULL) { printf ("Can't open configuration file\n"); exit (-1); @@ -194,7 +287,7 @@ int main(int argc, char *argv[]) fclose (fp); cfg = cfg_init(opts, 0); - cfg_parse(cfg, argv[argc_filename]); + cfg_parse(cfg, cfg_filename); filename = cfg_getstr(cfg, "filename"); if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0) @@ -207,7 +300,19 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if (_read > 0 || _erase > 0 || _flash > 0) + if (device_description != NULL) + { + i = ftdi_usb_open_string(ftdi, device_description); + + if (i != 0) + { + printf("Unable to find FTDI device with description: %s\n", + device_description); + printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi)); + exit (-1); + } + } + else if (command > 0) { int vendor_id = cfg_getint(cfg, "vendor_id"); int product_id = cfg_getint(cfg, "product_id"); @@ -229,15 +334,15 @@ int main(int argc, char *argv[]) } } } - ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"), - cfg_getstr(cfg, "product"), + 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); printf("EEPROM size: %d\n", my_eeprom_size); - if (_read > 0) + if (command == COMMAND_READ) { ftdi_eeprom_decode(ftdi, 0 /* debug: 1 */); @@ -281,11 +386,55 @@ int main(int argc, char *argv[]) eeprom_set_value(ftdi, CHIP_TYPE, cfg_getint(cfg, "eeprom_type")); 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)); - eeprom_set_value(ftdi, CBUS_FUNCTION_2, str_to_cbus(cfg_getstr(cfg, "cbus2"), 13)); - eeprom_set_value(ftdi, CBUS_FUNCTION_3, str_to_cbus(cfg_getstr(cfg, "cbus3"), 13)); - eeprom_set_value(ftdi, CBUS_FUNCTION_4, str_to_cbus(cfg_getstr(cfg, "cbus4"), 9)); + + if (ftdi->type == TYPE_R) + { + if (cfg_getint(cfg, "cbus0") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbus0")); + if (cfg_getint(cfg, "cbus1") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbus1")); + if (cfg_getint(cfg, "cbus2") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbus2")); + if (cfg_getint(cfg, "cbus3") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbus3")); + if (cfg_getint(cfg, "cbus4") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_4, cfg_getint(cfg, "cbus4")); + } + else if (ftdi->type == TYPE_232H) + { + if (cfg_getint(cfg, "cbush0") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbush0")); + if (cfg_getint(cfg, "cbush1") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbush1")); + if (cfg_getint(cfg, "cbush2") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbush2")); + if (cfg_getint(cfg, "cbush3") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbush3")); + if (cfg_getint(cfg, "cbush4") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_4, cfg_getint(cfg, "cbush4")); + if (cfg_getint(cfg, "cbush5") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_5, cfg_getint(cfg, "cbush5")); + if (cfg_getint(cfg, "cbush6") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_6, cfg_getint(cfg, "cbush6")); + if (cfg_getint(cfg, "cbush7") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_7, cfg_getint(cfg, "cbush7")); + if (cfg_getint(cfg, "cbush8") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_8, cfg_getint(cfg, "cbush8")); + if (cfg_getint(cfg, "cbush9") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_9, cfg_getint(cfg, "cbush9")); + } + else if (ftdi->type == TYPE_230X) + { + if (cfg_getint(cfg, "cbusx0") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbusx0")); + if (cfg_getint(cfg, "cbusx1") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbusx1")); + if (cfg_getint(cfg, "cbusx2") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbusx2")); + if (cfg_getint(cfg, "cbusx3") != -1) + eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbusx3")); + } + int invert = 0; if (cfg_getbool(cfg, "invert_rxd")) invert |= INVERT_RXD; if (cfg_getbool(cfg, "invert_txd")) invert |= INVERT_TXD; @@ -306,7 +455,7 @@ int main(int argc, char *argv[]) eeprom_set_value(ftdi, CHANNEL_C_RS485, 0); eeprom_set_value(ftdi, CHANNEL_D_RS485, 0); - if (_erase > 0) + if (command == COMMAND_ERASE) { printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(ftdi)); } @@ -316,18 +465,20 @@ int main(int argc, char *argv[]) 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); + printf ("Sorry, the eeprom can only contain 128 bytes.\n"); goto cleanup; - } else if (size_check < 0) { + } + else if (size_check < 0) + { printf ("ftdi_eeprom_build(): error: %d\n", size_check); + goto cleanup; } else { printf ("Used eeprom space: %d bytes\n", my_eeprom_size-size_check); } - if (_flash > 0) + if (command == COMMAND_FLASH) { if (cfg_getbool(cfg, "flash_raw")) { @@ -352,6 +503,7 @@ int main(int argc, char *argv[]) } } printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi)); + libusb_reset_device(ftdi->usb_dev); } // Write to file? @@ -377,7 +529,7 @@ int main(int argc, char *argv[]) cleanup: if (eeprom_buf) free(eeprom_buf); - if (_read > 0 || _erase > 0 || _flash > 0) + if (command > 0) { printf("FTDI close: %d\n", ftdi_usb_close(ftdi)); }