diff loadtools/sercomm.c @ 0:e7502631a0f9

initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Jun 2016 00:13:35 +0000
parents
children e7d5ce499693
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loadtools/sercomm.c	Sat Jun 11 00:13:35 2016 +0000
@@ -0,0 +1,87 @@
+/*
+ * This module handles the establishment of serial communication
+ * with the target, i.e., the host-side termios stuff.
+ */
+
+#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>
+#include "baudrate.h"
+
+char *target_ttydev;
+int target_fd;
+struct termios target_termios;
+
+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 usb2serial IC */
+	{"812500",	B921600,	-1},
+	{"406250",	B460800,	-1},
+	{"203125",	B230400,	-1},
+	/* table search terminator */
+	{NULL,		B0,		-1},
+};
+struct baudrate *current_baud_rate;
+
+open_target_serial()
+{
+	target_fd = open(target_ttydev, O_RDWR|O_NONBLOCK);
+	if (target_fd < 0) {
+		perror(target_ttydev);
+		exit(1);
+	}
+	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;
+	/* start at B19200, as that's what we'll need to use initially */
+	cfsetispeed(&target_termios, B19200);
+	cfsetospeed(&target_termios, B19200);
+	if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
+		perror("initial tcsetattr on target");
+		exit(1);
+	}
+	return 0;
+}
+
+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);
+	}
+}
+
+switch_baud_rate(br)
+	struct baudrate *br;
+{
+	cfsetispeed(&target_termios, br->termios_code);
+	cfsetospeed(&target_termios, br->termios_code);
+	if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
+		perror("tcsetattr to switch baud rate");
+		exit(1);
+	}
+	current_baud_rate = br;
+}