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