diff fteeprom/fteeprom-prog.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 c4b9026c8875
line wrap: on
line diff
--- /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);
+}