Remove other duplicate-code
[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
14int main(int argc, char **argv)
15{
f38b0866 16 struct ftdi_context *ftdi;
82e0fc5a 17 unsigned char buf[256];
2db3a766
UB
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;
4fb2ebbb 24 int use_defaults = 0;
2db3a766 25 int large_chip = 0;
0091182e 26 int do_write = 0;
a35aa9bd 27 int size;
82e0fc5a 28 int value;
2db3a766 29
f38b0866
UB
30 if ((ftdi = ftdi_new()) == 0)
31 {
05c2e40a
TJ
32 fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
33 ftdi_get_error_string(ftdi));
f38b0866
UB
34 return EXIT_FAILURE;
35 }
36
0091182e 37 while ((i = getopt(argc, argv, "d::ev:p:l:P:S:w")) != -1)
2db3a766
UB
38 {
39 switch (i)
40 {
05c2e40a
TJ
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':
2db3a766 56 desc = optarg;
05c2e40a
TJ
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 "
82e0fc5a 67 "EEPROM or for 256 Byte EEPROM if some [num] is given\n");
05c2e40a
TJ
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 "
2db3a766 74 "product description\n");
05c2e40a 75 fprintf(stderr, "\t-S <string? Search for device with given "
2db3a766 76 "serial number\n");
05c2e40a 77 exit(-1);
2db3a766
UB
78 }
79 }
80
2db3a766 81 // Select first interface
f38b0866 82 ftdi_set_interface(ftdi, INTERFACE_ANY);
2db3a766
UB
83
84 // Open device
f38b0866 85 f = ftdi_usb_open_desc(ftdi, vid, pid, desc, serial);
2db3a766
UB
86 if (f < 0)
87 {
88 fprintf(stderr, "Device VID 0x%04x PID 0x%04x", vid, pid);
05c2e40a 89 if (desc)
2db3a766 90 fprintf(stderr, " Desc %s", desc);
05c2e40a 91 if (serial)
2db3a766
UB
92 fprintf(stderr, " Serial %s", serial);
93 fprintf(stderr, "\n");
05c2e40a
TJ
94 fprintf(stderr, "unable to open ftdi device: %d (%s)\n",
95 f, ftdi_get_error_string(ftdi));
2db3a766
UB
96
97 exit(-1);
98 }
99
100 if (erase)
101 {
82e0fc5a 102 f = ftdi_erase_eeprom(ftdi); /* needed to determine EEPROM chip type */
2db3a766
UB
103 if (f < 0)
104 {
05c2e40a 105 fprintf(stderr, "Erase failed: %s",
f38b0866 106 ftdi_get_error_string(ftdi));
2db3a766
UB
107 return -2;
108 }
05c2e40a
TJ
109 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
110 {
111 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
82e0fc5a 112 f, ftdi_get_error_string(ftdi));
05c2e40a 113 }
82e0fc5a 114 if (value == -1)
2db3a766 115 fprintf(stderr, "No EEPROM\n");
82e0fc5a 116 else if (value == 0)
2db3a766
UB
117 fprintf(stderr, "Internal EEPROM\n");
118 else
82e0fc5a 119 fprintf(stderr, "Found 93x%02x\n", value);
2db3a766 120 return 0;
05c2e40a 121 }
2db3a766 122
05c2e40a 123 if (use_defaults)
2db3a766 124 {
82e0fc5a 125 ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001");
05c2e40a
TJ
126 if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
127 {
128 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
82e0fc5a 129 f, ftdi_get_error_string(ftdi));
05c2e40a
TJ
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 }
a35aa9bd 137 f=(ftdi_eeprom_build(ftdi));
2db3a766
UB
138 if (f < 0)
139 {
05c2e40a 140 fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n",
f38b0866 141 f, ftdi_get_error_string(ftdi));
2db3a766
UB
142 exit(-1);
143 }
144 }
05c2e40a 145 else if (do_write)
0091182e
UB
146 {
147 ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001");
05c2e40a
TJ
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",
82e0fc5a 152 f, ftdi_get_error_string(ftdi));
05c2e40a 153 }
82e0fc5a 154 f = ftdi_erase_eeprom(ftdi);/* needed to determine EEPROM chip type */
05c2e40a
TJ
155 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
156 {
157 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
82e0fc5a 158 f, ftdi_get_error_string(ftdi));
05c2e40a 159 }
82e0fc5a 160 if (value == -1)
0091182e 161 fprintf(stderr, "No EEPROM\n");
82e0fc5a 162 else if (value == 0)
0091182e
UB
163 fprintf(stderr, "Internal EEPROM\n");
164 else
82e0fc5a 165 fprintf(stderr, "Found 93x%02x\n", value);
0091182e
UB
166 f=(ftdi_eeprom_build(ftdi));
167 if (f < 0)
168 {
05c2e40a 169 fprintf(stderr, "Erase failed: %s",
0091182e
UB
170 ftdi_get_error_string(ftdi));
171 return -2;
172 }
173 f = ftdi_write_eeprom(ftdi);
174 {
05c2e40a 175 fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n",
0091182e
UB
176 f, ftdi_get_error_string(ftdi));
177 exit(-1);
178 }
05c2e40a 179 }
23fc2fe3
UB
180 f = ftdi_read_eeprom(ftdi);
181 if (f < 0)
2db3a766 182 {
23fc2fe3
UB
183 fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n",
184 f, ftdi_get_error_string(ftdi));
185 exit(-1);
2db3a766 186 }
0091182e
UB
187
188
82e0fc5a 189 ftdi_get_eeprom_value(ftdi, CHIP_SIZE, & value);
fa457b38
UB
190 if (value <0)
191 {
192 fprintf(stderr, "No EEPROM found\n");
193 return -1;
194
195 }
82e0fc5a 196 fprintf(stderr, "Chip type %d ftdi_eeprom_size: %d\n", ftdi->type, value);
a35aa9bd
UB
197 if (ftdi->type == TYPE_R)
198 size = 0xa0;
199 else
82e0fc5a
UB
200 size = value;
201 ftdi_get_eeprom_buf(ftdi, buf, size);
05c2e40a 202 for (i=0; i < size; i += 16)
2db3a766 203 {
05c2e40a
TJ
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");
2db3a766
UB
218 }
219
a35aa9bd 220 f = ftdi_eeprom_decode(ftdi, 1);
0091182e 221 if (f < 0)
2db3a766 222 {
05c2e40a
TJ
223 fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n",
224 f, ftdi_get_error_string(ftdi));
2db3a766
UB
225 exit(-1);
226 }
2db3a766 227
f38b0866
UB
228 ftdi_usb_close(ftdi);
229 ftdi_free(ftdi);
0091182e 230 return 0;
2db3a766 231}