changeset 2:a4918a161d2e

sw: starting with libserial, copied from fc-sim-sniff
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 11 Oct 2024 22:37:11 +0000
parents b3190839cce3
children de85c3680d7e
files .hgignore sw/libserial/Makefile sw/libserial/nonblock.c sw/libserial/openport.c sw/libserial/setbaud.c
diffstat 5 files changed, 87 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Fri Oct 11 21:11:24 2024 +0000
+++ b/.hgignore	Fri Oct 11 22:37:11 2024 +0000
@@ -1,5 +1,7 @@
 syntax: regexp
 
+\.[oa]$
+
 \.asc$
 \.bin$
 \.json$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sw/libserial/Makefile	Fri Oct 11 22:37:11 2024 +0000
@@ -0,0 +1,13 @@
+CC=	gcc
+CFLAGS=	-O2
+OBJS=	nonblock.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/sw/libserial/nonblock.c	Fri Oct 11 22:37:11 2024 +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/sw/libserial/openport.c	Fri Oct 11 22:37:11 2024 +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/sw/libserial/setbaud.c	Fri Oct 11 22:37:11 2024 +0000
@@ -0,0 +1,33 @@
+/*
+ * This module implements the termios/ioctl setting of the baud rate.
+ */
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <asm/ioctls.h>
+#include <asm/termbits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+extern int target_fd;
+
+set_serial_baudrate(br)
+	unsigned br;
+{
+	struct termios2 target_termios;
+
+	target_termios.c_iflag = IGNBRK;
+	target_termios.c_oflag = 0;
+	target_termios.c_cflag = BOTHER|CLOCAL|HUPCL|CREAD|CS8;
+	target_termios.c_lflag = 0;
+	target_termios.c_cc[VMIN] = 1;
+	target_termios.c_cc[VTIME] = 0;
+	target_termios.c_ispeed = br;
+	target_termios.c_ospeed = br;
+	if (ioctl(target_fd, TCSETSF2, &target_termios) < 0) {
+		perror("TCSETSF2");
+		exit(1);
+	}
+	return 0;
+}