- added
git-svn-id: http://moon:8086/svn/mips@118 a8ebac50-d88d-4704-bea3-6648445a41b3
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
@@ -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 <stdint.h>
|
||||||
|
|
||||||
|
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 */
|
||||||
|
|
||||||
Reference in New Issue
Block a user