Skip to content

Commit 397ccbf

Browse files
committed
Added cli-commands and keywords
1 parent e1eb241 commit 397ccbf

File tree

5 files changed

+171
-2
lines changed

5 files changed

+171
-2
lines changed

QCodeEditor/resources/languages/lisp.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,5 +450,8 @@
450450
<name>f-ls</name>
451451
<name>f-size</name>
452452
<name>f-fatinfo</name>
453+
<name>fw-erase</name>
454+
<name>fw-write</name>
455+
<name>fw-reboot</name>
453456
</section>
454457
</root>

main.cpp

Lines changed: 141 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "codeloader.h"
3030
#include "configparam.h"
3131
#include "utility.h"
32+
#include "heatshrink/heatshrinkif.h"
3233

3334
#include <QApplication>
3435
#include <QStyleFactory>
@@ -101,6 +102,8 @@ static void showHelp()
101102
qDebug() << "--eraseLisp : Erase lisp-script.";
102103
qDebug() << "--uploadFirmware [path] : Upload firmware-file from path.";
103104
qDebug() << "--uploadBootloaderBuiltin : Upload bootloader from generic included bootloaders.";
105+
qDebug() << "--createFirmwareForBootloader [fileIn:fileOut] : Generate firmware-file compatible with the bootloader. ";
106+
qDebug() << "--writeFileToSdCard [fileLocal:pathSdcard] : Write file to SD-card.";
104107
}
105108

106109
#ifdef Q_OS_LINUX
@@ -295,6 +298,10 @@ int main(int argc, char *argv[])
295298
bool eraseLisp = false;
296299
QString firmwarePath = "";
297300
bool uploadBootloaderBuiltin = false;
301+
QString fwForBootloaderIn = "";
302+
QString fwForBootloaderOut = "";
303+
QString fileForSdIn = "";
304+
QString fileForSdOut = "";
298305

299306
// Arguments can be hard-coded in a build like this:
300307
// qmlWindowSize = QSize(400, 800);
@@ -590,6 +597,46 @@ int main(int argc, char *argv[])
590597
}
591598
}
592599

