From 793df682b195733a9fe1c68aad2db2ab75155a8e Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sun, 22 Jan 2017 13:16:38 +0000 Subject: [PATCH] - added git-svn-id: http://moon:8086/svn/mips@118 a8ebac50-d88d-4704-bea3-6648445a41b3 --- src/bootloader/srec.c | 98 +++++++++++++++++++++++++++++++++++++++++++ src/bootloader/srec.h | 45 ++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/bootloader/srec.c create mode 100644 src/bootloader/srec.h diff --git a/src/bootloader/srec.c b/src/bootloader/srec.c new file mode 100644 index 0000000..9ddd58e --- /dev/null +++ b/src/bootloader/srec.c @@ -0,0 +1,98 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +#include "srec.h" + +uint8_t h2i(uint8_t *c) +{ + int i; + uint8_t t, b = 0; + + for (i=0; i < 2; i++) + { + t = c[i]; + if (t >= 'A') + t = t - 'A' + 10; + else + t -= '0'; + + b = (b << 4) | t; + } + return b; +} + +int srec_decode(srec_t *pRec, uint8_t *pBuf, int buflen) +{ + uint8_t chksum, byte; + int alen, i; + + if (!buflen) + return srec_err; + + pRec->type = srec_rec; + alen = 0; + if ((pBuf[1] > '0') && (pBuf[1] < '4')) + { + alen = pBuf[1] - '0' + 1; + pRec->type = srec_data; + } + else if ((pBuf[1] >= '7') && (pBuf[1] <= '9')) + { + alen = 11 - (pBuf[1] - '0'); + pRec->type = srec_eob; + } + else if (pBuf[1] == '0') + { + alen = 2; + pRec->type = srec_sob; + } + + byte = h2i(&pBuf[2]); + pRec->size = byte; + chksum = byte; + + pRec->addr = 0; + for (i=0; i < alen; i++) + { + byte = h2i(&pBuf[4+2*i]); + pRec->addr = pRec->addr << 8 | byte; + chksum += byte; + } + pRec->size -= (alen+1); + + pRec->data = (uint8_t*) &pBuf[4 + 2*alen]; + for (i=0; i < (int)pRec->size; i++) + { + byte = h2i(&pRec->data[2*i]); + pRec->data[i] = byte; + chksum += byte; + } + chksum = ~chksum; + byte = h2i(&pRec->data[2*i]); + if (chksum != byte) + return 0; + + return pRec->type; +} + +int srec_getline(uint8_t *pLine) +{ + char c; + int i = 0; + + do + { + c = readchar(); + + } while ((c != 's') && (c != 'S')); + + while(((c != 0x0D) && (c != 0x0A))) + { + pLine[i++] = c; + c = readchar(); + }; + + return i; +} diff --git a/src/bootloader/srec.h b/src/bootloader/srec.h new file mode 100644 index 0000000..b8032ea --- /dev/null +++ b/src/bootloader/srec.h @@ -0,0 +1,45 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/* + * File: srec.h + * Author: jens + * + * Created on 22. Januar 2017, 14:10 + */ + +#ifndef SREC_H +#define SREC_H + +#include + +typedef struct _ssrec_t +{ + uint32_t addr; + uint32_t size; + uint32_t type; + uint8_t *data; + +} srec_t; + +enum srec_type +{ + srec_err, srec_sob, srec_data, srec_rec, srec_eob +}; + +#ifdef __cplusplus +extern "C" { +#endif + +int srec_decode(srec_t *pRec, uint8_t *pBuf, int buflen); +int srec_getline(uint8_t *pLine); + +#ifdef __cplusplus +} +#endif + +#endif /* SREC_H */ +