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