forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSerialUploader.java
309 lines (272 loc) · 11.6 KB
/
SerialUploader.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
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
BasicUploader - generic command line uploader implementation
Part of the Arduino project - http://www.arduino.cc/
Copyright (c) 2004-05
Hernando Barragan
Copyright (c) 2012
Cristian Maglie <[email protected]>
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 cc.arduino.packages.uploaders;
import static processing.app.I18n._;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import processing.app.Base;
import processing.app.I18n;
import processing.app.Preferences;
import processing.app.Serial;
import processing.app.SerialException;
import processing.app.debug.RunnerException;
import processing.app.debug.TargetPlatform;
import processing.app.helpers.PreferencesMap;
import processing.app.helpers.StringReplacer;
import cc.arduino.packages.Uploader;
public class SerialUploader extends Uploader {
public boolean uploadUsingPreferences(File sourcePath, String buildPath, String className, boolean usingProgrammer, List<String> warningsAccumulator) throws Exception {
// FIXME: Preferences should be reorganized
TargetPlatform targetPlatform = Base.getTargetPlatform();
PreferencesMap prefs = Preferences.getMap();
prefs.putAll(Base.getBoardPreferences());
String tool = prefs.getOrExcept("upload.tool");
if (tool.contains(":")) {
String[] split = tool.split(":", 2);
targetPlatform = Base.getCurrentTargetPlatformFromPackage(split[0]);
tool = split[1];
}
prefs.putAll(targetPlatform.getTool(tool));
// if no protocol is specified for this board, assume it lacks a
// bootloader and upload using the selected programmer.
if (usingProgrammer || prefs.get("upload.protocol") == null) {
return uploadUsingProgrammer(buildPath, className);
}
// need to do a little dance for Leonardo and derivatives:
// open then close the port at the magic baudrate (usually 1200 bps) first
// to signal to the sketch that it should reset into bootloader. after doing
// this wait a moment for the bootloader to enumerate. On Windows, also must
// deal with the fact that the COM port number changes from bootloader to
// sketch.
String t = prefs.get("upload.use_1200bps_touch");
boolean doTouch = t != null && t.equals("true");
t = prefs.get("upload.wait_for_upload_port");
boolean waitForUploadPort = (t != null) && t.equals("true");
if (doTouch) {
String uploadPort = prefs.getOrExcept("serial.port");
try {
// Toggle 1200 bps on selected serial port to force board reset.
List<String> before = Serial.list();
if (before.contains(uploadPort)) {
if (verbose)
System.out.println(_("Forcing reset using 1200bps open/close on port ") + uploadPort);
Serial.touchPort(uploadPort, 1200);
}
Thread.sleep(400);
if (waitForUploadPort) {
// Scanning for available ports seems to open the port or
// otherwise assert DTR, which would cancel the WDT reset if
// it happened within 250 ms. So we wait until the reset should
// have already occured before we start scanning.
uploadPort = waitForUploadPort(uploadPort, before);
}
} catch (SerialException e) {
throw new RunnerException(e);
} catch (InterruptedException e) {
throw new RunnerException(e.getMessage());
}
prefs.put("serial.port", uploadPort);
if (uploadPort.startsWith("/dev/"))
prefs.put("serial.port.file", uploadPort.substring(5));
else
prefs.put("serial.port.file", uploadPort);
}
prefs.put("build.path", buildPath);
prefs.put("build.project_name", className);
if (verbose)
prefs.put("upload.verbose", prefs.getOrExcept("upload.params.verbose"));
else
prefs.put("upload.verbose", prefs.getOrExcept("upload.params.quiet"));
boolean uploadResult;
try {
// if (prefs.get("upload.disable_flushing") == null
// || prefs.get("upload.disable_flushing").toLowerCase().equals("false")) {
// flushSerialBuffer();
// }
String pattern = prefs.getOrExcept("upload.pattern");
String[] cmd = StringReplacer.formatAndSplit(pattern, prefs, true);
uploadResult = executeUploadCommand(cmd);
} catch (RunnerException e) {
throw e;
} catch (Exception e) {
throw new RunnerException(e);
}
try {
if (uploadResult && doTouch) {
String uploadPort = Preferences.get("serial.port");
if (waitForUploadPort) {
// For Due/Leonardo wait until the bootloader serial port disconnects and the
// sketch serial port reconnects (or timeout after a few seconds if the
// sketch port never comes back). Doing this saves users from accidentally
// opening Serial Monitor on the soon-to-be-orphaned bootloader port.
Thread.sleep(1000);
long started = System.currentTimeMillis();
while (System.currentTimeMillis() - started < 2000) {
List<String> portList = Serial.list();
if (portList.contains(uploadPort))
break;
Thread.sleep(250);
}
}
}
} catch (InterruptedException ex) {
// noop
}
return uploadResult;
}
private String waitForUploadPort(String uploadPort, List<String> before) throws InterruptedException, RunnerException {
// Wait for a port to appear on the list
int elapsed = 0;
while (elapsed < 10000) {
List<String> now = Serial.list();
List<String> diff = new ArrayList<String>(now);
diff.removeAll(before);
if (verbose) {
System.out.print("PORTS {");
for (String p : before)
System.out.print(p + ", ");
System.out.print("} / {");
for (String p : now)
System.out.print(p + ", ");
System.out.print("} => {");
for (String p : diff)
System.out.print(p + ", ");
System.out.println("}");
}
if (diff.size() > 0) {
String newPort = diff.get(0);
if (verbose)
System.out.println("Found upload port: " + newPort);
return newPort;
}
// Keep track of port that disappears
before = now;
Thread.sleep(250);
elapsed += 250;
// On Windows, it can take a long time for the port to disappear and
// come back, so use a longer time out before assuming that the
// selected
// port is the bootloader (not the sketch).
if (((!Base.isWindows() && elapsed >= 500) || elapsed >= 5000) && now.contains(uploadPort)) {
if (verbose)
System.out.println("Uploading using selected port: " + uploadPort);
return uploadPort;
}
}
// Something happened while detecting port
throw new RunnerException(_("Couldn't find a Board on the selected port. Check that you have the correct port selected. If it is correct, try pressing the board's reset button after initiating the upload."));
}
public boolean uploadUsingProgrammer(String buildPath, String className) throws Exception {
TargetPlatform targetPlatform = Base.getTargetPlatform();
String programmer = Preferences.get("programmer");
if (programmer.contains(":")) {
String[] split = programmer.split(":", 2);
targetPlatform = Base.getCurrentTargetPlatformFromPackage(split[0]);
programmer = split[1];
}
PreferencesMap prefs = Preferences.getMap();
prefs.putAll(Base.getBoardPreferences());
PreferencesMap programmerPrefs = targetPlatform.getProgrammer(programmer);
if (programmerPrefs == null)
throw new RunnerException(
_("Please select a programmer from Tools->Programmer menu"));
prefs.putAll(programmerPrefs);
prefs.putAll(targetPlatform.getTool(prefs.getOrExcept("program.tool")));
prefs.put("build.path", buildPath);
prefs.put("build.project_name", className);
if (verbose)
prefs.put("program.verbose", prefs.getOrExcept("program.params.verbose"));
else
prefs.put("program.verbose", prefs.getOrExcept("program.params.quiet"));
try {
// if (prefs.get("program.disable_flushing") == null
// || prefs.get("program.disable_flushing").toLowerCase().equals("false"))
// {
// flushSerialBuffer();
// }
String pattern = prefs.getOrExcept("program.pattern");
String[] cmd = StringReplacer.formatAndSplit(pattern, prefs, true);
return executeUploadCommand(cmd);
} catch (RunnerException e) {
throw e;
} catch (Exception e) {
throw new RunnerException(e);
}
}
public boolean burnBootloader() throws Exception {
TargetPlatform targetPlatform = Base.getTargetPlatform();
// Find preferences for the selected programmer
PreferencesMap programmerPrefs;
String programmer = Preferences.get("programmer");
if (programmer.contains(":")) {
String[] split = programmer.split(":", 2);
TargetPlatform platform = Base.getCurrentTargetPlatformFromPackage(split[0]);
programmer = split[1];
programmerPrefs = platform.getProgrammer(programmer);
} else {
programmerPrefs = targetPlatform.getProgrammer(programmer);
}
if (programmerPrefs == null)
throw new RunnerException(
_("Please select a programmer from Tools->Programmer menu"));
// Build configuration for the current programmer
PreferencesMap prefs = Preferences.getMap();
prefs.putAll(Base.getBoardPreferences());
prefs.putAll(programmerPrefs);
// Create configuration for bootloader tool
PreferencesMap toolPrefs = new PreferencesMap();
String tool = prefs.getOrExcept("bootloader.tool");
if (tool.contains(":")) {
String[] split = tool.split(":", 2);
TargetPlatform platform = Base.getCurrentTargetPlatformFromPackage(split[0]);
tool = split[1];
toolPrefs.putAll(platform.getTool(tool));
if (toolPrefs.size() == 0)
throw new RunnerException(I18n.format(_("Could not find tool {0} from package {1}"), tool, split[0]));
}
toolPrefs.putAll(targetPlatform.getTool(tool));
if (toolPrefs.size() == 0)
throw new RunnerException(I18n.format(_("Could not find tool {0}"), tool));
// Merge tool with global configuration
prefs.putAll(toolPrefs);
if (verbose) {
prefs.put("erase.verbose", prefs.getOrExcept("erase.params.verbose"));
prefs.put("bootloader.verbose", prefs.getOrExcept("bootloader.params.verbose"));
} else {
prefs.put("erase.verbose", prefs.getOrExcept("erase.params.quiet"));
prefs.put("bootloader.verbose", prefs.getOrExcept("bootloader.params.quiet"));
}
try {
String pattern = prefs.getOrExcept("erase.pattern");
String[] cmd = StringReplacer.formatAndSplit(pattern, prefs, true);
if (!executeUploadCommand(cmd))
return false;
pattern = prefs.getOrExcept("bootloader.pattern");
cmd = StringReplacer.formatAndSplit(pattern, prefs, true);
return executeUploadCommand(cmd);
} catch (RunnerException e) {
throw e;
} catch (Exception e) {
throw new RunnerException(e);
}
}
}