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