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-2013 by Intra2net AG and the libftdi developers
email : opensource@intra2net.com
***************************************************************************/
/*
TODO:
- - Remove 128 bytes limit
- Merge Uwe's eeprom tool. Current features:
- Init eeprom defaults based upon eeprom type
- Read -> Already there
#include <string.h>
#include <confuse.h>
+#include <libusb.h>
#include <ftdi.h>
#include <ftdi_eeprom_version.h>
static int str_to_cbus(char *str, int max_allowed)
{
- #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"};
+#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"
+ };
int i;
max_allowed += 1;
if (max_allowed > MAX_OPTION) max_allowed = MAX_OPTION;
- for (i=0; i<max_allowed; i++) {
- if (!(strcmp(options[i], str))) {
+ for (i=0; i<max_allowed; i++)
+ {
+ if (!(strcmp(options[i], str)))
+ {
return i;
}
}
CFG_STR("manufacturer", "Acme Inc.", 0),
CFG_STR("product", "USB Serial Converter", 0),
CFG_STR("serial", "08-15", 0),
+ CFG_INT("eeprom_type", 0x00, 0),
CFG_STR("filename", "", 0),
CFG_BOOL("flash_raw", cfg_false, 0),
CFG_BOOL("high_current", cfg_false, 0),
*/
int _read = 0, _erase = 0, _flash = 0;
+ const int max_eeprom_size = 256;
int my_eeprom_size = 0;
unsigned char *eeprom_buf = NULL;
char *filename;
}
}
}
- 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);
- // 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_set_value(ftdi, USE_SERIAL, cfg_getbool(cfg, "use_serial"));
eeprom_set_value(ftdi, USE_USB_VERSION, cfg_getbool(cfg, "change_usb_version"));
eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version"));
-
+ 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));
}
size_check = ftdi_eeprom_build(ftdi);
+ eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
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 if (size_check < 0) {
+ }
+ else if (size_check < 0)
+ {
printf ("ftdi_eeprom_build(): error: %d\n", size_check);
}
else
{
if (filename != NULL && strlen(filename) > 0)
{
- eeprom_buf = malloc(my_eeprom_size);
+ eeprom_buf = malloc(max_eeprom_size);
FILE *fp = fopen(filename, "rb");
- fread(eeprom_buf, 1, my_eeprom_size, fp);
+ if (fp == NULL)
+ {
+ printf ("Can't open eeprom file %s.\n", filename);
+ exit (-1);
+ }
+ my_eeprom_size = fread(eeprom_buf, 1, max_eeprom_size, fp);
fclose(fp);
+ if (my_eeprom_size < 128)
+ {
+ printf ("Can't read eeprom file %s.\n", filename);
+ exit (-1);
+ }
ftdi_set_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
}
}
printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi));
+ libusb_reset_device(ftdi->usb_dev);
}
// Write to file?