Provide internal eeprom structure buffer with internal byte buffer Purge external...
[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;
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 size;
27
28     if ((ftdi = ftdi_new()) == 0)
29     {
30        fprintf(stderr, "Failed to allocate ftdi structure :%s \n", 
31                    ftdi_get_error_string(ftdi));
32         return EXIT_FAILURE;
33     }
34
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     // Select first interface
76     ftdi_set_interface(ftdi, INTERFACE_ANY);
77
78     // Open device
79     f = ftdi_usb_open_desc(ftdi, vid, pid, desc, serial);
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", 
89                 f, ftdi_get_error_string(ftdi));
90
91         exit(-1);
92     }
93
94     if (erase)
95     {
96         f = ftdi_erase_eeprom(ftdi);
97         if (f < 0)
98         {
99             fprintf(stderr, "Erase failed: %s", 
100                     ftdi_get_error_string(ftdi));
101             return -2;
102         }
103         if (ftdi->eeprom->chip == -1)
104             fprintf(stderr, "No EEPROM\n");
105         else if (ftdi->eeprom->chip == 0)
106             fprintf(stderr, "Internal EEPROM\n");
107         else
108             fprintf(stderr, "Found 93x%02x\n",ftdi->eeprom->chip);
109         return 0;
110     }        
111
112     if(use_defaults)
113     {
114         ftdi_eeprom_initdefaults(ftdi);
115         ftdi->eeprom->manufacturer="IKDA";
116         ftdi->eeprom->product="CPS-CONN";
117         ftdi->eeprom->serial="0001";
118         ftdi->eeprom->chip= (large_chip)?0x66:0;
119         f=(ftdi_eeprom_build(ftdi));
120         if (f < 0)
121         {
122             fprintf(stderr, "ftdi_eeprom_build: %d (%s)\n", 
123                     f, ftdi_get_error_string(ftdi));
124             exit(-1);
125         }
126     }
127     else
128     {
129         f = ftdi_read_eeprom(ftdi);
130         if (f < 0)
131         {
132             fprintf(stderr, "ftdi_read_eeprom: %d (%s)\n", 
133                     f, ftdi_get_error_string(ftdi));
134             exit(-1);
135         }
136     }
137     fprintf(stderr, "Chip type %d ftdi_eeprom_size: %d\n", ftdi->type, ftdi->eeprom->size);
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)
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
161     f = ftdi_eeprom_decode(ftdi, 1);
162     {
163         fprintf(stderr, "ftdi_eeprom_decode: %d (%s)\n", 
164                 f, ftdi_get_error_string(ftdi));
165         exit(-1);
166     }
167         
168
169     ftdi_usb_close(ftdi);
170     ftdi_free(ftdi);
171 }