changeset 8:acaac9162574

loadtools modules coming along
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 30 Apr 2013 21:35:14 +0000
parents aa1f6fe16fef
children fea204bc7674
files loadtools/Makefile loadtools/srecreader.c loadtools/srecreader.h
diffstat 3 files changed, 78 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loadtools/Makefile	Tue Apr 30 21:35:14 2013 +0000
@@ -0,0 +1,8 @@
+CC=	gcc
+CFLAGS=	-O2
+OBJS=	hexdecode.o sercomm.o srecreader.o ttypassthru.o
+
+all:	${OBJS}
+
+clean:
+	rm -f *.o *.out *errs
--- a/loadtools/srecreader.c	Tue Apr 30 07:19:48 2013 +0000
+++ b/loadtools/srecreader.c	Tue Apr 30 21:35:14 2013 +0000
@@ -3,6 +3,7 @@
  */
 
 #include <sys/types.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <strings.h>
@@ -20,9 +21,70 @@
 	return(0);
 }
 
+static
+srec2bin(sr, asciiline)
+	struct srecreader *sr;
+	char *asciiline;
+{
+	register int i, l, b;
+
+	l = decode_hex_byte(asciiline + 2);
+	if (l < 1) {
+		fprintf(stderr, "%s line %d: S-record length octet is bad\n",
+			sr->filename, sr->lineno);
+		return(-1);
+	}
+	sr->record[0] = l;
+	for (i = 1; i <= l; i++) {
+		b = decode_hex_byte(asciiline + i*2 + 2);
+		if (b < 0) {
+			fprintf(stderr,
+				"%s line %d: S-record hex decode error\n",
+				sr->filename, sr->lineno);
+			return(-1);
+		}
+		sr->record[i] = b;
+	}
+	return(0);
+}
+
+static
+srec_cksum(sr)
+	struct srecreader *sr;
+{
+	u_char accum;
+	register int i, len;
+
+	len = sr->record[0] + 1;
+	accum = 0;
+	for (i = 0; i < len; i++)
+		accum += sr->record[i];
+	if (accum != 0xFF) {
+		fprintf(stderr, "%s line %d: bad S-record checksum\n",
+			sr->filename, sr->lineno);
+		return(-1);
+	}
+	return(0);
+}
+
 read_s_record(sr)
 	struct srecreader *sr;
 {
-
+	char asciiline[1024];
 
+	if (!fgets(asciiline, sizeof asciiline, sr->openfile)) {
+		fprintf(stderr, "%s: premature EOF after %d S-records\n",
+			sr->filename, sr->lineno);
+		return(-1);
+	}
+	sr->lineno++;
+	if (asciiline[0] != 'S' || !isdigit(asciiline[1])) {
+		fprintf(stderr, "%s line %d: S-record expected\n",
+			sr->filename, sr->lineno);
+		return(-1);
+	}
+	sr->record_type = asciiline[1];
+	if (srec2bin(sr, asciiline) < 0)
+		return(-1);
+	return srec_cksum(sr);
 }
--- a/loadtools/srecreader.h	Tue Apr 30 07:19:48 2013 +0000
+++ b/loadtools/srecreader.h	Tue Apr 30 21:35:14 2013 +0000
@@ -1,11 +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;
+	char		*filename;
+	FILE		*openfile;
+	int		lineno;
+	u_char		record[256];	/* binary */
+	char		record_type;	/* ASCII char */
+	u_char		datalen;
+	uint32_t	addr;
 };