Only try ftdi_usb_open_dev if devices found
[libftdi] / examples / eeprom.c
... / ...
CommitLineData
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
14int 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
70int main(int argc, char **argv)
71{
72 struct ftdi_context *ftdi;
73 int f, i;
74 int vid = 0;
75 int pid = 0;
76 char const *desc = 0;
77 char const *serial = 0;
78 int erase = 0;
79 int use_defaults = 0;
80 int large_chip = 0;
81 int do_write = 0;
82 int retval = 0;
83 int value;
84
85 if ((ftdi = ftdi_new()) == 0)
86 {
87 fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
88 ftdi_get_error_string(ftdi));
89 return EXIT_FAILURE;
90 }
91
92 while ((i = getopt(argc, argv, "d::ev:p:l:P:S:w")) != -1)
93 {
94 switch (i)
95 {
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':
111 desc = optarg;
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 "
122 "EEPROM or for 256 Byte EEPROM if some [num] is given\n");
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 "
129 "product description\n");
130 fprintf(stderr, "\t-S <string? Search for device with given "
131 "serial number\n");
132 retval = -1;
133 goto done;
134 }
135 }
136
137 // Select first interface
138 ftdi_set_interface(ftdi, INTERFACE_ANY);
139
140 if (!vid && !pid && desc == NULL && serial == NULL)
141 {
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 }
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 }
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 }
206 }
207 if (erase)
208 {
209 f = ftdi_erase_eeprom(ftdi); /* needed to determine EEPROM chip type */
210 if (f < 0)
211 {
212 fprintf(stderr, "Erase failed: %s",
213 ftdi_get_error_string(ftdi));
214 retval = -2;
215 goto done;
216 }
217 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
218 {
219 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
220 f, ftdi_get_error_string(ftdi));
221 }
222 if (value == -1)
223 fprintf(stderr, "No EEPROM\n");
224 else if (value == 0)
225 fprintf(stderr, "Internal EEPROM\n");
226 else
227 fprintf(stderr, "Found 93x%02x\n", value);
228 retval = 0;
229 goto done;
230 }
231
232 if (use_defaults)
233 {
234 ftdi_eeprom_initdefaults(ftdi, NULL, NULL, NULL);
235 if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
236 {
237 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
238 f, ftdi_get_error_string(ftdi));
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 }
246 f=(ftdi_eeprom_build(ftdi));
247 if (f < 0)
248 {
249 fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n",
250 f, ftdi_get_error_string(ftdi));
251 retval = -1;
252 goto done;
253 }
254 }
255 else if (do_write)
256 {
257 ftdi_eeprom_initdefaults(ftdi, NULL, NULL, NULL);
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",
262 f, ftdi_get_error_string(ftdi));
263 }
264 f = ftdi_erase_eeprom(ftdi);/* needed to determine EEPROM chip type */
265 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
266 {
267 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
268 f, ftdi_get_error_string(ftdi));
269 }
270 if (value == -1)
271 fprintf(stderr, "No EEPROM\n");
272 else if (value == 0)
273 fprintf(stderr, "Internal EEPROM\n");
274 else
275 fprintf(stderr, "Found 93x%02x\n", value);
276 f=(ftdi_eeprom_build(ftdi));
277 if (f < 0)
278 {
279 fprintf(stderr, "Erase failed: %s",
280 ftdi_get_error_string(ftdi));
281 retval = -2;
282 goto done;
283 }
284 f = ftdi_write_eeprom(ftdi);
285 {
286 fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n",
287 f, ftdi_get_error_string(ftdi));
288 retval = 1;
289 goto done;
290 }
291 }
292 retval = read_decode_eeprom(ftdi);
293done:
294 ftdi_usb_close(ftdi);
295do_deinit:
296 ftdi_free(ftdi);
297 return retval;
298}