diff --git a/lib/ArduinoHexParse/README.md b/lib/ArduinoHexParse/README.md new file mode 100644 index 000000000..c5c6a6751 --- /dev/null +++ b/lib/ArduinoHexParse/README.md @@ -0,0 +1,3 @@ +# ArduinoHexParse + +Parse hex files created by Arduino for Uno/Mini/Nano diff --git a/lib/ArduinoHexParse/keywords.txt b/lib/ArduinoHexParse/keywords.txt new file mode 100644 index 000000000..c60688578 --- /dev/null +++ b/lib/ArduinoHexParse/keywords.txt @@ -0,0 +1,25 @@ +####################################### +# Syntax Coloring Map for ArduinoHexParse +# (esp8266) +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +ArduinoHexParse KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +ArduinoHexParse KEYWORD2 +ParseLine KEYWORD2 +GetFlashPage KEYWORD2 +GetLoadAddress KEYWORD2 +IsFlashPageReady KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + diff --git a/lib/ArduinoHexParse/library.json b/lib/ArduinoHexParse/library.json new file mode 100644 index 000000000..896b53f6d --- /dev/null +++ b/lib/ArduinoHexParse/library.json @@ -0,0 +1,12 @@ +{ + "name": "ArduinoHexParse", + "version": "0.0.1", + "description": "Parse hex files created by Arduino for Uno/Mini/Nano", + "repository": + { + "type": "git", + "url": "https://github.com/arendst/Sonoff-Tasmota/lib/ArduinoHexParse" + }, + "frameworks": "arduino", + "platforms": "espressif8266" +} diff --git a/lib/ArduinoHexParse/library.properties b/lib/ArduinoHexParse/library.properties new file mode 100644 index 000000000..034c22e64 --- /dev/null +++ b/lib/ArduinoHexParse/library.properties @@ -0,0 +1,9 @@ +name=ArduinoHexParse +version=0.0.1 +author=Andre Thomas +maintainer=Andre Thomas +sentence=Parse hex files created by Arduino for Uno/Mini/Nano +paragraph= +category=Signal Input/Output +url= +architectures=esp8266 diff --git a/lib/ArduinoHexParse/src/ArduinoHexParse.cpp b/lib/ArduinoHexParse/src/ArduinoHexParse.cpp new file mode 100644 index 000000000..d4125f2f5 --- /dev/null +++ b/lib/ArduinoHexParse/src/ArduinoHexParse.cpp @@ -0,0 +1,134 @@ +/* + Copyright (C) 2019 Andre Thomas and Theo Arends + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include "ArduinoHexParse.h" + +ArduinoHexParse::ArduinoHexParse(void) +{ + loadAddress[0] = 0; + loadAddress[1] = 0; +} + +void ArduinoHexParse::ParseLine(byte* hexline) +{ + recordType = GetRecordType(hexline); + if (0 == recordType) { + address = GetAddress(hexline); + len = GetLength(hexline); + GetData(hexline, len); + if (128 == PageMemIdx) { + if (!firstRun) { + loadAddress[1] += 0x40; + if (0 == loadAddress[1]) { + loadAddress[0] += 1; + } + } + firstRun = false; + FlashPageReady = true; + PageMemIdx = 0; + } + nextAddress = address + len; + } + if (1 == recordType) { + EndOfFile(); + FlashPageReady = true; + } +} + +bool ArduinoHexParse::IsFlashPageReady(void) +{ + return FlashPageReady; +} + +byte* ArduinoHexParse::GetFlashPage(void) +{ + FlashPageReady = false; + return FlashPage; +} + +byte* ArduinoHexParse::GetLoadAddress(void) +{ + return loadAddress; +} + +void ArduinoHexParse::GetLoadAddress(byte* hexline) +{ + char buff[3]; + buff[2] = '\0'; + buff[0] = hexline[3]; + buff[1] = hexline[4]; + loadAddress[0] = strtol(buff, 0, 16); + buff[0] = hexline[5]; + buff[1] = hexline[6]; + loadAddress[1] = strtol(buff, 0, 16); +} + +byte* ArduinoHexParse::GetData(byte* hexline, uint32_t len) +{ + uint32_t start = 9; + uint32_t end = (len * 2) + start; + char buff[3]; + buff[2] = '\0'; + for (uint32_t x = start; x < end; x = x+2) { + buff[0] = hexline[x]; + buff[1] = hexline[x+1]; + FlashPage[PageMemIdx] = strtol(buff, 0, 16); + PageMemIdx++; + } +} + +void ArduinoHexParse::EndOfFile(void) +{ + loadAddress[1] += 0x40; + if (0 == loadAddress[1]) { + loadAddress[0] += 1; + } + while (128 > PageMemIdx) { // Fill the remaing space in the memory page with 0xFF + FlashPage[PageMemIdx] = 0xFF; + PageMemIdx++; + } +} + +uint32_t ArduinoHexParse::GetAddress(byte* hexline) +{ + char buff[5]; + buff[0] = hexline[3]; + buff[1] = hexline[4]; + buff[2] = hexline[5]; + buff[3] = hexline[6]; + buff[4] = '\0'; + return strtol(buff, 0, 16); +} + +uint16_t ArduinoHexParse::GetLength(byte* hexline) +{ + char buff[3]; + buff[0] = hexline[1]; + buff[1] = hexline[2]; + buff[2] = '\0'; + return strtol(buff, 0, 16); +} + +uint16_t ArduinoHexParse::GetRecordType(byte* hexline) +{ + char buff[3]; + buff[0] = hexline[7]; + buff[1] = hexline[8]; + buff[2] = '\0'; + return strtol(buff, 0, 16); +} diff --git a/lib/ArduinoHexParse/src/ArduinoHexParse.h b/lib/ArduinoHexParse/src/ArduinoHexParse.h new file mode 100644 index 000000000..7b941eea1 --- /dev/null +++ b/lib/ArduinoHexParse/src/ArduinoHexParse.h @@ -0,0 +1,47 @@ +/* + Copyright (C) 2019 Andre Thomas and Theo Arends + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef __ARDUINOHEXPARSE_H__ + +#include + +class ArduinoHexParse { + public: + ArduinoHexParse(void); + void ParseLine(byte* data); + byte* GetFlashPage(void); + byte* GetLoadAddress(void); + bool IsFlashPageReady(void); + private: + uint32_t address = 0; + uint32_t len = 0; + uint32_t nextAddress = 0; + uint32_t PageMemIdx = 0; + uint32_t recordType = 0; + byte FlashPage[128]; + byte loadAddress[2]; + bool FlashPageReady = false; + bool firstRun = true; + uint32_t GetAddress(byte* hexline); + uint16_t GetLength(byte* hexline); + uint16_t GetRecordType(byte* hexline); + byte* GetData(byte* hexline, uint32_t len); + void GetLoadAddress(byte* hexline); + void EndOfFile(void); +}; + +#endif // __ARDUINOHEXPARSE_H__ \ No newline at end of file