changeset 3:de85c3680d7e

sw: fc-mcsi-rx program put together
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 11 Oct 2024 23:54:39 +0000
parents a4918a161d2e
children 1dacfe7d5b3d
files .hgignore sw/mcsi-rx/Makefile sw/mcsi-rx/initflush.c sw/mcsi-rx/main.c sw/mcsi-rx/mainloop.c sw/mcsi-rx/robe_out.c
diffstat 6 files changed, 179 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Fri Oct 11 22:37:11 2024 +0000
+++ b/.hgignore	Fri Oct 11 23:54:39 2024 +0000
@@ -6,3 +6,5 @@
 \.bin$
 \.json$
 \.rpt$
+
+^sw/mcsi-rx/fc-mcsi-rx$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sw/mcsi-rx/Makefile	Fri Oct 11 23:54:39 2024 +0000
@@ -0,0 +1,21 @@
+CC=	gcc
+CFLAGS=	-O2
+PROG=	fc-mcsi-rx
+OBJS=	initflush.o main.o mainloop.o robe_out.o
+LIBS=	../libserial/libserial.a
+
+INSTALL_PREFIX=	/opt/freecalypso
+
+INSTBIN=${INSTALL_PREFIX}/bin
+
+all:	${PROG}
+
+${PROG}:	${OBJS} ${LIBS}
+	${CC} ${CFLAGS} -o $@ ${OBJS} ${LIBS}
+
+install:
+	mkdir -p ${INSTBIN}
+	install -c ${PROG} ${INSTBIN}
+
+clean:
+	rm -f *.o ${PROG}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sw/mcsi-rx/initflush.c	Fri Oct 11 23:54:39 2024 +0000
@@ -0,0 +1,41 @@
+/*
+ * FTDI chip+driver combo (specifically the combination of FTDI chips and
+ * ftdi_sio driver in Linux, not sure who is the actual culprit) exhibits
+ * this unpleasant behaviour: even though we request "please flush all
+ * previous input" when we set our termios params, old accumulated serial
+ * Rx bytes still remain in some buffer somewhere, and a newly started
+ * serial application receives this stale garbage.  As a workaround,
+ * we do an additional flush of our own: we put the fd in non-blocking mode
+ * and keep reading and discarding data until we get EAGAIN or EWOULDBLOCK.
+ */
+
+#include <sys/types.h>
+#include <sys/errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+extern int target_fd;
+
+void
+init_serial_flush()
+{
+	u_char buf[512];
+	int cc;
+
+	set_serial_nonblock(1);
+	for (;;) {
+		cc = read(target_fd, buf, sizeof buf);
+		if (cc <= 0)
+			break;
+	}
+	if (cc == 0) {
+		fprintf(stderr,
+			"read EOF from serial port during initial flush\n");
+		exit(1);
+	}
+	if (errno == EAGAIN || errno == EWOULDBLOCK)
+		return;		/* success */
+	perror("serial port read");
+	exit(1);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sw/mcsi-rx/main.c	Fri Oct 11 23:54:39 2024 +0000
@@ -0,0 +1,26 @@
+/*
+ * Main module for fc-mcsi-rx program.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+FILE *out_binfile;
+
+main(argc, argv)
+	char **argv;
+{
+	if (argc != 3) {
+		fprintf(stderr, "usage: %s ttyport out-bin-file\n", argv[0]);
+		exit(1);
+	}
+	open_serial_port(argv[1]);
+	set_serial_baudrate(187500);
+	init_serial_flush();
+	out_binfile = fopen(argv[2], "w");
+	if (!out_binfile) {
+		perror(argv[2]);
+		exit(1);
+	}
+	main_loop();		/* does not return */
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sw/mcsi-rx/mainloop.c	Fri Oct 11 23:54:39 2024 +0000
@@ -0,0 +1,67 @@
+/*
+ * This module holds our main loop code, factored out into a separate
+ * function that is called from main() after initialization.
+ */
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+extern int target_fd;
+extern FILE *out_binfile;
+
+main_loop()
+{
+	fd_set fds;
+	struct timeval tv;
+	u_char buf[320];
+	unsigned off;
+	int cc, is_active;
+
+	is_active = 0;
+	off = 0;
+	for (;;) {
+		FD_ZERO(&fds);
+		FD_SET(target_fd, &fds);
+		if (is_active) {
+			tv.tv_sec = 0;
+			tv.tv_usec = 100000;
+			cc = select(target_fd+1, &fds, 0, 0, &tv);
+		} else
+			cc = select(target_fd+1, &fds, 0, 0, 0);
+		if (cc < 0) {
+			if (errno == EINTR)
+				continue;
+			perror("select");
+			exit(1);
+		}
+		if (cc == 0) {
+			is_active = 0;
+			fflush(out_binfile);
+			printf("Rx stream stopped, buffer dribble = %u\n", off);
+			off = 0;
+			continue;
+		}
+		cc = read(target_fd, buf + off, sizeof(buf) - off);
+		if (cc < 0) {
+			perror("serial port read");
+			exit(1);
+		}
+		if (cc == 0) {
+			fprintf(stderr, "read EOF from serial port\n");
+			exit(1);
+		}
+		if (!is_active) {
+			printf("Rx stream started\n");
+			is_active = 1;
+		}
+		off += cc;
+		if (off >= sizeof(buf)) {
+			process_rx_block(buf);
+			off = 0;
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sw/mcsi-rx/robe_out.c	Fri Oct 11 23:54:39 2024 +0000
@@ -0,0 +1,22 @@
+/*
+ * The function in this module is responsible for writing "robe" output.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+
+extern FILE *out_binfile;
+
+process_rx_block(buf)
+	u_char *buf;
+{
+	unsigned n;
+	u_char *sp;
+
+	sp = buf;
+	for (n = 0; n < 160; n++) {
+		putc(sp[1], out_binfile);
+		putc(sp[0], out_binfile);
+		sp += 2;
+	}
+}