changeset 187:219ae678b955

fc-serscpi utility written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 23 Apr 2017 00:51:01 +0000
parents 9968717eedd7
children ad6df4952e33
files .hgignore rfcal/cmu200/Makefile rfcal/cmu200/openport.c rfcal/cmu200/sertool.c
diffstat 4 files changed, 178 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Fri Apr 14 07:45:57 2017 +0000
+++ b/.hgignore	Sun Apr 23 00:51:01 2017 +0000
@@ -27,6 +27,7 @@
 ^miscutil/fc-vm2hex$
 ^miscutil/imei-luhn$
 
+^rfcal/cmu200/fc-serscpi$
 ^rfcal/vcxo-manual/fc-vcxo-linear$
 ^rfcal/vcxo-manual/fc-vcxo-param$
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rfcal/cmu200/Makefile	Sun Apr 23 00:51:01 2017 +0000
@@ -0,0 +1,18 @@
+CC=	gcc
+CFLAGS=	-O2
+PROGS=	fc-serscpi
+INSTBIN=/opt/freecalypso/bin
+
+SERSCPI_OBJS=	openport.o sertool.o
+
+all:	${PROGS}
+
+fc-serscpi:	${SERSCPI_OBJS}
+	${CC} ${CFLAGS} -o $@ ${SERSCPI_OBJS}
+
+install:
+	mkdir -p ${INSTBIN}
+	install -c ${PROGS} ${INSTBIN}
+
+clean:
+	rm -f *.o *.out *errs ${PROGS}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rfcal/cmu200/openport.c	Sun Apr 23 00:51:01 2017 +0000
@@ -0,0 +1,71 @@
+/*
+ * Serial port opening code for talking to CMU200
+ */
+
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/ioctl.h>
+#include <termios.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+
+extern int target_fd;
+
+static struct baudrate {
+	char	*name;
+	speed_t	termios_code;
+} baud_rate_table[] = {
+	{"1200",	B1200},
+	{"2400",	B2400},
+	{"4800",	B4800},
+	{"9600",	B9600},
+	{"19200",	B19200},
+	{"38400",	B38400},
+	{"57600",	B57600},
+	{"115200",	B115200},
+	/* table search terminator */
+	{NULL,		B0}
+};
+
+open_target_serial(ttydev, baudname)
+	char *ttydev, *baudname;
+{
+	struct termios target_termios;
+	struct baudrate *br;
+
+	for (br = baud_rate_table; br->name; br++)
+		if (!strcmp(br->name, baudname))
+			break;
+	if (!br->name) {
+		fprintf(stderr, "baud rate \"%s\" unknown/unsupported\n",
+			baudname);
+		exit(1);
+	}
+	target_fd = open(ttydev, O_RDWR|O_NONBLOCK);
+	if (target_fd < 0) {
+		perror(ttydev);
+		exit(1);
+	}
+	target_termios.c_iflag = IGNBRK;
+	target_termios.c_oflag = 0;
+	target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8|CRTSCTS;
+	target_termios.c_lflag = 0;
+	target_termios.c_cc[VMIN] = 1;
+	target_termios.c_cc[VTIME] = 0;
+	cfsetispeed(&target_termios, br->termios_code);
+	cfsetospeed(&target_termios, br->termios_code);
+	if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
+		perror("initial tcsetattr on target");
+		exit(1);
+	}
+	return 0;
+}
+
+set_serial_nonblock(state)
+	int state;
+{
+	ioctl(target_fd, FIONBIO, &state);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rfcal/cmu200/sertool.c	Sun Apr 23 00:51:01 2017 +0000
@@ -0,0 +1,88 @@
+/*
+ * This module contains the main() function for fc-serscpi, a manual tool
+ * intended for learning how to control the CMU200 with SCPI commands
+ * over RS-232.
+ */
+
+#include <sys/types.h>
+#include <sys/errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <strings.h>
+
+extern int errno;
+
+int target_fd;
+
+static void
+safe_output(buf, cc)
+	u_char *buf;
+{
+	int i, c;
+
+	for (i = 0; i < cc; i++) {
+		c = buf[i];
+		if (c == '\r' || c == '\n' || c == '\t' || c == '\b') {
+			putchar(c);
+			continue;
+		}
+		if (c & 0x80) {
+			putchar('M');
+			putchar('-');
+			c &= 0x7F;
+		}
+		if (c < 0x20) {
+			putchar('^');
+			putchar(c + '@');
+		} else if (c == 0x7F) {
+			putchar('^');
+			putchar('?');
+		} else
+			putchar(c);
+	}
+	fflush(stdout);
+}
+
+main(argc, argv)
+	char **argv;
+{
+	char buf[BUFSIZ];
+	fd_set fds, fds1;
+	register int i, cc, max;
+
+	if (argc != 3) {
+		fprintf(stderr, "usage: %s ttyname baudrate\n", argv[0]);
+		exit(1);
+	}
+	open_target_serial(argv[1], argv[2]);
+	set_serial_nonblock(0);
+	FD_ZERO(&fds);
+	FD_SET(0, &fds);
+	FD_SET(target_fd, &fds);
+	max = target_fd + 1;
+	for (;;) {
+		bcopy(&fds, &fds1, sizeof(fd_set));
+		i = select(max, &fds1, NULL, NULL, NULL);
+		if (i < 0) {
+			if (errno == EINTR)
+				continue;
+			perror("select");
+			exit(1);
+		}
+		if (FD_ISSET(0, &fds1)) {
+			cc = read(0, buf, sizeof buf);
+			if (cc <= 0)
+				exit(0);
+			write(target_fd, buf, cc);
+		}
+		if (FD_ISSET(target_fd, &fds1)) {
+			cc = read(target_fd, buf, sizeof buf);
+			if (cc <= 0) {
+				fprintf(stderr, "EOF/error on target tty\n");
+				exit(1);
+			}
+			safe_output(buf, cc);
+		}
+	}
+}