1 #include <QtCore/QCoreApplication>
6 void showProgress(QString status
);
7 void progressUpdated(int percent
);
8 void usage(QTextStream
*standardOutput
);
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;
18 bool umodereset
= false;
19 OP_DFU::Actions action
;
24 QStringList args
= QCoreApplication::arguments();
26 if (args
.contains("-debug")) {
29 if (args
.contains("-ur")) {
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();
44 if (argumentCount
== 0 || args
.contains("-?") || !args
.contains(USE_SERIAL
)) {
45 usage(&standardOutput
);
47 } else if (args
.contains(PROGRAMFW
)) {
48 if (args
.contains(VERIFY
)) {
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
)) {
61 if (args
.contains(COMPARECRC
)) {
62 index
= args
.indexOf(COMPARECRC
);
63 action
= OP_DFU::actionCompareCrc
;
65 index
= args
.indexOf(COMPAREALL
);
66 action
= OP_DFU::actionCompareAll
;
68 } else if (args
.contains(DOWNLOAD
)) {
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
);
85 if (args
.contains(USE_SERIAL
)) {
86 if (args
.indexOf(USE_SERIAL
) + 1 < args
.length()) {
87 serialport
= (args
[args
.indexOf(USE_SERIAL
) + 1]);
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
;
99 qDebug() << "Port Name" << serialport
<< endl
;
103 if (args
.contains(NO_COUNTDOWN
)) {} else {
104 standardOutput
<< "Connect the board" << endl
;
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
);
123 dfu
.AbortOperation();
124 if (!dfu
.enterDFU(0)) {
125 standardOutput
<< "Could not enter DFU mode\n" << endl
;
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
)) {
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
;
148 standardOutput
<< "Description:" << dfu
.DownloadDescription(size
).toLatin1().data() << "\n" << endl
;
149 standardOutput
<< "\n";
153 if (device
> dfu
.numberOfDevices
- 1) {
154 standardOutput
<< "Error:Invalid Device" << endl
;
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
;
164 if (action
== OP_DFU::actionProgram
) {
165 if (((OP_DFU::device
)dfu
.devices
[device
]).Writable
== false) {
166 standardOutput
<< "ERROR device not Writable\n" << endl
;
169 standardOutput
<< "Uploading..." << endl
;
170 bool retstatus
= dfu
.UploadFirmware(file
.toLatin1(), verify
, device
);
173 standardOutput
<< "Upload failed with code:" << retstatus
<< endl
;
176 while (!dfu
.isFinished()) {
177 QThread::msleep(500);
179 if (file
.endsWith("opfw")) {
182 if (!fwfile
.open(QIODevice::ReadOnly
)) {
183 standardOutput
<< "Cannot open file " << file
<< endl
;
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
;
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
;
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
;
210 dfu
.DownloadFirmware(&fw
, 0);
211 bool ret
= dfu
.SaveByteArrayToFile(file
.toLatin1(), fw
);
213 } else if (action
== OP_DFU::actionCompareCrc
) {
214 dfu
.CompareFirmware(file
.toLatin1(), OP_DFU::crccompare
, device
);
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
;
221 dfu
.CompareFirmware(file
.toLatin1(), OP_DFU::bytetobytecompare
, device
);
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
) {
228 } else if (action
== OP_DFU::actionJump
) {
229 dfu
.JumpToApp(false, false);
236 void showProgress(QString status
)
238 QTextStream
standardOutput(stdout
);
240 standardOutput
<< status
<< endl
;
244 void progressUpdated(int percent
)
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, ">");
254 bar
.replace(i
, 1, " ");
257 std::cout
<< "\r" << label
.toLatin1().data() << "[" << bar
<< "] ";
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
;