Const correctness for char* strings
[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 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
71int 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 while ((i = getopt(argc, argv, "d::ev:p:l:P:S:w")) != -1)
94 {
95 switch (i)
96 {
97 case 'd':
98 use_defaults = 1;
99 if (optarg)
100 large_chip = 0x66;
101 break;
102 case 'e':
103 erase = 1;
104 break;
105 case 'v':
106 vid = strtoul(optarg, NULL, 0);
107 break;
108 case 'p':
109 pid = strtoul(optarg, NULL, 0);
110 break;
111 case 'P':
112 desc = optarg;
113 break;
114 case 'S':
115 serial = optarg;
116 break;
117 case 'w':
118 do_write = 1;
119 break;
120 default:
121 fprintf(stderr, "usage: %s [options]\n", *argv);
122 fprintf(stderr, "\t-d[num] Work with default valuesfor 128 Byte "
123 "EEPROM or for 256 Byte EEPROM if some [num] is given\n");
124 fprintf(stderr, "\t-w write\n");
125 fprintf(stderr, "\t-e erase\n");
126 fprintf(stderr, "\t-v verbose decoding\n");
127 fprintf(stderr, "\t-p <number> Search for device with PID == number\n");
128 fprintf(stderr, "\t-v <number> Search for device with VID == number\n");
129 fprintf(stderr, "\t-P <string? Search for device with given "
130 "product description\n");
131 fprintf(stderr, "\t-S <string? Search for device with given "
132 "serial number\n");
133 retval = -1;
134 goto done;
135 }
136 }
137
138 // Select first interface
139 ftdi_set_interface(ftdi, INTERFACE_ANY);
140
141 if (!vid && !pid && desc == NULL && serial == NULL)
142 {
143 struct ftdi_device_list *devlist, *curdev;
144 int res;
145 if ((res = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0)
146 {
147 fprintf(stderr, "No FTDI with default VID/PID found\n");
148 retval = EXIT_FAILURE;
149 goto do_deinit;
150 }
151 if (res > 1)
152 {
153 int i = 1;
154 fprintf(stderr, "%d FTDI devices found: Only Readout on EEPROM done. ",res);
155 fprintf(stderr, "Use VID/PID/desc/serial to select device\n");
156 for (curdev = devlist; curdev != NULL; curdev= curdev->next, i++)
157 {
158 f = ftdi_usb_open_dev(ftdi, curdev->dev);
159 if (f<0)
160 {
161 fprintf(stderr, "Unable to open device %d: (%s)",
162 i, ftdi_get_error_string(ftdi));
163 continue;
164 }
165 fprintf(stderr, "Decoded values of device %d:\n", i);
166 read_decode_eeprom(ftdi);
167 ftdi_usb_close(ftdi);
168 }
169 ftdi_list_free(&devlist);
170 retval = EXIT_SUCCESS;
171 goto do_deinit;
172 }
173 else if (res == 1)
174 {
175 f = ftdi_usb_open_dev(ftdi, devlist[0].dev);
176 if (f<0)
177 {
178 fprintf(stderr, "Unable to open device %d: (%s)",
179 i, ftdi_get_error_string(ftdi));
180 }
181 }
182 else
183 {
184 fprintf(stderr, "No devices found\n");
185 f = 0;
186 }
187 ftdi_list_free(&devlist);
188 }
189 else
190 {
191 // Open device
192 f = ftdi_usb_open_desc(ftdi, vid, pid, desc, serial);
193 if (f < 0)
194 {
195 fprintf(stderr, "Device VID 0x%04x PID 0x%04x", vid, pid);
196 if (desc)
197 fprintf(stderr, " Desc %s", desc);
198 if (serial)
199 fprintf(stderr, " Serial %s", serial);
200 fprintf(stderr, "\n");
201 fprintf(stderr, "unable to open ftdi device: %d (%s)\n",
202 f, ftdi_get_error_string(ftdi));
203
204 retval = -1;
205 goto done;
206 }
207 }
208 if (erase)
209 {
210 f = ftdi_erase_eeprom(ftdi); /* needed to determine EEPROM chip type */
211 if (f < 0)
212 {
213 fprintf(stderr, "Erase failed: %s",
214 ftdi_get_error_string(ftdi));
215 retval = -2;
216 goto done;
217 }
218 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
219 {
220 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
221 f, ftdi_get_error_string(ftdi));
222 }
223 if (value == -1)
224 fprintf(stderr, "No EEPROM\n");
225 else if (value == 0)
226 fprintf(stderr, "Internal EEPROM\n");
227 else
228 fprintf(stderr, "Found 93x%02x\n", value);
229 retval = 0;
230 goto done;
231 }
232
233 if (use_defaults)
234 {
235 ftdi_eeprom_initdefaults(ftdi, NULL, NULL, NULL);
236 if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
237 {
238 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
239 f, ftdi_get_error_string(ftdi));
240 }
241 if (large_chip)
242 if (ftdi_set_eeprom_value(ftdi, CHIP_TYPE, 0x66) <0)
243 {
244 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
245 f, ftdi_get_error_string(ftdi));
246 }
247 f=(ftdi_eeprom_build(ftdi));
248 if (f < 0)
249 {
250 fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n",
251 f, ftdi_get_error_string(ftdi));
252 retval = -1;
253 goto done;
254 }
255 }
256 else if (do_write)
257 {
258 ftdi_eeprom_initdefaults(ftdi, NULL, NULL, NULL);
259 f = ftdi_erase_eeprom(ftdi);
260 if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
261 {
262 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
263 f, ftdi_get_error_string(ftdi));
264 }
265 f = ftdi_erase_eeprom(ftdi);/* needed to determine EEPROM chip type */
266 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
267 {
268 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
269 f, ftdi_get_error_string(ftdi));
270 }
271 if (value == -1)
272 fprintf(stderr, "No EEPROM\n");
273 else if (value == 0)
274 fprintf(stderr, "Internal EEPROM\n");
275 else
276 fprintf(stderr, "Found 93x%02x\n", value);
277 f=(ftdi_eeprom_build(ftdi));
278 if (f < 0)
279 {
280 fprintf(stderr, "Erase failed: %s",
281 ftdi_get_error_string(ftdi));
282 retval = -2;
283 goto done;
284 }
285 f = ftdi_write_eeprom(ftdi);
286 {
287 fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n",
288 f, ftdi_get_error_string(ftdi));
289 retval = 1;
290 goto done;
291 }
292 }
293 retval = read_decode_eeprom(ftdi);
294done:
295 ftdi_usb_close(ftdi);
296do_deinit:
297 ftdi_free(ftdi);
298 return retval;
299}