#include #include "string.h" #include "endien.h" #include "intelhex.h" static uint8_t hexval (const char c) { if ('0' <= c && c <= '9') return (uint8_t) c - '0'; else if ('a' <= c && c <= 'f') return (uint8_t) c - 'a' + 10; else return 0; } static unsigned to_hex_str (char * str, const uint8_t * data, const unsigned len) { unsigned result = 0; const char * hexstr = "0123456789ABCDEF"; for (unsigned n=0; n> 4]; str [result++] = hexstr [c & 0xF]; } return result; } static uint8_t chk_sum (const uint8_t * data, const unsigned len) { uint8_t sum = 0; for (unsigned n=0; n> 1) + 1]; uint8_t val = 0; unsigned k = 0; for (unsigned n=1; n> 12); // CS:IP ? čerti vědí, jak je to myšleno offset = hdr.load.get(); break; default: break; } return t; } bool IntelHex::CompareAckOffsset() { //printf ("ofs: %04X=%04X\n", ackofs, offset); if (ackofs == offset) return true; return false; } void IntelHex::getAddress (uint32_t & adr) { adr = address; } void IntelHex::getLenght (uint32_t & len) { len = total_lenght; } void IntelHex::getOffset (uint32_t & ofs) { ofs = offset; } void IntelHex::WriteData (uint8_t * data) { memcpy (data + offset, chunk, lenght); offset += lenght; } uint8_t * IntelHex::getDataRow (uint32_t & ofs, uint32_t & len) { ofs = offset; len = lenght; return chunk; } void IntelHex::AddOffset (uint32_t len) { offset += len; } uint32_t IntelHex::ElaRecord (char * string, const uint32_t adr) { offset = 0; uint32_t result = 0; string [result++] = ':'; UlbaAddr lba; IhxHeader hdr; hdr.load.set(0); hdr.reclen = sizeof (lba); hdr.rectyp = elaRecord; lba.addr.set (adr >> 16); uint8_t chk = 0; chk += chk_sum (hdr.bytes, sizeof(IhxHeader)); chk += chk_sum (lba.bytes, sizeof(UlbaAddr )); chk = 0 - chk; result += to_hex_str (string + result, hdr.bytes, sizeof(IhxHeader)); result += to_hex_str (string + result, lba.bytes, sizeof(UlbaAddr )); result += to_hex_str (string + result, & chk, 1); string [result++] = '\r'; string [result++] = '\n'; string [result] = '\0'; return result; } uint32_t IntelHex::SlaRecord (char* string, const uint32_t adr) { offset = 0; uint32_t result = 0; string [result++] = ':'; EipAddr lba; IhxHeader hdr; hdr.load.set(0); hdr.reclen = sizeof (lba); hdr.rectyp = slaRecord; lba.addr.set (adr); uint8_t chk = 0; chk += chk_sum (hdr.bytes, sizeof(IhxHeader)); chk += chk_sum (lba.bytes, sizeof(EipAddr )); chk = 0 - chk; result += to_hex_str (string + result, hdr.bytes, sizeof(IhxHeader)); result += to_hex_str (string + result, lba.bytes, sizeof(EipAddr )); result += to_hex_str (string + result, & chk, 1); string [result++] = '\r'; string [result++] = '\n'; string [result] = '\0'; return result; } uint32_t IntelHex::DataRecord (char * string, const uint8_t * data, const uint32_t len) { uint32_t result = 0; string [result++] = ':'; IhxHeader hdr; hdr.load.set (offset); hdr.reclen = len; hdr.rectyp = dataRecord; uint8_t chk = 0; chk += chk_sum (hdr.bytes, sizeof(IhxHeader)); chk += chk_sum (data, len); chk = 0 - chk; result += to_hex_str (string + result, hdr.bytes, sizeof(IhxHeader)); if (len) result += to_hex_str (string + result, data, len); result += to_hex_str (string + result, & chk, 1); string [result++] = '\r'; string [result++] = '\n'; string [result] = '\0'; return result; } uint32_t IntelHex::EofRecord (char * string) { offset = 0; const char * eof = ":00000001FF\r\n"; const uint32_t result = strlen (eof); memcpy (string, eof, result); string [result] = '\0'; return result; } uint32_t IntelHex::BTxRecord (char * string, RowTypes t) { uint32_t result = 0; string [result++] = ':'; IhxHeader hdr; hdr.load.set (offset); hdr.reclen = 0; hdr.rectyp = t; uint8_t chk = 0; chk += chk_sum (hdr.bytes, sizeof(IhxHeader)); chk = 0 - chk; result += to_hex_str (string + result, hdr.bytes, sizeof(IhxHeader)); result += to_hex_str (string + result, & chk, 1); string [result++] = '\r'; string [result++] = '\n'; string [result] = '\0'; //offset += len; return result; }