comparison fteeprom/ftee-gen2232c.c @ 73:201503e4be18

ftee-gen2232c: add ftdi-chip and eeprom settings
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 25 Sep 2023 23:23:32 +0000
parents b2c891299e83
children 7ff9b4857aaf
comparison
equal deleted inserted replaced
72:6dc3aa777fd6 73:201503e4be18
1 /*
2 * This program constucts a configuration EEPROM image for FT2232C/D chips
3 * based on a configuration source file giving various settings.
4 */
5
1 #include <sys/types.h> 6 #include <sys/types.h>
2 #include <ctype.h> 7 #include <ctype.h>
3 #include <string.h> 8 #include <string.h>
4 #include <strings.h> 9 #include <strings.h>
5 #include <stdio.h> 10 #include <stdio.h>
16 u_char byte08 = 0x80, byte0A = 0x00; 21 u_char byte08 = 0x80, byte0A = 0x00;
17 unsigned maxpower = 100; 22 unsigned maxpower = 100;
18 u_short usb_version = 0x0200; 23 u_short usb_version = 0x0200;
19 24
20 u_short eeprom[128]; 25 u_short eeprom[128];
21 u_char eeprom_chip = 0x46; 26 u_char eeprom_chip = 0x46, eeprom_chip_cmdline;
22 unsigned eeprom_size, eeprom_string_ptr; 27 unsigned eeprom_size, eeprom_string_ptr;
23 28
29 static void
24 process_cmdline(argc, argv) 30 process_cmdline(argc, argv)
25 char **argv; 31 char **argv;
26 { 32 {
27 int c; 33 int c;
28 extern int optind; 34 extern int optind;
29 35
30 while ((c = getopt(argc, argv, "bB")) != EOF) { 36 while ((c = getopt(argc, argv, "bBs")) != EOF) {
31 switch (c) { 37 switch (c) {
32 case 'b': 38 case 'b':
33 eeprom_chip = 0x56; 39 eeprom_chip = eeprom_chip_cmdline = 0x56;
34 continue; 40 continue;
35 case 'B': 41 case 'B':
36 eeprom_chip = 0x66; 42 eeprom_chip = eeprom_chip_cmdline = 0x66;
43 continue;
44 case 's':
45 eeprom_chip = eeprom_chip_cmdline = 0x46;
37 continue; 46 continue;
38 default: 47 default:
39 /* error msg already printed */ 48 /* error msg already printed */
40 exit(1); 49 exit(1);
41 } 50 }
48 } 57 }
49 configfile = argv[optind]; 58 configfile = argv[optind];
50 serial = argv[optind+1]; 59 serial = argv[optind+1];
51 } 60 }
52 61
53 init_eeprom_size() 62 static void
54 { 63 ftdi_chip_setting(arg, filename_for_errs, lineno)
55 if (eeprom_chip == 0x46) { 64 char *arg, *filename_for_errs;
56 eeprom_size = 64; 65 {
57 eeprom_string_ptr = 0x0B; 66 if (!strcasecmp(arg, "FT2232C"))
58 } else { 67 return;
59 eeprom_size = 128; 68 if (!strcasecmp(arg, "FT2232D"))
60 eeprom_string_ptr = 0x4B; 69 return;
61 } 70 if (!strcasecmp(arg, "FT2232x"))
62 } 71 return;
63 72 fprintf(stderr, "%s line %d: config is for wrong FTDI chip\n",
73 filename_for_errs, lineno);
74 exit(1);
75 }
76
77 static void
78 eeprom_setting(arg, filename_for_errs, lineno)
79 char *arg, *filename_for_errs;
80 {
81 if (!strcasecmp(arg, "93C46"))
82 eeprom_chip = 0x46;
83 else if (!strcasecmp(arg, "93C56"))
84 eeprom_chip = 0x56;
85 else if (!strcasecmp(arg, "93C66"))
86 eeprom_chip = 0x66;
87 else {
88 fprintf(stderr, "%s line %d: invalid eeprom setting\n",
89 filename_for_errs, lineno);
90 exit(1);
91 }
92 if (eeprom_chip_cmdline && eeprom_chip_cmdline != eeprom_chip) {
93 fprintf(stderr,
94 "%s line %d: eeprom setting disagrees with command line option\n",
95 filename_for_errs, lineno);
96 exit(1);
97 }
98 }
99
100 static void
64 read_config_file() 101 read_config_file()
65 { 102 {
66 FILE *inf; 103 FILE *inf;
67 char linebuf[1024]; 104 char linebuf[1024];
68 int lineno; 105 int lineno;
116 byte0A = strtoul(cp, 0, 16); 153 byte0A = strtoul(cp, 0, 16);
117 else if (!strcmp(np, "maxpower")) 154 else if (!strcmp(np, "maxpower"))
118 maxpower = strtoul(cp, 0, 10); 155 maxpower = strtoul(cp, 0, 10);
119 else if (!strcmp(np, "usbver")) 156 else if (!strcmp(np, "usbver"))
120 usb_version = strtoul(cp, 0, 16); 157 usb_version = strtoul(cp, 0, 16);
158 else if (!strcmp(np, "ftdi-chip"))
159 ftdi_chip_setting(cp, configfile, lineno);
160 else if (!strcmp(np, "eeprom"))
161 eeprom_setting(cp, configfile, lineno);
121 else { 162 else {
122 fprintf(stderr, "%s line %d: unknown \"%s\" setting\n", 163 fprintf(stderr, "%s line %d: unknown \"%s\" setting\n",
123 configfile, lineno, np); 164 configfile, lineno, np);
124 exit(1); 165 exit(1);
125 } 166 }
133 fprintf(stderr, "error: product not set in %s\n", configfile); 174 fprintf(stderr, "error: product not set in %s\n", configfile);
134 exit(1); 175 exit(1);
135 } 176 }
136 } 177 }
137 178
179 static void
180 init_eeprom_size()
181 {
182 if (eeprom_chip == 0x46) {
183 eeprom_size = 64;
184 eeprom_string_ptr = 0x0B;
185 } else {
186 eeprom_size = 128;
187 eeprom_string_ptr = 0x4B;
188 }
189 }
190
191 static int
138 write_string(str) 192 write_string(str)
139 char *str; 193 char *str;
140 { 194 {
141 unsigned longlen, startptr; 195 unsigned longlen, startptr;
142 196
150 while (*str) 204 while (*str)
151 eeprom[eeprom_string_ptr++] = *str++; 205 eeprom[eeprom_string_ptr++] = *str++;
152 return (longlen << 8) | 0x80 | (startptr << 1); 206 return (longlen << 8) | 0x80 | (startptr << 1);
153 } 207 }
154 208
209 static void
155 fill_eeprom() 210 fill_eeprom()
156 { 211 {
157 u_char byte09; 212 u_char byte09;
158 213
159 if (serial) 214 if (serial)
175 else 230 else
176 eeprom[9] = 0; 231 eeprom[9] = 0;
177 eeprom[10] = eeprom_chip; 232 eeprom[10] = eeprom_chip;
178 } 233 }
179 234
235 static void
180 do_checksum() 236 do_checksum()
181 { 237 {
182 u_short chksum = 0xAAAA; 238 u_short chksum = 0xAAAA;
183 unsigned n; 239 unsigned n;
184 240
187 chksum = (chksum << 1) | (chksum >> 15); 243 chksum = (chksum << 1) | (chksum >> 15);
188 } 244 }
189 eeprom[n] = chksum; 245 eeprom[n] = chksum;
190 } 246 }
191 247
248 static void
192 emit_output() 249 emit_output()
193 { 250 {
194 unsigned n, col; 251 unsigned n, col;
195 252
196 for (n = 0; n < eeprom_size; n++) { 253 for (n = 0; n < eeprom_size; n++) {