changeset 53:d4d3531d342a

cp2102-read-eeprom program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Sep 2023 18:49:07 +0000
parents 61cdcf2eb17b
children 10789bcf07c4
files .hgignore cp2102/Makefile cp2102/cp210x_defs.h cp2102/intel_hex_out.c cp2102/read_eeprom.c cp2102/read_eeprom_main.c
diffstat 6 files changed, 128 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Mon Sep 11 17:48:53 2023 +0000
+++ b/.hgignore	Mon Sep 11 18:49:07 2023 +0000
@@ -2,6 +2,7 @@
 
 \.[oa]$
 
+^cp2102/cp2102-read-eeprom$
 ^cp2102/cp2102-read-partno$
 
 ^duart28/fc-duart28-conf$
--- a/cp2102/Makefile	Mon Sep 11 17:48:53 2023 +0000
+++ b/cp2102/Makefile	Mon Sep 11 18:49:07 2023 +0000
@@ -1,14 +1,19 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	cp2102-read-partno
+PROGS=	cp2102-read-eeprom cp2102-read-partno
 LIBS=	../libuwrap/libuwrap.a
 
 INSTALL_PREFIX=	/opt/freecalypso
 
 INSTBIN=${INSTALL_PREFIX}/bin
 
+READ_EEPROM_OBJS=	intel_hex_out.o read_eeprom.o read_eeprom_main.o
+
 all:	${PROGS}
 
+cp2102-read-eeprom:	${READ_EEPROM_OBJS} ${LIBS}
+	${CC} ${CFLAGS} -o $@ ${READ_EEPROM_OBJS} ${LIBS} -lusb
+
 cp2102-read-partno:	read_partno.o ${LIBS}
 	${CC} ${CFLAGS} -o $@ read_partno.o ${LIBS} -lusb
 
--- a/cp2102/cp210x_defs.h	Mon Sep 11 17:48:53 2023 +0000
+++ b/cp2102/cp210x_defs.h	Mon Sep 11 18:49:07 2023 +0000
@@ -26,6 +26,8 @@
 #define	SIZE_BAUDRATE_TABLE	(SIZE_BAUDRATES * SIZE_BAUDRATE_CFG)
 #define	SIZE_VENDOR_STRING	50
 
+#define	EEPROM_START_ADDR	0x3600
+
 /* a little bit adapted from libftdi */
 
 #define DEVICE_OUT_REQTYPE (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cp2102/intel_hex_out.c	Mon Sep 11 18:49:07 2023 +0000
@@ -0,0 +1,45 @@
+/*
+ * This module implements a function for writing out the same Intel HEX
+ * format as used by cp210x-program-1.0, the Python-language tool from 2014.
+ */
+
+#include <sys/types.h>
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "cp210x_defs.h"
+
+void
+intel_hex_out(eeprom, outf)
+	u_char *eeprom;
+	FILE *outf;
+{
+	unsigned linecnt, bytecnt;
+	unsigned addr;
+	u_char *sp;
+	u_char record[21], *dp, csum;
+
+	sp = eeprom;
+	addr = EEPROM_START_ADDR;
+	for (linecnt = 0; linecnt < 64; linecnt++) {
+		dp = record;
+		*dp++ = 0x10;
+		*dp++ = addr >> 8;
+		*dp++ = addr;
+		*dp++ = 0x00;
+		for (bytecnt = 0; bytecnt < 16; bytecnt++)
+			*dp++ = *sp++;
+		csum = 0;
+		for (bytecnt = 0; bytecnt < 20; bytecnt++)
+			csum = record[bytecnt];
+		csum = 0x100 - csum;
+		record[20] = csum;
+		putc(':', outf);
+		for (bytecnt = 0; bytecnt < 21; bytecnt++)
+			fprintf(outf, "%02X", record[bytecnt]);
+		putc('\n', outf);
+		addr += 16;
+	}
+	fputs(":00000001FF\n", outf);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cp2102/read_eeprom.c	Mon Sep 11 18:49:07 2023 +0000
@@ -0,0 +1,30 @@
+/*
+ * This module implements the elementary function for reading the EEPROM
+ * content out of 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
+read_eeprom(usbh)
+	usb_dev_handle *usbh;
+{
+	int rc;
+
+	rc = usb_control_msg(usbh, DEVICE_IN_REQTYPE, CP210x_CONFIG,
+			     REG_EEPROM, 0, (char *) eeprom, SIZE_EEPROM,
+			     USB_READ_TIMEOUT);
+	if (rc != SIZE_EEPROM) {
+		fprintf(stderr,
+			"EEPROM read error: usb_control_msg() returned %d\n",
+			rc);
+		exit(1);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cp2102/read_eeprom_main.c	Mon Sep 11 18:49:07 2023 +0000
@@ -0,0 +1,44 @@
+/*
+ * This program locates a CP2102 device via libusb, reads its internal
+ * 1024-byte EEPROM and emits (on stdout) an image of this EEPROM
+ * in the same Intel HEX format as used by cp210x-program-1.0,
+ * the Python-language tool from 2014.
+ */
+
+#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 "cp210x_defs.h"
+
+u_char eeprom[SIZE_EEPROM];
+
+main(argc, argv)
+	char **argv;
+{
+	struct usb_device *dev;
+	usb_dev_handle *usbh;
+
+	if (argc != 2) {
+		fprintf(stderr, "usage: %s device-selector\n", argv[0]);
+		exit(1);
+	}
+	dev = find_usbdev_by_desc_string(argv[1]);
+	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);
+	}
+	read_eeprom(usbh);
+	usb_close(usbh);
+	intel_hex_out(eeprom, stdout);
+	exit(0);
+}