Skip to content

Commit 415cac2

Browse files
committed
Adds support for only reading I8HEX (Intel HEX with 16 bit addresses) format.
1 parent 13c41b8 commit 415cac2

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ endif ()
5656

5757
set(QFM1000_APPLICATION_NAME "qFM1000")
5858
set(QFM1000_APPLICATION_VERSION_MAJOR "0")
59-
set(QFM1000_APPLICATION_VERSION_MINOR "4")
60-
set(QFM1000_APPLICATION_VERSION_PATCH "2")
59+
set(QFM1000_APPLICATION_VERSION_MINOR "5")
60+
set(QFM1000_APPLICATION_VERSION_PATCH "0")
6161

6262
set(QFM1000_ORGANIZATION_NAME "The HellNet.org")
6363
set(QFM1000_ORGANIZATION_DOMAIN "thehellnet.org")

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Windows build are created using the official releases of Qt 5.9.2. Just download
3030
## Changelog
3131

3232

33+
* 0.5.0
34+
* ADD: Support for reading files in I8HEX (Intel HEX with 16 bit addresses) format
3335
* 0.4.2
3436
* FIX: Arduino serial port in Windows
3537
* 0.4.1

src/eeprom/filemanager.cpp

+76-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@
2525

2626
bool FileManager::loadFromFile(EEPROM *eeprom, QString filename) {
2727
QFile file(filename);
28-
if (!file.exists() || file.size() != 2048)
28+
if (!file.exists())
2929
return false;
3030

3131
if (!file.open(QIODevice::ReadOnly))
3232
return false;
3333

34-
QByteArray data = file.readAll();
34+
QByteArray rawData = file.readAll();
35+
QByteArray data = parseFile(rawData);
3536
file.close();
3637

38+
if (data.length() != 2048)
39+
return false;
40+
3741
eeprom->setData(data);
3842

3943
return true;
@@ -50,3 +54,73 @@ bool FileManager::saveToFile(EEPROM *eeprom, QString filename) {
5054

5155
return true;
5256
}
57+
58+
QByteArray FileManager::parseFile(const QByteArray &rawData) {
59+
QByteArray data;
60+
61+
if (rawData.size() == 2048) {
62+
data = rawData;
63+
} else if (isIntelHex(rawData)) {
64+
data = intelHexToByteArray(rawData);
65+
}
66+
67+
return data;
68+
}
69+
70+
bool FileManager::isIntelHex(const QByteArray &rawFile) {
71+
QStringList rows = splitInLines(rawFile);
72+
73+
for (const QString &row : rows)
74+
if (!row.startsWith(":"))
75+
return false;
76+
77+
return rows.last() == ":00000001FF";
78+
79+
}
80+
81+
QStringList FileManager::splitInLines(const QByteArray &rawData) {
82+
QString rawFileString = QString::fromLatin1(rawData);
83+
QStringList rows = rawFileString.replace('\r', '\n').split('\n', QString::SkipEmptyParts);
84+
return rows;
85+
}
86+
87+
QByteArray FileManager::intelHexToByteArray(const QByteArray &rawData) {
88+
QStringList rows = splitInLines(rawData);
89+
90+
QByteArray data;
91+
int previousOffest = 0;
92+
93+
for (const QString &row : rows) {
94+
unsigned int checksum = 0;
95+
for (auto &c : QByteArray::fromHex(row.toLatin1()))
96+
checksum += (unsigned char) c;
97+
98+
checksum &= 0b11111111;
99+
100+
if (checksum != 0)
101+
return QByteArray();
102+
103+
int size = row.mid(1, 2).toInt(nullptr, 16);
104+
int offset = row.mid(3, 4).toInt(nullptr, 16);
105+
int type = row.mid(7, 2).toInt(nullptr, 16);
106+
QByteArray lineData = QByteArray::fromHex(row.mid(9, size * 2).toLatin1());
107+
108+
if (type == 0x01)
109+
break;
110+
if (type != 0x00)
111+
continue;
112+
113+
if (offset != previousOffest)
114+
return QByteArray();
115+
116+
previousOffest = offset + size;
117+
118+
119+
if (lineData.size() != size)
120+
return QByteArray();
121+
122+
data.append(lineData);
123+
}
124+
125+
return data;
126+
}

src/eeprom/filemanager.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ class FileManager {
3232
static bool loadFromFile(EEPROM *eeprom, QString filename);
3333

3434
static bool saveToFile(EEPROM *eeprom, QString filename);
35+
36+
private:
37+
static QByteArray parseFile(const QByteArray &rawData);
38+
39+
static bool isIntelHex(const QByteArray &rawFile);
40+
41+
static QStringList splitInLines(const QByteArray &rawData);
42+
43+
static QByteArray intelHexToByteArray(const QByteArray &rawData);
3544
};
3645

3746
#endif

0 commit comments

Comments
 (0)