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