600+
if (str == "--createFirmwareForBootloader") {
601+
if ((i + 1) < args.size()) {
602+
i++;
603+
auto p = args.at(i).split(":");
604+
if (p.size() == 2) {
605+
fwForBootloaderIn = p.at(0);
606+
fwForBootloaderOut = p.at(1);
607+
} else {
608+
qCritical() << "Invalid paths specified";
609+
return 1;
610+
}
611+
612+
found = true;
613+
} else {
614+
i++;
615+
qCritical() << "No paths specified";
616+
return 1;
617+
}
618+
}
619+
620+
if (str == "--writeFileToSdCard") {
621+
if ((i + 1) < args.size()) {
622+
i++;
623+
auto p = args.at(i).split(":");
624+
if (p.size() == 2) {
625+
fileForSdIn = p.at(0);
626+
fileForSdOut = p.at(1);
627+
} else {
628+
qCritical() << "Invalid paths specified";
629+
return 1;
630+
}
631+
632+
found = true;
633+
} else {
634+
i++;
635+
qCritical() << "No paths specified";
636+
return 1;
637+
}
638+
}
639+
593640
if (!found) {
594641
if (dash) {
595642
qCritical() << "At least one of the flags is invalid:" << str;
@@ -628,6 +675,68 @@ int main(int argc, char *argv[])
628675
return 0;
629676
}
630677

678+
if (!fwForBootloaderIn.isEmpty()) {
679+
QFile fIn(fwForBootloaderIn);
680+
if (!fIn.open(QIODevice::ReadOnly)) {
681+
qWarning() << QString("Could not open %1 for reading.").arg(fwForBootloaderIn);
682+
return 1;
683+
}
684+
685+
QFile fOut(fwForBootloaderOut);
686+
if (!fOut.open(QIODevice::WriteOnly)) {
687+
qWarning() << QString("Could not open %1 for writing.").arg(fwForBootloaderOut);
688+
return 1;
689+
}
690+
691+
QByteArray newFirmware = fIn.readAll();
692+
fIn.close();
693+
694+
int szTot = newFirmware.size();
695+
696+
bool useHeatshrink = false;
697+
if (szTot > 393208 && szTot < 700000) { // If fw is much larger it is probably for the esp32
698+
useHeatshrink = true;
699+
qDebug() << "Firmware is big, using heatshrink compression library";
700+
int szOld = szTot;
701+
HeatshrinkIf hs;
702+
newFirmware = hs.encode(newFirmware);
703+
szTot = newFirmware.size();
704+
qDebug() << "New size:" << szTot << "(" << 100.0 * (double)szTot / (double)szOld << "%)";
705+
706+
if (szTot > 393208) {
707+
qWarning() << "Firmware too big" <<
708+
"The firmware you are trying to upload is too large for the bootloader even after compression.";
709+
return -1;
710+
}
711+
}
712+
713+
if (szTot > 5000000) {
714+
qWarning() << "Firmware too big" <<
715+
"The firmware you are trying to upload is unreasonably "
716+
"large, most likely it is an invalid file";
717+
return -2;
718+
}
719+
720+
quint16 crc = Packet::crc16((const unsigned char*)newFirmware.constData(),
721+
uint32_t(newFirmware.size()));
722+
VByteArray sizeCrc;
723+
if (useHeatshrink) {
724+
uint32_t szShift = 0xCC;
725+
szShift <<= 24;
726+
szShift |= szTot;
727+
sizeCrc.vbAppendUint32(szShift);
728+
} else {
729+
sizeCrc.vbAppendUint32(szTot);
730+
}
731+
sizeCrc.vbAppendUint16(crc);
732+
newFirmware.prepend(sizeCrc);
733+
fOut.write(newFirmware);
734+
fOut.close();
735+
736+
qDebug() << "Done!";
737+
return 0;
738+
}
739+
631740
if (!pkgArgs.isEmpty()) {
632741
if (pkgArgs.size() < 4) {
633742
qWarning() << "Invalid arguments";
@@ -795,7 +904,8 @@ int main(int argc, char *argv[])
795904
bool isCustomConf = !getCustomConfPath.isEmpty() || !setCustomConfPath.isEmpty();
796905

797906
if (isMcConf || isAppConf || isCustomConf || !lispPath.isEmpty() ||
798-
eraseLisp || !firmwarePath.isEmpty() || uploadBootloaderBuiltin) {
907+
eraseLisp || !firmwarePath.isEmpty() || uploadBootloaderBuiltin ||
908+
!fileForSdIn.isEmpty()) {
799909
qputenv("QT_QPA_PLATFORM", "offscreen");
800910
app = new QCoreApplication(argc, argv);
801911
vesc = new VescInterface;
@@ -842,6 +952,15 @@ int main(int argc, char *argv[])
842952
}
843953
});
844954

955+
QObject::connect(vesc->commands(), &Commands::fileProgress, []
956+
(int32_t prog, int32_t tot, double percentage, double bytesPerSec) {
957+
(void)prog;
958+
(void)tot;
959+
960+
fprintf(stderr, "%s", QString("\rUpload progress: %1% (%2 kbps)").
961+
arg(floor(percentage)).arg(bytesPerSec / 1024).toLatin1().data());
962+
});
963+
845964
QTimer::singleShot(10, [&]() {
846965
int exitCode = 0;
847966
bool ok = false;
@@ -910,6 +1029,27 @@ int main(int argc, char *argv[])
9101029
}
9111030
}
9121031

1032+
if (!fileForSdIn.isEmpty()) {
1033+
QFile f(fileForSdIn);
1034+
QFileInfo fi(f);
1035+
if (f.open(QIODevice::ReadOnly)) {
1036+
QFileInfo fi(f);
1037+
vesc->commands()->fileBlockMkdir(fileForSdOut);
1038+
QString target = fileForSdOut + "/" + fi.fileName();
1039+
if (vesc->commands()->fileBlockWrite(target.replace("//", "/"), f.readAll())) {
1040+
qDebug() << "Done!";
1041+
} else {
1042+
qWarning() << "Could not write file";
1043+
exitCode = -51;
1044+
}
1045+
1046+
f.close();
1047+
} else {
1048+
qWarning() << "Could not open file for reading.";
1049+
exitCode = -50;
1050+
}
1051+
}
1052+
9131053
if (isMcConf || isAppConf || isCustomConf) {
9141054
bool res = vesc->customConfigRxDone();
9151055
if (!res) {

res/Lisp/Examples/fw_update.lisp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(def f (f-open "fws/str500.boot" "r"))
2+
(def can-id 93)
3+
4+
;(def f (f-open "fws/express.boot" "r"))
5+
;(def can-id -1)
6+
7+
(def fwsize (f-size f))
8+
9+
(print "erase")
10+
(print (list "Erase res" (fw-erase (f-size f) can-id)))
11+
12+
(def offset 0)
13+
(loopwhile t {
14+
(var data (f-read f 256))
15+
(if (eq data nil) {
16+
(print "Upload done")
17+
(break)
18+
})
19+
20+
(fw-write offset data can-id)
21+
(setq offset (+ offset (buflen data)))
22+
(print (list "Progress" (floor (* 100 (/ (to-float offset) fwsize)))))
23+
})
24+
25+
(fw-reboot can-id)

res_lisp.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@
4444
<file>res/Lisp/Examples/crc16_v2.lisp</file>
4545
<file>res/Lisp/Examples/speed_test_tak_q2.lisp</file>
4646
<file>res/Lisp/Examples/log_vesc_tool.lisp</file>
47+
<file>res/Lisp/Examples/fw_update.lisp</file>
4748
</qresource>
4849
</RCC>

vescinterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ bool VescInterface::fwUpload(QByteArray &newFirmware, bool isBootloader, bool fw
15451545
QByteArray in = newFirmware.mid(0, sz);
15461546

15471547
bool hasData = false;
1548-
for (auto b: in) {
1548+
foreach (auto b, in) {
15491549
if (b != (char)0xff) {
15501550
hasData = true;
15511551
break;

0 commit comments

Comments
 (0)