FreeCalypso > hg > fc-usbser-tools
annotate fteeprom/fteeprom-erase.c @ 107:bc3367755586 default tip
add INSTALL document
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 21 Nov 2023 22:11:09 +0000 |
parents | 5cbde3c80c24 |
children |
rev | line source |
---|---|
21
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
1 /* |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
2 * This program connects to an FTDI chip via libusb and commands that FTDI |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
3 * chip to erase its EEPROM in one fell swoop. This operation is known |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
4 * to work on FT2232C/D chips with 5V-powered EEPROMs, but is NOT appropriate |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
5 * for FT232R. This operation is also not recommended for external EEPROMs |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
6 * powered with 3.3V, as typically used with FT2232H. |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
7 * |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
8 * The present version has been converted to use our local libraries |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
9 * (libftmini and libuwrap) instead of libftdi - thus the external dependency |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
10 * is only libusb. |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
11 */ |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
12 |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <sys/types.h> |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 #include <stdio.h> |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 #include <stdlib.h> |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 #include <unistd.h> |
21
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
17 #include <usb.h> |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
18 #include "../libuwrap/find_dev.h" |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
19 #include "../libuwrap/open_close.h" |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
20 #include "../libftmini/eeprom_func.h" |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 |
46
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
22 char *device_selector; |
68
5cbde3c80c24
fteeprom-{erase,prog}: detach logic: change to detach by default
Mychaela Falconia <falcon@freecalypso.org>
parents:
46
diff
changeset
|
23 int no_detach; |
46
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
24 |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
25 process_cmdline(argc, argv) |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
26 char **argv; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
27 { |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
28 int c; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
29 extern int optind; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
30 extern char *optarg; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
31 |
68
5cbde3c80c24
fteeprom-{erase,prog}: detach logic: change to detach by default
Mychaela Falconia <falcon@freecalypso.org>
parents:
46
diff
changeset
|
32 while ((c = getopt(argc, argv, "n")) != EOF) { |
46
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
33 switch (c) { |
68
5cbde3c80c24
fteeprom-{erase,prog}: detach logic: change to detach by default
Mychaela Falconia <falcon@freecalypso.org>
parents:
46
diff
changeset
|
34 case 'n': |
5cbde3c80c24
fteeprom-{erase,prog}: detach logic: change to detach by default
Mychaela Falconia <falcon@freecalypso.org>
parents:
46
diff
changeset
|
35 no_detach = 1; |
46
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
36 continue; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
37 default: |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
38 /* error msg already printed */ |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
39 exit(1); |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
40 } |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
41 } |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
42 if (argc != optind + 1) { |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
43 fprintf(stderr, "usage: %s [options] device-selector\n", |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
44 argv[0]); |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
45 exit(1); |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
46 } |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
47 device_selector = argv[optind]; |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
48 } |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
49 |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 main(argc, argv) |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 char **argv; |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 { |
21
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
53 struct usb_device *dev; |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
54 usb_dev_handle *usbh; |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 |
46
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
56 process_cmdline(argc, argv); |
d682cac178c2
fteeprom-erase: implement -d option for interface detach
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
57 dev = find_usbdev_by_desc_string(device_selector); |
21
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
58 if (!dev) { |
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
59 fprintf(stderr, "error: specified USB device not found\n"); |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 exit(1); |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } |
42
2746b160afb7
fteeprom-erase: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
21
diff
changeset
|
62 usbh = usb_open(dev); |
2746b160afb7
fteeprom-erase: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
21
diff
changeset
|
63 if (!usbh) { |
2746b160afb7
fteeprom-erase: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
21
diff
changeset
|
64 fprintf(stderr, "error: usb_open() failed\n"); |
2746b160afb7
fteeprom-erase: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
21
diff
changeset
|
65 exit(1); |
2746b160afb7
fteeprom-erase: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
21
diff
changeset
|
66 } |
68
5cbde3c80c24
fteeprom-{erase,prog}: detach logic: change to detach by default
Mychaela Falconia <falcon@freecalypso.org>
parents:
46
diff
changeset
|
67 if (!no_detach) |
5cbde3c80c24
fteeprom-{erase,prog}: detach logic: change to detach by default
Mychaela Falconia <falcon@freecalypso.org>
parents:
46
diff
changeset
|
68 usbwrap_claim_all_ifs(usbh); |
21
af801ab43a33
fteeprom-erase: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
69 ftmini_erase_eeprom(usbh); |
42
2746b160afb7
fteeprom-erase: we don't need to claim the interface
Mychaela Falconia <falcon@freecalypso.org>
parents:
21
diff
changeset
|
70 usb_close(usbh); |
0
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 exit(0); |
11b8a30333b3
fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 } |