changeset 96:c6d04771bf6a

cp2102-write-eeprom program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Sep 2023 02:38:59 +0000
parents a378bf95c26c
children 8d35346f1d46
files .hgignore cp2102/Makefile cp2102/write_eeprom.c cp2102/write_eeprom_main.c
diffstat 4 files changed, 108 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Thu Sep 28 02:19:39 2023 +0000
+++ b/.hgignore	Thu Sep 28 02:38:59 2023 +0000
@@ -8,6 +8,7 @@
 ^cp2102/cp2102-read-baudtab$
 ^cp2102/cp2102-read-eeprom$
 ^cp2102/cp2102-read-partno$
+^cp2102/cp2102-write-eeprom$
 ^cp2102/file_rw_test$
 
 ^duart28/fc-duart28-conf$
--- a/cp2102/Makefile	Thu Sep 28 02:19:39 2023 +0000
+++ b/cp2102/Makefile	Thu Sep 28 02:38:59 2023 +0000
@@ -1,7 +1,8 @@
 CC=	gcc
 CFLAGS=	-O2
 PROGS=	cp2102-decode-baudtab cp2102-decode-ee-desc cp2102-patch-ee-image \
-	cp2102-read-baudtab cp2102-read-eeprom cp2102-read-partno
+	cp2102-read-baudtab cp2102-read-eeprom cp2102-read-partno \
+	cp2102-write-eeprom
 NOINST=	file_rw_test
 LIBS=	../libuwrap/libuwrap.a
 
@@ -19,6 +20,7 @@
 READ_BAUDTAB_OBJS=	decode_baudtab.o read_baudtab.o read_eeprom.o
 READ_EEPROM_OBJS=	intel_hex_out.o read_eeprom.o read_eeprom_main.o
 RW_TEST_OBJS=		intel_hex_in.o intel_hex_out.o file_rw_test.o
+WRITE_EEPROM_OBJS=	intel_hex_in.o write_eeprom.o write_eeprom_main.o
 
 all:	${PROGS} ${NOINST}
 
@@ -40,6 +42,9 @@
 cp2102-read-partno:	read_partno.o ${LIBS}
 	${CC} ${CFLAGS} -o $@ read_partno.o ${LIBS} -lusb
 
+cp2102-write-eeprom:	${WRITE_EEPROM_OBJS} ${LIBS}
+	${CC} ${CFLAGS} -o $@ ${WRITE_EEPROM_OBJS} ${LIBS} -lusb
+
 file_rw_test:		${RW_TEST_OBJS}
 	${CC} ${CFLAGS} -o $@ ${RW_TEST_OBJS}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cp2102/write_eeprom.c	Thu Sep 28 02:38:59 2023 +0000
@@ -0,0 +1,30 @@
+/*
+ * This module implements the elementary function for writing the EEPROM
+ * on a CP2102 device.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <usb.h>
+#include "cp210x_defs.h"
+
+extern u_char eeprom[SIZE_EEPROM];
+
+void
+write_eeprom(usbh)
+	usb_dev_handle *usbh;
+{
+	int rc;
+
+	rc = usb_control_msg(usbh, DEVICE_OUT_REQTYPE, CP210x_CONFIG,
+			     REG_EEPROM, 0, (char *) eeprom, SIZE_EEPROM,
+			     USB_WRITE_TIMEOUT);
+	if (rc != SIZE_EEPROM) {
+		fprintf(stderr,
+			"EEPROM write error: usb_control_msg() returned %d\n",
+			rc);
+		exit(1);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cp2102/write_eeprom_main.c	Thu Sep 28 02:38:59 2023 +0000
@@ -0,0 +1,71 @@
+/*
+ * This program reads a CP2102 EEPROM image from an Intel HEX file
+ * and writes it into a physical CP2102 USB device.
+ */
+
+#include <sys/types.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <usb.h>
+#include "../libuwrap/find_dev.h"
+#include "../libuwrap/open_close.h"
+#include "cp210x_defs.h"
+
+u_char eeprom[SIZE_EEPROM];
+char *device_selector, *input_filename;
+int no_detach;
+
+process_cmdline(argc, argv)
+	char **argv;
+{
+	int c;
+	extern int optind;
+	extern char *optarg;
+
+	while ((c = getopt(argc, argv, "n")) != EOF) {
+		switch (c) {
+		case 'n':
+			no_detach = 1;
+			continue;
+		default:
+			/* error msg already printed */
+			exit(1);
+		}
+	}
+	if (argc != optind + 2) {
+		fprintf(stderr,
+			"usage: %s [options] device-selector eeprom-image\n",
+			argv[0]);
+		exit(1);
+	}
+	device_selector = argv[optind];
+	input_filename = argv[optind + 1];
+}
+
+main(argc, argv)
+	char **argv;
+{
+	struct usb_device *dev;
+	usb_dev_handle *usbh;
+
+	process_cmdline(argc, argv);
+	read_intel_hex(input_filename);
+	dev = find_usbdev_by_desc_string(device_selector);
+	if (!dev) {
+		fprintf(stderr, "error: specified USB device not found\n");
+		exit(1);
+	}
+	usbh = usb_open(dev);
+	if (!usbh) {
+		fprintf(stderr, "error: usb_open() failed\n");
+		exit(1);
+	}
+	if (!no_detach)
+		usbwrap_claim_all_ifs(usbh);
+	write_eeprom(usbh);
+	usb_close(usbh);
+	exit(0);
+}