FreeCalypso > hg > freecalypso-tools
view loadtools/srecreader.c @ 995:74024eb17e04
fc-loadtool help: improve language regarding 16 MiB flash chips
In FC project history, 16 MiB flash originally meant Pirelli DP-L10.
Then we got FCDEV3B with the same flash (our own design), but now we are
discovering more Calypso devices that used such large flash, both late
Calypso era (Sony Ericsson K2x0) as well as much earlier ones (FIC FLUID
devices.txt file with 2004 dates, Leonardo+ rev 5). Hence we need to
migrate to more generic or neutral language in associated documentation,
without giving elevated status to specific examples that drove our
early project history.
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 03 Dec 2023 21:11:12 +0000 |
parents | e7502631a0f9 |
children |
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); } s3s7_get_addr_data(sr) struct srecreader *sr; { if (sr->record[0] < 5) { fprintf(stderr, "%s line %d: S%c record is too short\n", sr->filename, sr->lineno, sr->record_type); return(-1); } sr->datalen = sr->record[0] - 5; sr->addr = ((uint32_t)sr->record[1] << 24) | ((uint32_t)sr->record[2] << 16) | ((uint32_t)sr->record[3] << 8) | (uint32_t)sr->record[4]; return(0); }