changeset 43:ea0b5aed9a9c

ftee-gen2232c reworked to support larger EEPROMs
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 22 Apr 2019 07:07:39 +0000
parents c4b9026c8875
children 6a2886f9943e
files fteeprom/ftee-gen2232c.c
diffstat 1 files changed, 64 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/fteeprom/ftee-gen2232c.c	Mon Apr 22 01:17:07 2019 +0000
+++ b/fteeprom/ftee-gen2232c.c	Mon Apr 22 07:07:39 2019 +0000
@@ -4,6 +4,9 @@
 #include <strings.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
+
+char *configfile, *serial;
 
 u_short vid = 0x0403, pid = 0x6010;
 char *manuf, *product;
@@ -12,20 +15,60 @@
 unsigned maxpower = 100;
 u_short usb_version = 0x0200;
 
-u_short eeprom[64];
-unsigned eeprom_string_ptr = 0x0B;
+u_short eeprom[128];
+u_char eeprom_chip = 0x46;
+unsigned eeprom_size, eeprom_string_ptr;
+
+process_cmdline(argc, argv)
+	char **argv;
+{
+	int c;
+	extern int optind;
 
-read_config_file(filename)
-	char *filename;
+	while ((c = getopt(argc, argv, "bB")) != EOF) {
+		switch (c) {
+		case 'b':
+			eeprom_chip = 0x56;
+			continue;
+		case 'B':
+			eeprom_chip = 0x66;
+			continue;
+		default:
+			/* error msg already printed */
+			exit(1);
+		}
+	}
+	if (argc < optind + 1 || argc > optind + 2) {
+		fprintf(stderr,
+			"usage: %s [options] config-file [serial-num]\n",
+			argv[0]);
+		exit(1);
+	}
+	configfile = argv[optind];
+	serial = argv[optind+1];
+}
+
+init_eeprom_size()
+{
+	if (eeprom_chip == 0x46) {
+		eeprom_size = 64;
+		eeprom_string_ptr = 0x0B;
+	} else {
+		eeprom_size = 128;
+		eeprom_string_ptr = 0x4B;
+	}
+}
+
+read_config_file()
 {
 	FILE *inf;
 	char linebuf[1024];
 	int lineno;
 	char *cp, *np;
 
-	inf = fopen(filename, "r");
+	inf = fopen(configfile, "r");
 	if (!inf) {
-		perror(filename);
+		perror(configfile);
 		exit(1);
 	}
 	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
@@ -33,7 +76,7 @@
 		if (!cp) {
 			fprintf(stderr,
 				"%s line %d: too long or unterminated\n",
-				filename, lineno);
+				configfile, lineno);
 			exit(1);
 		}
 		*cp = '\0';
@@ -50,7 +93,7 @@
 		if (*cp == '\0' || *cp == '#') {
 			fprintf(stderr,
 				"%s line %d: \"%s\" setting without argument\n",
-				filename, lineno, np);
+				configfile, lineno, np);
 			exit(1);
 		}
 		if (!strcmp(np, "vid"))
@@ -75,17 +118,17 @@
 			usb_version = strtoul(cp, 0, 16);
 		else {
 			fprintf(stderr, "%s line %d: unknown \"%s\" setting\n",
-				filename, lineno, np);
+				configfile, lineno, np);
 			exit(1);
 		}
 	}
 	fclose(inf);
 	if (!manuf) {
-		fprintf(stderr, "error: manuf not set in %s\n", filename);
+		fprintf(stderr, "error: manuf not set in %s\n", configfile);
 		exit(1);
 	}
 	if (!product) {
-		fprintf(stderr, "error: product not set in %s\n", filename);
+		fprintf(stderr, "error: product not set in %s\n", configfile);
 		exit(1);
 	}
 }
@@ -95,7 +138,7 @@
 {
 	unsigned longlen, startptr;
 
-	if (63 - eeprom_string_ptr < strlen(str) + 1) {
+	if (eeprom_size - 1 - eeprom_string_ptr < strlen(str) + 1) {
 		fprintf(stderr, "error: strings are too long\n");
 		exit(1);
 	}
@@ -107,8 +150,7 @@
 	return (longlen << 8) | 0x80 | (startptr << 1);
 }
 
-fill_eeprom(serial)
-	char *serial;
+fill_eeprom()
 {
 	u_char byte09;
 
@@ -130,7 +172,7 @@
 		eeprom[9] = write_string(serial);
 	else
 		eeprom[9] = 0;
-	eeprom[10] = 0x46;
+	eeprom[10] = eeprom_chip;
 }
 
 do_checksum()
@@ -138,18 +180,18 @@
 	u_short chksum = 0xAAAA;
 	unsigned n;
 
-	for (n = 0; n < 63; n++) {
+	for (n = 0; n < eeprom_size - 1; n++) {
 		chksum ^= eeprom[n];
 		chksum = (chksum << 1) | (chksum >> 15);
 	}
-	eeprom[63] = chksum;
+	eeprom[n] = chksum;
 }
 
 emit_output()
 {
 	unsigned n, col;
 
-	for (n = 0; n < 64; n++) {
+	for (n = 0; n < eeprom_size; n++) {
 		col = n & 7;
 		if (col == 0)
 			printf("%02X:", n * 2);
@@ -162,13 +204,10 @@
 main(argc, argv)
 	char **argv;
 {
-	if (argc < 2 || argc > 3) {
-		fprintf(stderr, "usage: %s config-file [serial-num]\n",
-			argv[0]);
-		exit(1);
-	}
-	read_config_file(argv[1]);
-	fill_eeprom(argv[2]);
+	process_cmdline(argc, argv);
+	read_config_file();
+	init_eeprom_size();
+	fill_eeprom();
 	do_checksum();
 	emit_output();
 	exit(0);