FreeCalypso > hg > freecalypso-sw
view loadtools/srecreader.c @ 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 |
line wrap: on
line source
/* * This module contains the functions for reading S-record files. */ #include <sys/types.h> #include <stdint.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); } 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); }