changeset 58:4890ded06a8b

cp2102-decode-baudtab program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Sep 2023 20:33:26 +0000
parents deba1d5c8024
children 672c7adc8bc9
files .hgignore cp2102/Makefile cp2102/decode_baudtab.c cp2102/decode_baudtab_main.c
diffstat 4 files changed, 74 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Mon Sep 11 19:54:28 2023 +0000
+++ b/.hgignore	Mon Sep 11 20:33:26 2023 +0000
@@ -2,6 +2,7 @@
 
 \.[oa]$
 
+^cp2102/cp2102-decode-baudtab$
 ^cp2102/cp2102-read-eeprom$
 ^cp2102/cp2102-read-partno$
 ^cp2102/file_rw_test$
--- a/cp2102/Makefile	Mon Sep 11 19:54:28 2023 +0000
+++ b/cp2102/Makefile	Mon Sep 11 20:33:26 2023 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	cp2102-read-eeprom cp2102-read-partno
+PROGS=	cp2102-decode-baudtab cp2102-read-eeprom cp2102-read-partno
 NOINST=	file_rw_test
 LIBS=	../libuwrap/libuwrap.a
 
@@ -8,12 +8,15 @@
 
 INSTBIN=${INSTALL_PREFIX}/bin
 
+DECODE_BAUDTAB_OBJS=	decode_baudtab.o decode_baudtab_main.o intel_hex_in.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
 
 all:	${PROGS} ${NOINST}
 
+cp2102-decode-baudtab:	${DECODE_BAUDTAB_OBJS}
+	${CC} ${CFLAGS} -o $@ ${DECODE_BAUDTAB_OBJS}
+
 cp2102-read-eeprom:	${READ_EEPROM_OBJS} ${LIBS}
 	${CC} ${CFLAGS} -o $@ ${READ_EEPROM_OBJS} ${LIBS} -lusb
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cp2102/decode_baudtab.c	Mon Sep 11 20:33:26 2023 +0000
@@ -0,0 +1,45 @@
+/*
+ * The function implemented in this module decodes the baud rate table
+ * contained in the captured CP2102 EEPROM image.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "cp210x_defs.h"
+
+extern u_char eeprom[SIZE_EEPROM];
+
+void
+decode_baud_table(outf)
+	FILE *outf;
+{
+	unsigned entry;
+	u_char *dp;
+	unsigned baudrate, baudgen, timegen, prescaler, sparebyte;
+	unsigned baud_div, calc_baud, time_us;
+
+	dp = eeprom;
+	for (entry = 0; entry < SIZE_BAUDRATES; entry++) {
+		baudgen = (dp[0] << 8) | dp[1];
+		timegen = (dp[2] << 8) | dp[3];
+		prescaler = dp[4];
+		sparebyte = dp[5];
+		baudrate = dp[6] | (dp[7] << 8) | (dp[8] << 16) | (dp[9] << 24);
+		fprintf(outf, "baud-entry %2u: %7u = %04X, %04X, %u, %u",
+			entry, baudrate, baudgen, timegen, prescaler,
+			sparebyte);
+		if (prescaler) {
+			baud_div = (0x10000 - baudgen) * prescaler;
+			calc_baud = 24000000 / baud_div;
+			fprintf(outf, "\t# %u baud, ", calc_baud);
+			time_us = (0x10000 - timegen) * 2;
+			if (time_us >= 1000)
+				fprintf(outf, "%u.%03u ms", time_us / 1000,
+					time_us % 1000);
+			else
+				fprintf(outf, "%u us", time_us);
+		}
+		putc('\n', outf);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cp2102/decode_baudtab_main.c	Mon Sep 11 20:33:26 2023 +0000
@@ -0,0 +1,23 @@
+/*
+ * This program reads a CP2102 EEPROM image from an Intel HEX file
+ * and decodes the baud rate table contained therein.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "cp210x_defs.h"
+
+u_char eeprom[SIZE_EEPROM];
+
+main(argc, argv)
+	char **argv;
+{
+	if (argc != 2) {
+		fprintf(stderr, "usage: %s ihex-file\n", argv[0]);
+		exit(1);
+	}
+	read_intel_hex(argv[1]);
+	decode_baud_table(stdout);
+	exit(0);
+}