CMake: bump the minimal required version to 3.5
[libftdi] / examples / eeprom.c
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
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 or EEPROM empty\n");
34         fprintf(stderr, "On empty EEPROM, use -w option to write default values\n");
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
71 int main(int argc, char **argv)
72 {
73     struct ftdi_context *ftdi;
74     int f, i;
75     int vid = 0;
76     int pid = 0;
77     char const *desc    = 0;
78     char const *serial  = 0;
79     int erase = 0;
80     int use_defaults = 0;
81     int large_chip = 0;
82     int do_write = 0;
83     int retval = 0;
84     int value;
85
86     if ((ftdi = ftdi_new()) == 0)
87     {
88         fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
89                 ftdi_get_error_string(ftdi));
90         return EXIT_FAILURE;
91     }
92
93     ftdi->module_detach_mode = AUTO_DETACH_REATACH_SIO_MODULE;
94
95     while ((i = getopt(argc, argv, "d::ev:p:l:P:S:w")) != -1)
96     {
97         switch (i)
98         {
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':
114                 desc = optarg;
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 "
125                         "EEPROM or for 256 Byte EEPROM if some [num] is given\n");
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 "
132                         "product description\n");
133                 fprintf(stderr, "\t-S <string? Search for device with given "
134                         "serial number\n");
135                 retval = -1;
136                 goto done;
137         }
138     }
139
140     // Select first interface
141     ftdi_set_interface(ftdi, INTERFACE_ANY);
142
143     if (!vid && !pid && desc == NULL && serial == NULL)
144     {
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;
156             fprintf(stderr, "%d FTDI devices found: Only Readout on EEPROM done. \n",res);
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                 {
163                     fprintf(stderr, "Unable to open device %d: (%s)\n",
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         }
175         else if (res == 1)
176         {
177             f = ftdi_usb_open_dev(ftdi,  devlist[0].dev);
178             if (f<0)
179             {
180                 fprintf(stderr, "Unable to open device %d: (%s)\n",
181                         i, ftdi_get_error_string(ftdi));
182             }
183         }
184         else
185         {
186             fprintf(stderr, "No devices found\n");
187             f = 0;
188         }
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));
205
206             retval = -1;
207             goto done;
208         }
209     }
210     if (erase)
211     {
212         f = ftdi_erase_eeprom(ftdi); /* needed to determine EEPROM chip type */
213         if (f < 0)
214         {
215             fprintf(stderr, "Erase failed: %s\n",
216                     ftdi_get_error_string(ftdi));
217             retval =  -2;
218             goto done;
219         }
220         if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
221         {
222             fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
223                     f, ftdi_get_error_string(ftdi));
224         }
225         if (value == -1)
226             fprintf(stderr, "No EEPROM\n");
227         else if (value == 0)
228             fprintf(stderr, "Internal EEPROM\n");
229         else
230             fprintf(stderr, "Found 93x%02x\n", value);
231         retval = 0;
232         goto done;
233     }
234
235     if (use_defaults)
236     {
237         ftdi_eeprom_initdefaults(ftdi, NULL, NULL, NULL);
238         if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
239         {
240             fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
241                     f, ftdi_get_error_string(ftdi));
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             }
249         f=(ftdi_eeprom_build(ftdi));
250         if (f < 0)
251         {
252             fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n",
253                     f, ftdi_get_error_string(ftdi));
254             retval = -1;
255             goto done;
256         }
257     }
258     else if (do_write)
259     {
260         ftdi_eeprom_initdefaults(ftdi, NULL, NULL, NULL);
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",
265                     f, ftdi_get_error_string(ftdi));
266         }
267         f = ftdi_erase_eeprom(ftdi);/* needed to determine EEPROM chip type */
268         if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
269         {
270             fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
271                     f, ftdi_get_error_string(ftdi));
272         }
273         if (value == -1)
274             fprintf(stderr, "No EEPROM\n");
275         else if (value == 0)
276             fprintf(stderr, "Internal EEPROM\n");
277         else
278             fprintf(stderr, "Found 93x%02x\n", value);
279         f=(ftdi_eeprom_build(ftdi));
280         if (f < 0)
281         {
282             fprintf(stderr, "Erase failed: %s\n",
283                     ftdi_get_error_string(ftdi));
284             retval = -2;
285             goto done;
286         }
287         f = ftdi_write_eeprom(ftdi);
288         {
289             fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n",
290                     f, ftdi_get_error_string(ftdi));
291             retval = 1;
292             goto done;
293         }
294     }
295     retval = read_decode_eeprom(ftdi);
296 done:
297     ftdi_usb_close(ftdi);
298 do_deinit:
299     ftdi_free(ftdi);
300     return retval;
301 }