Reformat example code to match libftdi style:
[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
UB
125 ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001");
126 ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001");
05c2e40a
TJ
127 if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
128 {
129 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
82e0fc5a 130 f, ftdi_get_error_string(ftdi));
05c2e40a
TJ
131 }
132 if (large_chip)
133 if (ftdi_set_eeprom_value(ftdi, CHIP_TYPE, 0x66) <0)
134 {
135 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
136 f, ftdi_get_error_string(ftdi));
137 }
a35aa9bd 138 f=(ftdi_eeprom_build(ftdi));
2db3a766
UB
139 if (f < 0)
140 {
05c2e40a 141 fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n",
f38b0866 142 f, ftdi_get_error_string(ftdi));
2db3a766
UB
143 exit(-1);
144 }
145 }
05c2e40a 146 else if (do_write)
0091182e
UB
147 {
148 ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001");
05c2e40a
TJ
149 f = ftdi_erase_eeprom(ftdi);
150 if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0)
151 {
152 fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n",
82e0fc5a 153 f, ftdi_get_error_string(ftdi));
05c2e40a 154 }
82e0fc5a 155 f = ftdi_erase_eeprom(ftdi);/* needed to determine EEPROM chip type */
05c2e40a
TJ
156 if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0)
157 {
158 fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n",
82e0fc5a 159 f, ftdi_get_error_string(ftdi));
05c2e40a 160 }
82e0fc5a 161 if (value == -1)
0091182e 162 fprintf(stderr, "No EEPROM\n");
82e0fc5a 163 else if (value == 0)
0091182e
UB
164 fprintf(stderr, "Internal EEPROM\n");
165 else
82e0fc5a 166 fprintf(stderr, "Found 93x%02x\n", value);
0091182e
UB
167 f=(ftdi_eeprom_build(ftdi));
168 if (f < 0)
169 {
05c2e40a 170 fprintf(stderr, "Erase failed: %s",
0091182e
UB
171 ftdi_get_error_string(ftdi));
172 return -2;
173 }
174 f = ftdi_write_eeprom(ftdi);
175 {
05c2e40a 176 fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n",
0091182e
UB
177 f, ftdi_get_error_string(ftdi));
178 exit(-1);
179 }
180 f = ftdi_read_eeprom(ftdi);
181 if (f < 0)
182 {
05c2e40a 183 fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n",
0091182e
UB
184 f, ftdi_get_error_string(ftdi));
185 exit(-1);
186 }
05c2e40a 187 }
2db3a766
UB
188 else
189 {
a35aa9bd 190 f = ftdi_read_eeprom(ftdi);
2db3a766
UB
191 if (f < 0)
192 {
05c2e40a 193 fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n",
f38b0866 194 f, ftdi_get_error_string(ftdi));
2db3a766
UB
195 exit(-1);
196 }
197 }
0091182e
UB
198
199
82e0fc5a
UB
200 ftdi_get_eeprom_value(ftdi, CHIP_SIZE, & value);
201 fprintf(stderr, "Chip type %d ftdi_eeprom_size: %d\n", ftdi->type, value);
a35aa9bd
UB
202 if (ftdi->type == TYPE_R)
203 size = 0xa0;
204 else
82e0fc5a
UB
205 size = value;
206 ftdi_get_eeprom_buf(ftdi, buf, size);
05c2e40a 207 for (i=0; i < size; i += 16)
2db3a766 208 {
05c2e40a
TJ
209 fprintf(stdout,"0x%03x:", i);
210
211 for (j = 0; j< 8; j++)
212 fprintf(stdout," %02x", buf[i+j]);
213 fprintf(stdout," ");
214 for (; j< 16; j++)
215 fprintf(stdout," %02x", buf[i+j]);
216 fprintf(stdout," ");
217 for (j = 0; j< 8; j++)
218 fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.');
219 fprintf(stdout," ");
220 for (; j< 16; j++)
221 fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.');
222 fprintf(stdout,"\n");
2db3a766
UB
223 }
224
a35aa9bd 225 f = ftdi_eeprom_decode(ftdi, 1);
0091182e 226 if (f < 0)
2db3a766 227 {
05c2e40a
TJ
228 fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n",
229 f, ftdi_get_error_string(ftdi));
2db3a766
UB
230 exit(-1);
231 }
2db3a766 232
f38b0866
UB
233 ftdi_usb_close(ftdi);
234 ftdi_free(ftdi);
0091182e 235 return 0;
2db3a766 236}