changeset 248:cb1ba53a1106

beginning of factored-out libserial
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 21 Sep 2017 21:43:34 +0000
parents b5b148ef63da
children d0a4c05d98dc
files libserial-orig/Makefile libserial-orig/baudrate.h libserial-orig/baudtab.c libserial-orig/openport.c libserial-orig/setbaud.c
diffstat 5 files changed, 127 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libserial-orig/Makefile	Thu Sep 21 21:43:34 2017 +0000
@@ -0,0 +1,13 @@
+CC=	gcc
+CFLAGS=	-O2
+OBJS=	baudtab.o openport.o setbaud.o
+LIB=	libserial.a
+
+all:	${LIB}
+
+${LIB}:	${OBJS}
+	ar rcu $@ ${OBJS}
+	ranlib $@
+
+clean:
+	rm -f *.[oa] errs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libserial-orig/baudrate.h	Thu Sep 21 21:43:34 2017 +0000
@@ -0,0 +1,7 @@
+/* this header file defines the data structure for baud rate machinations */
+
+struct baudrate {
+	char	*name;
+	int	termios_code;
+	int	bootrom_code;
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libserial-orig/baudtab.c	Thu Sep 21 21:43:34 2017 +0000
@@ -0,0 +1,47 @@
+/*
+ * This module contains the table of baud rates supported
+ * by this implementation of FreeCalypso libserial.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <termios.h>
+#include "baudrate.h"
+
+struct baudrate baud_rate_table[] = {
+	/* the first listed rate will be our default */
+	{"115200",	B115200,	0},
+	{"57600",	B57600,		1},
+	{"38400",	B38400,		2},
+	{"19200",	B19200,		4},
+	/*
+	 * Non-standard high baud rates remapped by CP2102 EEPROM programming
+	 * or by a hacky patch to the ftdi_sio Linux kernel driver to work
+	 * with FTDI adapters.
+	 */
+	{"812500",	B921600,	-1},
+	{"406250",	B460800,	-1},
+	{"203125",	B230400,	-1},
+	/* table search terminator */
+	{NULL,		B0,		-1},
+};
+
+struct baudrate *
+find_baudrate_by_name(srch_name)
+	char *srch_name;
+{
+	struct baudrate *br;
+
+	for (br = baud_rate_table; br->name; br++)
+		if (!strcmp(br->name, srch_name))
+			break;
+	if (br->name)
+		return(br);
+	else {
+		fprintf(stderr, "error: baud rate \"%s\" not known\n",
+			srch_name);
+		return(NULL);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libserial-orig/openport.c	Thu Sep 21 21:43:34 2017 +0000
@@ -0,0 +1,24 @@
+/*
+ * This module implements the basic serial port opening operation.
+ */
+
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/ioctl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int target_fd;
+
+open_serial_port(ttyport)
+	char *ttyport;
+{
+	target_fd = open(ttyport, O_RDWR|O_NONBLOCK);
+	if (target_fd < 0) {
+		perror(ttyport);
+		exit(1);
+	}
+	ioctl(target_fd, TIOCEXCL);
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libserial-orig/setbaud.c	Thu Sep 21 21:43:34 2017 +0000
@@ -0,0 +1,36 @@
+/*
+ * This module implements the termios/ioctl setting of the baud rate.
+ */
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <termios.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "baudrate.h"
+
+extern int target_fd;
+
+struct baudrate *current_baud_rate;
+
+set_serial_baudrate(br)
+	struct baudrate *br;
+{
+	struct termios target_termios;
+
+	target_termios.c_iflag = IGNBRK;
+	target_termios.c_oflag = 0;
+	target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8;
+	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("tcsetattr");
+		exit(1);
+	}
+	current_baud_rate = br;
+	return 0;
+}