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; |
2db3a766 | 29 | |
f38b0866 UB |
30 | if ((ftdi = ftdi_new()) == 0) |
31 | { | |
a35aa9bd UB |
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 | { | |
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; | |
0091182e UB |
61 | case 'w': |
62 | do_write = 1; | |
63 | break; | |
2db3a766 UB |
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"); |
4e7fa44d | 68 | fprintf(stderr, "\t-w write\n"); |
2db3a766 UB |
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 | ||
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); | |
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", | |
f38b0866 | 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 | { | |
105 | fprintf(stderr, "Erase failed: %s", | |
f38b0866 | 106 | ftdi_get_error_string(ftdi)); |
2db3a766 UB |
107 | return -2; |
108 | } | |
82e0fc5a UB |
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) | |
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 UB |
120 | return 0; |
121 | } | |
122 | ||
2db3a766 UB |
123 | if(use_defaults) |
124 | { | |
82e0fc5a UB |
125 | ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001"); |
126 | ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001"); | |
127 | if (ftdi_set_eeprom_value(ftdi, MAX_POWER, 500) <0) | |
128 | { | |
129 | fprintf(stderr, "ftdi_set_eeprom_value: %d (%s)\n", | |
130 | f, ftdi_get_error_string(ftdi)); | |
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 | { | |
141 | fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n", | |
f38b0866 | 142 | f, ftdi_get_error_string(ftdi)); |
2db3a766 UB |
143 | exit(-1); |
144 | } | |
145 | } | |
0091182e UB |
146 | else if(do_write) |
147 | { | |
148 | ftdi_eeprom_initdefaults(ftdi, "IKDA", "FTDIJTAG", "0001"); | |
82e0fc5a UB |
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", | |
153 | f, ftdi_get_error_string(ftdi)); | |
154 | } | |
155 | f = ftdi_erase_eeprom(ftdi);/* needed to determine EEPROM chip type */ | |
156 | if (ftdi_get_eeprom_value(ftdi, CHIP_TYPE, & value) <0) | |
157 | { | |
158 | fprintf(stderr, "ftdi_get_eeprom_value: %d (%s)\n", | |
159 | f, ftdi_get_error_string(ftdi)); | |
160 | } | |
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 | { | |
170 | fprintf(stderr, "Erase failed: %s", | |
171 | ftdi_get_error_string(ftdi)); | |
172 | return -2; | |
173 | } | |
174 | f = ftdi_write_eeprom(ftdi); | |
175 | { | |
176 | fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n", | |
177 | f, ftdi_get_error_string(ftdi)); | |
178 | exit(-1); | |
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 | } | |
2db3a766 UB |
188 | else |
189 | { | |
a35aa9bd | 190 | f = ftdi_read_eeprom(ftdi); |
2db3a766 UB |
191 | if (f < 0) |
192 | { | |
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); | |
a35aa9bd | 207 | for(i=0; i < size; i += 16) |
2db3a766 UB |
208 | { |
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"); | |
223 | } | |
224 | ||
a35aa9bd | 225 | f = ftdi_eeprom_decode(ftdi, 1); |
0091182e | 226 | if (f < 0) |
2db3a766 UB |
227 | { |
228 | fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n", | |
f38b0866 | 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 | } |