Remove other duplicate-code
[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 main(int argc, char **argv)
15{
16 struct ftdi_context *ftdi;
17 unsigned char buf[256];
18 int f, i, j;
19 int vid = 0x0403;
20 int pid = 0x6010;
21 char const *desc = 0;
22 char const *serial = 0;
23 int erase = 0;
24 int use_defaults = 0;
25 int large_chip = 0;
26 int do_write = 0;
27 int size;
28 int value;
29
30 if ((ftdi = ftdi_new()) == 0)
31 {
32 fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
33 ftdi_get_error_string(ftdi));
34 return EXIT_FAILURE;
35 }
36
37 while ((i = getopt(argc, argv, "d::ev:p:l:P:S:w")) != -1)
38 {
39 switch (i)
40 {
41 case 'd':
42 use_defaults = 1;
43 if (optarg)
44 large_chip = 0x66;
45 break;
46 case 'e':
47 erase = 1;
48 break;
49 case 'v':
50 vid = strtoul(optarg, NULL, 0);
51 break;
52 case 'p':
53 pid = strtoul(optarg, NULL, 0);
54 break;
55 case 'P':
56 desc = optarg;
57 break;
58 case 'S':
59 serial = optarg;
60 break;
61 case 'w':
62 do_write = 1;
63 break;
64 default:
65 fprintf(stderr, "usage: %s [options]\n", *argv);
66 fprintf(stderr, "\t-d[num] Work with default valuesfor 128 Byte "
67 "EEPROM or for 256 Byte EEPROM if some [num] is given\n");
68 fprintf(stderr, "\t-w write\n");
69 fprintf(stderr, "\t-e erase\n");
70 fprintf(stderr, "\t-v verbose decoding\n");
71 fprintf(stderr, "\t-p <number> Search for device with PID == number\n");
72 fprintf(stderr, "\t-v <number> Search for device with VID == number\n");
73 fprintf(stderr, "\t-P <string? Search for device with given "
74 "product description\n");
75 fprintf(stderr, "\t-S <string? Search for device with given "
76 "serial number\n");
77 exit(-1);
78 }
79 }
80
81 // Select first interface
82 ftdi_set_interface(ftdi, INTERFACE_ANY);
83
84 // Open device
85 f = ftdi_usb_open_desc(ftdi, vid, pid, desc, serial);
86 if (f < 0)
87 {
88 fprintf(stderr, "Device VID 0x%04x PID 0x%04x", vid, pid);
89 if (desc)
90 fprintf(stderr, " Desc %s", desc);
91 if (serial)
92 fprintf(stderr, " Serial %s", serial);
93 fprintf(stderr, "\n");
94 fprintf(stderr, "unable to open ftdi device: %d (%s)\n",
95 f, ftdi_get_error_string(ftdi));
96
97 exit(-1);
98 }
99
100 if (erase)
101 {
102 f = ftdi_erase_eeprom(ftdi); /* needed to determine EEPROM chip type */
103 if (f < 0)
104 {
105 fprintf(stderr, "Erase failed: %s",
106 ftdi_get_error_string(ftdi));
107 return -2;
108 }
109 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
110 {
111 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
112 f, ftdi_get_error_string(ftdi));
113 }
114 if (value == -1)
115 fprintf(stderr, "No EEPROM\n");
116 else if (value == 0)
117 fprintf(stderr, "Internal EEPROM\n");
118 else
119 fprintf(stderr, "Found 93x%02x\n", value);
120 return 0;
121 }
122
123 if (use_defaults)
124 {
125 ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001");
126 if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
127 {
128 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
129 f, ftdi_get_error_string(ftdi));
130 }
131 if (large_chip)
132 if (ftdi_set_eeprom_value(ftdi, CHIP_TYPE, 0x66) <0)
133 {
134 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
135 f, ftdi_get_error_string(ftdi));
136 }
137 f=(ftdi_eeprom_build(ftdi));
138 if (f < 0)
139 {
140 fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n",
141 f, ftdi_get_error_string(ftdi));
142 exit(-1);
143 }
144 }
145 else if (do_write)
146 {
147 ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001");
148 f = ftdi_erase_eeprom(ftdi);
149 if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
150 {
151 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
152 f, ftdi_get_error_string(ftdi));
153 }
154 f = ftdi_erase_eeprom(ftdi);/* needed to determine EEPROM chip type */
155 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
156 {
157 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
158 f, ftdi_get_error_string(ftdi));
159 }
160 if (value == -1)
161 fprintf(stderr, "No EEPROM\n");
162 else if (value == 0)
163 fprintf(stderr, "Internal EEPROM\n");
164 else
165 fprintf(stderr, "Found 93x%02x\n", value);
166 f=(ftdi_eeprom_build(ftdi));
167 if (f < 0)
168 {
169 fprintf(stderr, "Erase failed: %s",
170 ftdi_get_error_string(ftdi));
171 return -2;
172 }
173 f = ftdi_write_eeprom(ftdi);
174 {
175 fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n",
176 f, ftdi_get_error_string(ftdi));
177 exit(-1);
178 }
179 }
180 f = ftdi_read_eeprom(ftdi);
181 if (f < 0)
182 {
183 fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n",
184 f, ftdi_get_error_string(ftdi));
185 exit(-1);
186 }
187
188
189 ftdi_get_eeprom_value(ftdi, CHIP_SIZE, & value);
190 if (value <0)
191 {
192 fprintf(stderr, "No EEPROM found\n");
193 return -1;
194
195 }
196 fprintf(stderr, "Chip type %d ftdi_eeprom_size: %d\n", ftdi->type, value);
197 if (ftdi->type == TYPE_R)
198 size = 0xa0;
199 else
200 size = value;
201 ftdi_get_eeprom_buf(ftdi, buf, size);
202 for (i=0; i < size; i += 16)
203 {
204 fprintf(stdout,"0x%03x:", i);
205
206 for (j = 0; j< 8; j++)
207 fprintf(stdout," %02x", buf[i+j]);
208 fprintf(stdout," ");
209 for (; j< 16; j++)
210 fprintf(stdout," %02x", buf[i+j]);
211 fprintf(stdout," ");
212 for (j = 0; j< 8; j++)
213 fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.');
214 fprintf(stdout," ");
215 for (; j< 16; j++)
216 fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.');
217 fprintf(stdout,"\n");
218 }
219
220 f = ftdi_eeprom_decode(ftdi, 1);
221 if (f < 0)
222 {
223 fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n",
224 f, ftdi_get_error_string(ftdi));
225 exit(-1);
226 }
227
228 ftdi_usb_close(ftdi);
229 ftdi_free(ftdi);
230 return 0;
231}