changeset 254:6984b76f3def

beginning of libserial-newlnx: copy of libserial-orig
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 22 Sep 2017 00:09:37 +0000
parents 6f078c4a5506
children ab8410d06ca7
files libserial-newlnx/baudrate.h libserial-newlnx/baudtab.c libserial-newlnx/nonblock.c libserial-newlnx/openport.c libserial-newlnx/setbaud.c libserial-newlnx/setbyname.c
diffstat 6 files changed, 150 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libserial-newlnx/baudrate.h	Fri Sep 22 00:09:37 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-newlnx/baudtab.c	Fri Sep 22 00:09:37 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-newlnx/nonblock.c	Fri Sep 22 00:09:37 2017 +0000
@@ -0,0 +1,15 @@
+/*
+ * This module contains the set_serial_nonblock() function.
+ */
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+extern int target_fd;
+
+set_serial_nonblock(state)
+	int state;
+{
+	ioctl(target_fd, FIONBIO, &state);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libserial-newlnx/openport.c	Fri Sep 22 00:09:37 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-newlnx/setbaud.c	Fri Sep 22 00:09:37 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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libserial-newlnx/setbyname.c	Fri Sep 22 00:09:37 2017 +0000
@@ -0,0 +1,21 @@
+/*
+ * This module contains the wrapper function (used by loadtools)
+ * that looks up a baud rate by name and calls set_serial_baud().
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "baudrate.h"
+
+extern struct baudrate *find_baudrate_by_name();
+
+set_fixed_baudrate(baudname)
+	char *baudname;
+{
+	struct baudrate *br;
+
+	br = find_baudrate_by_name(baudname);
+	if (!br)
+		exit(1);	/* error msg already printed */
+	set_serial_baudrate(br);
+}