changeset 31:530ec3792de8

fc-duart28-conf: implement set command
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 09 Sep 2023 19:40:58 +0000
parents 8de3891460db
children bb1d0956b0a8
files duart28/Makefile duart28/eeprom_wr.c duart28/main.c
diffstat 3 files changed, 72 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/duart28/Makefile	Sat Sep 09 19:39:15 2023 +0000
+++ b/duart28/Makefile	Sat Sep 09 19:40:58 2023 +0000
@@ -1,7 +1,7 @@
 CC=	gcc
 CFLAGS=	-O2
 PROG=	fc-duart28-conf
-OBJS=	eeprom_rd.o find_usb.o main.o
+OBJS=	eeprom_rd.o eeprom_wr.o find_usb.o main.o
 LIBS=	../libftmini/libftmini.a ../libuwrap/libuwrap.a
 
 INSTALL_PREFIX=	/opt/freecalypso
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/duart28/eeprom_wr.c	Sat Sep 09 19:40:58 2023 +0000
@@ -0,0 +1,49 @@
+/*
+ * This module implements the final step of modifying the EEPROM
+ * for the desired configuration.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <usb.h>
+#include "../libftmini/eeprom_func.h"
+
+extern u_short eeprom[64];
+
+static void
+recompute_checksum()
+{
+	u_short chksum = 0xAAAA;
+	unsigned n;
+
+	for (n = 0; n < 63; n++) {
+		chksum ^= eeprom[n];
+		chksum = (chksum << 1) | (chksum >> 15);
+	}
+	eeprom[63] = chksum;
+}
+
+void
+update_eeprom(usbh, newconf)
+	usb_dev_handle *usbh;
+{
+	switch (newconf) {
+	case 'C':
+		eeprom[2] = 0x7152;
+		break;
+	case 'S':
+		eeprom[2] = 0x6010;
+		break;
+	default:
+		fprintf(stderr, "BUG: wrong argument to update_eeprom()\n");
+		abort();
+	}
+	eeprom[31] = newconf;
+	recompute_checksum();
+	ftmini_write_eeprom_loc(usbh, 2, eeprom[2]);
+	ftmini_write_eeprom_loc(usbh, 31, eeprom[31]);
+	ftmini_write_eeprom_loc(usbh, 63, eeprom[63]);
+}
--- a/duart28/main.c	Sat Sep 09 19:39:15 2023 +0000
+++ b/duart28/main.c	Sat Sep 09 19:40:58 2023 +0000
@@ -41,8 +41,28 @@
 
 oper_program(newconf)
 {
-	fprintf(stderr, "error: set command not yet implemented\n");
-	exit(1);
+	struct usb_device *dev;
+	usb_dev_handle *usbh;
+	int prev;
+
+	dev = find_duart28_usbdev();
+	if (!dev) {
+		fprintf(stderr, "error: no DUART28 adapter found\n");
+		exit(1);
+	}
+	usbh = usbwrap_open_dev(dev, 1);
+	read_eeprom(usbh);
+	prev = analyze_eeprom();
+	if (prev == newconf) {
+		printf("EEPROM is already in the right state, nothing to do\n");
+		usbwrap_close_dev(usbh);
+		return 0;
+	}
+	printf("Reprogramming to DUART28%c configuration\n", newconf);
+	update_eeprom(usbh, newconf);
+	usbwrap_close_dev(usbh);
+	printf("EEPROM changed, replug the USB device to take effect\n");
+	return 0;
 }
 
 main(argc, argv)