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