/
DFUUploader.java
345 lines (281 loc) · 10.5 KB
/
DFUUploader.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
DFUUploader - uploader implementation using DFU
Copyright (c) 2010
Andrew Meyer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package CliTemplate;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import processing.app.Preferences;
import processing.app.Serial;
import processing.app.debug.MessageConsumer;
import processing.app.debug.MessageSiphon;
import processing.app.debug.RunnerException;
/**
*
* @author bob
*/
public class DFUUploader implements MessageConsumer {
boolean firstErrorFound;
boolean secondErrorFound;
// part of the PdeMessageConsumer interface
boolean notFoundError;
boolean verbose;
RunnerException exception;
static final String SUPER_BADNESS =
"Compiler error!";
public boolean uploadUsingPreferences(String binPath, boolean verbose)
throws RunnerException {
this.verbose = verbose;
return uploadViaDFU(binPath);
}
// works with old and new versions of dfu-util
private boolean found_device (String dfuData, String usbID)
{
return dfuData.contains(("Found DFU: [0x"+usbID.substring(0,4)).toUpperCase()) ||
dfuData.contains(("Found DFU: ["+usbID.substring(0,4)).toUpperCase());
}
public boolean uploadViaDFU (String binPath)
throws RunnerException {
this.verbose = Preferences.getBoolean ("upload.verbose");
/* todo, check for size overruns! */
String fileType="bin";
if (fileType.equals("bin")) {
String usbID = Preferences.get("upload.usbID");
if (usbID == null) {
/* fall back on default */
/* this isnt great because is default Avrdude or dfu-util? */
usbID = Preferences.get("upload.usbID");
}
/* if auto-reset, then emit the reset pulse on dtr/rts */
if (Preferences.get("upload.auto_reset") != null) {
if (Preferences.get("upload.auto_reset").toLowerCase().equals("true")) {
System.out.println("Resetting to bootloader via DTR pulse");
emitResetPulse();
}
} else {
System.out.println("Resetting to bootloader via DTR pulse");
emitResetPulse();
}
String dfuList = new String();
List commandCheck = new ArrayList();
commandCheck.add("dfu-util");
commandCheck.add("-l");
long startChecking = System.currentTimeMillis();
System.out.println("Searching for DFU device [" + usbID + "]...");
do {
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
dfuList = executeCheckCommand(commandCheck);
//System.out.println(dfuList);
}
while ( !found_device (dfuList.toUpperCase(), usbID) && (System.currentTimeMillis() - startChecking < 7000));
if ( !found_device (dfuList.toUpperCase(), usbID) )
{
System.out.println(dfuList);
System.err.println("Couldn't find the DFU device: [" + usbID + "]");
return false;
}
System.out.println("Found it!");
/* todo, add handle to let user choose altIf at upload time! */
String altIf = Preferences.get("upload.altID");
List commandDownloader = new ArrayList();
commandDownloader.add("dfu-util");
commandDownloader.add("-a "+altIf);
commandDownloader.add("-R");
commandDownloader.add("-d "+usbID);
commandDownloader.add("-D"+ binPath); //"./thisbin.bin");
return executeUploadCommand(commandDownloader);
}
System.err.println("Only .bin files are supported at this time");
return false;
}
/* we need to ensure both RTS and DTR are low to start,
then pulse DTR on its own. This is the reset signal
maple responds to
*/
private void emitResetPulse() throws RunnerException {
/* wait a while for the device to reboot */
int programDelay = Preferences.getInteger("programDelay");
try {
Serial serialPort = new Serial();
// try to toggle DTR/RTS (old scheme)
serialPort.setRTS(false);
serialPort.setDTR(false);
serialPort.setDTR(true);
try {
Thread.sleep(50);
} catch (InterruptedException e) {}
serialPort.setDTR(false);
// try magic number
serialPort.setRTS(true);
serialPort.setDTR(true);
try {
Thread.sleep(50);
} catch (InterruptedException e) {}
serialPort.setDTR(false);
try {
Thread.sleep(50);
} catch (InterruptedException e) {}
serialPort.write("1EAF");
try {
Thread.sleep(50);
} catch (InterruptedException e) {}
serialPort.dispose();
} catch(Exception e) {
System.err.println("Reset via USB Serial Failed! Did you select the right serial port?\nAssuming the board is in perpetual bootloader mode and continuing to attempt dfu programming...\n");
}
}
protected String executeCheckCommand(Collection commandDownloader)
throws RunnerException
{
firstErrorFound = false; // haven't found any errors yet
secondErrorFound = false;
notFoundError = false;
int result=0; // pre-initialized to quiet a bogus warning from jikes
String userdir = System.getProperty("user.dir") + File.separator;
String returnStr = new String();
try {
String[] commandArray = new String[commandDownloader.size()];
commandDownloader.toArray(commandArray);
String armBasePath;
//armBasePath = new String(Base.getHardwarePath() + "/tools/arm/bin/");
armBasePath = "";
commandArray[0] = armBasePath + commandArray[0];
if (verbose || Preferences.getBoolean("upload.verbose")) {
for(int i = 0; i < commandArray.length; i++) {
System.out.print(commandArray[i] + " ");
}
System.out.println();
}
Process process = Runtime.getRuntime().exec(commandArray);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
// wait for the process to finish. if interrupted
// before waitFor returns, continue waiting
//
boolean busy = true;
while (busy) {
try {
result = process.waitFor();
busy = false;
} catch (InterruptedException intExc) {
}
}
String s;
while ((s = stdInput.readLine()) != null) {
returnStr += s + "\n";
}
process.destroy();
if(exception!=null) {
exception.hideStackTrace();
throw exception;
}
if(result!=0) return "Error!";
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println("result2 is "+result);
// if the result isn't a known, expected value it means that something
// is fairly wrong, one possibility is that jikes has crashed.
//
if (exception != null) throw exception;
if ((result != 0) && (result != 1 )) {
exception = new RunnerException(SUPER_BADNESS);
}
return returnStr; // ? true : false;
}
// Need to overload this from Uploader to use the system-wide dfu-util
protected boolean executeUploadCommand(Collection commandDownloader)
throws RunnerException
{
firstErrorFound = false; // haven't found any errors yet
secondErrorFound = false;
notFoundError = false;
int result=0; // pre-initialized to quiet a bogus warning from jikes
String userdir = System.getProperty("user.dir") + File.separator;
try {
String[] commandArray = new String[commandDownloader.size()];
commandDownloader.toArray(commandArray);
String armBasePath;
//armBasePath = new String(Base.getHardwarePath() + "/tools/arm/bin/");
armBasePath = "";
commandArray[0] = armBasePath + commandArray[0];
if (verbose || Preferences.getBoolean("upload.verbose")) {
for(int i = 0; i < commandArray.length; i++) {
System.out.print(commandArray[i] + " ");
}
System.out.println();
}
Process process = Runtime.getRuntime().exec(commandArray);
new MessageSiphon(process.getInputStream(), this);
new MessageSiphon(process.getErrorStream(), this);
// wait for the process to finish. if interrupted
// before waitFor returns, continue waiting
//
boolean compiling = true;
while (compiling) {
try {
result = process.waitFor();
compiling = false;
} catch (InterruptedException intExc) {
}
}
if(exception!=null) {
exception.hideStackTrace();
throw exception;
}
if(result!=0)
return false;
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println("result2 is "+result);
// if the result isn't a known, expected value it means that something
// is fairly wrong, one possibility is that jikes has crashed.
//
if (exception != null) throw exception;
if ((result != 0) && (result != 1 )) {
exception = new RunnerException(SUPER_BADNESS);
//editor.error(exception);
//PdeBase.openURL(BUGS_URL);
//throw new PdeException(SUPER_BADNESS);
}
return (result == 0); // ? true : false;
}
// deal with messages from dfu-util...
public void message(String s) {
if(s.indexOf("dfu-util - (C) ") != -1) { return; }
if(s.indexOf("This program is Free Software and has ABSOLUTELY NO WARRANTY") != -1) { return; }
if(s.indexOf("No DFU capable USB device found") != -1) {
System.err.print(s);
exception = new RunnerException("Problem uploading via dfu-util: No Maple found");
return;
}
if(s.indexOf("Operation not perimitted") != -1) {
System.err.print(s);
exception = new RunnerException("Problem uploading via dfu-util: Insufficient privilages");
return;
}
// else just print everything...
System.out.print(s);
}
}