changeset 31:16b625911e19

fteeprom: generalization of previous ee2232 tools
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 Apr 2019 04:14:00 +0000
parents 56780c191b58
children 60b8cf977c3f
files .hgignore fteeprom/Makefile fteeprom/ftee-gen2232c.c fteeprom/fteeprom-prog.c fteeprom/fteeprom-read.c
diffstat 5 files changed, 370 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Apr 21 01:31:54 2019 +0000
+++ b/.hgignore	Sun Apr 21 04:14:00 2019 +0000
@@ -6,5 +6,9 @@
 ^ee2232/ee2232-prog$
 ^ee2232/ee2232-read$
 
+^fteeprom/ftee-gen2232c$
+^fteeprom/fteeprom-prog$
+^fteeprom/fteeprom-read$
+
 ^lcdtest/lcdphone$
 ^lcdtest/lcdtest$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fteeprom/Makefile	Sun Apr 21 04:14:00 2019 +0000
@@ -0,0 +1,22 @@
+CC=	gcc
+CFLAGS=	-O2
+PROGS=	ftee-gen2232c fteeprom-prog fteeprom-read
+INSTBIN=/opt/freecalypso/bin
+
+all:	${PROGS}
+
+ftee-gen2232c:	ftee-gen2232c.c
+	${CC} ${CFLAGS} -o $@ $@.c
+
+fteeprom-prog:	fteeprom-prog.c
+	${CC} ${CFLAGS} -o $@ $@.c -lftdi
+
+fteeprom-read:	fteeprom-read.c
+	${CC} ${CFLAGS} -o $@ $@.c -lftdi
+
+install:
+	mkdir -p ${INSTBIN}
+	install -c ${PROGS} ${INSTBIN}
+
+clean:
+	rm -f ${PROGS} *.o *errs *.out
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fteeprom/ftee-gen2232c.c	Sun Apr 21 04:14:00 2019 +0000
@@ -0,0 +1,171 @@
+#include <sys/types.h>
+#include <ctype.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+u_short vid = 0x0403, pid = 0x6010;
+char *manuf, *product;
+u_char byte00 = 0x08, byte01 = 0x08;
+u_char byte08 = 0x80, byte0A = 0x00;
+unsigned maxpower = 100;
+u_short usb_version = 0x0200;
+
+u_short eeprom[64];
+unsigned eeprom_string_ptr = 0x0B;
+
+read_config_file(filename)
+	char *filename;
+{
+	FILE *inf;
+	char linebuf[1024];
+	int lineno;
+	char *cp, *np;
+
+	inf = fopen(filename, "r");
+	if (!inf) {
+		perror(filename);
+		exit(1);
+	}
+	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
+		cp = index(linebuf, '\n');
+		if (!cp) {
+			fprintf(stderr,
+				"%s line %d: too long or unterminated\n",
+				filename, lineno);
+			exit(1);
+		}
+		*cp = '\0';
+		for (cp = linebuf; isspace(*cp); cp++)
+			;
+		if (*cp == '\0' || *cp == '#')
+			continue;
+		for (np = cp; *cp && !isspace(*cp); cp++)
+			;
+		if (*cp)
+			*cp++ = '\0';
+		while (isspace(*cp))
+			cp++;
+		if (*cp == '\0' || *cp == '#') {
+			fprintf(stderr,
+				"%s line %d: \"%s\" setting without argument\n",
+				filename, lineno, np);
+			exit(1);
+		}
+		if (!strcmp(np, "vid"))
+			vid = strtoul(cp, 0, 16);
+		else if (!strcmp(np, "pid"))
+			pid = strtoul(cp, 0, 16);
+		else if (!strcmp(np, "manuf"))
+			manuf = strdup(cp);
+		else if (!strcmp(np, "product"))
+			product = strdup(cp);
+		else if (!strcmp(np, "byte00"))
+			byte00 = strtoul(cp, 0, 16);
+		else if (!strcmp(np, "byte01"))
+			byte01 = strtoul(cp, 0, 16);
+		else if (!strcmp(np, "byte08"))
+			byte08 = strtoul(cp, 0, 16);
+		else if (!strcmp(np, "byte0A"))
+			byte0A = strtoul(cp, 0, 16);
+		else if (!strcmp(np, "maxpower"))
+			maxpower = strtoul(cp, 0, 10);
+		else if (!strcmp(np, "usbver"))
+			usb_version = strtoul(cp, 0, 16);
+		else {
+			fprintf(stderr, "%s line %d: unknown \"%s\" setting\n",
+				filename, lineno, np);
+			exit(1);
+		}
+	}
+	fclose(inf);
+	if (!manuf) {
+		fprintf(stderr, "error: manuf not set in %s\n", filename);
+		exit(1);
+	}
+	if (!product) {
+		fprintf(stderr, "error: product not set in %s\n", filename);
+		exit(1);
+	}
+}
+
+write_string(str)
+	char *str;
+{
+	unsigned longlen, startptr;
+
+	longlen = strlen(str) * 2 + 2;
+	startptr = eeprom_string_ptr;
+	eeprom[eeprom_string_ptr++] = 0x0300 | longlen;
+	while (*str)
+		eeprom[eeprom_string_ptr++] = *str++;
+	return (longlen << 8) | 0x80 | (startptr << 1);
+}
+
+fill_eeprom(serial)
+	char *serial;
+{
+	u_char byte09;
+
+	if (serial)
+		byte0A |= 0x08;
+	else
+		byte0A &= 0xF7;
+	byte09 = maxpower / 2;
+	eeprom[0] = (byte01 << 8) | byte00;
+	eeprom[1] = vid;
+	eeprom[2] = pid;
+	eeprom[3] = 0x0500;
+	eeprom[4] = (byte09 << 8) | byte08;
+	eeprom[5] = byte0A;
+	eeprom[6] = usb_version;
+	eeprom[7] = write_string(manuf);
+	eeprom[8] = write_string(product);
+	if (serial)
+		eeprom[9] = write_string(serial);
+	else
+		eeprom[9] = 0;
+	eeprom[10] = 0x46;
+}
+
+do_checksum()
+{
+	u_short chksum = 0xAAAA;
+	unsigned n;
+
+	for (n = 0; n < 63; n++) {
+		chksum ^= eeprom[n];
+		chksum = (chksum << 1) | (chksum >> 15);
+	}
+	eeprom[63] = chksum;
+}
+
+emit_output()
+{
+	unsigned n, col;
+
+	for (n = 0; n < 64; n++) {
+		col = n & 7;
+		if (col == 0)
+			printf("%02X:", n * 2);
+		printf(" %04X", eeprom[n]);
+		if (col == 7)
+			putchar('\n');
+	}
+}
+
+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]);
+	do_checksum();
+	emit_output();
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fteeprom/fteeprom-prog.c	Sun Apr 21 04:14:00 2019 +0000
@@ -0,0 +1,97 @@
+#include <sys/types.h>
+#include <ctype.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <ftdi.h>
+
+char *device_selector;
+unsigned eeprom_size = 64;
+u_short eeprom[256];
+int erase;
+
+process_cmdline(argc, argv)
+	char **argv;
+{
+	int c;
+	extern int optind;
+	extern char *optarg;
+
+	while ((c = getopt(argc, argv, "et:")) != EOF) {
+		switch (c) {
+		case 'e':
+			erase = 1;
+			continue;
+		case 't':
+			if (!strcmp(optarg, "46"))
+				eeprom_size = 64;
+			else if (!strcmp(optarg, "56"))
+				eeprom_size = 128;
+			else if (!strcmp(optarg, "66"))
+				eeprom_size = 256;
+			else {
+				fprintf(stderr,
+				"error: -t option invalid value \"%s\"\n",
+					optarg);
+				exit(1);
+			}
+			continue;
+		default:
+			/* error msg already printed */
+			exit(1);
+		}
+	}
+	if (argc != optind + 1) {
+		fprintf(stderr, "usage: %s [options] device-selector\n",
+			argv[0]);
+		exit(1);
+	}
+	device_selector = argv[optind];
+}
+
+read_eeprom_from_stdin()
+{
+	unsigned n, off;
+	int rc;
+
+	for (n = 0; n < eeprom_size; n += 8) {
+		rc = scanf("%x: %hx %hx %hx %hx %hx %hx %hx %hx", &off,
+				eeprom + n, eeprom + n + 1, eeprom + n + 2,
+				eeprom + n + 3, eeprom + n + 4, eeprom + n + 5,
+				eeprom + n + 6, eeprom + n + 7);
+		if (rc != 9 || off != n * 2) {
+			fprintf(stderr,
+				"ee2232-prog error: invalid input on stdin\n");
+			exit(1);
+		}
+	}
+}
+
+main(argc, argv)
+	char **argv;
+{
+	struct ftdi_context ftdi;
+	unsigned n;
+
+	process_cmdline(argc, argv);
+	if (erase)
+		memset(eeprom, 0xFF, eeprom_size * 2);
+	else
+		read_eeprom_from_stdin();
+	ftdi_init(&ftdi);
+	if (ftdi_usb_open_string(&ftdi, device_selector) < 0) {
+		fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str);
+		exit(1);
+	}
+	for (n = 0; n < eeprom_size; n++) {
+		if (ftdi_write_eeprom_location(&ftdi, n, eeprom[n]) < 0) {
+			fprintf(stderr, "EEPROM write error: %s\n",
+				ftdi.error_str);
+			exit(1);
+		}
+	}
+	ftdi_usb_close(&ftdi);
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fteeprom/fteeprom-read.c	Sun Apr 21 04:14:00 2019 +0000
@@ -0,0 +1,76 @@
+#include <sys/types.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <ftdi.h>
+
+char *device_selector;
+unsigned eeprom_size = 64;
+
+process_cmdline(argc, argv)
+	char **argv;
+{
+	int c;
+	extern int optind;
+	extern char *optarg;
+
+	while ((c = getopt(argc, argv, "t:")) != EOF) {
+		switch (c) {
+		case 't':
+			if (!strcmp(optarg, "46"))
+				eeprom_size = 64;
+			else if (!strcmp(optarg, "56"))
+				eeprom_size = 128;
+			else if (!strcmp(optarg, "66"))
+				eeprom_size = 256;
+			else {
+				fprintf(stderr,
+				"error: -t option invalid value \"%s\"\n",
+					optarg);
+				exit(1);
+			}
+			continue;
+		default:
+			/* error msg already printed */
+			exit(1);
+		}
+	}
+	if (argc != optind + 1) {
+		fprintf(stderr, "usage: %s [options] device-selector\n",
+			argv[0]);
+		exit(1);
+	}
+	device_selector = argv[optind];
+}
+
+main(argc, argv)
+	char **argv;
+{
+	struct ftdi_context ftdi;
+	u_short word;
+	unsigned n, col;
+
+	process_cmdline(argc, argv);
+	ftdi_init(&ftdi);
+	if (ftdi_usb_open_string(&ftdi, device_selector) < 0) {
+		fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str);
+		exit(1);
+	}
+	for (n = 0; n < eeprom_size; n++) {
+		if (ftdi_read_eeprom_location(&ftdi, n, &word) < 0) {
+			fprintf(stderr, "EEPROM read error: %s\n",
+				ftdi.error_str);
+			exit(1);
+		}
+		col = n & 7;
+		if (col == 0)
+			printf("%02X:", n * 2);
+		printf(" %04X", word);
+		if (col == 7)
+			putchar('\n');
+	}
+	ftdi_usb_close(&ftdi);
+	exit(0);
+}