1
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
4
AvrdudeUploader - uploader implementation using avrdude
5
Part of the Arduino project - http://www.arduino.cc/
10
This program is free software; you can redistribute it and/or modify
11
it under the terms of the GNU General Public License as published by
12
the Free Software Foundation; either version 2 of the License, or
13
(at your option) any later version.
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software Foundation,
22
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
$Id: AvrdudeUploader.java 804 2009-12-18 16:05:52Z dmellis $
27
package processing.app.debug;
29
import processing.app.Base;
30
import processing.app.Preferences;
31
import processing.app.Serial;
35
import java.util.zip.*;
40
public class AvrdudeUploader extends Uploader {
41
public AvrdudeUploader() {
44
// XXX: add support for uploading sketches using a programmer
45
public boolean uploadUsingPreferences(String buildPath, String className, boolean verbose)
46
throws RunnerException {
47
this.verbose = verbose;
48
Map<String, String> boardPreferences = Base.getBoardPreferences();
49
String uploadUsing = boardPreferences.get("upload.using");
50
if (uploadUsing == null) {
51
// fall back on global preference
52
uploadUsing = Preferences.get("upload.using");
54
if (uploadUsing.equals("bootloader")) {
55
return uploadViaBootloader(buildPath, className);
57
// XXX: this needs to handle programmers in other targets.
58
Collection params = getProgrammerCommands(Base.getTarget().getName(), uploadUsing);
59
params.add("-Uflash:w:" + buildPath + File.separator + className + ".hex:i");
60
return avrdude(params);
64
private boolean uploadViaBootloader(String buildPath, String className)
65
throws RunnerException {
66
Map<String, String> boardPreferences = Base.getBoardPreferences();
67
List commandDownloader = new ArrayList();
68
String protocol = boardPreferences.get("upload.protocol");
70
// avrdude wants "stk500v1" to distinguish it from stk500v2
71
if (protocol.equals("stk500"))
72
protocol = "stk500v1";
73
commandDownloader.add("-c" + protocol);
74
commandDownloader.add(
75
"-P" + (Base.isWindows() ? "\\\\.\\" : "") + Preferences.get("serial.port"));
76
commandDownloader.add(
77
"-b" + Integer.parseInt(boardPreferences.get("upload.speed")));
78
commandDownloader.add("-D"); // don't erase
79
commandDownloader.add("-Uflash:w:" + buildPath + File.separator + className + ".hex:i");
81
if (boardPreferences.get("upload.disable_flushing") == null ||
82
boardPreferences.get("upload.disable_flushing").toLowerCase().equals("false")) {
86
return avrdude(commandDownloader);
89
public boolean burnBootloader(String target, String programmer) throws RunnerException {
90
return burnBootloader(getProgrammerCommands(target, programmer));
93
private Collection getProgrammerCommands(String targetName, String programmer) {
94
Target target = Base.targetsTable.get(targetName);
95
Map<String, String> programmerPreferences = target.getProgrammers().get(programmer);
96
List params = new ArrayList();
97
params.add("-c" + programmerPreferences.get("protocol"));
99
if ("usb".equals(programmerPreferences.get("communication"))) {
101
} else if ("serial".equals(programmerPreferences.get("communication"))) {
102
params.add("-P" + (Base.isWindows() ? "\\\\.\\" : "") + Preferences.get("serial.port"));
103
if (programmerPreferences.get("speed") != null) {
104
params.add("-b" + Integer.parseInt(programmerPreferences.get("speed")));
107
// XXX: add support for specifying the port address for parallel
108
// programmers, although avrdude has a default that works in most cases.
110
if (programmerPreferences.get("force") != null &&
111
programmerPreferences.get("force").toLowerCase().equals("true"))
114
if (programmerPreferences.get("delay") != null)
115
params.add("-i" + programmerPreferences.get("delay"));
120
protected boolean burnBootloader(Collection params)
121
throws RunnerException {
122
Map<String, String> boardPreferences = Base.getBoardPreferences();
123
List fuses = new ArrayList();
124
fuses.add("-e"); // erase the chip
125
fuses.add("-Ulock:w:" + boardPreferences.get("bootloader.unlock_bits") + ":m");
126
if (boardPreferences.get("bootloader.extended_fuses") != null)
127
fuses.add("-Uefuse:w:" + boardPreferences.get("bootloader.extended_fuses") + ":m");
128
fuses.add("-Uhfuse:w:" + boardPreferences.get("bootloader.high_fuses") + ":m");
129
fuses.add("-Ulfuse:w:" + boardPreferences.get("bootloader.low_fuses") + ":m");
131
if (!avrdude(params, fuses))
136
} catch (InterruptedException e) {}
139
String bootloaderPath = boardPreferences.get("bootloader.path");
141
if (bootloaderPath.indexOf(':') == -1) {
142
t = Base.getTarget(); // the current target (associated with the board)
144
String targetName = bootloaderPath.substring(0, bootloaderPath.indexOf(':'));
145
t = Base.targetsTable.get(targetName);
146
bootloaderPath = bootloaderPath.substring(bootloaderPath.indexOf(':') + 1);
149
File bootloadersFile = new File(t.getFolder(), "bootloaders");
150
File bootloaderFile = new File(bootloadersFile, bootloaderPath);
151
bootloaderPath = bootloaderFile.getAbsolutePath();
153
List bootloader = new ArrayList();
154
bootloader.add("-Uflash:w:" + bootloaderPath + File.separator +
155
boardPreferences.get("bootloader.file") + ":i");
156
bootloader.add("-Ulock:w:" + boardPreferences.get("bootloader.lock_bits") + ":m");
158
return avrdude(params, bootloader);
161
public boolean avrdude(Collection p1, Collection p2) throws RunnerException {
162
ArrayList p = new ArrayList(p1);
167
public boolean avrdude(Collection params) throws RunnerException {
168
List commandDownloader = new ArrayList();
169
commandDownloader.add("avrdude");
171
// Point avrdude at its config file since it's in a non-standard location.
172
if (Base.isLinux()) {
173
// ???: is it better to have Linux users install avrdude themselves, in
174
// a way that it can find its own configuration file?
175
commandDownloader.add("-C" + Base.getHardwarePath() + "/tools/avrdude.conf");
177
commandDownloader.add("-C" + Base.getHardwarePath() + "/tools/avr/etc/avrdude.conf");
180
if (verbose || Preferences.getBoolean("upload.verbose")) {
181
commandDownloader.add("-v");
182
commandDownloader.add("-v");
183
commandDownloader.add("-v");
184
commandDownloader.add("-v");
186
commandDownloader.add("-q");
187
commandDownloader.add("-q");
189
commandDownloader.add("-p" + Base.getBoardPreferences().get("build.mcu"));
190
commandDownloader.addAll(params);
192
return executeUploadCommand(commandDownloader);