diff cmu200/openport.c @ 0:bd62be88259d

initial import of rfcal code and docs from freecalypso-tools repository
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 20 May 2017 18:49:35 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmu200/openport.c	Sat May 20 18:49:35 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);
+}