examples/eeprom: With no VID/PID/desc/serial, print eeprom content
[libftdi] / examples / eeprom.c
CommitLineData
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
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
2db3a766
UB
70int 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 }
172 f = ftdi_usb_open_dev(ftdi, devlist[0].dev);
173 ftdi_list_free(&devlist);
174 }
175 else
176 {
177 // Open device
178 f = ftdi_usb_open_desc(ftdi, vid, pid, desc, serial);
179 if (f < 0)
180 {
181 fprintf(stderr, "Device VID 0x%04x PID 0x%04x", vid, pid);
182 if (desc)
183 fprintf(stderr, " Desc %s", desc);
184 if (serial)
185 fprintf(stderr, " Serial %s", serial);
186 fprintf(stderr, "\n");
187 fprintf(stderr, "unable to open ftdi device: %d (%s)\n",
188 f, ftdi_get_error_string(ftdi));
189
190 retval = -1;
191 goto done;
192 }
2db3a766 193 }
2db3a766
UB
194 if (erase)
195 {
82e0fc5a 196 f = ftdi_erase_eeprom(ftdi); /* needed to determine EEPROM chip type */
2db3a766
UB
197 if (f < 0)
198 {
05c2e40a 199 fprintf(stderr, "Erase failed: %s",
f38b0866 200 ftdi_get_error_string(ftdi));
bd4a9e38
UB
201 retval = -2;
202 goto done;
2db3a766 203 }
05c2e40a
TJ
204 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
205 {
206 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
82e0fc5a 207 f, ftdi_get_error_string(ftdi));
05c2e40a 208 }
82e0fc5a 209 if (value == -1)
2db3a766 210 fprintf(stderr, "No EEPROM\n");
82e0fc5a 211 else if (value == 0)
2db3a766
UB
212 fprintf(stderr, "Internal EEPROM\n");
213 else
82e0fc5a 214 fprintf(stderr, "Found 93x%02x\n", value);
bd4a9e38
UB
215 retval = 0;
216 goto done;
05c2e40a 217 }
2db3a766 218
05c2e40a 219 if (use_defaults)
2db3a766 220 {
bd4a9e38 221 ftdi_eeprom_initdefaults(ftdi, NULL, NULL, NULL);
05c2e40a
TJ
222 if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
223 {
224 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
82e0fc5a 225 f, ftdi_get_error_string(ftdi));
05c2e40a
TJ
226 }
227 if (large_chip)
228 if (ftdi_set_eeprom_value(ftdi, CHIP_TYPE, 0x66) <0)
229 {
230 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
231 f, ftdi_get_error_string(ftdi));
232 }
a35aa9bd 233 f=(ftdi_eeprom_build(ftdi));
2db3a766
UB
234 if (f < 0)
235 {
05c2e40a 236 fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n",
f38b0866 237 f, ftdi_get_error_string(ftdi));
bd4a9e38
UB
238 retval = -1;
239 goto done;
2db3a766
UB
240 }
241 }
05c2e40a 242 else if (do_write)
0091182e 243 {
bd4a9e38 244 ftdi_eeprom_initdefaults(ftdi, NULL, NULL, NULL);
05c2e40a
TJ
245 f = ftdi_erase_eeprom(ftdi);
246 if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
247 {
248 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
82e0fc5a 249 f, ftdi_get_error_string(ftdi));
05c2e40a 250 }
82e0fc5a 251 f = ftdi_erase_eeprom(ftdi);/* needed to determine EEPROM chip type */
05c2e40a
TJ
252 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
253 {
254 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
82e0fc5a 255 f, ftdi_get_error_string(ftdi));
05c2e40a 256 }
82e0fc5a 257 if (value == -1)
0091182e 258 fprintf(stderr, "No EEPROM\n");
82e0fc5a 259 else if (value == 0)
0091182e
UB
260 fprintf(stderr, "Internal EEPROM\n");
261 else
82e0fc5a 262 fprintf(stderr, "Found 93x%02x\n", value);
0091182e
UB
263 f=(ftdi_eeprom_build(ftdi));
264 if (f < 0)
265 {
05c2e40a 266 fprintf(stderr, "Erase failed: %s",
0091182e 267 ftdi_get_error_string(ftdi));
bd4a9e38
UB
268 retval = -2;
269 goto done;
0091182e
UB
270 }
271 f = ftdi_write_eeprom(ftdi);
272 {
05c2e40a 273 fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n",
0091182e 274 f, ftdi_get_error_string(ftdi));
bd4a9e38
UB
275 retval = 1;
276 goto done;
0091182e 277 }
05c2e40a 278 }
8ee8775e 279 retval = read_decode_eeprom(ftdi);
bd4a9e38 280done:
f38b0866 281 ftdi_usb_close(ftdi);
8ee8775e 282do_deinit:
f38b0866 283 ftdi_free(ftdi);
bd4a9e38 284 return retval;
2db3a766 285}