FreeCalypso > hg > freecalypso-hwlab
comparison fteeprom/ftee-gen2232c.c @ 31:16b625911e19
fteeprom: generalization of previous ee2232 tools
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 21 Apr 2019 04:14:00 +0000 |
parents | |
children | ae48b7d69744 |
comparison
equal
deleted
inserted
replaced
30:56780c191b58 | 31:16b625911e19 |
---|---|
1 #include <sys/types.h> | |
2 #include <ctype.h> | |
3 #include <string.h> | |
4 #include <strings.h> | |
5 #include <stdio.h> | |
6 #include <stdlib.h> | |
7 | |
8 u_short vid = 0x0403, pid = 0x6010; | |
9 char *manuf, *product; | |
10 u_char byte00 = 0x08, byte01 = 0x08; | |
11 u_char byte08 = 0x80, byte0A = 0x00; | |
12 unsigned maxpower = 100; | |
13 u_short usb_version = 0x0200; | |
14 | |
15 u_short eeprom[64]; | |
16 unsigned eeprom_string_ptr = 0x0B; | |
17 | |
18 read_config_file(filename) | |
19 char *filename; | |
20 { | |
21 FILE *inf; | |
22 char linebuf[1024]; | |
23 int lineno; | |
24 char *cp, *np; | |
25 | |
26 inf = fopen(filename, "r"); | |
27 if (!inf) { | |
28 perror(filename); | |
29 exit(1); | |
30 } | |
31 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) { | |
32 cp = index(linebuf, '\n'); | |
33 if (!cp) { | |
34 fprintf(stderr, | |
35 "%s line %d: too long or unterminated\n", | |
36 filename, lineno); | |
37 exit(1); | |
38 } | |
39 *cp = '\0'; | |
40 for (cp = linebuf; isspace(*cp); cp++) | |
41 ; | |
42 if (*cp == '\0' || *cp == '#') | |
43 continue; | |
44 for (np = cp; *cp && !isspace(*cp); cp++) | |
45 ; | |
46 if (*cp) | |
47 *cp++ = '\0'; | |
48 while (isspace(*cp)) | |
49 cp++; | |
50 if (*cp == '\0' || *cp == '#') { | |
51 fprintf(stderr, | |
52 "%s line %d: \"%s\" setting without argument\n", | |
53 filename, lineno, np); | |
54 exit(1); | |
55 } | |
56 if (!strcmp(np, "vid")) | |
57 vid = strtoul(cp, 0, 16); | |
58 else if (!strcmp(np, "pid")) | |
59 pid = strtoul(cp, 0, 16); | |
60 else if (!strcmp(np, "manuf")) | |
61 manuf = strdup(cp); | |
62 else if (!strcmp(np, "product")) | |
63 product = strdup(cp); | |
64 else if (!strcmp(np, "byte00")) | |
65 byte00 = strtoul(cp, 0, 16); | |
66 else if (!strcmp(np, "byte01")) | |
67 byte01 = strtoul(cp, 0, 16); | |
68 else if (!strcmp(np, "byte08")) | |
69 byte08 = strtoul(cp, 0, 16); | |
70 else if (!strcmp(np, "byte0A")) | |
71 byte0A = strtoul(cp, 0, 16); | |
72 else if (!strcmp(np, "maxpower")) | |
73 maxpower = strtoul(cp, 0, 10); | |
74 else if (!strcmp(np, "usbver")) | |
75 usb_version = strtoul(cp, 0, 16); | |
76 else { | |
77 fprintf(stderr, "%s line %d: unknown \"%s\" setting\n", | |
78 filename, lineno, np); | |
79 exit(1); | |
80 } | |
81 } | |
82 fclose(inf); | |
83 if (!manuf) { | |
84 fprintf(stderr, "error: manuf not set in %s\n", filename); | |
85 exit(1); | |
86 } | |
87 if (!product) { | |
88 fprintf(stderr, "error: product not set in %s\n", filename); | |
89 exit(1); | |
90 } | |
91 } | |
92 | |
93 write_string(str) | |
94 char *str; | |
95 { | |
96 unsigned longlen, startptr; | |
97 | |
98 longlen = strlen(str) * 2 + 2; | |
99 startptr = eeprom_string_ptr; | |
100 eeprom[eeprom_string_ptr++] = 0x0300 | longlen; | |
101 while (*str) | |
102 eeprom[eeprom_string_ptr++] = *str++; | |
103 return (longlen << 8) | 0x80 | (startptr << 1); | |
104 } | |
105 | |
106 fill_eeprom(serial) | |
107 char *serial; | |
108 { | |
109 u_char byte09; | |
110 | |
111 if (serial) | |
112 byte0A |= 0x08; | |
113 else | |
114 byte0A &= 0xF7; | |
115 byte09 = maxpower / 2; | |
116 eeprom[0] = (byte01 << 8) | byte00; | |
117 eeprom[1] = vid; | |
118 eeprom[2] = pid; | |
119 eeprom[3] = 0x0500; | |
120 eeprom[4] = (byte09 << 8) | byte08; | |
121 eeprom[5] = byte0A; | |
122 eeprom[6] = usb_version; | |
123 eeprom[7] = write_string(manuf); | |
124 eeprom[8] = write_string(product); | |
125 if (serial) | |
126 eeprom[9] = write_string(serial); | |
127 else | |
128 eeprom[9] = 0; | |
129 eeprom[10] = 0x46; | |
130 } | |
131 | |
132 do_checksum() | |
133 { | |
134 u_short chksum = 0xAAAA; | |
135 unsigned n; | |
136 | |
137 for (n = 0; n < 63; n++) { | |
138 chksum ^= eeprom[n]; | |
139 chksum = (chksum << 1) | (chksum >> 15); | |
140 } | |
141 eeprom[63] = chksum; | |
142 } | |
143 | |
144 emit_output() | |
145 { | |
146 unsigned n, col; | |
147 | |
148 for (n = 0; n < 64; n++) { | |
149 col = n & 7; | |
150 if (col == 0) | |
151 printf("%02X:", n * 2); | |
152 printf(" %04X", eeprom[n]); | |
153 if (col == 7) | |
154 putchar('\n'); | |
155 } | |
156 } | |
157 | |
158 main(argc, argv) | |
159 char **argv; | |
160 { | |
161 if (argc < 2 || argc > 3) { | |
162 fprintf(stderr, "usage: %s config-file [serial-num]\n", | |
163 argv[0]); | |
164 exit(1); | |
165 } | |
166 read_config_file(argv[1]); | |
167 fill_eeprom(argv[2]); | |
168 do_checksum(); | |
169 emit_output(); | |
170 exit(0); | |
171 } |