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