-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy patharduino_intelhex.cpp
More file actions
65 lines (57 loc) · 2.15 KB
/
Copy patharduino_intelhex.cpp
File metadata and controls
65 lines (57 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "arduino_intelhex.h"
void ArduinoIntelHex::loadIntelHex(QString filename)
{
this->resize(1024*1024);
QFile intelHex(filename);
intelHex.open(QIODevice::ReadOnly);
QTextStream in(&intelHex);
int optionoffset = 0;
int total = 0;
int linenum = 0;
bool endFound = false;
while (!in.atEnd()) {
QString line = in.readLine();
if (line.startsWith(":")) {
linenum++;
bool ok;
int length = QString("0x" + line.mid(1, 2)).toUInt(&ok, 16);
int positionA = QString("0x" + line.mid(3, 2)).toUInt(&ok, 16);
int positionB = QString("0x" + line.mid(5, 2)).toUInt(&ok, 16);
int position = (positionA * 256) + positionB;
int option = QString("0x" + line.mid(7, 2)).toUInt(&ok, 16);
int checksum = QString("0x" + line.right(2)).toUInt(&ok, 16);
unsigned char checksumact = 0;
for (int i = 0; i < ((line.length() - 3) / 2); i++) {
checksumact += QString("0x" + line.mid(i * 2 + 1, 2)).toUInt(&ok, 16);
}
if (checksum != (0x100 - checksumact) && checksumact != 0) {
qDebug()<<checksum<<checksumact<<(0x100 - checksumact);
qWarning()<<"Wrong checksum on "<<linenum;
this->clear();
return;
}
//Data
if (option == 0) {
QByteArray tmp = QByteArray::fromHex(line.mid(9, length * 2).toLatin1());
total = optionoffset + position + tmp.length();
this->insert(optionoffset + position, tmp);
}
else if (option == 2)
{
int optionoffsetA = QString("0x" + line.mid(9, 2)).toUInt(&ok, 16);
int optionoffsetB = QString("0x" + line.mid(11, 2)).toUInt(&ok, 16);
optionoffset = ((optionoffsetA * 256) + optionoffsetB) << 4;
}
//End
else if (option == 1) {
endFound = true;
}
}
}
if (!endFound) {
qWarning()<<"No end found";
this->clear();
return;
}
this->resize(total);
}