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 | { | |
74e8e79d | 114 | ftdi_eeprom_initdefaults(ftdi, "IKDA", "CPS-CONN", "0001"); |
a35aa9bd UB |
115 | ftdi->eeprom->chip= (large_chip)?0x66:0; |
116 | f=(ftdi_eeprom_build(ftdi)); | |
2db3a766 UB |
117 | if (f < 0) |
118 | { | |
119 | fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n", | |
f38b0866 | 120 | f, ftdi_get_error_string(ftdi)); |
2db3a766 UB |
121 | exit(-1); |
122 | } | |
123 | } | |
124 | else | |
125 | { | |
a35aa9bd | 126 | f = ftdi_read_eeprom(ftdi); |
2db3a766 UB |
127 | if (f < 0) |
128 | { | |
129 | fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n", | |
f38b0866 | 130 | f, ftdi_get_error_string(ftdi)); |
2db3a766 UB |
131 | exit(-1); |
132 | } | |
133 | } | |
f38b0866 | 134 | fprintf(stderr, "Chip type %d ftdi_eeprom_size: %d\n", ftdi->type, ftdi->eeprom->size); |
a35aa9bd UB |
135 | buf = ftdi->eeprom->buf; |
136 | if (ftdi->type == TYPE_R) | |
137 | size = 0xa0; | |
138 | else | |
139 | size = ftdi->eeprom->size; | |
140 | for(i=0; i < size; i += 16) | |
2db3a766 UB |
141 | { |
142 | fprintf(stdout,"0x%03x:", i); | |
143 | ||
144 | for (j = 0; j< 8; j++) | |
145 | fprintf(stdout," %02x", buf[i+j]); | |
146 | fprintf(stdout," "); | |
147 | for (; j< 16; j++) | |
148 | fprintf(stdout," %02x", buf[i+j]); | |
149 | fprintf(stdout," "); | |
150 | for (j = 0; j< 8; j++) | |
151 | fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.'); | |
152 | fprintf(stdout," "); | |
153 | for (; j< 16; j++) | |
154 | fprintf(stdout,"%c", isprint(buf[i+j])?buf[i+j]:'.'); | |
155 | fprintf(stdout,"\n"); | |
156 | } | |
157 | ||
a35aa9bd | 158 | f = ftdi_eeprom_decode(ftdi, 1); |
2db3a766 UB |
159 | { |
160 | fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n", | |
f38b0866 | 161 | f, ftdi_get_error_string(ftdi)); |
2db3a766 UB |
162 | exit(-1); |
163 | } | |
164 | ||
165 | ||
f38b0866 UB |
166 | ftdi_usb_close(ftdi); |
167 | ftdi_free(ftdi); | |
2db3a766 | 168 | } |