changeset 7:aa1f6fe16fef

loadtools building blocks started
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 30 Apr 2013 07:19:48 +0000
parents 5eaafa83be60
children acaac9162574
files loadtools/hexdecode.c loadtools/sercomm.c loadtools/srecreader.c loadtools/srecreader.h loadtools/ttypassthru.c
diffstat 5 files changed, 188 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loadtools/hexdecode.c	Tue Apr 30 07:19:48 2013 +0000
@@ -0,0 +1,30 @@
+/*
+ * This module contains the decode_hex_byte() function,
+ * which is used by the SREC file reader and will likely be used
+ * by other code as well, such as the dump-to-file function
+ * of loadtool.
+ */
+
+#include <ctype.h>
+
+decode_hex_byte(s)
+	char *s;
+{
+	register int u, l;
+
+	if (!isxdigit(s[0]) || !isxdigit(s[1]))
+		return(-1);
+	if (isdigit(s[0]))
+		u = s[0] - '0';
+	else if (isupper(s[0]))
+		u = s[0] - 'A' + 10;
+	else
+		u = s[0] - 'a' + 10;
+	if (isdigit(s[1]))
+		l = s[1] - '0';
+	else if (isupper(s[1]))
+		l = s[1] - 'A' + 10;
+	else
+		l = s[1] - 'a' + 10;
+	return((u << 4) | l);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loadtools/sercomm.c	Tue Apr 30 07:19:48 2013 +0000
@@ -0,0 +1,39 @@
+/*
+ * 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 <unistd.h>
+
+char *target_ttydev;
+int target_fd;
+struct termios target_termios;
+
+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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loadtools/srecreader.c	Tue Apr 30 07:19:48 2013 +0000
@@ -0,0 +1,28 @@
+/*
+ * This module contains the functions for reading S-record files.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <strings.h>
+#include "srecreader.h"
+
+open_srec_file(sr)
+	struct srecreader *sr;
+{
+	sr->openfile = fopen(sr->filename, "r");
+	if (!sr->openfile) {
+		perror(sr->filename);
+		return(-1);
+	}
+	sr->lineno = 0;
+	return(0);
+}
+
+read_s_record(sr)
+	struct srecreader *sr;
+{
+
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loadtools/srecreader.h	Tue Apr 30 07:19:48 2013 +0000
@@ -0,0 +1,11 @@
+/* this header file defines the data structures for the SREC reader module */
+
+struct srecreader {
+	char	*filename;
+	FILE	*openfile;
+	int	lineno;
+	u_char	record[256];	/* binary */
+	char	record_type;	/* ASCII char */
+	u_char	datalen;
+	u_long	addr;
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loadtools/ttypassthru.c	Tue Apr 30 07:19:48 2013 +0000
@@ -0,0 +1,80 @@
+/*
+ * This module implements the pass-thru operation mode, in which
+ * the Unix host tty is cross-connected directly to the target
+ * running some code we have just loaded.
+ */
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/errno.h>
+#include <termios.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <strings.h>
+
+extern int errno;
+
+extern int target_fd;
+
+static struct termios saved_termios, my_termios;
+
+static void
+loop()
+{
+	char buf[BUFSIZ];
+	fd_set fds, fds1;
+	register int i, cc, max;
+
+	FD_ZERO(&fds);
+	FD_SET(0, &fds);
+	FD_SET(target_fd, &fds);
+	max = target_fd + 1;
+	for (;;) {
+		bcopy(&fds, &fds1, sizeof(fd_set));
+		i = select(max, &fds1, NULL, NULL, NULL);
+		if (i < 0) {
+			if (errno == EINTR)
+				continue;
+			tcsetattr(0, TCSAFLUSH, &saved_termios);
+			perror("select");
+			exit(1);
+		}
+		if (FD_ISSET(0, &fds1)) {
+			cc = read(0, buf, sizeof buf);
+			if (cc <= 0)
+				return;
+			if (cc == 1 && buf[0] == 0x1C)
+				return;
+			write(target_fd, buf, cc);
+		}
+		if (FD_ISSET(target_fd, &fds1)) {
+			cc = read(target_fd, buf, sizeof buf);
+			if (cc <= 0) {
+				tcsetattr(0, TCSAFLUSH, &saved_termios);
+				fprintf(stderr, "EOF/error on target tty\n");
+				exit(1);
+			}
+			write(1, buf, cc);
+		}
+	}
+}
+
+tty_passthru()
+{
+	static int zero = 0;
+
+	ioctl(target_fd, FIONBIO, &zero);
+
+	tcgetattr(0, &saved_termios);
+	bcopy(&saved_termios, &my_termios, sizeof(struct termios));
+	cfmakeraw(&my_termios);
+	my_termios.c_cc[VMIN] = 1;
+	my_termios.c_cc[VTIME] = 0;
+	tcsetattr(0, TCSAFLUSH, &my_termios);
+
+	printf("Entering tty pass-thru; type ^\\ to exit\r\n");
+	loop();
+	tcsetattr(0, TCSAFLUSH, &saved_termios);
+	return 0;
+}