Commit | Line | Data |
---|---|---|
e47d7975 TJ |
1 | /*************************************************************************** |
2 | main.c - description | |
3 | ------------------- | |
4 | begin : Mon Apr 7 12:05:22 CEST 2003 | |
8a987aa2 | 5 | copyright : (C) 2003-2011 by Intra2net AG and the libftdi developers |
e47d7975 TJ |
6 | email : opensource@intra2net.com |
7 | ***************************************************************************/ | |
8 | ||
9 | /*************************************************************************** | |
10 | * * | |
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. * | |
14 | * * | |
15 | ***************************************************************************/ | |
16 | ||
8a987aa2 TJ |
17 | /* |
18 | TODO: | |
8a987aa2 TJ |
19 | - Remove 128 bytes limit |
20 | - Merge Uwe's eeprom tool. Current features: | |
21 | - Init eeprom defaults based upon eeprom type | |
22 | - Read -> Already there | |
23 | - Write -> Already there | |
24 | - Erase -> Already there | |
25 | - Decode on stdout | |
26 | - Ability to find device by PID/VID, product name or serial | |
27 | ||
28 | TODO nice-to-have: | |
29 | - Out-of-the-box compatibility with FTDI's eeprom tool configuration files | |
30 | */ | |
31 | ||
e47d7975 TJ |
32 | #ifdef HAVE_CONFIG_H |
33 | #include <config.h> | |
34 | #endif | |
35 | ||
36 | #include <stdlib.h> | |
37 | #include <stdio.h> | |
38 | #include <string.h> | |
39 | ||
40 | #include <confuse.h> | |
41 | #include <ftdi.h> | |
ade814a5 | 42 | #include <ftdi_eeprom_version.h> |
e47d7975 | 43 | |
d327f924 | 44 | static int str_to_cbus(char *str, int max_allowed) |
e47d7975 TJ |
45 | { |
46 | #define MAX_OPTION 14 | |
47 | const char* options[MAX_OPTION] = { | |
48 | "TXDEN", "PWREN", "RXLED", "TXLED", "TXRXLED", "SLEEP", | |
49 | "CLK48", "CLK24", "CLK12", "CLK6", | |
50 | "IO_MODE", "BITBANG_WR", "BITBANG_RD", "SPECIAL"}; | |
51 | int i; | |
52 | max_allowed += 1; | |
53 | if (max_allowed > MAX_OPTION) max_allowed = MAX_OPTION; | |
54 | for (i=0; i<max_allowed; i++) { | |
55 | if (!(strcmp(options[i], str))) { | |
56 | return i; | |
57 | } | |
58 | } | |
59 | printf("WARNING: Invalid cbus option '%s'\n", str); | |
60 | return 0; | |
61 | } | |
62 | ||
d327f924 TJ |
63 | /** |
64 | * @brief Set eeprom value | |
65 | * | |
66 | * \param ftdi pointer to ftdi_context | |
67 | * \param value_name Enum of the value to set | |
68 | * \param value Value to set | |
69 | * | |
70 | * Function will abort the program on error | |
71 | **/ | |
72 | static void eeprom_set_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value) | |
73 | { | |
74 | if (ftdi_set_eeprom_value(ftdi, value_name, value) < 0) | |
75 | { | |
76 | printf("Unable to set eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi)); | |
77 | exit (-1); | |
78 | } | |
79 | } | |
80 | ||
81 | /** | |
82 | * @brief Get eeprom value | |
83 | * | |
84 | * \param ftdi pointer to ftdi_context | |
85 | * \param value_name Enum of the value to get | |
86 | * \param value Value to get | |
87 | * | |
88 | * Function will abort the program on error | |
89 | **/ | |
90 | static void eeprom_get_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int *value) | |
91 | { | |
92 | if (ftdi_get_eeprom_value(ftdi, value_name, value) < 0) | |
93 | { | |
94 | printf("Unable to get eeprom value %d: %s. Aborting\n", value_name, ftdi_get_error_string(ftdi)); | |
95 | exit (-1); | |
96 | } | |
97 | } | |
98 | ||
e47d7975 TJ |
99 | int main(int argc, char *argv[]) |
100 | { | |
101 | /* | |
102 | configuration options | |
103 | */ | |
104 | cfg_opt_t opts[] = | |
105 | { | |
106 | CFG_INT("vendor_id", 0, 0), | |
107 | CFG_INT("product_id", 0, 0), | |
108 | CFG_BOOL("self_powered", cfg_true, 0), | |
109 | CFG_BOOL("remote_wakeup", cfg_true, 0), | |
e47d7975 TJ |
110 | CFG_BOOL("in_is_isochronous", cfg_false, 0), |
111 | CFG_BOOL("out_is_isochronous", cfg_false, 0), | |
112 | CFG_BOOL("suspend_pull_downs", cfg_false, 0), | |
113 | CFG_BOOL("use_serial", cfg_false, 0), | |
114 | CFG_BOOL("change_usb_version", cfg_false, 0), | |
115 | CFG_INT("usb_version", 0, 0), | |
89e7cfb5 | 116 | CFG_INT("default_pid", 0x6001, 0), |
e47d7975 TJ |
117 | CFG_INT("max_power", 0, 0), |
118 | CFG_STR("manufacturer", "Acme Inc.", 0), | |
119 | CFG_STR("product", "USB Serial Converter", 0), | |
120 | CFG_STR("serial", "08-15", 0), | |
121 | CFG_STR("filename", "", 0), | |
122 | CFG_BOOL("flash_raw", cfg_false, 0), | |
123 | CFG_BOOL("high_current", cfg_false, 0), | |
124 | CFG_STR_LIST("cbus0", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), | |
125 | CFG_STR_LIST("cbus1", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), | |
126 | CFG_STR_LIST("cbus2", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), | |
127 | CFG_STR_LIST("cbus3", "{TXDEN,PWREN,RXLED,TXLED,TXRXLED,SLEEP,CLK48,CLK24,CLK12,CLK6,IO_MODE,BITBANG_WR,BITBANG_RD,SPECIAL}", 0), | |
128 | CFG_STR_LIST("cbus4", "{TXDEN,PWRON,RXLED,TXLED,TX_RX_LED,SLEEP,CLK48,CLK24,CLK12,CLK6}", 0), | |
129 | CFG_BOOL("invert_txd", cfg_false, 0), | |
130 | CFG_BOOL("invert_rxd", cfg_false, 0), | |
131 | CFG_BOOL("invert_rts", cfg_false, 0), | |
132 | CFG_BOOL("invert_cts", cfg_false, 0), | |
133 | CFG_BOOL("invert_dtr", cfg_false, 0), | |
134 | CFG_BOOL("invert_dsr", cfg_false, 0), | |
135 | CFG_BOOL("invert_dcd", cfg_false, 0), | |
136 | CFG_BOOL("invert_ri", cfg_false, 0), | |
137 | CFG_END() | |
138 | }; | |
139 | cfg_t *cfg; | |
140 | ||
141 | /* | |
142 | normal variables | |
143 | */ | |
144 | int _read = 0, _erase = 0, _flash = 0; | |
200bd3ed | 145 | |
785ddbca | 146 | int my_eeprom_size = 0; |
4359228a | 147 | unsigned char *eeprom_buf = NULL; |
e47d7975 TJ |
148 | char *filename; |
149 | int size_check; | |
150 | int i, argc_filename; | |
151 | FILE *fp; | |
152 | ||
de4871c4 | 153 | struct ftdi_context *ftdi = NULL; |
e47d7975 | 154 | |
ade814a5 | 155 | printf("\nFTDI eeprom generator v%s\n", EEPROM_VERSION_STRING); |
8a987aa2 | 156 | printf ("(c) Intra2net AG and the libftdi developers <opensource@intra2net.com>\n"); |
e47d7975 TJ |
157 | |
158 | if (argc != 2 && argc != 3) | |
159 | { | |
160 | printf("Syntax: %s [commands] config-file\n", argv[0]); | |
161 | printf("Valid commands:\n"); | |
162 | printf("--read-eeprom Read eeprom and write to -filename- from config-file\n"); | |
163 | printf("--erase-eeprom Erase eeprom\n"); | |
164 | printf("--flash-eeprom Flash eeprom\n"); | |
165 | exit (-1); | |
166 | } | |
167 | ||
168 | if (argc == 3) | |
169 | { | |
170 | if (strcmp(argv[1], "--read-eeprom") == 0) | |
171 | _read = 1; | |
94c637b8 | 172 | else if (strcmp(argv[1], "--erase-eeprom") == 0) |
e47d7975 | 173 | _erase = 1; |
94c637b8 | 174 | else if (strcmp(argv[1], "--flash-eeprom") == 0) |
e47d7975 | 175 | _flash = 1; |
94c637b8 UB |
176 | else |
177 | { | |
178 | printf ("Can't open configuration file\n"); | |
179 | exit (-1); | |
180 | } | |
e47d7975 TJ |
181 | argc_filename = 2; |
182 | } | |
183 | else | |
184 | { | |
185 | argc_filename = 1; | |
186 | } | |
187 | ||
188 | if ((fp = fopen(argv[argc_filename], "r")) == NULL) | |
189 | { | |
190 | printf ("Can't open configuration file\n"); | |
191 | exit (-1); | |
192 | } | |
193 | fclose (fp); | |
194 | ||
195 | cfg = cfg_init(opts, 0); | |
196 | cfg_parse(cfg, argv[argc_filename]); | |
197 | filename = cfg_getstr(cfg, "filename"); | |
198 | ||
199 | if (cfg_getbool(cfg, "self_powered") && cfg_getint(cfg, "max_power") > 0) | |
200 | printf("Hint: Self powered devices should have a max_power setting of 0.\n"); | |
201 | ||
de4871c4 TJ |
202 | if ((ftdi = ftdi_new()) == 0) |
203 | { | |
204 | fprintf(stderr, "Failed to allocate ftdi structure :%s \n", | |
205 | ftdi_get_error_string(ftdi)); | |
206 | return EXIT_FAILURE; | |
207 | } | |
208 | ||
e47d7975 TJ |
209 | if (_read > 0 || _erase > 0 || _flash > 0) |
210 | { | |
7bdae7cd UB |
211 | int vendor_id = cfg_getint(cfg, "vendor_id"); |
212 | int product_id = cfg_getint(cfg, "product_id"); | |
d327f924 TJ |
213 | |
214 | i = ftdi_usb_open(ftdi, vendor_id, product_id); | |
e47d7975 | 215 | |
e8d32978 | 216 | if (i != 0) |
e47d7975 | 217 | { |
89e7cfb5 | 218 | int default_pid = cfg_getint(cfg, "default_pid"); |
d327f924 | 219 | printf("Unable to find FTDI devices under given vendor/product id: 0x%X/0x%X\n", vendor_id, product_id); |
de4871c4 | 220 | printf("Error code: %d (%s)\n", i, ftdi_get_error_string(ftdi)); |
89e7cfb5 | 221 | printf("Retrying with default FTDI pid=%#04x.\n", default_pid); |
e47d7975 | 222 | |
89e7cfb5 | 223 | i = ftdi_usb_open(ftdi, 0x0403, default_pid); |
e47d7975 TJ |
224 | if (i != 0) |
225 | { | |
de4871c4 | 226 | printf("Error: %s\n", ftdi->error_str); |
e47d7975 TJ |
227 | exit (-1); |
228 | } | |
229 | } | |
230 | } | |
e8d32978 UB |
231 | ftdi_eeprom_initdefaults (ftdi, cfg_getstr(cfg, "manufacturer"), |
232 | cfg_getstr(cfg, "product"), | |
233 | cfg_getstr(cfg, "serial")); | |
234 | ||
235 | printf("FTDI read eeprom: %d\n", ftdi_read_eeprom(ftdi)); | |
236 | eeprom_get_value(ftdi, CHIP_SIZE, &my_eeprom_size); | |
237 | // TODO: Do we know the eeprom size already? | |
238 | printf("EEPROM size: %d\n", my_eeprom_size); | |
239 | ||
e47d7975 TJ |
240 | if (_read > 0) |
241 | { | |
61620879 | 242 | ftdi_eeprom_decode(ftdi, 0 /* debug: 1 */); |
e47d7975 | 243 | |
4359228a UB |
244 | eeprom_buf = malloc(my_eeprom_size); |
245 | ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size); | |
246 | ||
247 | if (eeprom_buf == NULL) | |
248 | { | |
249 | fprintf(stderr, "Malloc failed, aborting\n"); | |
250 | goto cleanup; | |
251 | } | |
e47d7975 TJ |
252 | if (filename != NULL && strlen(filename) > 0) |
253 | { | |
200bd3ed | 254 | |
e47d7975 | 255 | FILE *fp = fopen (filename, "wb"); |
200bd3ed | 256 | fwrite (eeprom_buf, 1, my_eeprom_size, fp); |
e47d7975 TJ |
257 | fclose (fp); |
258 | } | |
259 | else | |
260 | { | |
261 | printf("Warning: Not writing eeprom, you must supply a valid filename\n"); | |
262 | } | |
263 | ||
264 | goto cleanup; | |
265 | } | |
266 | ||
7bdae7cd UB |
267 | eeprom_set_value(ftdi, VENDOR_ID, cfg_getint(cfg, "vendor_id")); |
268 | eeprom_set_value(ftdi, PRODUCT_ID, cfg_getint(cfg, "product_id")); | |
269 | ||
7bdae7cd UB |
270 | eeprom_set_value(ftdi, SELF_POWERED, cfg_getbool(cfg, "self_powered")); |
271 | eeprom_set_value(ftdi, REMOTE_WAKEUP, cfg_getbool(cfg, "remote_wakeup")); | |
272 | eeprom_set_value(ftdi, MAX_POWER, cfg_getint(cfg, "max_power")); | |
273 | ||
274 | eeprom_set_value(ftdi, IN_IS_ISOCHRONOUS, cfg_getbool(cfg, "in_is_isochronous")); | |
275 | eeprom_set_value(ftdi, OUT_IS_ISOCHRONOUS, cfg_getbool(cfg, "out_is_isochronous")); | |
276 | eeprom_set_value(ftdi, SUSPEND_PULL_DOWNS, cfg_getbool(cfg, "suspend_pull_downs")); | |
277 | ||
278 | eeprom_set_value(ftdi, USE_SERIAL, cfg_getbool(cfg, "use_serial")); | |
279 | eeprom_set_value(ftdi, USE_USB_VERSION, cfg_getbool(cfg, "change_usb_version")); | |
280 | eeprom_set_value(ftdi, USB_VERSION, cfg_getint(cfg, "usb_version")); | |
281 | ||
282 | ||
283 | eeprom_set_value(ftdi, HIGH_CURRENT, cfg_getbool(cfg, "high_current")); | |
284 | eeprom_set_value(ftdi, CBUS_FUNCTION_0, str_to_cbus(cfg_getstr(cfg, "cbus0"), 13)); | |
285 | eeprom_set_value(ftdi, CBUS_FUNCTION_1, str_to_cbus(cfg_getstr(cfg, "cbus1"), 13)); | |
286 | eeprom_set_value(ftdi, CBUS_FUNCTION_2, str_to_cbus(cfg_getstr(cfg, "cbus2"), 13)); | |
287 | eeprom_set_value(ftdi, CBUS_FUNCTION_3, str_to_cbus(cfg_getstr(cfg, "cbus3"), 13)); | |
288 | eeprom_set_value(ftdi, CBUS_FUNCTION_4, str_to_cbus(cfg_getstr(cfg, "cbus4"), 9)); | |
289 | int invert = 0; | |
290 | if (cfg_getbool(cfg, "invert_rxd")) invert |= INVERT_RXD; | |
291 | if (cfg_getbool(cfg, "invert_txd")) invert |= INVERT_TXD; | |
292 | if (cfg_getbool(cfg, "invert_rts")) invert |= INVERT_RTS; | |
293 | if (cfg_getbool(cfg, "invert_cts")) invert |= INVERT_CTS; | |
294 | if (cfg_getbool(cfg, "invert_dtr")) invert |= INVERT_DTR; | |
295 | if (cfg_getbool(cfg, "invert_dsr")) invert |= INVERT_DSR; | |
296 | if (cfg_getbool(cfg, "invert_dcd")) invert |= INVERT_DCD; | |
297 | if (cfg_getbool(cfg, "invert_ri")) invert |= INVERT_RI; | |
298 | eeprom_set_value(ftdi, INVERT, invert); | |
299 | ||
be4bae37 AL |
300 | eeprom_set_value(ftdi, CHANNEL_A_DRIVER, DRIVER_VCP); |
301 | eeprom_set_value(ftdi, CHANNEL_B_DRIVER, DRIVER_VCP); | |
302 | eeprom_set_value(ftdi, CHANNEL_C_DRIVER, DRIVER_VCP); | |
303 | eeprom_set_value(ftdi, CHANNEL_D_DRIVER, DRIVER_VCP); | |
304 | eeprom_set_value(ftdi, CHANNEL_A_RS485, 0); | |
305 | eeprom_set_value(ftdi, CHANNEL_B_RS485, 0); | |
306 | eeprom_set_value(ftdi, CHANNEL_C_RS485, 0); | |
307 | eeprom_set_value(ftdi, CHANNEL_D_RS485, 0); | |
308 | ||
e47d7975 TJ |
309 | if (_erase > 0) |
310 | { | |
de4871c4 | 311 | printf("FTDI erase eeprom: %d\n", ftdi_erase_eeprom(ftdi)); |
e47d7975 TJ |
312 | } |
313 | ||
de4871c4 | 314 | size_check = ftdi_eeprom_build(ftdi); |
e47d7975 TJ |
315 | |
316 | if (size_check == -1) | |
317 | { | |
318 | printf ("Sorry, the eeprom can only contain 128 bytes (100 bytes for your strings).\n"); | |
319 | printf ("You need to short your string by: %d bytes\n", size_check); | |
320 | goto cleanup; | |
321 | } else if (size_check < 0) { | |
322 | printf ("ftdi_eeprom_build(): error: %d\n", size_check); | |
323 | } | |
324 | else | |
325 | { | |
200bd3ed | 326 | printf ("Used eeprom space: %d bytes\n", my_eeprom_size-size_check); |
e47d7975 TJ |
327 | } |
328 | ||
329 | if (_flash > 0) | |
330 | { | |
331 | if (cfg_getbool(cfg, "flash_raw")) | |
332 | { | |
333 | if (filename != NULL && strlen(filename) > 0) | |
334 | { | |
5a7acb07 | 335 | eeprom_buf = malloc(my_eeprom_size); |
e47d7975 | 336 | FILE *fp = fopen(filename, "rb"); |
200bd3ed | 337 | fread(eeprom_buf, 1, my_eeprom_size, fp); |
e47d7975 | 338 | fclose(fp); |
200bd3ed | 339 | |
98c974c7 | 340 | ftdi_set_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size); |
e47d7975 TJ |
341 | } |
342 | } | |
de4871c4 | 343 | printf ("FTDI write eeprom: %d\n", ftdi_write_eeprom(ftdi)); |
e47d7975 TJ |
344 | } |
345 | ||
346 | // Write to file? | |
643a89ea | 347 | if (filename != NULL && strlen(filename) > 0 && !cfg_getbool(cfg, "flash_raw")) |
e47d7975 TJ |
348 | { |
349 | fp = fopen(filename, "w"); | |
350 | if (fp == NULL) | |
351 | { | |
352 | printf ("Can't write eeprom file.\n"); | |
353 | exit (-1); | |
354 | } | |
355 | else | |
356 | printf ("Writing to file: %s\n", filename); | |
357 | ||
243f5f66 AL |
358 | if (eeprom_buf == NULL) |
359 | eeprom_buf = malloc(my_eeprom_size); | |
200bd3ed TJ |
360 | ftdi_get_eeprom_buf(ftdi, eeprom_buf, my_eeprom_size); |
361 | ||
362 | fwrite(eeprom_buf, my_eeprom_size, 1, fp); | |
e47d7975 TJ |
363 | fclose(fp); |
364 | } | |
365 | ||
366 | cleanup: | |
4359228a UB |
367 | if (eeprom_buf) |
368 | free(eeprom_buf); | |
e47d7975 TJ |
369 | if (_read > 0 || _erase > 0 || _flash > 0) |
370 | { | |
de4871c4 | 371 | printf("FTDI close: %d\n", ftdi_usb_close(ftdi)); |
e47d7975 TJ |
372 | } |
373 | ||
de4871c4 | 374 | ftdi_deinit (ftdi); |
4e494b76 | 375 | ftdi_free (ftdi); |
e47d7975 TJ |
376 | |
377 | cfg_free(cfg); | |
378 | ||
379 | printf("\n"); | |
380 | return 0; | |
381 | } |