Commit | Line | Data |
---|---|---|
2db3a766 UB |
1 | /* LIBFTDI EEPROM access example |
2 | ||
3 | This program is distributed under the GPL, version 2 | |
4 | */ | |
5 | ||
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | #include <string.h> | |
9 | #include <ctype.h> | |
10 | #include <unistd.h> | |
11 | #include <getopt.h> | |
12 | #include <ftdi.h> | |
13 | ||
8ee8775e UB |
14 | int read_decode_eeprom(struct ftdi_context *ftdi) |
15 | { | |
16 | int i, j, f; | |
17 | int value; | |
18 | int size; | |
19 | unsigned char buf[256]; | |
20 | ||
21 | f = ftdi_read_eeprom(ftdi); | |
22 | if (f < 0) | |
23 | { | |
24 | fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n", | |
25 | f, ftdi_get_error_string(ftdi)); | |
26 | return -1; | |
27 | } | |
28 | ||
29 | ||
30 | ftdi_get_eeprom_value(ftdi, CHIP_SIZE, & value); | |
31 | if (value <0) | |
32 | { | |
33 | fprintf(stderr, "No EEPROM found\n"); | |
34 | return -1; | |
35 | } | |
36 | fprintf(stderr, "Chip type %d ftdi_eeprom_size: %d\n", ftdi->type, value); | |
37 | if (ftdi->type == TYPE_R) | |
38 | size = 0xa0; | |
39 | else | |
40 | size = value; | |
41 | ftdi_get_eeprom_buf(ftdi, buf, size); | |
42 | for (i=0; i < size; i += 16) | |
43 | { | |
44 | fprintf(stdout,"0x%03x:", i); | |
45 | ||
46 | for (j = 0; j< 8; j++) | |
47 | fprintf(stdout," %02x", buf[i+j]); | |
48 | fprintf(stdout," "); | |
49 | for (; j< 16; j++) | |
50 | fprintf(stdout," %02x", buf[i+j]); | |
51 | fprintf(stdout," "); | |
52 | for (j = 0; j< 8; j++) | |
53 | fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.'); | |
54 | fprintf(stdout," "); | |
55 | for (; j< 16; j++) | |
56 | fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.'); | |
57 | fprintf(stdout,"\n"); | |
58 | } | |
59 | ||
60 | f = ftdi_eeprom_decode(ftdi, 1); | |
61 | if (f < 0) | |
62 | { | |
63 | fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n", | |
64 | f, ftdi_get_error_string(ftdi)); | |
65 | return -1; | |
66 | } | |
67 | return 0; | |
68 | } | |
69 | ||
2db3a766 UB |
70 | int main(int argc, char **argv) |
71 | { | |
f38b0866 | 72 | struct ftdi_context *ftdi; |
8ee8775e UB |
73 | int f, i; |
74 | int vid = 0; | |
75 | int pid = 0; | |
2db3a766 UB |
76 | char const *desc = 0; |
77 | char const *serial = 0; | |
78 | int erase = 0; | |
4fb2ebbb | 79 | int use_defaults = 0; |
2db3a766 | 80 | int large_chip = 0; |
0091182e | 81 | int do_write = 0; |
bd4a9e38 | 82 | int retval = 0; |
8ee8775e | 83 | int value; |
2db3a766 | 84 | |
f38b0866 UB |
85 | if ((ftdi = ftdi_new()) == 0) |
86 | { | |
05c2e40a TJ |
87 | fprintf(stderr, "Failed to allocate ftdi structure :%s \n", |
88 | ftdi_get_error_string(ftdi)); | |
f38b0866 UB |
89 | return EXIT_FAILURE; |
90 | } | |
91 | ||
0091182e | 92 | while ((i = getopt(argc, argv, "d::ev:p:l:P:S:w")) != -1) |
2db3a766 UB |
93 | { |
94 | switch (i) | |
95 | { | |
05c2e40a TJ |
96 | case 'd': |
97 | use_defaults = 1; | |
98 | if (optarg) | |
99 | large_chip = 0x66; | |
100 | break; | |
101 | case 'e': | |
102 | erase = 1; | |
103 | break; | |
104 | case 'v': | |
105 | vid = strtoul(optarg, NULL, 0); | |
106 | break; | |
107 | case 'p': | |
108 | pid = strtoul(optarg, NULL, 0); | |
109 | break; | |
110 | case 'P': | |
2db3a766 | 111 | desc = optarg; |
05c2e40a TJ |
112 | break; |
113 | case 'S': | |
114 | serial = optarg; | |
115 | break; | |
116 | case 'w': | |
117 | do_write = 1; | |
118 | break; | |
119 | default: | |
120 | fprintf(stderr, "usage: %s [options]\n", *argv); | |
121 | fprintf(stderr, "\t-d[num] Work with default valuesfor 128 Byte " | |
82e0fc5a | 122 | "EEPROM or for 256 Byte EEPROM if some [num] is given\n"); |
05c2e40a TJ |
123 | fprintf(stderr, "\t-w write\n"); |
124 | fprintf(stderr, "\t-e erase\n"); | |
125 | fprintf(stderr, "\t-v verbose decoding\n"); | |
126 | fprintf(stderr, "\t-p <number> Search for device with PID == number\n"); | |
127 | fprintf(stderr, "\t-v <number> Search for device with VID == number\n"); | |
128 | fprintf(stderr, "\t-P <string? Search for device with given " | |
2db3a766 | 129 | "product description\n"); |
05c2e40a | 130 | fprintf(stderr, "\t-S <string? Search for device with given " |
2db3a766 | 131 | "serial number\n"); |
bd4a9e38 UB |
132 | retval = -1; |
133 | goto done; | |
2db3a766 UB |
134 | } |
135 | } | |
136 | ||
2db3a766 | 137 | // Select first interface |
f38b0866 | 138 | ftdi_set_interface(ftdi, INTERFACE_ANY); |
2db3a766 | 139 | |
8ee8775e | 140 | if (!vid && !pid && desc == NULL && serial == NULL) |
2db3a766 | 141 | { |
8ee8775e UB |
142 | struct ftdi_device_list *devlist, *curdev; |
143 | int res; | |
144 | if ((res = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0) | |
145 | { | |
146 | fprintf(stderr, "No FTDI with default VID/PID found\n"); | |
147 | retval = EXIT_FAILURE; | |
148 | goto do_deinit; | |
149 | } | |
150 | if (res > 1) | |
151 | { | |
152 | int i = 1; | |
153 | fprintf(stderr, "%d FTDI devices found: Only Readout on EEPROM done. ",res); | |
154 | fprintf(stderr, "Use VID/PID/desc/serial to select device\n"); | |
155 | for (curdev = devlist; curdev != NULL; curdev= curdev->next, i++) | |
156 | { | |
157 | f = ftdi_usb_open_dev(ftdi, curdev->dev); | |
158 | if (f<0) | |
159 | { | |
160 | fprintf(stderr, "Unable to open device %d: (%s)", | |
161 | i, ftdi_get_error_string(ftdi)); | |
162 | continue; | |
163 | } | |
164 | fprintf(stderr, "Decoded values of device %d:\n", i); | |
165 | read_decode_eeprom(ftdi); | |
166 | ftdi_usb_close(ftdi); | |
167 | } | |
168 | ftdi_list_free(&devlist); | |
169 | retval = EXIT_SUCCESS; | |
170 | goto do_deinit; | |
171 | } | |
95f29fc4 UB |
172 | else if (res == 1) |
173 | { | |
174 | f = ftdi_usb_open_dev(ftdi, devlist[0].dev); | |
175 | if (f<0) | |
176 | { | |
177 | fprintf(stderr, "Unable to open device %d: (%s)", | |
178 | i, ftdi_get_error_string(ftdi)); | |
179 | } | |
180 | } | |
181 | else | |
182 | { | |
183 | fprintf(stderr, "No devices found\n"); | |
184 | f = 0; | |
185 | } | |
8ee8775e UB |
186 | ftdi_list_free(&devlist); |
187 | } | |
188 | else | |
189 | { | |
190 | // Open device | |
191 | f = ftdi_usb_open_desc(ftdi, vid, pid, desc, serial); | |
192 | if (f < 0) | |
193 | { | |
194 | fprintf(stderr, "Device VID 0x%04x PID 0x%04x", vid, pid); | |
195 | if (desc) | |
196 | fprintf(stderr, " Desc %s", desc); | |
197 | if (serial) | |
198 | fprintf(stderr, " Serial %s", serial); | |
199 | fprintf(stderr, "\n"); | |
200 | fprintf(stderr, "unable to open ftdi device: %d (%s)\n", | |
201 | f, ftdi_get_error_string(ftdi)); | |
202 | ||
203 | retval = -1; | |
204 | goto done; | |
205 | } | |
2db3a766 | 206 | } |
2db3a766 UB |
207 | if (erase) |
208 | { | |
82e0fc5a | 209 | f = ftdi_erase_eeprom(ftdi); /* needed to determine EEPROM chip type */ |
2db3a766 UB |
210 | if (f < 0) |
211 | { | |
05c2e40a | 212 | fprintf(stderr, "Erase failed: %s", |
f38b0866 | 213 | ftdi_get_error_string(ftdi)); |
bd4a9e38 UB |
214 | retval = -2; |
215 | goto done; | |
2db3a766 | 216 | } |
05c2e40a TJ |
217 | if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0) |
218 | { | |
219 | fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n", | |
82e0fc5a | 220 | f, ftdi_get_error_string(ftdi)); |
05c2e40a | 221 | } |
82e0fc5a | 222 | if (value == -1) |
2db3a766 | 223 | fprintf(stderr, "No EEPROM\n"); |
82e0fc5a | 224 | else if (value == 0) |
2db3a766 UB |
225 | fprintf(stderr, "Internal EEPROM\n"); |
226 | else | |
82e0fc5a | 227 | fprintf(stderr, "Found 93x%02x\n", value); |
bd4a9e38 UB |
228 | retval = 0; |
229 | goto done; | |
05c2e40a | 230 | } |
2db3a766 | 231 | |
05c2e40a | 232 | if (use_defaults) |
2db3a766 | 233 | { |
bd4a9e38 | 234 | ftdi_eeprom_initdefaults(ftdi, NULL, NULL, NULL); |
05c2e40a TJ |
235 | if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0) |
236 | { | |
237 | fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n", | |
82e0fc5a | 238 | f, ftdi_get_error_string(ftdi)); |
05c2e40a TJ |
239 | } |
240 | if (large_chip) | |
241 | if (ftdi_set_eeprom_value(ftdi, CHIP_TYPE, 0x66) <0) | |
242 | { | |
243 | fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n", | |
244 | f, ftdi_get_error_string(ftdi)); | |
245 | } | |
a35aa9bd | 246 | f=(ftdi_eeprom_build(ftdi)); |
2db3a766 UB |
247 | if (f < 0) |
248 | { | |
05c2e40a | 249 | fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n", |
f38b0866 | 250 | f, ftdi_get_error_string(ftdi)); |
bd4a9e38 UB |
251 | retval = -1; |
252 | goto done; | |
2db3a766 UB |
253 | } |
254 | } | |
05c2e40a | 255 | else if (do_write) |
0091182e | 256 | { |
bd4a9e38 | 257 | ftdi_eeprom_initdefaults(ftdi, NULL, NULL, NULL); |
05c2e40a TJ |
258 | f = ftdi_erase_eeprom(ftdi); |
259 | if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0) | |
260 | { | |
261 | fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n", | |
82e0fc5a | 262 | f, ftdi_get_error_string(ftdi)); |
05c2e40a | 263 | } |
82e0fc5a | 264 | f = ftdi_erase_eeprom(ftdi);/* needed to determine EEPROM chip type */ |
05c2e40a TJ |
265 | if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0) |
266 | { | |
267 | fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n", | |
82e0fc5a | 268 | f, ftdi_get_error_string(ftdi)); |
05c2e40a | 269 | } |
82e0fc5a | 270 | if (value == -1) |
0091182e | 271 | fprintf(stderr, "No EEPROM\n"); |
82e0fc5a | 272 | else if (value == 0) |
0091182e UB |
273 | fprintf(stderr, "Internal EEPROM\n"); |
274 | else | |
82e0fc5a | 275 | fprintf(stderr, "Found 93x%02x\n", value); |
0091182e UB |
276 | f=(ftdi_eeprom_build(ftdi)); |
277 | if (f < 0) | |
278 | { | |
05c2e40a | 279 | fprintf(stderr, "Erase failed: %s", |
0091182e | 280 | ftdi_get_error_string(ftdi)); |
bd4a9e38 UB |
281 | retval = -2; |
282 | goto done; | |
0091182e UB |
283 | } |
284 | f = ftdi_write_eeprom(ftdi); | |
285 | { | |
05c2e40a | 286 | fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n", |
0091182e | 287 | f, ftdi_get_error_string(ftdi)); |
bd4a9e38 UB |
288 | retval = 1; |
289 | goto done; | |
0091182e | 290 | } |
05c2e40a | 291 | } |
8ee8775e | 292 | retval = read_decode_eeprom(ftdi); |
bd4a9e38 | 293 | done: |
f38b0866 | 294 | ftdi_usb_close(ftdi); |
8ee8775e | 295 | do_deinit: |
f38b0866 | 296 | ftdi_free(ftdi); |
bd4a9e38 | 297 | return retval; |
2db3a766 | 298 | } |