LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / ground / gcs / src / experimental / USB_UPLOAD_TOOL / main.cpp
blobb52a51ba29a9b413e1da47b4af77ac8abd7a4349
1 #include <QtCore/QCoreApplication>
2 #include <QThread>
3 #include "op_dfu.h"
4 #include <QStringList>
6 void showProgress(QString status);
7 void progressUpdated(int percent);
8 void usage(QTextStream *standardOutput);
9 QString label;
10 int main(int argc, char *argv[])
12 QCoreApplication a(argc, argv);
13 QTextStream standardOutput(stdout);
14 int argumentCount = QCoreApplication::arguments().size();
15 bool use_serial = false;
16 bool verify;
17 bool debug = false;
18 bool umodereset = false;
19 OP_DFU::Actions action;
20 QString file;
21 QString serialport;
22 QString description;
23 int device = -1;
24 QStringList args = QCoreApplication::arguments();
26 if (args.contains("-debug")) {
27 debug = true;
29 if (args.contains("-ur")) {
30 umodereset = true;
32 standardOutput << "OpenPilot serial firmware uploader tool." << endl;
33 if (args.indexOf(PROGRAMFW) + 1 < args.length()) {
34 file = args[args.indexOf(PROGRAMFW) + 1];
36 if (args.contains(DEVICE)) {
37 if (args.indexOf(DEVICE) + 1 < args.length()) {
38 device = (args[args.indexOf(DEVICE) + 1]).toInt();
40 } else {
41 device = 0;
44 if (argumentCount == 0 || args.contains("-?") || !args.contains(USE_SERIAL)) {
45 usage(&standardOutput);
46 return -1;
47 } else if (args.contains(PROGRAMFW)) {
48 if (args.contains(VERIFY)) {
49 verify = true;
50 } else {
51 verify = false;
53 if (args.contains(PROGRAMDESC)) {
54 if (args.indexOf(PROGRAMDESC) + 1 < args.length()) {
55 description = (args[args.indexOf(PROGRAMDESC) + 1]);
58 action = OP_DFU::actionProgram;
59 } else if (args.contains(COMPARECRC) || args.contains(COMPAREALL)) {
60 int index;
61 if (args.contains(COMPARECRC)) {
62 index = args.indexOf(COMPARECRC);
63 action = OP_DFU::actionCompareCrc;
64 } else {
65 index = args.indexOf(COMPAREALL);
66 action = OP_DFU::actionCompareAll;
68 } else if (args.contains(DOWNLOAD)) {
69 int index;
70 index = args.indexOf(DOWNLOAD);
71 action = OP_DFU::actionDownload;
72 } else if (args.contains(STATUSREQUEST)) {
73 action = OP_DFU::actionStatusReq;
74 } else if (args.contains(RESET)) {
75 action = OP_DFU::actionReset;
76 } else if (args.contains(JUMP)) {
77 action = OP_DFU::actionJump;
78 } else if (args.contains(LISTDEVICES)) {
79 action = OP_DFU::actionListDevs;
81 if ((file.isEmpty() || device == -1) && action != OP_DFU::actionReset && action != OP_DFU::actionStatusReq && action != OP_DFU::actionListDevs && action != OP_DFU::actionJump) {
82 usage(&standardOutput);
83 return -1;
85 if (args.contains(USE_SERIAL)) {
86 if (args.indexOf(USE_SERIAL) + 1 < args.length()) {
87 serialport = (args[args.indexOf(USE_SERIAL) + 1]);
88 use_serial = true;
91 if (debug) {
92 qDebug() << "Action=" << (int)action << endl;
93 qDebug() << "File=" << file << endl;
94 qDebug() << "Device=" << device << endl;
95 qDebug() << "Action=" << action << endl;
96 qDebug() << "Desctription" << description << endl;
97 qDebug() << "Use Serial port" << use_serial << endl;
98 if (use_serial) {
99 qDebug() << "Port Name" << serialport << endl;
102 if (use_serial) {
103 if (args.contains(NO_COUNTDOWN)) {} else {
104 standardOutput << "Connect the board" << endl;
105 label = "";
106 for (int i = 0; i < 6; i++) {
107 progressUpdated(i * 100 / 5);
108 QThread::msleep(500);
111 standardOutput << endl << "Connect the board NOW" << endl;
112 QThread::msleep(1000);
114 ///////////////////////////////////ACTIONS START///////////////////////////////////////////////////
115 OP_DFU::DFUObject dfu(debug, use_serial, serialport);
117 QObject::connect(&dfu, &OP_DFU::DFUObject::operationProgress, showProgress);
118 QObject::connect(&dfu, &OP_DFU::DFUObject::progressUpdated, progressUpdated);
120 if (!dfu.ready()) {
121 return -1;
123 dfu.AbortOperation();
124 if (!dfu.enterDFU(0)) {
125 standardOutput << "Could not enter DFU mode\n" << endl;
126 return -1;
128 if (debug) {
129 OP_DFU::Status ret = dfu.StatusRequest();
130 qDebug() << dfu.StatusToString(ret);
132 if (!(action == OP_DFU::actionStatusReq || action == OP_DFU::actionReset || action == OP_DFU::actionJump)) {
133 dfu.findDevices();
134 if (action == OP_DFU::actionListDevs) {
135 standardOutput << "Found " << dfu.numberOfDevices << "\n" << endl;
136 for (int x = 0; x < dfu.numberOfDevices; ++x) {
137 standardOutput << "Device #" << x << "\n" << endl;
138 standardOutput << "Device ID=" << dfu.devices[x].ID << "\n" << endl;
139 standardOutput << "Device Readable=" << dfu.devices[x].Readable << "\n" << endl;
140 standardOutput << "Device Writable=" << dfu.devices[x].Writable << "\n" << endl;
141 standardOutput << "Device SizeOfCode=" << dfu.devices[x].SizeOfCode << "\n" << endl;
142 standardOutput << "BL Version=" << dfu.devices[x].BL_Version << "\n" << endl;
143 standardOutput << "Device SizeOfDesc=" << dfu.devices[x].SizeOfDesc << "\n" << endl;
144 standardOutput << "FW CRC=" << dfu.devices[x].FW_CRC << "\n";
146 int size = ((OP_DFU::device)dfu.devices[x]).SizeOfDesc;
147 dfu.enterDFU(x);
148 standardOutput << "Description:" << dfu.DownloadDescription(size).toLatin1().data() << "\n" << endl;
149 standardOutput << "\n";
151 return 0;
153 if (device > dfu.numberOfDevices - 1) {
154 standardOutput << "Error:Invalid Device" << endl;
155 return -1;
157 if (dfu.numberOfDevices == 1) {
158 dfu.use_delay = false;
160 if (!dfu.enterDFU(device)) {
161 standardOutput << "Error:Could not enter DFU mode\n" << endl;
162 return -1;
164 if (action == OP_DFU::actionProgram) {
165 if (((OP_DFU::device)dfu.devices[device]).Writable == false) {
166 standardOutput << "ERROR device not Writable\n" << endl;
167 return false;
169 standardOutput << "Uploading..." << endl;
170 bool retstatus = dfu.UploadFirmware(file.toLatin1(), verify, device);
172 if (!retstatus) {
173 standardOutput << "Upload failed with code:" << retstatus << endl;
174 return -1;
176 while (!dfu.isFinished()) {
177 QThread::msleep(500);
179 if (file.endsWith("opfw")) {
180 QByteArray firmware;
181 QFile fwfile(file);
182 if (!fwfile.open(QIODevice::ReadOnly)) {
183 standardOutput << "Cannot open file " << file << endl;
184 return -1;
186 firmware = fwfile.readAll();
187 QByteArray desc = firmware.right(100);
188 OP_DFU::Status status = dfu.UploadDescription(desc);
189 if (status != OP_DFU::Last_operation_Success) {
190 standardOutput << "Upload failed with code:" << retstatus << endl;
191 return -1;
193 } else if (!description.isEmpty()) {
194 OP_DFU::Status status = dfu.UploadDescription(description);
195 if (status != OP_DFU::Last_operation_Success) {
196 standardOutput << "Upload failed with code:" << status << endl;
197 return -1;
200 while (!dfu.isFinished()) {
201 QThread::msleep(500);
203 standardOutput << "Uploading Succeded!\n" << endl;
204 } else if (action == OP_DFU::actionDownload) {
205 if (((OP_DFU::device)dfu.devices[device]).Readable == false) {
206 standardOutput << "ERROR device not readable\n" << endl;
207 return false;
209 QByteArray fw;
210 dfu.DownloadFirmware(&fw, 0);
211 bool ret = dfu.SaveByteArrayToFile(file.toLatin1(), fw);
212 return ret;
213 } else if (action == OP_DFU::actionCompareCrc) {
214 dfu.CompareFirmware(file.toLatin1(), OP_DFU::crccompare, device);
215 return 1;
216 } else if (action == OP_DFU::actionCompareAll) {
217 if (((OP_DFU::device)dfu.devices[device]).Readable == false) {
218 standardOutput << "ERROR device not readable\n" << endl;
219 return false;
221 dfu.CompareFirmware(file.toLatin1(), OP_DFU::bytetobytecompare, device);
222 return 1;
224 } else if (action == OP_DFU::actionStatusReq) {
225 standardOutput << "Current device status=" << dfu.StatusToString(dfu.StatusRequest()).toLatin1().data() << "\n" << endl;
226 } else if (action == OP_DFU::actionReset) {
227 dfu.ResetDevice();
228 } else if (action == OP_DFU::actionJump) {
229 dfu.JumpToApp(false, false);
231 return 0;
233 // return a.exec();
236 void showProgress(QString status)
238 QTextStream standardOutput(stdout);
240 standardOutput << status << endl;
241 label = status;
244 void progressUpdated(int percent)
246 std::string bar;
248 for (int i = 0; i < 50; i++) {
249 if (i < (percent / 2)) {
250 bar.replace(i, 1, "=");
251 } else if (i == (percent / 2)) {
252 bar.replace(i, 1, ">");
253 } else {
254 bar.replace(i, 1, " ");
257 std::cout << "\r" << label.toLatin1().data() << "[" << bar << "] ";
258 std::cout.width(3);
259 std::cout << percent << "% " << std::flush;
261 void usage(QTextStream *standardOutput)
263 *standardOutput << "_________________________________________________________________________\n";
264 *standardOutput << "| Commands |\n";
265 *standardOutput << "| |\n";
266 *standardOutput << "| -ls : lists available devices |\n";
267 *standardOutput << "| -p <file> : program hw (requires:-d - optionals:-v,-w) |\n";
268 *standardOutput << "| -v : verify (requires:-d) |\n";
269 *standardOutput << "| -dn <file> : download firmware to file |\n";
270 // *standardOutput << "| -dd <file> : download discription (requires:-d) |\n";
271 *standardOutput << "| -d <Device Number> : target device number (default 0, first device) |\n";
272 // *standardOutput << "| -w <description> : (requires: -p) |\n";
273 *standardOutput << "| -ca <file> : compares byte by byte current firmware with file|\n";
274 *standardOutput << "| -cc <file> : compares CRC of current firmware with file |\n";
275 *standardOutput << "| -s : requests status of device |\n";
276 *standardOutput << "| -r : resets the device |\n";
277 *standardOutput << "| -j : exits bootloader and jumps to user FW |\n";
278 *standardOutput << "| -debug : prints debug information |\n";
279 *standardOutput << "| -t <port> : uses serial port |\n";
280 *standardOutput << "| -i : immediate, doesn't show the connection countdown|\n";
281 // *standardOutput << "| -ur <port> : user mode reset* |\n";
282 *standardOutput << "| |\n";
283 *standardOutput << "| examples: |\n";
284 *standardOutput << "| |\n";
285 *standardOutput << "| program and verify the fist device device connected to COM1 |\n";
286 *standardOutput << "| OPUploadTool -p c:/gpsp.opfw -v -t COM1 |\n";
287 *standardOutput << "| |\n";
288 *standardOutput << "| program and verify the fist device device connected to COM1 |\n";
289 *standardOutput << "| OPUploadTool -p c:/gpsp.opfw -v -t COM1 |\n";
290 *standardOutput << "| |\n";
291 *standardOutput << "| Perform a quick compare of FW in file with FW in device #1 |\n";
292 *standardOutput << "| OPUploadTool -ch /home/user1/gpsp.opfw -t ttyUSB0 |\n";
293 *standardOutput << "| |\n";
294 // *standardOutput << "| *requires valid user space firmwares already running |\n";
295 *standardOutput << "|________________________________________________________________________|\n";
296 *standardOutput << endl;
299 void howToUsage(QTextStream *standardOutput)
301 *standardOutput << "run the tool with -? for more informations" << endl;