changeset 194:31d43f0e469a

fc-cmu200d skeleton started, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 24 Apr 2017 00:45:54 +0000
parents a57c93cf7f5c
children db9ee7745cdd
files .hgignore rfcal/cmu200/Makefile rfcal/cmu200/init.c rfcal/cmu200/main.c rfcal/cmu200/secaddr.h rfcal/cmu200/sercmd.c
diffstat 6 files changed, 159 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Apr 23 22:39:45 2017 +0000
+++ b/.hgignore	Mon Apr 24 00:45:54 2017 +0000
@@ -27,6 +27,7 @@
 ^miscutil/fc-vm2hex$
 ^miscutil/imei-luhn$
 
+^rfcal/cmu200/fc-cmu200d$
 ^rfcal/cmu200/fc-serscpi$
 ^rfcal/vcxo-manual/fc-vcxo-linear$
 ^rfcal/vcxo-manual/fc-vcxo-param$
--- a/rfcal/cmu200/Makefile	Sun Apr 23 22:39:45 2017 +0000
+++ b/rfcal/cmu200/Makefile	Mon Apr 24 00:45:54 2017 +0000
@@ -1,12 +1,16 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	fc-serscpi
+PROGS=	fc-cmu200d fc-serscpi
 INSTBIN=/opt/freecalypso/bin
 
+CMU200D_OBJS=	init.o main.o openport.o sercmd.o
 SERSCPI_OBJS=	openport.o sertool.o
 
 all:	${PROGS}
 
+fc-cmu200d:	${CMU200D_OBJS}
+	${CC} ${CFLAGS} -o $@ ${CMU200D_OBJS}
+
 fc-serscpi:	${SERSCPI_OBJS}
 	${CC} ${CFLAGS} -o $@ ${SERSCPI_OBJS}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rfcal/cmu200/init.c	Mon Apr 24 00:45:54 2017 +0000
@@ -0,0 +1,48 @@
+/*
+ * This module contains the code that fc-cmu200d runs at startup,
+ * to put the CMU200 into a known state at the global level.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include "secaddr.h"
+
+extern char instrument_response[];
+
+static char id_string[] = "Rohde&Schwarz,CMU";
+
+static void
+assign_secondary_addr(addr, func)
+	char *func;
+{
+	char cmdbuf[80];
+
+	sprintf(cmdbuf, "SYST:REM:ADDR:SEC %d,\"%s\"\n", addr, func);
+	send_scpi_cmd(cmdbuf);
+}
+
+init_cmu200()
+{
+	/* Test if we are connected to a CMU */
+	send_scpi_cmd("*IDN?\n");
+	collect_instr_response();
+	if (strncasecmp(instrument_response, id_string, strlen(id_string))) {
+		fprintf(stderr, "error: not connected to a CMU200\n");
+		exit(1);
+	}
+	/* init commands */
+	send_scpi_cmd("*SEC 0\n");
+	send_scpi_cmd("*RST;*OPC?\n");
+	collect_staropc_response();
+	send_scpi_cmd("SYST:NONV:DIS\n");
+	assign_secondary_addr(SECADDR_RF_NSIG, "RF_NSig");
+	assign_secondary_addr(SECADDR_GSM900MS_NSIG, "GSM900MS_NSig");
+	assign_secondary_addr(SECADDR_GSM1800MS_NSIG, "GSM1800MS_NSig");
+	assign_secondary_addr(SECADDR_GSM1900MS_NSIG, "GSM1900MS_NSig");
+	assign_secondary_addr(SECADDR_GSM850MS_NSIG, "GSM850MS_NSig");
+	send_scpi_cmd("*OPC?\n");
+	collect_staropc_response();
+	printf("CMU200 init complete\n");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rfcal/cmu200/main.c	Mon Apr 24 00:45:54 2017 +0000
@@ -0,0 +1,33 @@
+/*
+ * This module contains the main() function for fc-cmu200d.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int target_fd;
+
+static char default_socket_pathname[] = "/tmp/fc_rftest_socket";
+
+char *bind_socket_pathname;
+
+main(argc, argv)
+	char **argv;
+{
+	if (argc < 3 || argc > 4) {
+		fprintf(stderr,
+			"usage: %s serial-port baud [socket-pathname]\n",
+			argv[0]);
+		exit(1);
+	}
+	open_target_serial(argv[1], argv[2]);
+	set_serial_nonblock(0);
+	init_cmu200();
+	if (argc > 3)
+		bind_socket_pathname = argv[3];
+	else
+		bind_socket_pathname = default_socket_pathname;
+
+	/* to be continued */
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rfcal/cmu200/secaddr.h	Mon Apr 24 00:45:54 2017 +0000
@@ -0,0 +1,14 @@
+/*
+ * The assignment of secondary addresses to CMU200 function groups is
+ * arbitrary, but we need *some* assignment in order to access the SCPI
+ * objects that correspond to useful functions.  We define our secondary
+ * address assignments in this header file, set them up on the CMU200
+ * at start-up, and then use them to access the functions we need
+ * when we need them.
+ */
+
+#define	SECADDR_RF_NSIG		1
+#define	SECADDR_GSM900MS_NSIG	2
+#define	SECADDR_GSM1800MS_NSIG	3
+#define	SECADDR_GSM1900MS_NSIG	4
+#define	SECADDR_GSM850MS_NSIG	5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rfcal/cmu200/sercmd.c	Mon Apr 24 00:45:54 2017 +0000
@@ -0,0 +1,58 @@
+/*
+ * This module contains the functions that send serial commands to the CMU200
+ * and collect the instrument's serial responses.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <strings.h>
+
+extern int target_fd;
+
+char instrument_response[4096];
+
+send_scpi_cmd(cmd)
+	char *cmd;
+{
+	printf("Command to CMU: %s", cmd);
+	write(target_fd, cmd, strlen(cmd));
+}
+
+collect_instr_response()
+{
+	char buf[BUFSIZ];
+	int cc, pos;
+
+	for (pos = 0; ; ) {
+		cc = read(target_fd, buf, sizeof buf);
+		if (cc <= 0) {
+			perror("error reading from serial port");
+			exit(1);
+		}
+		if (pos + cc > sizeof instrument_response) {
+			fprintf(stderr,
+		"error: response from CMU200 exceeds our buffer size\n");
+			exit(1);
+		}
+		bcopy(buf, instrument_response + pos, cc);
+		pos += cc;
+		if (instrument_response[pos-1] == '\n')
+			break;
+	}
+	instrument_response[pos-1] = '\0';
+	printf("Instrument response: %s\n", instrument_response);
+}
+
+collect_staropc_response()
+{
+	collect_instr_response();
+	if (instrument_response[0] != '1' ||
+	    instrument_response[1] && !isspace(instrument_response[1])) {
+		fprintf(stderr, "error: unexpected response to *OPC?\n");
+		exit(1);
+	}
+}