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; |
2db3a766 UB |
17 | struct ftdi_eeprom eeprom; |
18 | unsigned char buf[2048]; | |
19 | int size; | |
20 | int f, i, j; | |
21 | int vid = 0x0403; | |
22 | int pid = 0x6010; | |
23 | char const *desc = 0; | |
24 | char const *serial = 0; | |
25 | int erase = 0; | |
4fb2ebbb | 26 | int use_defaults = 0; |
2db3a766 UB |
27 | int large_chip = 0; |
28 | ||
f38b0866 UB |
29 | if ((ftdi = ftdi_new()) == 0) |
30 | { | |
31 | fprintf(stderr,"Failed to allocate ftdi structure\n"); | |
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 | ||
75 | // Init | |
f38b0866 | 76 | if (ftdi_init(ftdi) < 0) |
2db3a766 UB |
77 | { |
78 | fprintf(stderr, "ftdi_init failed\n"); | |
79 | return EXIT_FAILURE; | |
80 | } | |
81 | ||
82 | // Select first interface | |
f38b0866 | 83 | ftdi_set_interface(ftdi, INTERFACE_ANY); |
2db3a766 UB |
84 | |
85 | // Open device | |
f38b0866 | 86 | f = ftdi_usb_open_desc(ftdi, vid, pid, desc, serial); |
2db3a766 UB |
87 | if (f < 0) |
88 | { | |
89 | fprintf(stderr, "Device VID 0x%04x PID 0x%04x", vid, pid); | |
90 | if(desc) | |
91 | fprintf(stderr, " Desc %s", desc); | |
92 | if(serial) | |
93 | fprintf(stderr, " Serial %s", serial); | |
94 | fprintf(stderr, "\n"); | |
95 | fprintf(stderr, "unable to open ftdi device: %d (%s)\n", | |
f38b0866 | 96 | f, ftdi_get_error_string(ftdi)); |
2db3a766 UB |
97 | |
98 | exit(-1); | |
99 | } | |
100 | ||
101 | if (erase) | |
102 | { | |
f38b0866 UB |
103 | ftdi_eeprom_setsize(ftdi, &eeprom, 2048); |
104 | f = ftdi_erase_eeprom(ftdi); | |
2db3a766 UB |
105 | if (f < 0) |
106 | { | |
107 | fprintf(stderr, "Erase failed: %s", | |
f38b0866 | 108 | ftdi_get_error_string(ftdi)); |
2db3a766 UB |
109 | return -2; |
110 | } | |
f38b0866 | 111 | if (ftdi->eeprom->chip == -1) |
2db3a766 | 112 | fprintf(stderr, "No EEPROM\n"); |
f38b0866 | 113 | else if (ftdi->eeprom->chip == 0) |
2db3a766 UB |
114 | fprintf(stderr, "Internal EEPROM\n"); |
115 | else | |
f38b0866 | 116 | fprintf(stderr, "Found 93x%02x\n",ftdi->eeprom->chip); |
2db3a766 UB |
117 | return 0; |
118 | } | |
119 | ||
120 | size = 2048; | |
121 | memset(buf,0, size); | |
f38b0866 | 122 | ftdi->eeprom = &eeprom; |
2db3a766 UB |
123 | if(use_defaults) |
124 | { | |
f38b0866 UB |
125 | ftdi_eeprom_initdefaults(ftdi); |
126 | ftdi->eeprom->manufacturer="IKDA"; | |
127 | ftdi->eeprom->product="CPS-CONN"; | |
128 | ftdi->eeprom->serial="0001"; | |
129 | ftdi->eeprom->chip= large_chip; | |
130 | ftdi->eeprom->cbus_function[0]= CBUS_BB_RD; | |
131 | ftdi->eeprom->cbus_function[1]= CBUS_CLK48; | |
132 | ftdi->eeprom->cbus_function[2]= CBUS_IOMODE; | |
133 | ftdi->eeprom->cbus_function[3]= CBUS_BB; | |
134 | ftdi->eeprom->cbus_function[4]= CBUS_CLK6; | |
135 | f=(ftdi_eeprom_build(ftdi, buf)); | |
2db3a766 UB |
136 | if (f < 0) |
137 | { | |
138 | fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n", | |
f38b0866 | 139 | f, ftdi_get_error_string(ftdi)); |
2db3a766 UB |
140 | exit(-1); |
141 | } | |
142 | } | |
143 | else | |
144 | { | |
f38b0866 | 145 | f = ftdi_read_eeprom(ftdi, buf); |
2db3a766 UB |
146 | if (f < 0) |
147 | { | |
148 | fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n", | |
f38b0866 | 149 | f, ftdi_get_error_string(ftdi)); |
2db3a766 UB |
150 | exit(-1); |
151 | } | |
152 | } | |
f38b0866 UB |
153 | fprintf(stderr, "Chip type %d ftdi_eeprom_size: %d\n", ftdi->type, ftdi->eeprom->size); |
154 | for(i=0; i < ftdi->eeprom->size; i += 16) | |
2db3a766 UB |
155 | { |
156 | fprintf(stdout,"0x%03x:", i); | |
157 | ||
158 | for (j = 0; j< 8; j++) | |
159 | fprintf(stdout," %02x", buf[i+j]); | |
160 | fprintf(stdout," "); | |
161 | for (; j< 16; j++) | |
162 | fprintf(stdout," %02x", buf[i+j]); | |
163 | fprintf(stdout," "); | |
164 | for (j = 0; j< 8; j++) | |
165 | fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.'); | |
166 | fprintf(stdout," "); | |
167 | for (; j< 16; j++) | |
168 | fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.'); | |
169 | fprintf(stdout,"\n"); | |
170 | } | |
171 | ||
f38b0866 | 172 | f = ftdi_eeprom_decode(ftdi,buf, size, 1); |
2db3a766 UB |
173 | { |
174 | fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n", | |
f38b0866 | 175 | f, ftdi_get_error_string(ftdi)); |
2db3a766 UB |
176 | exit(-1); |
177 | } | |
178 | ||
179 | ||
f38b0866 UB |
180 | ftdi_usb_close(ftdi); |
181 | ftdi_free(ftdi); | |
2db3a766 | 182 | } |