1 /***************************************************************************
4 begin : Mon Apr 7 12:05:22 CEST 2003
5 copyright : (C) 2003-2014 by Intra2net AG and the libftdi developers
6 email : opensource@intra2net.com
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License version 2 as *
13 * published by the Free Software Foundation. *
15 ***************************************************************************/
19 - Merge Uwe's eeprom tool. Current features:
20 - Init eeprom defaults based upon eeprom type
21 - Read -> Already there
22 - Write -> Already there
23 - Erase -> Already there
25 - Ability to find device by PID/VID, product name or serial
28 - Out-of-the-box compatibility with FTDI's eeprom tool configuration files
42 #include <ftdi_eeprom_version.h>
44 static int parse_cbus(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
46 static const char* options[] =
48 "TXDEN", "PWREN", "RXLED", "TXLED", "TXRXLED", "SLEEP", "CLK48",
49 "CLK24", "CLK12", "CLK6", "IOMODE", "BB_WR", "BB_RD"
53 for (i=0; i<sizeof(options)/sizeof(*options); i++)
55 if (!(strcmp(options[i], value)))
62 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
66 static int parse_cbush(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
68 static const char* options[] =
70 "TRISTATE", "TXLED", "RXLED", "TXRXLED", "PWREN", "SLEEP",
71 "DRIVE_0", "DRIVE1", "IOMODE", "TXDEN", "CLK30", "CLK15", "CLK7_5"
75 for (i=0; i<sizeof(options)/sizeof(*options); i++)
77 if (!(strcmp(options[i], value)))
84 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
88 static int parse_cbusx(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
90 static const char* options[] =
92 "TRISTATE", "TXLED", "RXLED", "TXRXLED", "PWREN", "SLEEP",
93 "DRIVE_0", "DRIVE1", "IOMODE", "TXDEN", "CLK24", "CLK12",
94 "CLK6", "BAT_DETECT", "BAT_DETECT_NEG", "I2C_TXE", "I2C_RXF", "VBUS_SENSE",
95 "BB_WR", "BB_RD", "TIME_STAMP", "AWAKE"
99 for (i=0; i<sizeof(options)/sizeof(*options); i++)
101 if (!(strcmp(options[i], value)))
108 cfg_error(cfg, "Invalid %s option '%s'", cfg_opt_name(opt), value);
113 * @brief Set eeprom value
115 * \param ftdi pointer to ftdi_context
116 * \param value_name Enum of the value to set
117 * \param value Value to set
119 * Function will abort the program on error
121 static void eeprom_set_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
123 if (ftdi_set_eeprom_value(ftdi, value_name, value) < 0)
125 printf("Unable to set eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi));
131 * @brief Get eeprom value
133 * \param ftdi pointer to ftdi_context
134 * \param value_name Enum of the value to get
135 * \param value Value to get
137 * Function will abort the program on error
139 static void eeprom_get_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int *value)
141 if (ftdi_get_eeprom_value(ftdi, value_name, value) < 0)
143 printf("Unable to get eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi));
148 int main(int argc, char *argv[])
151 configuration options
155 CFG_INT("vendor_id", 0, 0),
156 CFG_INT("product_id", 0, 0),
157 CFG_BOOL("self_powered", cfg_true, 0),
158 CFG_BOOL("remote_wakeup", cfg_true, 0),
159 CFG_BOOL("in_is_isochronous", cfg_false, 0),
160 CFG_BOOL("out_is_isochronous", cfg_false, 0),
161 CFG_BOOL("suspend_pull_downs", cfg_false, 0),
162 CFG_BOOL("use_serial", cfg_false, 0),
163 CFG_BOOL("change_usb_version", cfg_false, 0),
164 CFG_INT("usb_version", 0, 0),
165 CFG_INT("default_pid", 0x6001, 0),
166 CFG_INT("max_power", 0, 0),
167 CFG_STR("manufacturer", "Acme Inc.", 0),
168 CFG_STR("product", "USB Serial Converter", 0),
169 CFG_STR("serial", "08-15", 0),
170 CFG_INT("eeprom_type", 0x00, 0),
171 CFG_STR("filename", "", 0),
172 CFG_BOOL("flash_raw", cfg_false, 0),
173 CFG_BOOL("high_current", cfg_false, 0),
174 CFG_INT_CB("cbus0", -1, 0, parse_cbus),
175 CFG_INT_CB("cbus1", -1, 0, parse_cbus),
176 CFG_INT_CB("cbus2", -1, 0, parse_cbus),
177 CFG_INT_CB("cbus3", -1, 0, parse_cbus),
178 CFG_INT_CB("cbus4", -1, 0, parse_cbus),
179 CFG_INT_CB("cbush0", -1, 0, parse_cbush),
180 CFG_INT_CB("cbush1", -1, 0, parse_cbush),
181 CFG_INT_CB("cbush2", -1, 0, parse_cbush),
182 CFG_INT_CB("cbush3", -1, 0, parse_cbush),
183 CFG_INT_CB("cbush4", -1, 0, parse_cbush),
184 CFG_INT_CB("cbush5", -1, 0, parse_cbush),
185 CFG_INT_CB("cbush6", -1, 0, parse_cbush),
186 CFG_INT_CB("cbush7", -1, 0, parse_cbush),
187 CFG_INT_CB("cbush8", -1, 0, parse_cbush),
188 CFG_INT_CB("cbush9", -1, 0, parse_cbush),
189 CFG_INT_CB("cbusx0", -1, 0, parse_cbusx),
190 CFG_INT_CB("cbusx1", -1, 0, parse_cbusx),
191 CFG_INT_CB("cbusx2", -1, 0, parse_cbusx),
192 CFG_INT_CB("cbusx3", -1, 0, parse_cbusx),
193 CFG_BOOL("invert_txd", cfg_false, 0),
194 CFG_BOOL("invert_rxd", cfg_false, 0),
195 CFG_BOOL("invert_rts", cfg_false, 0),
196 CFG_BOOL("invert_cts", cfg_false, 0),
197 CFG_BOOL("invert_dtr", cfg_false, 0),
198 CFG_BOOL("invert_dsr", cfg_false, 0),
199 CFG_BOOL("invert_dcd", cfg_false, 0),
200 CFG_BOOL("invert_ri", cfg_false, 0),
208 int _read = 0, _erase = 0, _flash = 0;
210 const int max_eeprom_size = 256;
211 int my_eeprom_size = 0;
212 unsigned char *eeprom_buf = NULL;
215 int i, argc_filename;
218 struct ftdi_context *ftdi = NULL;
220 printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING);
221 printf ("(c) Intra2net AG and the libftdi developers <opensource@intra2net.com>\n");
223 if (argc != 2 && argc != 3)
225 printf("Syntax: %s [commands] config-file\n", argv[0]);
226 printf("Valid commands:\n");
227 printf("--read-eeprom Read eeprom and write to -filename- from config-file\n");
228 printf("--erase-eeprom Erase eeprom\n");
229 printf("--flash-eeprom Flash eeprom\n");
235 if (strcmp(argv[1], "--read-eeprom") == 0)
237 else if (strcmp(argv[1], "--erase-eeprom") == 0)
239 else if (strcmp(argv[1], "--flash-eeprom") == 0)
243 printf ("Can't open configuration file\n");
253 if ((fp = fopen(argv[argc_filename], "r")) == NULL)
255 printf ("Can't open configuration file\n");
260 cfg = cfg_init(opts, 0);
261 cfg_parse(cfg, argv[argc_filename]);
262 filename = cfg_getstr(cfg, "filename");
264 if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0)
265 printf("Hint: Self powered devices should have a max_power setting of 0.\n");
267 if ((ftdi = ftdi_new()) == 0)
269 fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
270 ftdi_get_error_string(ftdi));
274 if (_read > 0 || _erase > 0 || _flash > 0)
276 int vendor_id = cfg_getint(cfg, "vendor_id");
277 int product_id = cfg_getint(cfg, "product_id");
279 i = ftdi_usb_open(ftdi, vendor_id, product_id);
283 int default_pid = cfg_getint(cfg, "default_pid");
284 printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", vendor_id, product_id);
285 printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi));
286 printf("Retrying with default FTDI pid=%#04x.\n", default_pid);
288 i = ftdi_usb_open(ftdi, 0x0403, default_pid);
291 printf("Error: %s\n", ftdi->error_str);
296 ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"),
297 cfg_getstr(cfg, "product"),
298 cfg_getstr(cfg, "serial"));
300 printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi));
301 eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
302 printf("EEPROM size: %d\n", my_eeprom_size);
306 ftdi_eeprom_decode(ftdi, 0 /* debug: 1 */);
308 eeprom_buf = malloc(my_eeprom_size);
309 ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
311 if (eeprom_buf == NULL)
313 fprintf(stderr, "Malloc failed, aborting\n");
316 if (filename != NULL && strlen(filename) > 0)
319 FILE *fp = fopen (filename, "wb");
320 fwrite (eeprom_buf, 1, my_eeprom_size, fp);
325 printf("Warning: Not writing eeprom, you must supply a valid filename\n");
331 eeprom_set_value(ftdi, VENDOR_ID, cfg_getint(cfg, "vendor_id"));
332 eeprom_set_value(ftdi, PRODUCT_ID, cfg_getint(cfg, "product_id"));
334 eeprom_set_value(ftdi, SELF_POWERED, cfg_getbool(cfg, "self_powered"));
335 eeprom_set_value(ftdi, REMOTE_WAKEUP, cfg_getbool(cfg, "remote_wakeup"));
336 eeprom_set_value(ftdi, MAX_POWER, cfg_getint(cfg, "max_power"));
338 eeprom_set_value(ftdi, IN_IS_ISOCHRONOUS, cfg_getbool(cfg, "in_is_isochronous"));
339 eeprom_set_value(ftdi, OUT_IS_ISOCHRONOUS, cfg_getbool(cfg, "out_is_isochronous"));
340 eeprom_set_value(ftdi, SUSPEND_PULL_DOWNS, cfg_getbool(cfg, "suspend_pull_downs"));
342 eeprom_set_value(ftdi, USE_SERIAL, cfg_getbool(cfg, "use_serial"));
343 eeprom_set_value(ftdi, USE_USB_VERSION, cfg_getbool(cfg, "change_usb_version"));
344 eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version"));
345 eeprom_set_value(ftdi, CHIP_TYPE, cfg_getint(cfg, "eeprom_type"));
347 eeprom_set_value(ftdi, HIGH_CURRENT, cfg_getbool(cfg, "high_current"));
349 if (ftdi->type == TYPE_R)
351 if (cfg_getint(cfg, "cbus0") != -1)
352 eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbus0"));
353 if (cfg_getint(cfg, "cbus1") != -1)
354 eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbus1"));
355 if (cfg_getint(cfg, "cbus2") != -1)
356 eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbus2"));
357 if (cfg_getint(cfg, "cbus3") != -1)
358 eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbus3"));
359 if (cfg_getint(cfg, "cbus4") != -1)
360 eeprom_set_value(ftdi, CBUS_FUNCTION_4, cfg_getint(cfg, "cbus4"));
362 else if (ftdi->type == TYPE_232H)
364 if (cfg_getint(cfg, "cbush0") != -1)
365 eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbush0"));
366 if (cfg_getint(cfg, "cbush1") != -1)
367 eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbush1"));
368 if (cfg_getint(cfg, "cbush2") != -1)
369 eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbush2"));
370 if (cfg_getint(cfg, "cbush3") != -1)
371 eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbush3"));
372 if (cfg_getint(cfg, "cbush4") != -1)
373 eeprom_set_value(ftdi, CBUS_FUNCTION_4, cfg_getint(cfg, "cbush4"));
374 if (cfg_getint(cfg, "cbush5") != -1)
375 eeprom_set_value(ftdi, CBUS_FUNCTION_5, cfg_getint(cfg, "cbush5"));
376 if (cfg_getint(cfg, "cbush6") != -1)
377 eeprom_set_value(ftdi, CBUS_FUNCTION_6, cfg_getint(cfg, "cbush6"));
378 if (cfg_getint(cfg, "cbush7") != -1)
379 eeprom_set_value(ftdi, CBUS_FUNCTION_7, cfg_getint(cfg, "cbush7"));
380 if (cfg_getint(cfg, "cbush8") != -1)
381 eeprom_set_value(ftdi, CBUS_FUNCTION_8, cfg_getint(cfg, "cbush8"));
382 if (cfg_getint(cfg, "cbush9") != -1)
383 eeprom_set_value(ftdi, CBUS_FUNCTION_9, cfg_getint(cfg, "cbush9"));
385 else if (ftdi->type == TYPE_230X)
387 if (cfg_getint(cfg, "cbusx0") != -1)
388 eeprom_set_value(ftdi, CBUS_FUNCTION_0, cfg_getint(cfg, "cbusx0"));
389 if (cfg_getint(cfg, "cbusx1") != -1)
390 eeprom_set_value(ftdi, CBUS_FUNCTION_1, cfg_getint(cfg, "cbusx1"));
391 if (cfg_getint(cfg, "cbusx2") != -1)
392 eeprom_set_value(ftdi, CBUS_FUNCTION_2, cfg_getint(cfg, "cbusx2"));
393 if (cfg_getint(cfg, "cbusx3") != -1)
394 eeprom_set_value(ftdi, CBUS_FUNCTION_3, cfg_getint(cfg, "cbusx3"));
398 if (cfg_getbool(cfg, "invert_rxd")) invert |= INVERT_RXD;
399 if (cfg_getbool(cfg, "invert_txd")) invert |= INVERT_TXD;
400 if (cfg_getbool(cfg, "invert_rts")) invert |= INVERT_RTS;
401 if (cfg_getbool(cfg, "invert_cts")) invert |= INVERT_CTS;
402 if (cfg_getbool(cfg, "invert_dtr")) invert |= INVERT_DTR;
403 if (cfg_getbool(cfg, "invert_dsr")) invert |= INVERT_DSR;
404 if (cfg_getbool(cfg, "invert_dcd")) invert |= INVERT_DCD;
405 if (cfg_getbool(cfg, "invert_ri")) invert |= INVERT_RI;
406 eeprom_set_value(ftdi, INVERT, invert);
408 eeprom_set_value(ftdi, CHANNEL_A_DRIVER, DRIVER_VCP);
409 eeprom_set_value(ftdi, CHANNEL_B_DRIVER, DRIVER_VCP);
410 eeprom_set_value(ftdi, CHANNEL_C_DRIVER, DRIVER_VCP);
411 eeprom_set_value(ftdi, CHANNEL_D_DRIVER, DRIVER_VCP);
412 eeprom_set_value(ftdi, CHANNEL_A_RS485, 0);
413 eeprom_set_value(ftdi, CHANNEL_B_RS485, 0);
414 eeprom_set_value(ftdi, CHANNEL_C_RS485, 0);
415 eeprom_set_value(ftdi, CHANNEL_D_RS485, 0);
419 printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(ftdi));
422 size_check = ftdi_eeprom_build(ftdi);
423 eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size);
425 if (size_check == -1)
427 printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for your strings).\n");
428 printf ("You need to short your string by: %d bytes\n", size_check);
431 else if (size_check < 0)
433 printf ("ftdi_eeprom_build(): error: %d\n", size_check);
437 printf ("Used eeprom space: %d bytes\n", my_eeprom_size-size_check);
442 if (cfg_getbool(cfg, "flash_raw"))
444 if (filename != NULL && strlen(filename) > 0)
446 eeprom_buf = malloc(max_eeprom_size);
447 FILE *fp = fopen(filename, "rb");
450 printf ("Can't open eeprom file %s.\n", filename);
453 my_eeprom_size = fread(eeprom_buf, 1, max_eeprom_size, fp);
455 if (my_eeprom_size < 128)
457 printf ("Can't read eeprom file %s.\n", filename);
461 ftdi_set_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
464 printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi));
465 libusb_reset_device(ftdi->usb_dev);
469 if (filename != NULL && strlen(filename) > 0 && !cfg_getbool(cfg, "flash_raw"))
471 fp = fopen(filename, "w");
474 printf ("Can't write eeprom file.\n");
478 printf ("Writing to file: %s\n", filename);
480 if (eeprom_buf == NULL)
481 eeprom_buf = malloc(my_eeprom_size);
482 ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size);
484 fwrite(eeprom_buf, my_eeprom_size, 1, fp);
491 if (_read > 0 || _erase > 0 || _flash > 0)
493 printf("FTDI close: %d\n", ftdi_usb_close(ftdi));